Subversion Repositories php-qbpwcf

Rev

Rev 350 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 350 Rev 391
Line 5605... Line 5605...
5605
			$conf["timeout"]--;
5605
			$conf["timeout"]--;
5606
 
5606
 
5607
			}#while end
5607
			}#while end
5608
 
5608
 
5609
		}#function timeout end
5609
		}#function timeout end
-
 
5610
		
-
 
5611
	/*
-
 
5612
	#函式說明:
-
 
5613
	#呼叫shell執行系統php程式字串.
-
 
5614
	#回傳結果:
-
 
5615
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
-
 
5616
	#$result["error"],錯誤訊息陣列.
-
 
5617
	#$result["function"],當前執行的函數名稱.
-
 
5618
	#$result["cmd"],執行的指令內容.
-
 
5619
	#$result["statusCode"],運行php程式後,shell回傳的代碼.
-
 
5620
	#$result["content"],為輸出的結果陣列
-
 
5621
	#$result["content"][std"],標準輸出結果.
-
 
5622
	#$result["content"]["err"],錯誤輸出結果.
-
 
5623
	#必填參數:
-
 
5624
	#$conf["code"],字串,要運行的php程式字串,可以用cmd::createPHPcodeString產生.
-
 
5625
	$conf["code"]="";
-
 
5626
	#可省略參數:
-
 
5627
	#$conf["getCmdOnly"],字串,若為"true"則代表不要運行程式,只要回傳cmd結果就好;反之預設為"false".
-
 
5628
	#$conf["getCmdOnly"]="false";
-
 
5629
	#參考資料:
-
 
5630
	#http://php.net/manual/en/function.exec.php
-
 
5631
	#備註:
-
 
5632
	#無.
-
 
5633
	*/
-
 
