#!/usr/local/bin/php -q
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->isEmpty('$field', _(\"$title cannot be blank.\"));";
// ----------- 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, _(\"$title has an invalid selection.\"));";
}
if ($max_length > 0 && $len_type == 'string') {
$o[] = "\$fv->stringLength('$field', 0, $max_length, _(\"$title must contain less than " . ($max_length+1) . " characters.\"));";
}
if ($len_type == 'num') {
$o[] = "\$fv->numericRange('$field', $min, $max, _(\"$title must be a valid number between $min and $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', _(\"$title must be an integer.\")$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', _(\"$title must be a valid number.\")$negative_ok);";
break;
case 'date' :
case 'datetime' :
$o[] = "\$fv->validateStrDate('$field', _(\"$title must be a valid date in YYYY-MM-DD format.\"));";
$o[] = "\$fv->checkRegex('$field', '/^\d{4}-\d{2}-\d{2}$/', true, _(\"$title must be a valid date in YYYY-MM-DD format.\"));";
break;
case 'timestamp' :
$o[] = "\$fv->checkRegex('$field', '/^\d{14}$/', true, _(\"$title must be a valid mysql timestamp in YYYYMMDDhhmmss format.\"));";
break;
case 'time' :
$o[] = "\$fv->checkRegex('$field', '/^\d{1,3}:\d{2}:\d{2}$/', true, _(\"$title must be a valid time in hh:mm:ss format.\"));";
break;
case 'year' :
$o[] = "\$fv->checkRegex('$field', '/^\d{4}$/', true, _(\"$title must be a valid year in YYYY format.\"));";
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);
echo "function validateInput()
{
global \$fv;
";
echo join("\n ", $o);
echo "
}";
?>