Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class AuthController extends BaseController {
- public function login()
- {
- $this->output['error'] = isset($this->params['error']) ? $this->params['error'] : null;
- $this->output['register'] = isset($this->params['register']) ? $this->params['register'] : null;
- $this->output['templatePath'] = APP_PATH . 'templates/login.php';
- }
- public function doLogin()
- {
- if (isPost()) {
- $user = new User();
- $user->read('email', $_POST['email']);
- $expectedPassword = md5($_POST['password'] . User::PASSWORD_APPEND);
- if ($user->password === $expectedPassword){
- $user->login();
- redirect('/index.php');
- } else {
- redirect('/index.php?page=login&error=1');
- }
- } else {
- redirect('/index.php?page=login&error=2');
- }
- }
- public function doLogout()
- {
- User::logout();
- redirect('/index.php');
- }
- public function register()
- {
- $error = false;
- if (isPost()){
- if (empty($_POST['name'])){
- $error = 'Insert name';
- } elseif (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false){
- $error = 'Invalid email address';
- } elseif (User::emailExists($_POST['email'])){
- $error = 'Email already exists';
- }
- if (!$error){
- $user = new User();
- $user->name = $_POST['name'];
- $user->email = $_POST['email'];
- $user->password = md5($_POST['password'] . User::PASSWORD_APPEND);
- $user->type = 'user';
- $user->save();
- redirect('/index.php?page=login®ister=1');
- }
- $this->output['post'] = $_POST;
- }
- $this->output['registerError'] = $error;
- $this->output['templatePath'] = APP_PATH . 'templates/register.php';
- $this->output['register'] = true;
- }
- }
- <?php
- class BaseController {
- protected $params, $output;
- public function __construct($params = array())
- {
- $this->params = $params;
- $this->output = array();
- }
- public function getOutput()
- {
- return $this->output;
- }
- }
- <?php
- class HomeController extends BaseController {
- public function index()
- {
- $p = isset($this->params['p']) ? $this->params['p'] : 1;
- $articlePerPage = 2;
- $limitStart = ($p-1)*$articlePerPage;
- $articles = Article::getAll(
- "WHERE status = 'published' ORDER by date_published DESC
- LIMIT {$limitStart},{$articlePerPage}
- "
- );
- $this->output['p'] = $p;
- $this->output['articles'] = $articles;
- $this->output['templatePath'] = APP_PATH . 'templates/home.php';
- }
- }
- <?php
- class NotFoundController extends BaseController {
- public function __construct($params)
- {
- parent::__construct($params);
- $this->output['templatePath'] = APP_PATH . 'templates/components/not-found.php';
- $this->output['pageNotFound'] = true;
- }
- }
- index.php
- <?php
- require_once 'application/init.php';
- session_start();
- $params = $_REQUEST;
- $controllerName = isset($_GET['page']) ? $_GET['page'] : 'home';
- $actionName = isset($_GET['action']) ? $_GET['action'] : 'index';
- $controller = null;
- switch($controllerName) {
- case 'auth':
- $controller = new AuthController($params);
- break;
- case 'home':
- $controller = new HomeController($params);
- break;
- }
- if($controller != null && method_exists($controller, $actionName)){
- $controller->{$actionName}();
- } else {
- $controller = new NotFoundController($params);
- }
- $TEMPLATE_VARS = array(
- 'pageNotFound' => false,
- 'userIsLogged' => User::isLogged()
- );
- $TEMPLATE_VARS = array_merge($TEMPLATE_VARS, $controller->getOutput());
- /*if (file_exists($pagePath)){
- require_once $pagePath;
- } else {
- $TEMPLATE_VARS['templatePath'] = APP_PATH . 'templates/components/not-found.php';
- $TEMPLATE_VARS['pageNotFound'] = true;
- }*/
- require_once APP_PATH . 'templates/components/main.php';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement