[1] | 1 | #!/usr/local/bin/php -q |
---|
| 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/> |
---|
[376] | 6 | * Copyright 2001-2010 Strangecode, LLC |
---|
[362] | 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 | /** |
---|
[1] | 25 | * validation.cli.php |
---|
| 26 | */ |
---|
| 27 | |
---|
| 28 | include_once dirname(__FILE__) . '/_config.inc.php'; |
---|
| 29 | |
---|
| 30 | // Test arguments. |
---|
| 31 | if (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. |
---|
[136] | 39 | $qid = $db->query("SHOW TABLES"); |
---|
[1] | 40 | while (list($row) = mysql_fetch_row($qid)) { |
---|
| 41 | $tables[] = $row; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | // Make sure requested table is in database. |
---|
| 45 | if (!in_array($db_tbl, $tables)) { |
---|
[136] | 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))); |
---|
[1] | 47 | } |
---|
| 48 | |
---|
| 49 | // Get DB table column info. |
---|
[136] | 50 | $qid = $db->query("DESCRIBE " . $db->escapeString($db_tbl)); |
---|
[1] | 51 | while ($row = mysql_fetch_row($qid)) { |
---|
| 52 | $cols[] = $row; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | // Exclude these fields. |
---|
[19] | 56 | $exclude = array('added_by_user_id', 'added_datetime', 'hit_count', 'modified_datetime', 'modified_by_user_id'); |
---|
[1] | 57 | |
---|
| 58 | // Loop through columns |
---|
| 59 | if (is_array($cols) && !empty($cols)) { |
---|
[42] | 60 | |
---|
[1] | 61 | $o = array(); |
---|
| 62 | |
---|
| 63 | foreach ($cols as $col) { |
---|
[42] | 64 | |
---|
[1] | 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]); |
---|
[145] | 70 | $unsigned = preg_match('/\s+unsigned\s*$/i', $col[1]); |
---|
[42] | 71 | |
---|
[1] | 72 | if (in_array($field, $exclude)) { |
---|
| 73 | continue; |
---|
| 74 | } |
---|
[42] | 75 | |
---|
[1] | 76 | // ----------- isEmpty ------------ |
---|
[145] | 77 | $o[] = "\$fv->notEmpty('$field', sprintf(_(\"%s cannot be blank.\"), _(\"$title\")));"; |
---|
[1] | 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; |
---|
[42] | 90 | |
---|
[1] | 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; |
---|
[42] | 99 | |
---|
[1] | 100 | case 'tinytext' : |
---|
| 101 | case 'tinyblob' : |
---|
| 102 | case 'char' : |
---|
| 103 | case 'varchar' : |
---|
| 104 | $len_type = 'string'; |
---|
| 105 | $max_length = 255; |
---|
| 106 | break; |
---|
[42] | 107 | |
---|
[1] | 108 | case 'text' : |
---|
| 109 | case 'blob' : |
---|
| 110 | $len_type = 'string'; |
---|
| 111 | $max_length = 65535; |
---|
| 112 | break; |
---|
[42] | 113 | |
---|
[1] | 114 | case 'mediumtext' : |
---|
| 115 | case 'mediumblob' : |
---|
| 116 | $len_type = 'string'; |
---|
| 117 | $max_length = 16777215; |
---|
| 118 | break; |
---|
[42] | 119 | |
---|
[1] | 120 | case 'longtext' : |
---|
| 121 | case 'longblob' : |
---|
| 122 | $len_type = 'string'; |
---|
| 123 | $max_length = 4294967295; |
---|
| 124 | break; |
---|
[42] | 125 | |
---|
[1] | 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; |
---|
[42] | 138 | |
---|
[1] | 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; |
---|
[42] | 149 | |
---|
[1] | 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; |
---|
[42] | 160 | |
---|
[1] | 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; |
---|
[42] | 172 | |
---|
[1] | 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; |
---|
[42] | 183 | |
---|
[1] | 184 | case 'float' : |
---|
| 185 | $len_type = 'num'; |
---|
| 186 | $min = -3.40282E+38; |
---|
| 187 | $max = 3.40282E+38; |
---|
| 188 | break; |
---|
[42] | 189 | |
---|
[1] | 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; |
---|
[42] | 200 | |
---|
[1] | 201 | default : |
---|
| 202 | $len_type = null; |
---|
| 203 | break; |
---|
| 204 | } |
---|
| 205 | if ($max_length > 0 && $len_type == 'setenum') { |
---|
[266] | 206 | $o[] = "\$fv->stringLength('$field', 0, $max_length, sprintf(_(\"%s has an invalid selection.\"), _(\"$title\")));"; |
---|
[1] | 207 | } |
---|
| 208 | if ($max_length > 0 && $len_type == 'string') { |
---|
[151] | 209 | $o[] = "\$fv->stringLength('$field', 0, $max_length, sprintf(_(\"%s must be %d-to-%d characters in length.\"), _(\"$title\"), 0, $max_length));"; |
---|
[1] | 210 | } |
---|
| 211 | if ($len_type == 'num') { |
---|
[151] | 212 | $o[] = "\$fv->numericRange('$field', $min, $max, sprintf(_(\"%s must be a number between %d and %d.\"), _(\"$title\"), $min, $max));"; |
---|
[1] | 213 | } |
---|
[42] | 214 | |
---|
[1] | 215 | // ----------- type check ------------ |
---|
| 216 | switch ($type) { |
---|
| 217 | |
---|
| 218 | case 'enum' : |
---|
[42] | 219 | |
---|
[1] | 220 | case 'set' : |
---|
| 221 | break; |
---|
[42] | 222 | |
---|
[1] | 223 | case 'char' : |
---|
| 224 | case 'varchar' : |
---|
[42] | 225 | |
---|
[1] | 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; |
---|
[42] | 236 | |
---|
[1] | 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'; |
---|
[145] | 246 | $o[] = "\$fv->isInteger('$field', sprintf(_(\"%s must be an integer.\"), _(\"$title\"))$negative_ok);"; |
---|
[1] | 247 | break; |
---|
[42] | 248 | |
---|
[1] | 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'; |
---|
[145] | 258 | $o[] = "\$fv->isFloat('$field', sprintf(_(\"%s must be a valid number.\"), _(\"$title\"))$negative_ok);"; |
---|
[1] | 259 | break; |
---|
[42] | 260 | |
---|
[1] | 261 | case 'date' : |
---|
| 262 | case 'datetime' : |
---|
[266] | 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\")));"; |
---|
[1] | 266 | break; |
---|
[42] | 267 | |
---|
[1] | 268 | case 'timestamp' : |
---|
[266] | 269 | $o[] = "\$fv->checkRegex('$field', '/^\d{14}$/', true, sprintf(_(\"%s must be a valid mysql timestamp in YYYYMMDDhhmmss format.\"), _(\"$title\")));"; |
---|
[1] | 270 | break; |
---|
[42] | 271 | |
---|
[1] | 272 | case 'time' : |
---|
[266] | 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\")));"; |
---|
[1] | 274 | break; |
---|
[42] | 275 | |
---|
[1] | 276 | case 'year' : |
---|
[266] | 277 | $o[] = "\$fv->checkRegex('$field', '/^\d{4}$/', true, sprintf(_(\"%s must be a valid year in YYYY format.\"), _(\"$title\")));"; |
---|
[1] | 278 | break; |
---|
[42] | 279 | |
---|
[1] | 280 | default : |
---|
| 281 | break; |
---|
| 282 | } |
---|
[42] | 283 | |
---|
[1] | 284 | if (preg_match('/email/', $field)) { |
---|
| 285 | $o[] = "\$fv->validateEmail('$field');"; |
---|
| 286 | } else if (preg_match('/phone/', $field)) { |
---|
| 287 | $o[] = "\$fv->validatePhone('$field');"; |
---|
| 288 | } |
---|
[42] | 289 | |
---|
[1] | 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 | |
---|
[136] | 300 | // Remove last empty element. |
---|
| 301 | if ('' == end($o)) { |
---|
| 302 | array_pop($o); |
---|
| 303 | } |
---|
| 304 | |
---|
[1] | 305 | echo "function validateInput() |
---|
[42] | 306 | { |
---|
[1] | 307 | global \$fv; |
---|
| 308 | |
---|
[136] | 309 | " . join("\n ", $o) . " |
---|
[1] | 310 | }"; |
---|
| 311 | ?> |
---|