<?php require_once '../core/init.php'; req::once('functions/rand_pass.php'); $user = new user(); if ($user->notHasPermission('logged in')) { redirect::to('index.php'); } if (input::exists()) { $validate = new validate(); $validate->check($_POST, array()); if ($validate->passed()) { try { //database actions } catch (Exception $e) { die($e->getMessage()); } } }
* Set $_db data member to current database * * function validate::check($src, $items) * Take an array of data to be validated (e.g. $_POST) and check whether * element fit criteria specified by $items. Each criterion manually * defined within validate::check() * $src: associative array of data to be validated * $items: criteria to judge $src by, shares keys with $src * Pushes messages to $_errors if any criterion is not met, sets $_passed to * true if there were no errors, else false * * function addError($error) * Push $error to $_errors array * $error: Error message telling the user what needs to be corrected */ req::once('functions/email_validate.php'); class validate { private $_passed = false, $_errors = array(), $_db = null; public function __construct() { $this->_db = db::getInstance(); } public function check($src, $items = array()) { foreach ($items as $item => $rules) { foreach ($rules as $rule => $rule_value) { $value = $src[$item]; $item = str_replace('-', ' ', escape($item)); if ($rule === 'required' && (empty($value) || $value == '')) { $this->addError("{$item} cannot be left blank");