Subversion Repositories qbpwcf-lib(archive)

Rev

Rev 709 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
708 liveuser 1
<?php
2
 
3
/*
4
 
5
        QBPWCF, Quick Build PHP website Component base on Fedora Linux.
6
    Copyright (C) 2015~2024 Min-Jhin,Chen
7
 
8
    This file is part of QBPWCF.
9
 
10
    QBPWCF is free software: you can redistribute it and/or modify
11
    it under the terms of the GNU General Public License as published by
12
    the Free Software Foundation, either version 3 of the License, or
13
    (at your option) any later version.
14
 
15
    QBPWCF is distributed in the hope that it will be useful,
16
    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
    GNU General Public License for more details.
19
 
20
    You should have received a copy of the GNU General Public License
21
    along with QBPWCF.  If not, see <http://www.gnu.org/licenses/>.
22
 
23
*/
24
 
25
#使用命名空間qbpwcf
26
namespace qbpwcf;
27
 
28
#匯入套件
29
require_once("qbpwcf/allInOneForJson.php");
30
 
31
#建議的log位置
32
$logFile=$_SERVER["DOCUMENT_ROOT"].$_SERVER["PHP_SELF"].".log";
33
 
34
#./usr_bin_test.php --cmd configCache.php
35
 
36
#函式說明:
37
#呼叫shell執行系統命令,並取得回傳的內容.
38
#回傳結果:
39
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
40
#$result["error"],錯誤訊息陣列.
41
#$result["function"],當前執行的函數名稱.
42
#$result["argu"],使用的參數.
43
#$result["cmd"],執行的指令內容.
44
#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
45
#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
46
#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
47
#$result["running"],是否還在執行.
48
#$result["pid"],pid.
49
#$result["statusCode"],執行結束後的代碼.
50
#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
51
#必填參數:
52
#$conf["command"],字串,要執行的指令.
53
$conf["command"]="./usr_bin_test.php";
54
#$conf["fileArgu"],字串,變數__FILE__的內容.
55
$conf["fileArgu"]=__FILE__;
56
#可省略參數:
57
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
58
$conf["argu"]=array("--cmd","configCache.php");
59
#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
60
#$conf["arguIsAddr"]=array();
61
#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
62
#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
63
#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
64
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
65
#$conf["enablePrintDescription"]="true";
66
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
67
#$conf["printDescription"]="";
68
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
69
#$conf["escapeshellarg"]="false";
70
#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
71
#$conf["thereIsShellVar"]=array();
72
#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
73
#$conf["username"]="";
74
#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
75
#$conf["password"]="";
76
#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
77
#$conf["useScript"]="";
78
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
79
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
80
#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
81
#$conf["inBackGround"]="";
82
#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
83
#$conf["getErr"]="false";
84
#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
85
$conf["doNotRun"]="true";
86
#參考資料:
87
#exec=>http://php.net/manual/en/function.exec.php
88
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
89
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
90
#備註:
91
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
92
#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
93
$callShell=external::callShell($conf);
94
unset($conf);
95
 
96
#如果執行失敗
97
if($callShell["status"]==="false"){
98
 
99
	#debug
100
	var_dump($callShell);
101
 
102
	#結束執行,回傳1給shell.
103
	exit(1);
104
 
105
	}#if end
106
 
107
#函式說明:
108
#將一維陣列轉換為用特定符號間隔的字串,ex:array("1","2","3") to "a;b;c;".
109
#回傳的結果:
110
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
111
#$result["function"],當前執行的function名稱
112
#$result["error"],錯誤訊息陣列.
113
#$result["content"],處理好的字串.
114
#$result["argu"],使用的參數.
115
#必填參數:
116
#$conf["inputArray"],字串陣列,要轉成字串的一維陣列.
117
$conf["inputArray"]=$callShell["escape"]["array"];
118
#可省略參數:
119
#$conf["spiltSymbol"],字串,用來區隔字串的符號,預設為;
120
$conf["spiltSymbol"]="";
121
#$conf["skipEnd"],字串,結尾是否不要加上符號,預設為"false",要加上符號,"true"代表不要加上符號。
122
#$conf["skipEnd"]="";
123
#$conf["spiltSymbolAtStart"],字串,是否要在開頭加上spiltSymbol,預設為"false",代表不要;反之為“true”.
124
#$conf["spiltSymbolAtStart"]="";
125
#參考資料:
126
#無.
127
#備註:
128
#無.
129
$arrayToString=arrays::arrayToString($conf);
130
unset($conf);
131
 
