PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/movil/libs/login.php

https://bitbucket.org/jonarano/joneame
PHP | 215 lines | 175 code | 28 blank | 12 comment | 52 complexity | 05003e0bae88c781888cc44383c27cd9 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?PHP
  2. // The source code packaged with this file is Free Software, Copyright (C) 2005 by
  3. // Ricardo Galli <gallir at uib dot es>.
  4. // It's licensed under the AFFERO GENERAL PUBLIC LICENSE unless stated otherwise.
  5. // You can get copies of the licenses here:
  6. // http://www.affero.org/oagpl.html
  7. // AFFERO GENERAL PUBLIC LICENSE is also included in the file called "COPYING".
  8. class UserAuth {
  9. var $user_id = 0;
  10. var $user_login = '';
  11. var $user_email = '';
  12. var $md5_pass = '';
  13. var $authenticated = FALSE;
  14. var $user_level='';
  15. var $user_karma=0;
  16. var $admin = false;
  17. var $devel = false;
  18. var $user_avatar=0;
  19. var $mnm_user = False;
  20. var $especial = false;
  21. function UserAuth() {
  22. global $db, $site_key, $globals;
  23. $this->now = $globals['now'];
  24. if(!empty($_COOKIE['joneame'])) {
  25. $this->mnm_user=explode(":", $_COOKIE['joneame']);
  26. }
  27. if($this->mnm_user[0] && !empty($_COOKIE['joneame_key'])) {
  28. $userInfo=explode(":", base64_decode($_COOKIE['joneame_key']));
  29. if($this->mnm_user[0] === $userInfo[0]) {
  30. $cookietime = (int) $userInfo[3];
  31. $dbusername = $db->escape($this->mnm_user[0]);
  32. $user=$db->get_row("SELECT SQL_CACHE user_id, user_pass, user_level, user_thumb, UNIX_TIMESTAMP(user_validated_date) as user_date, user_karma, user_email, user_avatar, user_prev_carisma FROM users WHERE user_login = '$dbusername'");
  33. // We have two versions from now
  34. // The second is more strong agains brute force md5 attacks
  35. switch ($userInfo[2]) {
  36. case '3':
  37. if (($this->now - $cookietime) > 864000) $cookietime = 'expired'; // after 10 days expiration is forced
  38. $key = md5($user->user_email.$site_key.$dbusername.$user->user_id.$cookietime);
  39. break;
  40. case '2':
  41. $key = md5($user->user_email.$site_key.$dbusername.$user->user_id);
  42. $cookietime = 0;
  43. break;
  44. default:
  45. $key = md5($site_key.$dbusername.$user->user_id);
  46. $cookietime = 0;
  47. }
  48. if ( !$user || !$user->user_id > 0 || $key !== $userInfo[1] ||
  49. $user->user_level == 'disabled' ||
  50. empty($user->user_date)) {
  51. $this->Logout();
  52. return;
  53. }
  54. $this->user_id = $user->user_id;
  55. $this->user_login = $userInfo[0];
  56. $this->md5_pass = $user->user_pass;
  57. $this->user_level = $user->user_level;
  58. if ($this->user_level == 'admin' || $this->user_level == 'god') $this->admin = true;
  59. if ($this->user_level == 'admin' || $this->user_level == 'god' || $this->user_level == 'devel') $this->devel = true;
  60. if ($this->user_level == 'special' || $this->user_level == 'devel') $this->especial = true;
  61. $this->user_karma = $user->user_karma;
  62. $this->user_email = $user->user_email;
  63. $this->user_avatar = $user->user_avatar;
  64. $this->user_prev_carisma = $user->user_prev_carisma;
  65. $this->user_date = $user->user_date;
  66. if ($this->user_id == 0) $this->thumb= 1;
  67. else $this->thumb = $user->user_thumb;
  68. $this->unread_messages = $this->unread_messages();
  69. $this->authenticated = TRUE;
  70. if ($userInfo[2] != '3') { // Update the cookie to version 3
  71. $this->SetIDCookie(2, true);
  72. } elseif ($this->now - $cookietime > 3600) { // Update the time each hour
  73. $this->SetIDCookie(2, $userInfo[4] > 0 ? true : false);
  74. }
  75. }
  76. }
  77. // Mysql variables to use en join queries
  78. $db->query("set @user_id = $this->user_id, @ip_int = ".$globals['user_ip_int'].
  79. ", @ip_int = ".$globals['user_ip_int'].
  80. ", @enabled_votes = date_sub(now(), interval ". intval($globals['time_enabled_votes']/3600). " hour)");
  81. }
  82. function unread_messages(){
  83. global $db;
  84. return intval($db->get_var("SELECT count(*) FROM mezuak WHERE nori = '".$this->user_id."' AND irakurrita = '0' "));
  85. }
  86. function SetIDCookie($what, $remember) {
  87. global $site_key, $globals;
  88. switch ($what) {
  89. case 0: // Borra cookie, logout
  90. setcookie ("joneame_key", '', $this->now - 3600, $globals['base_url']); // Expiramos el cookie
  91. $this->SetUserCookie(false);
  92. break;
  93. case 1: // Usuario logeado, actualiza el cookie
  94. $this->AddClone();
  95. $this->SetUserCookie(true);
  96. case 2: // Only update the key
  97. // Atencion, cambiar aquĆ­ cuando se cambie el password de base de datos a MD5
  98. if($remember) $time = $this->now + 3600000; // Valid for 1000 hours
  99. else $time = 0;
  100. $strCookie=base64_encode(
  101. $this->user_login.':'
  102. .md5($this->user_email.$site_key.$this->user_login.$this->user_id.$this->now).':'
  103. .'3'.':' // Version number
  104. .$this->now.':'
  105. .$time);
  106. setcookie("joneame_key", $strCookie, $time, $globals['base_url'].'; HttpOnly');
  107. break;
  108. }
  109. }
  110. function Authenticate($username, $hash, $remember=0/* Just this session */) {
  111. global $db;
  112. $dbusername=$db->escape($username);
  113. if (preg_match('/.+@.+\..+/', $username)) {
  114. // It's an email address, get
  115. $user=$db->get_row("SELECT user_id, user_login, user_pass md5_pass, user_level, UNIX_TIMESTAMP(user_validated_date) as user_date, user_karma, user_email FROM users WHERE user_email = '$dbusername'");
  116. } else {
  117. $user=$db->get_row("SELECT user_id, user_login, user_pass md5_pass, user_level, UNIX_TIMESTAMP(user_validated_date) as user_date, user_karma, user_email FROM users WHERE user_login = '$dbusername'");
  118. }
  119. if ($user->user_level == 'disabled' || ! $user->user_date) return false;
  120. if ($user->user_id > 0 && $user->md5_pass == $hash) {
  121. foreach(get_object_vars($user) as $var => $value) $this->$var = $value;
  122. $this->authenticated = true;
  123. $this->SetIDCookie(1, $remember);
  124. return true;
  125. }
  126. return false;
  127. }
  128. function Logout($url='./') {
  129. $this->user_id = 0;
  130. $this->user_login = "";
  131. $this->authenticated = FALSE;
  132. $this->SetIDCookie (0, false);
  133. //header("Pragma: no-cache");
  134. header("Cache-Control: no-cache, must-revalidate");
  135. header("Location: $url");
  136. header("Expires: " . gmdate("r", $this->now - 3600));
  137. header('ETag: "logingout' . $this->now . '"');
  138. die;
  139. }
  140. function Date() {
  141. return (int) $this->user_date;
  142. }
  143. function SetUserCookie($do_login) {
  144. global $globals;
  145. if ($do_login) {
  146. setcookie("joneame", $this->user_login.':'.$this->mnm_user[1], $this->now + 3600000, $globals['base_url']);
  147. } else {
  148. setcookie("joneame", '_:'.$this->mnm_user[1], $this->now + 360000, $globals['base_url']);
  149. }
  150. }
  151. function AddClone() {
  152. if (!empty($this->mnm_user[1])) {
  153. $ids = explode("x", $this->mnm_user[1]);
  154. while(count($ids) > 4) {
  155. array_shift($ids);
  156. }
  157. } else {
  158. $ids = array();
  159. }
  160. array_push($ids, $this->user_id);
  161. $this->mnm_user[1] = implode('x', $ids);
  162. }
  163. function GetClones() {
  164. $clones = array();
  165. foreach (explode('x', $this->mnm_user[1]) as $id) {
  166. $id = intval($id);
  167. if ($id > 0 && $id != $this->user_id) {
  168. array_push($clones, $id);
  169. }
  170. }
  171. return $clones;
  172. }
  173. function GetOAuthIds($service = false) {
  174. global $db;
  175. if (! $this->user_id) return false;
  176. if (! $service) {
  177. $sql = "select service, uid from auths where user_id = $this->user_id";
  178. $res = $db->get_results($sql);
  179. } else {
  180. $sql = "select uid from auths where user_id = $this->user_id and service = '$service'";
  181. $res = $db->get_var($sql);
  182. }
  183. return $res;
  184. }
  185. }
  186. $current_user = new UserAuth();
  187. ?>