Advertisement
saadimran

Untitled

Oct 14th, 2014
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.89 KB | None | 0 0
  1. <?PHP
  2. namespace GalaxisDark\Mvc;
  3.  
  4. use GalaxisDark\Models\Forums\User;
  5.  
  6. class Controller extends \Phalcon\Mvc\Controller {
  7.  
  8.     private $theme = "galaxisdark";
  9.     protected $user = false;
  10.  
  11.     public function beforeExecuteRoute($dispatcher) {
  12.         $this->url->setBasePath(realpath(__DIR__ . "/../.."));
  13.         $this->url->setBaseUri("//galaxisdark.com");
  14.        
  15.         $this->url->setThemeBasePath($this->url->path("/themes/{$this->theme}"));
  16.         $this->url->setThemeBaseUri($this->url->get("/themes/{$this->theme}"));
  17.        
  18.         $this->view->setViewsDir($this->url->getThemePath("/views/"));
  19.         $this->view->setMainView("template");
  20.         $this->view->setLayout("main");
  21.        
  22.         $this->login();
  23.     }
  24.    
  25.     public function login($force = false) {
  26.         if($this->user === false) {
  27.             $lastvisit = time();
  28.             if(isset($_POST['login']['form'])) {
  29.                 $this->user = $this->login_using_form();               
  30.             } else if (isset($_COOKIE['mybbuser'])) {
  31.                 $this->user = $this->login_using_cookies();
  32.                 $lastvisit = null;
  33.             }
  34.            
  35.             if($this->user) {
  36.                 setCookie("mybbuser", "{$this->user->uid}_{$this->user->loginkey}", (time() + 3600), '/', '.galaxisdark.com');
  37.                
  38.                 $this->user->lastip = $_SERVER['REMOTE_ADDR'];
  39.                 $this->user->longlastip = $this->ip2long($this->user->lastip);
  40.                 $this->user->lastactive = time();
  41.                 $this->user->lastvisit = $lastvisit ? $lastvisit : $this->user->lastvisit;
  42.                
  43.                 $this->user->update();
  44.             }
  45.            
  46.             $this->view->user = $this->user;
  47.         }
  48.  
  49.         if($force == true && !$this->user) {
  50.             $this->dispatcher->forward([
  51.                 'controller' => 'rpg',
  52.                 'action' => 'login'
  53.             ]);
  54.         }
  55.        
  56.         return $this->user ? true : false;
  57.     }
  58.    
  59.     public function logout() {
  60.         setCookie("mybbuser", null, (time() - 3600), '/', '.galaxisdark.com');
  61.         unset($_COOKIE['mybbuser']);
  62.         $this->user = null;
  63.         $this->view->user = null;
  64.     }
  65.    
  66.     private function ip2long($ip) {
  67.         $ip_long = ip2long($ip);
  68.         if(!$ip_long) {
  69.             $ip_long = sprintf("%u", ip2long($ip));
  70.             if(!$ip_long)
  71.                 return 0;
  72.         }
  73.  
  74.         if($ip_long >= 2147483648)
  75.             $ip_long -= 4294967296;
  76.  
  77.         return $ip_long;
  78.     }
  79.    
  80.     private function login_using_form() {
  81.         $username = $_POST['username'];
  82.         $password = $_POST['password'];
  83.        
  84.         $user = User::find_first([
  85.             'username = :username:',
  86.             'bind' => ['username' => $username],
  87.             ':cache' => false
  88.         ]);
  89.        
  90.         if($user === false || $user->password !== md5(md5($user->salt) . md5($password)))
  91.             $user = null;
  92.        
  93.         return $user;
  94.     }
  95.    
  96.     private function login_using_cookies() {
  97.         list($uid, $loginKey) = explode("_", $_COOKIE['mybbuser']);
  98.        
  99.         $user = User::get_by_id($uid, [':cache' => ['query' => 0]]);
  100.        
  101.         if($user == false || $user->loginkey !== $loginKey)
  102.             $user = null;
  103.            
  104.         return $user;
  105.     }
  106.    
  107.     public function start_journey() {
  108.         if($this->user && $this->user->trainer->region_id == 0) {
  109.             $this->response->redirect('/start_journey');
  110.             return true;
  111.         }
  112.         return false;
  113.     }
  114.    
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement