Subversion Repositories php-qbpwcf

Rev

Rev 226 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 226 Rev 232
Line 2181... Line 2181...
2181
			case "qbpwcf":
2181
			case "qbpwcf":
2182
 
2182
 
2183
				#如果是要加密
2183
				#如果是要加密
2184
				if($conf["qbpwcfDecode"]==="false"){
2184
				if($conf["qbpwcfDecode"]==="false"){
2185
 
2185
 
2186
					#to json string
-
 
2187
					$jsonData=json_encode($conf["enCodeStr"]);
-
 
2188
 
-
 
2189
					#如果出錯
-
 
2190
					if($jsonData===false){
-
 
2191
 
-
 
2192
						#設置執行失敗
-
 
2193
						$result["status"]="false";
-
 
2194
 
-
 
2195
						#設置執行錯誤
-
 
2196
						$result["error"][]="encode 「".$conf["enCodeStr"]."」 失敗";
-
 
2197
 
-
 
2198
						#回傳結果
-
 
2199
						return $result;
-
 
2200
 
-
 
2201
						}#if end
-
 
2202
 
-
 
2203
					#設置加密好的內容
2186
					#設置編碼好的內容
2204
					$result["content"]=base64_encode(urlencode($jsonData));
2187
					$result["content"]=base64_encode(serialize($conf["enCodeStr"]));
2205
 
2188
					
2206
					}#if end
2189
					}#if end
2207
 
2190
 
2208
				#反之是要解密
2191
				#反之是要解密
2209
				else{
2192
				else{
2210
 
2193
 
2211
					#base64_decode then urldecode data
-
 
2212
					$urlDecodeData=urldecode(base64_decode($conf["enCodeStr"]));
-
 
2213
 
-
 
2214
					#json_decode data
-
 
2215
					$json_decode_data=json_decode($urlDecodeData);
-
 
2216
 
-
 
2217
					#如果異常
-
 
2218
					if($urlDecodeData!==null && $json_decode_data===null){
-
 
2219
 
-
 
2220
						#設置執行失敗
-
 
2221
						$result["status"]="false";
-
 
2222
 
-
 
2223
						#設置執行錯誤
-
 
2224
						$result["error"][]="decode 「".$conf["enCodeStr"]."」 失敗";
-
 
2225
 
-
 
2226
						#回傳結果
-
 
2227
						return $result;
-
 
2228
 
-
 
2229
						}#if end
-
 
2230
 
-
 
2231
					#設置解密好的內容
2194
					#設置解密好的內容
2232
					$result["content"]=$json_decode_data;
2195
					$result["content"]=unserialize(base64_decode($conf["enCodeStr"]));
2233
 
2196
 
2234
					}#else end
2197
					}#else end
2235
 
2198
 
2236
				#跳出 switch
2199
				#跳出 switch
2237
				break;
2200
				break;
Line 2294... Line 2257...
2294
					#回傳結果
2257
					#回傳結果
2295
					return $result;
2258
					return $result;
2296
 
2259
 
2297
					}#if end
2260
					}#if end
2298
 
2261
 
2299
				#如果是用加密
2262
				#如果是用gpg加密
