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

Last change on this file since 693 was 655, checked in by anonymous, 5 years ago

Update module_maker. Minor fixes.

File size: 10.5 KB
RevLine 
[612]1#!/usr/bin/env php
[1]2<?php
3/**
[362]4 * The Strangecode Codebase - a general application development framework for PHP
5 * For details visit the project site: <http://trac.strangecode.com/codebase/>
[396]6 * Copyright 2001-2012 Strangecode, LLC
[533]7 *
[362]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.
[533]14 *
[362]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.
[533]19 *
[362]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/**
[1]25 * validation.cli.php
26 */
27
[533]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}
[1]34
[533]35include_once dirname(__FILE__) . '/../_config.inc.php';
36
[1]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.
[136]46$qid = $db->query("SHOW TABLES");
[1]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)) {
[136]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)));
[1]54}
55
56// Get DB table column info.
[136]57$qid = $db->query("DESCRIBE " . $db->escapeString($db_tbl));
[1]58while ($row = mysql_fetch_row($qid)) {
59    $cols[] = $row;
60}
61
62// Exclude these fields.
[19]63$exclude = array('added_by_user_id', 'added_datetime', 'hit_count', 'modified_datetime', 'modified_by_user_id');
[1]64
65// Loop through columns
66if (is_array($cols) && !empty($cols)) {
[42]67
[1]68    $o = array();
69
70    foreach ($cols as $col) {
[42]71
[1]72        // Human readable.
73        $field = $col[0];
74        $title = ucfirst(str_replace('_', ' ', $field));
[534]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]);
[1]78        $is_primary_key = ('PRI' == $col[3]);
[145]79        $unsigned = preg_match('/\s+unsigned\s*$/i', $col[1]);
[42]80
[1]81        if (in_array($field, $exclude)) {
82            continue;
83        }
[42]84
[1]85        // ----------- isEmpty ------------
[145]86        $o[] = "\$fv->notEmpty('$field', sprintf(_(\"%s cannot be blank.\"), _(\"$title\")));";
[1]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;
[42]99
[1]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;
[42]108
[1]109        case 'tinytext' :
110        case 'tinyblob' :
111        case 'char' :
112        case 'varchar' :
113            $len_type = 'string';
[534]114            $max_length = '' != $max_dig ? $max_dig : 255;
[1]115            break;
[42]116
[1]117        case 'text' :
118        case 'blob' :
119            $len_type = 'string';
120            $max_length = 65535;
121            break;
[42]122
[1]123        case 'mediumtext' :
124        case 'mediumblob' :
125            $len_type = 'string';
126            $max_length = 16777215;
127            break;
[42]128
[1]129        case 'longtext' :
130        case 'longblob' :
131            $len_type = 'string';
132            $max_length = 4294967295;
133            break;
[42]134
[534]135        case 'bit' :
136            $len_type = 'num';
137            $min = 0;
138            $max = '' != $max_dig ? $max_dig : 64;
139            break;
140
[1]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;
[42]152
[1]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;
[42]163
[1]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;
[42]174
[1]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;
[42]186
[1]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;
[42]197
[1]198        case 'float' :
199            $len_type = 'num';
[534]200            if ($unsigned) {
201                $min = 0;
202                $max = 3.40282E+38;
203            } else {
204                $min = -3.40282E+38;
205                $max = 3.40282E+38;
206            }
[1]207            break;
[42]208
[1]209        case 'double' :
210        case 'double precision' :
[534]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
[1]221        case 'real' :
222        case 'decimal' :
223        case 'dec' :
224        case 'numeric' :
[534]225        case 'fixed' :
226            $len_type = 'decimal'; // This doesn't match anything, on purpose.
[1]227            break;
[42]228
[1]229        default :
230            $len_type = null;
231            break;
232        }
233        if ($max_length > 0 && $len_type == 'setenum') {
[266]234            $o[] = "\$fv->stringLength('$field', 0, $max_length, sprintf(_(\"%s has an invalid selection.\"), _(\"$title\")));";
[1]235        }
236        if ($max_length > 0 && $len_type == 'string') {
[151]237            $o[] = "\$fv->stringLength('$field', 0, $max_length, sprintf(_(\"%s must be %d-to-%d characters in length.\"), _(\"$title\"), 0, $max_length));";
[1]238        }
239        if ($len_type == 'num') {
[151]240            $o[] = "\$fv->numericRange('$field', $min, $max, sprintf(_(\"%s must be a number between %d and %d.\"), _(\"$title\"), $min, $max));";
[1]241        }
[42]242
[1]243        // ----------- type check ------------
244        switch ($type) {
245
246        case 'enum' :
[42]247
[1]248        case 'set' :
249            break;
[42]250
[1]251        case 'char' :
252        case 'varchar' :
[42]253
[1]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;
[42]264
[1]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';
[145]274            $o[] = "\$fv->isInteger('$field', sprintf(_(\"%s must be an integer.\"), _(\"$title\"))$negative_ok);";
[1]275            break;
[42]276
[1]277        case 'float' :
278        case 'double' :
[534]279            $negative_ok = $unsigned ? '' : ', true';
280            $o[] = "\$fv->isFloat('$field', sprintf(_(\"%s must be a valid number.\"), _(\"$title\"))$negative_ok);";
281            break;
282
[1]283        case 'real' :
284        case 'decimal' :
285        case 'dec' :
286        case 'numeric' :
[534]287        case 'float' :
288            if ($unsigned) {
289                $negative_ok = 'false';
290            } else {
291                $negative_ok = 'true';
292            }
293            $negative_ok = $unsigned ? 'false' : 'true';
[655]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__);";
[1]295            break;
[42]296
[1]297        case 'date' :
298        case 'datetime' :
[266]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\")));";
[1]302            break;
[42]303
[1]304        case 'timestamp' :
[266]305            $o[] = "\$fv->checkRegex('$field', '/^\d{14}$/', true, sprintf(_(\"%s must be a valid mysql timestamp in YYYYMMDDhhmmss format.\"), _(\"$title\")));";
[1]306            break;
[42]307
[1]308        case 'time' :
[266]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\")));";
[1]310            break;
[42]311
[1]312        case 'year' :
[266]313            $o[] = "\$fv->checkRegex('$field', '/^\d{4}$/', true, sprintf(_(\"%s must be a valid year in YYYY format.\"), _(\"$title\")));";
[1]314            break;
[42]315
[1]316        default :
317            break;
318        }
[42]319
[1]320        if (preg_match('/email/', $field)) {
321            $o[] = "\$fv->validateEmail('$field');";
322        } else if (preg_match('/phone/', $field)) {
323            $o[] = "\$fv->validatePhone('$field');";
324        }
[42]325
[1]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
[136]336// Remove last empty element.
337if ('' == end($o)) {
338    array_pop($o);
339}
340
[1]341echo "function validateInput()
[42]342{
[1]343    global \$fv;
344
[136]345    " . join("\n    ", $o) . "
[1]346}";
Note: See TracBrowser for help on using the repository browser.