PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/sp_1/system/libraries/Session/drivers/Session_files_driver.php

https://gitlab.com/rezaul007/Hospital-information-bank
PHP | 362 lines | 212 code | 27 blank | 123 comment | 19 complexity | 3a004146b71c08bf2eef2a32589eb589 MD5 | raw file
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP
  6. *
  7. * This content is released under the MIT License (MIT)
  8. *
  9. * Copyright (c) 2014 - 2015, British Columbia Institute of Technology
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. *
  29. * @package CodeIgniter
  30. * @author EllisLab Dev Team
  31. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
  33. * @license http://opensource.org/licenses/MIT MIT License
  34. * @link http://codeigniter.com
  35. * @since Version 3.0.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * CodeIgniter Session Files Driver
  41. *
  42. * @package CodeIgniter
  43. * @subpackage Libraries
  44. * @category Sessions
  45. * @author Andrey Andreev
  46. * @link http://codeigniter.com/user_guide/libraries/sessions.html
  47. */
  48. class CI_Session_files_driver extends CI_Session_driver implements SessionHandlerInterface {
  49. /**
  50. * Save path
  51. *
  52. * @var string
  53. */
  54. protected $_save_path;
  55. /**
  56. * File handle
  57. *
  58. * @var resource
  59. */
  60. protected $_file_handle;
  61. /**
  62. * File name
  63. *
  64. * @var resource
  65. */
  66. protected $_file_path;
  67. /**
  68. * File new flag
  69. *
  70. * @var bool
  71. */
  72. protected $_file_new;
  73. // ------------------------------------------------------------------------
  74. /**
  75. * Class constructor
  76. *
  77. * @param array $params Configuration parameters
  78. * @return void
  79. */
  80. public function __construct(&$params)
  81. {
  82. parent::__construct($params);
  83. if (isset($this->_config['save_path']))
  84. {
  85. $this->_config['save_path'] = rtrim($this->_config['save_path'], '/\\');
  86. ini_set('session.save_path', $this->_config['save_path']);
  87. }
  88. else
  89. {
  90. $this->_config['save_path'] = rtrim(ini_get('session.save_path'), '/\\');
  91. }
  92. }
  93. // ------------------------------------------------------------------------
  94. /**
  95. * Open
  96. *
  97. * Sanitizes the save_path directory.
  98. *
  99. * @param string $save_path Path to session files' directory
  100. * @param string $name Session cookie name
  101. * @return bool
  102. */
  103. public function open($save_path, $name)
  104. {
  105. if ( ! is_dir($save_path))
  106. {
  107. if ( ! mkdir($save_path, 0700, TRUE))
  108. {
  109. throw new Exception("Session: Configured save path '".$this->_config['save_path']."' is not a directory, doesn't exist or cannot be created.");
  110. }
  111. }
  112. elseif ( ! is_writable($save_path))
  113. {
  114. throw new Exception("Session: Configured save path '".$this->_config['save_path']."' is not writable by the PHP process.");
  115. }
  116. $this->_config['save_path'] = $save_path;
  117. $this->_file_path = $this->_config['save_path'].DIRECTORY_SEPARATOR
  118. .$name // we'll use the session cookie name as a prefix to avoid collisions
  119. .($this->_config['match_ip'] ? md5($_SERVER['REMOTE_ADDR']) : '');
  120. return TRUE;
  121. }
  122. // ------------------------------------------------------------------------
  123. /**
  124. * Read
  125. *
  126. * Reads session data and acquires a lock
  127. *
  128. * @param string $session_id Session ID
  129. * @return string Serialized session data
  130. */
  131. public function read($session_id)
  132. {
  133. // This might seem weird, but PHP 5.6 introduces session_reset(),
  134. // which re-reads session data
  135. if ($this->_file_handle === NULL)
  136. {
  137. // Just using fopen() with 'c+b' mode would be perfect, but it is only
  138. // available since PHP 5.2.6 and we have to set permissions for new files,
  139. // so we'd have to hack around this ...
  140. if (($this->_file_new = ! file_exists($this->_file_path.$session_id)) === TRUE)
  141. {
  142. if (($this->_file_handle = fopen($this->_file_path.$session_id, 'w+b')) === FALSE)
  143. {
  144. log_message('error', "Session: File '".$this->_file_path.$session_id."' doesn't exist and cannot be created.");
  145. return FALSE;
  146. }
  147. }
  148. elseif (($this->_file_handle = fopen($this->_file_path.$session_id, 'r+b')) === FALSE)
  149. {
  150. log_message('error', "Session: Unable to open file '".$this->_file_path.$session_id."'.");
  151. return FALSE;
  152. }
  153. if (flock($this->_file_handle, LOCK_EX) === FALSE)
  154. {
  155. log_message('error', "Session: Unable to obtain lock for file '".$this->_file_path.$session_id."'.");
  156. fclose($this->_file_handle);
  157. $this->_file_handle = NULL;
  158. return FALSE;
  159. }
  160. // Needed by write() to detect session_regenerate_id() calls
  161. $this->_session_id = $session_id;
  162. if ($this->_file_new)
  163. {
  164. chmod($this->_file_path.$session_id, 0600);
  165. $this->_fingerprint = md5('');
  166. return '';
  167. }
  168. }
  169. else
  170. {
  171. rewind($this->_file_handle);
  172. }
  173. $session_data = '';
  174. for ($read = 0, $length = filesize($this->_file_path.$session_id); $read < $length; $read += strlen($buffer))
  175. {
  176. if (($buffer = fread($this->_file_handle, $length - $read)) === FALSE)
  177. {
  178. break;
  179. }
  180. $session_data .= $buffer;
  181. }
  182. $this->_fingerprint = md5($session_data);
  183. return $session_data;
  184. }
  185. // ------------------------------------------------------------------------
  186. /**
  187. * Write
  188. *
  189. * Writes (create / update) session data
  190. *
  191. * @param string $session_id Session ID
  192. * @param string $session_data Serialized session data
  193. * @return bool
  194. */
  195. public function write($session_id, $session_data)
  196. {
  197. // If the two IDs don't match, we have a session_regenerate_id() call
  198. // and we need to close the old handle and open a new one
  199. if ($session_id !== $this->_session_id && ( ! $this->close() OR $this->read($session_id) === FALSE))
  200. {
  201. return FALSE;
  202. }
  203. if ( ! is_resource($this->_file_handle))
  204. {
  205. return FALSE;
  206. }
  207. elseif ($this->_fingerprint === md5($session_data))
  208. {
  209. return ($this->_file_new)
  210. ? TRUE
  211. : touch($this->_file_path.$session_id);
  212. }
  213. if ( ! $this->_file_new)
  214. {
  215. ftruncate($this->_file_handle, 0);
  216. rewind($this->_file_handle);
  217. }
  218. if (($length = strlen($session_data)) > 0)
  219. {
  220. for ($written = 0; $written < $length; $written += $result)
  221. {
  222. if (($result = fwrite($this->_file_handle, substr($session_data, $written))) === FALSE)
  223. {
  224. break;
  225. }
  226. }
  227. if ( ! is_int($result))
  228. {
  229. $this->_fingerprint = md5(substr($session_data, 0, $written));
  230. log_message('error', 'Session: Unable to write data.');
  231. return FALSE;
  232. }
  233. }
  234. $this->_fingerprint = md5($session_data);
  235. return TRUE;
  236. }
  237. // ------------------------------------------------------------------------
  238. /**
  239. * Close
  240. *
  241. * Releases locks and closes file descriptor.
  242. *
  243. * @return bool
  244. */
  245. public function close()
  246. {
  247. if (is_resource($this->_file_handle))
  248. {
  249. flock($this->_file_handle, LOCK_UN);
  250. fclose($this->_file_handle);
  251. $this->_file_handle = $this->_file_new = $this->_session_id = NULL;
  252. return TRUE;
  253. }
  254. return TRUE;
  255. }
  256. // ------------------------------------------------------------------------
  257. /**
  258. * Destroy
  259. *
  260. * Destroys the current session.
  261. *
  262. * @param string $session_id Session ID
  263. * @return bool
  264. */
  265. public function destroy($session_id)
  266. {
  267. if ($this->close())
  268. {
  269. return file_exists($this->_file_path.$session_id)
  270. ? (unlink($this->_file_path.$session_id) && $this->_cookie_destroy())
  271. : TRUE;
  272. }
  273. elseif ($this->_file_path !== NULL)
  274. {
  275. clearstatcache();
  276. return file_exists($this->_file_path.$session_id)
  277. ? (unlink($this->_file_path.$session_id) && $this->_cookie_destroy())
  278. : TRUE;
  279. }
  280. return FALSE;
  281. }
  282. // ------------------------------------------------------------------------
  283. /**
  284. * Garbage Collector
  285. *
  286. * Deletes expired sessions
  287. *
  288. * @param int $maxlifetime Maximum lifetime of sessions
  289. * @return bool
  290. */
  291. public function gc($maxlifetime)
  292. {
  293. if ( ! is_dir($this->_config['save_path']) OR ($directory = opendir($this->_config['save_path'])) === FALSE)
  294. {
  295. log_message('debug', "Session: Garbage collector couldn't list files under directory '".$this->_config['save_path']."'.");
  296. return FALSE;
  297. }
  298. $ts = time() - $maxlifetime;
  299. $pattern = sprintf(
  300. '/^%s[0-9a-f]{%d}$/',
  301. preg_quote($this->_config['cookie_name'], '/'),
  302. ($this->_config['match_ip'] === TRUE ? 72 : 40)
  303. );
  304. while (($file = readdir($directory)) !== FALSE)
  305. {
  306. // If the filename doesn't match this pattern, it's either not a session file or is not ours
  307. if ( ! preg_match($pattern, $file)
  308. OR ! is_file($this->_config['save_path'].DIRECTORY_SEPARATOR.$file)
  309. OR ($mtime = filemtime($this->_config['save_path'].DIRECTORY_SEPARATOR.$file)) === FALSE
  310. OR $mtime > $ts)
  311. {
  312. continue;
  313. }
  314. unlink($this->_config['save_path'].DIRECTORY_SEPARATOR.$file);
  315. }
  316. closedir($directory);
  317. return TRUE;
  318. }
  319. }