2300
				if($conf["gpgDecrypt"]==="false"){
2263
				if($conf["gpgDecrypt"]==="false"){
2301
 
2264
 
-
 
2265
					#如果是 array 或 object
-
 
2266
					if( gettype($conf["enCodeStr"])==="array" || gettype($conf["enCodeStr"])==="object"){
-
 
2267
					
2302
					#用GnuPG加密
2268
						#用 qbpwcf encode
2303
					#函式說明:
2269
						#函式說明:
2304
					#呼叫shell執行系統命令,並取得回傳的內容.
2270
						#加密或編碼字串,可以用的方法有sha1,md5,password_sha,qbpwcf,bin2hex,hex2bin,gpg,sha1可以回傳20或40的數值字串,md5可以回傳32個數值字串,password_hash可以回傳60~255個字元.
2305
					#回傳結果:
2271
						#回傳結果:
2306
					#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
2272
						#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
2307
					#$result["error"],錯誤訊息陣列.
-
 
2308
					#$result["function"],當前執行的函數名稱.
2273
						#$result["function"],當前執行的函數名稱.
2309
					#$result["argu"],使用的參數.
-
 
2310
					#$result["cmd"],執行的指令內容.
-
 
2311
					#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
-
 
2312
					#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
-
 
2313
					#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
-
 
2314
					#$result["content"],為執行完後的輸出字串.
2274
						#$result["content"],加密後的結果.
2315
					#$result["running"],是否還在執行.
2275
						#$result["error"],錯誤訊息陣列.
2316
					#$result["pid"],pid.
2276
						#$result["argu"],使用的參數.
2317
					#$result["statusCode"],執行結束後的代碼.
-
 
2318
					#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
-
 
2319
					#必填參數:
2277
						#必填參數:
2320
					#$conf["command"],字串,要執行的指令.
2278
						#$conf["enCodeStr"],any,要加密的字串,陣列,物件.
2321
					$conf["external::callShell"]["command"]="base64decode.php";
2279
						$conf["self::enCodeStr"]["enCodeStr"]=$conf["enCodeStr"];
2322
					#$conf["fileArgu"],字串,變數__FILE__的內容.
2280
						#$conf["enCodeType"],"字串",加密或編碼的類型,有"sha1"與"md5"與"p_hash"與"aes256"與"qbpwcf"與"bin2hex"與"hex2bin"與"gpg"與"hex2bin&gpg",9種,"sha1"較耗時;"md5"較快;"p_hash"適用於密碼加密;"aes256"是對稱式加解密;"qbpwcf"是透過json_encode、urlencode、base64_encode的結果;"bin2hex"是依照每個byte的整數數值轉成"00"~"FF"後的結果,反之為"hex2bin";"gpg"是應用gpg進行加解密,需要先有ID對應的key;"hex2bin&gpg"為先hex2bin在進行gpg加解密.
2323
					$conf["external::callShell"]["fileArgu"]=__FILE__;
2281
						$conf["self::enCodeStr"]["enCodeType"]="qbpwcf";
2324
					#可省略參數:
2282
						#可省略參數:
2325
					#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
-
 
2326
					$conf["external::callShell"]["argu"]=array(base64_encode($conf["enCodeStr"]),"|","gpg","-r",$conf["gpgId"],"--trust-model","always","-v","-e");
-
 
2327
					#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
-
 
2328
					#$conf["arguIsAddr"]=array();
-
 
2329
					#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
-
 
2330
					#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
-
 
2331
					#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
-
 
2332
					#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
2283
						#$conf["sha1Raw"],字串,sha1加密的結果要用20個0與1組合還是要用40位數的16進位數值,"true"代表前者,"false"代表後者,預設為"false".
2333
					#$conf["enablePrintDescription"]="true";
-
 
2334
					#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
-
 
2335
					#$conf["printDescription"]="";
2284
						#$conf["sha1Raw"]="false";
2336
					#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
-
 
2337
					$conf["external::callShell"]["escapeshellarg"]="true";
-
 
2338
					#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
2285
						#$conf["p_hash"],字串,p_hash加密過後的字串,該參數若存在且$conf["enCodeType"]為"p_hash",則代表是要檢查$conf["enCodeStr"]是否為符合$conf["p_hash"]的密碼.
2339
					#$conf["thereIsShellVar"]=array();
-
 
2340
					#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
-
 
2341
					#$conf["username"]="";
2286
						#$conf["p_hash"]="";
2342
					#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
2287
						#$conf["keyForAes256"],字串,用來 AES256 加解密的金鑰.
2343
					#$conf["password"]="";
2288
						#$conf["keyForAes256"]="";
2344
					#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
2289
						#$conf["aes256Encode"],字串,"true"代表是要加密,"false"代表是要解密.
2345
					#$conf["useScript"]="";
2290
						#$conf["aes256Encode"]="";
2346
					#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
2291
						#$conf["qbpwcfDecode"],字串,若"enCodeType"為"qbpwcf",則預設為"false",代表加密;反之為"true",代表解密.
2347
					#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
-
 
2348
					#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
-
 
2349
					#$conf["inBackGround"]="";
2292
						#$conf["qbpwcfDecode"]="false";
2350
					#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
2293
						#$conf["gpgDecrypt"],字串,若"enCodeType"為"gpg",則預設為"false",代表加密;反之為"true",代表解密.
2351
					#$conf["getErr"]="false";
2294
						#$conf["gpgDecrypt"]="false";
2352
					#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
2295
						#$conf["gpgId"],字串,若"enCodeType"為"gpg"時,要使用的gpg id,預設為gnupgId.
2353
					#$conf["doNotRun"]="false";
2296
						#$conf["gpgId"]="";
2354
					#參考資料:
2297
						#參考資料:
2355
					#exec=>http://php.net/manual/en/function.exec.php
2298
						#sha1=>http://php.net/manual/en/function.sha1.php
-
 
2299
						#md5=>http://php.net/manual/en/function.md5.php
2356
					#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
2300
						#password_hash=>http://php.net/manual/en/function.password-hash.php
2357
					#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
2301
						#password_verify=>http://php.net/manual/en/function.password-verify.php
-
 
2302
						#json_decode=>https://www.php.net/manual/en/function.json-decode.php
2358
					#備註:
2303
						#備註:
2359
					#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
-
 
2360
					#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
-
 
-
 
2304
						#無.
2361
					$callShell=external::callShell($conf["external::callShell"]);
2305
						$enCodeStr=self::enCodeStr($conf["self::enCodeStr"]);
2362
					unset($conf["external::callShell"]);
2306
						unset($conf["self::enCodeStr"]);
-
 
2307
					
-
 
2308
						#如果執行失敗
-
 
2309
						if($enCodeStr["status"]==="false"){
-
 
2310
 
-
 
2311
							#設置執行失敗
-
 
2312
							$result["status"]="false";
-
 
2313
 
-
 
2314
							#設置執行錯誤
-
 
2315
							$result["error"]=$enCodeStr;
-
 
2316
 
-
 
2317
							#回傳結果
-
 
2318
							return $result;
-
 
2319
 
-
 
2320
							}#if end
-
 
2321
							
-
 
2322
						#取得 qbpwcf 編碼後的結果
-
 
2323
						$conf["enCodeStr"]=$enCodeStr["content"];
-
 
2324
					
-
 
2325
						}#if end
2363
 
2326
 
-
 
2327
					#如果已經為字串
-
 
2328
					if(gettype($conf["enCodeStr"])==="string"){
-
 
2329
					
-
 
2330
						#先用 base64 decode 為原始加密後的結果 再用 gpg 解密.
-
 
2331
						#函式說明:
-
 
2332
						#呼叫shell執行系統命令,並取得回傳的內容.
-
 
2333
						#回傳結果:
-
 
2334
						#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
-
 
2335
						#$result["error"],錯誤訊息陣列.
-
 
2336
						#$result["function"],當前執行的函數名稱.
-
 
2337
						#$result["argu"],使用的參數.
-
 
2338
						#$result["cmd"],執行的指令內容.
-
 
2339
						#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
-
 
2340
						#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
-
 
2341
						#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
-
 
2342
						#$result["content"],為執行完後的輸出字串.
-
 
2343
						#$result["running"],是否還在執行.
-
 
2344
						#$result["pid"],pid.
-
 
2345
						#$result["statusCode"],執行結束後的代碼.
-
 
2346
						#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
-
 
2347
						#必填參數:
-
 
2348
						#$conf["command"],字串,要執行的指令.
-
 
2349
						$conf["external::callShell"]["command"]="base64decode.php";
-
 
2350
						#$conf["fileArgu"],字串,變數__FILE__的內容.
-
 
2351
						$conf["external::callShell"]["fileArgu"]=__FILE__;
-
 
2352
						#可省略參數:
-
 
2353
						#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
-
 
2354
						$conf["external::callShell"]["argu"]=array(base64_encode($conf["enCodeStr"]),"|","gpg","-r",$conf["gpgId"],"--trust-model","always","-v","-e");
-
 
2355
						#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
-
 
2356
						#$conf["arguIsAddr"]=array();
-
 
2357
						#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
-
 
2358
						#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
-
 
2359
						#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
-
 
2360
						#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
-
 
2361
						#$conf["enablePrintDescription"]="true";
-
 
2362
						#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
-
 
2363
						#$conf["printDescription"]="";
-
 
2364
						#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
-
 
2365
						$conf["external::callShell"]["escapeshellarg"]="true";
-
 
2366
						#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
-
 
2367
						#$conf["thereIsShellVar"]=array();
-
 
2368
						#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
-
 
2369
						#$conf["username"]="";
-
 
2370
						#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
-
 
2371
						#$conf["password"]="";
-
 
2372
						#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
-
 
2373
						#$conf["useScript"]="";
-
 
2374
						#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
-
 
2375
						#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
-
 
2376
						#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
-
 
2377
						#$conf["inBackGround"]="";
-
 
2378
						#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
-
 
2379
						#$conf["getErr"]="false";
-
 
2380
						#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
-
 
2381
						#$conf["doNotRun"]="false";
-
 
2382
						#參考資料:
-
 
2383
						#exec=>http://php.net/manual/en/function.exec.php
-
 
2384
						#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
-
 
2385
						#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
-
 
2386
						#備註:
-
 
2387
						#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
-
 
2388
						#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
-
 
2389
						$callShell=external::callShell($conf["external::callShell"]);
-
 
2390
						unset($conf["external::callShell"]);
-
 
2391
 
2364
					#如果執行失敗
2392
						#如果執行失敗
2365
					if($callShell["status"]==="false"){
2393
						if($callShell["status"]==="false"){
-
 
2394
 
-
 
2395
							#設置執行失敗
-
 
2396
							$result["status"]="false";
-
 
2397
 
-
 
2398
							#設置執行錯誤
-
 
2399
							$result["error"]=$callShell;
-
 
2400
 
-
 
2401
							#回傳結果
-
 
2402
							return $result;
-
 
2403
 
-
 
2404
							}#if end
-
 
2405
						
-
 
2406
						#取得加密後的內容
-
 
2407
						$result["content"]=$callShell["content"];
-
 
2408
					
-
 
2409
						}#if end
2366
 
2410
 
-
 
2411
					#反之為不支援的參數形態
-
 
2412
					else{
-
 
2413
					
2367
						#設置執行失敗
2414
						#設置執行失敗
2368
						$result["status"]="false";
2415
						$result["status"]="false";
2369
 
2416
 
2370
						#設置執行錯誤
2417
						#設置執行錯誤
2371
						$result["error"]=$callShell;
2418
						$result["error"]="unsupported input type(".$gettype($conf["enCodeStr"]).")";
2372
 
2419
 
2373
						#回傳結果
2420
						#回傳結果
2374
						return $result;
2421
						return $result;
2375
 
2422
					
2376
						}#if end
2423
						}#else end
2377
 
-
 
2378
					#取得加密後的內容
-
 
2379
					$result["content"]=$callShell["content"];
-
 
2380
 
2424
 
2381
					}#if end
2425
					}#if end
