source: trunk/bin/module_maker/validation.cli.php @ 807

Last change on this file since 807 was 807, checked in by anonymous, 3 months ago

Minor improvements. Add Validator::IPAddress() method.

File size: 10.5 KB
Line 
1#!/usr/bin/env php
2<?php
3/**
4 * The Strangecode Codebase - a general application development framework for PHP
5 * For details visit the project site: <http://trac.strangecode.com/codebase/>
6 * Copyright 2001-2012 Strangecode, LLC
7 *
8 * This file is part of The Strangecode Codebase.
9 *
10 * The Strangecode Codebase is free software: you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as published by the
12 * Free Software Foundation, either version 3 of the License, or (at your option)
13 * any later version.
14 *
15 * The Strangecode Codebase is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * details.
19 *
20 * You should have received a copy of the GNU General Public License along with
21 * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24/**
25 * validation.cli.php
26 */
27
28if ($_SERVER['argc'] > 1 && isset($_SERVER['argv'][1]) && '' != $_SERVER['argv'][1] && is_dir($_SERVER['argv'][1])) {
29    // First arg is path to current site. Realpath removes trailing /s
30    define('COMMON_BASE', realpath($_SERVER['argv'][1]));
31} else {
32    die("Error: First argument must be the directory path to an existing site (ex: /home/sc/www.strangecode.com).\n");
33}
34
35include_once dirname(__FILE__) . '/../_config.inc.php';
36
37// Test arguments.
38if (isset($_SERVER['argv'][2])) {
39    // Second arg is db table.
40    $db_tbl = $_SERVER['argv'][2];
41} else {
42    die(sprintf("Usage: %s site_directory db_table\n", basename($_SERVER['argv'][0])));
43}
44
45// Get DB tables.
46$qid = $db->query("SHOW TABLES");
47while (list($row) = mysql_fetch_row($qid)) {
48    $tables[] = $row;
49}
50
51// Make sure requested table is in database.
52if (!in_array($db_tbl, $tables)) {
53    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)));
54}
55
56// Get DB table column info.
57$qid = $db->query("DESCRIBE " . $db->escapeString($db_tbl));
58while ($row = mysql_fetch_row($qid)) {
59    $cols[] = $row;
60}
61
62// Exclude these fields.
63$exclude = array('added_by_user_id', 'added_datetime', 'hit_count', 'modified_datetime', 'modified_by_user_id');
64
65// Loop through columns
66if (is_array($cols) && !empty($cols)) {
67
68    $o = array();
69
70    foreach ($cols as $col) {
71
72        // Human readable.
73        $field = $col[0];
74        $title = ucfirst(str_replace('_', ' ', $field));
75        $type = preg_replace('/^(\w+).*$/', '$1', $col[1]);
76        $max_dig = preg_replace('/^\w+\((\d+)(?:,\d+)?\).*$/', '$1', $col[1]);
77        $max_dec = preg_replace('/^\w+\((?:\d+)(?:,(\d+))?\).*$/', '$1', $col[1]);
78        $is_primary_key = ('PRI' == $col[3]);
79        $unsigned = preg_match('/\s+unsigned\s*$/i', $col[1]);
80
81        if (in_array($field, $exclude)) {
82            continue;
83        }
84
85        // ----------- isEmpty ------------
86        $o[] = "\$fv->notEmpty('$field', sprintf(_(\"%s cannot be blank.\"), _(\"$title\")));";
87
88        // ----------- stringLength ------------
89        $max_length = null;
90        $min = null;
91        $max = null;
92        $len_type = null;
93        switch ($type) {
94        case 'enum' :
95        case 'set' :
96            $max_length = 255;
97            $len_type = 'setenum';
98            break;
99
100        case 'date' :
101        case 'datetime' :
102        case 'timestamp' :
103        case 'time' :
104        case 'year' :
105            $len_type = 'string';
106            $max_length = 127;
107            break;
108
109        case 'tinytext' :
110        case 'tinyblob' :
111        case 'char' :
112        case 'varchar' :
113            $len_type = 'string';
114            $max_length = '' != $max_dig ? $max_dig : 255;
115            break;
116
117        case 'text' :
118        case 'blob' :
119            $len_type = 'string';
120            $max_length = 65535;
121            break;
122
123        case 'mediumtext' :
124        case 'mediumblob' :
125            $len_type = 'string';
126            $max_length = 16777215;
127            break;
128
129        case 'longtext' :
130        case 'longblob' :
131            $len_type = 'string';
132            $max_length = 4294967295;
133            break;
134
135        case 'bit' :
136            $len_type = 'num';
137            $min = 0;
138            $max = '' != $max_dig ? $max_dig : 64;
139            break;
140
141        case 'tinyint' :
142        case 'bool' :
143            $len_type = 'num';
144            if ($unsigned) {
145                $min = 0;
146                $max = 255;
147            } else {
148                $min = -128;
149                $max = 127;
150            }
151            break;
152
153        case 'smallint' :
154            $len_type = 'num';
155            if ($unsigned) {
156                $min = 0;
157                $max = 65536;
158            } else {
159                $min = -32768;
160                $max = 32767;
161            }
162            break;
163
164        case 'mediumint' :
165            $len_type = 'num';
166            if ($unsigned) {
167                $min = 0;
168                $max = 16777215;
169            } else {
170                $min = -8388608;
171                $max = 8388607;
172            }
173            break;
174
175        case 'int' :
176        case 'integer' :
177            $len_type = 'num';
178            if ($unsigned) {
179                $min = 0;
180                $max = 4294967295;
181            } else {
182                $min = -2147483648;
183                $max = 2147483647;
184            }
185            break;
186
187        case 'bigint' :
188            $len_type = 'num';
189            if ($unsigned) {
190                $min = 0;
191                $max = 1.84467E+19;
192            } else {
193                $min = -9.22337E+18;
194                $max = 9.22337E+18;
195            }
196            break;
197
198        case 'float' :
199            $len_type = 'num';
200            if ($unsigned) {
201                $min = 0;
202                $max = 3.40282E+38;
203            } else {
204                $min = -3.40282E+38;
205                $max = 3.40282E+38;
206            }
207            break;
208
209        case 'double' :
210        case 'double precision' :
211            $len_type = 'num';
212            if ($unsigned) {
213                $min = 0;
214                $max = 1.7976931348623157E+308;
215            } else {
216                $min = -1.7976931348623157E+308;
217                $max = 1.7976931348623157E+308;
218            }
219            break;
220
221        case 'real' :
222        case 'decimal' :
223        case 'dec' :
224        case 'numeric' :
225        case 'fixed' :
226            $len_type = 'decimal'; // This doesn't match anything, on purpose.
227            break;
228
229        default :
230            $len_type = null;
231            break;
232        }
233        if ($max_length > 0 && $len_type == 'setenum') {
234            $o[] = "\$fv->stringLength('$field', 0, $max_length, sprintf(_(\"%s has an invalid selection.\"), _(\"$title\")));";
235        }
236        if ($max_length > 0 && $len_type == 'string') {
237            $o[] = "\$fv->stringLength('$field', 0, $max_length, sprintf(_(\"%s must be %d-to-%d characters in length.\"), _(\"$title\"), 0, $max_length));";
238        }
239        if ($len_type == 'num') {
240            $o[] = "\$fv->numericRange('$field', $min, $max, sprintf(_(\"%s must be a number between %d and %d.\"), _(\"$title\"), $min, $max));";
241        }
242
243        // ----------- type check ------------
244        switch ($type) {
245
246        case 'enum' :
247
248        case 'set' :
249            break;
250
251        case 'char' :
252        case 'varchar' :
253
254        case 'tinytext' :
255        case 'text' :
256        case 'mediumtext' :
257        case 'longtext' :
258        case 'tinyblob' :
259        case 'blob' :
260        case 'mediumblob' :
261        case 'longblob' :
262//             $o[] = "\$fv->isString('$field', _(\"<strong>$title</strong> must be a string.\"));"; // Pretty useless
263            break;
264
265        case 'tinyint' :
266        case 'bit' :
267        case 'bool' :
268        case 'smallint' :
269        case 'mediumint' :
270        case 'int' :
271        case 'integer' :
272        case 'bigint' :
273            $negative_ok = $unsigned ? '' : ', true';
274            $o[] = "\$fv->isInteger('$field', sprintf(_(\"%s must be an integer.\"), _(\"$title\"))$negative_ok);";
275            break;
276
277        case 'float' :
278        case 'double' :
279            $negative_ok = $unsigned ? '' : ', true';
280            $o[] = "\$fv->isFloat('$field', sprintf(_(\"%s must be a valid number.\"), _(\"$title\"))$negative_ok);";
281            break;
282
283        case 'real' :
284        case 'decimal' :
285        case 'dec' :
286        case 'numeric' :
287        case 'float' :
288            if ($unsigned) {
289                $negative_ok = 'false';
290            } else {
291                $negative_ok = 'true';
292            }
293            $negative_ok = $unsigned ? 'false' : 'true';
294            $o[] = "\$fv->isDecimal('$field', $max_dig, $max_dec, false, sprintf(_(\"%s must be a number with a maximum of %d integer digits and %d fractional digits, e.g., {EX}.\"), _(\"$title\"), {$max_dig}-{$max_dec}, $max_dec), MSG_ERR, __FILE__, __LINE__);";
295            break;
296
297        case 'date' :
298        case 'datetime' :
299            // \$fv->validateStrDate('$field', sprintf(_(\"%s must be a valid date in YYYY-MM-DD format.\"), _(\"$title\")));
300            $o[] = "\$fv->validateStrDate('$field', sprintf(_(\"%s must be a valid date in YYYY-MM-DD format.\"), _(\"$title\")));";
301            $o[] = "\$fv->checkRegex('$field', '/^\d{4}-\d{2}-\d{2}$/', true, sprintf(_(\"%s must be in YYYY-MM-DD format.\"), _(\"$title\")));";
302            break;
303
304        case 'timestamp' :
305            $o[] = "\$fv->checkRegex('$field', '/^\d{14}$/', true, sprintf(_(\"%s must be a valid mysql timestamp in YYYYMMDDhhmmss format.\"), _(\"$title\")));";
306            break;
307
308        case 'time' :
309            $o[] = "\$fv->checkRegex('$field', '/^\d{1,3}:\d{2}:\d{2}$/', true, sprintf(_(\"%s must be a valid time in hh:mm:ss format.\"), _(\"$title\")));";
310            break;
311
312        case 'year' :
313            $o[] = "\$fv->checkRegex('$field', '/^\d{4}$/', true, sprintf(_(\"%s must be a valid year in YYYY format.\"), _(\"$title\")));";
314            break;
315
316        default :
317            break;
318        }
319
320        if (preg_match('/email/', $field)) {
321            $o[] = "\$fv->validateEmail('$field');";
322        } else if (preg_match('/phone/', $field)) {
323            $o[] = "\$fv->validatePhone('$field');";
324        }
325
326        // Blank between cols?
327        $o[] = '';
328    }
329} else {
330    die(sprintf("%s Warning: %s does not have any columns.\n", basename($_SERVER['argv'][0]), $db_tbl));
331}
332
333// Sort?
334// natsort($o);
335
336// Remove last empty element.
337if ('' == end($o)) {
338    array_pop($o);
339}
340
341echo "function validateInput()
342{
343    global \$fv;
344
345    " . join("\n    ", $o) . "
346}";
Note: See TracBrowser for help on using the repository browser.