Session.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php namespace Dever;
  2. use Dever;
  3. use Dever\Helper\Secure;
  4. class Session
  5. {
  6. private static $start = false;
  7. private static $save;
  8. private $key = '';
  9. private $prefix = 'dever_';
  10. private $method = 'session';
  11. public static function start()
  12. {
  13. if (!self::$start) {
  14. @header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');
  15. @ini_set('session.gc_maxlifetime', 86400);
  16. if (isset(Dever::config('setting')['session']) && $session = Dever::config('setting')['session']) {
  17. ;
  18. if (isset($session['host'])) {
  19. $link = 'tcp://'.$session['host'].':'.$session['port'] . '?persistent=1&weight=1&timeout=1&retry_interval=15';
  20. if (isset($session['password']) && $session['password']) {
  21. $link .= '&auth='.$session['password'];
  22. }
  23. @ini_set('session.save_handler', $session['type']);
  24. @ini_set('session.save_path', $link);
  25. } elseif (isset($session['path'])) {
  26. @ini_set('session.save_path', $session['path']);
  27. }
  28. if (isset($session['cookie'])) {
  29. ini_set('session.cookie_domain', $session['cookie']);
  30. }
  31. }
  32. @session_start();
  33. self::$start = true;
  34. }
  35. }
  36. public static function oper($key, $value = false, $timeout = 86400, $type = 'session', $encode = true)
  37. {
  38. if (empty(self::$save[$key])) {
  39. self::$save[$key] = new self(DEVER_APP_NAME, $type);
  40. }
  41. if ($value) {
  42. if ($value == 'delete') {
  43. return self::$save[$key]->un($key);
  44. } else {
  45. return self::$save[$key]->set($key, $value, $timeout, $encode);
  46. }
  47. } else {
  48. return self::$save[$key]->get($key, $encode);
  49. }
  50. }
  51. public function __construct($key = false, $method = 'session')
  52. {
  53. if (DEVER_PROTO == 'command') {
  54. $method = 'file';
  55. } else {
  56. Session::start();
  57. }
  58. if ($key) {
  59. $this->key = $key;
  60. }
  61. if ($method) {
  62. $this->method = $method;
  63. }
  64. $this->method = ucwords($this->method);
  65. $this->key($this->key);
  66. return $this;
  67. }
  68. public function set($key, $value, $time = 3600, $encode = true)
  69. {
  70. $key = DEVER_PROJECT . '_' . $key;
  71. if ($encode) {
  72. $value = Secure::encode(serialize($value), $this->key);
  73. }
  74. $method = '_set' . $this->method;
  75. $this->$method($key, $value, $time);
  76. return $value;
  77. }
  78. public function get($key, $encode = true)
  79. {
  80. $key = DEVER_PROJECT . '_' . $key;
  81. $method = '_get' . $this->method;
  82. $value = $this->$method($key);
  83. if ($encode) {
  84. $value = unserialize(Secure::decode($value, $this->key));
  85. }
  86. return $value;
  87. }
  88. public function un($key)
  89. {
  90. $key = DEVER_PROJECT . '_' . $key;
  91. $method = '_unset' . $this->method;
  92. return $this->$method($key);
  93. }
  94. private function key($key)
  95. {
  96. $this->key = $this->prefix . '_' . $this->method . '_' . $key;
  97. }
  98. private function _setCookie($key, $value, $time = 3600)
  99. {
  100. return setCookie($this->prefix . $key, $value, time() + $time, '/', '');
  101. }
  102. private function _getCookie($key)
  103. {
  104. if (isset($_COOKIE[$this->prefix . $key])) {
  105. return $_COOKIE[$this->prefix . $key];
  106. }
  107. return false;
  108. }
  109. private function _unsetCookie($key)
  110. {
  111. return setCookie($this->prefix . $key, false, time() - 3600, '/', '');
  112. }
  113. private function _setSession($key, $value, $time = 3600)
  114. {
  115. setCookie(session_name(), session_id(), time() + $time, '/', '');
  116. return $_SESSION[$this->prefix . $key] = $value;
  117. }
  118. private function _getSession($key)
  119. {
  120. if ((isset($_SESSION[$this->prefix . $key]) && $_SESSION[$this->prefix . $key])) {
  121. return $_SESSION[$this->prefix . $key];
  122. }
  123. return false;
  124. }
  125. private function _unsetSession($key)
  126. {
  127. unset($_SESSION[$this->prefix . $key]);
  128. return true;
  129. }
  130. private function _initFile()
  131. {
  132. $this->id = md5($this->key);
  133. $this->file = File::get('session/' . $this->id);
  134. if (is_file($this->file)) {
  135. $this->data = unserialize(file_get_contents($this->file));
  136. return;
  137. }
  138. file_put_contents($this->file, null);
  139. }
  140. private function _setFile($key, $value, $time = 3600)
  141. {
  142. $this->_initFile();
  143. $key = $this->prefix . $key;
  144. $this->data[$key] = $value;
  145. file_put_contents($this->file, serialize($this->data));
  146. return $value;
  147. }
  148. private function _getFile($key)
  149. {
  150. $this->_initFile();
  151. $key = $this->prefix . $key;
  152. if (isset($this->data[$key]) && $this->data[$key]) {
  153. return $this->data[$key];
  154. }
  155. return false;
  156. }
  157. private function _unsetFile($key)
  158. {
  159. $this->_initFile();
  160. $key = $this->prefix . $key;
  161. unset($this->data[$key]);
  162. file_put_contents($this->file, serialize($this->data));
  163. return true;
  164. }
  165. }