2382
 
2426
 
2383
				#反之如果是解密
2427
				#反之如果是 gpg 解密
2384
				else if($conf["gpgDecrypt"]==="true"){
2428
				else if($conf["gpgDecrypt"]==="true"){
2385
 
2429
 
2386
					#用GnuPG解密
2430
					#用 base64 decode 解編碼
2387
					#函式說明:
2431
					#函式說明:
2388
					#呼叫shell執行系統命令,並取得回傳的內容.
2432
					#呼叫shell執行系統命令,並取得回傳的內容.
2389
					#回傳結果:
2433
					#回傳結果:
2390
					#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
2434
					#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
2391
					#$result["error"],錯誤訊息陣列.
2435
					#$result["error"],錯誤訊息陣列.
Line 2457... Line 2501...
2457
						#回傳結果
2501
						#回傳結果
2458
						return $result;
2502
						return $result;
2459
 
2503
 
2460
						}#if end
2504
						}#if end
2461
 
2505
 
2462
					#取得加密後的內容
2506
					#取得解密後的內容
2463
					$result["content"]=$callShell["content"];
2507
					$result["content"]=$callShell["content"];
2464
 
2508
 
2465
					}#else end
2509
					}#else end
2466
 
2510
 
2467
				#跳出 switch
2511
				#跳出 switch