WscriptShell.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. namespace DistributeHelper;
  3. error_reporting(E_ALL & ~E_NOTICE);
  4. include_once "Utility.php";
  5. class WscriptShell
  6. {
  7. public static $shell = null;
  8. public function __construct()
  9. {
  10. try{
  11. if(self::$shell == null)
  12. self::$shell = new \com("WScript.Shell");
  13. }catch (\Exception $e) {
  14. logError('failed to creeate WScript.Shell',$e->getCode(),$e->getMessage());
  15. }
  16. }
  17. /* style
  18. 0 SW_HIDE
  19. 1 SW_SHOWNORMAL
  20. 2 SW_SHOWMINIMIZED
  21. 3 SW_SHOWMAXIMIZED
  22. 4 SW_SHOWNOACTIVATE
  23. 5 SW_SHOW
  24. 6 SW_MINIMIZE
  25. 7 SW_SHOWMINNOACTIVE
  26. 8 SW_SHOWNA
  27. 9 SW_RESTORE
  28. */
  29. public static function Run($cmd,$style,$wait)
  30. {
  31. $shell = self::$shell;
  32. if(! $shell){
  33. logError('failed to Run. shell is null.');
  34. return false;
  35. }
  36. try{
  37. return $shell->Run($cmd,$style,$wait);
  38. } catch (\Exception $e) {
  39. logError('failed to Run',$cmd, $e->getCode(),$e->getMessage());
  40. return false;
  41. }
  42. }
  43. public static function RegRead($key)
  44. {
  45. $shell = self::$shell;
  46. if(! $shell){
  47. logError('failed to RegRead. shell is null.');
  48. return false;
  49. }
  50. try{
  51. return $shell->RegRead($key);
  52. } catch (\Exception $e) {
  53. logError('failed to RegRead',$e->getCode(),$e->getMessage());
  54. return false;
  55. }
  56. }
  57. public static function RegWrite($key,$value,$type)
  58. {
  59. $shell = self::$shell;
  60. if(! $shell){
  61. logError('failed to RegWrite. shell is null.');
  62. return false;
  63. }
  64. try{
  65. $shell->RegWrite($key,$value,$type);
  66. return true;
  67. } catch (\Exception $e) {
  68. logError('failed to RegWrite',$e->getCode(),$e->getMessage());
  69. return false;
  70. }
  71. }
  72. public static function RegDelete($key)
  73. {
  74. $shell = self::$shell;
  75. if(! $shell){
  76. logError('failed to RegDelete. shell is null.');
  77. return false;
  78. }
  79. //check if key exists
  80. try{
  81. $shell->RegRead($key);
  82. } catch (\Exception $e) {
  83. logInfo('reg key not exists. skip delete',$key);
  84. return true;
  85. }
  86. //delete key
  87. try{
  88. $shell->RegDelete($key);
  89. return true;
  90. } catch (\Exception $e) {
  91. logError('failed to RegDelete',$e->getCode(),$e->getMessage());
  92. return false;
  93. }
  94. }
  95. public static function createShortcut($shortcutPath,$targetPath,$description = '')
  96. {
  97. $shell = self::$shell;
  98. if(! $shell){
  99. logError('failed to createShortcut. shell is null.');
  100. return false;
  101. }
  102. try{
  103. $link = $shell->CreateShortcut($shortcutPath);
  104. $link->TargetPath = $targetPath;
  105. $link->WorkingDirectory = dirname($targetPath);
  106. $link->Description = $description;
  107. $link->Save();
  108. return true;
  109. } catch (\Exception $e) {
  110. logError('failed to createShortcut',$e->getCode(),$e->getMessage());
  111. return false;
  112. }
  113. }
  114. public static function getDesktopFolder()
  115. {
  116. $shell = self::$shell;
  117. if(! $shell){
  118. logError('failed to getDesktopFolder. shell is null.');
  119. return false;
  120. }
  121. try{
  122. return $shell->SpecialFolders("Desktop");
  123. } catch (\Exception $e) {
  124. logError('failed to getDesktopFolder',$e->getCode(),$e->getMessage());
  125. return false;
  126. }
  127. }
  128. }
  129. //windows进程操作
  130. class WinProcess extends WscriptShell
  131. {
  132. private static $tmp_dir;
  133. public function __construct($tmp_dir)
  134. {
  135. WscriptShell::__construct();
  136. self::$tmp_dir = $tmp_dir;
  137. }
  138. private static function pathJoin($base, $path) {
  139. $base = str_replace('\\','/',$base);
  140. $path = str_replace('\\','/',$path);
  141. return rtrim( $base, '/' ) . '/' . ltrim( $path, '/' );
  142. }
  143. public static function find($name){
  144. $out_file = pathJoin(self::$tmp_dir,$name . '.txt');
  145. if( file_exists($out_file) ){
  146. unlink($out_file);
  147. }
  148. $cmd = "cmd.exe /C tasklist /FI \"IMAGENAME eq {$name}\" /FO list > {$out_file}";
  149. if( self::Run($cmd,0,1) != 0){
  150. logError('failed to Run',$cmd);
  151. return false;
  152. }
  153. if( !file_exists($out_file) ){
  154. logError('failed to find out file',$out_file);
  155. return false;
  156. }
  157. $pids = array();
  158. $fp = fopen($out_file,"r");
  159. while($line = fgets($fp) ){
  160. if( substr($line,0,4) == 'PID:'){
  161. $pid = trim(substr($line,4));
  162. array_push($pid);
  163. }
  164. }
  165. fclose($fp);
  166. return $pids;
  167. }
  168. public static function remove($name)
  169. {
  170. //关闭进程
  171. $cmd = "cmd.exe /C taskkill /F /IM {name}";
  172. if( self::Run($cmd,0,1) != 0){
  173. logError('failed to Run',$cmd);
  174. return false;
  175. }
  176. //检测是否已经没有该任务进程
  177. $pids = self::find($name);
  178. if($pids === false){
  179. logError('failed to execute find',$name);
  180. return false;
  181. }
  182. if( count($pids) == 0){
  183. logInfo('no process exists',$name);
  184. return true;
  185. }
  186. else{
  187. logInfo('process still exists after stop',$name);
  188. return false;
  189. }
  190. }
  191. };
  192. //windows系统是否有管理员权限,写入一个注册表键值测试
  193. class WinAdminPriv extends WscriptShell
  194. {
  195. private static $KEY_PATH = "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment\\DistHelper";
  196. public function __construct()
  197. {
  198. WscriptShell::__construct();
  199. }
  200. public static function check()
  201. {
  202. return self::RegWrite(self::$KEY_PATH,'AdminPriv','REG_EXPAND_SZ');
  203. }
  204. };
  205. //windows系统环境路径操作类
  206. class WinEnvPath extends WscriptShell
  207. {
  208. private static $KEY_PATH = "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment\\Path";
  209. public function __construct()
  210. {
  211. WscriptShell::__construct();
  212. }
  213. public static function readPath()
  214. {
  215. return self::RegRead(self::$KEY_PATH);
  216. }
  217. public static function updatePath(array $removePatterns ,array $addPathes,array &$removePathes)
  218. {
  219. $envPath = self::RegRead(self::$KEY_PATH);
  220. if($envPath === false){
  221. return false;
  222. }
  223. $newPath = self::removePath($envPath,$removePatterns,$removePathes);
  224. $newPath = self::addPath($newPath,$addPathes);
  225. return self::RegWrite(self::$KEY_PATH,$newPath,'REG_EXPAND_SZ');
  226. }
  227. private static function isPattern(string $path,array $patterns)
  228. {
  229. foreach($patterns as $pattern){
  230. if(strpos($path,$pattern) !== false){
  231. return true;
  232. }
  233. }
  234. return false;
  235. }
  236. private static function removePath(string $envPath,array $patterns,array &$removePathes)
  237. {
  238. $array = explode(';',$envPath);
  239. $result = array();
  240. foreach($array as $path){
  241. if( self::isPattern($path,$patterns)){
  242. array_push($removePathes,$path);
  243. }
  244. else{
  245. array_push($result,$path);
  246. }
  247. }
  248. return implode(';',$result);
  249. }
  250. private static function addPath(string $envPath,array $addPathes)
  251. {
  252. $array = explode(';',$envPath);
  253. $result = array_merge($array,$addPathes);
  254. return implode(';',$result);
  255. }
  256. };
  257. //windows系统自启动操作类
  258. class WinAutorun extends WscriptShell
  259. {
  260. private static $KEY_PATH = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\";
  261. public function __construct()
  262. {
  263. WscriptShell::__construct();
  264. }
  265. public static function remove($name)
  266. {
  267. if( strlen($name) == 0){
  268. logError('failed to removeAutorun. name is empty.');
  269. return false;
  270. }
  271. $key = self::$KEY_PATH . $name;
  272. return self::RegDelete($key);
  273. }
  274. public static function register($name,$cmd)
  275. {
  276. if( strlen($name) == 0){
  277. logError('failed to registerAutorun. name is empty.');
  278. return false;
  279. }
  280. if( strlen($cmd) == 0){
  281. logError('failed to registerAutorun. cmd is empty.');
  282. return false;
  283. }
  284. $key = self::$KEY_PATH . $name;
  285. return self::RegWrite($key,$cmd,'REG_EXPAND_SZ');
  286. }
  287. };
  288. //windows 快捷键操作
  289. class WinDesktopShortcut extends WscriptShell
  290. {
  291. public function __construct()
  292. {
  293. WscriptShell::__construct();
  294. }
  295. public static function remove($shortcutName)
  296. {
  297. $desktopFolder = self::getDesktopFolder();
  298. if(! $desktopFolder){
  299. return false;
  300. }
  301. $shortcutPath = pathJoin($desktopFolder,$shortcutName);
  302. if( file_exists($shortcutPath) )
  303. return unlink($shortcutPath);
  304. else
  305. return true;
  306. }
  307. public static function register($shortcutName,$targetPath,$description)
  308. {
  309. $desktopFolder = self::getDesktopFolder();
  310. if(! $desktopFolder){
  311. return false;
  312. }
  313. $shortcutPath = pathJoin($desktopFolder,$shortcutName);
  314. return self::createShortcut($shortcutPath,$targetPath,$description);
  315. }
  316. };