Advertisement
Guest User

Untitled

a guest
Jul 7th, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.40 KB | None | 0 0
  1. <?php
  2.  
  3. class user
  4. {
  5.  
  6. private $session_id = null;
  7.  
  8. public $logged_in = false,
  9. $id = 0;
  10.  
  11. public function __construct( )
  12. {
  13.  
  14. global $core, $db;
  15.  
  16. $this->session_id = $core->encrypt( session_id( ) );
  17.  
  18. $db->setParam( 'rndKey', $core->clean( $_SESSION['ticket'] ) );
  19.  
  20. $db->setParam( 'sessionId', $this->session_id );
  21.  
  22. /* TODO: work on time based session handling.. p: low */
  23.  
  24. $query = $db->query( "SELECT id_user FROM %prefix%sessions WHERE id_rndKey = @rndKey AND id_sessionId = @sessionId LIMIT 1;" );
  25.  
  26. if( $db->num_rows( $query, true ) > 0 )
  27. {
  28.  
  29. $this->id = $db->result( $query, true );
  30.  
  31. if( $this->_userChecks( ) )
  32. {
  33.  
  34. $this->_cacheUser();
  35.  
  36. $this->logged_in = true;
  37.  
  38. }
  39.  
  40. }
  41.  
  42. }
  43.  
  44. public function reCache( )
  45. {
  46.  
  47. $this->_cacheUser();
  48.  
  49. }
  50.  
  51. public function doLogin( )
  52. {
  53.  
  54. global $core, $db;
  55.  
  56. if( empty( $_POST['username'] ) || empty( $_POST['password'] ) )
  57. {
  58.  
  59. return 'All fields are required';
  60.  
  61. }
  62.  
  63. $db->setParam
  64. (
  65. array
  66. (
  67.  
  68. array( 'credName', $core->clean( $_POST['username'] ) ),
  69.  
  70. array( 'credPass', $core->encrypt( $_POST['password'] ) )
  71.  
  72. )
  73. );
  74.  
  75. $query = $db->query("SELECT id, lock_to_email, username FROM %prefix%users WHERE (username = @credName OR email = @credName) AND password = @credPass LIMIT 1;");
  76.  
  77. if( $db->num_rows( $query, true ) > 0 )
  78. {
  79.  
  80. $fetch = $db->fetch_array( $query, true );
  81.  
  82. if( $fetch[1] != '1' )
  83. {
  84.  
  85. $this->_createSession( $fetch[0] );
  86. return true;
  87. }
  88. else
  89. {
  90.  
  91. if( $fetch[2] == $_POST['username'] )
  92. {
  93.  
  94. return 'Your credenitals do not match our records..';
  95.  
  96. }
  97. else
  98. {
  99.  
  100. $this->_createSession( $fetch[0] );
  101. return true;
  102.  
  103. }
  104.  
  105. }
  106. }
  107. else
  108. {
  109.  
  110. return 'Your credenitals do not match our records..';
  111.  
  112. }
  113.  
  114. }
  115.  
  116. public function hasPermissions( $userGroup, $permissionName )
  117. {
  118.  
  119. global $userGroups;
  120.  
  121. if( $this->data['privileges'] == null )
  122. {
  123.  
  124. return false;
  125.  
  126. }
  127.  
  128. if( $userGroups[ $userGroup ][ $permissionName ] & $this->data['privileges'][ $userGroup ] )
  129. {
  130.  
  131. return true;
  132.  
  133. }
  134. else
  135. {
  136.  
  137. return false;
  138.  
  139. }
  140.  
  141. }
  142.  
  143. public function group2html( $groupId, $userName = null )
  144. {
  145. global $db;
  146.  
  147. $db->setParam( 'groupId', ( int ) $groupId );
  148.  
  149. $fetchArray = $db->fetch_array( "SELECT groupColour, groupBold FROM %prefix%usergroups WHERE groupId = @groupId LIMIT 1;" );
  150.  
  151. return sprintf('<span style="color: %s; font-weight: %s">%s</span>', $fetchArray[0], ( ( ( bool ) $fetchArray[1] ) ? 'bold' : 'normal' ), ( ( $userName ) ? $userName : $this->data['username'] ) );
  152.  
  153. }
  154.  
  155. private function _createSession( $userId )
  156. {
  157.  
  158. global $db, $core;
  159.  
  160. for( $i = 0; $i < 10; $i++ )
  161. {
  162.  
  163. $ticket .= rand( 0, 1 );
  164.  
  165. }
  166.  
  167. $_SESSION['ticket'] = $ticket;
  168.  
  169. $db->setParam
  170. (
  171. array(
  172. array( 'id', $userId ),
  173. array( 'id_session', $core->encrypt( session_id( ) ) ),
  174. array( 'rndKey', $ticket )
  175. )
  176. );
  177.  
  178. $db->query( 'INSERT INTO %prefix%sessions (id_user, id_sessionId, id_rndKey)VALUES(@id, @id_session, @rndKey)' );
  179.  
  180. }
  181.  
  182. private function _cacheUser( )
  183. {
  184.  
  185. global $db;
  186.  
  187. $query = $db->fetch_array( "SELECT * FROM %prefix%users WHERE id = '".$this->id."' LIMIT 1;" );
  188.  
  189. foreach ( $query as $key => $value )
  190. {
  191.  
  192. if( !is_numeric( $key ) )
  193. {
  194.  
  195. if( $key != 'privileges' )
  196. {
  197.  
  198. $this->data[ $key ] = $value;
  199.  
  200. }
  201. else
  202. {
  203.  
  204. $this->data[ $key ] = unserialize( $value );
  205.  
  206. }
  207.  
  208. }
  209.  
  210.  
  211. }
  212. }
  213.  
  214. private function _userChecks( )
  215. {
  216.  
  217. return true;
  218.  
  219. }
  220. }
  221.  
  222. $user = new user();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement