Advertisement
Guest User

Untitled

a guest
Jun 9th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.46 KB | None | 0 0
  1. <?php
  2.  
  3. class AuthController extends BaseController {
  4.    
  5.     public function login()
  6.     {
  7.         $this->output['error'] = isset($this->params['error']) ? $this->params['error'] : null;
  8.         $this->output['register'] = isset($this->params['register']) ? $this->params['register'] : null;
  9.         $this->output['templatePath'] = APP_PATH . 'templates/login.php';
  10.     }
  11.    
  12.     public function doLogin()
  13.     {
  14.         if (isPost()) {
  15.        
  16.             $user = new User();
  17.             $user->read('email', $_POST['email']);
  18.            
  19.             $expectedPassword = md5($_POST['password'] . User::PASSWORD_APPEND);
  20.            
  21.             if ($user->password === $expectedPassword){
  22.                 $user->login();
  23.                 redirect('/index.php');
  24.             } else {
  25.                 redirect('/index.php?page=login&error=1');
  26.             }
  27.         } else {
  28.             redirect('/index.php?page=login&error=2');
  29.         }
  30.     }
  31.    
  32.     public function doLogout()
  33.     {
  34.         User::logout();
  35.         redirect('/index.php');
  36.     }
  37.    
  38.     public function register()
  39.     {
  40.                
  41.         $error = false;
  42.        
  43.         if (isPost()){
  44.            
  45.             if (empty($_POST['name'])){
  46.                 $error = 'Insert name';
  47.             } elseif (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false){
  48.                 $error = 'Invalid email address';
  49.             } elseif (User::emailExists($_POST['email'])){
  50.                 $error = 'Email already exists';
  51.             }
  52.            
  53.             if (!$error){
  54.                 $user = new User();
  55.                 $user->name = $_POST['name'];
  56.                 $user->email = $_POST['email'];
  57.                 $user->password = md5($_POST['password'] . User::PASSWORD_APPEND);
  58.                 $user->type = 'user';
  59.                 $user->save();
  60.                 redirect('/index.php?page=login&register=1');
  61.             }
  62.            
  63.             $this->output['post'] = $_POST;
  64.            
  65.         }
  66.        
  67.         $this->output['registerError'] = $error;
  68.         $this->output['templatePath'] = APP_PATH . 'templates/register.php';
  69.         $this->output['register'] = true;    
  70.     }
  71. }
  72.  
  73. <?php
  74. class BaseController {
  75.     protected $params, $output;
  76.    
  77.     public function __construct($params = array())
  78.     {
  79.         $this->params = $params;
  80.         $this->output = array();
  81.     }
  82.    
  83.     public function getOutput()
  84.     {
  85.         return $this->output;
  86.     }
  87. }
  88.  
  89. <?php
  90.  
  91. class HomeController extends BaseController {
  92.    
  93.     public function index()
  94.     {
  95.         $p = isset($this->params['p']) ? $this->params['p'] : 1;
  96.        
  97.         $articlePerPage = 2;
  98.         $limitStart = ($p-1)*$articlePerPage;
  99.        
  100.         $articles = Article::getAll(
  101.             "WHERE status = 'published' ORDER by date_published DESC
  102.            LIMIT {$limitStart},{$articlePerPage}
  103.            "
  104.         );
  105.        
  106.         $this->output['p'] = $p;
  107.         $this->output['articles'] = $articles;
  108.         $this->output['templatePath'] = APP_PATH . 'templates/home.php';
  109.     }
  110. }
  111.  
  112. <?php
  113.  
  114. class NotFoundController extends BaseController {
  115.    
  116.     public function __construct($params)
  117.     {
  118.         parent::__construct($params);
  119.         $this->output['templatePath'] = APP_PATH . 'templates/components/not-found.php';
  120.         $this->output['pageNotFound'] = true;
  121.     }
  122. }
  123.  
  124. index.php
  125. <?php
  126.  
  127. require_once 'application/init.php';
  128.  
  129. session_start();
  130.  
  131. $params = $_REQUEST;
  132.  
  133. $controllerName = isset($_GET['page']) ? $_GET['page'] : 'home';
  134. $actionName = isset($_GET['action']) ? $_GET['action'] : 'index';
  135.  
  136. $controller = null;
  137. switch($controllerName) {
  138.     case 'auth':
  139.         $controller = new AuthController($params);
  140.         break;
  141.     case 'home':
  142.         $controller = new HomeController($params);
  143.         break;
  144. }
  145.  
  146. if($controller != null && method_exists($controller, $actionName)){
  147.     $controller->{$actionName}();
  148. } else {
  149.     $controller = new NotFoundController($params);
  150. }
  151.  
  152. $TEMPLATE_VARS = array(
  153.     'pageNotFound' => false,
  154.     'userIsLogged' => User::isLogged()
  155. );
  156.  
  157. $TEMPLATE_VARS = array_merge($TEMPLATE_VARS, $controller->getOutput());
  158.  
  159. /*if (file_exists($pagePath)){
  160.     require_once $pagePath;
  161.    
  162. } else {
  163.     $TEMPLATE_VARS['templatePath'] = APP_PATH . 'templates/components/not-found.php';
  164.     $TEMPLATE_VARS['pageNotFound'] = true;
  165. }*/
  166.  
  167. require_once APP_PATH . 'templates/components/main.php';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement