Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace YetAnotherPanel;
- class User extends Core
- {
- private $isLoggedIn = false,
- $cache = array(),
- $id = 0,
- $userName = '';
- public function __construct()
- {
- $this->_checkSessions();
- if( $this->isLoggedIn() )
- {
- if( !$this->_grabCache() )
- {
- $this->_generateCache();
- }
- }
- }
- public function isLoggedIn()
- {
- return $this->isLoggedIn;
- }
- public function login( $userName, $passWord )
- {
- if( !$userName || !$passWord )
- {
- return 'All fields are required';
- }
- $query = $this->query('SELECT id FROM {prefix}users WHERE username = {0} AND password = {1} LIMIT 1;',
- array( $userName, $passWord ) );
- if( $query->countRows() > 0 )
- {
- $this->id = $query->fetchResult();
- if( $this->_isBanned() )
- {
- return 'You have been banned from the system.';
- }
- $this->_createSession();
- }
- else
- {
- return 'Invalid username or password.';
- }
- }
- private function _createSession()
- {
- global $engine;
- $key = $engine->randomChars( 10 );
- $_SESSION['y__rndKey'] = $key;
- $query = $this->query('SELECT null FROM {prefix}sessions WHERE id = {0, i}', array( $this->id ) );
- session_regenerate_id();
- if( $query->countRows() > 0 )
- {
- $this->query('UPDATE {prefix}sessions SET sessionTimeout = {now} + 500, sessionId = {0}, sessionKey = {1} WHERE userId = {2, i} LIMIT 1;', array( session_id(), $key, $this->id ) );
- }
- else
- {
- $this->query('INSERT INTO {prefix}session VALUES({0}, {1, i},{now} + 500, {2} )', array( session_id(), $this->id, $key ) );
- }
- }
- private function _isBanned()
- {
- $query = $this->query('
- SELECT
- null
- FROM
- {prefix}bans
- WHERE
- userId = {0, i}
- AND
- timeExpire - {now} > 0
- LIMIT 1;
- ', array( $this->id ) );
- if( $query->countRows() > 0 )
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- private function _checkSessions()
- {
- $query = $this->Query('
- SELECT
- userId, sessionKey
- FROM
- {prefix}sessions
- WHERE
- sessionId = {0}
- AND
- sessionTimeOut + 500 - {now} > 0
- LIMIT 1;
- ', array( session_id( ) ) );
- if( $query->countRows() > 0 )
- {
- $data = $query->fetchRow();
- if( $data[ 1 ] == $_SESSION['y__rndKey'] )
- {
- $this->isLoggedIn = true;
- $this->id = $data[ 0 ];
- $this->Query( 'UPDATE {prefix}sessions SET sessionTimeout = {now} + 500 WHERE userId = {0, i} LIMIT 1;', array( $this->id ) );
- }
- }
- }
- private function _grabCache()
- {
- $cacheUrl = BASE . '_cache/_users/' . $this->id . '.cache';
- if( !file_exists( $cacheUrl ) )
- {
- return false;
- }
- $this->cache = unserialize( @file_get_contents( BASE . '_cache/_users/' . $this->id . '.cache' ) );
- }
- private function _generateCache()
- {
- $query = $this->Query('SELECT * FROM {prefix}users WHERE id = {0, i} LIMIT 1;', array( $this->id ) );
- foreach( $query->fetchArray( MYSQL_ASSOC ) as $key => $value )
- {
- if( $key == 'password' ) continue;
- $userCache[ $key ] = $value;
- }
- $userCache = serialize( $userCache );
- file_put_contents( BASE . '_cache/_users/' . $this->id . '.cache', $userCache );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement