/action_pack/session.php

https://github.com/ameximes/akelos · PHP · 231 lines · 112 code · 16 blank · 103 comment · 15 complexity · dd618c171b4dd98eefb8f937b5e406e3 MD5 · raw file

  1. <?php
  2. # This file is part of the Akelos Framework
  3. # (Copyright) 2004-2010 Bermi Ferrer bermi a t bermilabs com
  4. # See LICENSE and CREDITS for details
  5. /**
  6. * Memcache based session.
  7. *
  8. * This class enables saving sessions into a database or memcache.
  9. * see: config/DEFAULT-DB-sessions.yml or config/DEFAULT-MEMCACHE-sessions.yml
  10. *
  11. * This can
  12. * be usefull for multiple server sites, and to have more
  13. * control over sessions.
  14. *
  15. * <code>
  16. *
  17. * $SessionHandler = AkSession::initHandler();
  18. *
  19. * </code>
  20. *
  21. * @author Bermi Ferrer <bermi at akelos com>
  22. * @author Arno Schneider <arno at bermilabs com>
  23. * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
  24. * @since 0.9
  25. * @version $Revision 0.9 $
  26. */
  27. class AkSession
  28. {
  29. /**
  30. * Session driver
  31. *
  32. * Stores session data using cache handlers
  33. *
  34. * @access protected
  35. * @var object $_driverInstance
  36. */
  37. public $_driverInstance;
  38. public $sessions_enabled;
  39. /**
  40. * Original session value for avoiding hitting the cache system in case nothing has changed
  41. *
  42. * @access private
  43. * @var string $_db
  44. */
  45. public $_original_sess_value = '';
  46. static function initHandler() {
  47. $settings = Ak::getSettings('sessions', false);
  48. return AkSession::lookupStore($settings);
  49. }
  50. static function &lookupStore($options = null) {
  51. static $session_store;
  52. $false = false;
  53. if ($options === true && !empty($session_store)) {
  54. return $session_store;
  55. } else if (is_array($options) &&
  56. isset($options['enabled']) && $options['enabled']==true &&
  57. isset($options['handler']) &&
  58. isset($options['handler']['type'])) {
  59. $type = $options['handler']['type'];
  60. $options = isset($options['handler']['options'])?$options['handler']['options']:array();
  61. } else if (is_string($options) || is_int($options)) {
  62. $type = $options;
  63. $options = array();
  64. } else {
  65. return $false;
  66. }
  67. $session_store = new AkSession();
  68. $session_store->init($options, $type);
  69. if ($session_store->sessions_enabled) {
  70. return $session_store;
  71. }
  72. return $false;
  73. }
  74. public function init($options = array(),$type = null) {
  75. $options = is_int($options) ? array('lifeTime'=>$options) : (is_array($options) ? $options : array());
  76. switch ($type) {
  77. case 1:
  78. // session_save_path();
  79. $this->sessions_enabled = false; // Use PHP session handling
  80. session_start();
  81. return true;
  82. case 2:
  83. $AkDbSession = new AkDbSession();
  84. $AkDbSession->session_life = AK_SESSION_EXPIRE;
  85. session_set_save_handler (
  86. array($AkDbSession, '_open'),
  87. array($AkDbSession, '_close'),
  88. array($AkDbSession, '_read'),
  89. array($AkDbSession, '_write'),
  90. array($AkDbSession, '_destroy'),
  91. array($AkDbSession, '_gc')
  92. );
  93. /*
  94. $this->_driverInstance = new AkAdodbCache();
  95. $res = $this->_driverInstance->init($options);
  96. $this->sessions_enabled = $res;
  97. break;
  98. */
  99. case 3:
  100. $this->_driverInstance = new AkMemcache();
  101. $res = $this->_driverInstance->init($options);
  102. $this->sessions_enabled = $res;
  103. break;
  104. case 4:
  105. $this->_driverInstance = new AkCookieStore();
  106. $this->_driverInstance->init($options);
  107. Ak::setStaticVar('SessionHandler', $this->_driverInstance);
  108. return true;
  109. default:
  110. $this->sessions_enabled = false;
  111. break;
  112. }
  113. if ($this->sessions_enabled) {
  114. $this->sessionLife = $options['lifeTime'];
  115. session_set_save_handler (
  116. array($this, '_open'),
  117. array($this, '_close'),
  118. array($this, '_read'),
  119. array($this, '_write'),
  120. array($this, '_destroy'),
  121. array($this, '_gc')
  122. );
  123. session_start();
  124. }
  125. }
  126. /**
  127. * $this->sessionLife setter
  128. *
  129. * Use this method to set $this->sessionLife value
  130. *
  131. * @access public
  132. * @see get$sessionLife
  133. * @param integer $sessionLife Secconds for the session to expire.
  134. * @return bool Returns true if $this->sessionLife has been set
  135. * correctly.
  136. */
  137. public function setSessionLife($sessionLife) {
  138. $this->sessionLife = $sessionLife;
  139. }
  140. // ---- Protected methods ---- //
  141. /**
  142. * Session open handler
  143. *
  144. * @access protected
  145. * @return boolean
  146. */
  147. public function _open() {
  148. return true;
  149. }
  150. /**
  151. * Session close handler
  152. *
  153. * @access protected
  154. * @return boolean
  155. */
  156. public function _close() {
  157. /**
  158. * @todo Get from cached vars last time garbage collection was made to avoid hitting db
  159. * on every request
  160. */
  161. $this->_gc();
  162. return true;
  163. }
  164. /**
  165. * Session read handler
  166. *
  167. * @access protected
  168. * @param string $id Session Id
  169. * @return string
  170. */
  171. public function _read($id) {
  172. $result = $this->_driverInstance->get($id,'AK_SESSIONS');
  173. return is_null($result) ? '' : (string)$result;
  174. }
  175. /**
  176. * Session write handler
  177. *
  178. * @access protected
  179. * @param string $id
  180. * @param string $data
  181. * @return boolean
  182. */
  183. public function _write($id, $data) {
  184. // We don't want to hit the cache handler if nothing has changed
  185. if($this->_original_sess_value != $data){
  186. $ret = $this->_driverInstance->save($data, $id,'AK_SESSIONS');
  187. if(!$ret){
  188. return false;
  189. }else{
  190. return true;
  191. }
  192. }else {
  193. return true;
  194. }
  195. }
  196. /**
  197. * Session destroy handler
  198. *
  199. * @access protected
  200. * @param string $id
  201. * @return boolean
  202. */
  203. public function _destroy($id) {
  204. return (bool)$this->_driverInstance->remove($id,'AK_SESSIONS');
  205. }
  206. /**
  207. * Session garbage collection handler
  208. *
  209. * @access protected
  210. * @return boolean
  211. */
  212. public function _gc() {
  213. return (bool)$this->_driverInstance->clean('AK_SESSIONS','old');
  214. }
  215. }