source: tags/2.1.5/bin/module_maker/validation.cli.php @ 377

Last change on this file since 377 was 377, checked in by quinn, 14 years ago

Releasing trunk as stable version 2.1.5

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