-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck.php
More file actions
197 lines (175 loc) · 6.18 KB
/
check.php
File metadata and controls
197 lines (175 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
require_once 'constants.php';
require_once 'return_interface.php';
require_once 'sql_action.php';
//If invalid, stop this script and return error message.
function checkColValid($tables, $action_columns) {
if(count($action_columns) <= 0) {
returnException("column數量至少要是1");
}
$table_columns = $GLOBALS['table_columns'];
$confusing_columns = $GLOBALS['confusing_columns'];
$allow_cols = [];
foreach($tables as $table) {
$allow_cols = array_merge($allow_cols, $table_columns[$table]);
}
foreach($action_columns as $column) {
if(in_array($column, $confusing_columns)) {
returnException("出現易混淆column,請使用更名後的column");
}
if(!in_array($column, $allow_cols)) {
returnException($column.",此column無法使用,請檢查是否有打錯字或是少join_table。");
}
}
}
//If invalid, stop this script and return error message.
function checkValValid($columns, $values) {
if(count($columns) != count($values)) {
returnException("欄位和值數量不同");
}
for($i=0; $i<count($columns); $i++) {
//if invalid, stop script and echo error message.
checkSingleValValid($columns[$i], $values[$i]);
}
}
//If invalid, stop this script and return error message.
function checkValRangeValid($columns, $values) {
if(count($columns) != count($values)) {
returnException("欄位和值數量不同");
}
for($i=0; $i<count($columns); $i++) {
$value = $values[$i];
if(in_array($columns[$i], $GLOBALS['number_like_columns'])) {
// start with [ or (, and then number, and then space or not, and then number, and then end with ) or ].
if(preg_match("/^[\[\(][-0-9.]*,[-0-9.]*[\]\)]$/i", $value)) {
$value = explode(",", substr($values[$i], 1, -1));
if($value[0] != "") {
checkSingleValValid($columns[$i], $value[0]);
}
if($value[1] != "") {
checkSingleValValid($columns[$i], $value[1]);
}
continue;
}
}
if(in_array($columns[$i], $GLOBALS['text_like_columns'])) {
return;
}
//if invalid, stop script and echo error message.
checkSingleValValid($columns[$i], $value);
}
}
function checkSingleValValid($column, $value) {
$text_columns = $GLOBALS['text_columns'];
$id_columns = $GLOBALS['id_columns'];
$foreign_id_columns = $GLOBALS['foreign_id_columns'];
$int_columns = $GLOBALS['int_columns'];
$double_columns = $GLOBALS['double_columns'];
$date_columns = $GLOBALS['date_columns'];
$value_rules = $GLOBALS['value_rules'];
if(in_array($column, $text_columns)) {
//no need to check valid.
}
else if(in_array($column, $id_columns)) {
if(!preg_match("/^[0-9a-zA-Z+\/,=-]{22}$/i", $value)) {
returnException($ccolumn."欄位傳入值為: ".$value.",應為僅包含英數且長度為22的字串");
}
}
else if(in_array($column, $foreign_id_columns)) {
if(!preg_match("/^[0-9a-zA-Z+\/,=-]{22}$/i", $value)) {
returnException($column."欄位傳入值為: ".$value.",應為僅包含英數且長度為22的字串,如果想讓他null,直接不要把它放在action_columns裡面就好");
}
//for only one nullable foreign key in this database.
if(!inSQL("audio_features", "audio_features_id", $value)) {
returnException($column."欄位傳入值為: ".$value.",未在audio_features.id中找到對應外鍵");
}
}
else if(in_array($column, $int_columns)) {
$val = (int)$value;
$rule = $value_rules[$column];
if($val != $value) {
returnException($column."欄位傳入值為: ".$value.",請輸入整數");
}
if($val < $rule[0] || $val > $rule[1]) {
returnException($column."欄位傳入值為: ".$val.",數值範圍應介於".$rule[0]."和".$rule[1]."之間");
}
}
else if(in_array($column, $double_columns)) {
$val = (float)$value;
$rule = $value_rules[$column];
if($val != $value) {
returnException($column."欄位傳入值為: ".$value.",請輸入數字");
}
if($val < $rule[0] || $val > $rule[1]) {
returnException($column."欄位傳入值為: ".$val.",數值範圍應介於".$rule[0]."和".$rule[1]."之間");
}
}
else if(in_array($column, $date_columns)) {
if(!preg_match("/^\d{4}-\d{2}-\d{2}$/i", $value)) {
returnException($column."欄位傳入值為: ".$value.",請輸入yyyy-mm-dd形式");
}
if(!checkDateValid($value)) {
returnException($column."欄位傳入值為: ".$value.",請輸入合理的日期");
}
}
}
function checkDateValid($date) {
$year = substr($date, 0, 4);
$month = substr($date, 5, 2);
$day = substr($date, 8, 2);
return checkdate($month, $day, $year);
}
function checkCanJoin($tables) {
if(count($tables) == 1) {
return;
}
if(in_array('genres', $tables)) {
if(!in_array('artists', $tables)) {
returnException("檢測到輸入table包含genres,但並未包含artists,無法join");
}
}
if(in_array('audio_features', $tables)) {
if(!in_array('tracks', $tables)) {
returnException("檢測到輸入table包含audio_features,但並未包含tracks,無法join");
}
}
}
function checkParaValid($para, $tables) {
$to_ret = [];
if($para['limit'] != (int)$para['limit']) {
returnException("limit傳入值為: ".$para['limit'].",請輸入正整數");
}
if((int)$para['limit'] <= 0) {
returnException("limit傳入值為: ".$para['limit'].",請輸入大於0的數字");
}
$to_ret['limit'] = (int)$para['limit'];
if($para['page'] != (int)$para['page']) {
returnException("page傳入值為: ".$para['page'].",請輸入正整數");
}
if((int)$para['page'] < 0) {
returnException("page傳入值為: ".$para['page'].",請輸入不小於0的數字");
}
$to_ret['page'] = (int)$para['page'];
if($para['order'] == -1) {
unset($para['order']);
unset($para['order_direction']);
return $to_ret;
}
if($para['order_direction'] != 0 && $para['order_direction'] != 1) {
returnException("order_direction傳入值為: ".$para['order_direction'].",請輸入0或1");
}
$in_table = false;
foreach($tables as $table) {
if(in_array($para['order'], $GLOBALS['table_columns'][$table])) {
$in_table = true;
break;
}
}
if(!$in_table) {
returnException("order傳入值為: ".$para['order'].",未找到此column,請確認是否輸入錯誤或是否join到");
}
$to_ret['order'] = $para['order'];
$to_ret['order_direction'] = (int)$para['order_direction'];
return $to_ret;
}
?>