人人商城

wesession.class.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. class WeSession implements SessionHandlerInterface {
  8. public static $uniacid;
  9. public static $openid;
  10. public static $expire;
  11. public static function start($uniacid, $openid, $expire = 3600) {
  12. WeSession::$uniacid = $uniacid;
  13. WeSession::$openid = $openid;
  14. WeSession::$expire = $expire;
  15. $cache_setting = $GLOBALS['_W']['config']['setting'];
  16. if (extension_loaded('memcache') && !empty($cache_setting['memcache']['server']) && !empty($cache_setting['memcache']['session'])) {
  17. self::setHandler('memcache');
  18. } elseif (extension_loaded('redis') && !empty($cache_setting['redis']['server']) && !empty($cache_setting['redis']['session'])) {
  19. self::setHandler('redis');
  20. } else {
  21. self::setHandler('mysql');
  22. }
  23. register_shutdown_function('session_write_close');
  24. session_start();
  25. }
  26. public static function setHandler($type = 'mysql') {
  27. $classname = "WeSession{$type}";
  28. if (class_exists($classname)) {
  29. $sess = new $classname;
  30. }
  31. if (version_compare(PHP_VERSION, '5.5') >= 0) {
  32. session_set_save_handler($sess, true);
  33. } else {
  34. session_set_save_handler(
  35. array(&$sess, 'open'),
  36. array(&$sess, 'close'),
  37. array(&$sess, 'read'),
  38. array(&$sess, 'write'),
  39. array(&$sess, 'destroy'),
  40. array(&$sess, 'gc')
  41. );
  42. }
  43. return true;
  44. }
  45. public function open($save_path, $session_name) {
  46. return true;
  47. }
  48. public function close() {
  49. return true;
  50. }
  51. public function read($sessionid) {
  52. return '';
  53. }
  54. public function write($sessionid, $data) {
  55. return true;
  56. }
  57. public function destroy($sessionid) {
  58. return true;
  59. }
  60. public function gc($expire) {
  61. return true;
  62. }
  63. }
  64. class WeSessionMemcache extends WeSession {
  65. protected $session_name;
  66. protected function key($sessionid) {
  67. return $this->session_name . ':' . $sessionid;
  68. }
  69. public function open($save_path, $session_name) {
  70. $this->session_name = $session_name;
  71. if (cache_type() != 'memcache') {
  72. trigger_error('Memcache 扩展不可用或是服务未开启,请将 \$config[\'setting\'][\'memcache\'][\'session\'] 设置为0 ');
  73. return false;
  74. }
  75. return true;
  76. }
  77. public function read($sessionid) {
  78. $row = cache_read($this->key($sessionid));
  79. if (empty($row) || $row['expiretime'] < TIMESTAMP) {
  80. return '';
  81. }
  82. if(is_array($row) && !empty($row['data'])) {
  83. return $row['data'];
  84. }
  85. return '';
  86. }
  87. public function write($sessionid, $data) {
  88. $row = array();
  89. $row['data'] = $data;
  90. $row['uniacid'] = WeSession::$uniacid;
  91. $row['openid'] = WeSession::$openid;
  92. $row['expiretime'] = TIMESTAMP + WeSession::$expire;
  93. return cache_write($this->key($sessionid), $row);
  94. }
  95. public function destroy($sessionid) {
  96. return cache_write($this->key($sessionid), '');
  97. }
  98. }
  99. class WeSessionRedis extends WeSessionMemcache {
  100. public function open($save_path, $session_name) {
  101. $this->session_name = $session_name;
  102. if (cache_type() != 'redis') {
  103. trigger_error('Redis 扩展不可用或是服务未开启,请将 \$config[\'setting\'][\'redis\'][\'session\'] 设置为0 ');
  104. return false;
  105. }
  106. return true;
  107. }
  108. }
  109. class WeSessionMysql extends WeSession {
  110. public function open($save_path, $session_name) {
  111. $tablename = str_replace('`', "'", tablename('core_sessions'));
  112. $status = pdo_fetch("SHOW TABLE STATUS LIKE {$tablename}");
  113. if (strexists($status['Comment'], 'crashed')) {
  114. pdo_run("REPAIR TABLE " . tablename('core_sessions'));
  115. }
  116. return true;
  117. }
  118. public function read($sessionid) {
  119. $sql = 'SELECT * FROM ' . tablename('core_sessions') . ' WHERE `sid`=:sessid AND `expiretime`>:time';
  120. $params = array();
  121. $params[':sessid'] = $sessionid;
  122. $params[':time'] = TIMESTAMP;
  123. $row = pdo_fetch($sql, $params);
  124. if(is_array($row) && !empty($row['data'])) {
  125. return $row['data'];
  126. }
  127. return '';
  128. }
  129. public function write($sessionid, $data) {
  130. $row = array();
  131. $row['sid'] = $sessionid;
  132. $row['uniacid'] = WeSession::$uniacid;
  133. $row['openid'] = WeSession::$openid;
  134. $row['data'] = $data;
  135. $row['expiretime'] = TIMESTAMP + WeSession::$expire;
  136. return pdo_insert('core_sessions', $row, true) >= 1;
  137. }
  138. public function destroy($sessionid) {
  139. $row = array();
  140. $row['sid'] = $sessionid;
  141. return pdo_delete('core_sessions', $row) == 1;
  142. }
  143. public function gc($expire) {
  144. $sql = 'DELETE FROM ' . tablename('core_sessions') . ' WHERE `expiretime`<:expire';
  145. return pdo_query($sql, array(':expire' => TIMESTAMP)) == 1;
  146. }
  147. }