123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- <?php
- namespace DistributeHelper;
- error_reporting(E_ALL & ~E_NOTICE);
- include_once "Utility.php";
- class WscriptShell
- {
- public static $shell = null;
- public function __construct()
- {
- try{
- if(self::$shell == null)
- self::$shell = new \com("WScript.Shell");
- }catch (\Exception $e) {
- logError('failed to creeate WScript.Shell',$e->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);
- }
- };
|