5634
	public static function execPHPcode(&$conf){
-
 
5635
	
-
 
5636
		#初始化要回傳的結果
-
 
5637
		$result=array();
-
 
5638
 
-
 
5639
		#取得當前執行的函數名稱
-
 
5640
		$result["function"]=__FUNCTION__;
-
 
5641
 
-
 
5642
		#如果沒有參數
-
 
5643
		if(func_num_args()==0){
-
 
5644
 
-
 
5645
			#設置執行失敗
-
 
5646
			$result["status"]="false";
-
 
5647
 
-
 
5648
			#設置執行錯誤訊息
-
 
5649
			$result["error"]="函數".$result["function"]."需要參數";
-
 
5650
 
-
 
5651
			#回傳結果
-
 
5652
			return $result;
-
 
5653
 
-
 
5654
			}#if end
-
 
5655
 
-
 
5656
		#取得參數
-
 
5657
		$result["argu"]=$conf;
-
 
5658
 
-
 
5659
		#如果 $conf 不為陣列
-
 
5660
		if(gettype($conf)!=="array"){
-
 
5661
 
-
 
5662
			#設置執行失敗
-
 
5663
			$result["status"]="false";
-
 
5664
 
-
 
5665
			#設置執行錯誤訊息
-
 
5666
			$result["error"][]="\$conf變數須為陣列形態";
-
 
5667
 
-
 
5668
			#如果傳入的參數為 null
-
 
5669
			if($conf===null){
-
 
5670
 
-
 
5671
				#設置執行錯誤訊息
-
 
5672
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
-
 
5673
 
-
 
5674
				}#if end
-
 
5675
 
-
 
5676
			#回傳結果
-
 
5677
			return $result;
-
 
5678
 
-
 
5679
			}#if end
-
 
5680
			
-
 
5681
		#檢查參數
-
 
5682
		#函式說明:
-
 
5683
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
-
 
5684
		#回傳結果:
-
 
5685
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
-
 
5686
		#$result["error"],執行不正常結束的錯訊息陣列.
-
 
5687
		#$result["simpleError"],簡單表示的錯誤訊息.
-
 
5688
		#$result["function"],當前執行的函式名稱.
-
 
5689
		#$result["argu"],設置給予的參數.
-
 
5690
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
-
 
5691
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
-
 
5692
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
-
 
5693
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
-
 
5694
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
-
 
5695
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
-
 
5696
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
-
 
5697
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
-
 
5698
		#必填參數:
-
 
5699
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
-
 
5700
		$conf["variable::checkArguments"]["varInput"]=&$conf;
-
 
5701
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
-
 
5702
		$conf["variable::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
-
 
5703
		#可省略參數:
-
 
5704
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
-
 
5705
		$conf["variable::checkArguments"]["mustBeFilledVariableName"]=array("code");
-
 
5706
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null、any代表不指定變數形態.其中 resource也包含"resource (closed)".
-
 
5707
		$conf["variable::checkArguments"]["mustBeFilledVariableType"]=array("string");
-
 
5708
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
-
 
5709
		$conf["variable::checkArguments"]["canBeEmptyString"]="false";
-
 
5710
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
-
 
5711
		#$conf["canNotBeEmpty"]=array();
-
 
5712
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
-
 
5713
		#$conf["canBeEmpty"]=array();
-
 
5714
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
-
 
5715
		$conf["variable::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("getCmdOnly");
-
 
5716
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
-
 
5717
		$conf["variable::checkArguments"]["skipableVariableName"]=array("getCmdOnly");
-
 
5718
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
-
 
5719
		$conf["variable::checkArguments"]["skipableVariableType"]=array("string");
-
 
5720
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
-
 
5721
		$conf["variable::checkArguments"]["skipableVarDefaultValue"]=array("false");
-
 
5722
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
-
 
5723
		#$conf["disallowAllSkipableVarIsEmpty"]="";
-
 
5724
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
-
 
5725
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
-
 
5726
		#$conf["disallowAllSkipableVarNotExist"],字串,是否不允許每個可省略參數都不存在,預設為"false"代表允許,反之為"true".
-
 
5727
		#$conf["disallowAllSkipableVarNotExist"]="";
-
 
5728
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
-
 
5729
		#$conf["arrayCountEqualCheck"][]=array();
-
 
5730
		#參考資料:
-
 
5731
		#array_keys=>http://php.net/manual/en/function.array-keys.php
-
 
5732
		#備註:
-
 
5733
		#無.
-
 
5734
		$checkArguments=variable::checkArguments($conf["variable::checkArguments"]);
-
 
5735
		unset($conf["variable::checkArguments"]);
-
 
5736
	
-
 
5737
		#如果執行異常
-
 
5738
		if($checkArguments["status"]==="false"){
-
 
5739
		
-
 
5740
			#設置執行錯誤
-
 
5741
			$result["error"]=$checkArguments;
-
 
5742
			
-
 
5743
			#設置執行異常
-
 
5744
			$result["status"]="false";
-
 
5745
			
-
 
5746
			#回傳結果
-
 
5747
			return $result;
-
 
5748
		
-
 
5749
			}#if end
-
 
5750
			
-
 
5751
		#如果參數檢查不通過
-
 
5752
		if($checkArguments["passed"]==="false"){
-
 
5753
		
-
 
5754
			#設置執行錯誤
-
 
5755
			$result["error"]=$checkArguments;
-
 
5756
			
-
 
5757
			#設置執行異常
-
 
5758
			$result["status"]="false";
-
 
5759
			
-
 
5760
			#回傳結果
-
 
5761
			return $result;
-
 
5762
		
-
 
5763
			}#if end
-
 
5764
		
-
 
5765
		#設置要給予 hreads::proc 的 cmds 參數
-
 
5766
		$paramsFor_cmds="php -r ".escapeshellarg($conf["code"]);
-
 
5767
		
-
 
5768
		#如果 "getCmdOnly" 為 "true"
-
 
5769
		if($conf["getCmdOnly"]==="true"){
-
 
5770
		
-
 
5771
			#儲存要運行的指令
-
 
5772
			$result["cmd"]=$paramsFor_cmds;
-
 
5773
			
-
 
5774
			#設置執行正常與否的識別
-
 
5775
			$result["status"]="true";
-
 
5776
		
-
 
5777
			#回傳結果
-
 
5778
			return $result;
-
 
5779
		
-
 
5780
			}#if end
-
 
5781
		
-
 
5782
		#函式說明:
-
 
5783
		#透過proc來多執行序運作.
-
 
5784
		#回傳結果:
-
 
5785
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
-
 
5786
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
-
 
5787
		#$result["function"],當前執行的函式名稱.
-
 
5788
		#$result["argu"],使用的參數.
-
 
5789
		#$result["content"],陣列,每個元素為其指令執行的結果訊息陣列,key為"status"代表執行是否正常的識別;key為"statusCode"代表程式結束後回傳給對應executeBy程式的數值,若為"?"則代表程序尚未結束,若為整數0,則代表順利執行結束,可透過proc_update函式進行資訊的更新與取得;key為"content"代表標準輸出,若為resource,則代表為pipe;key為"error"代表非標準輸出,若為resource,則代表為pipe;key為"input"代表成功輸入的指令;key為"process"代表該程序經proc_open後的process source;key為"proc_get_status"代表程序的資訊.
-
 
5790
		#必填參數:
-
 
5791
		#$conf["cmds"],字串陣列,每個元素代表單一程序要執行的指令與參數.
-
 
5792
		$conf["threads::proc"]["cmds"]=array($paramsFor_cmds);
-
 
5793
		#可省略參數:
-
 
5794
		#$conf["wait"],字串,是否需要等待所有程序結束,預設為"true"要等待;反之為"false"不要等待.
-
 
5795
		#$conf["wait"]="false";
-
 
5796
		#$conf["timeout"],字串陣列,每個元素代表單一程序執行的最大等待秒數,超過後將會強迫停止執行,僅當wait參數為"true"時生效.
-
 
5797
		#$conf["timeout"]=array("10");
-
 
5798
		#$conf["workingDir"],字串陣列,個別程式執行時的家目錄,預設不指定.
-
 
5799
		#$conf["workingDir"]=array("path");
-
 
5800
		#$conf["envs"],2維字串陣列,每個元素代表個別程式執行時的指定環境變數,key變數名稱;value為變數內容.預設為array("QBPWCF" => "Quick Build PHP Website Componment base on Fedora Linux");
-
 
5801
		#$conf["envs"]=array(array("key"=>"value"));
-
 
5802
		#$conf["executeBy"],字串陣列,每個元素代表個別指令要用什麼程式執行,預設為"bash".
-
 
5803
		#$conf["executeBy"]=array("bash");
-
 
5804
		#參考資料:
-
 
5805
		#https://www.php.net/manual/en/function.proc-open.php
-
 
5806
		#https://www.php.net/manual/en/function.proc-get-status.php
-
 
5807
		#https://www.php.net/manual/en/function.proc-terminate.php
-
 
5808
		#備註:
-
 
5809
		#當wait參數為"true"時,會自動解析stdout與stderr,因此不用再透過 self::proc_update 來更新.
-
 
5810
		#當wait參數不為"true"時,若需要取得當下的執行狀況,請使用 self::proc_update 來更新.
-
 
5811
		$proc=threads::proc($conf["threads::proc"]);
-
 
5812
		unset($conf["threads::proc"]);
-
 
5813
	
-
 
5814
		#如果執行異常
-
 
5815
		if($proc["status"]==="false"){
-
 
5816
		
-
 
5817
			#設置執行錯誤
-
 
5818
			$result["error"]=$proc;
-
 
5819
			
-
 
5820
			#設置執行異常
-
 
5821
			$result["status"]="false";
-
 
5822
			
-
 
5823
			#回傳結果
-
 
5824
			return $result;
-
 
5825
		
-
 
5826
			}#if end
-
 
5827
		
-
 
5828
		#儲存標準輸出的字串
-
 
5829
		$result["content"]["std"]=$proc[0]["content"];
-
 
5830
	
-
 
5831
		#儲存錯誤輸出的字串
-
 
5832
		$result["content"]["err"]=$proc[0]["error"];
-
 
5833
		
-
 
5834
		#儲存執行的完整指令
-
 
5835
		$result["cmd"]=$proc[0]["input"];
-
 
5836
		
-
 
5837
		#設置shell回傳的代碼
-
 
5838
		$result["statusCode"]=$proc[0]["statusCode"];
-
 
5839
		
-
 
5840
		#設置執行正常與否的識別
-
 
5841
		$result["status"]=$proc[0]["status"];
-
 
5842
		
-
 
5843
		#回傳結果
-
 
5844
		return $result;
-
 
5845
	
-
 
5846
		}#function execPHPcode end
5610
 
5847
 
5611
	}#class external end
5848
	}#class external end
5612
 
5849
 
5613
?>
5850
?>