Browse Source

add wscript.shell

elviss 2 năm trước cách đây
mục cha
commit
cf19d3ab49
3 tập tin đã thay đổi với 315 bổ sung11 xóa
  1. 73 0
      src/PackageClass.php
  2. 13 11
      src/Utility.php
  3. 229 0
      src/WscriptShell.php

+ 73 - 0
src/PackageClass.php

@@ -2,6 +2,7 @@
 namespace DistributeHelper;
 error_reporting(E_ALL & ~E_NOTICE);
 include "Utility.php";
+include "WscriptShell.php";
 
 class PackageClass
 {
@@ -138,6 +139,9 @@ class PackageClass
 		
 		//注册自启动程序
 		self::registerAutorun($packageInfo);
+		
+		//注册桌面快捷方式
+		self::registerShortcut($packageInfo);
 		return true;
 		
 	}
@@ -428,9 +432,78 @@ class PackageClass
 
 	private static function registerPath($packageInfo)
 	{
+		$app = self::$app;	
+		$rootDir = $packageInfo['rootDir'];
+		
+		$removePath = $app["distribute-root"]["remove-path"] ? : array();
+		$registerPath = $app["distribute-root"]["register-path"] ? : array();
+		
+		$arrRemovePath = array();
+		foreach($removePath as $item){
+			$path = pathJoin($rootDir,$item['path']);
+			array_push($arrRemovePath,$path);
+		}
+		$arrAddPath = array();
+		foreach($registerPath as $item){
+			$path = pathJoin($rootDir,$item['path']);
+			array_push($arrAddPath,$path);
+		}
+		
+		$env = new WinEnvPath();
+		if(! $env->updatePath($arrRemovePath,$arrAddPath) ){
+			logError('failed to registerPath');
+			return false;
+		}
+		return true;
 	}
 	
 	private static function registerAutorun($packageInfo)
 	{
+		$app = self::$app;	
+		$rootDir = $packageInfo['rootDir'];
+		
+		$removeAutorun = $app["distribute-root"]["remove-autorun"] ? : array();
+		$regAutorun = $app["distribute-root"]["register-autorun"] ? : array();
+		
+		$auto = new WinAutorun();
+		for($removeAutorun as $item){
+			if(! $auto->remove($item['name']) ){
+				logError('failed to remove autorun',$item['name']);
+				return false;
+			}
+		}
+		for($regAutorun as $item){
+			$cmd = pathJoin($rootDir,$item['path']);
+			if(! $auto->register($item['name'],$cmd) ){
+				logError('failed to register autorun',$item['name']);
+				return false;
+			}
+		}
+		return true;
+	}
+	
+	private static function registerShortcut($packageInfo)
+	{
+		$app = self::$app;	
+		$rootDir = $packageInfo['rootDir'];
+		
+		$removeShortcut = $app["distribute-root"]["remove-shortcut"] ? : array();
+		$regShortcut = $app["distribute-root"]["register-shortcut"] ? : array();
+		
+		$sc = new WinDesktopShortcut();
+		for($removeShortcut as $item){
+			if(! $sc->remove($item['name']) ){
+				logError('failed to remove shortcut',$item['name']);
+				return false;
+			}
+		}
+		for($regShortcut as $item){
+			$path = pathJoin($rootDir,$item['path']);
+			if(! $sc->register($item['name'],$path,$item['desc']) ){
+				logError('failed to register shortcut',$item['name']);
+				return false;
+			}
+		}
+		return true;
 	}
 }

+ 13 - 11
src/Utility.php

@@ -1,4 +1,15 @@
 <?php
+//默认配置信息
+function defaultConfig(){
+	return array(
+		'distribute-mode' => 'install',
+		'cache-dir' => 'd:/dist-cache/',
+		'tool-dir' => '',
+		//'oss-host' => 'http://sis31-disthelp.oss-cn-hangzhou.aliyuncs.com',
+		'oss-host' => 'http://192.168.3.10:8996',
+		'mysql-host'=> '',
+	);
+}
 //记录信息日志
 function logInfo(){
 	$content = '[INF] ';
@@ -206,17 +217,7 @@ function loadRepoConfigs($vendorDir,$repoNames){
 	}
 	return $configs;
 }
-//默认配置信息
-function defaultConfig(){
-	return array(
-		'distribute-mode' => 'install',
-		'cache-dir' => 'd:/dist-cache/',
-		'tool-dir' => '',
-		//'oss-host' => 'http://distribute-helper.oss-cn-hangzhou.aliyuncs.com',
-		'oss-host' => 'http://192.168.3.10:8996',
-		'mysql-host'=> '',
-	);
-}
+
 //解析部署配置文件
 function loadConfig($cfgPath){
 	$config = json_decode(file_get_contents($cfgPath), true);
@@ -284,3 +285,4 @@ function unzipFile($toolDir,$zipFile, $targetDir){
 	}
 	return false;
 }
+

+ 229 - 0
src/WscriptShell.php

@@ -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);
+	}
+};