[648] | 1 | #!/usr/bin/env php |
---|
[1] | 2 | <?php |
---|
| 3 | /** |
---|
| 4 | * genrate_validation.cli.php |
---|
| 5 | * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information |
---|
| 6 | */ |
---|
| 7 | |
---|
| 8 | |
---|
[79] | 9 | // Get module maker configuration. |
---|
| 10 | require_once dirname(__FILE__) . '/_config.inc.php'; |
---|
[1] | 11 | |
---|
| 12 | // Test arguments. |
---|
[79] | 13 | if ($_SERVER['argc'] == 3) { |
---|
| 14 | $db_tbl = $_SERVER['argv'][2]; |
---|
[1] | 15 | } else { |
---|
[79] | 16 | die(basename($_SERVER['argv'][0]) . " Warning: You must specify a DB table as the second argument.\n"); |
---|
[1] | 17 | } |
---|
| 18 | |
---|
| 19 | // Get DB tables. |
---|
| 20 | $qid = dbQuery("SHOW TABLES"); |
---|
| 21 | while (list($row) = mysql_fetch_row($qid)) { |
---|
| 22 | $tables[] = $row; |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | // Make sure requested table is in database. |
---|
| 26 | if (!in_array($db_tbl, $tables)) { |
---|
| 27 | die(basename($_SERVER['argv'][0]) . " Warning: $db_tbl does not exist in database $CFG->database. Please select one of: \n\n" . join("\n", $tables) . "\n\n"); |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | // Get DB table column info. |
---|
[90] | 31 | $qid = dbQuery("DESCRIBE " . mysql_real_escape_string($db_tbl)); |
---|
[1] | 32 | while ($row = mysql_fetch_row($qid)) { |
---|
| 33 | $cols[] = $row; |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | // Exclude these fields. |
---|
| 37 | $exclude = array('added_by_admin_id', 'added_datetime', 'hit_count', 'modified_datetime', 'modified_by_admin_id'); |
---|
| 38 | |
---|
| 39 | // Loop through columns |
---|
| 40 | if (is_array($cols) && !empty($cols)) { |
---|
| 41 | |
---|
| 42 | $o = array(); |
---|
| 43 | |
---|
| 44 | foreach ($cols as $col) { |
---|
| 45 | |
---|
| 46 | // Human readable. |
---|
| 47 | $field = $col[0]; |
---|
| 48 | $title = ucfirst(str_replace('_', ' ', $field)); |
---|
| 49 | $type = preg_replace('/^(\w+).*$/', '\\1', $col[1]); |
---|
| 50 | $is_primary_key = ('PRI' == $col[3]); |
---|
| 51 | $unsigned = preg_match('/\s*unsigned\s*$/i', $col[1]); |
---|
| 52 | |
---|
| 53 | if (in_array($field, $exclude)) { |
---|
| 54 | continue; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | // ----------- isEmpty ------------ |
---|
| 58 | $o[] = "\$fv->isEmpty('$field', _(\"<strong>$title</strong> cannot be blank.\"));"; |
---|
| 59 | |
---|
| 60 | // ----------- stringLength ------------ |
---|
| 61 | $max_length = null; |
---|
| 62 | $min = null; |
---|
| 63 | $max = null; |
---|
| 64 | $len_type = null; |
---|
| 65 | switch ($type) { |
---|
| 66 | case 'enum' : |
---|
| 67 | case 'set' : |
---|
| 68 | $max_length = 255; |
---|
| 69 | $len_type = 'setenum'; |
---|
| 70 | break; |
---|
| 71 | |
---|
| 72 | case 'date' : |
---|
| 73 | case 'datetime' : |
---|
| 74 | case 'timestamp' : |
---|
| 75 | case 'time' : |
---|
| 76 | case 'year' : |
---|
| 77 | $len_type = 'string'; |
---|
| 78 | $max_length = 127; |
---|
| 79 | break; |
---|
| 80 | |
---|
| 81 | case 'tinytext' : |
---|
| 82 | case 'tinyblob' : |
---|
| 83 | case 'char' : |
---|
| 84 | case 'varchar' : |
---|
| 85 | $len_type = 'string'; |
---|
| 86 | $max_length = 255; |
---|
| 87 | break; |
---|
| 88 | |
---|
| 89 | case 'text' : |
---|
| 90 | case 'blob' : |
---|
| 91 | $len_type = 'string'; |
---|
| 92 | $max_length = 65535; |
---|
| 93 | break; |
---|
| 94 | |
---|
| 95 | case 'mediumtext' : |
---|
| 96 | case 'mediumblob' : |
---|
| 97 | $len_type = 'string'; |
---|
| 98 | $max_length = 16777215; |
---|
| 99 | break; |
---|
| 100 | |
---|
| 101 | case 'longtext' : |
---|
| 102 | case 'longblob' : |
---|
| 103 | $len_type = 'string'; |
---|
| 104 | $max_length = 4294967295; |
---|
| 105 | break; |
---|
| 106 | |
---|
| 107 | case 'tinyint' : |
---|
| 108 | case 'bit' : |
---|
| 109 | case 'bool' : |
---|
| 110 | $len_type = 'num'; |
---|
| 111 | if ($unsigned) { |
---|
| 112 | $min = 0; |
---|
| 113 | $max = 255; |
---|
| 114 | } else { |
---|
| 115 | $min = -128; |
---|
| 116 | $max = 127; |
---|
| 117 | } |
---|
| 118 | break; |
---|
| 119 | |
---|
| 120 | case 'smallint' : |
---|
| 121 | $len_type = 'num'; |
---|
| 122 | if ($unsigned) { |
---|
| 123 | $min = 0; |
---|
| 124 | $max = 65536; |
---|
| 125 | } else { |
---|
| 126 | $min = -32768; |
---|
| 127 | $max = 32767; |
---|
| 128 | } |
---|
| 129 | break; |
---|
| 130 | |
---|
| 131 | case 'mediumint' : |
---|
| 132 | $len_type = 'num'; |
---|
| 133 | if ($unsigned) { |
---|
| 134 | $min = 0; |
---|
| 135 | $max = 16777215; |
---|
| 136 | } else { |
---|
| 137 | $min = -8388608; |
---|
| 138 | $max = 8388607; |
---|
| 139 | } |
---|
| 140 | break; |
---|
| 141 | |
---|
| 142 | case 'int' : |
---|
| 143 | case 'integer' : |
---|
| 144 | $len_type = 'num'; |
---|
| 145 | if ($unsigned) { |
---|
| 146 | $min = 0; |
---|
| 147 | $max = 4294967295; |
---|
| 148 | } else { |
---|
| 149 | $min = -2147483648; |
---|
| 150 | $max = 2147483647; |
---|
| 151 | } |
---|
| 152 | break; |
---|
| 153 | |
---|
| 154 | case 'bigint' : |
---|
| 155 | $len_type = 'num'; |
---|
| 156 | if ($unsigned) { |
---|
| 157 | $min = 0; |
---|
| 158 | $max = 1.84467E+19; |
---|
| 159 | } else { |
---|
| 160 | $min = -9.22337E+18; |
---|
| 161 | $max = 9.22337E+18; |
---|
| 162 | } |
---|
| 163 | break; |
---|
| 164 | |
---|
| 165 | case 'float' : |
---|
| 166 | $len_type = 'num'; |
---|
| 167 | $min = -3.40282E+38; |
---|
| 168 | $max = 3.40282E+38; |
---|
| 169 | break; |
---|
| 170 | |
---|
| 171 | case 'double' : |
---|
| 172 | case 'double precision' : |
---|
| 173 | case 'real' : |
---|
| 174 | case 'decimal' : |
---|
| 175 | case 'dec' : |
---|
| 176 | case 'numeric' : |
---|
| 177 | $len_type = 'num'; |
---|
| 178 | $min = -1.79769E+308; |
---|
| 179 | $max = 1.79769E+308; |
---|
| 180 | break; |
---|
| 181 | |
---|
| 182 | default : |
---|
| 183 | $len_type = null; |
---|
| 184 | break; |
---|
| 185 | } |
---|
| 186 | if ($max_length > 0 && $len_type == 'setenum') { |
---|
| 187 | $o[] = "\$fv->stringLength('$field', 0, $max_length, _(\"<strong>$title</strong> has an invalid selection.\"));"; |
---|
| 188 | } |
---|
| 189 | if ($max_length > 0 && $len_type == 'string') { |
---|
| 190 | $o[] = "\$fv->stringLength('$field', 0, $max_length, _(\"<strong>$title</strong> must contain less than " . ($max_length+1) . " characters.\"));"; |
---|
| 191 | } |
---|
| 192 | if ($len_type == 'num') { |
---|
| 193 | $o[] = "\$fv->numericRange('$field', $min, $max, _(\"<strong>$title</strong> must be a valid number between $min and $max.\"));"; |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | // ----------- type check ------------ |
---|
| 197 | switch ($type) { |
---|
| 198 | |
---|
| 199 | case 'enum' : |
---|
| 200 | |
---|
| 201 | case 'set' : |
---|
| 202 | break; |
---|
| 203 | |
---|
| 204 | case 'char' : |
---|
| 205 | case 'varchar' : |
---|
| 206 | |
---|
| 207 | case 'tinytext' : |
---|
| 208 | case 'text' : |
---|
| 209 | case 'mediumtext' : |
---|
| 210 | case 'longtext' : |
---|
| 211 | case 'tinyblob' : |
---|
| 212 | case 'blob' : |
---|
| 213 | case 'mediumblob' : |
---|
| 214 | case 'longblob' : |
---|
| 215 | // $o[] = "\$fv->isString('$field', _(\"<strong>$title</strong> must be a string.\"));"; // Pretty useless |
---|
| 216 | break; |
---|
| 217 | |
---|
| 218 | case 'tinyint' : |
---|
| 219 | case 'bit' : |
---|
| 220 | case 'bool' : |
---|
| 221 | case 'smallint' : |
---|
| 222 | case 'mediumint' : |
---|
| 223 | case 'int' : |
---|
| 224 | case 'integer' : |
---|
| 225 | case 'bigint' : |
---|
| 226 | $negative_ok = $unsigned ? '' : ', true'; |
---|
| 227 | $o[] = "\$fv->isInteger('$field', _(\"<strong>$title</strong> must be an integer.\")$negative_ok);"; |
---|
| 228 | break; |
---|
| 229 | |
---|
| 230 | case 'float' : |
---|
| 231 | case 'float' : |
---|
| 232 | case 'double' : |
---|
| 233 | case 'double' : |
---|
| 234 | case 'real' : |
---|
| 235 | case 'decimal' : |
---|
| 236 | case 'dec' : |
---|
| 237 | case 'numeric' : |
---|
| 238 | $negative_ok = $unsigned ? '' : ', true'; |
---|
| 239 | $o[] = "\$fv->isFloat('$field', _(\"<strong>$title</strong> must be a valid number.\")$negative_ok);"; |
---|
| 240 | break; |
---|
| 241 | |
---|
| 242 | case 'date' : |
---|
| 243 | case 'datetime' : |
---|
| 244 | $o[] = "\$fv->validateStrDate('$field', _(\"<strong>$title</strong> must be a valid date in YYYY-MM-DD format.\"));"; |
---|
| 245 | $o[] = "\$fv->checkRegex('$field', '/^\d{4}-\d{2}-\d{2}$/', true, _(\"<strong>$title</strong> must be a valid date in YYYY-MM-DD format.\"));"; |
---|
| 246 | break; |
---|
| 247 | |
---|
| 248 | case 'timestamp' : |
---|
| 249 | $o[] = "\$fv->checkRegex('$field', '/^\d{14}$/', true, _(\"<strong>$title</strong> must be a valid mysql timestamp in YYYYMMDDhhmmss format.\"));"; |
---|
| 250 | break; |
---|
| 251 | |
---|
| 252 | case 'time' : |
---|
| 253 | $o[] = "\$fv->checkRegex('$field', '/^\d{1,3}:\d{2}:\d{2}$/', true, _(\"<strong>$title</strong> must be a valid time in hh:mm:ss format.\"));"; |
---|
| 254 | break; |
---|
| 255 | |
---|
| 256 | case 'year' : |
---|
| 257 | $o[] = "\$fv->checkRegex('$field', '/^\d{4}$/', true, _(\"<strong>$title</strong> must be a valid year in YYYY format.\"));"; |
---|
| 258 | break; |
---|
| 259 | |
---|
| 260 | default : |
---|
| 261 | break; |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | if (preg_match('/email/', $field)) { |
---|
| 265 | $o[] = "\$fv->validateEmail('$field');"; |
---|
| 266 | } else if (preg_match('/phone/', $field)) { |
---|
| 267 | $o[] = "\$fv->validatePhone('$field');"; |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | // Blank between cols? |
---|
| 271 | $o[] = ''; |
---|
| 272 | } |
---|
| 273 | } else { |
---|
| 274 | die(basename($_SERVER['argv'][0]) . " Warning: $db_tbl does not have any columns.\n"); |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | // Sort? |
---|
| 278 | // natsort($o); |
---|
| 279 | |
---|
| 280 | echo "function validateInput() |
---|
| 281 | { |
---|
| 282 | global \$fv; |
---|
| 283 | |
---|
| 284 | "; |
---|
| 285 | echo join("\n ", $o); |
---|
| 286 | echo " |
---|
| 287 | }"; |
---|
| 288 | ?> |
---|