PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/user.php

https://bitbucket.org/jonarano/joneame
PHP | 283 lines | 212 code | 58 blank | 13 comment | 40 complexity | 4e5aa249de992fb71c042e3f67d6a86b MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?
  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 User {
  9. const SQL = "user_id as id, user_login as username, user_sex, user_login_register as username_register, user_level as level, UNIX_TIMESTAMP(user_date) as date, user_ip as ip, UNIX_TIMESTAMP(user_modification) as modification, user_pass as pass, user_prev_carisma as previous_carisma, user_email as email, user_email_register as email_register, user_names as names, user_lang as lang, user_karma as karma, user_estado as estado, user_avatar as avatar, user_public_info as public_info, user_url as url, user_thumb as thumb, user_birth as birth";
  10. function User($id=0) {
  11. if ($id>0) {
  12. $this->id = $id;
  13. $this->read();
  14. }
  15. }
  16. function disabled() {
  17. return $this->level == 'disabled' ;
  18. }
  19. function disable() {
  20. global $db;
  21. require_once(mnminclude.'avatars.php');
  22. require_once(mnminclude.'geo.php');
  23. avatars_db_remove($this->id);
  24. avatars_remove_user_files($this->id);
  25. geo_delete('user', $this->id);
  26. // Delete relationships
  27. $db->query("DELETE FROM friends WHERE friend_type='manual' and (friend_from = $this->id or friend_to = $this->id)");
  28. // Delete preferences
  29. $db->query("DELETE FROM prefs WHERE pref_user_id = $this->id");
  30. // Delete posts
  31. //$db->query("delete from posts where post_user_id = $this->id");
  32. $this->username = '--'.$this->id.'--';
  33. $this->email = "$this->id@disabled";
  34. $this->url = '';
  35. $this->level = 'disabled';
  36. $this->sex = 'A ti que te importa';
  37. $this->names = 'disabled';
  38. $this->public_info = '';
  39. $this->adcode = '';
  40. $this->adchannel = '';
  41. $this->phone = '';
  42. $this->avatar = 0;
  43. $this->karma = 7;
  44. $this->estado = '';
  45. $this->thumb= 1;
  46. return $this->store();
  47. }
  48. function store($full_save = true) {
  49. global $db, $globals;
  50. if(!$this->date)
  51. $this->date=$globals['now'];
  52. $user_login = $db->escape($this->username);
  53. $user_level = $this->level;
  54. $user_comment_pref = $this->comment_pref;
  55. $user_karma = $this->karma;
  56. $user_avatar = $this->avatar;
  57. $user_date = $this->date;
  58. $user_ip = $this->ip;
  59. $user_pass = $db->escape($this->pass);
  60. $user_lang = $this->lang;
  61. $user_email = $db->escape($this->email);
  62. $user_names = $db->escape($this->names);
  63. $user_estado = $db->escape($this->estado);
  64. $user_sex = $db->escape($this->sex);
  65. $user_public_info = $db->escape(htmlentities($this->public_info));
  66. $user_url = $db->escape(htmlspecialchars($this->url));
  67. $birth = $db->escape($this->birth);
  68. $user_thumb = $db->escape($this->thumb);
  69. $user_prev_carisma = $this->previous_carisma;
  70. if(!$this->id) {
  71. $db->query("INSERT INTO users (user_login, user_level, user_karma, user_date, user_ip, user_pass, user_lang, user_email, user_names, user_public_info, user_url, user_phone, user_thumb) VALUES ('$user_login', '$user_level', $user_karma, FROM_UNIXTIME($user_date), '$user_ip', '$user_pass', $user_lang, '$user_email', '$user_names', '$user_url', '$user_phone', $user_thumb");
  72. $this->id = $db->insert_id;
  73. } else {
  74. if ($full_save) $modification = ', user_modification = now() ' ;
  75. $db->query("UPDATE users set user_login='$user_login', user_level='$user_level', user_sex='$user_sex', user_karma=$user_karma, user_estado='$user_estado', user_avatar=$user_avatar, user_date=FROM_UNIXTIME($user_date), user_ip='$user_ip', user_pass='$user_pass', user_lang=$user_lang, user_birth='$birth' , user_email='$user_email', user_names='$user_names', user_public_info='$user_public_info', user_url='$user_url',user_prev_carisma=$user_prev_carisma, user_thumb=$user_thumb $modification WHERE user_id=$this->id");
  76. }
  77. }
  78. function read() {
  79. global $db;
  80. if (isset($this->id))
  81. $id = $this->id;
  82. if (isset($id))
  83. $where = "user_id = $id";
  84. elseif (!empty($this->username))
  85. $where = "user_login='".$db->escape(mb_substr($this->username,0,64))."'";
  86. elseif (!empty($this->email))
  87. $where = "user_email='".$db->escape(mb_substr($this->email,0,64))."' and user_level != 'disabled'";
  88. if(!empty($where) && ($result = $db->get_row("SELECT ".User::SQL." FROM users WHERE $where LIMIT 1"))) {
  89. foreach(get_object_vars($result) as $var => $value) $this->$var = $value;
  90. if ($this->level == 'admin' || $this->level == 'god')
  91. $this->admin = true;
  92. else
  93. $this->admin = false;
  94. if ($this->admin || $this->level == 'devel')
  95. $this->devel = true;
  96. else
  97. $this->devel = false;
  98. $this->read = true;
  99. return true;
  100. }
  101. $this->read = false;
  102. return false;
  103. }
  104. function all_stats() {
  105. global $db, $globals;
  106. include_once mnminclude.'annotation.php';
  107. if(!$this->read) $this->read();
  108. $do_cache = ($this->date < $globals['now'] - 86400); // Don't cache for new users
  109. $stats = new Annotation("user_stats-$this->id");
  110. if ($do_cache && $stats->read()
  111. && ($stats->time > $globals['now'] - 7200
  112. || $stats->time > $this->get_last_date())
  113. ) {
  114. $obj = unserialize($stats->text);
  115. } else {
  116. $obj = new stdClass;
  117. $obj->total_votes = (int) $db->get_var("SELECT count(*) FROM votes WHERE vote_type='links' and vote_user_id = $this->id");
  118. $obj->total_links = (int) $db->get_var("SELECT count(*) FROM links WHERE link_author = $this->id and link_sent = 1");
  119. $obj->published_links = (int) $db->get_var("SELECT count(*) FROM links WHERE link_author = $this->id AND link_status = 'published'");
  120. $obj->total_comments = (int) $db->get_var("SELECT count(*) FROM comments WHERE comment_type != 'admin' AND comment_user_id = $this->id ");
  121. $obj->total_posts = (int) $db->get_var("SELECT count(*) FROM posts WHERE post_user_id = $this->id");
  122. $obj->cortos_totales = (int) $db->get_var("SELECT count(*)FROM cortos WHERE por = $this->id AND activado = 1");
  123. $obj->encuestas_totales = (int) $db->get_var("SELECT count(*)FROM encuestas WHERE encuesta_user_id= $this->id ");
  124. if ($do_cache) {
  125. $stats->text = serialize($obj);
  126. $stats->store();
  127. }
  128. }
  129. foreach(get_object_vars($obj) as $var => $value) $this->$var = $value;
  130. }
  131. function ranking() {
  132. global $db;
  133. if(!$this->read)
  134. $this->read();
  135. return (int) $db->get_var("SELECT SQL_CACHE count(*) FROM users WHERE user_karma > $this->karma") + 1;
  136. }
  137. static function get_valid_username($name) {
  138. $name = strip_tags($name);
  139. $name = preg_replace('/&.+?;/', '', $name); // kill entities
  140. $name = preg_replace('/[\s\'\"]/', '_', $name);
  141. if (preg_match('/^\d/', $name)) $name = 'u_' . $name; // Don't let start with a number
  142. return substr($name, 0, 24);
  143. }
  144. function blogs() {
  145. global $db;
  146. return $db->get_var("select count(distinct link_blog) from links where link_author=$this->id");
  147. }
  148. function give_api_key() {
  149. global $site_key;
  150. return substr(md5($this->user.$this->date.$this->pass.$site_key), 0, 10);
  151. }
  152. function get_api_key() {
  153. global $db;
  154. return $db->get_var("SELECT api from api_msg where uid=$this->id ");
  155. }
  156. function get_latlng() {
  157. require_once(mnminclude.'geo.php');
  158. return geo_latlng('user', $this->id);
  159. }
  160. // obtiene la última fecha en la que el usuario realizó alguna acción, y la devuelve en formato DD/MM/YYYY HH:MM:SS
  161. // función por KayDarks <kepazaman@gmail.com>
  162. function get_last_date() {
  163. global $db;
  164. $lastDate = $db->get_var("SELECT UNIX_TIMESTAMP(max(fecha)) as fecha FROM (
  165. SELECT max(post_date) as fecha FROM posts WHERE post_user_id = $this->id
  166. UNION
  167. SELECT max(vote_date) as fecha FROM votes WHERE vote_user_id = $this->id
  168. UNION
  169. SELECT max(comment_date) as fecha FROM comments WHERE comment_user_id = $this->id
  170. UNION
  171. SELECT max(encuesta_start) as fecha FROM encuestas WHERE encuesta_user_id = $this->id
  172. ) ultimo_movimiento");
  173. return $lastDate;
  174. }
  175. }
  176. // Following functions are related to users but not done as a class so can be easily used with User and UserAuth
  177. define('FRIEND_YES', '<img src="'.get_cover_pixel().'" title="'._('amigo').'" class="icon heart-on icono-amigo"/>');
  178. define('FRIEND_NO', '<img src="'.get_cover_pixel().'" title="'._('agregar a la lista de amigos').'" class="icon heart-off icono-amigo"/>');
  179. define('FRIEND_IGNORE', '<img src="'.get_cover_pixel().'" title="'._('ignorado').'" class="icon heart-black icono-amigo"/>');
  180. function friend_exists($from, $to) {
  181. global $db;
  182. if ($from == $to)
  183. return 0;
  184. return round($db->get_var("SELECT SQL_NO_CACHE friend_value FROM friends WHERE friend_type='manual' and friend_from = $from and friend_to = $to"));
  185. }
  186. function friend_insert($from, $to, $value = 1) {
  187. global $db;
  188. if ($from == $to)
  189. return 0;
  190. if (intval($db->get_var("SELECT SQL_NO_CACHE count(*) from users where user_id in ($from, $to)")) != 2)
  191. return false;
  192. return $db->query("REPLACE INTO friends (friend_type, friend_from, friend_to, friend_value) VALUES ('manual', $from, $to, $value)");
  193. }
  194. function friend_delete($from, $to) {
  195. global $db;
  196. return $db->query("DELETE FROM friends WHERE friend_type='manual' and friend_from = $from and friend_to = $to");
  197. }
  198. function friend_add_delete($from, $to) {
  199. if ($from == $to)
  200. return '';
  201. switch (friend_exists($from, $to)) {
  202. case 0:
  203. friend_insert($from, $to);
  204. return FRIEND_YES;
  205. case 1:
  206. friend_insert($from, $to, -1);
  207. return FRIEND_IGNORE;
  208. case -1:
  209. friend_delete($from, $to);
  210. return FRIEND_NO;
  211. }
  212. }
  213. function friend_teaser($from, $to) {
  214. if ($from == $to)
  215. return '';
  216. switch (friend_exists($from, $to)) {
  217. case 0:
  218. return FRIEND_NO;
  219. case 1:
  220. return FRIEND_YES;
  221. case -1:
  222. return FRIEND_IGNORE;
  223. }
  224. }
  225. ?>