132
#如果執行失敗
133
if($arrayToString["status"]==="false"){
134
 
135
	#debug
136
	var_dump($arrayToString);
137
 
138
	#結束執行,回傳1給shell.
139
	exit(1);
140
 
141
	}#if end
142
 
143
#函式說明:
144
#透過proc來多執行序運作.
145
#回傳結果:
146
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
147
#$reuslt["error"],執行不正常結束的錯訊息陣列.
148
#$result["function"],當前執行的函式名稱.
149
#$result["argu"],使用的參數.
150
#$result["content"],陣列,每個元素為其指令執行的結果訊息陣列,key為"status"代表執行是否正常的識別;key為"statusCode"代表程式結束後回傳給對應executeBy程式的數值;key為"output"代表標準輸出,若為resource,則代表為pipe;key為"error"代表非標準輸出,若為resource,則代表為pipe;key為"input"代表成功輸入的指令;key為"process"代表該程序經proc_open後的process source;key為"proc_get_status"代表程序的資訊.
151
#必填參數:
152
#$conf["cmds"],字串陣列,每個元素代表要執行的指令與參數.
153
$conf["cmds"]=array($arrayToString["content"]);
154
#可省略參數:
155
#$conf["wait"],字串,是否需要等待所有程序結束,預設為"true"要等待;反之為"false"不要等待.
156
$conf["wait"]="false";
157
#$conf["workingDir"],字串陣列,個別程式執行時的家目錄,預設不指定.
158
#$conf["workingDir"]=array("path");
159
#$conf["envs"],2維字串陣列,每個元素代表個別程式執行時的指定環境變數,key變數名稱;value為變數內容.預設為array("QBPWCF" => "Quick Build PHP Website Componment base on Fedora Linux");
160
#$conf["envs"]=array(array("key"=>"value"));
161
#$conf["executeBy"],字串陣列,每個元素代表個別指令要用什麼程式執行,預設為"bash".
162
#$conf["executeBy"]=array("bash");
163
#參考資料:
164
#https://www.php.net/manual/en/function.proc-open.php
165
#https://www.php.net/manual/en/function.proc-get-status.php
166
#備註:
167
#無.
168
$proc=threads::proc($conf);
169
unset($conf);
170
 
171
#如果執行失敗
172
if($proc["status"]==="false"){
173
 
174
	#debug
175
	var_dump($proc);
176
 
177
	#結束執行,回傳1給shell.
178
	exit(1);
179
 
180
	}#if end
181
 
182
#函式說明:
183
#查尋有使用 server 函式提供組態設定檔的服務,取得key的value.
184
#回傳結果:
185
#$result["status"],執行是否正常,"true"為正常,"false"為不正常.
186
#$result["error"],錯誤訊息陣列.
187
#$result["function"],當前執行的函數名稱.
188
#$result["rawContent"],得到的原始訊息.
189
#$result["content"],用json_decode後的結果;若"inBg"為"true",則其每個元素為其指令執行的結果訊息陣列,key為"status"代表執行是否正常的識別;key為"statusCode"代表程式結束後回傳給對應executeBy程式的數值;key為"output"代表標準輸出,若為resource,則代表為pipe;key為"error"代表非標準輸出,若為resource,則代表為pipe;key為"input"代表成功輸入的指令;key為"process"代表該程序經proc_open後的process source;key為"proc_get_status"代表程序的資訊.
190
#必填參數:
191
#$conf["server"],字串,組態服務設定服務的網址
192
$conf["server"]="https://qbpwc.sourceforge.io/demo/configServerDemo.php";
193
#$conf["postName"],字串,POST變數的名稱,亦即要查尋的定義名稱.
194
$conf["postName"]="";
195
#$conf["fileArgu"],字串,變數__FILE__的內容.
196
$conf["fileArgu"]=__FILE__;
197
#可省略參數:
198
#$conf["cache"],字串,預設為"true",代表會啟用cache服務;反之為"false".
199
#$conf["cache"]="false";
200
#$conf["clientCacheDaemonSock"],字串,提供cache服務的 unix domain socket 檔案位置,預設為套件根目錄底下的"var/config::client/clientCacheDaemon.sock";.
201
#$conf["clientCacheDaemonSock"]="";
202
#$conf["inBg"],字串,預設為"false",代表在前景執行;反之為"true"代表在背景中執行,會強制將"cache"參數設為"false".
203
#$conf["inBg"]="true";
204
#參考資料:
205
#無.
206
#備註:
207
#目前僅能跟server函式提供的服務對接.
208
$client=config::client($conf);
209
unset($conf);
210
 
211
 
212
 
213
?>