PageRenderTime 28ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/manage/expressionengine/libraries/Remember.php

https://bitbucket.org/myockey/clearcreek-chapel-website
PHP | 328 lines | 159 code | 61 blank | 108 comment | 14 complexity | 13aeecff74ff66e894f469c776f07022 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * ExpressionEngine - by EllisLab
  4. *
  5. * @package ExpressionEngine
  6. * @author EllisLab Dev Team
  7. * @copyright Copyright (c) 2003 - 2011, EllisLab, Inc.
  8. * @license http://ellislab.com/expressionengine/user-guide/license.html
  9. * @link http://ellislab.com
  10. * @since Version 2.4
  11. * @filesource
  12. */
  13. // ------------------------------------------------------------------------
  14. /**
  15. * ExpressionEngine Core Remember Me Class
  16. *
  17. * @package ExpressionEngine
  18. * @subpackage Core
  19. * @category Core
  20. * @author EllisLab Dev Team
  21. * @link http://ellislab.com
  22. */
  23. class Remember {
  24. private $max_per_site = 5; // remembers per site per user
  25. private $gc_probability = 10; // percentage of logins that gc
  26. protected $table = 'remember_me';
  27. protected $data = NULL;
  28. protected $cookie = 'remember';
  29. protected $cookie_value = FALSE;
  30. protected $ip_address = '';
  31. protected $user_agent = '';
  32. protected $EE;
  33. /**
  34. * Constructor
  35. *
  36. * @access public
  37. */
  38. function __construct()
  39. {
  40. $this->EE =& get_instance();
  41. $this->cookie_value = $this->EE->input->cookie($this->cookie);
  42. $this->ip_address = $this->EE->input->ip_address();
  43. $this->user_agent = substr($this->EE->input->user_agent(), 0, 120);
  44. }
  45. // --------------------------------------------------------------------
  46. /**
  47. * Create a new remember me
  48. *
  49. * @return void
  50. */
  51. public function create($expiration)
  52. {
  53. // this is a good time to check how many they have
  54. $active = $this->EE->db
  55. ->order_by('last_refresh', 'ASC')
  56. ->get_where($this->table, array(
  57. 'member_id' => $this->EE->session->userdata('member_id'),
  58. 'site_id' => $this->EE->config->item('site_id')
  59. ))
  60. ->result();
  61. $this->cookie_value = $this->_generate_id();
  62. $this->data = array(
  63. 'remember_me_id' => $this->cookie_value,
  64. 'member_id' => $this->EE->session->userdata('member_id'),
  65. 'ip_address' => $this->ip_address,
  66. 'user_agent' => $this->user_agent,
  67. 'admin_sess' => $this->EE->session->userdata('admin_sess'),
  68. 'site_id' => $this->EE->config->item('site_id'),
  69. 'expiration' => $this->EE->localize->now + $expiration,
  70. 'last_refresh' => $this->EE->localize->now
  71. );
  72. $this->EE->db->set($this->data);
  73. // If they have too many remembered sessions,
  74. // we replace their oldest one.
  75. if (count($active) >= $this->max_per_site)
  76. {
  77. $this->EE->db->where('remember_me_id', $active[0]->remember_me_id);
  78. $this->EE->db->update($this->table);
  79. }
  80. else
  81. {
  82. $this->EE->db->insert($this->table);
  83. $this->_garbage_collect();
  84. }
  85. $this->_set_cookie($this->data['remember_me_id'], $expiration);
  86. }
  87. // --------------------------------------------------------------------
  88. /**
  89. * Check if a remember me cookie + valid data exists
  90. *
  91. * @return void
  92. */
  93. public function exists()
  94. {
  95. if ($this->data === NULL)
  96. {
  97. $this->data = array();
  98. return $this->_validate_db();
  99. }
  100. return count($this->data);
  101. }
  102. // --------------------------------------------------------------------
  103. /**
  104. * Remember me data accessor
  105. *
  106. * @return void
  107. */
  108. public function data($key)
  109. {
  110. return (isset($this->data[$key])) ? $this->data[$key] : NULL;
  111. }
  112. // --------------------------------------------------------------------
  113. /**
  114. * Clear the current remember me
  115. *
  116. * @return void
  117. */
  118. public function delete()
  119. {
  120. if ($this->cookie_value)
  121. {
  122. $this->EE->db->where('remember_me_id', $this->cookie_value);
  123. $this->EE->db->delete($this->table);
  124. }
  125. $this->data = array();
  126. $this->_delete_cookie();
  127. }
  128. // --------------------------------------------------------------------
  129. /**
  130. * Clear all remember me's except for the current one
  131. *
  132. * Used when changing passwords to disable old
  133. * remember me's that may have been created with
  134. * compromised credentials
  135. *
  136. * @return void
  137. */
  138. public function delete_others()
  139. {
  140. $this->EE->db->where('member_id', $this->EE->session->userdata('member_id'));
  141. if ($this->cookie_value)
  142. {
  143. $this->EE->db->where('remember_me_id !=', $this->cookie_value);
  144. }
  145. $this->EE->db->delete($this->table);
  146. }
  147. // --------------------------------------------------------------------
  148. /**
  149. * Get the remember me data in the db and validate it
  150. *
  151. * @return void
  152. */
  153. public function refresh()
  154. {
  155. if ( ! $this->exists())
  156. {
  157. return;
  158. }
  159. $yesterday = $this->EE->localize->now - 60*60*24;
  160. if ($this->data['last_refresh'] < $yesterday)
  161. {
  162. $id = $this->_generate_id();
  163. // push the expiration date ahead by as much as we've lost
  164. $adjust_expire = $this->data['last_refresh'] - $this->EE->localize->now;
  165. // refresh all the data
  166. $this->data['last_refresh'] = $this->EE->localize->now;
  167. $this->data['remember_me_id'] = $id;
  168. $this->data['expiration'] += $adjust_expire;
  169. $this->EE->db->where('remember_me_id', $this->cookie_value)
  170. ->set($this->data)
  171. ->update($this->table);
  172. $expiration = $this->data['expiration'] - $this->EE->localize->now;
  173. $this->_set_cookie($id, $expiration);
  174. }
  175. }
  176. // --------------------------------------------------------------------
  177. /**
  178. * Get the remember me data in the db and validate it
  179. *
  180. * @return bool
  181. */
  182. protected function _validate_db()
  183. {
  184. if ( ! $this->cookie_value)
  185. {
  186. return FALSE;
  187. }
  188. // grab the db entry
  189. $rem_q = $this->EE->db->get_where($this->table, array(
  190. 'remember_me_id' => $this->cookie_value
  191. ));
  192. if ($rem_q->num_rows() != 1)
  193. {
  194. $this->_delete_cookie();
  195. return FALSE;
  196. }
  197. $rem_data = $rem_q->row_array();
  198. $rem_q->free_result();
  199. // validate browser markers
  200. if ($this->user_agent != $rem_data['user_agent'])
  201. {
  202. $this->_delete_cookie();
  203. return FALSE;
  204. }
  205. // validate time
  206. if ($rem_data['expiration'] < $this->EE->localize->now)
  207. {
  208. $this->_delete_cookie();
  209. return FALSE;
  210. }
  211. // remember the data we grabbed (haha!)
  212. $this->data = $rem_data;
  213. return TRUE;
  214. }
  215. // --------------------------------------------------------------------
  216. /**
  217. * Generates a unique id
  218. *
  219. * @return string random 40 character string
  220. */
  221. protected function _generate_id()
  222. {
  223. return sha1(uniqid(mt_rand(), TRUE));
  224. }
  225. // --------------------------------------------------------------------
  226. /**
  227. * Delete the remember me cookie
  228. *
  229. * @return void
  230. */
  231. protected function _delete_cookie()
  232. {
  233. $this->cookie_value = FALSE;
  234. $this->EE->functions->set_cookie($this->cookie);
  235. }
  236. // --------------------------------------------------------------------
  237. /**
  238. * Set the remember me cookie
  239. *
  240. * @return void
  241. */
  242. protected function _set_cookie($value, $expiration)
  243. {
  244. $this->cookie_value = $value;
  245. $this->EE->functions->set_cookie($this->cookie, $value, $expiration);
  246. }
  247. // --------------------------------------------------------------------
  248. /**
  249. * Garbage collect
  250. *
  251. * @return void
  252. */
  253. protected function _garbage_collect()
  254. {
  255. srand(time());
  256. if ((rand() % 100) < $this->gc_probability)
  257. {
  258. $last_year = $this->EE->localize->now - 60*60*24*365;
  259. $this->EE->db->where('expiration <', $this->EE->localize->now)
  260. ->or_where('last_refresh <', $last_year)
  261. ->delete($this->table);
  262. }
  263. }
  264. }
  265. // END Remember class
  266. /* End of file Remember.php */
  267. /* Location: ./system/expressionengine/libraries/Remember.php */