Subversion Repositories php-qbpwcf

Rev

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

Rev Author Line No. Line
1 liveuser 1
<?php
2
 
3
/*
4
	QBPWCF, Quick Build PHP website Component base on Fedora Linux.
5
    Copyright (C) 2015~2023 Min-Jhin,Chen
6
 
7
    This file is part of QBPWCF.
8
 
9
    QBPWCF is free software: you can redistribute it and/or modify
10
    it under the terms of the GNU General Public License as published by
11
    the Free Software Foundation, either version 3 of the License, or
12
    (at your option) any later version.
13
 
14
    QBPWCF is distributed in the hope that it will be useful,
15
    but WITHOUT ANY WARRANTY; without even the implied warranty of
16
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
    GNU General Public License for more details.
18
 
19
    You should have received a copy of the GNU General Public License
20
    along with QBPWCF.  If not, see <http://www.gnu.org/licenses/>.
21
 
22
*/
23
 
24
/*
25
 
26
更新資料庫資料
27
 
28
*/
29
 
30
#使用命名空間qbpwcf
31
namespace qbpwcf;
32
 
33
#初始化輸出
34
$output=array();
35
 
36
#取得 lib path
106 liveuser 37
exec("php -f ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/../../../../../usr/bin/libexec/folderOfUsrLib.php"),$output,$status);
1 liveuser 38
 
39
#如果執行失敗
40
if($status!==0){
41
 
42
	#debug
43
	var_dump(__LINE__,$output);
44
 
45
	#結束執行,回傳shell 1.
46
	exit(1);
47
 
48
	}#if end
49
 
50
#儲存lib path
51
$folderOfUsrLib=$output[0];
52
 
53
#初始化輸出
54
$output=array();
55
 
56
#以該檔案的實際位置的 lib path 為 include path 首位
106 liveuser 57
exec("cd ".pathinfo(__FILE__)["dirname"]."/../../../../../usr/".$folderOfUsrLib."/qbpwcf;pwd;",$output,$status);
1 liveuser 58
set_include_path($output[0].PATH_SEPARATOR.get_include_path());
59
 
60
#匯入外部套件
61
include("allInOneForJson.php");
62
 
63
#建議的log位置
64
$logFile=$_SERVER["DOCUMENT_ROOT"].$_SERVER["PHP_SELF"].".log"
65
 
66
/*
67
 
68
# test case - 1 - start
69
 
70
$params=array();
71
$params["dbSecret"]="";
72
$params["dbAddr"]="localhost";
73
$params["dbPort"]="3306";
74
$params["dbName"]="test";
75
$params["dbUser"]="test";
76
$params["dbPass"]="test";
77
$params["dtName"]="member";
78
$params["cols"]=array("account","password","comment");
79
$params["vals"]=array("test acct","test pass updated","test comment updated");
80
$params["wcols"]=array("account");
81
$params["wvals"]=array("test acct");
82
 
83
#設置參數
84
$_POST["params"]=base64_encode(urlencode(json_encode($params)));
85
 
86
# test case - 1 - end
87
 
88
*/
89
 
