|
@@ -0,0 +1,229 @@
|
|
|
+<?php
|
|
|
+namespace DistributeHelper;
|
|
|
+error_reporting(E_ALL & ~E_NOTICE);
|
|
|
+include "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());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ 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 WinEnvPath extends WscriptShell
|
|
|
+{
|
|
|
+ private static $KEY_PATH = "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment\\Path";
|
|
|
+ public function __construct()
|
|
|
+ {
|
|
|
+ WscriptShell::__construct();
|
|
|
+ }
|
|
|
+ public function readPath()
|
|
|
+ {
|
|
|
+ return self::RegRead(self::$KEY_PATH);
|
|
|
+ }
|
|
|
+ public function updatePath(array $removePatterns ,array $addPathes)
|
|
|
+ {
|
|
|
+ $envPath = self::RegRead(self::$KEY_PATH);
|
|
|
+ if($envPath === false){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ $newPath = self::removePath($envPath,$removePatterns);
|
|
|
+ $newPath = self::addPath($newPath,$addPathes);
|
|
|
+ return $shell->RegWrite(self::$KEY_PATH,$newPath,'REG_EXPAND_SZ');
|
|
|
+ }
|
|
|
+ private function isPattern(string $path,array $patterns)
|
|
|
+ {
|
|
|
+ foreach($patterns as $pattern){
|
|
|
+ if(strpos($path,$pattern) !== false){
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ private function removePath(string $envPath,array $patterns)
|
|
|
+ {
|
|
|
+ $array = explode(';',$envPath);
|
|
|
+ $result = array();
|
|
|
+ foreach($array as $path){
|
|
|
+ if(! self::isPattern($path,$patterns)){
|
|
|
+ array_push($result,$path);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return implode(';',$result);
|
|
|
+ }
|
|
|
+ private function addPath(string $envPath,array $addPathes)
|
|
|
+ {
|
|
|
+ $array = explode(';',$envPath);
|
|
|
+ $result = array_merge($array,$addPathes);
|
|
|
+ return implode(';',$result);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+//windows系统自启动操作类
|
|
|
+class WinAutorun extends WscriptShell
|
|
|
+{
|
|
|
+ private $shell = null;
|
|
|
+ public function __construct()
|
|
|
+ {
|
|
|
+ WscriptShell::__construct();
|
|
|
+ }
|
|
|
+ public function remove($name)
|
|
|
+ {
|
|
|
+ $shell = self::$shell;
|
|
|
+ if(! $shell){
|
|
|
+ logError('failed to removeAutorun. shell is null.');
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if( strlen($name) == 0){
|
|
|
+ logError('failed to removeAutorun. name is empty.');
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ try{
|
|
|
+ $key = 'HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\' . $name;
|
|
|
+ $shell->RegDelete($key;
|
|
|
+ return true;
|
|
|
+ } catch (Exception $e) {
|
|
|
+ logError('failed to removeAutorun',$e->getCode(),$e->getMessage());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public function register($name,$cmd)
|
|
|
+ {
|
|
|
+ $shell = self::$shell;
|
|
|
+ if(! $shell){
|
|
|
+ logError('failed to registerAutorun. shell is null.');
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ try{
|
|
|
+ $key = 'HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\' . $name;
|
|
|
+ $shell->RegWrite($key,$cmd,'REG_EXPAND_SZ');
|
|
|
+ return true;
|
|
|
+ } catch (Exception $e) {
|
|
|
+ logError('failed to registerAutorun',$e->getCode(),$e->getMessage());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+//windows 快捷键操作
|
|
|
+class WinDesktopShortcut extends WscriptShell
|
|
|
+{
|
|
|
+ public function __construct()
|
|
|
+ {
|
|
|
+ WscriptShell::__construct();
|
|
|
+ }
|
|
|
+ public 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 function register($shortcutName,$targetPath,$description)
|
|
|
+ {
|
|
|
+ $desktopFolder = self::getDesktopFolder();
|
|
|
+ if(! $desktopFolder){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ $shortcutPath = pathJoin($desktopFolder,$shortcutName);
|
|
|
+ return self::createShortcut($shortcutPath,$targetPath,$description);
|
|
|
+ }
|
|
|
+};
|