[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] | 28 | if ($_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] | 35 | include_once dirname(__FILE__) . '/../_config.inc.php'; |
---|
| 36 | |
---|
[1] | 37 | // Test arguments. |
---|
| 38 | if (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] | 47 | while (list($row) = mysql_fetch_row($qid)) { |
---|
| 48 | $tables[] = $row; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | // Make sure requested table is in database. |
---|
| 52 | if (!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] | 58 | while ($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 |
---|
| 66 | if (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 | $max = str_repeat('9', $max_dig - $max_dec); |
---|
| 289 | if ($max_dec > 0) { |
---|
| 290 | $max .= '.' . str_repeat('9', $max_dec); |
---|
| 291 | } |
---|
| 292 | if ($unsigned) { |
---|
| 293 | $negative_ok = 'false'; |
---|
| 294 | $min = 0; |
---|
| 295 | } else { |
---|
| 296 | $negative_ok = 'true'; |
---|
| 297 | $min = -$max; |
---|
| 298 | } |
---|
| 299 | $negative_ok = $unsigned ? 'false' : 'true'; |
---|
| 300 | $o[] = "\$fv->isDecimal('$field', sprintf(_(\"%s must be a number between %d and %d.\"), _(\"$title\"), $min, $max), $negative_ok, $max_dig, $max_dec);"; |
---|
[1] | 301 | break; |
---|
[42] | 302 | |
---|
[1] | 303 | case 'date' : |
---|
| 304 | case 'datetime' : |
---|
[266] | 305 | // \$fv->validateStrDate('$field', sprintf(_(\"%s must be a valid date in YYYY-MM-DD format.\"), _(\"$title\"))); |
---|
| 306 | $o[] = "\$fv->validateStrDate('$field', sprintf(_(\"%s must be a valid date in YYYY-MM-DD format.\"), _(\"$title\")));"; |
---|
| 307 | $o[] = "\$fv->checkRegex('$field', '/^\d{4}-\d{2}-\d{2}$/', true, sprintf(_(\"%s must be in YYYY-MM-DD format.\"), _(\"$title\")));"; |
---|
[1] | 308 | break; |
---|
[42] | 309 | |
---|
[1] | 310 | case 'timestamp' : |
---|
[266] | 311 | $o[] = "\$fv->checkRegex('$field', '/^\d{14}$/', true, sprintf(_(\"%s must be a valid mysql timestamp in YYYYMMDDhhmmss format.\"), _(\"$title\")));"; |
---|
[1] | 312 | break; |
---|
[42] | 313 | |
---|
[1] | 314 | case 'time' : |
---|
[266] | 315 | $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] | 316 | break; |
---|
[42] | 317 | |
---|
[1] | 318 | case 'year' : |
---|
[266] | 319 | $o[] = "\$fv->checkRegex('$field', '/^\d{4}$/', true, sprintf(_(\"%s must be a valid year in YYYY format.\"), _(\"$title\")));"; |
---|
[1] | 320 | break; |
---|
[42] | 321 | |
---|
[1] | 322 | default : |
---|
| 323 | break; |
---|
| 324 | } |
---|
[42] | 325 | |
---|
[1] | 326 | if (preg_match('/email/', $field)) { |
---|
| 327 | $o[] = "\$fv->validateEmail('$field');"; |
---|
| 328 | } else if (preg_match('/phone/', $field)) { |
---|
| 329 | $o[] = "\$fv->validatePhone('$field');"; |
---|
| 330 | } |
---|
[42] | 331 | |
---|
[1] | 332 | // Blank between cols? |
---|
| 333 | $o[] = ''; |
---|
| 334 | } |
---|
| 335 | } else { |
---|
| 336 | die(sprintf("%s Warning: %s does not have any columns.\n", basename($_SERVER['argv'][0]), $db_tbl)); |
---|
| 337 | } |
---|
| 338 | |
---|
| 339 | // Sort? |
---|
| 340 | // natsort($o); |
---|
| 341 | |
---|
[136] | 342 | // Remove last empty element. |
---|
| 343 | if ('' == end($o)) { |
---|
| 344 | array_pop($o); |
---|
| 345 | } |
---|
| 346 | |
---|
[1] | 347 | echo "function validateInput() |
---|
[42] | 348 | { |
---|
[1] | 349 | global \$fv; |
---|
| 350 | |
---|
[136] | 351 | " . join("\n ", $o) . " |
---|
[1] | 352 | }"; |
---|