90
#涵式說明:
91
#可以處理多個透過GET、POST而來的資訊,儲存成變數,同時限定傳送的方法、來源,來增加安全性,檢查有沒有皆收到必須要接收到的變數,沒有接收到的變數可以指定從session變數中取得.
92
#回傳的結果:
93
#$result["status"],執行正常與否,"true"代表正常,"false"代表不正常.
94
#$result["error"],錯誤訊息陣列.
95
#$resutl["function"],當前執行的涵式名稱.
96
#$result["warning"],警告訊息陣列.
97
#$result["passed"],是否有皆收到全部該接收到的變數,若有設定$conf["checkedVarName"]的話,執行正常後會回傳該結果。
98
#$result["lostVarName"],沒有皆收到的變數名稱陣列
99
#$result["inputDataContent"],所接收的參數陣列.
100
#$result["inputDataContent"]["變數名稱"],所接收變數的內容.
101
#$result["inputDataCount"],從表單總共接收到幾個元素.
102
#$result["HTTP_REFERER"],前一頁的網址,null代表不存在.
103
#必填的參數:
104
#$conf["method"],字串,傳送過來的資料是用"post/POST"還是"get/GET"?
105
$conf["method"]="POST";
106
#可省略的參數:
107
#$conf["allowGet"],字串,是否要允許 get 傳值,"true",代表允許;"false" ,代表不允許,預設爲不允許.
108
#$conf["allowGet"]="true";
109
#$conf["limitPrePage"],字串陣列,是否要限定前一頁的網址為哪些,才能接收內容,不符合則轉址.
110
#$conf["limitPrePage"]=array("");
111
#$conf["transferLocation"],字串,遇到get傳值,或前一個頁面不符合要求時要轉址到的頁面,預設爲資安素養網"https://isafe.moe.edu.tw/"
112
#$conf["transferLocation"]="";
113
#$conf["ignore"]=字串陣列,如果要接收的變數名稱與該陣列其一元素一樣,則不放進要回傳的變數裏面.
114
#$conf["ignore"]=array();
115
#$conf["correctCharacter"]=,字串,如果爲"false",則不處理可能導致網頁出問題的字串,預設爲要進行處理.
116
#$conf["correctCharacter"]="false";
117
#$conf["checkedVarName"],字串陣列,為檢查是否有接收到哪些變數.
118
$conf["checkedVarName"]=array("params");
119
#$conf["canBeEmptyString"],字串,用$conf["checkedVarName"]指定接收的變數名稱陣列,若接收到的內容為空字串是否算有接收到內容,預設為"false","true"代表接收到的內容可以為空字串,"false"代表接收到的內容不可以為空字串.
120
#$conf["canBeEmptyString"]="false";
121
#$conf["sessionNameArray"],陣列,若存在則代表若沒有從表單取得變數,則從session變數中取得內容,每個元素代表每個表單變數對應的session名稱,若不是要改用session方式取得變數內容的變數,請輸入null,數量請跟$conf["checkedVarName"]參數一致.
122
#$conf["sessionNameArray"]=array();
123
#$conf["unsetSessionArray"],陣列,與$conf["sessionNameArray"]對應的元素,是否要接收到變數後就卸除,"true"代表要卸除,"false"代表不要卸除.
124
#$conf["unsetSessionArray"]=array();
125
#$conf["recaptcha_url"],字串,有內容代表要檢查有無透過recaptcha於特定頁面網址認證過.
126
#$conf["recaptcha_url"]="";
127
#參考資料來源:
128
#foreach 的用法 -> http://php.net/manual/en/control-structures.foreach.php
129
#伺服器端的變數 -> http://php.net/manual/en/reserved.variables.server.php
130
#備註:
131
#表單變數的名稱若含有「.」,則會變成「-」。
132
$responseMultiInputDataSecurityEnhance=form::responseMultiInputDataSecurityEnhance($conf);
133
unset($conf);
134
 
135
#函式說明:
136
#撰寫log
137
#回傳結果:
138
#$result["status"],狀態,"true"或"false".
139
#$result["error"],錯誤訊息陣列.
140
#$result["function"],當前函式的名稱.
141
#$result["argu"],使用的參數.
142
#必填參數:
143
#$conf["path"],log檔案的路徑與名稱.
144
$conf["path"]=$logFile;
145
#$conf["content"],字串,要寫的內容.
146
$conf["content"]=json_encode($responseMultiInputDataSecurityEnhance).PHP_EOL;
147
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
148
$conf["fileArgu"]=__FILE__;
149
#可省略參數:
150
#$conf["rewrite"],預設為"false",接續寫入;反之"true"代表重新寫入.
151
#$conf["rewrite"]="false";
152
#參考資料:
153
#無.
154
#備註:
155
#無.	
156
$record=logs::record($conf);
157
unset($conf);
158
 
159
#如果寫log 失敗
160
if($record["status"]==="false"){
161
 
162
	#印出訊息
163
	echo json_encode($record);
164
 
165
	#結束執行
166
	exit;
167
 
168
	}#if end
169
 
170
#如果執行失敗
171
if($responseMultiInputDataSecurityEnhance["status"]==="false"){
172
 
173
	#印出訊息
174
	echo json_encode($responseMultiInputDataSecurityEnhance);
175
 
176
	#結束執行
177
	exit;
178
 
179
	}#if end
180
 
181
#如果邊數檢查不通過
182
if($responseMultiInputDataSecurityEnhance["passed"]==="false"){
183
 
184
	#印出訊息
185
	echo json_encode($responseMultiInputDataSecurityEnhance);
186
 
187
	#結束執行
188
	exit;
189
 
190
	}#if end
191
 
192
#取得參數
193
$params=$responseMultiInputDataSecurityEnhance["inputDataContent"]["params"];
194
 
