1 | #!/usr/local/bin/php -q |
---|
2 | <?php |
---|
3 | /** |
---|
4 | * validation.cli.php |
---|
5 | * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information |
---|
6 | */ |
---|
7 | |
---|
8 | include_once dirname(__FILE__) . '/_config.inc.php'; |
---|
9 | |
---|
10 | // Test arguments. |
---|
11 | if (isset($_SERVER['argv'][2])) { |
---|
12 | // Second arg is db table. |
---|
13 | $db_tbl = $_SERVER['argv'][2]; |
---|
14 | } else { |
---|
15 | die(sprintf("Usage: %s site_directory db_table\n", basename($_SERVER['argv'][0]))); |
---|
16 | } |
---|
17 | |
---|
18 | // Get DB tables. |
---|
19 | $qid = $db->query("SHOW TABLES"); |
---|
20 | while (list($row) = mysql_fetch_row($qid)) { |
---|
21 | $tables[] = $row; |
---|
22 | } |
---|
23 | |
---|
24 | // Make sure requested table is in database. |
---|
25 | if (!in_array($db_tbl, $tables)) { |
---|
26 | die(sprintf("%s Warning: %s does not exist in database %s. Please select one of: \n\n%s\n\n", basename($_SERVER['argv'][0]), $db_tbl, $app->getParam('db_name'), join("\n", $tables))); |
---|
27 | } |
---|
28 | |
---|
29 | // Get DB table column info. |
---|
30 | $qid = $db->query("DESCRIBE " . $db->escapeString($db_tbl)); |
---|
31 | while ($row = mysql_fetch_row($qid)) { |
---|
32 | $cols[] = $row; |
---|
33 | } |
---|
34 | |
---|
35 | // Exclude these fields. |
---|
36 | $exclude = array('added_by_user_id', 'added_datetime', 'hit_count', 'modified_datetime', 'modified_by_user_id'); |
---|
37 | |
---|
38 | // Loop through columns |
---|
39 | if (is_array($cols) && !empty($cols)) { |
---|
40 | |
---|
41 | $o = array(); |
---|
42 | |
---|
43 | foreach ($cols as $col) { |
---|
44 | |
---|
45 | // Human readable. |
---|
46 | $field = $col[0]; |
---|
47 | $title = ucfirst(str_replace('_', ' ', $field)); |
---|
48 | $type = preg_replace('/^(\w+).*$/', '\\1', $col[1]); |
---|
49 | $is_primary_key = ('PRI' == $col[3]); |
---|
50 | $unsigned = preg_match('/\s*unsigned\s*$/i', $col[1]); |
---|
51 | |
---|
52 | if (in_array($field, $exclude)) { |
---|
53 | continue; |
---|
54 | } |
---|
55 | |
---|
56 | // ----------- isEmpty ------------ |
---|
57 | $o[] = "\$fv->isEmpty('$field', _(\"<strong>$title</strong> cannot be blank.\"));"; |
---|
58 | |
---|
59 | // ----------- stringLength ------------ |
---|
60 | $max_length = null; |
---|
61 | $min = null; |
---|
62 | $max = null; |
---|
63 | $len_type = null; |
---|
64 | switch ($type) { |
---|
65 | case 'enum' : |
---|
66 | case 'set' : |
---|
67 | $max_length = 255; |
---|
68 | $len_type = 'setenum'; |
---|
69 | break; |
---|
70 | |
---|
71 | case 'date' : |
---|
72 | case 'datetime' : |
---|
73 | case 'timestamp' : |
---|
74 | case 'time' : |
---|
75 | case 'year' : |
---|
76 | $len_type = 'string'; |
---|
77 | $max_length = 127; |
---|
78 | break; |
---|
79 | |
---|
80 | case 'tinytext' : |
---|
81 | case 'tinyblob' : |
---|
82 | case 'char' : |
---|
83 | case 'varchar' : |
---|
84 | $len_type = 'string'; |
---|
85 | $max_length = 255; |
---|
86 | break; |
---|
87 | |
---|
88 | case 'text' : |
---|
89 | case 'blob' : |
---|
90 | $len_type = 'string'; |
---|
91 | $max_length = 65535; |
---|
92 | break; |
---|
93 | |
---|
94 | case 'mediumtext' : |
---|
95 | case 'mediumblob' : |
---|
96 | $len_type = 'string'; |
---|
97 | $max_length = 16777215; |
---|
98 | break; |
---|
99 | |
---|
100 | case 'longtext' : |
---|
101 | case 'longblob' : |
---|
102 | $len_type = 'string'; |
---|
103 | $max_length = 4294967295; |
---|
104 | break; |
---|
105 | |
---|
106 | case 'tinyint' : |
---|
107 | case 'bit' : |
---|
108 | case 'bool' : |
---|
109 | $len_type = 'num'; |
---|
110 | if ($unsigned) { |
---|
111 | $min = 0; |
---|
112 | $max = 255; |
---|
113 | } else { |
---|
114 | $min = -128; |
---|
115 | $max = 127; |
---|
116 | } |
---|
117 | break; |
---|
118 | |
---|
119 | case 'smallint' : |
---|
120 | $len_type = 'num'; |
---|
121 | if ($unsigned) { |
---|
122 | $min = 0; |
---|
123 | $max = 65536; |
---|
124 | } else { |
---|
125 | $min = -32768; |
---|
126 | $max = 32767; |
---|
127 | } |
---|
128 | break; |
---|
129 | |
---|
130 | case 'mediumint' : |
---|
131 | $len_type = 'num'; |
---|
132 | if ($unsigned) { |
---|
133 | $min = 0; |
---|
134 | $max = 16777215; |
---|
135 | } else { |
---|
136 | $min = -8388608; |
---|
137 | $max = 8388607; |
---|
138 | } |
---|
139 | break; |
---|
140 | |
---|
141 | case 'int' : |
---|
142 | case 'integer' : |
---|
143 | $len_type = 'num'; |
---|
144 | if ($unsigned) { |
---|
145 | $min = 0; |
---|
146 | $max = 4294967295; |
---|
147 | } else { |
---|
148 | $min = -2147483648; |
---|
149 | $max = 2147483647; |
---|
150 | } |
---|
151 | break; |
---|
152 | |
---|
153 | case 'bigint' : |
---|
154 | $len_type = 'num'; |
---|
155 | if ($unsigned) { |
---|
156 | $min = 0; |
---|
157 | $max = 1.84467E+19; |
---|
158 | } else { |
---|
159 | $min = -9.22337E+18; |
---|
160 | $max = 9.22337E+18; |
---|
161 | } |
---|
162 | break; |
---|
163 | |
---|
164 | case 'float' : |
---|
165 | $len_type = 'num'; |
---|
166 | $min = -3.40282E+38; |
---|
167 | $max = 3.40282E+38; |
---|
168 | break; |
---|
169 | |
---|
170 | case 'double' : |
---|
171 | case 'double precision' : |
---|
172 | case 'real' : |
---|
173 | case 'decimal' : |
---|
174 | case 'dec' : |
---|
175 | case 'numeric' : |
---|
176 | $len_type = 'num'; |
---|
177 | $min = -1.79769E+308; |
---|
178 | $max = 1.79769E+308; |
---|
179 | break; |
---|
180 | |
---|
181 | default : |
---|
182 | $len_type = null; |
---|
183 | break; |
---|
184 | } |
---|
185 | if ($max_length > 0 && $len_type == 'setenum') { |
---|
186 | $o[] = "\$fv->stringLength('$field', 0, $max_length, _(\"<strong>$title</strong> has an invalid selection.\"));"; |
---|
187 | } |
---|
188 | if ($max_length > 0 && $len_type == 'string') { |
---|
189 | $o[] = "\$fv->stringLength('$field', 0, $max_length, _(\"<strong>$title</strong> must contain less than " . ($max_length+1) . " characters.\"));"; |
---|
190 | } |
---|
191 | if ($len_type == 'num') { |
---|
192 | $o[] = "\$fv->numericRange('$field', $min, $max, _(\"<strong>$title</strong> must be a valid number between $min and $max.\"));"; |
---|
193 | } |
---|
194 | |
---|
195 | // ----------- type check ------------ |
---|
196 | switch ($type) { |
---|
197 | |
---|
198 | case 'enum' : |
---|
199 | |
---|
200 | case 'set' : |
---|
201 | break; |
---|
202 | |
---|
203 | case 'char' : |
---|
204 | case 'varchar' : |
---|
205 | |
---|
206 | case 'tinytext' : |
---|
207 | case 'text' : |
---|
208 | case 'mediumtext' : |
---|
209 | case 'longtext' : |
---|
210 | case 'tinyblob' : |
---|
211 | case 'blob' : |
---|
212 | case 'mediumblob' : |
---|
213 | case 'longblob' : |
---|
214 | // $o[] = "\$fv->isString('$field', _(\"<strong>$title</strong> must be a string.\"));"; // Pretty useless |
---|
215 | break; |
---|
216 | |
---|
217 | case 'tinyint' : |
---|
218 | case 'bit' : |
---|
219 | case 'bool' : |
---|
220 | case 'smallint' : |
---|
221 | case 'mediumint' : |
---|
222 | case 'int' : |
---|
223 | case 'integer' : |
---|
224 | case 'bigint' : |
---|
225 | $negative_ok = $unsigned ? '' : ', true'; |
---|
226 | $o[] = "\$fv->isInteger('$field', _(\"<strong>$title</strong> must be an integer.\")$negative_ok);"; |
---|
227 | break; |
---|
228 | |
---|
229 | case 'float' : |
---|
230 | case 'float' : |
---|
231 | case 'double' : |
---|
232 | case 'double' : |
---|
233 | case 'real' : |
---|
234 | case 'decimal' : |
---|
235 | case 'dec' : |
---|
236 | case 'numeric' : |
---|
237 | $negative_ok = $unsigned ? '' : ', true'; |
---|
238 | $o[] = "\$fv->isFloat('$field', _(\"<strong>$title</strong> must be a valid number.\")$negative_ok);"; |
---|
239 | break; |
---|
240 | |
---|
241 | case 'date' : |
---|
242 | case 'datetime' : |
---|
243 | $o[] = "\$fv->validateStrDate('$field', _(\"<strong>$title</strong> must be a valid date in YYYY-MM-DD format.\"));"; |
---|
244 | $o[] = "\$fv->checkRegex('$field', '/^\d{4}-\d{2}-\d{2}$/', true, _(\"<strong>$title</strong> must be a valid date in YYYY-MM-DD format.\"));"; |
---|
245 | break; |
---|
246 | |
---|
247 | case 'timestamp' : |
---|
248 | $o[] = "\$fv->checkRegex('$field', '/^\d{14}$/', true, _(\"<strong>$title</strong> must be a valid mysql timestamp in YYYYMMDDhhmmss format.\"));"; |
---|
249 | break; |
---|
250 | |
---|
251 | case 'time' : |
---|
252 | $o[] = "\$fv->checkRegex('$field', '/^\d{1,3}:\d{2}:\d{2}$/', true, _(\"<strong>$title</strong> must be a valid time in hh:mm:ss format.\"));"; |
---|
253 | break; |
---|
254 | |
---|
255 | case 'year' : |
---|
256 | $o[] = "\$fv->checkRegex('$field', '/^\d{4}$/', true, _(\"<strong>$title</strong> must be a valid year in YYYY format.\"));"; |
---|
257 | break; |
---|
258 | |
---|
259 | default : |
---|
260 | break; |
---|
261 | } |
---|
262 | |
---|
263 | if (preg_match('/email/', $field)) { |
---|
264 | $o[] = "\$fv->validateEmail('$field');"; |
---|
265 | } else if (preg_match('/phone/', $field)) { |
---|
266 | $o[] = "\$fv->validatePhone('$field');"; |
---|
267 | } |
---|
268 | |
---|
269 | // Blank between cols? |
---|
270 | $o[] = ''; |
---|
271 | } |
---|
272 | } else { |
---|
273 | die(sprintf("%s Warning: %s does not have any columns.\n", basename($_SERVER['argv'][0]), $db_tbl)); |
---|
274 | } |
---|
275 | |
---|
276 | // Sort? |
---|
277 | // natsort($o); |
---|
278 | |
---|
279 | echo "function validateInput() |
---|
280 | { |
---|
281 | global \$fv; |
---|
282 | |
---|
283 | "; |
---|
284 | echo join("\n ", $o); |
---|
285 | echo " |
---|
286 | }"; |
---|
287 | ?> |
---|