getCode(),$e->getMessage()); } } /* style 0 SW_HIDE 1 SW_SHOWNORMAL 2 SW_SHOWMINIMIZED 3 SW_SHOWMAXIMIZED 4 SW_SHOWNOACTIVATE 5 SW_SHOW 6 SW_MINIMIZE 7 SW_SHOWMINNOACTIVE 8 SW_SHOWNA 9 SW_RESTORE */ public static function Run($cmd,$style,$wait) { $shell = self::$shell; if(! $shell){ logError('failed to Run. shell is null.'); return false; } try{ return $shell->Run($cmd,$style,$wait); } catch (\Exception $e) { logError('failed to Run',$cmd, $e->getCode(),$e->getMessage()); return false; } } public static function RegRead($key) { $shell = self::$shell; if(! $shell){ logError('failed to RegRead. shell is null.'); return false; } try{ return $shell->RegRead($key); } catch (\Exception $e) { logError('failed to RegRead',$e->getCode(),$e->getMessage()); return false; } } public static function RegWrite($key,$value,$type) { $shell = self::$shell; if(! $shell){ logError('failed to RegWrite. shell is null.'); return false; } try{ $shell->RegWrite($key,$value,$type); return true; } catch (\Exception $e) { logError('failed to RegWrite',$e->getCode(),$e->getMessage()); return false; } } public static function RegDelete($key) { $shell = self::$shell; if(! $shell){ logError('failed to RegDelete. shell is null.'); return false; } //check if key exists try{ $shell->RegRead($key); } catch (\Exception $e) { logInfo('reg key not exists. skip delete',$key); return true; } //delete key try{ $shell->RegDelete($key); return true; } catch (\Exception $e) { logError('failed to RegDelete',$e->getCode(),$e->getMessage()); return false; } } public static function createShortcut($shortcutPath,$targetPath,$description = '') { $shell = self::$shell; if(! $shell){ logError('failed to createShortcut. shell is null.'); return false; } try{ $link = $shell->CreateShortcut($shortcutPath); $link->TargetPath = $targetPath; $link->WorkingDirectory = dirname($targetPath); $link->Description = $description; $link->Save(); return true; } catch (\Exception $e) { logError('failed to createShortcut',$e->getCode(),$e->getMessage()); return false; } } public static function getDesktopFolder() { $shell = self::$shell; if(! $shell){ logError('failed to getDesktopFolder. shell is null.'); return false; } try{ return $shell->SpecialFolders("Desktop"); } catch (\Exception $e) { logError('failed to getDesktopFolder',$e->getCode(),$e->getMessage()); return false; } } } //windows进程操作 class WinProcess extends WscriptShell { private static $tmp_dir; public function __construct($tmp_dir) { WscriptShell::__construct(); self::$tmp_dir = $tmp_dir; } private static function pathJoin($base, $path) { $base = str_replace('\\','/',$base); $path = str_replace('\\','/',$path); return rtrim( $base, '/' ) . '/' . ltrim( $path, '/' ); } public static function find($name){ $out_file = pathJoin(self::$tmp_dir,$name . '.txt'); if( file_exists($out_file) ){ unlink($out_file); } $cmd = "cmd.exe /C tasklist /FI \"IMAGENAME eq {$name}\" /FO list > {$out_file}"; if( self::Run($cmd,0,1) != 0){ logError('failed to Run',$cmd); return false; } if( !file_exists($out_file) ){ logError('failed to find out file',$out_file); return false; } $pids = array(); $fp = fopen($out_file,"r"); while($line = fgets($fp) ){ if( substr($line,0,4) == 'PID:'){ $pid = trim(substr($line,4)); array_push($pid); } } fclose($fp); return $pids; } public static function remove($name) { //关闭进程 $cmd = "cmd.exe /C taskkill /F /IM {name}"; if( self::Run($cmd,0,1) != 0){ logError('failed to Run',$cmd); return false; } //检测是否已经没有该任务进程 $pids = self::find($name); if($pids === false){ logError('failed to execute find',$name); return false; } if( count($pids) == 0){ logInfo('no process exists',$name); return true; } else{ logInfo('process still exists after stop',$name); return false; } } }; //windows系统是否有管理员权限,写入一个注册表键值测试 class WinAdminPriv extends WscriptShell { private static $KEY_PATH = "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment\\DistHelper"; public function __construct() { WscriptShell::__construct(); } public static function check() { return self::RegWrite(self::$KEY_PATH,'AdminPriv','REG_EXPAND_SZ'); } }; //windows系统环境路径操作类 class WinEnvPath extends WscriptShell { private static $KEY_PATH = "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment\\Path"; public function __construct() { WscriptShell::__construct(); } public static function readPath() { return self::RegRead(self::$KEY_PATH); } public static function updatePath(array $removePatterns ,array $addPathes,array &$removePathes) { $envPath = self::RegRead(self::$KEY_PATH); if($envPath === false){ return false; } $newPath = self::removePath($envPath,$removePatterns,$removePathes); $newPath = self::addPath($newPath,$addPathes); return self::RegWrite(self::$KEY_PATH,$newPath,'REG_EXPAND_SZ'); } private static function isPattern(string $path,array $patterns) { foreach($patterns as $pattern){ if(strpos($path,$pattern) !== false){ return true; } } return false; } private static function removePath(string $envPath,array $patterns,array &$removePathes) { $array = explode(';',$envPath); $result = array(); foreach($array as $path){ if( self::isPattern($path,$patterns)){ array_push($removePathes,$path); } else{ array_push($result,$path); } } return implode(';',$result); } private static function addPath(string $envPath,array $addPathes) { $array = explode(';',$envPath); $result = array_merge($array,$addPathes); return implode(';',$result); } }; //windows系统自启动操作类 class WinAutorun extends WscriptShell { private static $KEY_PATH = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\"; public function __construct() { WscriptShell::__construct(); } public static function remove($name) { if( strlen($name) == 0){ logError('failed to removeAutorun. name is empty.'); return false; } $key = self::$KEY_PATH . $name; return self::RegDelete($key); } public static function register($name,$cmd) { if( strlen($name) == 0){ logError('failed to registerAutorun. name is empty.'); return false; } if( strlen($cmd) == 0){ logError('failed to registerAutorun. cmd is empty.'); return false; } $key = self::$KEY_PATH . $name; return self::RegWrite($key,$cmd,'REG_EXPAND_SZ'); } }; //windows 快捷键操作 class WinDesktopShortcut extends WscriptShell { public function __construct() { WscriptShell::__construct(); } public static function remove($shortcutName) { $desktopFolder = self::getDesktopFolder(); if(! $desktopFolder){ return false; } $shortcutPath = pathJoin($desktopFolder,$shortcutName); if( file_exists($shortcutPath) ) return unlink($shortcutPath); else return true; } public static function register($shortcutName,$targetPath,$description) { $desktopFolder = self::getDesktopFolder(); if(! $desktopFolder){ return false; } $shortcutPath = pathJoin($desktopFolder,$shortcutName); return self::createShortcut($shortcutPath,$targetPath,$description); } }; //windows文件操作 class WinFile extends WscriptShell { public function __construct() { WscriptShell::__construct(); } public static function setHidden($path) { $cmd = "cmd.exe /C attrib \"{$path}\" +s +h"; return self::Run($cmd,0,1); } }; function setHiddenAttrib($path){ $os = getOsName(); if( $os == 'win'){ $wf = new WinFile(); return $wf->setHidden($path); } else{ return true; } }