PageRenderTime 25ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/studio-42/elfinder/php/elFinderSession.php

https://gitlab.com/itlboy/yii2-starter-installed
PHP | 237 lines | 186 code | 29 blank | 22 comment | 44 complexity | 590924cd9890597b85b83973ee626955 MD5 | raw file
  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. }