source: tags/1.0.0/bin/module_maker/validation.cli.php @ 1

Last change on this file since 1 was 1, checked in by scdev, 19 years ago

Initial import.

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