Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.60 KB | None | 0 0
  1. <?php
  2.     class Authorizer {
  3.         public function __construct() {
  4.             /* pass */
  5.         }
  6.        
  7.         public function login( $username, $password, $expires = 1800 /* 30 minutes */ ) {
  8.             global $g_database;
  9.            
  10.             $db = $g_database->handler();
  11.            
  12.             // check if already logged in:
  13.             $sql = $db->prepare( 'SELECT COUNT(*) FROM user_salts WHERE expires >= :t AND session_salt = :s' );
  14.             $sql->bindParam( ':t', time() );
  15.             $sql->bindParam( ':s', session_id() );
  16.             $sql->execute();
  17.             $x = $sql->fetch();
  18.             if( $x["COUNT(*)"] > 0 ) return FALSE;
  19.            
  20.             $sql = $db->prepare( 'SELECT id FROM users WHERE db_username = :uname AND password = :pass LIMIT 1' );
  21.             if( !class_exists( 'VirtualTools' ) ) require_once DIRECTORY_GLOBALS . 'VirtualTools.php';
  22.            
  23.             $sql->bindParam( ':uname', Tools::forDatabase( $username ), PDO::PARAM_STR );
  24.             $sql->bindParam( ':pass', sha1( $password ), PDO::PARAM_STR );
  25.            
  26.             $sql->execute();
  27.            
  28.             if( ($row = $sql->fetch( PDO::FETCH_NUM )) && $row !== FALSE ) {
  29.                 try {
  30.                     $db->beginTransaction();
  31.                     $sql = $db->prepare( 'INSERT INTO user_salts VALUES( NULL, :ssid, :uid, :phash, :expires )' );
  32.                     $ssid = session_id();
  33.                     $uid = $row[0];
  34.                     $phash = sha1( $password );
  35.                     $expires = time() + $expires;
  36.  
  37.                     $sql->bindParam( ':ssid', $ssid );
  38.                     $sql->bindParam( ':uid', $uid, PDO::PARAM_INT );
  39.                     $sql->bindParam( ':phash', $phash );
  40.                     $sql->bindParam( ':expires', $expires );
  41.                     $sql->execute();   
  42.                     $db->commit();
  43.                 }
  44.                 catch( PDOException $e ) {
  45.                     $db->rollBack();
  46.                     throw new GlobalException( 'failed to add an user salt: ' . $e->getMessage() );
  47.                     return FALSE;
  48.                 }
  49.             }
  50.            
  51.             return TRUE;
  52.         }
  53.        
  54.         public function logout() {
  55.             global $g_database;
  56.             $db = $g_database->handler();
  57.            
  58.             try {
  59.                 $db->beginTransaction();
  60.                 $sql = $db->prepare( 'DELETE FROM user_salts WHERE session_salt = :ssid' );
  61.                 $sql->bindParam( ':ssid', session_id() );
  62.                 $sql->execute();
  63.                 $db->commit();
  64.             }
  65.             catch( PDOException $e ) {
  66.                 $db->rollBack();
  67.                 throw new GlobalException( 'failed to logout: ' . $e->getMessage() );
  68.                 return FALSE;
  69.             }
  70.            
  71.             return TRUE;
  72.         }
  73.        
  74.         public function user() {
  75.             global $g_database;
  76.             $db = $g_database->handler();
  77.            
  78.             $sql = $db->prepare( 'SELECT * FROM user_salts WHERE session_salt = :ssid AND expires >= :t ORDER BY expires DESC' );
  79.             $sql->bindParam( ':ssid', session_id() );
  80.             $sql->bindParam( ':t', time() );
  81.             $sql->execute();
  82.            
  83.             $result = $sql->fetch( PDO::FETCH_ASSOC );
  84.             if( $result !== FALSE ) {
  85.                 $recheck = $db->prepare( 'SELECT COUNT(*) FROM users WHERE password = :p AND id = :i' );
  86.                 $recheck->bindParam( ':p', $result['phash'] );
  87.                 $recheck->bindParam( ':i', $result['uid'] );
  88.                 $recheck->execute();
  89.                 $recheck = $recheck->fetch();
  90.                
  91.                 if( $recheck["COUNT(*)"] > 0 ) return new User( $result['uid'] );
  92.             }
  93.             else return NULL;
  94.         }
  95.     }
  96.    
  97.     class User {
  98.         protected $data;
  99.        
  100.         public static function get($id) { return new User($id); }
  101.  
  102.         public function __construct( $id ) {
  103.             global $g_database;
  104.             $db = $g_database->handler();
  105.            
  106.             $sql = $db->prepare( "SELECT * FROM users WHERE id = :id" );
  107.             $sql->bindParam( "id", intval( $id ), PDO::PARAM_INT );
  108.             $sql->execute();
  109.            
  110.             $result = $sql->fetch( PDO::FETCH_ASSOC );
  111.             if( $result === FALSE ) return NULL;
  112.             else foreach( $result as $key => $value ) $this->data[$key] = $value;
  113.            
  114.             $sql = $db->prepare( "SELECT * FROM user_properties WHERE user_id = :id" );
  115.             $sql->bindParam( ":id", $result['id'], PDO::PARAM_INT );
  116.             $sql->execute();
  117.            
  118.             $result = $sql->fetchAll( PDO::FETCH_ASSOC );
  119.             foreach( $result as $res ) {
  120.                 $this->data[$res['property']] = $res['value'];
  121.             }
  122.         }
  123.        
  124.         public function __get( $attr ) {
  125.             $attr = strtolower( $attr );
  126.             if( !isSet( $this->data[$attr] ) ) {
  127.                 return NULL;
  128.             }
  129.             else return $this->data[$attr];
  130.         }
  131.        
  132.         public function __set( $attr, $value ) {
  133.             $attr = strtolower( $attr );
  134.             $inUserVars = Array( 'id', 'password', 'db_username', 'username' );
  135.             if( !in_array( $attr, $inUserVars ) ) {
  136.                
  137.                 global $g_database;
  138.                 $db = $g_database->handler();
  139.                
  140.                 try {
  141.                     $db->beginTransaction();
  142.                     if( isSet( $this->data[$attr] ) ) {
  143.                         $this->data[ $attr ] = $value;
  144.                         $sql = $db->prepare( 'UPDATE user_properties SET value = :v WHERE property = :p AND user_id = :i' );
  145.                         $sql->execute( Array( ':v' => $this->data[ $attr ], ':p' => $attr, ':i' => $this->data['id'] ) );
  146.                     }
  147.                     else {
  148.                         $this->data[ $attr ] = $value;
  149.                         $sql = $db->prepare( 'INSERT INTO user_properties VALUES( NULL, :uid, :prop, :val )' );
  150.                         $sql->execute( Array( ':uid' => $this->data['id'], ':prop' => $attr, ':val' => $this->data[$attr] ) );
  151.                     }
  152.                     $db->commit();
  153.                 }
  154.                 catch( PDOException $e ) {
  155.                     $db->rollBack();
  156.                     throw new GlobalException( 'failed to change an user property: '. $e->getMessage() );
  157.                     return FALSE;
  158.                 }
  159.             }
  160.             else return FALSE;
  161.            
  162.             return TRUE;
  163.         }
  164.        
  165.         public function setPassword( $password ) {
  166.             $this->data[ 'password' ] = sha1($password);
  167.             global $g_database;
  168.             $db = $g_database->handler();
  169.             try {
  170.                 $db->beginTransaction();
  171.                 $sql = $db->prepare( 'UPDATE users SET password = :u WHERE id = :i' );
  172.                 $sql->execute( Array( ':u' => sha1($password), ':i' => $this->data['id'] ) );
  173.                 $db->commit();
  174.             }
  175.             catch( PDOException $e ) {
  176.                 $db->rollBack();
  177.                 throw new GlobalException( 'failed to set a password: ' . $e->getMessage() );
  178.                 return FALSE;
  179.             }
  180.             return TRUE;
  181.         }
  182.        
  183.         public function setUsername( $username ) {
  184.             $this->data[ 'username' ] = $username;
  185.             global $g_database;
  186.             $db = $g_database->handler();
  187.             try {
  188.                 $db->beginTransaction();
  189.                 $sql = $db->prepare( 'UPDATE users SET username = :u WHERE id = :i' );
  190.                 $sql->execute( Array( ':u' => $username, ':i' => $this->data['id'] ) );
  191.                 $db->commit();
  192.             }
  193.             catch( PDOException $e ) {
  194.                 $db->rollBack();
  195.                 throw new GlobalException( 'failed to set an user name' . $e->getMessage() );
  196.                 return FALSE;
  197.             }
  198.            
  199.             return TRUE;
  200.         }
  201.        
  202.         public static function create( $userName, $userPassword ) {
  203.             global $g_database;
  204.             $db = $g_database->handler();
  205.             $db_name = Tools::forDatabase( $userName );
  206.             $pass = sha1( $userPassword );
  207.                
  208.             // checking if user already exists...
  209.             $sql = $db->prepare( 'SELECT id FROM users WHERE db_username = :dbu' );
  210.             $sql->bindParam( ':dbu', $db_name, PDO::PARAM_STR );
  211.             $sql->execute();
  212.             $x = $sql->fetch();
  213.             if( $x !== FALSE ) {
  214.                 throw new GlobalException( 'user already exists' );
  215.                 return NULL;
  216.             }
  217.  
  218.             try {
  219.                 $db->beginTransaction();
  220.                 $sql = $db->prepare( 'INSERT INTO users VALUES( NULL, :uname, :uname_db, :pass )' );
  221.                 $sql->bindParam( ':uname', $userName );
  222.                 $sql->bindParam( ':uname_db', $db_name );
  223.                 $sql->bindParam( ':pass', $pass );
  224.                 $sql->execute();
  225.                 $db->commit();
  226.             }
  227.             catch( PDOException $e ) {
  228.                 $db->rollBack();
  229.                 throw new GlobalException( 'failed to create an user: ' . $e->getMessage() );
  230.                 return NULL;
  231.             }
  232.            
  233.             $selected = $db->prepare( 'SELECT id FROM users WHERE db_username = ?' );
  234.             $selected->execute( Array( Tools::forDatabase( $userName ) ) );
  235.             $selected = $selected->fetch( PDO::FETCH_NUM );
  236.             if( !empty( $selected ) ) return new User( $selected[0] );
  237.             else return NULL;
  238.         }
  239.        
  240.         public function delete() {
  241.             global $g_database;
  242.             $db = $g_database->handler();
  243.            
  244.             $sql_user = $db->prepare( 'DELETE FROM users WHERE id = ?' );
  245.             $sql_user->execute( Array( $this->id ) );
  246.         }
  247.     }
  248. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement