elFinderSession.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. /**
  3. * elFinder - file manager for web.
  4. * Session Wrapper Class.
  5. *
  6. * @package elfinder
  7. * @author Naoki Sawada
  8. **/
  9. class elFinderSession implements elFinderSessionInterface
  10. {
  11. /**
  12. * A flag of session started
  13. *
  14. * @var boolean
  15. */
  16. protected $started = false;
  17. /**
  18. * To fix PHP bug that duplicate Set-Cookie header to be sent
  19. *
  20. * @var boolean
  21. * @see https://bugs.php.net/bug.php?id=75554
  22. */
  23. protected $fixCookieRegist = false;
  24. /**
  25. * Array of session keys of this instance
  26. *
  27. * @var array
  28. */
  29. protected $keys = array();
  30. /**
  31. * Is enabled base64encode
  32. *
  33. * @var boolean
  34. */
  35. protected $base64encode = false;
  36. /**
  37. * Default options array
  38. *
  39. * @var array
  40. */
  41. protected $opts = array(
  42. 'base64encode' => false,
  43. 'keys' => array(
  44. 'default' => 'elFinderCaches',
  45. 'netvolume' => 'elFinderNetVolumes'
  46. )
  47. );
  48. /**
  49. * Constractor
  50. *
  51. * @param array $opts The options
  52. *
  53. * @return self Instanse of this class
  54. */
  55. public function __construct($opts)
  56. {
  57. $this->opts = array_merge($this->opts, $opts);
  58. $this->base64encode = !empty($this->opts['base64encode']);
  59. $this->keys = $this->opts['keys'];
  60. if (function_exists('apache_get_version')) {
  61. $this->fixCookieRegist = true;
  62. }
  63. return $this;
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function get($key, $empty = null)
  69. {
  70. $closed = false;
  71. if (!$this->started) {
  72. $closed = true;
  73. $this->start();
  74. }
  75. $data = null;
  76. if ($this->started) {
  77. $session =& $this->getSessionRef($key);
  78. $data = $session;
  79. if ($data && $this->base64encode) {
  80. $data = $this->decodeData($data);
  81. }
  82. }
  83. $checkFn = null;
  84. if (!is_null($empty)) {
  85. if (is_string($empty)) {
  86. $checkFn = 'is_string';
  87. } elseif (is_array($empty)) {
  88. $checkFn = 'is_array';
  89. } elseif (is_object($empty)) {
  90. $checkFn = 'is_object';
  91. } elseif (is_float($empty)) {
  92. $checkFn = 'is_float';
  93. } elseif (is_int($empty)) {
  94. $checkFn = 'is_int';
  95. }
  96. }
  97. if (is_null($data) || ($checkFn && !$checkFn($data))) {
  98. $session = $data = $empty;
  99. }
  100. if ($closed) {
  101. $this->close();
  102. }
  103. return $data;
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function start()
  109. {
  110. if ($this->fixCookieRegist === true) {
  111. // apache2 SAPI has a bug of session cookie register
  112. // see https://bugs.php.net/bug.php?id=75554
  113. // see https://github.com/php/php-src/pull/3231
  114. ini_set('session.use_cookies', 0);
  115. }
  116. if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
  117. if (session_status() !== PHP_SESSION_ACTIVE) {
  118. session_start();
  119. }
  120. } else {
  121. set_error_handler(array($this, 'session_start_error'), E_NOTICE);
  122. session_start();
  123. restore_error_handler();
  124. }
  125. $this->started = session_id() ? true : false;
  126. return $this;
  127. }
  128. /**
  129. * Get variable reference of $_SESSION
  130. *
  131. * @param string $key key of $_SESSION array
  132. *
  133. * @return mixed|null
  134. */
  135. protected function & getSessionRef($key)
  136. {
  137. $session = null;
  138. if ($this->started) {
  139. list($cat, $name) = array_pad(explode('.', $key, 2), 2, null);
  140. if (is_null($name)) {
  141. if (!isset($this->keys[$cat])) {
  142. $name = $cat;
  143. $cat = 'default';
  144. }
  145. }
  146. if (isset($this->keys[$cat])) {
  147. $cat = $this->keys[$cat];
  148. } else {
  149. $name = $cat . '.' . $name;
  150. $cat = $this->keys['default'];
  151. }
  152. if (is_null($name)) {
  153. if (!isset($_SESSION[$cat])) {
  154. $_SESSION[$cat] = null;
  155. }
  156. $session =& $_SESSION[$cat];
  157. } else {
  158. if (!isset($_SESSION[$cat]) || !is_array($_SESSION[$cat])) {
  159. $_SESSION[$cat] = array();
  160. }
  161. if (!isset($_SESSION[$cat][$name])) {
  162. $_SESSION[$cat][$name] = null;
  163. }
  164. $session =& $_SESSION[$cat][$name];
  165. }
  166. }
  167. return $session;
  168. }
  169. /**
  170. * base64 decode of session val
  171. *
  172. * @param $data
  173. *
  174. * @return bool|mixed|string|null
  175. */
  176. protected function decodeData($data)
  177. {
  178. if ($this->base64encode) {
  179. if (is_string($data)) {
  180. if (($data = base64_decode($data)) !== false) {
  181. $data = unserialize($data);
  182. } else {
  183. $data = null;
  184. }
  185. } else {
  186. $data = null;
  187. }
  188. }
  189. return $data;
  190. }
  191. /**
  192. * {@inheritdoc}
  193. */
  194. public function close()
  195. {
  196. if ($this->started) {
  197. if ($this->fixCookieRegist === true) {
  198. // regist cookie only once for apache2 SAPI
  199. $cParm = session_get_cookie_params();
  200. setcookie(session_name(), session_id(), 0, $cParm['path'], $cParm['domain'], $cParm['secure'], $cParm['httponly']);
  201. $this->fixCookieRegist = false;
  202. }
  203. session_write_close();
  204. }
  205. $this->started = false;
  206. return $this;
  207. }
  208. /**
  209. * {@inheritdoc}
  210. */
  211. public function set($key, $data)
  212. {
  213. $closed = false;
  214. if (!$this->started) {
  215. $closed = true;
  216. $this->start();
  217. }
  218. $session =& $this->getSessionRef($key);
  219. if ($this->base64encode) {
  220. $data = $this->encodeData($data);
  221. }
  222. $session = $data;
  223. if ($closed) {
  224. $this->close();
  225. }
  226. return $this;
  227. }
  228. /**
  229. * base64 encode for session val
  230. *
  231. * @param $data
  232. *
  233. * @return string
  234. */
  235. protected function encodeData($data)
  236. {
  237. if ($this->base64encode) {
  238. $data = base64_encode(serialize($data));
  239. }
  240. return $data;
  241. }
  242. /**
  243. * {@inheritdoc}
  244. */
  245. public function remove($key)
  246. {
  247. $closed = false;
  248. if (!$this->started) {
  249. $closed = true;
  250. $this->start();
  251. }
  252. list($cat, $name) = array_pad(explode('.', $key, 2), 2, null);
  253. if (is_null($name)) {
  254. if (!isset($this->keys[$cat])) {
  255. $name = $cat;
  256. $cat = 'default';
  257. }
  258. }
  259. if (isset($this->keys[$cat])) {
  260. $cat = $this->keys[$cat];
  261. } else {
  262. $name = $cat . '.' . $name;
  263. $cat = $this->keys['default'];
  264. }
  265. if (is_null($name)) {
  266. unset($_SESSION[$cat]);
  267. } else {
  268. if (isset($_SESSION[$cat]) && is_array($_SESSION[$cat])) {
  269. unset($_SESSION[$cat][$name]);
  270. }
  271. }
  272. if ($closed) {
  273. $this->close();
  274. }
  275. return $this;
  276. }
  277. /**
  278. * sessioin error handler (Only for suppression of error at session start)
  279. *
  280. * @param $errno
  281. * @param $errstr
  282. */
  283. protected function session_start_error($errno, $errstr)
  284. {
  285. }
  286. }