elFinderSession.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. protected $started = false;
  12. protected $keys = array();
  13. protected $base64encode = false;
  14. protected $opts = array(
  15. 'base64encode' => false,
  16. 'keys' => array(
  17. 'default' => 'elFinderCaches',
  18. 'netvolume' => 'elFinderNetVolumes'
  19. )
  20. );
  21. public function __construct($opts)
  22. {
  23. $this->opts = array_merge($this->opts, $opts);
  24. $this->base64encode = !empty($this->opts['base64encode']);
  25. $this->keys = $this->opts['keys'];
  26. return $this;
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function start()
  32. {
  33. if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
  34. if (session_status() !== PHP_SESSION_ACTIVE) {
  35. session_start();
  36. }
  37. } else {
  38. set_error_handler(array($this, 'session_start_error'), E_NOTICE);
  39. session_start();
  40. restore_error_handler();
  41. }
  42. $this->started = session_id()? true : false;
  43. return $this;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function close()
  49. {
  50. if ($this->started) {
  51. session_write_close();
  52. }
  53. $this->started = false;
  54. return $this;
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function get($key, $empty = null)
  60. {
  61. $closed = false;
  62. if (! $this->started) {
  63. $closed = true;
  64. $this->start();
  65. }
  66. $data = null;
  67. if ($this->started) {
  68. $session =& $this->getSessionRef($key);
  69. $data = $session;
  70. if ($data && $this->base64encode) {
  71. $data = $this->decodeData($data);
  72. }
  73. }
  74. $checkFn = null;
  75. if (! is_null($empty)) {
  76. if (is_string($empty)) {
  77. $checkFn = 'is_string';
  78. } elseif (is_array($empty)) {
  79. $checkFn = 'is_array';
  80. } elseif (is_object($empty)) {
  81. $checkFn = 'is_object';
  82. } elseif (is_float($empty)) {
  83. $checkFn = 'is_float';
  84. } elseif (is_int($empty)) {
  85. $checkFn = 'is_int';
  86. }
  87. }
  88. if (is_null($data) || ($checkFn && ! $checkFn($data))) {
  89. $session = $data = $empty;
  90. }
  91. if ($closed) {
  92. $this->close();
  93. }
  94. return $data;
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function set($key, $data)
  100. {
  101. $closed = false;
  102. if (! $this->started) {
  103. $closed = true;
  104. $this->start();
  105. }
  106. $session =& $this->getSessionRef($key);
  107. if ($this->base64encode) {
  108. $data = $this->encodeData($data);
  109. }
  110. $session = $data;
  111. if ($closed) {
  112. $this->close();
  113. }
  114. return $this;
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function remove($key)
  120. {
  121. $closed = false;
  122. if (! $this->started) {
  123. $closed = true;
  124. $this->start();
  125. }
  126. list($cat, $name) = array_pad(explode('.', $key, 2), 2, null);
  127. if (is_null($name)) {
  128. if (! isset($this->keys[$cat])) {
  129. $name = $cat;
  130. $cat = 'default';
  131. }
  132. }
  133. if (isset($this->keys[$cat])) {
  134. $cat = $this->keys[$cat];
  135. } else {
  136. $name = $cat . '.' . $name;
  137. $cat = $this->keys['default'];
  138. }
  139. if (is_null($name)) {
  140. unset($_SESSION[$cat]);
  141. } else {
  142. if (isset($_SESSION[$cat]) && is_array($_SESSION[$cat])) {
  143. unset($_SESSION[$cat][$name]);
  144. }
  145. }
  146. if ($closed) {
  147. $this->close();
  148. }
  149. return $this;
  150. }
  151. protected function & getSessionRef($key)
  152. {
  153. $session = null;
  154. if ($this->started) {
  155. list($cat, $name) = array_pad(explode('.', $key, 2), 2, null);
  156. if (is_null($name)) {
  157. if (! isset($this->keys[$cat])) {
  158. $name = $cat;
  159. $cat = 'default';
  160. }
  161. }
  162. if (isset($this->keys[$cat])) {
  163. $cat = $this->keys[$cat];
  164. } else {
  165. $name = $cat . '.' . $name;
  166. $cat = $this->keys['default'];
  167. }
  168. if (is_null($name)) {
  169. if (! isset($_SESSION[$cat])) {
  170. $_SESSION[$cat] = null;
  171. }
  172. $session =& $_SESSION[$cat];
  173. } else {
  174. if (! isset($_SESSION[$cat]) || ! is_array($_SESSION[$cat])) {
  175. $_SESSION[$cat] = array();
  176. }
  177. if (! isset($_SESSION[$cat][$name])) {
  178. $_SESSION[$cat][$name] = null;
  179. }
  180. $session =& $_SESSION[$cat][$name];
  181. }
  182. }
  183. return $session;
  184. }
  185. protected function encodeData($data)
  186. {
  187. if ($this->base64encode) {
  188. $data = base64_encode(serialize($data));
  189. }
  190. return $data;
  191. }
  192. protected function decodeData($data)
  193. {
  194. if ($this->base64encode) {
  195. if (is_string($data)) {
  196. if (($data = base64_decode($data)) !== false) {
  197. $data = unserialize($data);
  198. } else {
  199. $data = null;
  200. }
  201. } else {
  202. $data = null;
  203. }
  204. }
  205. return $data;
  206. }
  207. protected function session_start_error($errno , $errstr) {}
  208. }