/** * Redirects page * * @param string $url * @param string $flushMessage Flush message to be shown */ public static function redirect($url, $flushMessage = null) { if ($flushMessage) { Token::set('flush', $flushMessage); } session_write_close(); header('Location: ' . Request::getHost() . $url); }
/** * Renders login form and authenticates user after form submitting * * @return string */ public function loginAction() { $this->_redirectIfLoggedIn(); $errors = array(); if (Request::isPost()) { $model = new SecurityModel(); if ($item = $model->set('email', Request::get('email'))->getItem()) { if (0 === strcmp(Token::cryptPassword(Request::get('password'), $item->salt), $item->password)) { Token::setUser($item); $this->redirect('/'); } } array_push($errors, 'Invalid username or password'); } return $this->_renderView('login.html', array('errors' => $errors)); }
/** * Updates user's profile * * @return string */ public function updateAction() { if (!$this->getUser()) { $this->redirect('/login', 'Please, login first!'); } $errors = array(); $model = new SecurityModel(); $model->setItem($this->getUser()); $model->set('email', Request::get('email'))->set('name', Request::get('name')); if ($model->isValid()) { try { $model->update(); $this->redirect('/', 'Data has been saved successfully'); } catch (DatabaseException $e) { $errors['email'] = 'Email already exists!'; } } else { $errors = $model->getErrors(); } return $this->_renderView('form.html', array('user' => $this->getUser(), 'errors' => $errors)); }
/** * Updates post * * @param int $id * * @return string */ public function editAction($id) { if (!$this->getUser()) { $this->redirect('/', 'Please, login first!'); } $model = new Post(); $date = new \DateTime(); $date->setTimezone(new \DateTimeZone(\Application::getConfig('timezone'))); $model->set('title', Request::get('title'))->set('content', Request::get('content'))->set('id', $id)->set('updated_at', $date->format('Y-m-d H:i:s')); if ($model->isValid()) { try { $model->update(); $this->redirect('/', 'The data has been saved successfully'); } catch (DatabaseException $e) { array_push($errors, $e->getMessage()); } } else { $post = $model->getFieldsObject(); return $this->_renderView('form.html', array('post' => $post, 'errors' => $model->getErrors(), 'action' => '/posts/' . $id . '/edit')); } }
/** * Searches actual route and return it with found param's values * * @param string $uri Url for searching in routes registry * * @return bool|array */ private function _findRoute($uri) { foreach ($this->_registry as $route) { $routeParams = $this->_prepareRoute($route['pattern'], isset($route['_requirements']) ? $route['_requirements'] : array()); if (preg_match($routeParams['uri'], $uri, $match)) { if (isset($route['_requirements']) && isset($route['_requirements']['_method'])) { if ($route['_requirements']['_method'] !== Request::method()) { continue; } } unset($match[0]); if (strpos($uri, '?') !== false) { array_pop($match); } if (count($match)) { $route['_values'] = array_combine($routeParams['params'], $match); } return $route; } } return false; }