/** * Insert and Update validation * In this case, its the same for both methods * * @return bool */ private function validate() { //Check username if (!$this->username) { Registry::addMessage("Debes introducir tu nombre de usuario", "error", "username"); } elseif (User::getBy("username", $this->username, $this->id)) { Registry::addMessage("Este nombre de usuario ya esta registrado", "error", "username"); } //Check email if (!$this->email) { Registry::addMessage("Debes introducir tu email", "error", "email"); } elseif (!filter_var($this->email, FILTER_VALIDATE_EMAIL)) { Registry::addMessage("Email incorrecto", "error", "email"); } elseif (User::getBy("email", $this->email, $this->id)) { Registry::addMessage("Este email ya esta registrado", "error", "email"); } //Return messages avoiding deletion return Registry::getMessages(true); }
static function login($account, $password) { $type = self::account_type($account); switch ($type) { case 'email': self::validate_email($account); break; case 'mobile': break; case 'name': self::validate_name($account); break; } $m = User::getBy($type, $account); if (!$m || !$m->password || !$m->salt) { _throw("用户名或者密码错误"); } if (!$m->test_password($password)) { _throw("用户名或者密码错误"); } if ($m->status != 0) { _throw("账号未激活"); } $ttl = 86400 * 30; return self::force_login($m, $ttl); }
/** * Return all the Clients corresponding to the query array * @param mixed $array * @return \App\Model\Client[] */ public static function getBy($array) { return parent::getBy($array); }
<?php require_once 'bootstrap.php'; $user_id = $_GET['user_id']; if ($user_id) { $user = User::getBy('id', $user_id); $posts = $user->getPosts(); } else { Util::redirect('/beginning-php/vanity'); } include 'includes/header.php'; ?> <h3>All posts for <?php echo $user->username; ?> :</h3> <br clear="all"> <div class="posts"> <?foreach ($posts as $post){?> <div class="post"> <p class="username"><?php echo $post->user->username; ?> posted at <?php echo $post->getTimestamp(); ?> :</p> <p class="content"><?php echo $post->content; ?> </p>
public function loadUser() { if (!$this->user) { $this->user = User::getBy('id', $this->user_id); } }
<?php require_once 'bootstrap.php'; if ($_POST) { $user = User::getBy('username', $_POST['username']); if ($user->authenticate($_POST['password'])) { Util::redirect('/beginning-php/vanity/'); } else { $error_message = 'Invalid username or password'; } } include 'includes/header.php'; ?> <?if ($error_message) {?> <p style="color: red"><?php echo $error_message; ?> </p> <?}?> <form method="POST" action="login.php"> <label>Username</label> <input type="text" name="username"/> <label>password</label> <input type="password" name="password"/> <input type="submit"/> </form> <?php include 'includes/footer.php';