function validate_input($valid, &$p, &$error) { $error = null; if ($valid['type'] != 'func') { if (is_array($p)) { $val =& $p[$valid['_input']]; } else { $val =& $p; } } switch ($valid['type']) { case 'address': $val = string_check($val); if (empty($valid['blank']) and strlen($val) < 4) { $error = !empty($valid['msg']) ? $valid['msg'] : 'You must enter a valid address.'; } if (!empty($valid['lines']) and !empty($val) and substr_count($val, "\n") < $valid['lines'] - 1) { $error = 'This address must contain at least ' . $valid['lines'] . ' lines.'; } if (!empty($valid['format'])) { $val = str_replace(array("\r", "\n", "\r\n", ', '), ',', $val); } break; case 'array': case 'choice': case 'select': // $val can't be an array at this point as that's sorted higher up by validate_input_array() if (!is_array($valid['options']) and function_exists($valid['options'])) { $valid['options'] = $valid['options'](); } if (is_array($valid['options'])) { if (is_assoc($valid['options'])) { $err = !@isset($valid['options'][$val]); } else { $err = !in_array($val, $valid['options']); } } elseif (isset($valid['no-opts'])) { $val = ''; } else { $err = true; $valid['msg'] = 'The options could not be found for this field.'; } if (isset($valid['not-empty']) and empty($val)) { $err = true; } if (!empty($err)) { if (!empty($valid['blank'])) { $val = ''; } elseif (!empty($valid['msg'])) { $error = $valid['msg']; } else { $error = 'You must select one of the available options.'; } } break; case 'bool': case 'boolean': if (!empty($val)) { $val = !empty($valid['set']) ? $valid['set'] : 1; } elseif (!empty($valid['mandatory'])) { $error = 'You must tick this box to continue.'; } else { $val = !empty($valid['empty']) ? $valid['empty'] : 0; } break; case 'clear': $val = false; break; // we can't do this because of the isset check in valid; use the func method to point to valid_copy instead // case 'copy': // $val=$p[$valid['copy']]; // break; // we can't do this because of the isset check in valid; use the func method to point to valid_copy instead // case 'copy': // $val=$p[$valid['copy']]; // break; case 'currency': if (!make_currency($val, $valid['blank'] ? 1 : false)) { $error = !empty($valid['msg']) ? $valid['msg'] : 'You must enter a valid currency value'; } if (!empty($valid['positive']) and $val < 0) { $val *= -1; } break; case 'dat': case 'date': // we had to be careful here, as when we moved to a function with &$error // it started adding the error even if we planned to ignore it // use $err in these cases but might be better to pass on the blank flag // to sub functions of the validator $func = 'sql_' . $valid['type']; $val = $func($val, $err); $today_date = date('Y-m-d'); if (empty($val)) { if (!empty($valid['blank'])) { $val = $valid['blank'] == 'today' ? $today_date : ''; } else { $error = !empty($err) ? $err : 'The date you entered was not recognised'; } } else { if (!empty($valid['past'])) { $valid['max'] = $today_date; } if (!empty($valid['future'])) { $valid['min'] = $today_date; } if (!empty($valid['max']) and $val > $valid['max']) { $error = 'The date specified is greater than the maximum allowed.'; } if (!empty($valid['min']) and $val < $valid['min']) { $error = 'The date specified is less than the minimum allowed.'; } } break; case 'dob': if (!empty($val)) { $val = date_from_dob($val); } if (empty($val) and empty($valid['blank'])) { if (!empty($valid['msg'])) { $error = $valid['msg']; } else { $error = 'You must enter a valid date of birth, try ' . (defined(DATE_USA) ? 'mm/dd/yy' : 'dd/mm/yy') . '.'; } } if (isset($valid['max']) or isset($valid['min'])) { $age = age_from_dob($val); if (!empty($valid['max']) and $age > $valid['max']) { $error = 'This date of birth indicates an age of ' . $age . '. It is required that the age is ' . $valid['max'] . ' or less.'; } if (!empty($valid['min']) and $age < $valid['min']) { $error = 'This date of birth indicates an age of ' . $age . '. It is required that the age is ' . $valid['min'] . ' or more.'; } } if ($val > date('Y-m-d')) { $error = 'A date of birth may not be in the future. If time travel has been invented, please let us know last year.'; } break; case 'email': if (!make_email($val, $valid['blank'] ? 1 : false)) { $error = !empty($valid['msg']) ? $valid['msg'] : 'You must enter a valid email address.'; } break; case 'equal': if (!string_compare($val, $valid['equal'])) { $error = !empty($valid['msg']) ? $valid['msg'] : 'You must enter the exact value.'; } break; // this isn't really a data type, could be removed now that we can accept arrays // this isn't really a data type, could be removed now that we can accept arrays case 'extra': $extra = array(); if (is_array($val['key'])) { foreach ($val['key'] as $n => $key) { $extra[string_check($key)] = string_check($val['val'][$n]); } } $val = serialize($extra); break; case 'html': $val = make_html($val, $valid['tags'], !empty($valid['multi_byte']) ? true : false); if ($valid['length'] > 0) { if (strlen($val) < $valid['length']) { $error = !empty($valid['msg']) ? $valid['msg'] : 'You must enter a value at least ' . ($valid['length'] == 1 ? '1 character' : $valid['length'] . ' characters.') . ' long'; } } break; case 'image': break; case 'keygen': if (empty($val) and empty($valid['regen'])) { $val = rand_pass(); } break; case 'name': $val = make_name($val); if (empty($valid['blank']) and empty($val)) { $error = !empty($valid['msg']) ? $valid['msg'] : 'You must enter a valid name.'; } break; case 'num': case 'number': if (!is_number($val, $valid['blank'] ? 1 : false)) { if (!empty($valid['default'])) { $val = $valid['default']; } else { $error = !empty($valid['msg']) ? $valid['msg'] : 'You must enter a valid number.'; } } if (!empty($val)) { // for legacy support if (isset($valid['ulimit'])) { $valid['max'] = $valid['ulimit']; } if (isset($valid['dlimit'])) { $valid['min'] = $valid['dlimit']; } // if (isset($valid['max']) and $val > $valid['max']) { $error = 'You must enter a number no greater than ' . $valid['max'] . '.'; } if (isset($valid['min']) and $val < $valid['min']) { $error = 'You must enter a number no lower than ' . $valid['min'] . '.'; } if (isset($valid['max-other']) and $val > $p[$valid['max-other']]) { $error = 'You must enter a number no greater than ' . $p[$valid['max-other']] . '.'; } } break; case 'phone': if (isset($valid['other'])) { $error = !make_phones($val, $p[$valid['other']]); } else { $error = !make_phone($val, $valid['blank'] ? 1 : false); } if (!empty($error)) { $error = !empty($valid['msg']) ? $valid['msg'] : 'You must enter a valid phone number.'; } break; case 'postcode': if (!make_postcode($val, $valid['blank'] ? 1 : false)) { $error = !empty($valid['msg']) ? $valid['msg'] : 'You must enter a valid postcode.'; } break; case 'time': if (!make_time($val, $valid['blank'] ? 1 : false, $valid['format'] ? $valid['format'] : null)) { $error = !empty($valid['msg']) ? $valid['msg'] : 'You must enter a valid time.'; } break; case 'url': case 'website': if (!make_website($val, $valid['blank'] ? 1 : false)) { $error = !empty($valid['msg']) ? $valid['msg'] : 'You must enter a valid website address.'; } if (is_array($valid['unique'])) { $check = query("SELECT " . $valid['unique']['id'] . " FROM " . $valid['unique']['table'] . " WHERE website='{$val}'", 'single'); if ($check > 0) { $error = 'The website address you entered is already registered.'; } } break; case 'func': $func = $valid['func']; if (function_exists($func)) { if (!$func($p, $err, $valid)) { $error = !empty($valid['msg']) ? $valid['msg'] : $err; } break; } default: if (!empty($val)) { $val = string_check($val, $valid['strip']); } if (!empty($valid['length'])) { if (strlen($val) < $valid['length']) { $error = !empty($valid['msg']) ? $valid['msg'] : 'You must enter a value at least ' . ($valid['length'] == 1 ? '1 character' : $valid['length'] . ' characters.') . ' long'; } } elseif (!empty($valid['default']) and empty($val)) { $val = $valid['default']; } if (!empty($valid['max']) and $strlen > $valid['max']) { $error = 'You may not enter a value longer than ' . $valid['max'] . ' characters.'; } } validate_unique($valid, $val, $error); if ($error) { return false; } return true; }
function testAgeLastMonth() { $dob = '05/06/1988'; $age = age_from_dob($dob); $this->assertEquals($age, $this->howOldIsMike); }