195
#解析變數
196
$params=json_decode(urldecode(base64_decode($params)));
197
 
198
#轉換為陣列
199
$params=(array)$params;
200
 
201
#函式說明:
202
#撰寫log
203
#回傳結果:
204
#$result["status"],狀態,"true"或"false".
205
#$result["error"],錯誤訊息陣列.
206
#$result["function"],當前函式的名稱.
207
#$result["argu"],使用的參數.
208
#必填參數:
209
#$conf["path"],log檔案的路徑與名稱.
210
$conf["path"]=$logFile;
211
#$conf["content"],字串,要寫的內容.
212
$conf["content"]=json_encode($params).PHP_EOL;
213
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
214
$conf["fileArgu"]=__FILE__;
215
#可省略參數:
216
#$conf["rewrite"],預設為"false",接續寫入;反之"true"代表重新寫入.
217
#$conf["rewrite"]="false";
218
#參考資料:
219
#無.
220
#備註:
221
#無.	
222
$record=logs::record($conf);
223
unset($conf);
224
 
225
#如果寫log 失敗
226
if($record["status"]==="false"){
227
 
228
	#印出訊息
229
	echo json_encode($record);
230
 
231
	#結束執行
232
	exit;
233
 
234
	}#if end
235
 
236
#如果少了使用資料庫系統的dbSecret
237
if(!isset($params["dbSecret"])){
238
 
239
	#結束執行
240
	exit;
241
 
242
	}#if end
243
 
244
#驗證密碼是否正確
245
else{
246
 
247
	#若dbSecret錯誤
248
	if($params["dbSecret"]!==dbSecret){
249
 
250
		#結束執行
251
		exit;
252
 
253
		}#if end
254
 
255
	#移除傳遞的 dbSecret
256
	unset($params["dbSecret"]);
257
 
258
	}#else end
259
 
260
#檢查變數
261
#函式說明:
262
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
263
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
264
#$reuslt["error"],執行不正常結束的錯訊息陣列.
265
#$result["function"],當前執行的函式名稱.
266
#$result["argu"],設置給予的參數.
267
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
268
#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
269
#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
270
#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
271
#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
272
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
273
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
274
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
275
#必填寫的參數:
276
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
277
$conf["variableCheck::checkArguments"]["varInput"]=&$params;
278
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
279
$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
280
#可以省略的參數:
281
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
282
$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("dbAddr","dbName","dbUser","dbPass","dtName","cols","vals","wcols","wvals");
283
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
284
$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string","string","string","string","array","array","array","array");
285
#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
286
$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
287
#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
288
#$conf["canNotBeEmpty"]=array();
289
#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
290
#$conf["canBeEmpty"]=array();
291
#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
292
#$conf["skipableVariableCanNotBeEmpty"]=array();
293
#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
294
$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("dbPort");
295
#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
296
$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string");
297
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
298
$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("3306");
299
#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
300
#$conf["disallowAllSkipableVarIsEmpty"]="";
301
#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
302
#$conf["disallowAllSkipableVarIsEmptyArray"]="";
303
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
304
$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"][]=array("cols","vals");
305
#參考資料來源:
306
#array_keys=>http://php.net/manual/en/function.array-keys.php
307
$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
308
unset($conf["variableCheck::checkArguments"]);
309
 
310
#函式說明:
311
#撰寫log
312
#回傳結果:
313
#$result["status"],狀態,"true"或"false".
314
#$result["error"],錯誤訊息陣列.
315
#$result["function"],當前函式的名稱.
316
#$result["argu"],使用的參數.
317
#必填參數:
318
#$conf["path"],log檔案的路徑與名稱.
319
$conf["path"]=$logFile;
320
#$conf["content"],字串,要寫的內容.
321
$conf["content"]=json_encode($checkArguments).PHP_EOL;
322
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
323
$conf["fileArgu"]=__FILE__;
324
#可省略參數:
325
#$conf["rewrite"],預設為"false",接續寫入;反之"true"代表重新寫入.
326
#$conf["rewrite"]="false";
327
#參考資料:
328
#無.
329
#備註:
330
#無.	
331
$record=logs::record($conf);
332
unset($conf);
333
 
334
#如果寫log 失敗
335
if($record["status"]==="false"){
336
 
337
	#印出訊息
338
	echo json_encode($record);
339
 
340
	#結束執行
341
	exit;
342
 
343
	}#if end
