You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rcube_session_memcache.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------+
  4. | This file is part of the Roundcube Webmail client |
  5. | Copyright (C) 2005-2014, The Roundcube Dev Team |
  6. | Copyright (C) 2011, Kolab Systems AG |
  7. | |
  8. | Licensed under the GNU General Public License version 3 or |
  9. | any later version with exceptions for skins & plugins. |
  10. | See the README file for a full license statement. |
  11. | |
  12. | PURPOSE: |
  13. | Provide database supported session management |
  14. +-----------------------------------------------------------------------+
  15. | Author: Thomas Bruederli <roundcube@gmail.com> |
  16. | Author: Aleksander Machniak <alec@alec.pl> |
  17. | Author: Cor Bosman <cor@roundcu.bet> |
  18. +-----------------------------------------------------------------------+
  19. */
  20. /**
  21. * Class to provide memcache session storage
  22. *
  23. * @package Framework
  24. * @subpackage Core
  25. * @author Thomas Bruederli <roundcube@gmail.com>
  26. * @author Aleksander Machniak <alec@alec.pl>
  27. * @author Cor Bosman <cor@roundcu.be>
  28. */
  29. class rcube_session_memcache extends rcube_session
  30. {
  31. private $memcache;
  32. private $debug;
  33. /**
  34. * @param Object $config
  35. */
  36. public function __construct($config)
  37. {
  38. parent::__construct($config);
  39. $this->memcache = rcube::get_instance()->get_memcache();
  40. $this->debug = $config->get('memcache_debug');
  41. if (!$this->memcache) {
  42. rcube::raise_error(array(
  43. 'code' => 604, 'type' => 'db',
  44. 'line' => __LINE__, 'file' => __FILE__,
  45. 'message' => "Failed to connect to memcached. Please check configuration"),
  46. true, true);
  47. }
  48. // register sessions handler
  49. $this->register_session_handler();
  50. }
  51. /**
  52. * @param $save_path
  53. * @param $session_name
  54. * @return bool
  55. */
  56. public function open($save_path, $session_name)
  57. {
  58. return true;
  59. }
  60. /**
  61. * @return bool
  62. */
  63. public function close()
  64. {
  65. return true;
  66. }
  67. /**
  68. * Handler for session_destroy() with memcache backend
  69. *
  70. * @param $key
  71. * @return bool
  72. */
  73. public function destroy($key)
  74. {
  75. if ($key) {
  76. // #1488592: use 2nd argument
  77. $result = $this->memcache->delete($key, 0);
  78. if ($this->debug) {
  79. $this->debug('delete', $key, null, $result);
  80. }
  81. }
  82. return true;
  83. }
  84. /**
  85. * Read session data from memcache
  86. *
  87. * @param $key
  88. * @return null|string
  89. */
  90. public function read($key)
  91. {
  92. if ($value = $this->memcache->get($key)) {
  93. $arr = unserialize($value);
  94. $this->changed = $arr['changed'];
  95. $this->ip = $arr['ip'];
  96. $this->vars = $arr['vars'];
  97. $this->key = $key;
  98. }
  99. if ($this->debug) {
  100. $this->debug('get', $key, $value);
  101. }
  102. return $this->vars ?: '';
  103. }
  104. /**
  105. * Write data to memcache storage
  106. *
  107. * @param $key
  108. * @param $vars
  109. *
  110. * @return bool
  111. */
  112. public function write($key, $vars)
  113. {
  114. $data = serialize(array('changed' => time(), 'ip' => $this->ip, 'vars' => $vars));
  115. $result = $this->memcache->set($key, $data, MEMCACHE_COMPRESSED, $this->lifetime + 60);
  116. if ($this->debug) {
  117. $this->debug('set', $key, $data, $result);
  118. }
  119. return $result;
  120. }
  121. /**
  122. * Update memcache session data
  123. *
  124. * @param $key
  125. * @param $newvars
  126. * @param $oldvars
  127. *
  128. * @return bool
  129. */
  130. public function update($key, $newvars, $oldvars)
  131. {
  132. $ts = microtime(true);
  133. if ($newvars !== $oldvars || $ts - $this->changed > $this->lifetime / 3) {
  134. $data = serialize(array('changed' => time(), 'ip' => $this->ip, 'vars' => $newvars));
  135. $result = $this->memcache->set($key, $data, MEMCACHE_COMPRESSED, $this->lifetime + 60);
  136. if ($this->debug) {
  137. $this->debug('set', $key, $data, $result);
  138. }
  139. return $result;
  140. }
  141. return true;
  142. }
  143. /**
  144. * Write memcache debug info to the log
  145. */
  146. protected function debug($type, $key, $data = null, $result = null)
  147. {
  148. $line = strtoupper($type) . ' ' . $key;
  149. if ($data !== null) {
  150. $line .= ' ' . $data;
  151. }
  152. rcube::debug('memcache', $line, $result);
  153. }
  154. }