7abb69ba50297a23e0491c1800ef1ed3a9c7163a.svn-base 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php namespace Maze\Session;
  2. use Maze\Security\Internal as Security;
  3. use Maze\Config\Load as Config;
  4. @header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');
  5. @session_start();
  6. ini_set('session.cookie_domain', Config::$global['host']['cookie']);
  7. class Save
  8. {
  9. /**
  10. * key
  11. *
  12. * @var string
  13. */
  14. private $key = '';
  15. /**
  16. * prefix
  17. *
  18. * @var string
  19. */
  20. private $prefix = 'maze';
  21. /**
  22. * project
  23. *
  24. * @var string
  25. */
  26. private $project = '';
  27. /**
  28. * method
  29. *
  30. * @var string
  31. */
  32. private $method = 'session';
  33. /**
  34. * __construct
  35. * @param string $key
  36. * @param string $method
  37. *
  38. * @return mixed
  39. */
  40. public function __construct($key = false, $method = 'session')
  41. {
  42. $this->key = $key ? $key : $this->key;
  43. $this->method = $method ? $method : $this->method;
  44. $this->method = ucwords($this->method);
  45. $this->project = defined('MAZE_PROJECT') ? MAZE_PROJECT : 'default';
  46. $this->key($this->key);
  47. return $this;
  48. }
  49. /**
  50. * add
  51. * @param string $key
  52. * @param mixed $value
  53. *
  54. * @return mixed
  55. */
  56. public function add($key, $value, $time = 3600)
  57. {
  58. $key = $this->project . '_' . $key;
  59. $value = Security::encode(base64_encode(serialize($value)), $this->key);
  60. $method = '_set' . $this->method;
  61. $this->$method($key, $value, $time);
  62. return $value;
  63. }
  64. /**
  65. * get
  66. * @param string $key
  67. * @param mixed $type
  68. *
  69. * @return mixed
  70. */
  71. public function get($key, $type = false)
  72. {
  73. $key = $this->project . '_' . $key;
  74. $method = '_get' . $this->method;
  75. $value = $this->$method($key);
  76. $type == false && $value = Security::decode($value, $this->key);
  77. $value = unserialize(base64_decode($value));
  78. return $value;
  79. }
  80. /**
  81. * un
  82. * @param string $key
  83. *
  84. * @return mixed
  85. */
  86. public function un($key)
  87. {
  88. $key = $this->project . '_' . $key;
  89. $method = '_unset' . $this->method;
  90. return $this->$method($key);
  91. }
  92. /**
  93. * key
  94. * @param string $key
  95. *
  96. * @return mixed
  97. */
  98. private function key($key)
  99. {
  100. $this->key = $this->prefix . '_' . $this->method . '_' . $key;
  101. }
  102. /**
  103. * _setCookie
  104. * @param string $key
  105. * @param string $value
  106. *
  107. * @return mixed
  108. */
  109. private function _setCookie($key, $value, $time = 3600)
  110. {
  111. return setCookie($this->prefix . $key, $value, time() + $time, "/", Config::$global['host']['cookie']);
  112. }
  113. /**
  114. * _getCookie
  115. * @param string $key
  116. *
  117. * @return mixed
  118. */
  119. private function _getCookie($key)
  120. {
  121. return isset($_COOKIE[$this->prefix . $key]) ? $_COOKIE[$this->prefix . $key] : false;
  122. }
  123. /**
  124. * _unsetCookie
  125. * @param string $key
  126. *
  127. * @return mixed
  128. */
  129. private function _unsetCookie($key)
  130. {
  131. return setCookie($this->prefix . $key, false, time() - 3600, "/", Config::$global['host']['cookie']);
  132. }
  133. /**
  134. * _setSession
  135. * @param string $key
  136. * @param string $value
  137. *
  138. * @return mixed
  139. */
  140. private function _setSession($key, $value, $time = 3600)
  141. {
  142. return $_SESSION[$this->prefix . $key] = $value;
  143. }
  144. /**
  145. * _getSession
  146. * @param string $key
  147. *
  148. * @return mixed
  149. */
  150. private function _getSession($key)
  151. {
  152. return (isset($_SESSION[$this->prefix . $key]) && $_SESSION[$this->prefix . $key]) ? $_SESSION[$this->prefix . $key] : false;
  153. }
  154. /**
  155. * _unsetSession
  156. * @param string $key
  157. *
  158. * @return mixed
  159. */
  160. private function _unsetSession($key)
  161. {
  162. unset($_SESSION[$this->prefix . $key]);
  163. return true;
  164. }
  165. }