elviss 2 tahun lalu
induk
melakukan
c145373996
3 mengubah file dengan 225 tambahan dan 22 penghapusan
  1. 35 1
      src/PackageClass.php
  2. 10 0
      src/Utility.php
  3. 180 21
      src/WscriptShell.php

+ 35 - 1
src/PackageClass.php

@@ -229,6 +229,12 @@ class PackageClass
 			self::setProgressFailed('REGISTER_AUTORUN_FAILED');
 			return false;
 		}
+
+		//注册服务程序
+		if(! self::registerService($packageInfo) ){
+			self::setProgressFailed('REGISTER_SERVICE_FAILED');
+			return false;
+		}
 		
 		//注册桌面快捷方式
 		if(! self::registerShortcut($packageInfo) ){
@@ -754,7 +760,7 @@ class PackageClass
 		}
 		foreach($regAutorun as $item){
 			$cmd = pathJoin($rootDir,$item['path']);
-			$cmd = str_replace('/','\\',$cmd);
+			$cmd = toOsSlashes($cmd);
 			if(! $auto->register($item['name'],$cmd) ){
 				logError('failed to register autorun',$item['name']);
 				return false;
@@ -764,6 +770,34 @@ class PackageClass
 		return true;
 	}
 	
+	private static function registerService($packageInfo)
+	{
+		$app = self::$app;	
+		$rootDir = $packageInfo['rootDir'];
+		
+		$removeService = $app["distribute-root"]["remove-service"] ? : array();
+		$regService = $app["distribute-root"]["register-service"] ? : array();
+		
+		$service = new WinService(DIST_DIR);
+		foreach($removeService as $item){
+			if(! $service->remove($item['name']) ){
+				logError('failed to remove autorun',$item['name']);
+				return false;
+			}
+			logInfo('succeeded to remove autorun',$item['name']);
+		}
+		foreach($regService as $item){
+			$path = pathJoin($rootDir,$item['path']);
+			$path = toOsSlashes($path);
+			if(! $service->register($item['name'],$path) ){
+				logError('failed to register autorun',$item['name']);
+				return false;
+			}
+			logInfo('succeeded to register autorun',$item['name']);
+		}
+		return true;
+	}
+	
 	private static function registerShortcut($packageInfo)
 	{
 		$app = self::$app;	

+ 10 - 0
src/Utility.php

@@ -59,6 +59,16 @@ function getOsName(){
 		return "linux";
 }
 
+//按操作系统生成斜杠
+function toOsSlashes($str){
+	$os = getOsName();
+	if($os == "win"){
+		return str_replace('/','\\',$str);
+	}
+	else{
+		return str_replace('\\','/',$str);
+	}
+}
 
 //获取当前缓存目录
 function getCacheDir(){

+ 180 - 21
src/WscriptShell.php

@@ -15,6 +15,11 @@ class WscriptShell
 			 logError('failed to creeate WScript.Shell',$e->getCode(),$e->getMessage());
 		}
 	}
+	public static function pathJoin($base, $path) {
+		$base = str_replace('\\','/',$base);
+		$path = str_replace('\\','/',$path);
+		return rtrim( $base, '/' ) . '/' . ltrim( $path, '/' );
+	}
 	/* style
 	0 SW_HIDE
 	1 SW_SHOWNORMAL
@@ -35,12 +40,42 @@ class WscriptShell
 			return false;
 		}
 		try{
-			return $shell->Run($cmd,$style,$wait);
+			$code = $shell->Run($cmd,$style,$wait);
+			if($code != 0){
+				logError('failed to Run',$cmd, 'code=',$code);
+			}
+			return $code;
 		} catch (\Exception $e) {
 			logError('failed to Run',$cmd, $e->getCode(),$e->getMessage());
 			return false;
 		}
 	}
+	public static function RunOutput($cmd,$out_file) //运行并获取输出文本数组
+	{
+		$shell = self::$shell;
+		if(! $shell){
+			logError('failed to Run. shell is null.');
+			return false;
+		}
+		if( file_exists($out_file) ){
+			unlink($out_file);
+		}
+		$cmd = "cmd.exe /C {$cmd} > {$out_file}";
+		$ret = self::Run($cmd,0,1);
+		if( $ret === false){
+			logError('failed to RunOutput',$cmd);
+			return false;
+		}
+		if( $ret !== 0){
+			logError('failed to RunOutput',$cmd, 'code',$ret);
+			return false;
+		}
+		if( !file_exists($out_file) ){
+			logError('failed to find out file',$out_file);
+			return false;
+		}
+		return $ret;
+	}
 	public static function RegRead($key)
 	{
 		$shell = self::$shell;
@@ -136,27 +171,19 @@ class WinProcess extends WscriptShell
 		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);
+		$cmd = "tasklist /FI \"IMAGENAME eq {$name}\" /FO list";
+		if( self::RunOutput($cmd,$out_file) !== 0){
+			logError('failed to RunOutput',$cmd);
 			return false;
 		}
 		$pids = array();
 		$fp = fopen($out_file,"r");
+		if(!$fp){
+			logError('failed to open',$out_file);
+			return false;
+		}
 		while($line = fgets($fp) ){
 			if( substr($line,0,4) == 'PID:'){
 				$pid = trim(substr($line,4));
@@ -171,7 +198,7 @@ class WinProcess extends WscriptShell
 	{
 		//关闭进程
 		$cmd = "cmd.exe /C taskkill /F /IM {name}";
-		if( self::Run($cmd,0,1) != 0){
+		if( self::Run($cmd,0,1) !== 0){
 			logError('failed to Run',$cmd);
 			return false;
 		}
@@ -270,7 +297,7 @@ class WinAutorun extends WscriptShell
 	}
 	public static function remove($name)
 	{
-		if( strlen($name) == 0){
+		if( strlen($name) == 0){ 
 			logError('failed to removeAutorun. name is empty.');
 			return false;
 		}
@@ -332,10 +359,9 @@ class WinFile extends WscriptShell
 	public static function setHidden($path)
 	{
 		$cmd = "cmd.exe /C attrib \"{$path}\" +s +h";
-		return self::Run($cmd,0,1);
+		return self::Run($cmd,0,1) === 0;
 	}	
 };
-
 function setHiddenAttrib($path){
 	$os = getOsName();
 	if( $os == 'win'){
@@ -345,4 +371,137 @@ function setHiddenAttrib($path){
 	else{
 		return true;
 	}
-}
+}
+//windows服务操作
+class WinService extends WscriptShell
+{
+	private static $tmp_dir;
+	public function __construct($tmp_dir)
+	{
+		WscriptShell::__construct();
+		self::$tmp_dir = $tmp_dir;
+	}
+	public static function register($name,$path)
+	{
+		if(stripos($name,'mysql') !== false){
+			return self::mysqldInstall($name,$path);
+		}
+		else{
+			logError('failed to register service. not support name.',$name,$path);
+			return false;
+		}
+	}
+	public static function remove($name)
+	{
+		//先停止服务
+		if(! self::scstop($name) ){
+			logError('failed to stop service.',$name);
+			return false;
+		}
+		//再移除服务
+		if(! self::scdelete($name)){
+			logError('failed to delete service.',$name);
+			return false;
+		}
+		return true;
+	}
+	
+	// false: failed
+	// 0: exists
+	// 1060: not exists
+	// other int: other error
+	public static function scexists($name)
+	{
+		$cmd = "sc query {$name}"; 
+		return self::Run($cmd,0,1);
+	}
+	// false: failed
+	// 1060: not exists
+	// other int: other error
+	// array: service info
+	// *** not implemented ***
+	public static function scquery($name)
+	{
+		$cmd = "sc query {$name}"; 
+		return self::Run($cmd,0,1) === 0;
+	}
+	public static function scstart($name)
+	{
+		$cmd = "sc start {$name}";
+		return self::Run($cmd,0,1) === 0;
+	}
+	public static function scdelete($name)
+	{
+		$cmd = "sc delete {$name}";
+		$code = self::Run($cmd,0,1);
+		if($code === false){
+			return false;
+		}
+		else if($code === 0){
+			logInfo('service deleted successfully.',$name);
+			return true;
+		}
+		else if($code === 1060){
+			logInfo('service not exists. no need to delete.',$name);
+			return true;
+		}
+		else{
+			logError('unknow error during delete service.',$name,$code);
+			return false;
+		}
+	}
+	// false: execute failed
+	// true: stop succeeded ( service not exists, service already stopped )
+	public static function scstop($name)
+	{
+		$cmd = "sc stop {$name}";
+		$code = self::Run($cmd,0,1);
+		if($code === false){
+			return false;
+		}
+		else if($code === 0){
+			logInfo('service stopped successfully.',$name);
+			return true;
+		}
+		else if($code === 1060){
+			logInfo('service not exists. no need to stop.',$name);
+			return true;
+		}
+		else if($code === 1062){
+			logInfo('service not running. no need to stop.',$name);
+			return true;
+		}else{
+			logError('unknow error during stopping service.',$name,$code);
+			return false;
+		}
+	}
+	private static function mysqldInstall($name,$path)
+	{
+		$cmd = "{$path} --install {$name}";
+		$code = self::Run($cmd,0,1);
+		if($code === 0){
+			logError('failed to install mysql service.',$name,$path);
+			return true;
+		}
+		else{
+			return false;
+		}
+	}
+	private static function mysqldRemove($path)
+	{
+		$cmd = "{$path} --remove";
+		$code =  self::Run($cmd,0,1);
+		if($code === 0){
+			logError('failed to remove mysql service.',$name,$path);
+			return true;
+		}
+		else{
+			return false;
+		}
+	}
+};
+
+$a = new WscriptShell();
+$cmd = 'cmd.exe /C sc query phpStudySrv > 1.txt';
+$cmd = 'sc stop phpStudySrv';
+print($a::Run($cmd,0,1));