Subversion Repositories php-qbpwcf

Rev

Rev 207 | Rev 232 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 1
<?php
2
 
3
/*
4
 
5
	QBPWCF, Quick Build PHP website Component base on Fedora Linux.
226 liveuser 6
    Copyright (C) 2014~2025 MIN ZHI, CHEN
3 liveuser 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
namespace qbpwcf;
25
 
26
/*
27
類別說明:
28
用於驗證處理的類別.
29
備註:
30
無.
31
*/
32
class authenticate{
33
 
34
	/*
35
	#函式說明:
36
	#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
37
	#回傳結果:
38
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
39
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
40
	#$result["function"],當前執行的函式名稱.
41
	#必填參數:
42
	#$method,物件,為物件實體或類別名稱,會自動置入該參數.
43
	#$arguments,陣列,為呼叫方法時所用的參數.
44
	#參考資料:
45
	#__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
46
	*/
47
	public function __call($method,$arguments){
226 liveuser 48
 
3 liveuser 49
		#取得當前執行的函式
50
		$result["function"]=__FUNCTION__;
226 liveuser 51
 
3 liveuser 52
		#設置執行不正常
53
		$result["status"]="false";
226 liveuser 54
 
3 liveuser 55
		#設置執行錯誤
56
		$result["error"][]=__NAMESPACE__ ."/".$method."() 不存在!";
226 liveuser 57
 
3 liveuser 58
		#設置所丟入的參數
59
		$result["error"][]=$arguments;
226 liveuser 60
 
3 liveuser 61
		#回傳結果
62
		return $result;
226 liveuser 63
 
3 liveuser 64
		}#function __call end
226 liveuser 65
 
3 liveuser 66
	/*
67
	#函式說明:
68
	#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
69
	#回傳結果:
70
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
71
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
72
	#$result["function"],當前執行的函式名稱.
73
	#必填參數:
74
	#$method,物件,為物件實體或類別名稱,會自動置入該參數.
75
	#$arguments,陣列,為呼叫方法時所用的參數.
76
	#參考資料:
77
	#__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
78
	*/
79
	public static function __callStatic($method,$arguments){
226 liveuser 80
 
3 liveuser 81
		#取得當前執行的函式
82
		$result["function"]=__FUNCTION__;
226 liveuser 83
 
3 liveuser 84
		#設置執行不正常
85
		$result["status"]="false";
226 liveuser 86
 
3 liveuser 87
		#設置執行錯誤
88
		$result["error"][]="欲呼叫的". __NAMESPACE__ ."/".$method."() 不存在!";
226 liveuser 89
 
3 liveuser 90
		#設置所丟入的參數
91
		$result["error"][]=$arguments;
226 liveuser 92
 
3 liveuser 93
		#回傳結果
94
		return $result;
226 liveuser 95
 
3 liveuser 96
		}#function __callStatic end
97
 
98
	/*
99
	#函式說明:
100
	#用來檢查 $_SESSION['password'] 是否存在以及其值是否正確
101
	#回傳結果:
102
	#$result["status"],執行是否正常,"true"為正常,"false"為不正常.
103
	#$result["passed"],是否驗證通過,"true"為驗證通過,"false"為驗證不通過.
104
	#$result["function"],當前執行的函數名稱.
105
	#$result["error"],錯誤訊息陣列.
106
	#必填參數:
107
	$conf["password"]="";#是驗證碼的內容應該要等於什麼才是對的。
108
	#可省略參數:
109
	#無.
110
	#參考資料:
111
	#無.
112
	#備註:
113
	#無.
226 liveuser 114
	*/
3 liveuser 115
	public static function simpleAuthenticate(&$conf){
226 liveuser 116
 
3 liveuser 117
		#初始化要回傳的變數
118
		$result=array();
226 liveuser 119
 
3 liveuser 120
		#記錄當前執行的函數名稱
121
		$result["function"]=__FUNCTION__;
226 liveuser 122
 
3 liveuser 123
		#如果 $conf 不為陣列
124
		if(gettype($conf)!="array"){
226 liveuser 125
 
3 liveuser 126
			#設置執行失敗
127
			$result["status"]="false";
226 liveuser 128
 
3 liveuser 129
			#設置執行錯誤訊息
130
			$result["error"][]="\$conf變數須為陣列形態";
131
 
132
			#如果傳入的參數為 null
133
			if($conf==null){
226 liveuser 134
 
3 liveuser 135
				#設置執行錯誤訊息
136
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 137
 
3 liveuser 138
				}#if end
139
 
140
			#回傳結果
141
			return $result;
226 liveuser 142
 
3 liveuser 143
			}#if end
226 liveuser 144
 
3 liveuser 145
		#檢查參數
146
		#函式說明:
147
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
148
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
149
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
150
		#$result["function"],當前執行的函式名稱.
151
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
152
		#$result[$shouldBtCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
153
		#$result[$shouldBtCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
154
		#$result[$shouldBtCheckedVarName]["error"],每個參數設定的錯誤訊息
155
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
156
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
157
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
158
		#必填參數:
159
		#$conf["variableCheck::checkArguments"]["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
160
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
161
		#$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
162
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("password");
226 liveuser 163
		#$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列 例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double");
3 liveuser 164
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string");
165
		#$conf["variableCheck::checkArguments"]["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
166
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
167
		#可以省略的參數:
168
		#$conf["canBeEmptyString"],必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true"。
169
		#$conf["canBeEmptyString"]="false";
170
		#$conf["skipableVariableName"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
171
		#$conf["skipableVariableName"]=array();
226 liveuser 172
		#$conf["skipableVariableType"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
3 liveuser 173
		#$conf["skipableVariableType"]=array();
174
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是必填參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
175
		#$conf["skipableVarDefaultValue"]=array("");
176
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
177
		#$conf["arrayCountEqualCheck"][]=array();
178
		#參考資料來源:
179
		#array_keys=>http://php.net/manual/en/function.array-keys.php
180
		$checkResult=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
181
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 182
 
3 liveuser 183
		#debug
184
		#var_dump($checkResult);
185
		#exit;
226 liveuser 186
 
3 liveuser 187
		#debug
188
		#var_dump($result);
189
		#exit;
226 liveuser 190
 
3 liveuser 191
		#如果檢查失敗
192
		if($checkResult["status"]=="false"){
226 liveuser 193
 
3 liveuser 194
			#debug
195
			#var_dump($result);
196
			#exit;
226 liveuser 197
 
3 liveuser 198
			#debug
199
			#var_dump($checkResult);
200
			#exit;
226 liveuser 201
 
3 liveuser 202
			#設置執行失敗
203
			$result["status"]="false";
226 liveuser 204
 
3 liveuser 205
			#設置執行錯誤訊息
206
			$result["error"]=$checkResult;
207
 
208
			#回傳結果
209
			return $result;
226 liveuser 210
 
3 liveuser 211
			}#if end
226 liveuser 212
 
3 liveuser 213
		#如果檢查不通過
214
		if($checkResult["passed"]=="false"){
226 liveuser 215
 
3 liveuser 216
			#設置執行失敗
217
			$result["status"]="false";
226 liveuser 218
 
3 liveuser 219
			#設置執行錯誤訊息
220
			$result["error"]=$checkResult;
221
 
222
			#回傳結果
223
			return $result;
226 liveuser 224
 
3 liveuser 225
			}#if end
226 liveuser 226
 
3 liveuser 227
		#debug
228
		#var_dump($result);
229
		#exit;
226 liveuser 230
 
3 liveuser 231
		#echo "entered";
226 liveuser 232
 
3 liveuser 233
		#若沒有供驗證的變數,或其驗證內容爲""
234
		if(!isset($_SESSION['password']) || $_SESSION['password']===""){
235
 
236
			#設置執行成功
237
			$result["status"]="true";
226 liveuser 238
 
3 liveuser 239
			#設置驗證不通過
240
			$result["passed"]="false";
226 liveuser 241
 
3 liveuser 242
			#回傳結果
243
			return $result;
226 liveuser 244
 
3 liveuser 245
			}#if end
246
 
247
		#如果驗證碼不正確
248
		if($_SESSION['password']!==$conf["password"]){
249
 
250
			#設置執行成功
251
			$result["status"]="true";
226 liveuser 252
 
3 liveuser 253
			#設置驗證不通過
254
			$result["passed"]="false";
226 liveuser 255
 
3 liveuser 256
			#回傳結果
257
			return $result;
258
 
259
			}#if end
260
 
261
		#反之驗證成功
262
		else{
263
 
264
			#設置執行成功
265
			$result["status"]="true";
226 liveuser 266
 
3 liveuser 267
			#設置驗證通過
268
			$result["passed"]="true";
226 liveuser 269
 
3 liveuser 270
			#回傳結果
271
			return $result;
272
 
273
			}#else end
226 liveuser 274
 
3 liveuser 275
		#不應該執行到這邊
276
		#設置執行失敗
277
		$result["status"]="false";
226 liveuser 278
 
3 liveuser 279
		#設置驗證不通過
280
		$result["passed"]="false";
226 liveuser 281
 
3 liveuser 282
		#回傳結果
283
		return $result;
226 liveuser 284
 
3 liveuser 285
		}#函式結束
286
 
287
	/*
288
	#函式說明:
289
	#用來檢查 $_SESSION['password'] 是否存在以及其值是否正確,如果檢查不通過,則會轉址.
290
	#回傳結果:
291
	#$result["status"],執行是否正常,"true"為正常,"false"為不正常.
292
	#$result["passed"],是否驗證通過,"true"為驗證通過,"false"為驗證不通過.
293
	#$result["function"],當前執行的函數名稱.
294
	#必填參數:
295
	$conf["failLocation"]="";#是當驗證失敗時要轉址到的位置
296
	$conf["password"]="";#是驗證碼的內容應該要等於什麼才是對的。
297
	#可省略參數:
298
	#無.
299
	#參考資料:
300
	#無.
301
	#備註:
302
	#無.
226 liveuser 303
	*/
3 liveuser 304
	public static function sessionAuthenticate(&$conf){
226 liveuser 305
 
3 liveuser 306
		#初始化要回傳的變數
307
		$result=array();
226 liveuser 308
 
3 liveuser 309
		#記錄當前執行的函數名稱
310
		$result["function"]=__FUNCTION__;
226 liveuser 311
 
3 liveuser 312
		#如果 $conf 不為陣列
313
		if(gettype($conf)!="array"){
226 liveuser 314
 
3 liveuser 315
			#設置執行失敗
316
			$result["status"]="false";
226 liveuser 317
 
3 liveuser 318
			#設置執行錯誤訊息
319
			$result["error"][]="\$conf變數須為陣列形態";
320
 
321
			#如果傳入的參數為 null
322
			if($conf==null){
226 liveuser 323
 
3 liveuser 324
				#設置執行錯誤訊息
325
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 326
 
3 liveuser 327
				}#if end
328
 
329
			#回傳結果
330
			return $result;
226 liveuser 331
 
3 liveuser 332
			}#if end
226 liveuser 333
 
3 liveuser 334
		#檢查參數
335
		#函式說明:
336
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
337
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
338
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
339
		#$result["function"],當前執行的函式名稱.
340
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
341
		#$result[$shouldBtCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
342
		#$result[$shouldBtCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
343
		#$result[$shouldBtCheckedVarName]["error"],每個參數設定的錯誤訊息
344
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
345
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
346
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
347
		#必填參數:
348
		#$conf["variableCheck::checkArguments"]["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
349
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
350
		#$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
351
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("failLocation","password");
226 liveuser 352
		#$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列 例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double");
3 liveuser 353
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string");
354
		#$conf["variableCheck::checkArguments"]["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
355
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
356
		#可以省略的參數:
357
		#$conf["canBeEmptyString"],必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true"。
358
		#$conf["canBeEmptyString"]="false";
359
		#$conf["skipableVariableName"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
360
		#$conf["skipableVariableName"]=array();
226 liveuser 361
		#$conf["skipableVariableType"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
3 liveuser 362
		#$conf["skipableVariableType"]=array();
363
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是必填參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
364
		#$conf["skipableVarDefaultValue"]=array("");
365
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
366
		#$conf["arrayCountEqualCheck"][]=array();
367
		#參考資料來源:
368
		#array_keys=>http://php.net/manual/en/function.array-keys.php
369
		$checkResult=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
370
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 371
 
3 liveuser 372
		#如果檢查失敗
373
		if($checkResult["status"]=="false"){
226 liveuser 374
 
3 liveuser 375
			#設置執行失敗
376
			$result["status"]="false";
226 liveuser 377
 
3 liveuser 378
			#設置執行錯誤訊息
379
			$result["error"]=$checkResult;
380
 
381
			#回傳結果
382
			return $result;
226 liveuser 383
 
3 liveuser 384
			}#if end
226 liveuser 385
 
3 liveuser 386
		#如果檢查不通過
387
		if($checkResult["status"]=="false"){
226 liveuser 388
 
3 liveuser 389
			#設置執行失敗
390
			$result["status"]="false";
226 liveuser 391
 
3 liveuser 392
			#設置執行錯誤訊息
393
			$result["error"]=$checkResult;
394
 
395
			#回傳結果
396
			return $result;
226 liveuser 397
 
3 liveuser 398
			}#if end
226 liveuser 399
 
3 liveuser 400
		#取得驗證碼驗證的回傳值
401
		#函式說明:
402
		#用來檢查 $_SESSION['password'] 是否存在以及其值是否正確
403
		#回傳結果:
404
		#$result["status"],執行是否正常,"true"為正常,"false"為不正常.
405
		#$result["passed"],是否驗證通過,"true"為驗證通過,"false"為驗證不通過.
406
		#$result["function"],當前執行的函數名稱.
407
		#$result["error"],錯誤訊息陣列.
408
		#必填參數:
409
		$conf["authenticate::simpleAuthenticate"]["password"]=$conf["password"];#是驗證碼的內容應該要等於什麼才是對的。
410
		$simpleAuthenticate=authenticate::simpleAuthenticate($conf["authenticate::simpleAuthenticate"]);
411
		unset($conf["authenticate::simpleAuthenticate"]);
412
 
413
		#如果執行驗證失敗
414
		if($simpleAuthenticate["status"]=="false"){
226 liveuser 415
 
3 liveuser 416
			#設置執行失敗
417
			$result["status"]="false";
226 liveuser 418
 
3 liveuser 419
			#設置錯誤訊息
420
			$result["error"]=$simpleAuthenticate;
226 liveuser 421
 
3 liveuser 422
			#回傳結果
423
			return $result;
226 liveuser 424
 
3 liveuser 425
			}#if end
426
 
427
		#如果驗證失敗
428
		if($simpleAuthenticate["passed"]=="false"){
226 liveuser 429
 
3 liveuser 430
			#進行轉址
431
			#函式說明:
432
			#設定session變數後,立即轉址.
433
			#回傳的結果:
434
			#$result["status"],執行是否成功,"true"代表成功,"false"代表不成功.
435
			#$result["error"],錯誤訊息陣列.
436
			#必填的參數:
437
			#$conf["headerLocation"],為要轉址到的地方,若爲 $_SERVER["PHP_SELF"],則爲回到目前頁面,全民資安素養網爲 https://isafe.moe.edu.tw/.
438
			$conf["header::redirectionNow"]["headerLocation"]=$conf["failLocation"];
439
			#可省略的參數:
440
			#$conf["sessionName"],字串陣列,可以指派session變數的名稱.
441
			#$conf["sessionName"]=array("");
442
			#$conf["sessionValue"]字串陣列,可以指派session變數的內容.
443
			#$conf["sessionValue"]=array("");
444
			#參靠資料來源:
445
			#http://php.net/manual/en/reserved.variables.server.php
446
			$redirectionNow=header::redirectionNow($conf["header::redirectionNow"]);
447
			unset($conf["header::redirectionNow"]);
226 liveuser 448
 
3 liveuser 449
			#如果轉址失敗
450
			if($redirectionNow["status"]=="false"){
226 liveuser 451
 
3 liveuser 452
				#設置執行失敗
453
				$result["status"]="false";
226 liveuser 454
 
3 liveuser 455
				#設置錯誤訊息
456
				$result["error"]=$redirectionNow;
226 liveuser 457
 
3 liveuser 458
				#回傳結果
459
				return $result;
226 liveuser 460
 
3 liveuser 461
				}#if end
462
 
179 liveuser 463
			}#if end
226 liveuser 464
 
3 liveuser 465
		#執行到這邊代表正常
466
		#設置執行正常
467
		$result["status"]="true";
226 liveuser 468
 
3 liveuser 469
		#回傳結果
470
		return $result;
471
 
472
		}#函式結束
473
 
474
	/*
475
	#函式說明:
179 liveuser 476
	#用來檢查指定 session 變數是否存在以及其值是否正確,如果檢查不通過,可進行轉址.
477
	#回傳結果:
478
	#$result["status"],執行是否正常,"true"為正常,"false"為不正常.
479
	#$result["function"],當前執行的函數名稱.
480
	#必填參數:
481
	#$conf["key"],字串陣列,session的key名稱與層級.
482
	$conf["key"]=array();
483
	#$conf["content"],session的內容應該要等於什麼才是對的.
484
	$conf["content"]=;
485
	#$conf["failLocation"],當驗證失敗時要轉址到的位置.
486
	$conf["failLocation"]="";
487
	#可省略參數:
488
	#$conf["unsetSess"],字串,預設為"false",代表不需要清除檢查過的seession變數;反之為"true".
489
	#$conf["unsetSess"]="false";
490
	#參考資料:
491
	#無.
492
	#備註:
493
	#無.
207 liveuser 494
	*/
179 liveuser 495
	public static function validateSession(&$conf){
226 liveuser 496
 
179 liveuser 497
		#初始化要回傳的變數
498
		$result=array();
226 liveuser 499
 
179 liveuser 500
		#記錄當前執行的函數名稱
501
		$result["function"]=__FUNCTION__;
226 liveuser 502
 
179 liveuser 503
		#如果 $conf 不為陣列
504
		if(gettype($conf)!="array"){
226 liveuser 505
 
179 liveuser 506
			#設置執行失敗
507
			$result["status"]="false";
226 liveuser 508
 
179 liveuser 509
			#設置執行錯誤訊息
510
			$result["error"][]="\$conf變數須為陣列形態";
511
 
512
			#如果傳入的參數為 null
513
			if($conf==null){
226 liveuser 514
 
179 liveuser 515
				#設置執行錯誤訊息
516
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 517
 
179 liveuser 518
				}#if end
519
 
520
			#回傳結果
521
			return $result;
226 liveuser 522
 
179 liveuser 523
			}#if end
226 liveuser 524
 
179 liveuser 525
		#檢查參數
526
		#函式說明:
527
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
528
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
529
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
530
		#$result["function"],當前執行的函式名稱.
531
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
532
		#$result[$shouldBtCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
533
		#$result[$shouldBtCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
534
		#$result[$shouldBtCheckedVarName]["error"],每個參數設定的錯誤訊息
535
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
536
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
537
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
538
		#必填參數:
539
		#$conf["variableCheck::checkArguments"]["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
540
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
541
		#$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
542
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("key","content","failLocation");
226 liveuser 543
		#$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列 例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double");
179 liveuser 544
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("array","string","string");
545
		#$conf["variableCheck::checkArguments"]["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
546
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
547
		#可以省略的參數:
548
		#$conf["canBeEmptyString"],必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true"。
549
		$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
550
		#$conf["skipableVariableName"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
551
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("unsetSess");
226 liveuser 552
		#$conf["skipableVariableType"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
179 liveuser 553
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string");
554
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是必填參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
555
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("false");
556
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
557
		#$conf["arrayCountEqualCheck"][]=array();
558
		#參考資料來源:
559
		#array_keys=>http://php.net/manual/en/function.array-keys.php
560
		$checkResult=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
561
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 562
 
179 liveuser 563
		#如果檢查失敗
564
		if($checkResult["status"]==="false"){
226 liveuser 565
 
179 liveuser 566
			#設置執行失敗
567
			$result["status"]="false";
226 liveuser 568
 
179 liveuser 569
			#設置執行錯誤訊息
570
			$result["error"]=$checkResult;
571
 
572
			#回傳結果
573
			return $result;
226 liveuser 574
 
179 liveuser 575
			}#if end
226 liveuser 576
 
179 liveuser 577
		#如果檢查不通過
578
		if($checkResult["passed"]==="false"){
226 liveuser 579
 
179 liveuser 580
			#設置執行失敗
581
			$result["status"]="false";
226 liveuser 582
 
179 liveuser 583
			#設置執行錯誤訊息
584
			$result["error"]=$checkResult;
585
 
586
			#回傳結果
587
			return $result;
226 liveuser 588
 
179 liveuser 589
			}#if end
226 liveuser 590
 
179 liveuser 591
		#函式說明:
226 liveuser 592
		#取得指定的session數值
179 liveuser 593
		#回傳結果:
594
		#$result["status"],執行是否正常,"true"代表正常,"false"代表有誤.
595
		#$result["error"],錯誤訊息.
596
		#$result["founded"],是否有該session變數,"true"代表有,"false"代表沒有.
597
		#$result["content"],session變數的內容
598
		#$result["address"],session變數的位址,請用 $yourVar=&$result["address"] 來取得位址.
599
		#$result["function"],當前執行的函數名稱.
600
		#$result["argu"],使用的參數.
601
		#必填參數:
602
		#$conf["key"],字串陣列,session的key名稱與層級.
603
		$conf["session::getVal"]["key"]=$conf["key"];
604
		#可省略參數:
605
		#$conf["unsetSess"],字串,是否要於成功取得session變數後,將之移除,預設為"false"代表不移除;反之為"true".
606
		$conf["session::getVal"]["unsetSess"]=$conf["unsetSess"];
607
		#參考資料:
608
		#無.
609
		#備註:
610
		#無.
611
		$getVal=session::getVal($conf["session::getVal"]);
612
		unset($conf["session::getVal"]);
226 liveuser 613
 
179 liveuser 614
		#如果執行異常
615
		if($getVal["status"]=="false"){
226 liveuser 616
 
179 liveuser 617
			#設置執行失敗
618
			$result["status"]="false";
226 liveuser 619
 
179 liveuser 620
			#設置執行錯誤訊息
621
			$result["error"]=$getVal;
622
 
623
			#回傳結果
624
			return $result;
226 liveuser 625
 
179 liveuser 626
			}#if end
226 liveuser 627
 
179 liveuser 628
		#如果指定的session變數不存在 或 session 內容不一樣
629
		if($getVal["founded"]==="false" || $getVal["content"]!==$conf["content"]){
226 liveuser 630
 
179 liveuser 631
			#函式說明:
632
			#設定session變數後,立即轉址.
633
			#回傳的結果:
634
			#$result["status"],執行是否成功,"true"代表成功,"false"代表不成功.
635
			#$result["function"],當前執行的函數名稱.
636
			#$result["error"],錯誤訊息陣列.
637
			#必填參數:
638
			#$conf["headerLocation"],為要轉址到的地方,若爲 $_SERVER["PHP_SELF"],則爲回到目前頁面,全民資安素養網爲 https://isafe.moe.edu.tw/.
639
			$conf["header::redirectionNow"]["headerLocation"]=$conf["failLocation"];
640
			#可省略參數:
641
			#$conf["sessionName"],字串陣列,可以指派session變數的名稱.
642
			#$conf["sessionName"]=array("");
643
			#$conf["sessionValue"]字串陣列,可以指派session變數的內容.
644
			#$conf["sessionValue"]=array("");
645
			#參考資料:
646
			#http://php.net/manual/en/reserved.variables.server.php
647
			#備註:
648
			#無.
649
			$redirectionNow=header::redirectionNow($conf["header::redirectionNow"]);
650
			unset($conf["header::redirectionNow"]);
226 liveuser 651
 
179 liveuser 652
			#如果執行異常
653
			if($redirectionNow["status"]=="false"){
226 liveuser 654
 
179 liveuser 655
				#設置執行失敗
656
				$result["status"]="false";
226 liveuser 657
 
179 liveuser 658
				#設置執行錯誤訊息
659
				$result["error"]=$redirectionNow;
660
 
661
				#回傳結果
662
				return $result;
226 liveuser 663
 
179 liveuser 664
				}#if end
226 liveuser 665
 
179 liveuser 666
			}#if end
226 liveuser 667
 
179 liveuser 668
		}#function validateSession end
669
 
670
	/*
671
	#函式說明:
3 liveuser 672
	#檢查session裏面的使用者$_SESSION["account"]是否在會員清單裏面,與該會員所屬的羣組是否具有權限瀏覽該子功能頁面.
673
	#回傳的參數:
674
	#$result["status"],執行是否正常的識別,"true"代表一切正常,"false"代表不正常
675
	#$result["error"],錯誤訊息
676
	#$result["function"],當前執行的函數名稱
677
	#$result["passed"],是否通過檢查,"true"代表通過檢查,"false"代表沒有通過檢查.
678
	#$result["warning"],記錄不會影響程式執行,但是需要注意的事情。
679
	#$result["sql"],查詢用的sql.
680
	#必填參數:
681
	$conf["thisSubSystemName"]="";#爲該子系統的名稱,若該羣組所能瀏覽的子系統裏面有該名稱,則通過驗證。
682
	$conf["dbName"]="";#爲指定的資料庫名稱,欲選擇的資料庫名稱
683
	$conf["dbPassword"]="";#爲連線到資料庫時要使用的密碼,可省略,若省略則代表不使用密碼
684
	#可省略參數:
685
	#$conf["dbAddress"]="localhost";#爲資料庫的位置,預設爲 "localhost"
686
	#$conf["dbAccount"]="root";#爲用於連入資料庫時要使用的帳號,預設爲 "root"
687
	#$conf["dtName.member"]="member";#儲存會員資料的資料表,預設爲 "member"
688
	#$conf["dtName.userGroup"]="userGroupAuthorties";#儲存羣組資料的資,預設爲 "userGroupAuthorities"
689
	#參考資料:
690
	#無.
691
	#備註:
692
	#無.
693
	*/
694
	public static function checkUserGroupAuthorities($conf){
695
 
696
		#初始化要回傳的結果
697
		$result=array();
226 liveuser 698
 
3 liveuser 699
		#初始化儲存要回傳的警告訊息
700
		$result["warning"]=array();
226 liveuser 701
 
3 liveuser 702
		#記錄當前執行的涵式名稱
703
		$result["function"]=__FUNCTION__;
226 liveuser 704
 
3 liveuser 705
		#如果 $conf 不為陣列
706
		if(gettype($conf)!="array"){
226 liveuser 707
 
3 liveuser 708
			#設置執行失敗
709
			$result["status"]="false";
226 liveuser 710
 
3 liveuser 711
			#設置執行錯誤訊息
712
			$result["error"][]="\$conf變數須為陣列形態";
713
 
714
			#如果傳入的參數為 null
715
			if($conf==null){
226 liveuser 716
 
3 liveuser 717
				#設置執行錯誤訊息
718
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 719
 
3 liveuser 720
				}#if end
721
 
722
			#回傳結果
723
			return $result;
226 liveuser 724
 
3 liveuser 725
			}#if end
226 liveuser 726
 
3 liveuser 727
		#檢查參數
728
		#函式說明:
729
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
730
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
731
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
732
		#$result["function"],當前執行的函式名稱.
733
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
734
		#$result[$shouldBtCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
735
		#$result[$shouldBtCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
736
		#$result[$shouldBtCheckedVarName]["error"],每個參數設定的錯誤訊息
737
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
738
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
739
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
740
		#必填參數:
741
		#$conf["variableCheck::checkArguments"]["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
742
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
743
		#$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
744
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("thisSubSystemName","dbName","dbPassword");
226 liveuser 745
		#$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列 例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double");
3 liveuser 746
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string","string");
747
		#$conf["variableCheck::checkArguments"]["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
748
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
749
		#可以省略的參數:
750
		#$conf["variableCheck::checkArguments"]["canBeEmptyString"],必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true"。
751
		$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
752
		#$conf["variableCheck::checkArguments"]["skipableVariableName"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
753
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("dbAddress","dbAccount","dtName.member","dtName.userGroup");
226 liveuser 754
		#$conf["variableCheck::checkArguments"]["skipableVariableType"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
3 liveuser 755
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string","string","string");
756
		#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是必填參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
757
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("localhost","root","member","userGroupAuthorities");
758
		#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
759
		#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"][]=array();
760
		#參考資料來源:
761
		#array_keys=>http://php.net/manual/en/function.array-keys.php
762
		$checkResult=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
763
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 764
 
3 liveuser 765
		#如果 $checkResult["status"]=="false"
766
		if($checkResult["status"]=="false"){
226 liveuser 767
 
3 liveuser 768
			#設置執行不正常的適別
769
			$result["status"]="false";
226 liveuser 770
 
3 liveuser 771
			#設置執行錯誤的訊息
772
			$result["error"]=$checkResult;
226 liveuser 773
 
3 liveuser 774
			#回傳結果
775
			return $result;
226 liveuser 776
 
3 liveuser 777
			}#if end
778
 
779
		#如果 $checkResult["passed"] 等於 "false"
780
		if($checkResult["passed"]=="false"){
226 liveuser 781
 
3 liveuser 782
			#設置執行不正常的適別
783
			$result["status"]="false";
226 liveuser 784
 
3 liveuser 785
			#設置執行錯誤的訊息
786
			$result["error"]=$checkResult;
226 liveuser 787
 
3 liveuser 788
			#回傳結果
789
			return $result;
226 liveuser 790
 
3 liveuser 791
			}#if end
226 liveuser 792
 
3 liveuser 793
		#檢查是否有session["username"],如果不存在
794
		if(!isset($_SESSION["username"])){
795
 
796
			#設置執行正常的適別
797
			$result["status"]="true";
226 liveuser 798
 
3 liveuser 799
			#設置檢查不通過
800
			$result["passed"]="false";
226 liveuser 801
 
3 liveuser 802
			#設置警告訊息
803
			$result["warning"][]="使用者名稱變數不存在";
226 liveuser 804
 
3 liveuser 805
			#回傳結果
806
			return $result;
807
 
808
			}#if end
809
 
810
		#檢查$_SESSION["username"]所屬的羣組爲何
226 liveuser 811
 
3 liveuser 812
		#函式說明:
813
		#一次取得資料庫、表的資料
814
		#回傳的結果
815
		#$result["status"],執行結果"true"為成功;"false"為執行失敗。
816
		#$result["error"],錯誤訊息陣列。
817
		#$result["dataContent"],爲資料的內容。
818
		#$result["dataContent"][$conf["WhereColumnName"][$i]][$dataSetNum]
819
			#$dataSetNum 爲第$dataSetNum+1筆資料
820
			#$conf["WhereColumnName"][$i] 爲第 $i+1 個欄位的名稱
821
		#$result["dataCount"],爲取得的資料筆數。
822
		#$result["sql"],執行的sql字串.
823
		#必填參數:
824
		$conf["db"]["fastGetDbData"]["dbAddress"]=$conf["dbAddress"];#爲dbServer的位置。
825
		$conf["db"]["fastGetDbData"]["dbAccount"]=$conf["dbAccount"];#爲登入dbServer的帳號。
826
		$conf["db"]["fastGetDbData"]["dbName"]=$conf["dbName"];#爲要存取的資料庫名稱
827
		$conf["db"]["fastGetDbData"]["tableName"]=$conf["dtName.member"];#爲要存取的資料表名稱
828
		$conf["db"]["fastGetDbData"]["columnYouWant"]=array("account","groupName");#你想要的欄位!
829
		#可省略參數:
830
		$conf["db"]["fastGetDbData"]["dbPassword"]=$conf["dbPassword"];#爲要存取dbServer的密碼
831
		$conf["db"]["fastGetDbData"]["WhereColumnName"]=array("account");#用於判斷語句的欄位項目陣列。
832
		$conf["db"]["fastGetDbData"]["WhereColumnValue"]=array($_SESSION["account"]);#用於判斷語句的欄位數值陣列,若與LIKE搭配,則可以在關鍵自字串的左右名加上「%」符號,這樣就可以搜尋具有該字串的內容。
833
		#$conf["db"]["fastGetDbData"]["WhereColumnCombine"]=array("");#用於判斷語句當中需要()起來的判斷式,須爲陣列值,"s"代表「(」,"e"代表「)」 ,若無則須設爲""。
834
		#$conf["db"]["fastGetDbData"]["WhereColumnOperator"]=array("");#用於判斷語句的比較符號陣列,可以用的符號有「"="、">"、"<"、"LIKE"、"NOT LIKE"」,預設都爲「=」。
835
		#$conf["db"]["fastGetDbData"]["WhereColumnAndOr"]=array("");#用於判斷語句條件之間成立的條件是AND還是OR,須爲陣列值。其數量應爲要判斷的欄位數量減一。
836
		#$conf["db"]["fastGetDbData"]["orderItem"]="";#爲排序的項目依據,若要用隨機抽樣,可以用"rand()",可省略。
837
		#$conf["db"]["fastGetDbData"]["ascORdesc"]="";#爲要低增還是遞減排序,asc爲遞增;desc爲遞減。
838
		#$conf["db"]["fastGetDbData"]["numberStart"]="0";#為從第幾筆開始讀取,預設為0,代筆第一筆。
839
		#$conf["db"]["fastGetDbData"]["numLimit"]="30";#為要取幾筆資料,可以省略,省略則表示不限制數目。
840
		#$conf["db"]["fastGetDbData"]["groupBy"]=array("");#爲要以哪幾個欄爲作爲分羣的依據(欄位相同的數值僅會取出一筆)。
841
		$memberData=db::fastGetDbData($conf["db"]["fastGetDbData"]);
842
		unset($conf["db"]["fastGetDbData"]);
843
 
844
		#如果 $memberData["status"] 等於 "false"
845
		if($memberData["status"]=="false"){
226 liveuser 846
 
3 liveuser 847
			#設置執行不正常的適別
848
			$result["status"]="false";
226 liveuser 849
 
3 liveuser 850
			#設置執行錯誤的訊息
851
			$result["error"]=$memberData;
226 liveuser 852
 
3 liveuser 853
			#回傳結果
854
			return $result;
226 liveuser 855
 
3 liveuser 856
			}#if end
857
 
858
		#如果 $memberData["dataCount"] 等於 0
859
		if($memberData["dataCount"]==0){
226 liveuser 860
 
3 liveuser 861
			#代表沒有符合的帳戶
862
			$result["status"]="true";
226 liveuser 863
 
3 liveuser 864
			#設置警告訊息
865
			$result["warning"][]="使用者沒有歸屬於任何群組";
226 liveuser 866
 
3 liveuser 867
			#紀錄查詢用的sql
868
			$result["sql"]=$memberData["sql"];
226 liveuser 869
 
3 liveuser 870
			#設置檢查不通過
871
			$result["passed"]="false";
226 liveuser 872
 
3 liveuser 873
			#回傳結果
874
			return $result;
226 liveuser 875
 
3 liveuser 876
			}#if end
877
 
878
		#取得目前該使用者所屬的羣組
879
		$userGroup=$memberData["dataContent"]["groupName"][0];#符合資料的會員群組名稱。
880
 
881
		#檢查該群組能否瀏覽目標子系統
882
		#函式說明:
883
		#檢查資料庫裏的資料表有無指定條件的資料
884
		#回傳的結果:
885
		#$result["status"],執行是否成功,成功為"true",失敗為"false"。
226 liveuser 886
		#$result["error"],錯誤訊息
887
		#$result["founded"],是否找到資料,"true"代表有符合的資料;"false"代表沒有符合的資料.
3 liveuser 888
		#必填參數:
889
		$conf["db"]["checkDataExists"]["dbAddress"]=$conf["dbAddress"];#爲mysql-Server的位置
890
		$conf["db"]["checkDataExists"]["dbAccount"]=$conf["dbAccount"];#爲用於連入mysql-Server時要使用的帳號
891
		$conf["db"]["checkDataExists"]["selectedDataBaseName"]=$conf["dbName"];#爲指定的資料庫名稱,欲選擇的資料庫名稱
892
		$conf["db"]["checkDataExists"]["selectedDataTableName"]=$conf["dtName.userGroup"];#欲選擇的資料表名稱
893
		#可省略的參數:
894
		$conf["db"]["checkDataExists"]["dbPassword"]=$conf["dbPassword"];#爲連線到mysql-Server時要使用的密碼,可省略,若省略則代表不使用密碼
895
		$conf["db"]["checkDataExists"]["conditionTargetName"]=array("groupName","visitableSubSystem");#用來判斷的資料表數值名稱,需爲陣列,可省略
896
		$conf["db"]["checkDataExists"]["conditionTargetValue"]=array($userGroup,$conf["thisSubSystemName"]);#用來判斷的資料表數值名稱若等於該數值,表示條件成立,需爲陣列,可省略
897
		$checkDataExistResult=db::checkDataExists($conf["db"]["checkDataExists"]);
898
		unset($conf["db"]["checkDataExists"]);
226 liveuser 899
 
3 liveuser 900
		#var_dump($checkDataExistResult);
226 liveuser 901
 
3 liveuser 902
		#如果 $checkDataExistResult["status"] 等於 "false"
903
		if($checkDataExistResult["status"]=="false"){
226 liveuser 904
 
3 liveuser 905
			#代表操作失敗
906
			$result["status"]="false";
226 liveuser 907
 
3 liveuser 908
			#設置警告訊息
909
			$result["error"]=$checkDataExistResult;
226 liveuser 910
 
3 liveuser 911
			#回傳結果
912
			return $result;
226 liveuser 913
 
3 liveuser 914
			}#if end
226 liveuser 915
 
3 liveuser 916
		#反之代表執行成功
917
		else{
226 liveuser 918
 
3 liveuser 919
			#如果 $checkDataExistResult["founded"] 等於 "true"
920
			if($checkDataExistResult["founded"]=="true"){
226 liveuser 921
 
3 liveuser 922
				#代表有相符合的資料
226 liveuser 923
 
3 liveuser 924
				#設置符合權限
925
				$result["passed"]="true";
226 liveuser 926
 
3 liveuser 927
				#設置執行正常
928
				$result["status"]="true";
226 liveuser 929
 
3 liveuser 930
				#回傳結果
931
				return $result;
226 liveuser 932
 
3 liveuser 933
				}#if end
226 liveuser 934
 
935
			#反之代表沒有找到相符的資料,不符合權限.
3 liveuser 936
			else{
226 liveuser 937
 
3 liveuser 938
				#設置符合權限
939
				$result["passed"]="false";
226 liveuser 940
 
3 liveuser 941
				#設置執行正常
226 liveuser 942
				$result["status"]="true";
943
 
3 liveuser 944
				#設置警告訊息
226 liveuser 945
				$result["warning"][]="使用者所屬的群組沒有權限使用該子系統";
946
 
3 liveuser 947
				#回傳結果
226 liveuser 948
				return $result;
949
 
3 liveuser 950
				}#else end
226 liveuser 951
 
3 liveuser 952
			}#else end
226 liveuser 953
 
3 liveuser 954
		#執行到這邊代表執行不正常
955
		$result["status"]="false";
226 liveuser 956
 
3 liveuser 957
		#設置錯誤訊息
226 liveuser 958
		$result["error"][]="不應該執行到的位置";
959
 
3 liveuser 960
		#回傳結果
961
		return $result;
226 liveuser 962
 
3 liveuser 963
		}#函式結束
226 liveuser 964
 
3 liveuser 965
	/*
966
	#函式說明:
967
	#建立以圖片(PNG格式)呈現的驗證碼.
968
	#回傳的解果:
969
	#$result["status"],執行是否正常,"true"代表執行成功,"false"代表執行失敗.
970
	#$result["error"],錯誤訊息.
971
	#$result["function"],檔前執行的函數名稱.
972
	#$result["randNumberWord"],傳驗證碼的內容.
973
	#$result["imgAddress"],包含src圖片的位置與名稱.
974
	#$result["imgSrcVal"],放在src裏面的圖片位置與名稱.
975
	#必填參數:
976
	#$conf["imgAddressAndName"],字串,爲驗證碼圖片儲存的位置與名稱,副檔名程式會自動產生
977
	$conf["imgAddressAndName"]="";
978
	#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
979
	$conf["fileArgu"]=__FILE__;
980
	#可省略參數:
981
	#$conf["num"],字串,爲驗證碼的位數,請輸入阿拉伯數字,預設為"8"位數.
982
	#$conf["num"]="8";
983
	#$conf["disableImg"],字串,是否要取消驗證碼圖片的輸出,"true"為要取消,預設為"false"為不取消
984
	#$conf["disableImg"]="false";
985
	#$conf["imgToData"],字串,預設為"true"代表將圖片轉存成base64圖片,並將原始圖片移除;反之為"false"
986
	#$conf["imgToData"]="true";
987
	#$conf["class"],字串,圖片要套用的css樣式類別.
988
	#$conf["class"]="";
989
	#$conf["content"],字串陣列,允許的亂數陣列內容,預設爲(1~9 and A~Z).
990
	#$conf["content"]=array();
991
	#參考資料:
992
	#無.
993
	#備註:
994
	#無.
207 liveuser 995
	*/
3 liveuser 996
	public static function validationCode(&$conf){
997
 
998
		#初始化要回傳的結果
999
		$result=array();
226 liveuser 1000
 
3 liveuser 1001
		#取得當前執行的函數
1002
		$result["function"]=__FUNCTION__;
1003
 
1004
		#如果 $conf 不為陣列
1005
		if(gettype($conf)!="array"){
226 liveuser 1006
 
3 liveuser 1007
			#設置執行失敗
1008
			$result["status"]="false";
226 liveuser 1009
 
3 liveuser 1010
			#設置執行錯誤訊息
1011
			$result["error"][]="\$conf變數須為陣列形態";
1012
 
1013
			#如果傳入的參數為 null
1014
			if($conf==null){
226 liveuser 1015
 
3 liveuser 1016
				#設置執行錯誤訊息
1017
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 1018
 
3 liveuser 1019
				}#if end
1020
 
1021
			#回傳結果
1022
			return $result;
226 liveuser 1023
 
3 liveuser 1024
			}#if end
1025
 
1026
		#檢查參數
1027
		#函式說明:
1028
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
1029
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1030
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
1031
		#$result["function"],當前執行的函式名稱.
1032
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
1033
		#$result[$shouldBtCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
1034
		#$result[$shouldBtCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
1035
		#$result[$shouldBtCheckedVarName]["error"],每個參數設定的錯誤訊息
1036
		#必填參數:
1037
		#$conf["variableCheck::checkArguments"]["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
1038
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
1039
		#$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
1040
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("imgAddressAndName","fileArgu");
226 liveuser 1041
		#$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列 例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double");
3 liveuser 1042
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string");
1043
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
1044
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
1045
		#可以省略的參數:
1046
		#$conf["variableCheck::checkArguments"]["canBeEmptyString"],必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true"。
1047
		$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
1048
		#$conf["variableCheck::checkArguments"]["skipableVariableName"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
1049
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("fileArgu","num","disableImg","imgToData","class","content");
226 liveuser 1050
		#$conf["variableCheck::checkArguments"]["skipableVariableType"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
3 liveuser 1051
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string","string","string","string","array");
1052
		#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是必填參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
1053
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array(null,"8","false","true",null,array("1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","V","U","W","X","Y","Z"));
1054
		#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
1055
		#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"][]=array();
1056
		$checkResult=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
1057
		unset($conf["variableCheck::checkArguments"]);
1058
 
1059
		#如果檢查失敗
1060
		if($checkResult["status"]=="false"){
226 liveuser 1061
 
3 liveuser 1062
			#設置錯誤狀態
1063
			$result["status"]="false";
226 liveuser 1064
 
3 liveuser 1065
			#設置錯誤提示
1066
			$result["error"]=$checkResult;
226 liveuser 1067
 
3 liveuser 1068
			return $result;
226 liveuser 1069
 
3 liveuser 1070
			}#if end
1071
 
1072
		#如果檢查不通過
1073
		if($checkResult["passed"]=="false"){
226 liveuser 1074
 
3 liveuser 1075
			#設置錯誤狀態
1076
			$result["status"]="false";
226 liveuser 1077
 
3 liveuser 1078
			#設置錯誤提示
1079
			$result["error"]=$checkResult;
226 liveuser 1080
 
3 liveuser 1081
			return $result;
226 liveuser 1082
 
3 liveuser 1083
			}#if end
1084
 
1085
		#可以出現的亂數字碼
1086
		$target=$conf["content"];
1087
 
1088
		#建立$randNumberWordUn的初始值
1089
		$randNumberWordUn="";
1090
 
1091
		#跑$num次
1092
		for($i=0;$i<=($conf["num"]-1);$i++){
1093
 
1094
			#則將該代數值轉換成對應的target陣列值。
1095
			$number[$i]=$target[rand(0,count($target)-1)];
226 liveuser 1096
 
3 liveuser 1097
			#將轉換完的驗證碼合併
1098
			$randNumberWordUn=$randNumberWordUn.$number[$i];
226 liveuser 1099
 
3 liveuser 1100
			}#for end
226 liveuser 1101
 
1102
		#將合併好的驗證碼到randNumberWord
3 liveuser 1103
		$randNumberWord = $randNumberWordUn;
1104
 
1105
		#如果有設定產出圖片
1106
		if($conf["disableImg"]=="false"){
226 liveuser 1107
 
3 liveuser 1108
			#如果驗證碼數量爲$num則放置驗證碼的圖片寬度就等於
1109
			$backgroundImgWidth=$conf["num"]*10;
1110
 
1111
			#函式說明:
1112
			#建立含有文字的png圖檔
1113
			#回傳的結果:
1114
			#$result["status"],執行是否正常,"true"為正常,"false"為不正常.
1115
			#$result["error"],錯誤訊息陣列
1116
			#$result["function"],當前執行的函數名稱
1117
			#$result["content"],圖片的位置與檔案名稱
1118
			#必填參數:
1119
			$conf["img::create"]["imgWidth"]=$backgroundImgWidth;#圖片的寬度(int)
1120
			$conf["img::create"]["imgHeight"]=20;#圖片的高度(int)
1121
			$conf["img::create"]["imgStoreAddAndName"]=$conf["imgAddressAndName"];#圖片要儲存到的地方以及其名稱
1122
			#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
1123
			$conf["img::create"]["fileArgu"]=$conf["fileArgu"];
1124
			#可省略參數:
1125
			#$conf["bgRedNum"]=0;#圖片的紅色底色數值0~255,預設爲0
1126
			#$conf["bgGreenNum"]=0;#圖片的綠色底色數值0~255,預設爲0
1127
			#$conf["bgBlueNum"]=0;#圖片的藍色底色數值0~255,預設爲0
1128
			$conf["img::create"]["strOnImg"]=$randNumberWord;#要出現在圖片上的文字,預設為""
1129
			#$conf["textRedNum"]=255;#文字顏色的紅色數值0~255,預設爲255
1130
			#$conf["textGreenNum"]=255;#文字顏色的綠色數值0~255,預設爲255
1131
			#$conf["textBlueNum"]=255;#文字顏色的藍色數值0~255,預設爲255
1132
			#$conf["drawLine"]=array("");#可以畫多條線,參數格式爲「0,0,200,100,50,100,150 」,每個參數依序代表x0、y0爲線條的起始點;x1、y1爲線條的終點;red、green、blue爲線條的顏色數值(0~255)
1133
			#在圖片上畫左下到右上的線
1134
			$conf["img::create"]["drawLine"][]="0,0,".$backgroundImgWidth.",".$conf["img::create"]["imgHeight"].",255,0,0";#可以畫多條線,參數格式爲「0,0,200,100,50,100,150 」,每個參數依序代表x0、y0爲線條的起始點;x1、y1爲線條的終點;red、green、blue爲線條的顏色數值(0~255)
1135
			#在圖片上畫左上到右下的線
1136
			$conf["img::create"]["drawLine"][]="0,".$conf["img::create"]["imgHeight"].",".$backgroundImgWidth.",0,0,255,0";#可以畫多條線,參數格式爲「0,0,200,100,50,100,150 」,每個參數依序代表x0、y0爲線條的起始點;x1、y1爲線條的終點;red、green、blue爲線條的顏色數值(0~255)
1137
			#在圖片上畫左中到右中的線
1138
			$conf["img::create"]["drawLine"][]="0,".($conf["img::create"]["imgHeight"]/2).",".$backgroundImgWidth.",".($conf["img::create"]["imgHeight"]/2).",0,0,255";#可以畫多條線,參數格式爲「0,0,200,100,50,100,150 」,每個參數依序代表x0、y0爲線條的起始點;x1、y1爲線條的終點;red、green、blue爲線條的顏色數值(0~255)
1139
			#在圖片上畫中上到中下的線
1140
			$conf["img::create"]["drawLine"][]=($backgroundImgWidth/2).",".$conf["img::create"]["imgHeight"].",".($backgroundImgWidth/2).",0,50,50,50";#可以畫多條線,參數格式爲「0,0,200,100,50,100,150 」,每個參數依序代表x0、y0爲線條的起始點;x1、y1爲線條的終點;red、green、blue爲線條的顏色數值(0~255)
1141
			$conf["img::create"]["replaceOriImg"]="true";#如果遇到相同名字會將之刪除,flase爲不要刪除,預設為"false".
1142
			#參考資料來源:
1143
			#imagecreate=>http://php.net/manual/en/function.imagecreate.php
1144
			#imagecolorallocate=>http://php.net/manual/en/function.imagecolorallocate.php
1145
			#imagestring=>http://php.net/manual/en/function.imagestring.php
1146
			#imageline=>http://php.net/manual/en/function.imageline.php
1147
			#imagepng=>http://php.net/manual/en/function.imagepng.php
1148
			#imagedestroy=>http://php.net/manual/en/function.imagedestroy.php
1149
			$imgAddress=img::create($conf["img::create"]);
1150
			unset($conf["img::create"]);
226 liveuser 1151
 
3 liveuser 1152
			#如果建立圖片失敗
1153
			if($imgAddress["status"]=="false"){
226 liveuser 1154
 
3 liveuser 1155
				#設置錯誤狀態
1156
				$result["status"]="false";
226 liveuser 1157
 
3 liveuser 1158
				#設置錯誤提示
1159
				$result["error"]=$imgAddress;
226 liveuser 1160
 
3 liveuser 1161
				return $result;
226 liveuser 1162
 
3 liveuser 1163
				}#if end
226 liveuser 1164
 
3 liveuser 1165
			#設置圖片的位置與名稱
1166
			$result["imgAddress"]=$imgAddress["content"];
226 liveuser 1167
 
3 liveuser 1168
			#如果要把圖片轉存成base64
1169
			if($conf["imgToData"]=="true"){
226 liveuser 1170
 
3 liveuser 1171
				#函數說明:
1172
				#用data:mimeType;base64,imgVar的形式來提供圖片的連結,亦即圖片儲存在變數裡面,本函式包含顯示圖片的語法.
1173
				#回傳結果:
1174
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1175
				#$result["error"],錯誤訊息
1176
				#$result["content"]["imgWithoutImgTag"],img的src數值內容.
1177
				#$result["content"]["img"],圖片的連結資訊
226 liveuser 1178
				#$result["function"],當前執行的函數名稱
3 liveuser 1179
				#必填參數:
1180
				#$conf["imgPosition"],要轉存成2元碼的圖片檔案位置與名稱
1181
				$conf["img::data"]["imgPosition"]=$result["imgAddress"];
1182
				#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑.
1183
				$conf["img::data"]["fileArgu"]=$conf["fileArgu"];
1184
				#可省略參數:
1185
				#$conf["alt"],若沒有圖片要用什麼文字顯示。
1186
				#$conf["alt"]="";
1187
				#$conf["class"],圖片要套用的css樣式名稱.
226 liveuser 1188
 
3 liveuser 1189
				#如果有設定 class
1190
				if(isset($conf["class"])){
226 liveuser 1191
 
3 liveuser 1192
					#設置圖片的 class
1193
					$conf["img::data"]["class"]=$conf["class"];
226 liveuser 1194
 
3 liveuser 1195
					}#if end
226 liveuser 1196
 
3 liveuser 1197
				#$conf["mimeType"],2元碼的內容是什麼,預設為"image/*".
1198
				#$conf["mimeType"]="image/*";
1199
				#$conf["compressType"],2元碼壓縮的方式,預設為"base64".
1200
				#$conf["compressType"]="base64";
1201
				#$conf["delImg"],讀取完圖片檔案後,要移除圖片嗎?"true"代表要移除,"false"代表不要移除,預設為"false".
1202
				$conf["img::data"]["delImg"]="true";
1203
				#參考資料:
1204
				#將檔案用字串變數儲存起來=>http://php.net/manual/en/function.file-get-contents.php
1205
				#壓縮2元碼=>http://php.net/manual/en/function.base64-encode.php
1206
				$data=img::data($conf["img::data"]);
1207
				unset($conf["img::data"]);
226 liveuser 1208
 
3 liveuser 1209
				#如果轉存成base64失敗
1210
				if($data["status"]=="false"){
226 liveuser 1211
 
3 liveuser 1212
					#設置錯誤狀態
1213
					$result["status"]="false";
226 liveuser 1214
 
3 liveuser 1215
					#設置錯誤提示
1216
					$result["error"]=$data;
226 liveuser 1217
 
3 liveuser 1218
					return $result;
226 liveuser 1219
 
3 liveuser 1220
					}#if end
226 liveuser 1221
 
1222
				#設置圖片src的數值
3 liveuser 1223
				$result["imgSrcVal"]=$data["content"]["imgWithoutImgTag"];
1224
 
1225
				#更新圖片位置為base64
1226
				$result["imgAddress"]=$data["content"]["img"];
226 liveuser 1227
 
3 liveuser 1228
				}#if end
226 liveuser 1229
 
3 liveuser 1230
			}#if end
226 liveuser 1231
 
3 liveuser 1232
		#設置執行成功狀態
1233
		$result["status"]="true";
226 liveuser 1234
 
3 liveuser 1235
		#設置亂數文字結果
1236
		$result["randNumberWord"]=$randNumberWord;
226 liveuser 1237
 
3 liveuser 1238
		#回傳內容
1239
		return $result;
1240
 
1241
		}#函式結束
226 liveuser 1242
 
3 liveuser 1243
	/*
1244
	#函式說明:
1245
	#產生亂數驗證碼或驗證亂數驗證碼與表單資料
1246
	#回傳結果:
1247
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1248
	#$result["error"],錯誤訊息.
1249
	#$result["argu"],使用的參數.
1250
	#$result["function"],當前執行的函數名稱.
1251
	#$result["content"],語法.
1252
	#必填參數:
1253
	#$conf["fileArgu"]
1254
	$conf["fileArgu"]=__FILE__;
1255
	#可省略參數:
1256
	#$conf["sendedName"],字串,傳送的post變數名稱,預設爲"formData".
226 liveuser 1257
	#$conf["sendedName"]="";
3 liveuser 1258
	#$conf["codeFormName"],字串,驗證碼表單的名稱,預設為"randomCode",對應的 session 變數名稱為 authenticate.verifyCodeAndFormData.$conf["codeFormName"]
1259
	#$conf["codeFormName"]="randomCode";
1260
	#$conf["names"],字串陣列,需要處理的表單必填變數.
1261
	#$conf["names"]=array();
1262
	#$conf["skipNames"],字串陣列,需要處理的表單可爲空變數.
226 liveuser 1263
	#$conf["skipNames"]=array();
3 liveuser 1264
	#參考資料:
1265
	#無.
1266
	#備註:
1267
	#請跟javaScript類別的randomCode函式搭配.應用情境在會員登入、驗證與寄送Email表單.
1268
	*/
1269
	function verifyCodeAndFormData($conf){
1270
 
1271
		#初始化要回傳的結果
1272
		$result=array();
1273
 
1274
		#取得當前執行的函數名稱
1275
		$result["function"]=__FUNCTION__;
1276
 
1277
		#如果沒有參數
1278
		if(func_num_args()==0){
1279
 
1280
			#設置執行失敗
1281
			$result["status"]="false";
1282
 
1283
			#設置執行錯誤訊息
1284
			$result["error"]="函數".$result["function"]."需要參數";
1285
 
1286
			#回傳結果
1287
			return $result;
1288
 
1289
			}#if en
1290
 
1291
		#取得參數
1292
		$result["argu"]=$conf;
1293
 
1294
		#如果 $conf 不為陣列
1295
		if(gettype($conf)!=="array"){
1296
 
1297
			#設置執行失敗
1298
			$result["status"]="false";
1299
 
1300
			#設置執行錯誤訊息
1301
			$result["error"][]="\$conf變數須為陣列形態";
1302
 
1303
			#如果傳入的參數為 null
1304
			if(is_null($conf)){
1305
 
1306
				#設置執行錯誤訊息
1307
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
1308
 
1309
				}#if end
1310
 
1311
			#回傳結果
1312
			return $result;
1313
 
1314
			}#if end
226 liveuser 1315
 
3 liveuser 1316
		#函式說明:
1317
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
1318
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1319
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
1320
		#$result["function"],當前執行的函式名稱.
1321
		#$result["argu"],設置給予的參數.
1322
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
1323
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
1324
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
1325
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
1326
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
1327
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
1328
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
1329
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
1330
		#必填寫的參數:
1331
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
1332
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
1333
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
1334
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
1335
		#可以省略的參數:
1336
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
1337
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("fileArgu");
1338
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
1339
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string");
1340
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
1341
		$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
1342
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
1343
		#$conf["canNotBeEmpty"]=array();
1344
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
1345
		#$conf["canBeEmpty"]=array();
1346
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
1347
		$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("sendedName","codeFormName","names","skipNames");
1348
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
1349
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("sendedName","codeFormName","names","skipNames");
1350
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
1351
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string","array","array");
1352
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
1353
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("formData","randomCode",null,null);
1354
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
1355
		#$conf["disallowAllSkipableVarIsEmpty"]="";
1356
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
1357
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
1358
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
1359
		#$conf["arrayCountEqualCheck"][]=array();
1360
		#參考資料來源:
1361
		#array_keys=>http://php.net/manual/en/function.array-keys.php
1362
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
1363
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 1364
 
3 liveuser 1365
		#如果出錯
1366
		if($checkArguments["status"]==="false"){
1367
 
1368
			#設置執行失敗
1369
			$result["status"]="false";
1370
 
1371
			#設置執行失敗
1372
			$result["error"]=$checkArguments;
1373
 
1374
			#回傳結果
1375
			return $result;
1376
 
1377
			}#if end
1378
 
1379
		#如果出錯
1380
		if($checkArguments["passed"]==="false"){
1381
 
1382
			#設置執行失敗
1383
			$result["status"]="false";
1384
 
1385
			#設置執行失敗
1386
			$result["error"]=$checkArguments;
1387
 
1388
			#回傳結果
1389
			return $result;
1390
 
226 liveuser 1391
			}#if end
3 liveuser 1392
 
226 liveuser 1393
		#如果沒有設置 $conf["names"]
3 liveuser 1394
		if(!isset($conf["names"])){
226 liveuser 1395
 
3 liveuser 1396
			#初始化
1397
			$conf["names"]=array($conf["codeFormName"]);
226 liveuser 1398
 
3 liveuser 1399
			}#if end
226 liveuser 1400
 
3 liveuser 1401
		#反之
1402
		else{
226 liveuser 1403
 
3 liveuser 1404
			#加上驗證碼表單
1405
			$conf["names"][]=$conf["codeFormName"];
226 liveuser 1406
 
3 liveuser 1407
			}#else end
1408
 
226 liveuser 1409
		#如果沒有設置 $conf["skipNames"]
3 liveuser 1410
		if(!isset($conf["skipNames"])){
226 liveuser 1411
 
3 liveuser 1412
			#初始化
1413
			$conf["skipNames"]=array();
226 liveuser 1414
 
3 liveuser 1415
			}#if end
226 liveuser 1416
 
3 liveuser 1417
		#函式說明:
1418
		#建立一個陣列,可以指派陣列的內容,然後回傳
1419
		#回傳的結果:
1420
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1421
		#$result["function"],當前執行的函數名稱.
1422
		#$result["error"],錯誤訊息陣列.
1423
		#$result["argu"],使用的參數.
1424
		#$result["content"],爲陣列變數內容
1425
		#必填參數:
1426
		$conf["arrays::create"]["arrayContent"]=array("string");#陣列元素的內容,須爲陣列值。
1427
		#可省略參數:
1428
		$conf["arrays::create"]["arrayCounts"]=count($conf["names"])+count($conf["skipNames"]);#爲陣列的元素有幾個,若沒設定,則數量爲$conf["arrayContent"]裏的元素數量
1429
		$conf["arrays::create"]["theSameAs"]="true";#若設爲"true",則所有元素內容都跟第一個元素內容相同
1430
		$create=arrays::create($conf["arrays::create"]);
1431
		unset($conf["arrays::create"]);
226 liveuser 1432
 
3 liveuser 1433
		#如果失敗
1434
		if($create["status"]==="false"){
226 liveuser 1435
 
3 liveuser 1436
			#設置執行失敗
1437
			$result["status"]="false";
1438
 
1439
			#設置執行失敗
1440
			$result["error"]=$create;
1441
 
1442
			#回傳結果
1443
			return $result;
226 liveuser 1444
 
3 liveuser 1445
			}#if end
226 liveuser 1446
 
3 liveuser 1447
		#函式說明:
1448
		#將多個一維陣列串聯起來,key從0開始排序.
1449
		#回傳的結果:
1450
		#$result["status"],"true"表執行正常,"false"代表執行不正常.
1451
		#$result["error"],錯誤訊息陣列.
1452
		#$result["function"],當前執行的函數.
1453
		#$result["content"],合併好的一維陣列.
1454
		#必填的參數
1455
		#$conf["inputArray"],陣列,要合併的一維陣列變數,例如:=array($array1,$array2);
1456
		$conf["arrays::mergeArray"]["inputArray"]=array($conf["names"],$conf["skipNames"]);
1457
		#可省略的參數:
1458
		#$conf["allowRepeat"],字串,預設為"true",允許重複的結果;若為"false"則不會出現重複的元素內容.
1459
		#$conf["allowRepeat"]="true";
1460
		$mergeArray=arrays::mergeArray($conf["arrays::mergeArray"]);
1461
		unset($conf["arrays::mergeArray"]);
226 liveuser 1462
 
3 liveuser 1463
		#如果失敗
1464
		if($mergeArray["status"]==="false"){
226 liveuser 1465
 
3 liveuser 1466
			#設置執行失敗
1467
			$result["status"]="false";
1468
 
1469
			#設置執行失敗
1470
			$result["error"]=$mergeArray;
1471
 
1472
			#回傳結果
1473
			return $result;
226 liveuser 1474
 
3 liveuser 1475
			}#if end
226 liveuser 1476
 
3 liveuser 1477
		#如表單變數存在
1478
		if(isset($_POST[$conf["sendedName"]])){
1479
 
1480
			#解碼
1481
			$urlEncodeStr=base64_decode($_POST[$conf["sendedName"]]);
226 liveuser 1482
 
3 liveuser 1483
			#解碼
226 liveuser 1484
			$jsonEncodeStr=urldecode($urlEncodeStr);
3 liveuser 1485
 
1486
			#解碼
1487
			$obj=json_decode($jsonEncodeStr);
1488
 
1489
			#debug
1490
			#echo $urlEncodeStr."<p>";
1491
			#echo $jsonEncodeStr."<p>";
1492
			#var_dump($obj);
1493
 
1494
			#如果爲空物件
1495
			if($jsonEncodeStr==="{}"){
1496
 
1497
				#建立亂數驗證碼
1498
				#函式說明:
1499
				#建立以圖片(PNG格式)呈現的驗證碼.
1500
				#回傳的解果:
1501
				#$result["status"],執行是否正常,"true"代表執行成功,"false"代表執行失敗.
1502
				#$result["error"],錯誤訊息.
1503
				#$result["function"],檔前執行的函數名稱.
1504
				#$result["randNumberWord"],傳驗證碼的內容.
1505
				#$result["imgSrcVal"],放在src裏面的圖片位置與名稱.
1506
				#$result["imgAddress"],圖片的位置與名稱.
1507
				#必填的參數:
1508
				#$conf["imgAddressAndName"],字串,爲驗證碼圖片儲存的位置與名稱,副檔名程式會自動產生
1509
				$conf["authenticate::validationCode"]["imgAddressAndName"]="/tmp/authenticate.verifyCodeAndFormData.".$conf["codeFormName"];
1510
				#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
1511
				$conf["authenticate::validationCode"]["fileArgu"]=$conf["fileArgu"];
1512
				#可省略參數:
1513
				#$conf["num"],字串,爲驗證碼的位數,請輸入阿拉伯數字,預設為"8"位數.
1514
				#$conf["num"]="8";
1515
				#$conf["disableImg"],字串,是否要取消驗證碼圖片的輸出,"true"為要取消,預設為"false"為不取消
1516
				#$conf["disableImg"]="false";
1517
				#$conf["imgToData"],字串,預設為"true"代表將圖片轉存成base64圖片,並將原始圖片移除;反之為"false"
1518
				#$conf["imgToData"]="true";
1519
				#$conf["class"],字串,圖片要套用的css樣式類別.
1520
				#$conf["class"]="";
1521
				$validationCode=authenticate::validationCode($conf["authenticate::validationCode"]);
1522
				unset($conf["authenticate::validationCode"]);
1523
 
1524
				#如果建立亂碼圖片失敗
226 liveuser 1525
				if($validationCode["status"]==="false"){
3 liveuser 1526
 
1527
					#設置執行失敗
1528
					$result["status"]="false";
1529
 
1530
					#設置執行失敗
1531
					$result["error"]=$validationCode;
1532
 
1533
					#回傳結果
1534
					return $result;
1535
 
1536
					}#if end
1537
 
1538
				#設置驗證碼到session變數
1539
				$_SESSION["authenticate.verifyCodeAndFormData.".$conf["codeFormName"]]=$validationCode["randNumberWord"];
226 liveuser 1540
 
3 liveuser 1541
				#取得驗證碼長度
1542
				$json["content"]["randomCodeLength"]=strlen($_SESSION["authenticate.verifyCodeAndFormData.".$conf["codeFormName"]]);
1543
 
1544
				#設置圖片data
1545
				$json["content"]["randomCodeSrc"]=$validationCode["imgSrcVal"];
1546
 
1547
				#設置執行失敗
1548
				$json["status"]="false";
1549
 
1550
				#設置執行錯誤
1551
				$json["error"]="no data";
1552
 
1553
				#設置 json
1554
				$result["content"]=json_encode($json);
1555
 
1556
				#運行正常
1557
				$result["status"]="true";
1558
 
1559
				#回傳結果
1560
				return $result;
1561
 
226 liveuser 1562
				}#if end
1563
 
3 liveuser 1564
			#反之有內容
1565
			else{
226 liveuser 1566
 
3 liveuser 1567
				#清空post變數
226 liveuser 1568
				$_POST=array();
1569
 
3 liveuser 1570
				#debug
1571
				#echo $urlEncodeStr."<p>";
1572
				#echo $jsonEncodeStr."<p>";
1573
				#var_dump($obj);
1574
 
1575
				#依據每個物件
1576
				foreach($obj as $key=>$val){
1577
 
1578
					#設置對應的 post 數值
226 liveuser 1579
					$_POST[$key]=$val;
1580
 
3 liveuser 1581
					}#foreach end
1582
 
1583
				#debug
1584
				#echo json_encode($_POST);
1585
 
1586
				#如果驗證碼正確
1587
				if($_POST["randomCode"]===$_SESSION["authenticate.verifyCodeAndFormData.".$conf["codeFormName"]]){
1588
 
1589
					#設置執行正常
1590
					$json["status"]="true";
1591
 
1592
					#設置通過驗證
1593
					$json["passed"]="true";
1594
 
226 liveuser 1595
					#設置訊息
3 liveuser 1596
					$json["msg"]="通過驗證";
226 liveuser 1597
 
3 liveuser 1598
					#取得要檢查的變數名稱
1599
					$mustBeFilledVariableName=$mergeArray["content"];
226 liveuser 1600
 
3 liveuser 1601
					#取得要檢查的變數形態
226 liveuser 1602
					$mustBeFilledVariableType=$create["content"];
1603
 
3 liveuser 1604
					#檢查POST參數
1605
					#函式說明:
1606
					#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
1607
					#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1608
					#$reuslt["error"],執行不正常結束的錯訊息陣列.
1609
					#$result["function"],當前執行的函式名稱.
1610
					#$result["argu"],設置給予的參數.
1611
					#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
1612
					#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
1613
					#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
1614
					#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
1615
					#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
1616
					#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
1617
					#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
1618
					#$result["notNeedVar"],字串陣列,多餘的參數名稱.
1619
					#必填寫的參數:
1620
					#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
1621
					$conf["variableCheck::checkArguments"]["varInput"]=&$_POST;
1622
					#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
1623
					$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
1624
					#可以省略的參數:
1625
					#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
1626
					$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=$mustBeFilledVariableName;
1627
					#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
1628
					$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=$mustBeFilledVariableType;
1629
					#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
1630
					$conf["variableCheck::checkArguments"]["canBeEmptyString"]="true";
1631
					#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
1632
					$conf["variableCheck::checkArguments"]["canNotBeEmpty"]=$conf["names"];
1633
					#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
1634
					#$conf["canBeEmpty"]=array();
1635
					#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
1636
					#$conf["skipableVariableCanNotBeEmpty"]=array();
1637
					#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
1638
					#$conf["skipableVariableName"]=array();
1639
					#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
1640
					#$conf["skipableVariableType"]=array();
1641
					#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
1642
					#$conf["skipableVarDefaultValue"]=array("");
1643
					#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
1644
					#$conf["disallowAllSkipableVarIsEmpty"]="";
1645
					#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
1646
					#$conf["disallowAllSkipableVarIsEmptyArray"]="";
1647
					#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
1648
					#$conf["arrayCountEqualCheck"][]=array();
1649
					#參考資料來源:
1650
					#array_keys=>http://php.net/manual/en/function.array-keys.php
1651
					$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
1652
					unset($conf["variableCheck::checkArguments"]);
1653
 
1654
					#如果檢查失敗
1655
					if($checkArguments["status"]==="false"){
226 liveuser 1656
 
3 liveuser 1657
						#設置執行不正常
1658
						$jsons["status"]="false";
226 liveuser 1659
 
3 liveuser 1660
						#設置檢查不通過
1661
						$json["passed"]="false";
1662
 
1663
						#設置執行失敗
1664
						$json["error"]=$checkArguments;
1665
 
1666
						#設置 json
1667
						$result["content"]=json_encode($json);
226 liveuser 1668
 
3 liveuser 1669
						#運行正常
1670
						$result["status"]="true";
1671
 
1672
						#回傳結果
1673
						return $result;
226 liveuser 1674
 
3 liveuser 1675
						}#if end
226 liveuser 1676
 
3 liveuser 1677
					#如果檢查不通過
1678
					if($checkArguments["passed"]==="false"){
226 liveuser 1679
 
3 liveuser 1680
						#設置執行正常
1681
						$json["status"]="true";
226 liveuser 1682
 
3 liveuser 1683
						#設置檢查不通過
1684
						$json["passed"]="false";
1685
 
1686
						#設置執行失敗
1687
						$json["error"]=$checkArguments;
1688
 
1689
						#設置 json
1690
						$result["content"]=json_encode($json);
226 liveuser 1691
 
1692
						#運行正常
3 liveuser 1693
						$result["status"]="true";
1694
 
1695
						#回傳結果
1696
						return $result;
226 liveuser 1697
 
3 liveuser 1698
						}#if end
1699
 
1700
					#設置執行正常
1701
					$json["status"]="true";
1702
 
1703
					#設置通過驗證
226 liveuser 1704
					$json["passed"]="true";
1705
 
3 liveuser 1706
					#設置抓到的 post 內容
1707
					$json["post"]=$_POST;
1708
 
1709
					#設置 json
1710
					$result["content"]=json_encode($json);
226 liveuser 1711
 
1712
					#運行正常
3 liveuser 1713
					$result["status"]="true";
1714
 
1715
					#回傳結果
1716
					return $result;
226 liveuser 1717
 
3 liveuser 1718
					}#if end
1719
 
1720
				#反之
1721
				else{
1722
 
1723
					#設置執行正常
1724
					$json["status"]="true";
1725
 
1726
					#設置未通過驗證
1727
					$json["passed"]="false";
1728
 
226 liveuser 1729
					#設置錯誤訊息
3 liveuser 1730
					$json["error"]="驗證碼錯誤";
226 liveuser 1731
 
3 liveuser 1732
					#設置 json
1733
					$result["content"]=json_encode($json);
226 liveuser 1734
 
1735
					#運行正常
3 liveuser 1736
					$result["status"]="true";
1737
 
226 liveuser 1738
					#回傳結果
1739
					return $result;
3 liveuser 1740
 
1741
					}#else end
1742
 
1743
				}#else end
226 liveuser 1744
 
3 liveuser 1745
			#設置執行正常
1746
			$json["status"]="false";
1747
 
1748
			#設置通過驗證
1749
			$json["passed"]="false";
1750
 
226 liveuser 1751
			#設置錯誤訊息
1752
			$json["error"]="缺少POST變數";
1753
 
3 liveuser 1754
			#設置抓到的 post 內容
1755
			$json["post"]=$_POST;
1756
 
1757
			#設置 json
1758
			$result["content"]=json_encode($json);
226 liveuser 1759
 
1760
			#運行不正常
3 liveuser 1761
			$result["status"]="false";
1762
 
226 liveuser 1763
			#設置錯誤訊息
61 liveuser 1764
			$result["error"][]="缺少POST變數";
3 liveuser 1765
 
1766
			#回傳結果
1767
			return $result;
226 liveuser 1768
 
3 liveuser 1769
			}#if end
1770
 
1771
		}#function verifyCodeAndFormData end
226 liveuser 1772
 
3 liveuser 1773
	/*
1774
	#函式說明:
61 liveuser 1775
	#加密或編碼字串,可以用的方法有sha1,md5,password_sha,qbpwcf,bin2hex,hex2bin,gpg,sha1可以回傳20或40的數值字串,md5可以回傳32個數值字串,password_hash可以回傳60~255個字元.
3 liveuser 1776
	#回傳結果:
226 liveuser 1777
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
3 liveuser 1778
	#$result["function"],當前執行的函數名稱.
1779
	#$result["content"],加密後的結果.
1780
	#$result["error"],錯誤訊息陣列.
1781
	#$result["argu"],使用的參數.
1782
	#必填參數:
1783
	#$conf["enCodeStr"],any,要加密的字串,陣列,物件.
1784
	$conf["enCodeStr"]="";
170 liveuser 1785
	#$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加解密.
3 liveuser 1786
	$conf["enCodeType"]="sha1";
1787
	#可省略參數:
1788
	#$conf["sha1Raw"],字串,sha1加密的結果要用20個0與1組合還是要用40位數的16進位數值,"true"代表前者,"false"代表後者,預設為"false".
1789
	#$conf["sha1Raw"]="false";
1790
	#$conf["p_hash"],字串,p_hash加密過後的字串,該參數若存在且$conf["enCodeType"]為"p_hash",則代表是要檢查$conf["enCodeStr"]是否為符合$conf["p_hash"]的密碼.
1791
	#$conf["p_hash"]="";
1792
	#$conf["keyForAes256"],字串,用來 AES256 加解密的金鑰.
1793
	#$conf["keyForAes256"]="";
1794
	#$conf["aes256Encode"],字串,"true"代表是要加密,"false"代表是要解密.
1795
	#$conf["aes256Encode"]="";
1796
	#$conf["qbpwcfDecode"],字串,若"enCodeType"為"qbpwcf",則預設為"false",代表加密;反之為"true",代表解密.
1797
	#$conf["qbpwcfDecode"]="false";
61 liveuser 1798
	#$conf["gpgDecrypt"],字串,若"enCodeType"為"gpg",則預設為"false",代表加密;反之為"true",代表解密.
1799
	#$conf["gpgDecrypt"]="false";
170 liveuser 1800
	#$conf["gpgId"],字串,若"enCodeType"為"gpg"時,要使用的gpg id,預設為gnupgId.
61 liveuser 1801
	#$conf["gpgId"]="";
3 liveuser 1802
	#參考資料:
1803
	#sha1=>http://php.net/manual/en/function.sha1.php
1804
	#md5=>http://php.net/manual/en/function.md5.php
1805
	#password_hash=>http://php.net/manual/en/function.password-hash.php
1806
	#password_verify=>http://php.net/manual/en/function.password-verify.php
1807
	#json_decode=>https://www.php.net/manual/en/function.json-decode.php
1808
	#備註:
1809
	#無.
1810
	*/
1811
	public static function enCodeStr(&$conf){
226 liveuser 1812
 
3 liveuser 1813
		#初始化要回傳的結果
1814
		$result=array();
226 liveuser 1815
 
3 liveuser 1816
		#記錄當前執行的函數
1817
		$result["function"]=__FUNCTION__;
226 liveuser 1818
 
3 liveuser 1819
		#如果 $conf 不為陣列
1820
		if(gettype($conf)!="array"){
226 liveuser 1821
 
3 liveuser 1822
			#設置執行失敗
1823
			$result["status"]="false";
226 liveuser 1824
 
3 liveuser 1825
			#設置執行錯誤訊息
1826
			$result["error"][]="\$conf變數須為陣列形態";
1827
 
1828
			#如果傳入的參數為 null
1829
			if($conf==null){
226 liveuser 1830
 
3 liveuser 1831
				#設置執行錯誤訊息
1832
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 1833
 
3 liveuser 1834
				}#if end
1835
 
1836
			#回傳結果
1837
			return $result;
226 liveuser 1838
 
3 liveuser 1839
			}#if end
226 liveuser 1840
 
3 liveuser 1841
		#計數使用的參數
1842
		$result["argu"]=$conf;
226 liveuser 1843
 
3 liveuser 1844
		#檢查參數
1845
		#函式說明:
1846
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
1847
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1848
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
1849
		#$result["function"],當前執行的函式名稱.
1850
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
1851
		#$result[$shouldBtCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
1852
		#$result[$shouldBtCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
1853
		#$result[$shouldBtCheckedVarName]["error"],每個參數設定的錯誤訊息
1854
		#必填參數:
1855
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
1856
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
1857
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
1858
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("enCodeStr","enCodeType");
226 liveuser 1859
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列 例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double");
3 liveuser 1860
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array(null,"string");
1861
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
1862
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
1863
		#可以省略的參數:
1864
		#$conf["canBeEmptyString"],必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true"。
1865
		$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
1866
		#$conf["skipableVariableName"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
61 liveuser 1867
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("sha1Raw","p_hash","keyForAes256","aes256Encode","qbpwcfDecode","gpgDecrypt","gpgId");
226 liveuser 1868
		#$conf["skipableVariableType"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
61 liveuser 1869
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string","string","string","string","string","string");
3 liveuser 1870
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是必填參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
170 liveuser 1871
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("false",null,null,null,"false","false",gnupgId);
3 liveuser 1872
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
1873
		#$conf["arrayCountEqualCheck"][]=array();
1874
		$checkResult=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
1875
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 1876
 
3 liveuser 1877
		#如果檢查參數失敗
61 liveuser 1878
		if($checkResult["status"]==="false"){
226 liveuser 1879
 
3 liveuser 1880
			#設置執行失敗
1881
			$result["status"]="false";
226 liveuser 1882
 
3 liveuser 1883
			#設置執行錯誤
1884
			$result["error"]=$checkResult;
226 liveuser 1885
 
3 liveuser 1886
			#回傳結果
1887
			return $result;
226 liveuser 1888
 
3 liveuser 1889
			}#if end
226 liveuser 1890
 
3 liveuser 1891
		#如果檢查參數不通過
61 liveuser 1892
		if($checkResult["passed"]==="false"){
226 liveuser 1893
 
3 liveuser 1894
			#設置執行失敗
1895
			$result["status"]="false";
226 liveuser 1896
 
3 liveuser 1897
			#設置執行錯誤
1898
			$result["error"]=$checkResult;
226 liveuser 1899
 
3 liveuser 1900
			#回傳結果
1901
			return $result;
226 liveuser 1902
 
182 liveuser 1903
			}#if end
226 liveuser 1904
 
3 liveuser 1905
		#判斷是哪一種加密方法
1906
		switch($conf["enCodeType"]){
226 liveuser 1907
 
3 liveuser 1908
			#如果是 "sha1"
1909
			case "sha1":
226 liveuser 1910
 
3 liveuser 1911
				#如果輸入不是字串
1912
				if(gettype($conf["enCodeStr"])!=="string"){
226 liveuser 1913
 
3 liveuser 1914
					#設置執行失敗
1915
					$result["status"]="false";
226 liveuser 1916
 
3 liveuser 1917
					#設置執行錯誤
1918
					$result["error"][]="enCodeStr shoud be string when enCodeType is ".$conf["enCodeType"];
226 liveuser 1919
 
3 liveuser 1920
					#回傳結果
1921
					return $result;
226 liveuser 1922
 
3 liveuser 1923
					}#if end
226 liveuser 1924
 
3 liveuser 1925
				#如果要用2元碼輸出
1926
				if($conf["sha1Raw"]=="true"){
226 liveuser 1927
 
3 liveuser 1928
					#sha1加密
1929
					$encodeStr=sha1($conf["enCodeStr"],TRUE);
226 liveuser 1930
 
3 liveuser 1931
					#如果加密失敗
1932
					if($encodeStr==false){
226 liveuser 1933
 
3 liveuser 1934
						#設置執行失敗
1935
						$result["status"]="false";
226 liveuser 1936
 
3 liveuser 1937
						#設置執行錯誤
1938
						$result["error"][]="加密失敗";
226 liveuser 1939
 
3 liveuser 1940
						#回傳結果
1941
						return $result;
226 liveuser 1942
 
3 liveuser 1943
						}#if end
226 liveuser 1944
 
3 liveuser 1945
					#反之加密成功
1946
					else{
226 liveuser 1947
 
3 liveuser 1948
						#取得加密後的字串
1949
						$result["content"]=$encodeStr;
226 liveuser 1950
 
3 liveuser 1951
						}#else end
226 liveuser 1952
 
3 liveuser 1953
					}#if end
226 liveuser 1954
 
3 liveuser 1955
				#反之用16進位數值輸出
1956
				else{
226 liveuser 1957
 
3 liveuser 1958
					#sha1加密
1959
					$encodeStr=sha1($conf["enCodeStr"],FALSE);
226 liveuser 1960
 
3 liveuser 1961
					#如果加密失敗
1962
					if($encodeStr==false){
226 liveuser 1963
 
3 liveuser 1964
						#設置執行失敗
1965
						$result["status"]="false";
226 liveuser 1966
 
3 liveuser 1967
						#設置執行錯誤
1968
						$result["error"][]="加密失敗";
226 liveuser 1969
 
3 liveuser 1970
						#回傳結果
1971
						return $result;
226 liveuser 1972
 
3 liveuser 1973
						}#if end
226 liveuser 1974
 
3 liveuser 1975
					#反之加密成功
1976
					else{
226 liveuser 1977
 
3 liveuser 1978
						#取得加密後的字串
1979
						$result["content"]=$encodeStr;
226 liveuser 1980
 
3 liveuser 1981
						}#else end
1982
 
1983
					}#else end
226 liveuser 1984
 
3 liveuser 1985
				#跳出
1986
				break;
226 liveuser 1987
 
3 liveuser 1988
			#如果是 "md5"
1989
			case "md5";
226 liveuser 1990
 
3 liveuser 1991
				#如果輸入不是字串
1992
				if(gettype($conf["enCodeStr"])!=="string"){
226 liveuser 1993
 
3 liveuser 1994
					#設置執行失敗
1995
					$result["status"]="false";
226 liveuser 1996
 
3 liveuser 1997
					#設置執行錯誤
1998
					$result["error"][]="enCodeStr shoud be string when enCodeType is ".$conf["enCodeType"];
226 liveuser 1999
 
3 liveuser 2000
					#回傳結果
2001
					return $result;
226 liveuser 2002
 
3 liveuser 2003
					}#if end
226 liveuser 2004
 
3 liveuser 2005
				#md5加密
2006
				$encodeStr=md5($conf["enCodeStr"]);
226 liveuser 2007
 
3 liveuser 2008
				#如果加密失敗
2009
				if($encodeStr==false){
226 liveuser 2010
 
3 liveuser 2011
					#設置執行失敗
2012
					$result["status"]="false";
226 liveuser 2013
 
3 liveuser 2014
					#設置執行錯誤
2015
					$result["error"][]="加密失敗";
226 liveuser 2016
 
3 liveuser 2017
					#回傳結果
2018
					return $result;
226 liveuser 2019
 
3 liveuser 2020
					}#if end
226 liveuser 2021
 
3 liveuser 2022
				#反之加密成功
2023
				else{
226 liveuser 2024
 
3 liveuser 2025
					#取得加密後的字串
2026
					$result["content"]=$encodeStr;
226 liveuser 2027
 
3 liveuser 2028
					}#else end
226 liveuser 2029
 
3 liveuser 2030
				#跳出
2031
				break;
226 liveuser 2032
 
3 liveuser 2033
			#如果是"p_hash"
2034
			case "p_hash":
226 liveuser 2035
 
3 liveuser 2036
				#如果輸入不是字串
2037
				if(gettype($conf["enCodeStr"])!=="string"){
226 liveuser 2038
 
3 liveuser 2039
					#設置執行失敗
2040
					$result["status"]="false";
226 liveuser 2041
 
3 liveuser 2042
					#設置執行錯誤
2043
					$result["error"][]="enCodeStr shoud be string when enCodeType is ".$conf["enCodeType"];
226 liveuser 2044
 
3 liveuser 2045
					#回傳結果
2046
					return $result;
226 liveuser 2047
 
3 liveuser 2048
					}#if end
226 liveuser 2049
 
3 liveuser 2050
				#如果 $conf["p_hash"] 存在
2051
				if(isset($conf["p_hash"])){
226 liveuser 2052
 
3 liveuser 2053
					#檢驗 $conf["enCodeStr"] 是否與 $conf["p_hash"] 相符
2054
					if(password_verify($conf["enCodeStr"],$conf["p_hash"])){
226 liveuser 2055
 
3 liveuser 2056
						#設置執行正常
2057
						$result["status"]="true";
226 liveuser 2058
 
3 liveuser 2059
						#回傳結果
2060
						return $result;
226 liveuser 2061
 
3 liveuser 2062
						}#if end
226 liveuser 2063
 
3 liveuser 2064
					#反之
2065
					else{
226 liveuser 2066
 
3 liveuser 2067
						#設置執行失敗
2068
						$result["status"]="false";
226 liveuser 2069
 
3 liveuser 2070
						#設置執行錯誤
2071
						$result["error"][]="密碼不正確";
226 liveuser 2072
 
3 liveuser 2073
						#回傳結果
2074
						return $result;
226 liveuser 2075
 
3 liveuser 2076
						}#else end
226 liveuser 2077
 
3 liveuser 2078
					}#if end
226 liveuser 2079
 
3 liveuser 2080
				#加密密碼
2081
				$encodeStr=password_hash($conf["enCodeStr"],PASSWORD_DEFAULT);
226 liveuser 2082
 
3 liveuser 2083
				#如果加密失敗
2084
				if($encodeStr==false){
226 liveuser 2085
 
3 liveuser 2086
					#設置執行失敗
2087
					$result["status"]="false";
226 liveuser 2088
 
3 liveuser 2089
					#設置執行錯誤
2090
					$result["error"][]="加密失敗";
226 liveuser 2091
 
3 liveuser 2092
					#回傳結果
2093
					return $result;
226 liveuser 2094
 
3 liveuser 2095
					}#if end
226 liveuser 2096
 
3 liveuser 2097
				#反之加密成功
2098
				else{
226 liveuser 2099
 
3 liveuser 2100
					#取得加密後的字串
2101
					$result["content"]=$encodeStr;
226 liveuser 2102
 
3 liveuser 2103
					}#else end
226 liveuser 2104
 
3 liveuser 2105
				break;
226 liveuser 2106
 
3 liveuser 2107
			#如果是 AES 256 bit 加密
2108
			#參考資料:
2109
			#https://www.ucamc.com/e-learning/php/388-php%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8aes-openssl%E5%8A%A0%E5%AF%86%E4%BB%A3%E7%A2%BC%E3%80%81encrypt%E3%80%81decrypt
2110
			case "aes256":
226 liveuser 2111
 
3 liveuser 2112
				#如果輸入不是字串
2113
				if(gettype($conf["enCodeStr"])!=="string"){
226 liveuser 2114
 
3 liveuser 2115
					#設置執行失敗
2116
					$result["status"]="false";
226 liveuser 2117
 
3 liveuser 2118
					#設置執行錯誤
2119
					$result["error"][]="enCodeStr shoud be string when enCodeType is ".$conf["enCodeType"];
226 liveuser 2120
 
3 liveuser 2121
					#回傳結果
2122
					return $result;
226 liveuser 2123
 
3 liveuser 2124
					}#if end
226 liveuser 2125
 
3 liveuser 2126
				#如果參數有缺
2127
				if( !isset($conf["keyForAes256"]) || !isset($conf["aes256Encode"]) ){
226 liveuser 2128
 
3 liveuser 2129
					#設置執行失敗
2130
					$result["status"]="false";
226 liveuser 2131
 
3 liveuser 2132
					#設置執行錯誤
2133
					$result["error"][]="aes256 演算法需要 keyForAes256 與 aes256Encode 參數";
226 liveuser 2134
 
3 liveuser 2135
					#回傳結果
2136
					return $result;
226 liveuser 2137
 
3 liveuser 2138
					}#if end
226 liveuser 2139
 
3 liveuser 2140
				#如果是要加密
2141
				if($conf["aes256Encode"]==="true"){
226 liveuser 2142
 
3 liveuser 2143
					#取得iv
2144
					$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
226 liveuser 2145
 
3 liveuser 2146
					#加密內容
2147
					$encrypted = openssl_encrypt($conf["enCodeStr"], 'aes-256-cbc', $conf["keyForAes256"], 0, $iv);
226 liveuser 2148
 
3 liveuser 2149
					#使用base64加密的內容
2150
					$result["content"]=base64_encode($encrypted . '::' . $iv);
226 liveuser 2151
 
3 liveuser 2152
					#數值執行正常
2153
					$result["status"]="true";
226 liveuser 2154
 
3 liveuser 2155
					#回傳結果
2156
					return $result;
226 liveuser 2157
 
3 liveuser 2158
					}#if end
226 liveuser 2159
 
3 liveuser 2160
				#反之是要解密
2161
				else{
226 liveuser 2162
 
3 liveuser 2163
					#取得 base64_decoed 的本文與 iv
2164
					list($conf["enCodeStr"], $iv) = explode('::', base64_decode($conf["enCodeStr"]), 2);
226 liveuser 2165
 
3 liveuser 2166
					#取得 解密好的內容
61 liveuser 2167
					$result["content"]=openssl_decrypt($conf["enCodeStr"], 'aes-256-cbc', $conf["keyForAes256"], 0, $iv);
226 liveuser 2168
 
61 liveuser 2169
					#數值執行正常
3 liveuser 2170
					$result["status"]="true";
226 liveuser 2171
 
3 liveuser 2172
					#回傳結果
2173
					return $result;
226 liveuser 2174
 
3 liveuser 2175
					}#else end
226 liveuser 2176
 
3 liveuser 2177
				#跳出迴圈
226 liveuser 2178
				break;
2179
 
3 liveuser 2180
			#如果是 "qbpwcf"
2181
			case "qbpwcf":
226 liveuser 2182
 
3 liveuser 2183
				#如果是要加密
2184
				if($conf["qbpwcfDecode"]==="false"){
226 liveuser 2185
 
3 liveuser 2186
					#to json string
2187
					$jsonData=json_encode($conf["enCodeStr"]);
226 liveuser 2188
 
3 liveuser 2189
					#如果出錯
2190
					if($jsonData===false){
226 liveuser 2191
 
3 liveuser 2192
						#設置執行失敗
2193
						$result["status"]="false";
226 liveuser 2194
 
3 liveuser 2195
						#設置執行錯誤
2196
						$result["error"][]="encode 「".$conf["enCodeStr"]."」 失敗";
226 liveuser 2197
 
3 liveuser 2198
						#回傳結果
2199
						return $result;
226 liveuser 2200
 
3 liveuser 2201
						}#if end
226 liveuser 2202
 
2203
					#設置加密好的內容
3 liveuser 2204
					$result["content"]=base64_encode(urlencode($jsonData));
226 liveuser 2205
 
3 liveuser 2206
					}#if end
226 liveuser 2207
 
3 liveuser 2208
				#反之是要解密
2209
				else{
226 liveuser 2210
 
3 liveuser 2211
					#base64_decode then urldecode data
2212
					$urlDecodeData=urldecode(base64_decode($conf["enCodeStr"]));
226 liveuser 2213
 
3 liveuser 2214
					#json_decode data
2215
					$json_decode_data=json_decode($urlDecodeData);
226 liveuser 2216
 
3 liveuser 2217
					#如果異常
2218
					if($urlDecodeData!==null && $json_decode_data===null){
226 liveuser 2219
 
3 liveuser 2220
						#設置執行失敗
2221
						$result["status"]="false";
226 liveuser 2222
 
3 liveuser 2223
						#設置執行錯誤
2224
						$result["error"][]="decode 「".$conf["enCodeStr"]."」 失敗";
226 liveuser 2225
 
3 liveuser 2226
						#回傳結果
2227
						return $result;
226 liveuser 2228
 
3 liveuser 2229
						}#if end
226 liveuser 2230
 
3 liveuser 2231
					#設置解密好的內容
2232
					$result["content"]=$json_decode_data;
226 liveuser 2233
 
3 liveuser 2234
					}#else end
226 liveuser 2235
 
61 liveuser 2236
				#跳出 switch
226 liveuser 2237
				break;
2238
 
61 liveuser 2239
			#若是bin2hex
2240
			case "bin2hex":
226 liveuser 2241
 
61 liveuser 2242
				#用16進位的兩個字母來表示每個byte
66 liveuser 2243
				$unpack=unpack("H*",$conf["enCodeStr"]);
226 liveuser 2244
 
66 liveuser 2245
				#如果沒有結果
2246
				if(!isset($unpack[1])){
226 liveuser 2247
 
66 liveuser 2248
					#設置執行失敗
2249
					$result["status"]="false";
226 liveuser 2250
 
66 liveuser 2251
					#設置執行錯誤
2252
					$result["error"][]="bin2hex failed";
226 liveuser 2253
 
66 liveuser 2254
					#設置執行錯誤
2255
					$result["error"][]=$unpack;
226 liveuser 2256
 
66 liveuser 2257
					#回傳結果
2258
					return $result;
226 liveuser 2259
 
66 liveuser 2260
					}#if end
226 liveuser 2261
 
66 liveuser 2262
				#取得結果
2263
				$result["content"]=$unpack[1];
226 liveuser 2264
 
61 liveuser 2265
				#相當於
2266
				#$result["content"]=bin2hex($conf["enCodeStr"]);
226 liveuser 2267
 
61 liveuser 2268
				#跳出 switch
2269
				break;
226 liveuser 2270
 
61 liveuser 2271
			#若是hex2bin
2272
			case "hex2bin":
226 liveuser 2273
 
61 liveuser 2274
				#將每個byte用兩個16進位字母來表示的字串變成 binary string
66 liveuser 2275
 				$pack=pack("H*",$conf["enCodeStr"]);
226 liveuser 2276
 
66 liveuser 2277
				$result["content"]=$pack;
226 liveuser 2278
 
61 liveuser 2279
				#跳出 switch
2280
				break;
226 liveuser 2281
 
61 liveuser 2282
			#如果是"gpg加解密"
2283
			case "gpg":
226 liveuser 2284
 
61 liveuser 2285
				#如果沒有 gpgId
2286
				if(!isset($conf["gpgId"])){
226 liveuser 2287
 
61 liveuser 2288
					#設置執行失敗
2289
					$result["status"]="false";
226 liveuser 2290
 
61 liveuser 2291
					#設置執行錯誤
2292
					$result["error"]="param gpgId if required when enCodeType is ".$conf["enCodeType"];
226 liveuser 2293
 
61 liveuser 2294
					#回傳結果
2295
					return $result;
226 liveuser 2296
 
61 liveuser 2297
					}#if end
226 liveuser 2298
 
61 liveuser 2299
				#如果是用加密
2300
				if($conf["gpgDecrypt"]==="false"){
226 liveuser 2301
 
61 liveuser 2302
					#用GnuPG加密
2303
					#函式說明:
2304
					#呼叫shell執行系統命令,並取得回傳的內容.
2305
					#回傳結果:
2306
					#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
2307
					#$result["error"],錯誤訊息陣列.
2308
					#$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"],為執行完後的輸出字串.
2315
					#$result["running"],是否還在執行.
2316
					#$result["pid"],pid.
2317
					#$result["statusCode"],執行結束後的代碼.
2318
					#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
2319
					#必填參數:
2320
					#$conf["command"],字串,要執行的指令.
2321
					$conf["external::callShell"]["command"]="base64decode.php";
2322
					#$conf["fileArgu"],字串,變數__FILE__的內容.
2323
					$conf["external::callShell"]["fileArgu"]=__FILE__;
2324
					#可省略參數:
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".
2333
					#$conf["enablePrintDescription"]="true";
2334
					#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
2335
					#$conf["printDescription"]="";
2336
					#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
2337
					$conf["external::callShell"]["escapeshellarg"]="true";
2338
					#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
2339
					#$conf["thereIsShellVar"]=array();
2340
					#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
2341
					#$conf["username"]="";
2342
					#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
2343
					#$conf["password"]="";
2344
					#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
2345
					#$conf["useScript"]="";
2346
					#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
2347
					#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
2348
					#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
2349
					#$conf["inBackGround"]="";
2350
					#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
2351
					#$conf["getErr"]="false";
2352
					#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
2353
					#$conf["doNotRun"]="false";
2354
					#參考資料:
2355
					#exec=>http://php.net/manual/en/function.exec.php
2356
					#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
2357
					#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
2358
					#備註:
2359
					#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
2360
					#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
2361
					$callShell=external::callShell($conf["external::callShell"]);
2362
					unset($conf["external::callShell"]);
2363
 
2364
					#如果執行失敗
2365
					if($callShell["status"]==="false"){
2366
 
2367
						#設置執行失敗
2368
						$result["status"]="false";
226 liveuser 2369
 
61 liveuser 2370
						#設置執行錯誤
2371
						$result["error"]=$callShell;
226 liveuser 2372
 
61 liveuser 2373
						#回傳結果
2374
						return $result;
226 liveuser 2375
 
61 liveuser 2376
						}#if end
2377
 
2378
					#取得加密後的內容
2379
					$result["content"]=$callShell["content"];
226 liveuser 2380
 
61 liveuser 2381
					}#if end
226 liveuser 2382
 
61 liveuser 2383
				#反之如果是解密
2384
				else if($conf["gpgDecrypt"]==="true"){
226 liveuser 2385
 
61 liveuser 2386
					#用GnuPG解密
2387
					#函式說明:
2388
					#呼叫shell執行系統命令,並取得回傳的內容.
2389
					#回傳結果:
2390
					#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
2391
					#$result["error"],錯誤訊息陣列.
2392
					#$result["function"],當前執行的函數名稱.
2393
					#$result["argu"],使用的參數.
2394
					#$result["cmd"],執行的指令內容.
2395
					#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
2396
					#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
2397
					#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
2398
					#$result["content"],為執行完後的輸出字串.
2399
					#$result["running"],是否還在執行.
2400
					#$result["pid"],pid.
2401
					#$result["statusCode"],執行結束後的代碼.
2402
					#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
2403
					#必填參數:
2404
					#$conf["command"],字串,要執行的指令.
2405
					$conf["external::callShell"]["command"]="base64decode.php";
2406
					#$conf["fileArgu"],字串,變數__FILE__的內容.
2407
					$conf["external::callShell"]["fileArgu"]=__FILE__;
2408
					#可省略參數:
2409
					#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
2410
					$conf["external::callShell"]["argu"]=array(base64_encode($conf["enCodeStr"]),"|","gpg","-r",$conf["gpgId"],"--trust-model","always","-v","-d");
2411
					#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
2412
					#$conf["arguIsAddr"]=array();
2413
					#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
2414
					#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
2415
					#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
2416
					#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
2417
					#$conf["enablePrintDescription"]="true";
2418
					#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
2419
					#$conf["printDescription"]="";
2420
					#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
2421
					$conf["external::callShell"]["escapeshellarg"]="true";
2422
					#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
2423
					#$conf["thereIsShellVar"]=array();
2424
					#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
2425
					#$conf["username"]="";
2426
					#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
2427
					#$conf["password"]="";
2428
					#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
2429
					#$conf["useScript"]="";
2430
					#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
2431
					#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
2432
					#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
2433
					#$conf["inBackGround"]="";
2434
					#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
2435
					#$conf["getErr"]="false";
2436
					#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
2437
					#$conf["doNotRun"]="false";
2438
					#參考資料:
2439
					#exec=>http://php.net/manual/en/function.exec.php
2440
					#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
2441
					#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
2442
					#備註:
2443
					#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
2444
					#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
2445
					$callShell=external::callShell($conf["external::callShell"]);
2446
					unset($conf["external::callShell"]);
2447
 
2448
					#如果執行失敗
2449
					if($callShell["status"]==="false"){
2450
 
2451
						#設置執行失敗
2452
						$result["status"]="false";
226 liveuser 2453
 
61 liveuser 2454
						#設置執行錯誤
2455
						$result["error"]=$callShell;
226 liveuser 2456
 
61 liveuser 2457
						#回傳結果
2458
						return $result;
2459
 
2460
						}#if end
226 liveuser 2461
 
61 liveuser 2462
					#取得加密後的內容
2463
					$result["content"]=$callShell["content"];
226 liveuser 2464
 
61 liveuser 2465
					}#else end
226 liveuser 2466
 
61 liveuser 2467
				#跳出 switch
2468
				break;
226 liveuser 2469
 
3 liveuser 2470
			#如果是其他內容
2471
			default:
226 liveuser 2472
 
3 liveuser 2473
				#設置執行失敗
2474
				$result["status"]="false";
226 liveuser 2475
 
3 liveuser 2476
				#設置執行錯誤
2477
				$result["error"][]="不支援的加密方法「".$conf["enCodeType"]."」";
226 liveuser 2478
 
3 liveuser 2479
				#回傳結果
2480
				return $result;
226 liveuser 2481
 
3 liveuser 2482
			}#switch end
226 liveuser 2483
 
3 liveuser 2484
		#執行到這邊代表執行正常
2485
		$result["status"]="true";
226 liveuser 2486
 
3 liveuser 2487
		#回傳結果
2488
		return $result;
226 liveuser 2489
 
61 liveuser 2490
		}#function enCodeStr end
226 liveuser 2491
 
3 liveuser 2492
	/*
2493
	#函式說明:
2494
	#將字串變成2元碼或2元碼變成字串.
2495
	#回傳結果:
226 liveuser 2496
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
3 liveuser 2497
	#$result["function"],當前執行的函數名稱.
2498
	#$result["content"],加密後的結果.
2499
	#$result["error"],錯誤訊息陣列.
2500
	#必填參數:
2501
	#$conf["input"],"字串",要hex或unhex的字串
2502
	$conf["input"]="";
2503
	#$conf["action"],"字串",要toBin或toStr.
226 liveuser 2504
	$conf["action"]="";
3 liveuser 2505
	#可省略參數:
2506
	#無.
2507
	#參考資料:
2508
	#hex2bin=>http://php.net/manual/en/function.hex2bin.php #bin2hex=>http://php.net/manual/en/function.binhex.php #pack=>http://php.net/manual/en/function.pack.php 轉換的方法似乎有問題. bin2hex可以將10進位數字字串轉換成2進位字串.
2509
	#備註:
2510
	#無.
2511
	*/
2512
	public static function str2bin(&$conf){
226 liveuser 2513
 
3 liveuser 2514
		#初始化要回傳的結果
2515
		$result=array();
226 liveuser 2516
 
3 liveuser 2517
		#記錄當前執行的函數
2518
		$result["function"]=__FUNCTION__;
226 liveuser 2519
 
3 liveuser 2520
		#如果沒有參數
2521
		if(func_num_args()==0){
226 liveuser 2522
 
3 liveuser 2523
			#設置執行失敗
2524
			$result["status"]="false";
226 liveuser 2525
 
3 liveuser 2526
			#設置執行錯誤訊息
2527
			$result["error"]="函數".$result["function"]."需要參數";
226 liveuser 2528
 
3 liveuser 2529
			#回傳結果
2530
			return $result;
226 liveuser 2531
 
3 liveuser 2532
			}#if end
226 liveuser 2533
 
3 liveuser 2534
		#如果 $conf 不為陣列
2535
		if(gettype($conf)!="array"){
226 liveuser 2536
 
3 liveuser 2537
			#設置執行失敗
2538
			$result["status"]="false";
226 liveuser 2539
 
3 liveuser 2540
			#設置執行錯誤訊息
2541
			$result["error"][]="\$conf變數須為陣列形態";
2542
 
2543
			#如果傳入的參數為 null
2544
			if($conf==null){
226 liveuser 2545
 
3 liveuser 2546
				#設置執行錯誤訊息
2547
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 2548
 
3 liveuser 2549
				}#if end
2550
 
2551
			#回傳結果
2552
			return $result;
226 liveuser 2553
 
3 liveuser 2554
			}#if end
226 liveuser 2555
 
3 liveuser 2556
		#檢查參數
2557
		#函式說明:
2558
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
2559
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
2560
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
2561
		#$result["function"],當前執行的函式名稱.
2562
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
2563
		#$result[$shouldBtCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
2564
		#$result[$shouldBtCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
2565
		#$result[$shouldBtCheckedVarName]["error"],每個參數設定的錯誤訊息
2566
		#必填參數:
2567
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
2568
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
2569
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
2570
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("input","action");
226 liveuser 2571
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列 例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double");
3 liveuser 2572
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string");
2573
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
2574
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
2575
		#可以省略的參數:
2576
		#$conf["canBeEmptyString"],必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true"。
2577
		#$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
2578
		#$conf["skipableVariableName"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
2579
		#$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("sha1Raw","p_hash");
226 liveuser 2580
		#$conf["skipableVariableType"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
3 liveuser 2581
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string");
2582
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是必填參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
2583
		#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("false",null);
2584
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
2585
		#$conf["arrayCountEqualCheck"][]=array();
2586
		$checkResult=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
2587
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 2588
 
3 liveuser 2589
		#如果檢查參數失敗
2590
		if($checkResult["status"]=="false"){
226 liveuser 2591
 
3 liveuser 2592
			#設置執行失敗
2593
			$result["status"]="false";
226 liveuser 2594
 
3 liveuser 2595
			#設置執行錯誤
2596
			$result["error"]=$checkResult;
226 liveuser 2597
 
3 liveuser 2598
			#回傳結果
2599
			return $result;
226 liveuser 2600
 
3 liveuser 2601
			}#if end
226 liveuser 2602
 
3 liveuser 2603
		#如果要hex
2604
		if($conf["action"]=="toBin"){
226 liveuser 2605
 
3 liveuser 2606
			#將字串轉換成2進位
2607
			$result["content"]=pack('H*', base_convert($conf["input"], 2, 16));
226 liveuser 2608
 
2609
			}#if end
2610
 
3 liveuser 2611
		#反之如果要unhex
2612
		else if($conf["action"]=="toStr"){
226 liveuser 2613
 
3 liveuser 2614
			#將2進位轉為字串
2615
			$result["content"]=unpack('H*',$conf["input"]);
2616
 
2617
			#針對每個字
2618
			foreach($result["content"] as $key=>$bin){
2619
 
2620
				#轉換成字串
2621
				$result["content"][$key]=base_convert($result["content"][1], 16, 2);
2622
 
61 liveuser 2623
				}#foreach end
226 liveuser 2624
 
3 liveuser 2625
			}#if end
226 liveuser 2626
 
3 liveuser 2627
		#設置執行正常
2628
		$result["status"]="true";
226 liveuser 2629
 
3 liveuser 2630
		#回傳結果
2631
		return $result;
2632
 
226 liveuser 2633
		}#function str2bin end
2634
 
3 liveuser 2635
	/*
2636
	#函式說明:
66 liveuser 2637
	#驗證Linux使用者的密碼或ssh private key是否正確.
3 liveuser 2638
	#回傳結果:
226 liveuser 2639
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
3 liveuser 2640
	#$result["function"],當前執行的函數名稱.
2641
	#$result["error"],錯誤訊息陣列.
2642
	#$result["warninig"],警告訊息陣列.
2643
	#$result["valid"],是否為存在的使用者且密碼正確.
2644
	#必填參數:
2645
	#$conf["username"],字串,要驗證的使用者名稱.
2646
	$conf["username"]="";
2647
	#$conf["password"],字串,用於驗證使用者的密碼.
2648
	$conf["password"]="";
2649
	#$conf["fileArgu"],字串,變數__FILE__的內容.
2650
	$conf["fileArgu"]=__FILE__;
2651
	#可省略參數:
66 liveuser 2652
	#$conf["sshPrivateKey"],字串,"true"代表password為ssh private key的內容;反之預設為密碼,設置為"false".
2653
	#$conf["sshPrivateKey"]="false";
3 liveuser 2654
	#參考資料:
2655
	#無.
2656
	#備註:
66 liveuser 2657
	#僅能在命令列環境下運.
2658
	#目前僅支援驗證執行php端的使用者.
3 liveuser 2659
	*/
2660
	public static function validUser(&$conf){
226 liveuser 2661
 
3 liveuser 2662
		#初始化要回傳的結果
2663
		$result=array();
2664
 
2665
		#取得當前執行的函數名稱
2666
		$result["function"]=__FUNCTION__;
2667
 
2668
		#初始化警告訊息
2669
		$result["warning"]=array();
2670
 
2671
		#函式說明:
2672
		#判斷當前環境為web還是cmd
2673
		#回傳結果:
2674
		#$result,"web"或"cmd"
2675
		if(csInformation::getEnv()=="web"){
226 liveuser 2676
 
3 liveuser 2677
			#設置執行失敗
2678
			$result["status"]="false";
226 liveuser 2679
 
3 liveuser 2680
			#設置執行錯誤訊息
2681
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
226 liveuser 2682
 
3 liveuser 2683
			#回傳結果
2684
			return $result;
226 liveuser 2685
 
3 liveuser 2686
			}#if end
2687
 
2688
		#如果沒有參數
2689
		if(func_num_args()==0){
226 liveuser 2690
 
3 liveuser 2691
			#設置執行失敗
2692
			$result["status"]="false";
226 liveuser 2693
 
3 liveuser 2694
			#設置執行錯誤訊息
2695
			$result["error"]="函數".$result["function"]."需要參數";
226 liveuser 2696
 
3 liveuser 2697
			#回傳結果
2698
			return $result;
226 liveuser 2699
 
3 liveuser 2700
			}#if end
2701
 
2702
		#取得參數
2703
		$result["argu"]=$conf;
2704
 
2705
		#如果 $conf 不為陣列
2706
		if(gettype($conf)!="array"){
226 liveuser 2707
 
3 liveuser 2708
			#設置執行失敗
2709
			$result["status"]="false";
226 liveuser 2710
 
3 liveuser 2711
			#設置執行錯誤訊息
2712
			$result["error"][]="\$conf變數須為陣列形態";
226 liveuser 2713
 
3 liveuser 2714
			#如果傳入的參數為 null
2715
			if($conf==null){
226 liveuser 2716
 
3 liveuser 2717
				#設置執行錯誤訊息
2718
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 2719
 
3 liveuser 2720
				}#if end
2721
 
2722
			#回傳結果
2723
			return $result;
226 liveuser 2724
 
3 liveuser 2725
			}#if end
226 liveuser 2726
 
3 liveuser 2727
		#檢查參數
2728
		#函式說明:
2729
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
2730
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
2731
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
2732
		#$result["function"],當前執行的函式名稱.
2733
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
2734
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
2735
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
2736
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
2737
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
2738
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
2739
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
2740
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
2741
		#必填參數:
2742
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
2743
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
2744
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
2745
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("username","password","fileArgu");
2746
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
2747
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string","string");
2748
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
2749
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
2750
		#可以省略的參數:
2751
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
2752
		$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
2753
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列
2754
		#$conf["variableCheck::checkArguments"]["canNotBeEmpty"]=array("password");
2755
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列
2756
		$conf["variableCheck::checkArguments"]["canBeEmpty"]=array("password");
2757
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或集合.
2758
		#$conf["skipableVariableCanNotBeEmpty"]=array();
2759
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
66 liveuser 2760
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("sshPrivateKey");
226 liveuser 2761
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
66 liveuser 2762
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string");
3 liveuser 2763
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
66 liveuser 2764
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("false");
3 liveuser 2765
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
2766
		#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"][]=array("conName","conVal");
2767
		#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"][]=array("target","styleAttr","styleVal");
2768
		#參考資料來源:
2769
		#array_keys=>http://php.net/manual/en/function.array-keys.php
2770
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
226 liveuser 2771
		unset($conf["variableCheck::checkArguments"]);
2772
 
3 liveuser 2773
		#如果檢查參數失敗
2774
		if($checkArguments["status"]=="false"){
226 liveuser 2775
 
3 liveuser 2776
			#設置執行不正常
2777
			$result["status"]="false";
226 liveuser 2778
 
3 liveuser 2779
			#設置執行錯誤
2780
			$result["error"]=$checkArguments;
226 liveuser 2781
 
3 liveuser 2782
			#回傳結果
2783
			return $result;
226 liveuser 2784
 
3 liveuser 2785
			}#if end
226 liveuser 2786
 
3 liveuser 2787
		#如果檢查參數不通過
2788
		if($checkArguments["passed"]=="false"){
226 liveuser 2789
 
3 liveuser 2790
			#設置執行不正常
2791
			$result["status"]="false";
226 liveuser 2792
 
3 liveuser 2793
			#設置執行錯誤
2794
			$result["error"]=$checkArguments;
226 liveuser 2795
 
3 liveuser 2796
			#回傳結果
2797
			return $result;
226 liveuser 2798
 
3 liveuser 2799
			}#if end
226 liveuser 2800
 
3 liveuser 2801
		#檢查使用者是否存在
2802
		#函式說明:
2803
		#呼叫shell執行系統命令,並取得回傳的內容.
2804
		#回傳的結果:
2805
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
2806
		#$result["error"],錯誤訊息陣列.
2807
		#$result["function"],當前執行的函數名稱.
2808
		#$result["cmd"],執行的指令內容.
2809
		#$result["output"],爲執行完二元碼後的輸出陣列.
2810
		#必填的參數
2811
		#$conf["command"],字串,要執行的指令與.
2812
		$conf["external::callShell"]["command"]="id";
2813
		#可省略參數:
2814
		#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
2815
		$conf["external::callShell"]["argu"]=array($conf["username"]);
2816
		#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
2817
		#$conf["enablePrintDescription"]="true";
2818
		#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容.
2819
		#$conf["printDescription"]="";
2820
		#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".
2821
		#$conf["escapeshellarg"]="false";
2822
		#$conf["username"],字串,要用什麼使用者來執行,預設為執行apache的使用者.
2823
		#$conf["external::callShell"]["username"]="";
2824
		#$conf["password"],字串,與$conf["username"]搭配的使用者密碼,預設不使用密碼.
2825
		#$conf["external::callShell"]["password"]="";
2826
		#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要;"false"代表不要,預設為"false".
2827
		$conf["external::callShell"]["useScript"]="true";
2828
		#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 ".qbpwcf_tmp/external/callShell/".
2829
		#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
2830
		#$conf["fileArgu"],字串,變數__FILE__的內容,預設為當前檔案的路徑與名稱.
2831
		#$conf["fileArgu"],字串,變數__FILE__的內容.
2832
		$conf["external::callShell"]["fileArgu"]=$conf["fileArgu"];
2833
		#備註:
2834
		#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行.使用root身份可能會被selinux阻擋.
2835
		#參考資料:
2836
		#exec=>http://php.net/manual/en/function.exec.php
2837
		#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
2838
		#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
2839
		$callShell=external::callShell($conf["external::callShell"]);
2840
		unset($conf["external::callShell"]);
226 liveuser 2841
 
3 liveuser 2842
		#如果執行shell失敗
66 liveuser 2843
		if($callShell["status"]==="false"){
226 liveuser 2844
 
3 liveuser 2845
			#設置執行不正常
2846
			$result["status"]="false";
226 liveuser 2847
 
3 liveuser 2848
			#設置執行錯誤
2849
			$result["error"]=$callShell;
226 liveuser 2850
 
3 liveuser 2851
			#回傳結果
2852
			return $result;
226 liveuser 2853
 
3 liveuser 2854
			}#if end
226 liveuser 2855
 
3 liveuser 2856
		#如果使用者 $conf["username"] 不存在
66 liveuser 2857
		if($callShell["output"][0]==="id: ".$conf["username"].": no such user"){
226 liveuser 2858
 
3 liveuser 2859
			#設置執行不正常
2860
			$result["status"]="true";
226 liveuser 2861
 
3 liveuser 2862
			#設置使用者驗證失敗
2863
			$result["valid"]="false";
226 liveuser 2864
 
3 liveuser 2865
			#設置使用者不存在的警告
2866
			$result["warninig"][]="使用者 \"".$conf["username"]."\" 不存在!";
226 liveuser 2867
 
3 liveuser 2868
			#回傳結果
2869
			return $result;
226 liveuser 2870
 
3 liveuser 2871
			}#if end
226 liveuser 2872
 
66 liveuser 2873
		#如果要用ssh private key進行認證
2874
		if($conf["sshPrivateKey"]==="true"){
226 liveuser 2875
 
66 liveuser 2876
			#可以用 echo "env | grep SSH_CONNECTION=" | ssh username@127.0.0.1 來驗證使用者的ssh連線是否成功
226 liveuser 2877
 
66 liveuser 2878
			#預設用來測試ssh private key 與 username 是否可成功登入的賬戶
2879
			$sshAgentAcct="qbpwcf";
226 liveuser 2880
 
66 liveuser 2881
			#確認用來測試登入ssh的使用者qbpwcf存在
2882
			#檢查使用者是否存在
2883
			#函式說明:
2884
			#呼叫shell執行系統命令,並取得回傳的內容.
2885
			#回傳的結果:
2886
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
2887
			#$result["error"],錯誤訊息陣列.
2888
			#$result["function"],當前執行的函數名稱.
2889
			#$result["cmd"],執行的指令內容.
2890
			#$result["output"],爲執行完二元碼後的輸出陣列.
2891
			#必填的參數
2892
			#$conf["command"],字串,要執行的指令與.
2893
			$conf["external::callShell"]["command"]="id";
2894
			#可省略參數:
2895
			#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
2896
			$conf["external::callShell"]["argu"]=array($sshAgentAcct);
2897
			#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
2898
			#$conf["enablePrintDescription"]="true";
2899
			#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容.
2900
			#$conf["printDescription"]="";
2901
			#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".
2902
			#$conf["escapeshellarg"]="false";
2903
			#$conf["username"],字串,要用什麼使用者來執行,預設為執行apache的使用者.
2904
			#$conf["external::callShell"]["username"]="";
2905
			#$conf["password"],字串,與$conf["username"]搭配的使用者密碼,預設不使用密碼.
2906
			#$conf["external::callShell"]["password"]="";
2907
			#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要;"false"代表不要,預設為"false".
2908
			$conf["external::callShell"]["useScript"]="true";
2909
			#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 ".qbpwcf_tmp/external/callShell/".
2910
			#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
2911
			#$conf["fileArgu"],字串,變數__FILE__的內容,預設為當前檔案的路徑與名稱.
2912
			#$conf["fileArgu"],字串,變數__FILE__的內容.
2913
			$conf["external::callShell"]["fileArgu"]=$conf["fileArgu"];
2914
			#備註:
2915
			#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行.使用root身份可能會被selinux阻擋.
2916
			#參考資料:
2917
			#exec=>http://php.net/manual/en/function.exec.php
2918
			#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
2919
			#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
2920
			$callShell=external::callShell($conf["external::callShell"]);
2921
			unset($conf["external::callShell"]);
226 liveuser 2922
 
66 liveuser 2923
			#如果執行shell失敗
2924
			if($callShell["status"]==="false"){
226 liveuser 2925
 
66 liveuser 2926
				#設置執行不正常
2927
				$result["status"]="false";
226 liveuser 2928
 
66 liveuser 2929
				#設置執行錯誤
2930
				$result["error"]=$callShell;
226 liveuser 2931
 
66 liveuser 2932
				#回傳結果
2933
				return $result;
226 liveuser 2934
 
66 liveuser 2935
				}#if end
226 liveuser 2936
 
66 liveuser 2937
			#另外儲存存放ssh使用者設定的目錄
2938
			$dotsshFolderOfsshAgent="/home/".$sshAgentAcct."/.ssh";
226 liveuser 2939
 
66 liveuser 2940
			#如果使用者 $sshAgentAcct 不存在
2941
			if($callShell["output"][0]==="id: ".$sshAgentAcct.": no such user"){
226 liveuser 2942
 
66 liveuser 2943
				#透過 qbpwcf-usock.service 服務建立對應的使用者
2944
				#函式說明:
2945
				#連線到 usr/bin/qbpwcf-usock.php 產生的  unix domain socket,運行指定的指令.
2946
				#回傳結果:
2947
				#$result["status"],"true"代表執行正常;"false"代表執行不正常.
2948
				#$result["error"],錯誤訊息陣列.
2949
				#$result["function"],當前執行的函式名稱.
2950
				#$result["argu"],使用的參數.
2951
				#$result["content"],執行完指令的結果.
2952
				#必填參數:
2953
				#$conf["fileArgu"],字串,變數__FILE__的內容.
2954
				$conf["sock::execAnyCmdByQBPWCFunixSocket"]["fileArgu"]=__FILE__;
2955
				#$conf["command"],字串,要執行的指令名稱.
2956
				$conf["sock::execAnyCmdByQBPWCFunixSocket"]["command"]="useradd";
2957
				#可省略參數:
2958
				#$conf["sock"],字串,要連線的 usr/bin/qbpwcf-sock.php(sock::unixDomainSockServer) 所產生的 unix domain socket 路徑與名稱,預設為 qbpwcf_usock_path.
2959
				#$conf["sock"]=qbpwcf_usock_path;
2960
				#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
2961
				$conf["sock::execAnyCmdByQBPWCFunixSocket"]["argu"]=array($sshAgentAcct);
2962
				#$conf["commandIncludeArgu"],字串,是否command含有參數,預設為"false"代表沒有;反之為"true".
2963
				#$conf["commandIncludeArgu"]="false";
2964
				#$conf["commandInBg"],字串,是否要將程序放在背景執行,再取得相關資訊,預設為"false"代表不要;反之為"true".
2965
				#$conf["commandInBg"]="false";
2966
				#參考資料:
2967
				#無.
2968
				#備註:
2969
				#無.
2970
				$execAnyCmdByQBPWCFunixSocket=sock::execAnyCmdByQBPWCFunixSocket($conf["sock::execAnyCmdByQBPWCFunixSocket"]);
2971
				unset($conf["sock::execAnyCmdByQBPWCFunixSocket"]);
226 liveuser 2972
 
66 liveuser 2973
				#如果執行shell失敗
2974
				if($execAnyCmdByQBPWCFunixSocket["status"]==="false"){
226 liveuser 2975
 
66 liveuser 2976
					#設置執行不正常
2977
					$result["status"]="false";
226 liveuser 2978
 
66 liveuser 2979
					#設置執行錯誤
2980
					$result["error"]=$execAnyCmdByQBPWCFunixSocket;
226 liveuser 2981
 
66 liveuser 2982
					#回傳結果
2983
					return $result;
226 liveuser 2984
 
66 liveuser 2985
					}#if end
226 liveuser 2986
 
2987
				#透過 qbpwcf-usock.service 服務建立設置對應使用者的 .ssh 目錄
66 liveuser 2988
				#函式說明:
2989
				#連線到 usr/bin/qbpwcf-usock.php 產生的  unix domain socket,運行指定的指令.
2990
				#回傳結果:
2991
				#$result["status"],"true"代表執行正常;"false"代表執行不正常.
2992
				#$result["error"],錯誤訊息陣列.
2993
				#$result["function"],當前執行的函式名稱.
2994
				#$result["argu"],使用的參數.
2995
				#$result["content"],執行完指令的結果.
2996
				#必填參數:
2997
				#$conf["fileArgu"],字串,變數__FILE__的內容.
2998
				$conf["sock::execAnyCmdByQBPWCFunixSocket"]["fileArgu"]=__FILE__;
2999
				#$conf["command"],字串,要執行的指令名稱.
3000
				$conf["sock::execAnyCmdByQBPWCFunixSocket"]["command"]="mkdir";
3001
				#可省略參數:
3002
				#$conf["sock"],字串,要連線的 usr/bin/qbpwcf-sock.php(sock::unixDomainSockServer) 所產生的 unix domain socket 路徑與名稱,預設為 qbpwcf_usock_path.
3003
				#$conf["sock"]=qbpwcf_usock_path;
3004
				#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
3005
				$conf["sock::execAnyCmdByQBPWCFunixSocket"]["argu"]=array($dotsshFolderOfsshAgent);
3006
				#$conf["commandIncludeArgu"],字串,是否command含有參數,預設為"false"代表沒有;反之為"true".
3007
				#$conf["commandIncludeArgu"]="false";
3008
				#$conf["commandInBg"],字串,是否要將程序放在背景執行,再取得相關資訊,預設為"false"代表不要;反之為"true".
3009
				#$conf["commandInBg"]="false";
3010
				#參考資料:
3011
				#無.
3012
				#備註:
3013
				#無.
3014
				$execAnyCmdByQBPWCFunixSocket=sock::execAnyCmdByQBPWCFunixSocket($conf["sock::execAnyCmdByQBPWCFunixSocket"]);
3015
				unset($conf["sock::execAnyCmdByQBPWCFunixSocket"]);
226 liveuser 3016
 
66 liveuser 3017
				#如果執行shell失敗
3018
				if($execAnyCmdByQBPWCFunixSocket["status"]==="false"){
226 liveuser 3019
 
66 liveuser 3020
					#設置執行不正常
3021
					$result["status"]="false";
226 liveuser 3022
 
66 liveuser 3023
					#設置執行錯誤
3024
					$result["error"]=$execAnyCmdByQBPWCFunixSocket;
226 liveuser 3025
 
66 liveuser 3026
					#回傳結果
3027
					return $result;
226 liveuser 3028
 
66 liveuser 3029
					}#if end
226 liveuser 3030
 
66 liveuser 3031
				}#if end
226 liveuser 3032
 
66 liveuser 3033
			#確認 ssh 使用者設定的目錄 $dotsshFolderOfsshAgent 是否存在
3034
			#函式說明:
3035
			#連線到 usr/bin/qbpwcf-usock.php 產生的  unix domain socket,運行指定的指令.
3036
			#回傳結果:
3037
			#$result["status"],"true"代表執行正常;"false"代表執行不正常.
3038
			#$result["error"],錯誤訊息陣列.
3039
			#$result["function"],當前執行的函式名稱.
3040
			#$result["argu"],使用的參數.
3041
			#$result["content"],執行完指令的結果.
3042
			#必填參數:
3043
			#$conf["fileArgu"],字串,變數__FILE__的內容.
3044
			$conf["sock::execAnyCmdByQBPWCFunixSocket"]["fileArgu"]=__FILE__;
3045
			#$conf["command"],字串,要執行的指令名稱.
3046
			$conf["sock::execAnyCmdByQBPWCFunixSocket"]["command"]="checkNodeExist.php";
3047
			#可省略參數:
3048
			#$conf["sock"],字串,要連線的 usr/bin/qbpwcf-sock.php(sock::unixDomainSockServer) 所產生的 unix domain socket 路徑與名稱,預設為 qbpwcf_usock_path.
3049
			#$conf["sock"]=qbpwcf_usock_path;
3050
			#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
3051
			$conf["sock::execAnyCmdByQBPWCFunixSocket"]["argu"]=array($dotsshFolderOfsshAgent);
3052
			#$conf["commandIncludeArgu"],字串,是否command含有參數,預設為"false"代表沒有;反之為"true".
3053
			#$conf["sock::execAnyCmdByQBPWCFunixSocket"]["commandIncludeArgu"]="true";
3054
			#$conf["commandInBg"],字串,是否要將程序放在背景執行,再取得相關資訊,預設為"false"代表不要;反之為"true".
3055
			#$conf["commandInBg"]="false";
3056
			#參考資料:
3057
			#無.
3058
			#備註:
3059
			#無.
3060
			$execAnyCmdByQBPWCFunixSocket=sock::execAnyCmdByQBPWCFunixSocket($conf["sock::execAnyCmdByQBPWCFunixSocket"]);
3061
			unset($conf["sock::execAnyCmdByQBPWCFunixSocket"]);
226 liveuser 3062
 
66 liveuser 3063
			#如果執行shell失敗
3064
			if($execAnyCmdByQBPWCFunixSocket["status"]==="false"){
226 liveuser 3065
 
66 liveuser 3066
				#設置執行不正常
3067
				$result["status"]="false";
226 liveuser 3068
 
66 liveuser 3069
				#設置執行錯誤
3070
				$result["error"]=$execAnyCmdByQBPWCFunixSocket;
226 liveuser 3071
 
66 liveuser 3072
				#回傳結果
3073
				return $result;
226 liveuser 3074
 
66 liveuser 3075
				}#if end
226 liveuser 3076
 
66 liveuser 3077
			#如果得到的結果不為 json
3078
			if(!json_validate($execAnyCmdByQBPWCFunixSocket["content"])){
226 liveuser 3079
 
66 liveuser 3080
				#設置執行不正常
3081
				$result["status"]="false";
226 liveuser 3082
 
66 liveuser 3083
				#設置執行錯誤
3084
				$result["error"]=$execAnyCmdByQBPWCFunixSocket;
226 liveuser 3085
 
66 liveuser 3086
				#回傳結果
3087
				return $result;
226 liveuser 3088
 
66 liveuser 3089
				}#if end
226 liveuser 3090
 
66 liveuser 3091
			#解析 json 並轉換成 array
3092
			$execAnyCmdByQBPWCFunixSocket["content"]=(array)(json_decode($execAnyCmdByQBPWCFunixSocket["content"]));
226 liveuser 3093
 
66 liveuser 3094
			#如果沒有找到要存在的 $dotsshFolderOfsshAgent 目錄
3095
			if(count($execAnyCmdByQBPWCFunixSocket["content"])===0){
226 liveuser 3096
 
66 liveuser 3097
				#建立之
3098
				#函式說明:
3099
				#連線到 usr/bin/qbpwcf-usock.php 產生的  unix domain socket,運行指定的指令.
3100
				#回傳結果:
3101
				#$result["status"],"true"代表執行正常;"false"代表執行不正常.
3102
				#$result["error"],錯誤訊息陣列.
3103
				#$result["function"],當前執行的函式名稱.
3104
				#$result["argu"],使用的參數.
3105
				#$result["content"],執行完指令的結果.
3106
				#必填參數:
3107
				#$conf["fileArgu"],字串,變數__FILE__的內容.
3108
				$conf["sock::execAnyCmdByQBPWCFunixSocket"]["fileArgu"]=__FILE__;
3109
				#$conf["command"],字串,要執行的指令名稱.
3110
				$conf["sock::execAnyCmdByQBPWCFunixSocket"]["command"]="mkdir";
3111
				#可省略參數:
3112
				#$conf["sock"],字串,要連線的 usr/bin/qbpwcf-sock.php(sock::unixDomainSockServer) 所產生的 unix domain socket 路徑與名稱,預設為 qbpwcf_usock_path.
3113
				#$conf["sock"]=qbpwcf_usock_path;
3114
				#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
3115
				$conf["sock::execAnyCmdByQBPWCFunixSocket"]["argu"]=array("-p",$dotsshFolderOfsshAgent);
3116
				#$conf["commandIncludeArgu"],字串,是否command含有參數,預設為"false"代表沒有;反之為"true".
3117
				#$conf["sock::execAnyCmdByQBPWCFunixSocket"]["commandIncludeArgu"]="true";
3118
				#$conf["commandInBg"],字串,是否要將程序放在背景執行,再取得相關資訊,預設為"false"代表不要;反之為"true".
3119
				#$conf["commandInBg"]="false";
3120
				#參考資料:
3121
				#無.
3122
				#備註:
3123
				#無.
3124
				$createDotsshFolderOfsshAgent=sock::execAnyCmdByQBPWCFunixSocket($conf["sock::execAnyCmdByQBPWCFunixSocket"]);
3125
				unset($conf["sock::execAnyCmdByQBPWCFunixSocket"]);
226 liveuser 3126
 
66 liveuser 3127
				#如果執行shell失敗
3128
				if($createDotsshFolderOfsshAgent["status"]==="false"){
226 liveuser 3129
 
66 liveuser 3130
					#設置執行不正常
3131
					$result["status"]="false";
226 liveuser 3132
 
66 liveuser 3133
					#設置執行錯誤
3134
					$result["error"]=$createDotsshFolderOfsshAgent;
226 liveuser 3135
 
66 liveuser 3136
					#回傳結果
3137
					return $result;
226 liveuser 3138
 
66 liveuser 3139
					}#if end
226 liveuser 3140
 
66 liveuser 3141
				}#if end
226 liveuser 3142
 
66 liveuser 3143
			#如果沒有找到要存在的 $dotsshFolderOfsshAgent 目錄
3144
			else if($execAnyCmdByQBPWCFunixSocket[0]!==$dotsshFolderOfsshAgent){
226 liveuser 3145
 
66 liveuser 3146
				#建立之
3147
				#函式說明:
3148
				#連線到 usr/bin/qbpwcf-usock.php 產生的  unix domain socket,運行指定的指令.
3149
				#回傳結果:
3150
				#$result["status"],"true"代表執行正常;"false"代表執行不正常.
3151
				#$result["error"],錯誤訊息陣列.
3152
				#$result["function"],當前執行的函式名稱.
3153
				#$result["argu"],使用的參數.
3154
				#$result["content"],執行完指令的結果.
3155
				#必填參數:
3156
				#$conf["fileArgu"],字串,變數__FILE__的內容.
3157
				$conf["sock::execAnyCmdByQBPWCFunixSocket"]["fileArgu"]=__FILE__;
3158
				#$conf["command"],字串,要執行的指令名稱.
3159
				$conf["sock::execAnyCmdByQBPWCFunixSocket"]["command"]="mkdir";
3160
				#可省略參數:
3161
				#$conf["sock"],字串,要連線的 usr/bin/qbpwcf-sock.php(sock::unixDomainSockServer) 所產生的 unix domain socket 路徑與名稱,預設為 qbpwcf_usock_path.
3162
				#$conf["sock"]=qbpwcf_usock_path;
3163
				#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
3164
				$conf["sock::execAnyCmdByQBPWCFunixSocket"]["argu"]=array("-p",$dotsshFolderOfsshAgent);
3165
				#$conf["commandIncludeArgu"],字串,是否command含有參數,預設為"false"代表沒有;反之為"true".
3166
				#$conf["sock::execAnyCmdByQBPWCFunixSocket"]["commandIncludeArgu"]="true";
3167
				#$conf["commandInBg"],字串,是否要將程序放在背景執行,再取得相關資訊,預設為"false"代表不要;反之為"true".
3168
				#$conf["commandInBg"]="false";
3169
				#參考資料:
3170
				#無.
3171
				#備註:
3172
				#無.
3173
				$createDotsshFolderOfsshAgent=sock::execAnyCmdByQBPWCFunixSocket($conf["sock::execAnyCmdByQBPWCFunixSocket"]);
3174
				unset($conf["sock::execAnyCmdByQBPWCFunixSocket"]);
226 liveuser 3175
 
66 liveuser 3176
				#如果執行shell失敗
3177
				if($createDotsshFolderOfsshAgent["status"]==="false"){
226 liveuser 3178
 
66 liveuser 3179
					#設置執行不正常
3180
					$result["status"]="false";
226 liveuser 3181
 
66 liveuser 3182
					#設置執行錯誤
3183
					$result["error"]=$createDotsshFolderOfsshAgent;
226 liveuser 3184
 
66 liveuser 3185
					#回傳結果
3186
					return $result;
226 liveuser 3187
 
66 liveuser 3188
					}#if end
226 liveuser 3189
 
66 liveuser 3190
				}#if end
226 liveuser 3191
 
66 liveuser 3192
			#取得 $dotsshFolderOfsshAgent 的權限與擁有着
3193
			#函式說明:
3194
			#連線到 usr/bin/qbpwcf-usock.php 產生的  unix domain socket,運行指定的指令.
3195
			#回傳結果:
3196
			#$result["status"],"true"代表執行正常;"false"代表執行不正常.
3197
			#$result["error"],錯誤訊息陣列.
3198
			#$result["function"],當前執行的函式名稱.
3199
			#$result["argu"],使用的參數.
3200
			#$result["content"],執行完指令的結果.
3201
			#必填參數:
3202
			#$conf["fileArgu"],字串,變數__FILE__的內容.
3203
			$conf["sock::execAnyCmdByQBPWCFunixSocket"]["fileArgu"]=__FILE__;
3204
			#$conf["command"],字串,要執行的指令名稱.
3205
			$conf["sock::execAnyCmdByQBPWCFunixSocket"]["command"]="getNodeInfo.php";
3206
			#可省略參數:
3207
			#$conf["sock"],字串,要連線的 usr/bin/qbpwcf-sock.php(sock::unixDomainSockServer) 所產生的 unix domain socket 路徑與名稱,預設為 qbpwcf_usock_path.
3208
			#$conf["sock"]=qbpwcf_usock_path;
3209
			#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
3210
			$conf["sock::execAnyCmdByQBPWCFunixSocket"]["argu"]=array("--node",$dotsshFolderOfsshAgent);
3211
			#$conf["commandIncludeArgu"],字串,是否command含有參數,預設為"false"代表沒有;反之為"true".
3212
			#$conf["sock::execAnyCmdByQBPWCFunixSocket"]["commandIncludeArgu"]="true";
3213
			#$conf["commandInBg"],字串,是否要將程序放在背景執行,再取得相關資訊,預設為"false"代表不要;反之為"true".
3214
			#$conf["commandInBg"]="false";
3215
			#參考資料:
3216
			#無.
3217
			#備註:
3218
			#無.
3219
			$createDotsshFolderOfsshAgent=sock::execAnyCmdByQBPWCFunixSocket($conf["sock::execAnyCmdByQBPWCFunixSocket"]);
3220
			unset($conf["sock::execAnyCmdByQBPWCFunixSocket"]);
226 liveuser 3221
 
66 liveuser 3222
			#如果執行shell失敗
3223
			if($createDotsshFolderOfsshAgent["status"]==="false"){
226 liveuser 3224
 
66 liveuser 3225
				#設置執行不正常
3226
				$result["status"]="false";
226 liveuser 3227
 
66 liveuser 3228
				#設置執行錯誤
3229
				$result["error"]=$createDotsshFolderOfsshAgent;
226 liveuser 3230
 
66 liveuser 3231
				#回傳結果
3232
				return $result;
226 liveuser 3233
 
66 liveuser 3234
				}#if end
226 liveuser 3235
 
66 liveuser 3236
			#如果執行異常
3237
			if($createDotsshFolderOfsshAgent["status"]==="false"){
226 liveuser 3238
 
66 liveuser 3239
				#設置執行不正常
3240
				$result["status"]="false";
226 liveuser 3241
 
66 liveuser 3242
				#設置執行錯誤
3243
				$result["error"]=$createDotsshFolderOfsshAgent;
226 liveuser 3244
 
66 liveuser 3245
				#回傳結果
3246
				return $result;
226 liveuser 3247
 
66 liveuser 3248
				}#if end
226 liveuser 3249
 
66 liveuser 3250
			#如果沒有得到輸出
3251
			if(!isset($createDotsshFolderOfsshAgent["content"][0])){
226 liveuser 3252
 
66 liveuser 3253
				#設置執行不正常
3254
				$result["status"]="false";
226 liveuser 3255
 
66 liveuser 3256
				#設置執行錯誤
3257
				$result["error"]=$createDotsshFolderOfsshAgent;
226 liveuser 3258
 
66 liveuser 3259
				#回傳結果
3260
				return $result;
226 liveuser 3261
 
66 liveuser 3262
				}#if end
226 liveuser 3263
 
66 liveuser 3264
			#如果輸出結果不是json
3265
			if(!json_validate($createDotsshFolderOfsshAgent["content"][0])){
226 liveuser 3266
 
66 liveuser 3267
				#設置執行不正常
3268
				$result["status"]="false";
226 liveuser 3269
 
66 liveuser 3270
				#設置執行錯誤
3271
				$result["error"]=$createDotsshFolderOfsshAgent;
226 liveuser 3272
 
66 liveuser 3273
				#回傳結果
3274
				return $result;
226 liveuser 3275
 
66 liveuser 3276
				}#if end
226 liveuser 3277
 
66 liveuser 3278
			#解析json
3279
			$node_info=json_decode($createDotsshFolderOfsshAgent["content"][0]);
226 liveuser 3280
 
66 liveuser 3281
			#若 $dotsshFolderOfsshAgent 的權限,不為 "rwx------"
3282
			if($node_info->ownerPerm!=="rwx" || $node_info->groupPerm!=="---" || $node_info->otherPerm!=="---"){
226 liveuser 3283
 
66 liveuser 3284
				#更新其權限設置
3285
				#函式說明:
3286
				#連線到 usr/bin/qbpwcf-usock.php 產生的  unix domain socket,運行指定的指令.
3287
				#回傳結果:
3288
				#$result["status"],"true"代表執行正常;"false"代表執行不正常.
3289
				#$result["error"],錯誤訊息陣列.
3290
				#$result["function"],當前執行的函式名稱.
3291
				#$result["argu"],使用的參數.
3292
				#$result["content"],執行完指令的結果.
3293
				#必填參數:
3294
				#$conf["fileArgu"],字串,變數__FILE__的內容.
3295
				$conf["sock::execAnyCmdByQBPWCFunixSocket"]["fileArgu"]=__FILE__;
3296
				#$conf["command"],字串,要執行的指令名稱.
3297
				$conf["sock::execAnyCmdByQBPWCFunixSocket"]["command"]="chmod";
3298
				#可省略參數:
3299
				#$conf["sock"],字串,要連線的 usr/bin/qbpwcf-sock.php(sock::unixDomainSockServer) 所產生的 unix domain socket 路徑與名稱,預設為 qbpwcf_usock_path.
3300
				#$conf["sock"]=qbpwcf_usock_path;
3301
				#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
3302
				$conf["sock::execAnyCmdByQBPWCFunixSocket"]["argu"]=array("700",$dotsshFolderOfsshAgent);
3303
				#$conf["commandIncludeArgu"],字串,是否command含有參數,預設為"false"代表沒有;反之為"true".
3304
				#$conf["sock::execAnyCmdByQBPWCFunixSocket"]["commandIncludeArgu"]="true";
3305
				#$conf["commandInBg"],字串,是否要將程序放在背景執行,再取得相關資訊,預設為"false"代表不要;反之為"true".
3306
				#$conf["commandInBg"]="false";
3307
				#參考資料:
3308
				#無.
3309
				#備註:
3310
				#無.
3311
				$createDotsshFolderOfsshAgent=sock::execAnyCmdByQBPWCFunixSocket($conf["sock::execAnyCmdByQBPWCFunixSocket"]);
3312
				unset($conf["sock::execAnyCmdByQBPWCFunixSocket"]);
226 liveuser 3313
 
66 liveuser 3314
				#如果執行shell失敗
3315
				if($createDotsshFolderOfsshAgent["status"]==="false"){
226 liveuser 3316
 
66 liveuser 3317
					#設置執行不正常
3318
					$result["status"]="false";
226 liveuser 3319
 
66 liveuser 3320
					#設置執行錯誤
3321
					$result["error"]=$createDotsshFolderOfsshAgent;
226 liveuser 3322
 
66 liveuser 3323
					#回傳結果
3324
					return $result;
226 liveuser 3325
 
66 liveuser 3326
					}#if end
226 liveuser 3327
 
66 liveuser 3328
				}#if end
226 liveuser 3329
 
66 liveuser 3330
			#若 $dotsshFolderOfsshAgent 的擁有着不為 $sshAgentAcct:$sshAgentAcct
3331
			if($node_info->ownerName!==$sshAgentAcct || $node_info->groupName!==$sshAgentAcct){
226 liveuser 3332
 
66 liveuser 3333
				#更新其擁有着設置
3334
				#函式說明:
3335
				#連線到 usr/bin/qbpwcf-usock.php 產生的  unix domain socket,運行指定的指令.
3336
				#回傳結果:
3337
				#$result["status"],"true"代表執行正常;"false"代表執行不正常.
3338
				#$result["error"],錯誤訊息陣列.
3339
				#$result["function"],當前執行的函式名稱.
3340
				#$result["argu"],使用的參數.
3341
				#$result["content"],執行完指令的結果.
3342
				#必填參數:
3343
				#$conf["fileArgu"],字串,變數__FILE__的內容.
3344
				$conf["sock::execAnyCmdByQBPWCFunixSocket"]["fileArgu"]=__FILE__;
3345
				#$conf["command"],字串,要執行的指令名稱.
3346
				$conf["sock::execAnyCmdByQBPWCFunixSocket"]["command"]="chown";
3347
				#可省略參數:
3348
				#$conf["sock"],字串,要連線的 usr/bin/qbpwcf-sock.php(sock::unixDomainSockServer) 所產生的 unix domain socket 路徑與名稱,預設為 qbpwcf_usock_path.
3349
				#$conf["sock"]=qbpwcf_usock_path;
3350
				#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
3351
				$conf["sock::execAnyCmdByQBPWCFunixSocket"]["argu"]=array($sshAgentAcct.":".$sshAgentAcct,$dotsshFolderOfsshAgent);
3352
				#$conf["commandIncludeArgu"],字串,是否command含有參數,預設為"false"代表沒有;反之為"true".
3353
				#$conf["sock::execAnyCmdByQBPWCFunixSocket"]["commandIncludeArgu"]="true";
3354
				#$conf["commandInBg"],字串,是否要將程序放在背景執行,再取得相關資訊,預設為"false"代表不要;反之為"true".
3355
				#$conf["commandInBg"]="false";
3356
				#參考資料:
3357
				#無.
3358
				#備註:
3359
				#無.
3360
				$createDotsshFolderOfsshAgent=sock::execAnyCmdByQBPWCFunixSocket($conf["sock::execAnyCmdByQBPWCFunixSocket"]);
3361
				unset($conf["sock::execAnyCmdByQBPWCFunixSocket"]);
226 liveuser 3362
 
66 liveuser 3363
				#如果執行shell失敗
3364
				if($createDotsshFolderOfsshAgent["status"]==="false"){
226 liveuser 3365
 
66 liveuser 3366
					#設置執行不正常
3367
					$result["status"]="false";
226 liveuser 3368
 
66 liveuser 3369
					#設置執行錯誤
3370
					$result["error"]=$createDotsshFolderOfsshAgent;
226 liveuser 3371
 
66 liveuser 3372
					#回傳結果
3373
					return $result;
226 liveuser 3374
 
66 liveuser 3375
					}#if end
226 liveuser 3376
 
66 liveuser 3377
				}#if end
226 liveuser 3378
 
66 liveuser 3379
			#設置要使用的ssh private key
3380
			#函式說明:
3381
			#連線到 usr/bin/qbpwcf-usock.php 產生的  unix domain socket,運行指定的指令.
3382
			#回傳結果:
3383
			#$result["status"],"true"代表執行正常;"false"代表執行不正常.
3384
			#$result["error"],錯誤訊息陣列.
3385
			#$result["function"],當前執行的函式名稱.
3386
			#$result["argu"],使用的參數.
3387
			#$result["content"],執行完指令的結果.
3388
			#必填參數:
3389
			#$conf["fileArgu"],字串,變數__FILE__的內容.
3390
			$conf["sock::execAnyCmdByQBPWCFunixSocket"]["fileArgu"]=__FILE__;
3391
			#$conf["command"],字串,要執行的指令名稱.
3392
			$conf["sock::execAnyCmdByQBPWCFunixSocket"]["command"]="echo";
3393
			#可省略參數:
3394
			#$conf["sock"],字串,要連線的 usr/bin/qbpwcf-sock.php(sock::unixDomainSockServer) 所產生的 unix domain socket 路徑與名稱,預設為 qbpwcf_usock_path.
3395
			#$conf["sock"]=qbpwcf_usock_path;
3396
			#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
3397
			$conf["sock::execAnyCmdByQBPWCFunixSocket"]["argu"]=array($sshPrivateKeyContent,">",$dotsshFolderOfsshAgent."/".$sshPrivateKeyFileName);
3398
			#$conf["commandIncludeArgu"],字串,是否command含有參數,預設為"false"代表沒有;反之為"true".
3399
			#$conf["sock::execAnyCmdByQBPWCFunixSocket"]["commandIncludeArgu"]="true";
3400
			#$conf["commandInBg"],字串,是否要將程序放在背景執行,再取得相關資訊,預設為"false"代表不要;反之為"true".
3401
			#$conf["commandInBg"]="false";
3402
			#參考資料:
3403
			#無.
3404
			#備註:
3405
			#無.
3406
			$createSshPrivateKey=sock::execAnyCmdByQBPWCFunixSocket($conf["sock::execAnyCmdByQBPWCFunixSocket"]);
3407
			unset($conf["sock::execAnyCmdByQBPWCFunixSocket"]);
226 liveuser 3408
 
66 liveuser 3409
			#如果執行shell失敗
3410
			if($createSshPrivateKey["status"]==="false"){
226 liveuser 3411
 
66 liveuser 3412
				#設置執行不正常
3413
				$result["status"]="false";
226 liveuser 3414
 
66 liveuser 3415
				#設置執行錯誤
3416
				$result["error"]=$createSshPrivateKey;
226 liveuser 3417
 
66 liveuser 3418
				#回傳結果
3419
				return $result;
226 liveuser 3420
 
66 liveuser 3421
				}#if end
226 liveuser 3422
 
66 liveuser 3423
			#取得 ssh privacy key 檔案的權限與擁有着
3424
			#函式說明:
3425
			#連線到 usr/bin/qbpwcf-usock.php 產生的  unix domain socket,運行指定的指令.
3426
			#回傳結果:
3427
			#$result["status"],"true"代表執行正常;"false"代表執行不正常.
3428
			#$result["error"],錯誤訊息陣列.
3429
			#$result["function"],當前執行的函式名稱.
3430
			#$result["argu"],使用的參數.
3431
			#$result["content"],執行完指令的結果.
3432
			#必填參數:
3433
			#$conf["fileArgu"],字串,變數__FILE__的內容.
3434
			$conf["sock::execAnyCmdByQBPWCFunixSocket"]["fileArgu"]=__FILE__;
3435
			#$conf["command"],字串,要執行的指令名稱.
3436
			$conf["sock::execAnyCmdByQBPWCFunixSocket"]["command"]="getNodeInfo.php";
3437
			#可省略參數:
3438
			#$conf["sock"],字串,要連線的 usr/bin/qbpwcf-sock.php(sock::unixDomainSockServer) 所產生的 unix domain socket 路徑與名稱,預設為 qbpwcf_usock_path.
3439
			#$conf["sock"]=qbpwcf_usock_path;
3440
			#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
3441
			$conf["sock::execAnyCmdByQBPWCFunixSocket"]["argu"]=array("--node",$dotsshFolderOfsshAgent."/".$sshPrivateKeyFileName);
3442
			#$conf["commandIncludeArgu"],字串,是否command含有參數,預設為"false"代表沒有;反之為"true".
3443
			#$conf["sock::execAnyCmdByQBPWCFunixSocket"]["commandIncludeArgu"]="true";
3444
			#$conf["commandInBg"],字串,是否要將程序放在背景執行,再取得相關資訊,預設為"false"代表不要;反之為"true".
3445
			#$conf["commandInBg"]="false";
3446
			#參考資料:
3447
			#無.
3448
			#備註:
3449
			#無.
3450
			$getSshPrivateKeyFileInfo=sock::execAnyCmdByQBPWCFunixSocket($conf["sock::execAnyCmdByQBPWCFunixSocket"]);
3451
			unset($conf["sock::execAnyCmdByQBPWCFunixSocket"]);
226 liveuser 3452
 
66 liveuser 3453
			#如果執行shell失敗
3454
			if($getSshPrivateKeyFileInfo["status"]==="false"){
226 liveuser 3455
 
66 liveuser 3456
				#設置執行不正常
3457
				$result["status"]="false";
226 liveuser 3458
 
66 liveuser 3459
				#設置執行錯誤
3460
				$result["error"]=$getSshPrivateKeyFileInfo;
226 liveuser 3461
 
66 liveuser 3462
				#回傳結果
3463
				return $result;
226 liveuser 3464
 
66 liveuser 3465
				}#if end
226 liveuser 3466
 
66 liveuser 3467
			#如果執行異常
3468
			if($createDotsshFolderOfsshAgent["status"]==="false"){
226 liveuser 3469
 
66 liveuser 3470
				#設置執行不正常
3471
				$result["status"]="false";
226 liveuser 3472
 
66 liveuser 3473
				#設置執行錯誤
3474
				$result["error"]=$getSshPrivateKeyFileInfo;
226 liveuser 3475
 
66 liveuser 3476
				#回傳結果
3477
				return $result;
226 liveuser 3478
 
66 liveuser 3479
				}#if end
226 liveuser 3480
 
66 liveuser 3481
			#如果沒有得到輸出
3482
			if(!isset($createDotsshFolderOfsshAgent["content"][0])){
226 liveuser 3483
 
66 liveuser 3484
				#設置執行不正常
3485
				$result["status"]="false";
226 liveuser 3486
 
66 liveuser 3487
				#設置執行錯誤
3488
				$result["error"]=$createDotsshFolderOfsshAgent;
226 liveuser 3489
 
66 liveuser 3490
				#回傳結果
3491
				return $result;
226 liveuser 3492
 
66 liveuser 3493
				}#if end
226 liveuser 3494
 
66 liveuser 3495
			#如果輸出結果不是json
3496
			if(!json_validate($createDotsshFolderOfsshAgent["content"][0])){
226 liveuser 3497
 
66 liveuser 3498
				#設置執行不正常
3499
				$result["status"]="false";
226 liveuser 3500
 
66 liveuser 3501
				#設置執行錯誤
3502
				$result["error"]=$createDotsshFolderOfsshAgent;
226 liveuser 3503
 
66 liveuser 3504
				#回傳結果
3505
				return $result;
226 liveuser 3506
 
66 liveuser 3507
				}#if end
226 liveuser 3508
 
66 liveuser 3509
			#解析json
3510
			$node_info=json_decode($createDotsshFolderOfsshAgent["content"][0]);
226 liveuser 3511
 
66 liveuser 3512
			#若 ssh privacy key 檔案的擁有着不為 $sshAgentAcct:$sshAgentAcct
3513
			if($node_info->ownerName!==$sshAgentAcct || $node_info->groupName!==$sshAgentAcct){
226 liveuser 3514
 
66 liveuser 3515
				#更新 ssh privacy key 的檔案擁有者
3516
				#函式說明:
3517
				#連線到 usr/bin/qbpwcf-usock.php 產生的  unix domain socket,運行指定的指令.
3518
				#回傳結果:
3519
				#$result["status"],"true"代表執行正常;"false"代表執行不正常.
3520
				#$result["error"],錯誤訊息陣列.
3521
				#$result["function"],當前執行的函式名稱.
3522
				#$result["argu"],使用的參數.
3523
				#$result["content"],執行完指令的結果.
3524
				#必填參數:
3525
				#$conf["fileArgu"],字串,變數__FILE__的內容.
3526
				$conf["sock::execAnyCmdByQBPWCFunixSocket"]["fileArgu"]=__FILE__;
3527
				#$conf["command"],字串,要執行的指令名稱.
3528
				$conf["sock::execAnyCmdByQBPWCFunixSocket"]["command"]="chmod";
3529
				#可省略參數:
3530
				#$conf["sock"],字串,要連線的 usr/bin/qbpwcf-sock.php(sock::unixDomainSockServer) 所產生的 unix domain socket 路徑與名稱,預設為 qbpwcf_usock_path.
3531
				#$conf["sock"]=qbpwcf_usock_path;
3532
				#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
3533
				$conf["sock::execAnyCmdByQBPWCFunixSocket"]["argu"]=array("700",$dotsshFolderOfsshAgent);
3534
				#$conf["commandIncludeArgu"],字串,是否command含有參數,預設為"false"代表沒有;反之為"true".
3535
				#$conf["sock::execAnyCmdByQBPWCFunixSocket"]["commandIncludeArgu"]="true";
3536
				#$conf["commandInBg"],字串,是否要將程序放在背景執行,再取得相關資訊,預設為"false"代表不要;反之為"true".
3537
				#$conf["commandInBg"]="false";
3538
				#參考資料:
3539
				#無.
3540
				#備註:
3541
				#無.
3542
				$createDotsshFolderOfsshAgent=sock::execAnyCmdByQBPWCFunixSocket($conf["sock::execAnyCmdByQBPWCFunixSocket"]);
3543
				unset($conf["sock::execAnyCmdByQBPWCFunixSocket"]);
226 liveuser 3544
 
66 liveuser 3545
				#如果執行shell失敗
3546
				if($createDotsshFolderOfsshAgent["status"]==="false"){
226 liveuser 3547
 
66 liveuser 3548
					#設置執行不正常
3549
					$result["status"]="false";
226 liveuser 3550
 
66 liveuser 3551
					#設置執行錯誤
3552
					$result["error"]=$createDotsshFolderOfsshAgent;
226 liveuser 3553
 
66 liveuser 3554
					#回傳結果
3555
					return $result;
226 liveuser 3556
 
66 liveuser 3557
					}#if end
226 liveuser 3558
 
66 liveuser 3559
				}#if end
226 liveuser 3560
 
66 liveuser 3561
			#若 ssh privacy key 檔案的權限不為 "rw-------"
3562
			if($node_info->ownerPerm!=="rw-" || $node_info->groupPerm!=="---" || $node_info->otherPerm!=="---"){
226 liveuser 3563
 
66 liveuser 3564
				#更新 ssh privacy key 的檔案擁有者
3565
				#函式說明:
3566
				#連線到 usr/bin/qbpwcf-usock.php 產生的  unix domain socket,運行指定的指令.
3567
				#回傳結果:
3568
				#$result["status"],"true"代表執行正常;"false"代表執行不正常.
3569
				#$result["error"],錯誤訊息陣列.
3570
				#$result["function"],當前執行的函式名稱.
3571
				#$result["argu"],使用的參數.
3572
				#$result["content"],執行完指令的結果.
3573
				#必填參數:
3574
				#$conf["fileArgu"],字串,變數__FILE__的內容.
3575
				$conf["sock::execAnyCmdByQBPWCFunixSocket"]["fileArgu"]=__FILE__;
3576
				#$conf["command"],字串,要執行的指令名稱.
3577
				$conf["sock::execAnyCmdByQBPWCFunixSocket"]["command"]="chown";
3578
				#可省略參數:
3579
				#$conf["sock"],字串,要連線的 usr/bin/qbpwcf-sock.php(sock::unixDomainSockServer) 所產生的 unix domain socket 路徑與名稱,預設為 qbpwcf_usock_path.
3580
				#$conf["sock"]=qbpwcf_usock_path;
3581
				#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
3582
				$conf["sock::execAnyCmdByQBPWCFunixSocket"]["argu"]=array($sshAgentAcct.":".$sshAgentAcct,$dotsshFolderOfsshAgent."/".$sshPrivateKeyFileName);
3583
				#$conf["commandIncludeArgu"],字串,是否command含有參數,預設為"false"代表沒有;反之為"true".
3584
				#$conf["sock::execAnyCmdByQBPWCFunixSocket"]["commandIncludeArgu"]="true";
3585
				#$conf["commandInBg"],字串,是否要將程序放在背景執行,再取得相關資訊,預設為"false"代表不要;反之為"true".
3586
				#$conf["commandInBg"]="false";
3587
				#參考資料:
3588
				#無.
3589
				#備註:
3590
				#無.
3591
				$createDotsshFolderOfsshAgent=sock::execAnyCmdByQBPWCFunixSocket($conf["sock::execAnyCmdByQBPWCFunixSocket"]);
3592
				unset($conf["sock::execAnyCmdByQBPWCFunixSocket"]);
226 liveuser 3593
 
66 liveuser 3594
				#如果執行shell失敗
3595
				if($createDotsshFolderOfsshAgent["status"]==="false"){
226 liveuser 3596
 
66 liveuser 3597
					#設置執行不正常
3598
					$result["status"]="false";
226 liveuser 3599
 
66 liveuser 3600
					#設置執行錯誤
3601
					$result["error"]=$createDotsshFolderOfsshAgent;
226 liveuser 3602
 
66 liveuser 3603
					#回傳結果
3604
					return $result;
226 liveuser 3605
 
66 liveuser 3606
					}#if end
226 liveuser 3607
 
66 liveuser 3608
				}#if end
226 liveuser 3609
 
66 liveuser 3610
			#嘗試進行連線,並確認是否成功
3611
			#函式說明:
3612
			#連線到 usr/bin/qbpwcf-usock.php 產生的  unix domain socket,運行指定的指令.
3613
			#回傳結果:
3614
			#$result["status"],"true"代表執行正常;"false"代表執行不正常.
3615
			#$result["error"],錯誤訊息陣列.
3616
			#$result["function"],當前執行的函式名稱.
3617
			#$result["argu"],使用的參數.
3618
			#$result["content"],執行完指令的結果.
3619
			#必填參數:
3620
			#$conf["fileArgu"],字串,變數__FILE__的內容.
3621
			$conf["sock::execAnyCmdByQBPWCFunixSocket"]["fileArgu"]=__FILE__;
3622
			#$conf["command"],字串,要執行的指令名稱.
3623
			$conf["sock::execAnyCmdByQBPWCFunixSocket"]["command"]="echo";
3624
			#可省略參數:
3625
			#$conf["sock"],字串,要連線的 usr/bin/qbpwcf-sock.php(sock::unixDomainSockServer) 所產生的 unix domain socket 路徑與名稱,預設為 qbpwcf_usock_path.
3626
			#$conf["sock"]=qbpwcf_usock_path;
3627
			#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
3628
			$conf["sock::execAnyCmdByQBPWCFunixSocket"]["argu"]=array("env","|","ssh",$sshAgentAcct."@"."127.0.0.1","-i",$dotsshFolderOfsshAgent."/".$sshPrivateKeyFileName,"|","grep","SSH_CLIENT=127.0.0.1","|","wc","-l");
3629
			#$conf["commandIncludeArgu"],字串,是否command含有參數,預設為"false"代表沒有;反之為"true".
3630
			#$conf["sock::execAnyCmdByQBPWCFunixSocket"]["commandIncludeArgu"]="true";
3631
			#$conf["commandInBg"],字串,是否要將程序放在背景執行,再取得相關資訊,預設為"false"代表不要;反之為"true".
3632
			#$conf["commandInBg"]="false";
3633
			#參考資料:
3634
			#無.
3635
			#備註:
3636
			#無.
3637
			$createDotsshFolderOfsshAgent=sock::execAnyCmdByQBPWCFunixSocket($conf["sock::execAnyCmdByQBPWCFunixSocket"]);
3638
			unset($conf["sock::execAnyCmdByQBPWCFunixSocket"]);
226 liveuser 3639
 
66 liveuser 3640
			#如果執行shell失敗
3641
			if($createDotsshFolderOfsshAgent["status"]==="false"){
226 liveuser 3642
 
66 liveuser 3643
				#設置執行不正常
3644
				$result["status"]="false";
226 liveuser 3645
 
66 liveuser 3646
				#設置執行錯誤
3647
				$result["error"]=$createDotsshFolderOfsshAgent;
226 liveuser 3648
 
66 liveuser 3649
				#回傳結果
3650
				return $result;
226 liveuser 3651
 
66 liveuser 3652
				}#if end
226 liveuser 3653
 
66 liveuser 3654
			#若得到的輸出未含有ssh登入的資訊(SSH_CLIENT=127.0.0.1 ...)
3655
			if($createDotsshFolderOfsshAgent["content"]!=="1"){
226 liveuser 3656
 
66 liveuser 3657
				#設置執行正常
3658
				$result["status"]="true";
226 liveuser 3659
 
66 liveuser 3660
				#設置使用者驗證失敗
3661
				$result["valid"]="false";
226 liveuser 3662
 
66 liveuser 3663
				#設置使用者不存在的警告
3664
				$result["warning"][]="使用者 \"".$conf["username"]."\"可能不存在或無法被ssh登入!";
226 liveuser 3665
 
66 liveuser 3666
				#回傳結果
3667
				return $result;
226 liveuser 3668
 
66 liveuser 3669
				}#if end
226 liveuser 3670
 
66 liveuser 3671
			#設置執行正常
3 liveuser 3672
			$result["status"]="true";
226 liveuser 3673
 
66 liveuser 3674
			#設置使用者驗證正常
3675
			$result["valid"]="true";
226 liveuser 3676
 
3 liveuser 3677
			#回傳結果
3678
			return $result;
226 liveuser 3679
 
3 liveuser 3680
			}#if end
226 liveuser 3681
 
3 liveuser 3682
		#反之
66 liveuser 3683
		else{
226 liveuser 3684
 
66 liveuser 3685
			#可以用 echo 'password' | su username -c 'echo valid' 來檢查使用者密碼是否正確
3686
			#檢查使用者是否存在
3687
			#函式說明:
3688
			#呼叫shell執行系統命令,並取得回傳的內容.
3689
			#回傳的結果:
3690
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
3691
			#$result["error"],錯誤訊息陣列.
3692
			#$result["function"],當前執行的函數名稱.
3693
			#$result["cmd"],執行的指令內容.
3694
			#$result["output"],爲執行完二元碼後的輸出陣列.
3695
			#必填的參數
3696
			#$conf["command"],字串,要執行的指令與.
3697
			$conf["external::callShell"]["command"]="echo";
3698
			#可省略參數:
3699
			#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
3700
			$conf["external::callShell"]["argu"]=array($conf["password"],"|","su",$conf["username"],"-c","echo valid");
3701
			#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
3702
			#$conf["enablePrintDescription"]="true";
3703
			#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容.
3704
			#$conf["printDescription"]="";
3705
			#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".
3706
			#$conf["escapeshellarg"]="false";
3707
			#$conf["username"],字串,要用什麼使用者來執行,預設為執行apache的使用者.
3708
			#$conf["external::callShell"]["username"]="";
3709
			#$conf["password"],字串,與$conf["username"]搭配的使用者密碼,預設不使用密碼.
3710
			#$conf["external::callShell"]["password"]="";
3711
			#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要;"false"代表不要,預設為"false".
3712
			$conf["external::callShell"]["useScript"]="true";
3713
			#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 ".qbpwcf_tmp/external/callShell/".
3714
			#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
3715
			#$conf["fileArgu"],字串,變數__FILE__的內容,預設為當前檔案的路徑與名稱.
3716
			$conf["external::callShell"]["fileArgu"]=$conf["fileArgu"];
3717
			#備註:
3718
			#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行.使用root身份可能會被selinux阻擋.
3719
			#參考資料:
3720
			#exec=>http://php.net/manual/en/function.exec.php
3721
			#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
3722
			#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
3723
			$callShell=external::callShell($conf["external::callShell"]);
3724
			unset($conf["external::callShell"]);
226 liveuser 3725
 
66 liveuser 3726
			#如果執行shell失敗
3727
			if($callShell["status"]==="false"){
226 liveuser 3728
 
66 liveuser 3729
				#設置執行不正常
3730
				$result["status"]="false";
226 liveuser 3731
 
66 liveuser 3732
				#設置執行錯誤
3733
				$result["error"]=$callShell;
226 liveuser 3734
 
66 liveuser 3735
				#回傳結果
3736
				return $result;
226 liveuser 3737
 
66 liveuser 3738
				}#if end
226 liveuser 3739
 
66 liveuser 3740
			#範例輸出:
3741
			#Script started on Sat 05 Nov 2016 06:08:31 PM CST
3742
			#Password: valid
226 liveuser 3743
 
66 liveuser 3744
			#如果輸出 "valid"
3745
			if($callShell["output"][1]=="Password: valid"){
226 liveuser 3746
 
66 liveuser 3747
				#代表使用者密碼正確
226 liveuser 3748
 
66 liveuser 3749
				#設置執行正常
3750
				$result["status"]="true";
226 liveuser 3751
 
66 liveuser 3752
				#設置使用者驗證失敗
3753
				$result["valid"]="true";
226 liveuser 3754
 
66 liveuser 3755
				#回傳結果
3756
				return $result;
226 liveuser 3757
 
66 liveuser 3758
				}#if end
226 liveuser 3759
 
66 liveuser 3760
			#反之如果是
3761
			else if($callShell["output"][1]==="Password: su: Authentication failure"){
226 liveuser 3762
 
66 liveuser 3763
				#代表使用者密碼不正確
3764
				$result["status"]="true";
226 liveuser 3765
 
66 liveuser 3766
				#設置使用者驗證失敗
3767
				$result["valid"]="false";
226 liveuser 3768
 
66 liveuser 3769
				#設置使用者不存在的警告
3770
				$result["warning"][]="使用者 \"".$conf["username"]."\" 的密碼不為 \"".$conf["password"]."\" !";
226 liveuser 3771
 
66 liveuser 3772
				#回傳結果
3773
				return $result;
226 liveuser 3774
 
66 liveuser 3775
				}#if end
226 liveuser 3776
 
66 liveuser 3777
			#反之
3778
			else{
226 liveuser 3779
 
66 liveuser 3780
				#設置執行不正常
3781
				$result["status"]="false";
226 liveuser 3782
 
66 liveuser 3783
				#設置錯誤訊息
3784
				$result["error"][]="非預期的輸出結果!";
226 liveuser 3785
 
66 liveuser 3786
				#設置錯誤訊息
3787
				$result["error"][]=$callShell;
226 liveuser 3788
 
66 liveuser 3789
				#回傳結果
3790
				return $result;
226 liveuser 3791
 
66 liveuser 3792
				}#else end
226 liveuser 3793
 
3 liveuser 3794
			}#else end
226 liveuser 3795
 
3 liveuser 3796
		#設置執行不正常
3797
		$result["status"]="false";
226 liveuser 3798
 
3 liveuser 3799
		#設置錯誤訊息
66 liveuser 3800
		$result["error"][]="非預期的結果!";
226 liveuser 3801
 
3 liveuser 3802
		#回傳結果
3803
		return $result;
226 liveuser 3804
 
3 liveuser 3805
		}#function validUser end
226 liveuser 3806
 
3 liveuser 3807
	/*
3808
	#函式說明:
226 liveuser 3809
	#用php的password_hash方法來單向加密密碼.
3 liveuser 3810
	#回傳結果:
3811
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
3812
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
3813
	#$result["function"],當前執行的函式名稱.
3814
	#$result["content"],呼叫完WebService所得回傳結果.
3815
	#必填參數:
3816
	#無
3817
	#可省略參數:
3818
	#$conf["password"],字串,要加密的密碼,預設為"qbpwcf".
3819
	#$conf["password"]="qbpwcf";
3820
	#參考資料:
3821
	#無.
3822
	#備註:
3823
	#無.
3824
	*/
3825
	public static function encodePassword(&$conf=array()){
226 liveuser 3826
 
3 liveuser 3827
		#初始化要回傳的結果
3828
		$result=array();
3829
 
3830
		#取得當前執行的函數名稱
3831
		$result["function"]=__FUNCTION__;
3832
 
3833
		#如果沒有參數
3834
		if(func_num_args()==0){
226 liveuser 3835
 
3 liveuser 3836
			#設置執行失敗
3837
			$result["status"]="false";
226 liveuser 3838
 
3 liveuser 3839
			#設置執行錯誤訊息
3840
			$result["error"]="函數".$result["function"]."需要參數";
226 liveuser 3841
 
3 liveuser 3842
			#回傳結果
3843
			return $result;
226 liveuser 3844
 
3 liveuser 3845
			}#if end
226 liveuser 3846
 
3 liveuser 3847
		#取得參數
3848
		$result["argu"]=$conf;
3849
 
3850
		#如果 $conf 不為陣列
3851
		if(gettype($conf)!=="array"){
226 liveuser 3852
 
3 liveuser 3853
			#設置執行失敗
3854
			$result["status"]="false";
226 liveuser 3855
 
3 liveuser 3856
			#設置執行錯誤訊息
3857
			$result["error"][]="\$conf變數須為陣列形態";
226 liveuser 3858
 
3 liveuser 3859
			#如果傳入的參數為 null
3860
			if($conf===null){
226 liveuser 3861
 
3 liveuser 3862
				#設置執行錯誤訊息
3863
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 3864
 
3 liveuser 3865
				}#if end
3866
 
3867
			#回傳結果
3868
			return $result;
226 liveuser 3869
 
3 liveuser 3870
			}#if end
226 liveuser 3871
 
3 liveuser 3872
		#檢查參數
3873
		#函式說明:
3874
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
3875
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
3876
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
3877
		#$result["function"],當前執行的函式名稱.
3878
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
3879
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
3880
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
3881
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
3882
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
3883
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
3884
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
3885
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
3886
		#必填寫的參數:
3887
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
226 liveuser 3888
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
3 liveuser 3889
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
3890
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
3891
		#可以省略的參數:
3892
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
3893
		#$conf["mustBeFilledVariableName"]=array();
3894
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
3895
		#$conf["mustBeFilledVariableType"]=array();
3896
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
3897
		#$conf["canBeEmptyString"]="false";
3898
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
3899
		#$conf["canNotBeEmpty"]=array();
3900
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
3901
		#$conf["canBeEmpty"]=array();
3902
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
3903
		#$conf["skipableVariableCanNotBeEmpty"]=array();
3904
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
3905
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("password");
226 liveuser 3906
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
3 liveuser 3907
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string");
3908
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
3909
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("qbpwcf");
3910
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
3911
		#$conf["disallowAllSkipableVarIsEmpty"]="";
3912
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
3913
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
3914
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
3915
		#$conf["arrayCountEqualCheck"][]=array();
3916
		#參考資料來源:
3917
		#array_keys=>http://php.net/manual/en/function.array-keys.php
3918
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
3919
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 3920
 
3 liveuser 3921
		#如果驗證失敗
3922
		if($checkArguments["status"]==="false"){
226 liveuser 3923
 
3 liveuser 3924
			#設置執行失敗
3925
			$result["status"]="false";
226 liveuser 3926
 
3 liveuser 3927
			#設置錯誤訊息
3928
			$result["error"]=$checkArguments;
226 liveuser 3929
 
3 liveuser 3930
			#回傳結果
3931
			return $result;
226 liveuser 3932
 
3 liveuser 3933
			}#if end
226 liveuser 3934
 
3 liveuser 3935
		#如果驗證不成功
3936
		if($checkArguments["passed"]==="false"){
226 liveuser 3937
 
3 liveuser 3938
			#設置執行失敗
3939
			$result["status"]="false";
226 liveuser 3940
 
3 liveuser 3941
			#設置錯誤訊息
3942
			$result["error"]=$checkArguments;
226 liveuser 3943
 
3 liveuser 3944
			#回傳結果
3945
			return $result;
226 liveuser 3946
 
3 liveuser 3947
			}#if end
226 liveuser 3948
 
3 liveuser 3949
		#取得加密好的密碼
3950
		$result["content"]=password_hash($conf["password"],PASSWORD_DEFAULT);
226 liveuser 3951
 
3 liveuser 3952
		#設置執行正常
3953
		$result["status"]="true";
226 liveuser 3954
 
3 liveuser 3955
		#回傳結果
3956
		return $result;
226 liveuser 3957
 
3 liveuser 3958
		}#function encodePassword end
226 liveuser 3959
 
3 liveuser 3960
	/*
3961
	#函式說明:
226 liveuser 3962
	#用php的password_verify方法來驗證密碼是否正確.
3 liveuser 3963
	#回傳結果:
3964
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
3965
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
3966
	#$result["function"],當前執行的函式名稱.
3967
	#$result["content"],呼叫完WebService所得回傳結果.
3968
	#必填參數:
3969
	#$conf["input"],字串,要檢查是否正確的待驗證密碼.
3970
	$conf["input"]="";
3971
	#$conf["password"],字串,透過encodePassword加密後的密碼.
3972
	$conf["password"]="";
3973
	#可省略參數:
3974
	#無.
3975
	#參考資料:
3976
	#無.
3977
	#備註:
3978
	#無.
3979
	*/
3980
	public static function validPassword(&$conf=array()){
226 liveuser 3981
 
3 liveuser 3982
		#初始化要回傳的結果
3983
		$result=array();
3984
 
3985
		#取得當前執行的函數名稱
3986
		$result["function"]=__FUNCTION__;
3987
 
3988
		#如果沒有參數
3989
		if(func_num_args()==0){
226 liveuser 3990
 
3 liveuser 3991
			#設置執行失敗
3992
			$result["status"]="false";
226 liveuser 3993
 
3 liveuser 3994
			#設置執行錯誤訊息
3995
			$result["error"]="函數".$result["function"]."需要參數";
226 liveuser 3996
 
3 liveuser 3997
			#回傳結果
3998
			return $result;
226 liveuser 3999
 
3 liveuser 4000
			}#if end
226 liveuser 4001
 
3 liveuser 4002
		#取得參數
4003
		$result["argu"]=$conf;
4004
 
4005
		#如果 $conf 不為陣列
4006
		if(gettype($conf)!=="array"){
226 liveuser 4007
 
3 liveuser 4008
			#設置執行失敗
4009
			$result["status"]="false";
226 liveuser 4010
 
3 liveuser 4011
			#設置執行錯誤訊息
4012
			$result["error"][]="\$conf變數須為陣列形態";
226 liveuser 4013
 
3 liveuser 4014
			#如果傳入的參數為 null
4015
			if($conf===null){
226 liveuser 4016
 
3 liveuser 4017
				#設置執行錯誤訊息
4018
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 4019
 
3 liveuser 4020
				}#if end
4021
 
4022
			#回傳結果
4023
			return $result;
226 liveuser 4024
 
3 liveuser 4025
			}#if end
226 liveuser 4026
 
3 liveuser 4027
		#檢查參數
4028
		#函式說明:
4029
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
4030
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
4031
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
4032
		#$result["function"],當前執行的函式名稱.
4033
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
4034
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
4035
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
4036
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
4037
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
4038
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
4039
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
4040
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
4041
		#必填寫的參數:
4042
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
226 liveuser 4043
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
3 liveuser 4044
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
4045
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
4046
		#可以省略的參數:
4047
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
4048
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("input","password");
4049
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
4050
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string");
4051
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
4052
		#$conf["canBeEmptyString"]="false";
4053
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
4054
		#$conf["canNotBeEmpty"]=array();
4055
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
4056
		#$conf["canBeEmpty"]=array();
4057
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
4058
		#$conf["skipableVariableCanNotBeEmpty"]=array();
4059
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
4060
		#$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("password");
226 liveuser 4061
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
3 liveuser 4062
		#$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string");
4063
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
4064
		#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("qbpwcf");
4065
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
4066
		#$conf["disallowAllSkipableVarIsEmpty"]="";
4067
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
4068
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
4069
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
4070
		#$conf["arrayCountEqualCheck"][]=array();
4071
		#參考資料來源:
4072
		#array_keys=>http://php.net/manual/en/function.array-keys.php
4073
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
4074
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 4075
 
3 liveuser 4076
		#如果驗證失敗
4077
		if($checkArguments["status"]==="false"){
226 liveuser 4078
 
3 liveuser 4079
			#設置執行失敗
4080
			$result["status"]="false";
226 liveuser 4081
 
3 liveuser 4082
			#設置錯誤訊息
4083
			$result["error"]=$checkArguments;
226 liveuser 4084
 
3 liveuser 4085
			#回傳結果
4086
			return $result;
226 liveuser 4087
 
3 liveuser 4088
			}#if end
226 liveuser 4089
 
3 liveuser 4090
		#如果驗證不成功
4091
		if($checkArguments["passed"]==="false"){
226 liveuser 4092
 
3 liveuser 4093
			#設置執行失敗
4094
			$result["status"]="false";
226 liveuser 4095
 
3 liveuser 4096
			#設置錯誤訊息
4097
			$result["error"]=$checkArguments;
226 liveuser 4098
 
3 liveuser 4099
			#回傳結果
4100
			return $result;
226 liveuser 4101
 
3 liveuser 4102
			}#if end
226 liveuser 4103
 
3 liveuser 4104
		#儲存驗證後的結果
4105
		$pass=password_verify($conf["input"],$conf["password"]);
226 liveuser 4106
 
3 liveuser 4107
		#如果驗證成功
4108
		if($pass){
226 liveuser 4109
 
3 liveuser 4110
			#設置驗證成功
4111
			$result["passed"]="true";
226 liveuser 4112
 
3 liveuser 4113
			}#if end
226 liveuser 4114
 
3 liveuser 4115
		#反之驗證失敗
4116
		else{
226 liveuser 4117
 
3 liveuser 4118
			#設置驗證成功
4119
			$result["passed"]="false";
226 liveuser 4120
 
3 liveuser 4121
			}#else end
226 liveuser 4122
 
3 liveuser 4123
		#設置執行正常
4124
		$result["status"]="true";
226 liveuser 4125
 
3 liveuser 4126
		#回傳結果
4127
		return $result;
226 liveuser 4128
 
3 liveuser 4129
		}#function validPassword end
4130
 
66 liveuser 4131
	/*
4132
	#函式說明:
4133
	#id指令來取得目標賬號的群組資訊
4134
	#回傳結果:
4135
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
4136
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
4137
	#$result["function"],當前執行的函式名稱.
4138
	#$result["content"],呼叫完WebService所得回傳結果.
4139
	#$result["content"]["groupId"],群組的id.
4140
	#$result["content"]["belongGroupName"],陣列,隸屬的群組名稱陣列.
4141
	#$result["content"]["belongGroupId"],陣列,隸屬的群組id.
4142
	#必填參數:
4143
	#$conf["user"],字串,要取得群組資訊的目標使用者名稱.
4144
	$conf["user"]="";
4145
	#可省略參數:
4146
	#無.
4147
	#參考資料:
4148
	#無.
4149
	#備註:
4150
	#無.
4151
	*/
4152
	public static function getUserGroup(&$conf){
226 liveuser 4153
 
66 liveuser 4154
		#初始化要回傳的結果
4155
		$result=array();
4156
 
4157
		#取得當前執行的函數名稱
4158
		$result["function"]=__FUNCTION__;
4159
 
4160
		#如果沒有參數
4161
		if(func_num_args()==0){
226 liveuser 4162
 
66 liveuser 4163
			#設置執行失敗
4164
			$result["status"]="false";
226 liveuser 4165
 
66 liveuser 4166
			#設置執行錯誤訊息
4167
			$result["error"]="函數".$result["function"]."需要參數";
226 liveuser 4168
 
66 liveuser 4169
			#回傳結果
4170
			return $result;
226 liveuser 4171
 
66 liveuser 4172
			}#if end
226 liveuser 4173
 
66 liveuser 4174
		#取得參數
4175
		$result["argu"]=$conf;
4176
 
4177
		#如果 $conf 不為陣列
4178
		if(gettype($conf)!=="array"){
226 liveuser 4179
 
66 liveuser 4180
			#設置執行失敗
4181
			$result["status"]="false";
226 liveuser 4182
 
66 liveuser 4183
			#設置執行錯誤訊息
4184
			$result["error"][]="\$conf變數須為陣列形態";
226 liveuser 4185
 
66 liveuser 4186
			#如果傳入的參數為 null
4187
			if($conf===null){
226 liveuser 4188
 
66 liveuser 4189
				#設置執行錯誤訊息
4190
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 4191
 
66 liveuser 4192
				}#if end
4193
 
4194
			#回傳結果
4195
			return $result;
226 liveuser 4196
 
66 liveuser 4197
			}#if end
226 liveuser 4198
 
66 liveuser 4199
		#函式說明:
4200
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
4201
		#回傳結果:
4202
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
4203
		#$result["error"],執行不正常結束的錯訊息陣列.
4204
		#$result["simpleError"],簡單表示的錯誤訊息.
4205
		#$result["function"],當前執行的函式名稱.
4206
		#$result["argu"],設置給予的參數.
4207
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
4208
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
4209
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
4210
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
4211
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
4212
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
4213
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
4214
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
4215
		#必填參數:
4216
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
4217
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
4218
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
4219
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
4220
		#可省略參數:
4221
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
4222
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("user");
4223
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null、any代表不指定變數形態.其中 resource也包含"resource (closed)".
4224
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string");
4225
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
4226
		$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
4227
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
4228
		#$conf["canNotBeEmpty"]=array();
4229
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
4230
		#$conf["canBeEmpty"]=array();
4231
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
4232
		#$conf["skipableVariableCanNotBeEmpty"]=array();
4233
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
4234
		#$conf["skipableVariableName"]=array();
4235
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
4236
		#$conf["skipableVariableType"]=array();
4237
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
4238
		#$conf["skipableVarDefaultValue"]=array("");
4239
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
4240
		#$conf["disallowAllSkipableVarIsEmpty"]="";
4241
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
4242
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
4243
		#$conf["disallowAllSkipableVarNotExist"],字串,是否不允許每個可省略參數都不存在,預設為"false"代表允許,反之為"true".
4244
		#$conf["disallowAllSkipableVarNotExist"]="";
4245
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
4246
		#$conf["arrayCountEqualCheck"][]=array();
4247
		#參考資料:
4248
		#array_keys=>http://php.net/manual/en/function.array-keys.php
4249
		#備註:
4250
		#無.
4251
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
4252
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 4253
 
66 liveuser 4254
		#如果驗證失敗
4255
		if($checkArguments["status"]==="false"){
226 liveuser 4256
 
66 liveuser 4257
			#設置執行失敗
4258
			$result["status"]="false";
226 liveuser 4259
 
66 liveuser 4260
			#設置錯誤訊息
4261
			$result["error"]=$checkArguments;
226 liveuser 4262
 
66 liveuser 4263
			#回傳結果
4264
			return $result;
226 liveuser 4265
 
66 liveuser 4266
			}#if end
226 liveuser 4267
 
66 liveuser 4268
		#如果驗證不成功
4269
		if($checkArguments["passed"]==="false"){
226 liveuser 4270
 
66 liveuser 4271
			#設置執行失敗
4272
			$result["status"]="false";
226 liveuser 4273
 
66 liveuser 4274
			#設置錯誤訊息
4275
			$result["error"]=$checkArguments;
226 liveuser 4276
 
66 liveuser 4277
			#回傳結果
4278
			return $result;
226 liveuser 4279
 
66 liveuser 4280
			}#if end
226 liveuser 4281
 
66 liveuser 4282
		#函式說明:
4283
		#呼叫shell執行系統命令,並取得回傳的內容.
4284
		#回傳結果:
4285
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
4286
		#$result["error"],錯誤訊息陣列.
4287
		#$result["function"],當前執行的函數名稱.
4288
		#$result["argu"],使用的參數.
4289
		#$result["cmd"],執行的指令內容.
4290
		#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
4291
		#$result["output"],爲執行完後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
4292
		#$result["content"],為執行完後的輸出字串.
4293
		#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
4294
		#$result["running"],是否還在執行.
4295
		#$result["pid"],pid.
4296
		#$result["statusCode"],執行結束後的代碼.
4297
		#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
4298
		#$result["noEcaped"],陣列,儲存重新排序過未經過escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
4299
		#必填參數:
4300
		#$conf["command"],字串,要執行的指令.
4301
		$conf["external::callShell"]["command"]="id";
4302
		#$conf["fileArgu"],字串,變數__FILE__的內容.
4303
		$conf["external::callShell"]["fileArgu"]=__FILE__;
4304
		#可省略參數:
4305
		#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
4306
		$conf["external::callShell"]["argu"]=array($user);
4307
		#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
4308
		#$conf["arguIsAddr"]=array();
4309
		#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
4310
		#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
4311
		#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
4312
		#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
4313
		#$conf["enablePrintDescription"]="true";
4314
		#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
4315
		#$conf["printDescription"]="";
4316
		#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
4317
		$conf["external::callShell"]["escapeshellarg"]="true";
4318
		#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
4319
		#$conf["thereIsShellVar"]=array();
4320
		#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
4321
		#$conf["username"]="";
4322
		#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
4323
		#$conf["password"]="";
4324
		#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
4325
		#$conf["useScript"]="";
4326
		#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
4327
		#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
4328
		#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
4329
		#$conf["inBackGround"]="";
4330
		#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
4331
		#$conf["getErr"]="false";
4332
		#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
4333
		#$conf["doNotRun"]="false";
4334
		#參考資料:
4335
		#exec=>http://php.net/manual/en/function.exec.php
4336
		#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
4337
		#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
4338
		#備註:
4339
		#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
4340
		#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
4341
		$callShell=external::callShell($conf["external::callShell"]);
4342
		unset($conf["external::callShell"]);
226 liveuser 4343
 
66 liveuser 4344
		#如果執行異常
4345
		if($callShell["status"]==="false"){
226 liveuser 4346
 
66 liveuser 4347
			#設置執行失敗
4348
			$result["status"]="false";
226 liveuser 4349
 
66 liveuser 4350
			#設置錯誤訊息
4351
			$result["error"]=$callShell;
226 liveuser 4352
 
66 liveuser 4353
			#回傳結果
4354
			return $result;
226 liveuser 4355
 
66 liveuser 4356
			}#if end
226 liveuser 4357
 
66 liveuser 4358
		#若輸出不對
4359
		if(count($callShell["content"])===0){
226 liveuser 4360
 
66 liveuser 4361
			#設置執行失敗
4362
			$result["status"]="false";
226 liveuser 4363
 
66 liveuser 4364
			#設置錯誤訊息
4365
			$result["error"]=$callShell;
226 liveuser 4366
 
66 liveuser 4367
			#回傳結果
4368
			return $result;
226 liveuser 4369
 
66 liveuser 4370
			}#if end
226 liveuser 4371
 
66 liveuser 4372
		#解析輸出資訊
4373
		#範例輸出:
4374
		#uid=1000(liveuser) gid=1000(liveuser) groups=1000(liveuser),10(wheel),1002(web)
226 liveuser 4375
 
66 liveuser 4376
		#函式說明:
4377
		#尋找字串中是否含有符合格式的內容,且回傳解析好的變數數值.
4378
		#回傳結果:
4379
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
4380
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
4381
		#$result["function"],當前執行的函式名稱.
4382
		#$result["argu"],所使用的參數.
4383
		#$result["found"],是否有找到符合格式的字串內容,"true"代表有找到,"false"代表沒有找到.
4384
		#$result["content"],陣列,若為n個${*},則當found為"true"時,就會回傳n個元素.
4385
		#$result["parsedVar"][varName],陣列,解析好的變數陣列,varName為${}中的內容.
4386
		#必填參數:
4387
		#$conf["input"],字串,要檢查的字串.
4388
		$conf["search::findSpecifyStrFormat"]["input"]=$callShell["content"][0];
4389
		#$conf["format"],格式字串,要尋找的格式字串.格式為固定的字串("fixedStr format")與變數("${keyWordVarName}")組成.
4390
		$conf["search::findSpecifyStrFormat"]["format"]="uid=\${userId}(\${userName}) gid=\${groupId}(\${groupName}) groups=\${repeatedGroupId}(\${repeatedGroupName})\${otherGroupStr}";
4391
		#可省略參數:
4392
		#$conf["varEqual"],陣列,變數對應的數值,null代表不指定,其他內容代表該變數解析出來必須要為該內容.
4393
		#$conf["varEqual"]=array(null,"found");
4394
		#$conf["varCon"],陣列,每個varEqual為null者,其是否有其他條件,預設為null代表無其他條件,條件的表示是用陣列的key與value來表達,例如:array("no_tail"=>" not"),就代表變數的結尾不能為" not",可以用的key有"head",代表開頭要有什麼;"no_head",代表不能為什麼開頭;"tail",代表要什麼結尾;"no_tail",代表不能什麼結尾.
4395
		#$conf["varCon"]=array("no_tail"=>" not");
4396
		#參考資料:
4397
		#無.
4398
		#備註:
4399
		#無.
4400
		$findSpecifyStrFormat=search::findSpecifyStrFormat($conf["search::findSpecifyStrFormat"]);
4401
		unset($conf["search::findSpecifyStrFormat"]);
226 liveuser 4402
 
66 liveuser 4403
		#如果執行異常
4404
		if($findSpecifyStrFormat["status"]==="false"){
226 liveuser 4405
 
66 liveuser 4406
			#設置執行失敗
4407
			$result["status"]="false";
226 liveuser 4408
 
66 liveuser 4409
			#設置錯誤訊息
4410
			$result["error"]=$findSpecifyStrFormat;
226 liveuser 4411
 
66 liveuser 4412
			#回傳結果
4413
			return $result;
226 liveuser 4414
 
66 liveuser 4415
			}#if end
226 liveuser 4416
 
66 liveuser 4417
		#如果沒有符合格式的內容
4418
		if($findSpecifyStrFormat["found"]==="false"){
226 liveuser 4419
 
66 liveuser 4420
			#設置執行失敗
4421
			$result["status"]="false";
226 liveuser 4422
 
66 liveuser 4423
			#設置錯誤訊息
4424
			$result["error"]=$findSpecifyStrFormat;
226 liveuser 4425
 
66 liveuser 4426
			#回傳結果
4427
			return $result;
226 liveuser 4428
 
66 liveuser 4429
			}#if end
226 liveuser 4430
 
66 liveuser 4431
		#另存使用者id
4432
		$userId=$findSpecifyStrFormat["parsedVar"]["userId"][0];
226 liveuser 4433
 
66 liveuser 4434
		#另存群組的id
4435
		$groupId=$findSpecifyStrFormat["parsedVar"]["groupId"][0];
226 liveuser 4436
 
66 liveuser 4437
		#另存隸屬其他群組的資訊
4438
		$othergGroupStr=$findSpecifyStrFormat["parsedVar"]["othergGroupStr"][0];
226 liveuser 4439
 
66 liveuser 4440
		#如果有其他群組資訊
4441
		if(!empty($othergGroupStr)){
226 liveuser 4442
 
66 liveuser 4443
			#用逗號切割結果出來
4444
			#函式說明:
4445
			#將固定格式的字串分開,並回傳分開的結果.
4446
			#回傳結果:
4447
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
4448
			#$result["error"],錯誤訊息陣列
4449
			#$result["function"],當前執行的函數名稱.
4450
			#$result["argu"],使用的參數.
4451
			#$result["oriStr"],要分割的原始字串內容
4452
			#$result["dataArray"],爲分割好字串的陣列內容,$result["dataArray"][$i]爲第($i+1)段的內容。
4453
			#$result["dataCounts"],爲總共分成幾段
4454
			#$result["found"],是否有在$conf["stringIn"]找到$conf["spiltSymbol"],"true"代表有找到,"false"代表沒有找到.
4455
			#必填參數:
4456
			#$conf["stringIn"],字串,要處理的字串.
4457
			$conf["stringProcess::spiltString"]["stringIn"]=$othergGroupStr;
4458
			#$conf["spiltSymbol"],字串,爲以哪個符號作爲分割.
4459
			$conf["stringProcess::spiltString"]["spiltSymbol"]=",";
4460
			#可省略參數:
4461
			#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
4462
			#$conf["allowEmptyStr"]="false";
4463
			#參考資料:
4464
			#無.
4465
			#備註:
4466
			#無.
4467
			$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
4468
			unset($conf["stringProcess::spiltString"]);
226 liveuser 4469
 
66 liveuser 4470
			#如果執行異常
4471
			if($spiltString["status"]==="false"){
226 liveuser 4472
 
66 liveuser 4473
				#設置執行失敗
4474
				$result["status"]="false";
226 liveuser 4475
 
66 liveuser 4476
				#設置錯誤訊息
4477
				$result["error"]=$spiltString;
226 liveuser 4478
 
66 liveuser 4479
				#回傳結果
4480
				return $result;
226 liveuser 4481
 
66 liveuser 4482
				}#if end
226 liveuser 4483
 
66 liveuser 4484
			#如果沒有符合格式的內容
4485
			if($spiltString["found"]==="false"){
226 liveuser 4486
 
66 liveuser 4487
				#設置執行失敗
4488
				$result["status"]="false";
226 liveuser 4489
 
66 liveuser 4490
				#設置錯誤訊息
4491
				$result["error"]=$spiltString;
226 liveuser 4492
 
66 liveuser 4493
				#回傳結果
4494
				return $result;
226 liveuser 4495
 
66 liveuser 4496
				}#if end
226 liveuser 4497
 
66 liveuser 4498
			#依照分隔成的段數
4499
			foreach($spiltString["dataArray"] as $oginfo){
226 liveuser 4500
 
66 liveuser 4501
				#解析groudId跟groupName
226 liveuser 4502
 
66 liveuser 4503
				#範例輸出:
4504
				#10(wheel)
226 liveuser 4505
 
66 liveuser 4506
				#函式說明:
4507
				#尋找字串中是否含有符合格式的內容,且回傳解析好的變數數值.
4508
				#回傳結果:
4509
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
4510
				#$reuslt["error"],執行不正常結束的錯訊息陣列.
4511
				#$result["function"],當前執行的函式名稱.
4512
				#$result["argu"],所使用的參數.
4513
				#$result["found"],是否有找到符合格式的字串內容,"true"代表有找到,"false"代表沒有找到.
4514
				#$result["content"],陣列,若為n個${*},則當found為"true"時,就會回傳n個元素.
4515
				#$result["parsedVar"][varName],陣列,解析好的變數陣列,varName為${}中的內容.
4516
				#必填參數:
4517
				#$conf["input"],字串,要檢查的字串.
4518
				$conf["search::findSpecifyStrFormat"]["input"]=$oginfo;
4519
				#$conf["format"],格式字串,要尋找的格式字串.格式為固定的字串("fixedStr format")與變數("${keyWordVarName}")組成.
4520
				$conf["search::findSpecifyStrFormat"]["format"]="gid=\${groupId}(\${groupName})";
4521
				#可省略參數:
4522
				#$conf["varEqual"],陣列,變數對應的數值,null代表不指定,其他內容代表該變數解析出來必須要為該內容.
4523
				#$conf["varEqual"]=array(null,"found");
4524
				#$conf["varCon"],陣列,每個varEqual為null者,其是否有其他條件,預設為null代表無其他條件,條件的表示是用陣列的key與value來表達,例如:array("no_tail"=>" not"),就代表變數的結尾不能為" not",可以用的key有"head",代表開頭要有什麼;"no_head",代表不能為什麼開頭;"tail",代表要什麼結尾;"no_tail",代表不能什麼結尾.
4525
				#$conf["varCon"]=array("no_tail"=>" not");
4526
				#參考資料:
4527
				#無.
4528
				#備註:
4529
				#無.
4530
				$findSpecifyStrFormat=search::findSpecifyStrFormat($conf["search::findSpecifyStrFormat"]);
4531
				unset($conf["search::findSpecifyStrFormat"]);
226 liveuser 4532
 
66 liveuser 4533
				#如果執行異常
4534
				if($findSpecifyStrFormat["status"]==="false"){
226 liveuser 4535
 
66 liveuser 4536
					#設置執行失敗
4537
					$result["status"]="false";
226 liveuser 4538
 
66 liveuser 4539
					#設置錯誤訊息
4540
					$result["error"]=$findSpecifyStrFormat;
226 liveuser 4541
 
66 liveuser 4542
					#回傳結果
4543
					return $result;
226 liveuser 4544
 
66 liveuser 4545
					}#if end
226 liveuser 4546
 
66 liveuser 4547
				#如果格式異常
4548
				if($findSpecifyStrFormat["found"]==="false"){
226 liveuser 4549
 
66 liveuser 4550
					#設置執行失敗
4551
					$result["status"]="false";
226 liveuser 4552
 
66 liveuser 4553
					#設置錯誤訊息
4554
					$result["error"]=$findSpecifyStrFormat;
226 liveuser 4555
 
66 liveuser 4556
					#回傳結果
4557
					return $result;
226 liveuser 4558
 
66 liveuser 4559
					}#if end
226 liveuser 4560
 
66 liveuser 4561
				#如果是 user 自己的群組
4562
				if($findSpecifyStrFormat["parsedVar"]["groupName"][0]===$user){
226 liveuser 4563
 
66 liveuser 4564
					#跳過該群組資訊
4565
					continue;
226 liveuser 4566
 
4567
					}#if end
4568
 
66 liveuser 4569
				#儲存其他 group id
4570
				$otherGroupId[]=$findSpecifyStrFormat["parsedVar"]["groupId"][0];
226 liveuser 4571
 
66 liveuser 4572
				#儲存其他 group name
4573
				$otherGroupName[]=$findSpecifyStrFormat["parsedVar"]["groupName"][0];
226 liveuser 4574
 
66 liveuser 4575
				}#foreach end
226 liveuser 4576
 
66 liveuser 4577
			}#if end
226 liveuser 4578
 
66 liveuser 4579
		#設置 group id 結果
4580
		$result["content"]["groupId"]=$groupId;
226 liveuser 4581
 
66 liveuser 4582
		#設置隸屬的 group 名稱
4583
		$result["content"]["belongGroupName"]=$otherGroupName;
226 liveuser 4584
 
66 liveuser 4585
		#設置隸屬的 group id
4586
		$result["content"]["belongGroupId"]=$otherGroupId;
226 liveuser 4587
 
66 liveuser 4588
		#設置執行正常
4589
		$result["status"]="true";
226 liveuser 4590
 
4591
		#回傳結果
66 liveuser 4592
		return $result;
4593
 
226 liveuser 4594
		}#function getUserGroup end
4595
 
3 liveuser 4596
	}#class authenticate end
226 liveuser 4597
 
3 liveuser 4598
?>