public static function Send($to, $subject, $message, $from = null) { $ret = false; $santizer = Validator::instance(); $to = $santizer->Sanitize("email", $to); $subject = $santizer->Sanitize("string", $subject); $message = $santizer->Sanitize("string", $message); $from = $from === null ? "noreply" : $santizer->Sanitize("string", $from); $domain = "@sitandbefitresearch.org"; if (!$santizer->Validate("email", $to)) { echo "The 'To' address('{$to}') is not a valid email"; } else { $from .= $domain; $headers = array(); $headers[] = "From: {$from}"; $headers[] = "Reply-To: {$from}"; $headers[] = "X-Mailer: PHP/" . phpversion(); $headers = implode("\r\n", $headers); if ($_SERVER['SERVER_NAME'] === "localhost") { echo "Headers:\n\t{$headers}\n\n"; echo "To:\n\t{$to}\n"; echo "From:\n\t{$from}\n"; echo "Subject:\n\t{$subject}\n"; echo "Message:\n\t{$message}\n"; } else { $ret = mail($to, $subject, $message, $headers); } } return $ret; }
public static function GetInstance() { if (!self::$instance) { self::$instance = new Validator(); } return self::$instance; }
public static function singleton($keepolderror = false) { if (self::$instance == null) { self::$instance = new Validator(); } if (!$keepolderror) { self::$instance->errors = null; } return self::$instance; }
$valid = true; if (!$this->validate($element_value, 1 + $args['level'])) { $valid = false; } } } } if (0 < count($this->errors) && 0 === $args['level']) { Util::log_error("Validation Failed:\n\n\t- " . implode("\n\t- ", $this->errors)); } return $valid; } /** * @param Object $object * @param integer $level * * @return boolean */ function validate_object($object, $level = 0) { $valid = true; foreach ($object->get_properties() as $property_name => $property) { if (!$object->do_validate_value($property_name, $level)) { $valid = false; } } return $valid; } } Validator::$instance = new Validator();
<?php require_once "header.php"; $msg = ""; $id = Validator::instance()->sanitize("int", $_GET['id']); //get the ID from the link to prevent people from inserting their own ID // ****************************** Activate the user by ID *********************************************************************** $select = QueryFactory::Build("select"); $select->Select("id", "email", "created", "activated")->From("users")->Where(["id", "=", $id])->Limit(); $res = DatabaseManager::Query($select); $res = $res->Result(); if ($res["activated"] === 1) { $msg = ["Your account is already activated!", 1]; } $userActivationHash = sha1($res["id"] . $res["email"] . $res["created"]); // get user hash from database to compare against the link if ($userActivationHash === $_GET['link']) { if (UserModel::updateElement($res["id"], "activated", "1")) { // if acctivation is a success $msg = ["Account activation successful!", 1]; } else { $msg = ["Your account is already activated!", 0]; } } else { $msg = ["Invalid link, please try again!", 0]; } ?> <div class="background"> <h2><center> Activation </center></h2>
/** * @param string $property_name * @param integer $level * * @return boolean */ function do_validate_value($property_name, $level = 0) { $valid = true; $property_value = $this->get_value($property_name); $property = $this->get_property($property_name); if ($property_value instanceof Object) { $valid = Validator::instance()->validate_object($property_value, $level); } else { if ($property->required && empty($property_value)) { $error_msg = "Property \"{$property->property_name}\" is required for the \"%s\" %s"; $error_msg = sprintf($error_msg, $this->get_unique_id(), get_class($this)); if (is_array($property_value) && 0 === count($property_value)) { Util::log_error("{$error_msg} to have array elements."); $valid = false; } else { if (is_null($property_value)) { Util::log_error("{$error_msg} to have array elements."); $valid = false; } } } } return $valid; }
/** * 魔术方法(对外只暴露这一个方法) * @param string $name 方法名称 * @param array $arguments 参数 * @return void */ public static function __callStatic($method, $arguments) { if (!self::$instance instanceof self) { self::$instance = new self(); } $instance = self::$instance; $function = "_{$method}"; if (!is_callable(array($instance, $function))) { throw new Exception(sprintf('Method %s::%s Not Found', __CLASS__, $method)); } return call_user_func_array(array($instance, $function), $arguments); }
$activated = $_POST['activated' . $id]; $update = QueryFactory::Build("update")->Table('users'); $update->Set(["email", $email], ["pLevel", $level], ["activated", $activated]); $update->Where(["id", "=", $id]); $res = DatabaseManager::Query($update); $rows += $res->RowCount(); } if ($rows < 1) { $msg = ["Error: User(s) were not updated or there were no changes to save.", 0]; } else { $msg = ["Users were successfully updated.", 1]; } unset($res, $update, $email, $ids, $id, $level, $activated); } else { if (isset($_GET['delete']) && isset($_POST['id'])) { $id = (int) Validator::instance()->Sanitize("int", $_POST['id']); if (is_int($id)) { $delete = QueryFactory::Build("delete")->From("users")->Where(["id", "=", $id]); $res = DatabaseManager::Query($delete); if ($res->RowCount() > 0) { $msg = ["User successfully deleted", 1]; } else { $msg = ["Error deleting user or user does not exist", 0]; } } } else { if (isset($_GET['adduser']) && isset($_POST)) { $activated = $_POST['activated'] === "1" ? 1 : 0; UserModel::Register($_POST['email'], $_POST['pass'], $activated, $_POST['accesslevel']); } }