aFormVars = array(); $this->aChecks = array(); $this->aIsValid = array(); $this->oAction = new Action(); $this->strErrorIndication = ''; $this->type = $_REQUEST['verify']; $aInfo = $aSetup[$this->type]; if (!$aInfo) return; $this->oAction =& $aInfo['action']; $this->aChecks = $aInfo['checks']; $this->strErrorIndication = $aInfo['errorIndication']; // verify all checks, fill vars $bAllValid = 1; foreach (array_keys($this->aChecks) as $name) { $check = &$this->aChecks[$name]; $this->aFormVars[$name] = undoMagic($_REQUEST[$name]); $this->aIsValid[$name] = $check->isValid($this, $this->aFormVars[$name]); if (!$this->aIsValid[$name]) $bAllValid = 0; } if ($bAllValid == 1) { $this->oAction->doAction($this->aFormVars); exit; } } function insertVar($formtype, $name) { if ($this->type != $formtype) return; echo htmlspecialchars($this->aFormVars[$name]); } function errorClass($formtype, $name) { if ($this->type != $formtype) return; if (!$this->aIsValid[$name]) echo 'error'; } function errorIndication($formtype, $name) { if ($this->type != $formtype) return; if (!$this->aIsValid[$name]) echo $this->strErrorIndication; } function onError($formtype, $name, $text) { if ($this->type != $formtype) return; if (!$this->aIsValid[$name]) echo $text; } function errorSummary($formtype, $pre, $join, $post) { if ($this->type != $formtype) return; $aErrors = array(); foreach ($this->aIsValid as $name => $isValid) { if (!$isValid) { $message = $this->aChecks[$name]->errorMessage(); if ($message != '') array_push($aErrors, $message); } } if (count($aErrors) == 0) return; echo $pre; echo implode($join, $aErrors); echo $post; } function setSelected($formtype, $name, $val) { if ($this->type != $formtype) return; if ($this->aFormVars[$name] == $val) echo ' selected="selected"'; } function setChecked($formtype, $name, $val, $def = 0) { if ($this->type != $formtype) { if ($def == 1) echo ' checked="checked"'; return; } if ($this->aFormVars[$name] == $val) echo ' checked="checked"'; } } class Action { function doAction(&$aVars) {} } class Check { var $errorMessage; function Check($errorMessage) { $this->errorMessage = $errorMessage; } function isValid(&$verify, $value) { return false; } function errorMessage() { return $this->errorMessage; } } class NonEmptyCheck extends Check { function NonEmptyCheck($errorMessage) { $this->Check($errorMessage); } function isValid(&$verify, $value) { $valueTrimmed = trim($value); return ($valueTrimmed != ''); } } class RegexCheck extends Check { var $regex; function RegexCheck($regex, $errorMessage) { $this->Check($errorMessage); $this->regex = $regex; } function isValid(&$verify, $value) { return preg_match($this->regex, $value); } } class PostCodeCheck extends Check { var $errorInvalid; function PostCodeCheck($errorMessage, $errorInvalid) { $this->Check($errorMessage); $this->errorInvalid = $errorInvalid; } function isValid(&$verify, $value) { // check if postcode is in a format we expect if (!preg_match('/^[0-9]{4} ?[A-Za-z]{2}$/', $value)) return false; // check if postcode is actually valid if (!$this->lookup($value)) { $url = 'uitgebreid.php?verify=verify-brochure' . '&naam=' . urlencode(undoMagic($_REQUEST['naam'])) . '&postcode=' . urlencode(undoMagic($_REQUEST['postcode'])) . '&huisnummer=' . urlencode(undoMagic($_REQUEST['huisnummer'])); $this->errorMessage = str_replace('[uitgebreid]', htmlspecialchars($url), $this->errorInvalid); return false; } return true; } function lookup($code) { // 1234 AB is the only unknown code return (strtoupper($code) != '1234 AB'); } } class HuisNummerCheck extends Check { var $errorInvalid; // $errorMessage = non-empty // $errorInvalid = value does not correspond... function HuisNummerCheck($errorMessage, $errorInvalid) { $this->Check($errorMessage); $this->errorInvalid = $errorInvalid; } function isValid(&$verify, $value) { $valueTrimmed = trim($value); if ($valueTrimmed == '') return false; // check if value corresponds to postcode $postcode = $verify->aFormVars['postcode']; if (!$this->checkPostcode($postcode, $value)) { // use second error message $this->errorMessage = $this->errorInvalid . ' ' . $postcode; return false; } return true; } function checkPostcode($postcode, $num) { // example $num = intval($num); return (($num > 0) && ($num < 100)); } } ?>