Subversion Repositories qbpwcf-lib(archive)

Rev

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

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