/TCTF 2017/Web1(Ugly Web v0.1)/user.class.php

https://github.com/Hcamael/CTF_repo · PHP · 173 lines · 156 code · 17 blank · 0 comment · 33 complexity · f4f2eb3ddc0b991b78a6ccb0df0751fb MD5 · raw file

  1. <?php
  2. class User{
  3. var $dbTable = 'users';
  4. var $sessionVariable = 'userSessionValue';
  5. var $tbFields = array(
  6. 'userID'=> 'userID',
  7. 'login' => 'username',
  8. 'pass' => 'password',
  9. 'email' => 'email',
  10. 'active'=> 'active'
  11. );
  12. var $displayErrors = false;
  13. var $userID;
  14. var $userData=array();
  15. var $remTime = 2592000;
  16. var $remCookieName = 'ckSavePass';
  17. var $remCookieDomain = '';
  18. function __construct() {
  19. global $mysqli;
  20. if( !isset( $_SESSION ) ) session_start();
  21. $this->dbConn = $mysqli;
  22. if ( !empty($_SESSION[$this->sessionVariable]) )
  23. {
  24. $this->loadUser( $_SESSION[$this->sessionVariable] );
  25. }
  26. if ( isset($_COOKIE[$this->remCookieName]) && !$this->is_loaded()){
  27. $u = unserialize(base64_decode($_COOKIE[$this->remCookieName]));
  28. $this->login($u['email'], $u['password']);
  29. }
  30. }
  31. function login($email, $password, $remember = false, $loadUser = true)
  32. {
  33. $email = $this->escape($email);
  34. $originalPassword = $password;
  35. $password = md5($password);
  36. $res = $this->query("SELECT * FROM `{$this->dbTable}`
  37. WHERE `{$this->tbFields['email']}` = '$email' AND `{$this->tbFields['pass']}` = '$password' LIMIT 1",__LINE__);
  38. if ( $res->num_rows == 0)
  39. return false;
  40. if ( $loadUser )
  41. {
  42. $this->userData = $res->fetch_array();
  43. $this->userID = $this->userData[$this->tbFields['userID']];
  44. $_SESSION[$this->sessionVariable] = $this->userID;
  45. }
  46. if ( $remember ){
  47. $cookie = base64_encode(serialize(array('email'=>$email,'password'=>$originalPassword)));
  48. $a = setcookie($this->remCookieName,
  49. $cookie,time()+$this->remTime, $base_path, $this->remCookieDomain, false, true);
  50. }
  51. return true;
  52. }
  53. function logout($redirectTo = '')
  54. {
  55. $_SESSION[$this->sessionVariable] = '';
  56. $this->userData = '';
  57. if ( $redirectTo != '' && !headers_sent()){
  58. header('Location: '.$redirectTo );
  59. exit;//To ensure security
  60. }
  61. }
  62. function is($prop){
  63. return $this->get_property($prop)==1?true:false;
  64. }
  65. function get_property($property)
  66. {
  67. if (empty($this->userID)) $this->error('No user is loaded', __LINE__);
  68. if (!isset($this->userData[$property])) $this->error('Unknown property <b>'.$property.'</b>', __LINE__);
  69. return $this->userData[$property];
  70. }
  71. function is_active()
  72. {
  73. return $this->userData[$this->tbFields['active']];
  74. }
  75. function is_loaded()
  76. {
  77. return empty($this->userID) ? false : true;
  78. }
  79. function activate()
  80. {
  81. if (empty($this->userID)) $this->error('No user is loaded', __LINE__);
  82. if ( $this->is_active()) $this->error('Allready active account', __LINE__);
  83. $res = $this->query("UPDATE `{$this->dbTable}` SET {$this->tbFields['active']} = 1 AND `activationHash`=''
  84. WHERE `{$this->tbFields['userID']}` = '".$this->escape($this->userID)."' LIMIT 1");
  85. if ($res->affected_rows == 1)
  86. {
  87. $this->userData[$this->tbFields['active']] = true;
  88. return true;
  89. }
  90. return false;
  91. }
  92. function insertUser($data){
  93. if (!is_array($data)) $this->error('Data is not an array', __LINE__);
  94. $data[$this->tbFields['pass']] = md5($data[$this->tbFields['pass']]);
  95. foreach ($data as $k => $v ) $data[$k] = "'".$this->escape($v)."'";
  96. $this->query("INSERT INTO `{$this->dbTable}` (`".implode('`, `', array_keys($data))."`) VALUES (".implode(", ", $data).")");
  97. return $this->dbConn->insert_id;
  98. }
  99. function randomPass($length=10, $chrs = '1234567890qwertyuiopasdfghjklzxcvbnm'){
  100. for($i = 0; $i < $length; $i++) {
  101. $pwd .= $chrs{mt_rand(0, strlen($chrs)-1)};
  102. }
  103. return $pwd;
  104. }
  105. function query($sql, $line = 'Uknown')
  106. {
  107. $res = $this->dbConn->query($sql);
  108. if ( !$res )
  109. $this->error($this->dbConn->error, $line);
  110. return $res;
  111. }
  112. function loadUser($userID)
  113. {
  114. $res = $this->query("SELECT * FROM `{$this->dbTable}` WHERE `{$this->tbFields['userID']}` = '".$this->escape($userID)."' LIMIT 1");
  115. if ( $res->num_rows == 0 )
  116. return false;
  117. $this->userData = $res->fetch_array();
  118. $this->userID = $userID;
  119. $_SESSION[$this->sessionVariable] = $this->userID;
  120. return true;
  121. }
  122. function findUser($username)
  123. {
  124. $res = $this->query("SELECT * FROM `{$this->dbTable}` WHERE `{$this->tbFields['login']}` = '".$this->escape($username)."' LIMIT 1");
  125. if ( $res->num_rows == 0 )
  126. return false;
  127. return $res->fetch_array()['userID'];
  128. }
  129. function escape($str)
  130. {
  131. if (is_array($str))
  132. {
  133. $str = array_map([&$this, 'escape'], $str);
  134. return $str;
  135. }
  136. else if (is_string($str))
  137. {
  138. return $this->dbConn->real_escape_string($str);
  139. }
  140. else if (is_bool($str))
  141. {
  142. return ($str === false) ? 0 : 1;
  143. }
  144. else if ($str === null)
  145. {
  146. return 'NULL';
  147. }
  148. return $str;
  149. }
  150. function error($error, $line = '', $die = false) {
  151. if ( $this->displayErrors )
  152. echo '<b>Error: </b>'.$error.'<br /><b>Line: </b>'.($line==''?'Unknown':$line).'<br />';
  153. if ($die) exit;
  154. return false;
  155. }
  156. }