Guest User

Untitled

a guest
Nov 24th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.59 KB | None | 0 0
  1. <?php
  2.  
  3.     /**
  4.     *
  5.     * @author James Freeman
  6.     * @cofounder Chris Ball
  7.     * @projectName somethingyouwanttosaybutcant
  8.     *
  9.     */
  10.    
  11.     class User
  12.     {
  13.        
  14.         public $loggedIn = false;
  15.        
  16.         public function __construct()
  17.         {
  18.            
  19.             $this->_checkForSessions();
  20.            
  21.             if( $this->loggedIn )
  22.             {
  23.                
  24.                 $this->_cacheData();
  25.                
  26.             }
  27.            
  28.         }
  29.                
  30.         public function encrypt( $str )
  31.         {
  32.            
  33.             global $config;
  34.            
  35.             $i = 0;
  36.            
  37.             foreach( $config['settings']['encrypt']['algorithm'] as $algo )
  38.             {
  39.                                
  40.                 $str = $algo( $str . $config['settings']['encrypt']['salt' . ( $i % 2 ? 2 : 1 )] );
  41.                
  42.                 ++$i;
  43.                
  44.             }
  45.            
  46.             return $str;
  47.            
  48.         }
  49.        
  50.         public function login( $userName = '', $passWord = '' )
  51.         {
  52.            
  53.             if( !$userName || !$passWord ) return 'All fields are required';
  54.            
  55.             $query = MySQL::newQuery()->query('SELECT id FROM {prefix}users WHERE username = ? AND password = ? LIMIT 1;')
  56.                      ->bind('ss', $userName, $this->encrypt( $passWord ) );
  57.                      
  58.             if( $query->count() > 0 )
  59.             {
  60.                                
  61.                 $this->id = $query->fetch();
  62.                
  63.                 $query = MySQL::newQuery()
  64.                 ->Query('SELECT ban_reason, ban_expiry FROM {prefix}bans WHERE user_id = ? AND (UNIX_TIMESTAMP - ban_expiry) > 0 LIMIT 1;')
  65.                
  66.                 ->bind('i', $this->id );
  67.                
  68.                 $data = $query->fetch();
  69.                
  70.                
  71.                 if( $query->count() > 0 )
  72.                 {
  73.                
  74.                     return 'You are banned until ' . date( 'd/m/Y H:i', $data[1] . 'because ' . $data[0] );
  75.                    
  76.                 }
  77.                
  78.                 $this->_createSession();
  79.                
  80.                 header('Location: /');
  81.             }
  82.             else
  83.             {
  84.                
  85.                 return 'Please enter a valid username and password.';
  86.                
  87.             }
  88.            
  89.         }
  90.        
  91.         protected function _createSession()
  92.         {
  93.            
  94.             MySQL::newQuery()->query('DELETE FROM {prefix}sessions WHERE id_user = ?;')->bind('i', $this->id)->execute();
  95.            
  96.             MySQL::newQuery()
  97.             ->query('INSERT INTO {prefix}sessions(id_user, id_session, secKey, `timeout`)values(?, ?, ?, UNIX_TIMESTAMP + 600);')
  98.             ->bind('iss', $this->id, $this->encrypt( session_id( ) ), $this->encrypt( $_SERVER['HTTP_USER_AGENT'] ) )
  99.             ->execute();
  100.            
  101.         }
  102.        
  103.         protected function _checkForSessions()
  104.         {
  105.                                    
  106.             $query = mySQL::newQuery()
  107.             ->Query('
  108.                SELECT
  109.                
  110.                    id_user
  111.                    
  112.                FROM
  113.                
  114.                {prefix}sessions
  115.                
  116.                WHERE
  117.                
  118.                id_session = ?
  119.                
  120.                AND
  121.                
  122.                (`timeout` - UNIX_TIMESTAMP() ) > 0
  123.                
  124.                AND
  125.                
  126.                secKey = ?
  127.                
  128.                LIMIT 1;
  129.            ' )
  130.             ->bind( 'ss', $this->encrypt( session_id() ), $this->encrypt( $_SERVER['HTTP_USER_AGENT'] ) );
  131.                        
  132.             if( $query->count() < 1 )
  133.             {
  134.                
  135.                 return;
  136.                
  137.             }
  138.            
  139.             $this->id = $query->fetch();
  140.             $this->loggedIn = true;
  141.            
  142.             return;
  143.            
  144.         }
  145.        
  146.         protected function _cacheData()
  147.         {
  148.            
  149.            $query = mySQL::newQuery()->Query('SELECT * FROM {prefix}users WHERE id = ? LIMIT 1;' )
  150.            ->bind( 'i', $this->id );
  151.            
  152.            foreach( $query->fetch() as $key => $value )
  153.            {
  154.            
  155.                 $this->data[ $key ] = $value;
  156.              
  157.            }
  158.            
  159.            $this->data['permissions'] != null ? unserialize( $this->data['permissions'] ) : '';
  160.                                  
  161.         }
  162.     }
Add Comment
Please, Sign In to add comment