/classes/session/file.php

https://github.com/magicmarkker/core · PHP · 356 lines · 183 code · 64 blank · 109 comment · 26 complexity · 2d1bbda45428668d8722913e3c597bdc MD5 · raw file

  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.0
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2011 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Fuel\Core;
  13. // --------------------------------------------------------------------
  14. class Session_File extends \Session_Driver
  15. {
  16. /**
  17. * array of driver config defaults
  18. */
  19. protected static $_defaults = array(
  20. 'cookie_name' => 'fuelfid', // name of the session cookie for file based sessions
  21. 'path' => '/tmp', // path where the session files should be stored
  22. 'gc_probability' => 5 // probability % (between 0 and 100) for garbage collection
  23. );
  24. // --------------------------------------------------------------------
  25. public function __construct($config = array())
  26. {
  27. // merge the driver config with the global config
  28. $this->config = array_merge($config, is_array($config['file']) ? $config['file'] : static::$_defaults);
  29. $this->config = $this->_validate_config($this->config);
  30. }
  31. // --------------------------------------------------------------------
  32. /**
  33. * create a new session
  34. *
  35. * @access public
  36. * @return Fuel\Core\Session_File
  37. */
  38. public function create()
  39. {
  40. // create a new session
  41. $this->keys['session_id'] = $this->_new_session_id();
  42. $this->keys['previous_id'] = $this->keys['session_id']; // prevents errors if previous_id has a unique index
  43. $this->keys['ip_hash'] = md5(\Input::ip().\Input::real_ip());
  44. $this->keys['user_agent'] = \Input::user_agent();
  45. $this->keys['created'] = $this->time->get_timestamp();
  46. $this->keys['updated'] = $this->keys['created'];
  47. // create the session record
  48. $this->_write_file($this->keys['session_id'], serialize(array()));
  49. // and set the session cookie
  50. $this->_set_cookie();
  51. return $this;
  52. }
  53. // --------------------------------------------------------------------
  54. /**
  55. * read the session
  56. *
  57. * @access public
  58. * @param boolean, set to true if we want to force a new session to be created
  59. * @return Fuel\Core\Session_Driver
  60. */
  61. public function read($force = false)
  62. {
  63. // get the session cookie
  64. $cookie = $this->_get_cookie();
  65. // if no session cookie was present, create it
  66. if ($cookie === false or $force)
  67. {
  68. $this->create();
  69. }
  70. // read the session file
  71. $payload = $this->_read_file($this->keys['session_id']);
  72. if ($payload === false)
  73. {
  74. // try to find the previous one
  75. $payload = $this->_read_file($this->keys['previous_id']);
  76. if ($payload === false)
  77. {
  78. // cookie present, but session record missing. force creation of a new session
  79. $this->read(true);
  80. return;
  81. }
  82. }
  83. // unpack the payload
  84. $payload = $this->_unserialize($payload);
  85. // session referral?
  86. if (isset($payload['rotated_session_id']))
  87. {
  88. $payload = $this->_read_file($payload['rotated_session_id']);
  89. if ($payload === false)
  90. {
  91. // cookie present, but session record missing. force creation of a new session
  92. $this->read(true);
  93. return;
  94. }
  95. else
  96. {
  97. // update the session
  98. $this->keys['previous_id'] = $this->keys['session_id'];
  99. $this->keys['session_id'] = $payload['rotated_session_id'];
  100. // unpack the payload
  101. $payload = $this->_unserialize($payload);
  102. }
  103. }
  104. if (isset($payload[0])) $this->data = $payload[0];
  105. if (isset($payload[1])) $this->flash = $payload[1];
  106. parent::read();
  107. }
  108. // --------------------------------------------------------------------
  109. /**
  110. * write the session
  111. *
  112. * @access public
  113. * @return Fuel\Core\Session_File
  114. */
  115. public function write()
  116. {
  117. // do we have something to write?
  118. if ( ! empty($this->keys))
  119. {
  120. parent::write();
  121. // rotate the session id if needed
  122. $this->rotate(false);
  123. // session payload
  124. $payload = $this->_serialize(array($this->data, $this->flash));
  125. // create the session file
  126. $this->_write_file($this->keys['session_id'], $payload);
  127. // was the session id rotated?
  128. if ( isset($this->keys['previous_id']) and $this->keys['previous_id'] != $this->keys['session_id'])
  129. {
  130. // point the old session file to the new one, we don't want to lose the session
  131. $payload = $this->_serialize(array('rotated_session_id' => $this->keys['session_id']));
  132. $this->_write_file($this->keys['previous_id'], $payload);
  133. }
  134. $this->_set_cookie();
  135. // do some garbage collection
  136. if (mt_rand(0,100) < $this->config['gc_probability'])
  137. {
  138. if ($handle = opendir($this->config['path']))
  139. {
  140. $expire = $this->time->get_timestamp() - $this->config['expiration_time'];
  141. while (($file = readdir($handle)) !== false)
  142. {
  143. if (filetype($this->config['path'] . $file) == 'file' and
  144. strpos($file, $this->config['cookie_name'].'_') === 0 and
  145. filemtime($this->config['path'] . $file) < $expire)
  146. {
  147. @unlink($this->config['path'] . $file);
  148. }
  149. }
  150. closedir($handle);
  151. }
  152. }
  153. }
  154. return $this;
  155. }
  156. // --------------------------------------------------------------------
  157. /**
  158. * destroy the current session
  159. *
  160. * @access public
  161. * @return Fuel\Core\Session_File
  162. */
  163. public function destroy()
  164. {
  165. // do we have something to destroy?
  166. if ( ! empty($this->keys))
  167. {
  168. // delete the session file
  169. $file = $this->config['path'].$this->config['cookie_name'].'_'.$this->keys['session_id'];
  170. if (file_exists($file))
  171. {
  172. unlink($file);
  173. }
  174. }
  175. // reset the stored session data
  176. $this->keys = $this->flash = $this->data = array();
  177. return $this;
  178. }
  179. // --------------------------------------------------------------------
  180. /**
  181. * Writes the session file
  182. *
  183. * @access private
  184. * @return boolean, true if it was an existing session, false if not
  185. */
  186. protected function _write_file($session_id, $payload)
  187. {
  188. // create the session file
  189. $file = $this->config['path'].$this->config['cookie_name'].'_'.$session_id;
  190. $exists = file_exists($file);
  191. $handle = fopen($file,'c');
  192. if ($handle)
  193. {
  194. // wait for a lock
  195. while(!flock($handle, LOCK_EX));
  196. // erase existing contents
  197. ftruncate($handle, 0);
  198. // write the session data
  199. fwrite($handle, $payload);
  200. //release the lock
  201. flock($handle, LOCK_UN);
  202. // close the file
  203. fclose($handle);
  204. }
  205. return $exists;
  206. }
  207. // --------------------------------------------------------------------
  208. /**
  209. * Reads the session file
  210. *
  211. * @access private
  212. * @return mixed, the payload if the file exists, or false if not
  213. */
  214. protected function _read_file($session_id)
  215. {
  216. $payload = false;
  217. $file = $this->config['path'].$this->config['cookie_name'].'_'.$session_id;
  218. if (file_exists($file))
  219. {
  220. $handle = fopen($file,'r');
  221. if ($handle)
  222. {
  223. // wait for a lock
  224. while(!flock($handle, LOCK_EX));
  225. // read the session data
  226. $payload = fread($handle, filesize($file));
  227. //release the lock
  228. flock($handle, LOCK_UN);
  229. // close the file
  230. fclose($handle);
  231. }
  232. }
  233. return $payload;
  234. }
  235. // --------------------------------------------------------------------
  236. /**
  237. * validate a driver config value
  238. *
  239. * @param array array with configuration values
  240. * @access public
  241. * @return array validated and consolidated config
  242. */
  243. public function _validate_config($config)
  244. {
  245. $validated = array();
  246. foreach ($config as $name => $item)
  247. {
  248. // filter out any driver config
  249. if (!is_array($item))
  250. {
  251. switch ($name)
  252. {
  253. case 'cookie_name':
  254. if ( empty($item) OR ! is_string($item))
  255. {
  256. $item = 'fuelfid';
  257. }
  258. break;
  259. case 'path':
  260. // do we have a path?
  261. if ( empty($item) OR ! is_dir($item))
  262. {
  263. throw new \FuelException('You have specify a valid path to store the session data files.');
  264. }
  265. // and can we write to it?
  266. if ( ! is_writable($item))
  267. {
  268. throw new \FuelException('The webserver doesn\'t have write access to the path to store the session data files.');
  269. }
  270. // update the path, and add the trailing slash
  271. $item = realpath($item).'/';
  272. break;
  273. case 'gc_probability':
  274. // do we have a path?
  275. if ( ! is_numeric($item) OR $item < 0 OR $item > 100)
  276. {
  277. // default value: 5%
  278. $item = 5;
  279. }
  280. break;
  281. default:
  282. // no config item for this driver
  283. break;
  284. }
  285. // global config, was validated in the driver
  286. $validated[$name] = $item;
  287. }
  288. }
  289. // validate all global settings as well
  290. return parent::_validate_config($validated);
  291. }
  292. }