#!/usr/local/bin/php -q * Copyright 2001-2010 Strangecode, LLC * * This file is part of The Strangecode Codebase. * * The Strangecode Codebase is free software: you can redistribute it and/or * modify it under the terms of the GNU General Public License as published by the * Free Software Foundation, either version 3 of the License, or (at your option) * any later version. * * The Strangecode Codebase is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * The Strangecode Codebase. If not, see . */ /** * validation.cli.php */ include_once dirname(__FILE__) . '/_config.inc.php'; // Test arguments. if (isset($_SERVER['argv'][2])) { // Second arg is db table. $db_tbl = $_SERVER['argv'][2]; } else { die(sprintf("Usage: %s site_directory db_table\n", basename($_SERVER['argv'][0]))); } // Get DB tables. $qid = $db->query("SHOW TABLES"); while (list($row) = mysql_fetch_row($qid)) { $tables[] = $row; } // Make sure requested table is in database. if (!in_array($db_tbl, $tables)) { 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))); } // Get DB table column info. $qid = $db->query("DESCRIBE " . $db->escapeString($db_tbl)); while ($row = mysql_fetch_row($qid)) { $cols[] = $row; } // Exclude these fields. $exclude = array('added_by_user_id', 'added_datetime', 'hit_count', 'modified_datetime', 'modified_by_user_id'); // Loop through columns if (is_array($cols) && !empty($cols)) { $o = array(); foreach ($cols as $col) { // Human readable. $field = $col[0]; $title = ucfirst(str_replace('_', ' ', $field)); $type = preg_replace('/^(\w+).*$/', '\\1', $col[1]); $is_primary_key = ('PRI' == $col[3]); $unsigned = preg_match('/\s+unsigned\s*$/i', $col[1]); if (in_array($field, $exclude)) { continue; } // ----------- isEmpty ------------ $o[] = "\$fv->notEmpty('$field', sprintf(_(\"%s cannot be blank.\"), _(\"$title\")));"; // ----------- stringLength ------------ $max_length = null; $min = null; $max = null; $len_type = null; switch ($type) { case 'enum' : case 'set' : $max_length = 255; $len_type = 'setenum'; break; case 'date' : case 'datetime' : case 'timestamp' : case 'time' : case 'year' : $len_type = 'string'; $max_length = 127; break; case 'tinytext' : case 'tinyblob' : case 'char' : case 'varchar' : $len_type = 'string'; $max_length = 255; break; case 'text' : case 'blob' : $len_type = 'string'; $max_length = 65535; break; case 'mediumtext' : case 'mediumblob' : $len_type = 'string'; $max_length = 16777215; break; case 'longtext' : case 'longblob' : $len_type = 'string'; $max_length = 4294967295; break; case 'tinyint' : case 'bit' : case 'bool' : $len_type = 'num'; if ($unsigned) { $min = 0; $max = 255; } else { $min = -128; $max = 127; } break; case 'smallint' : $len_type = 'num'; if ($unsigned) { $min = 0; $max = 65536; } else { $min = -32768; $max = 32767; } break; case 'mediumint' : $len_type = 'num'; if ($unsigned) { $min = 0; $max = 16777215; } else { $min = -8388608; $max = 8388607; } break; case 'int' : case 'integer' : $len_type = 'num'; if ($unsigned) { $min = 0; $max = 4294967295; } else { $min = -2147483648; $max = 2147483647; } break; case 'bigint' : $len_type = 'num'; if ($unsigned) { $min = 0; $max = 1.84467E+19; } else { $min = -9.22337E+18; $max = 9.22337E+18; } break; case 'float' : $len_type = 'num'; $min = -3.40282E+38; $max = 3.40282E+38; break; case 'double' : case 'double precision' : case 'real' : case 'decimal' : case 'dec' : case 'numeric' : $len_type = 'num'; $min = -1.79769E+308; $max = 1.79769E+308; break; default : $len_type = null; break; } if ($max_length > 0 && $len_type == 'setenum') { $o[] = "\$fv->stringLength('$field', 0, $max_length, sprintf(_(\"%s has an invalid selection.\"), _(\"$title\")));"; } if ($max_length > 0 && $len_type == 'string') { $o[] = "\$fv->stringLength('$field', 0, $max_length, sprintf(_(\"%s must be %d-to-%d characters in length.\"), _(\"$title\"), 0, $max_length));"; } if ($len_type == 'num') { $o[] = "\$fv->numericRange('$field', $min, $max, sprintf(_(\"%s must be a number between %d and %d.\"), _(\"$title\"), $min, $max));"; } // ----------- type check ------------ switch ($type) { case 'enum' : case 'set' : break; case 'char' : case 'varchar' : case 'tinytext' : case 'text' : case 'mediumtext' : case 'longtext' : case 'tinyblob' : case 'blob' : case 'mediumblob' : case 'longblob' : // $o[] = "\$fv->isString('$field', _(\"$title must be a string.\"));"; // Pretty useless break; case 'tinyint' : case 'bit' : case 'bool' : case 'smallint' : case 'mediumint' : case 'int' : case 'integer' : case 'bigint' : $negative_ok = $unsigned ? '' : ', true'; $o[] = "\$fv->isInteger('$field', sprintf(_(\"%s must be an integer.\"), _(\"$title\"))$negative_ok);"; break; case 'float' : case 'float' : case 'double' : case 'double' : case 'real' : case 'decimal' : case 'dec' : case 'numeric' : $negative_ok = $unsigned ? '' : ', true'; $o[] = "\$fv->isFloat('$field', sprintf(_(\"%s must be a valid number.\"), _(\"$title\"))$negative_ok);"; break; case 'date' : case 'datetime' : // \$fv->validateStrDate('$field', sprintf(_(\"%s must be a valid date in YYYY-MM-DD format.\"), _(\"$title\"))); $o[] = "\$fv->validateStrDate('$field', sprintf(_(\"%s must be a valid date in YYYY-MM-DD format.\"), _(\"$title\")));"; $o[] = "\$fv->checkRegex('$field', '/^\d{4}-\d{2}-\d{2}$/', true, sprintf(_(\"%s must be in YYYY-MM-DD format.\"), _(\"$title\")));"; break; case 'timestamp' : $o[] = "\$fv->checkRegex('$field', '/^\d{14}$/', true, sprintf(_(\"%s must be a valid mysql timestamp in YYYYMMDDhhmmss format.\"), _(\"$title\")));"; break; case 'time' : $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\")));"; break; case 'year' : $o[] = "\$fv->checkRegex('$field', '/^\d{4}$/', true, sprintf(_(\"%s must be a valid year in YYYY format.\"), _(\"$title\")));"; break; default : break; } if (preg_match('/email/', $field)) { $o[] = "\$fv->validateEmail('$field');"; } else if (preg_match('/phone/', $field)) { $o[] = "\$fv->validatePhone('$field');"; } // Blank between cols? $o[] = ''; } } else { die(sprintf("%s Warning: %s does not have any columns.\n", basename($_SERVER['argv'][0]), $db_tbl)); } // Sort? // natsort($o); // Remove last empty element. if ('' == end($o)) { array_pop($o); } echo "function validateInput() { global \$fv; " . join("\n ", $o) . " }"; ?>