344
 
345
#如果執行失敗
346
if($checkArguments["status"]==="false"){
347
 
348
	#印出訊息
349
	echo json_encode($checkArguments);
350
 
351
	#結束執行
352
	exit;
353
 
354
	}#if end
355
 
356
#如果邊數檢查不通過
357
if($checkArguments["passed"]==="false"){
358
 
359
	#印出訊息
360
	echo json_encode($checkArguments);
361
 
362
	#結束執行
363
	exit;
364
 
365
	}#if end
366
 
367
#取得資料庫連線參數
368
$params["dbAddr"];
369
$params["dbPort"];#預設為3306
370
$params["dbName"];
371
$params["dbUser"];
372
$params["dbPass"];
373
$params["dtName"];
374
$params["cols"];#要更新的欄位
375
$params["vals"];#要更新的欄位數值
376
$params["wcols"];#條件欄位
377
$params["wvals"];#條件欄位對應數值
378
 
379
#函式說明:
380
#更新資料表裏面的資料
381
#回傳的結果:
382
#$result["status"],爲查詢是否成功,若爲0則成功,若爲1則表示失敗了
383
#$result["error"],錯誤訊息
384
#$result["function"],當前執行的函數名稱
385
#$result["sql"],執行的sql語法.
386
#必填參數:
387
#$conf["fileArgu"],字串,變數__FILE__的內容.
388
$conf["fileArgu"]=__FILE__;
389
#$conf["dbAddress"],字串,爲mysql-Server的位置
390
$conf["dbAddress"]=$params["dbAddr"];
391
#$conf["dbAccount"],字串,爲用於連入mysql-Server時要使用的帳號
392
$conf["dbAccount"]=$params["dbUser"];
393
#$conf["selectedDataBaseName"],字串,要選取的資料庫名稱
394
$conf["selectedDataBaseName"]=$params["dbName"];
395
#$conf["tableName"],字串,要修改的資料表名稱
396
$conf["tableName"]=$params["dtName"];
397
#$conf["whereColumn"],字串陣列,爲用來判斷要更新的哪筆資料的欄位名稱,須爲陣列值
398
$conf["whereColumn"]=$params["wcols"];
399
#$conf["whereValue"],字串陣列,為用來判斷是要更新哪筆資料的欄位對應數值,須爲陣列值
400
$conf["whereValue"]=$params["wvals"];
401
#$conf["tableColumnName"],字串陣列,資料表項目的名稱陣列
402
$conf["tableColumnName"]=$params["cols"];
403
#$conf["updateDataValue"],字串陣列,要更改成對應資料表項目的內容,須為陣列值
404
$conf["updateDataValue"]=$params["vals"];
405
#可省略參數:
406
#$conf["dbPassword"],字串,爲連線到mysql-Server時要使用的密碼,可省略,若省略則代表不使用密碼.
407
$conf["dbPassword"]=$params["dbPass"];
408
#$conf["dbPort"],字串,爲連線到mysql-Server時要使用的port,可省略,若省略則代表使用預設的port 3306.
409
$conf["dbPort"]=$params["dbPort"];
410
$updateDataTableContent=db::updateDataTableContent($conf);
411
unset($conf);
412
 
413
#函式說明:
414
#撰寫log
415
#回傳結果:
416
#$result["status"],狀態,"true"或"false".
417
#$result["error"],錯誤訊息陣列.
418
#$result["function"],當前函式的名稱.
419
#$result["argu"],使用的參數.
420
#必填參數:
421
#$conf["path"],log檔案的路徑與名稱.
422
$conf["path"]=$logFile;
423
#$conf["content"],字串,要寫的內容.
424
$conf["content"]=json_encode($updateDataTableContent).PHP_EOL;
425
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
426
$conf["fileArgu"]=__FILE__;
427
#可省略參數:
428
#$conf["rewrite"],預設為"false",接續寫入;反之"true"代表重新寫入.
429
#$conf["rewrite"]="false";
430
#參考資料:
431
#無.
432
#備註:
433
#無.	
434
$record=logs::record($conf);
435
unset($conf);
436
 
437
#如果寫log 失敗
438
if($record["status"]==="false"){
439
 
440
	#印出訊息
441
	echo json_encode($record);
442
 
443
	#結束執行
444
	exit;
445
 
446
	}#if end
447
 
448
#印出結果
449
echo json_encode($updateDataTableContent);