Advertisement
fabi0

Untitled

May 26th, 2014
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.99 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * To change this license header, choose License Headers in Project Properties.
  5.  * To change this template file, choose Tools | Templates
  6.  * and open the template in the editor.
  7.  */
  8.  
  9. /**
  10.  * Description of Session
  11.  *
  12.  * @author fabi0
  13.  */
  14.  
  15. namespace Models;
  16.  
  17. class Session {
  18.  
  19.     private $_instance = null;
  20.  
  21.     public function __construct($databaseInstance) {
  22.         $this->_instance = $databaseInstance;
  23.     }
  24.  
  25.     public static function exists($name) {
  26.  
  27.         $params = array(
  28.             ':name' => array(
  29.                 'param' => $name,
  30.                 'type' => 1
  31.             )
  32.         );
  33.         $sql = 'SELECT session_id FROM sessions WHERE session_name=:name;';
  34.         $this->_instance->query($sql, $params);
  35.         if ($this->_instance->getCount()) {
  36.             return TRUE;
  37.         } else {
  38.             return FALSE;
  39.         }
  40.     }
  41.  
  42.     public static function get($name) {
  43.         $params = array(
  44.             ':name' => array(
  45.                 'param' => $name,
  46.                 'type' => 2
  47.             )
  48.         );
  49.         $sql = 'SELECT session_object FROM sessions WHERE session_name=:name;';
  50.         $this->_instance->query($sql, $params);
  51.         if ($this->_instance->getCount()) {
  52.             return $this->_instance->getResult()[0]->session_object;
  53.         } else {
  54.             return FALSE;
  55.         }
  56.     }
  57.  
  58.     public static function createSession($array) {
  59.         @$expire = $array['remember_me'];
  60.         unset($array['remember_me']);
  61.         $array = base64_encode(serialize($array));
  62.         $salt = uniqid(rand(), true);
  63.         $session_name = sha1($salt . $salt);
  64.         $params = array(
  65.             ':salt' => array(
  66.                 'param' => $salt,
  67.                 'type' => 2
  68.             ),
  69.             ':name' => array(
  70.                 'param' => $session_name,
  71.                 'type' => 2
  72.             ),
  73.             ':object' => array(
  74.                 'param' => $array,
  75.                 'type' => 2
  76.             ),
  77.             ':time' => array(
  78.                 'param' => time(),
  79.                 'type' => 1
  80.             ),
  81.             ':expire' => array(
  82.                 'param' => (int) $expire,
  83.                 'type' => 1
  84.             )
  85.         );
  86.         $sql = "INSERT INTO `sessions` "
  87.                 . "( `session_salt`, `session_name`, `session_object`, `session_create`, `session_expire`) "
  88.                 . "VALUES (:salt, :name, :object, :time, :expire);";
  89.         $this->_instance->query($sql, $params);
  90.         return $session_name;
  91.     }
  92.  
  93.     public static function delete($name) {
  94.         $params = array(
  95.             ':name' => array(
  96.                 'param' => $name,
  97.                 'type' => 2
  98.             )
  99.         );
  100.         $sql = "DELETE FROM sessions WHERE session_name = :name";
  101.         $this->_instance->query($sql, $params);
  102.         return true;
  103.     }
  104.  
  105.     public static function update($name, $data) {
  106.         // TODO
  107.         $database = \Models\DB::getInstance();
  108.     }
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement