PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/model/visitor.php

https://github.com/NeoRazorX/feedstorm
PHP | 406 lines | 338 code | 50 blank | 18 comment | 62 complexity | 32e0a549d76fcc8410178e62a15a955c MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /*
  3. * This file is part of FeedStorm
  4. * Copyright (C) 2014 Carlos Garcia Gomez neorazorx@gmail.com
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. require_once 'base/fs_model.php';
  20. require_once 'model/feed_story.php';
  21. require_once 'model/story.php';
  22. require_once 'model/story_visit.php';
  23. require_once 'model/suscription.php';
  24. class visitor extends fs_model
  25. {
  26. public $nick;
  27. public $ip;
  28. public $user_agent;
  29. public $first_login_date;
  30. public $last_login_date;
  31. public $admin;
  32. public $num_suscriptions;
  33. public $num_stories;
  34. public $num_editions;
  35. public $num_comments;
  36. public $num_visits;
  37. public $points;
  38. public $extra_points;
  39. public $noob;
  40. public $need_save;
  41. private $suscriptions;
  42. public function __construct($k=FALSE)
  43. {
  44. parent::__construct('visitors');
  45. $this->id = NULL;
  46. $this->nick = $this->random_string(12);
  47. $this->ip = 'unknown';
  48. $this->user_agent = 'unknown';
  49. $this->first_login_date = time();
  50. $this->last_login_date = 0;
  51. $this->admin = FALSE;
  52. $this->num_suscriptions = 0;
  53. $this->num_stories = 0;
  54. $this->num_editions = 0;
  55. $this->num_comments = 0;
  56. $this->num_visits = 0;
  57. $this->points = 0;
  58. $this->extra_points = 0;
  59. $this->noob = TRUE;
  60. $this->need_save = FALSE;
  61. if($k)
  62. {
  63. $this->id = $k['_id'];
  64. $this->nick = $k['nick'];
  65. if( isset($k['ip']) )
  66. $this->ip = $k['ip'];
  67. $this->user_agent = $k['user_agent'];
  68. $this->first_login_date = $k['first_login_date'];
  69. $this->last_login_date = $k['last_login_date'];
  70. if( isset($k['admin']) )
  71. $this->admin = $k['admin'];
  72. $this->num_suscriptions = $k['num_suscriptions'];
  73. if( isset($k['num_stories']) )
  74. $this->num_stories = $k['num_stories'];
  75. if( isset($k['num_editions']) )
  76. $this->num_editions = $k['num_editions'];
  77. if( isset($k['num_comments']) )
  78. $this->num_comments = $k['num_comments'];
  79. if( isset($k['num_visits']) )
  80. $this->num_visits = $k['num_visits'];
  81. if( isset($k['points']) )
  82. $this->points = $k['points'];
  83. if( isset($k['extra_points']) )
  84. $this->extra_points = $k['extra_points'];
  85. $this->noob = FALSE;
  86. }
  87. }
  88. public function install_indexes()
  89. {
  90. $this->collection->ensureIndex('last_login_date');
  91. }
  92. public function login_date()
  93. {
  94. return Date('Y-m-d H:m', $this->last_login_date);
  95. }
  96. public function login_timesince()
  97. {
  98. return $this->time2timesince($this->last_login_date);
  99. }
  100. public function age()
  101. {
  102. $time = $this->last_login_date - $this->first_login_date;
  103. if($time <= 60)
  104. return $time.' segundos';
  105. else if(60 < $time && $time <= 3600)
  106. return round($time/60,0).' minutos';
  107. else if(3600 < $time && $time <= 86400)
  108. return round($time/3600,0).' horas';
  109. else if(86400 < $time && $time <= 604800)
  110. return round($time/86400,0).' dias';
  111. else if(604800 < $time && $time <= 2592000)
  112. return round($time/604800,0).' semanas';
  113. else if(2592000 < $time && $time <= 29030400)
  114. return round($time/2592000,0).' meses';
  115. else if($time > 29030400)
  116. return 'mĂĄs de un aĂąo';
  117. }
  118. public function mobile()
  119. {
  120. return (strpos(strtolower($this->user_agent), 'mobile') !== FALSE OR strpos(strtolower($this->user_agent), 'android') !== FALSE);
  121. }
  122. public function human()
  123. {
  124. if($this->user_agent == 'unknown')
  125. return FALSE;
  126. else if( strpos(strtolower($this->user_agent), 'mozilla') === FALSE AND strpos(strtolower($this->user_agent), 'opera') === FALSE )
  127. return FALSE;
  128. else if( strpos(strtolower($this->user_agent), 'href="http') !== FALSE )
  129. return FALSE;
  130. else if( strpos(strtolower($this->user_agent), 'bot') !== FALSE )
  131. return FALSE;
  132. else if( strpos(strtolower($this->user_agent), 'spider') !== FALSE )
  133. return FALSE;
  134. else if( strpos(strtolower($this->user_agent), 'wget') !== FALSE )
  135. return FALSE;
  136. else if( strpos(strtolower($this->user_agent), 'curl') !== FALSE )
  137. return FALSE;
  138. else if( strpos(strtolower($this->user_agent), 'sistrix') !== FALSE)
  139. return FALSE;
  140. else
  141. return TRUE;
  142. }
  143. public function login()
  144. {
  145. $this->ip = 'unknown';
  146. if( isset($_SERVER['REMOTE_ADDR']) )
  147. $this->ip = $_SERVER['REMOTE_ADDR'];
  148. $this->user_agent = 'unknown';
  149. if( isset($_SERVER['HTTP_USER_AGENT']) )
  150. $this->user_agent = $_SERVER['HTTP_USER_AGENT'];
  151. if( time() > $this->last_login_date + 300 )
  152. {
  153. $this->last_login_date = time();
  154. $this->need_save = TRUE;
  155. }
  156. if( $this->num_editions < (2*$this->num_visits) AND $this->num_stories < (2*$this->num_visits) )
  157. $this->points = intval( ($this->num_comments+$this->num_editions+$this->num_stories)/3 ) + $this->extra_points;
  158. else
  159. $this->points = 0;
  160. }
  161. public function last_visits()
  162. {
  163. $sv = new story_visit();
  164. $num_visits = $sv->count4visitor($this->id);
  165. if($this->num_visits != $num_visits)
  166. {
  167. $this->num_visits = $num_visits;
  168. $this->need_save = TRUE;
  169. $this->save();
  170. }
  171. return $sv->all4visitor($this->id);
  172. }
  173. public function suscriptions()
  174. {
  175. if( !isset($this->suscriptions) )
  176. {
  177. $sus0 = new suscription();
  178. $this->suscriptions = $sus0->all4visitor($this->id);
  179. if($this->num_suscriptions != count($this->suscriptions) )
  180. {
  181. $this->num_suscriptions = count($this->suscriptions);
  182. $this->need_save = TRUE;
  183. $this->save();
  184. }
  185. }
  186. return $this->suscriptions;
  187. }
  188. public function browser()
  189. {
  190. return $this->true_text_break($this->user_agent, 70);
  191. }
  192. public function last_stories()
  193. {
  194. if( $this->suscriptions() )
  195. {
  196. $fids = array();
  197. foreach($this->suscriptions as $sus)
  198. $fids[] = $sus->feed_id;
  199. $feed_story = new feed_story();
  200. $stories = array();
  201. foreach($feed_story->last4feeds($fids) as $fs)
  202. {
  203. if( $fs->story() )
  204. $stories[] = $fs->story();
  205. }
  206. return $stories;
  207. }
  208. else
  209. {
  210. $story = new story();
  211. return $story->published_stories();
  212. }
  213. }
  214. public function get($id)
  215. {
  216. $this->add2history(__CLASS__.'::'.__FUNCTION__);
  217. try
  218. {
  219. $data = $this->collection->findone( array('_id' => new MongoId($id)) );
  220. if($data)
  221. return new visitor($data);
  222. else
  223. return FALSE;
  224. }
  225. catch(Exception $e)
  226. {
  227. $this->new_error($e);
  228. return FALSE;
  229. }
  230. }
  231. public function exists()
  232. {
  233. if( is_null($this->id) )
  234. return FALSE;
  235. else
  236. {
  237. $this->add2history(__CLASS__.'::'.__FUNCTION__);
  238. $data = $this->collection->findone( array('_id' => $this->id) );
  239. if($data)
  240. return TRUE;
  241. else
  242. return FALSE;
  243. }
  244. }
  245. public function save()
  246. {
  247. if( $this->need_save AND $this->human() )
  248. {
  249. if( mt_rand(0, 1) == 0 )
  250. $this->last_visits();
  251. $data = array(
  252. 'nick' => $this->nick,
  253. 'ip' => $this->ip,
  254. 'user_agent' => $this->user_agent,
  255. 'first_login_date' => $this->first_login_date,
  256. 'last_login_date' => $this->last_login_date,
  257. 'admin' => $this->admin,
  258. 'num_suscriptions' => $this->num_suscriptions,
  259. 'num_stories' => $this->num_stories,
  260. 'num_editions' => $this->num_editions,
  261. 'num_comments' => $this->num_comments,
  262. 'num_visits' => $this->num_visits,
  263. 'points' => $this->points,
  264. 'extra_points' => $this->extra_points
  265. );
  266. if( $this->exists() )
  267. {
  268. $this->add2history(__CLASS__.'::'.__FUNCTION__.'@update');
  269. $filter = array('_id' => $this->id);
  270. $this->collection->update($filter, $data);
  271. }
  272. else
  273. {
  274. $this->add2history(__CLASS__.'::'.__FUNCTION__.'@insert');
  275. $this->collection->insert($data);
  276. $this->id = $data['_id'];
  277. }
  278. return TRUE;
  279. }
  280. else
  281. return FALSE;
  282. }
  283. public function force_insert($id)
  284. {
  285. $this->set_id($id);
  286. $data = array(
  287. '_id' => $this->id,
  288. 'nick' => $this->nick,
  289. 'ip' => $this->ip,
  290. 'user_agent' => $this->user_agent,
  291. 'first_login_date' => $this->first_login_date,
  292. 'last_login_date' => time(),
  293. 'admin' => $this->admin,
  294. 'num_suscriptions' => $this->num_suscriptions,
  295. 'num_stories' => $this->num_stories,
  296. 'num_editions' => $this->num_editions,
  297. 'num_comments' => $this->num_comments,
  298. 'num_visits' => $this->num_visits,
  299. 'points' => $this->points,
  300. 'extra_points' => $this->extra_points
  301. );
  302. if( $this->exists() )
  303. {
  304. $this->add2history(__CLASS__.'::'.__FUNCTION__.'@update');
  305. $filter = array('_id' => $this->id);
  306. $this->collection->update($filter, $data);
  307. }
  308. else
  309. {
  310. $this->add2history(__CLASS__.'::'.__FUNCTION__.'@insert');
  311. $this->collection->insert($data);
  312. }
  313. }
  314. public function delete()
  315. {
  316. $this->add2history(__CLASS__.'::'.__FUNCTION__);
  317. $this->collection->remove( array('_id' => $this->id) );
  318. /// eliminamos las suscripciones
  319. $sus0 = new suscription();
  320. $sus0->delete4visitor($this->id);
  321. }
  322. public function all()
  323. {
  324. $this->add2history(__CLASS__.'::'.__FUNCTION__);
  325. $vlist = array();
  326. foreach($this->collection->find() as $v)
  327. $vlist[] = new visitor($v);
  328. return $vlist;
  329. }
  330. public function last($num=FS_MAX_STORIES)
  331. {
  332. $this->add2history(__CLASS__.'::'.__FUNCTION__);
  333. $vlist = array();
  334. foreach($this->collection->find()->sort(array('last_login_date'=>-1))->limit($num) as $v)
  335. $vlist[] = new visitor($v);
  336. return $vlist;
  337. }
  338. public function usuals($num=FS_MAX_STORIES)
  339. {
  340. $this->add2history(__CLASS__.'::'.__FUNCTION__);
  341. $vlist = array();
  342. foreach($this->collection->find()->sort(array('num_visits'=>-1))->limit($num) as $v)
  343. $vlist[] = new visitor($v);
  344. return $vlist;
  345. }
  346. public function cron_job()
  347. {
  348. echo "\nEliminamos usuarios inactivos...";
  349. foreach($this->collection->find( array('last_login_date' => array('$lt'=>time()-FS_MAX_AGE)) ) as $v)
  350. {
  351. $visit0 = new visitor($v);
  352. $visit0->delete();
  353. echo '.';
  354. }
  355. }
  356. }