Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
391 liveuser 1
<?php
2
 
3
/*
4
 
5
        QBPWCF, Quick Build PHP website Component base on Fedora Linux.
6
    Copyright (C) 2014~2026 MIN ZHI, 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
$output=array();
30
 
31
#取得 lib path
32
exec("php -f ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/../../../../bin/libexec/folderOfUsrLib.php"),$output,$status);
33
 
34
#如果執行失敗
35
if($status!==0){
36
 
37
	#debug
38
	var_dump(__LINE__,$output);
39
 
40
	#結束執行,回傳shell 1.
41
	exit(1);
42
 
43
	}#if end
44
 
45
#儲存lib path
46
$folderOfUsrLib=$output[0];
47
 
48
#初始化輸出
49
$output=array();
50
 
51
#以該檔案的實際位置的 lib path 為 include path 首位
52
exec("cd ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/../../../../".$folderOfUsrLib."/qbpwcf").";pwd;",$output,$status);
53
set_include_path($output[0].PATH_SEPARATOR.get_include_path());
54
 
55
#匯入套件
56
require_once("allInOne.php");
57
 
58
#建議的log位置
59
$logFile=$_SERVER["DOCUMENT_ROOT"].$_SERVER["PHP_SELF"].".log";
60
 
61
#休息10秒
62
sleep(10);
63
 
64
#取得要用於 rotated file 的新內容
65
#產生一份 10 行資料的檔案
66
#函式說明:
67
#呼叫shell執行系統命令,並取得回傳的內容.
68
#回傳結果:
69
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
70
#$result["error"],錯誤訊息陣列.
71
#$result["function"],當前執行的函數名稱.
72
#$result["argu"],使用的參數.
73
#$result["cmd"],執行的指令內容.
74
#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
75
#$result["output"],爲執行完後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
76
#$result["content"],為執行完後的輸出字串.
77
#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
78
#$result["running"],是否還在執行.
79
#$result["pid"],pid.
80
#$result["statusCode"],執行結束後的代碼.
81
#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
82
#$result["noEscaped"],陣列,儲存重新排序過未經過escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
83
#必填參數:
84
#$conf["command"],字串,要執行的指令.
85
$conf["command"]="getContentByLineRange.php";
86
#$conf["fileArgu"],字串,變數__FILE__的內容.
87
$conf["fileArgu"]=__FILE__;
88
#可省略參數:
89
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
90
$conf["argu"]=array("21","30","readOnly_ssl_access_log");
91
#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
92
#$conf["arguIsAddr"]=array();
93
#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
94
#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
95
#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
96
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
97
#$conf["enablePrintDescription"]="true";
98
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
99
#$conf["printDescription"]="";
100
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
101
$conf["escapeshellarg"]="true";
102
#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
103
#$conf["thereIsShellVar"]=array();
104
#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
105
#$conf["username"]="";
106
#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
107
#$conf["password"]="";
108
#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
109
#$conf["useScript"]="";
110
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
111
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
112
#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
113
#$conf["inBackGround"]="";
114
#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
115
#$conf["getErr"]="false";
116
#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
117
#$conf["doNotRun"]="false";
118
#$conf["cmdContainArgu"],字串,cmd參數是否已經含有參數,預設為"false"代表沒有;反之為"true".
119
#$conf["cmdContainArgu"]="false";
120
#$conf["remoteIp"],字串,遠端ip或domainName,預設不使用,若存在則會透過ssh指令進行連線,然後執行指令.
121
#$conf["remoteIp"]="";
122
#$conf["remoteUser"],字串,遠端的使用者賬戶,預設不指定,亦即跟為運行該php程式的使用者.
123
#$conf["remoteUser"]="";
124
#$conf["sshOptions"],陣列,當具備 "remoteIp" 參數時,該參數每個元素為ssh指令的參數.
125
#$conf["sshOptions"]=array();
126
#參考資料:
127
#exec=>http://php.net/manual/en/function.exec.php
128
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
129
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
130
#備註:
131
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
132
#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
133
$callShell=external::callShell($conf);
134
unset($conf);
135
 
136
#如果執行異常
137
if($callShell["status"]==="false"){
138
 
139
	#debug
140
	var_dump($callShell);
141
 
142
	#結束執行,回傳 shell 1
143
	exit(1);
144
 
145
	}#if end
146
 
147
#儲存rotate後要逐行寫入的新內容
148
$dateToOverwrite=$callShell["output"];
149
 
150
#rotate log file
151
 
152
#預設一開始要覆寫
153
$pipe=">";
154
 
155
#針對每行新Log
156
foreach($dateToOverwrite as $line){
157
 
158
	#函式說明:
159
	#呼叫shell執行系統命令,並取得回傳的內容.
160
	#回傳結果:
161
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
162
	#$result["error"],錯誤訊息陣列.
163
	#$result["function"],當前執行的函數名稱.
164
	#$result["argu"],使用的參數.
165
	#$result["cmd"],執行的指令內容.
166
	#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
167
	#$result["output"],爲執行完後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
168
	#$result["content"],為執行完後的輸出字串.
169
	#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
170
	#$result["running"],是否還在執行.
171
	#$result["pid"],pid.
172
	#$result["statusCode"],執行結束後的代碼.
173
	#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
174
	#$result["noEscaped"],陣列,儲存重新排序過未經過escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
175
	#必填參數:
176
	#$conf["command"],字串,要執行的指令.
177
	$conf["command"]="echo";
178
	#$conf["fileArgu"],字串,變數__FILE__的內容.
179
	$conf["fileArgu"]=__FILE__;
180
	#可省略參數:
181
	#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
182
	$conf["argu"]=array($line,$pipe,"ssl_access_log");
183
	#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
184
	#$conf["arguIsAddr"]=array();
185
	#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
186
	#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
187
	#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
188
	#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
189
	#$conf["enablePrintDescription"]="true";
190
	#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
191
	#$conf["printDescription"]="";
192
	#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
193
	$conf["escapeshellarg"]="true";
194
	#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
195
	#$conf["thereIsShellVar"]=array();
196
	#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
197
	#$conf["username"]="";
198
	#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
199
	#$conf["password"]="";
200
	#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
201
	#$conf["useScript"]="";
202
	#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
203
	#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
204
	#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
205
	#$conf["inBackGround"]="";
206
	#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
207
	#$conf["getErr"]="false";
208
	#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
209
	#$conf["doNotRun"]="false";
210
	#$conf["cmdContainArgu"],字串,cmd參數是否已經含有參數,預設為"false"代表沒有;反之為"true".
211
	#$conf["cmdContainArgu"]="false";
212
	#$conf["remoteIp"],字串,遠端ip或domainName,預設不使用,若存在則會透過ssh指令進行連線,然後執行指令.
213
	#$conf["remoteIp"]="";
214
	#$conf["remoteUser"],字串,遠端的使用者賬戶,預設不指定,亦即跟為運行該php程式的使用者.
215
	#$conf["remoteUser"]="";
216
	#$conf["sshOptions"],陣列,當具備 "remoteIp" 參數時,該參數每個元素為ssh指令的參數.
217
	#$conf["sshOptions"]=array();
218
	#參考資料:
219
	#exec=>http://php.net/manual/en/function.exec.php
220
	#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
221
	#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
222
	#備註:
223
	#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
224
	#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
225
	$callShell=external::callShell($conf);
226
	unset($conf);
227
 
228
	#如果執行異常
229
	if($callShell["status"]==="false"){
230
 
231
		#debug
232
		var_dump($callShell);
233
 
234
		#結束執行,回傳 shell 1
235
		exit(1);
236
 
237
		}#if end
238
 
239
	#後續都用 append
240
	$pipe=">>";
241
 
242
	}#foreach end
243
 
244
?>