PageRenderTime 44ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/jelix/core/jSession.class.php

http://github.com/jelix/jelix
PHP | 181 lines | 104 code | 28 blank | 49 comment | 14 complexity | cff50e51388efc93d4e3d64515e24b3d MD5 | raw file
Possible License(s): BSD-3-Clause, JSON, GPL-3.0, LGPL-3.0, LGPL-2.1, GPL-2.0
  1. <?php
  2. /**
  3. * @package jelix
  4. * @subpackage core
  5. * @author Julien Issler
  6. * @contributor Laurent Jouanneau
  7. * @copyright 2007-2009 Julien Issler, 2008-2012 Laurent Jouanneau
  8. * @link http://www.jelix.org
  9. * @licence GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
  10. * @since 1.0
  11. */
  12. /**
  13. * session management class of the jelix core
  14. *
  15. * @package jelix
  16. * @subpackage core
  17. * @since 1.0
  18. */
  19. class jSession {
  20. protected static $_params;
  21. /**
  22. * start a session
  23. */
  24. public static function start(){
  25. $params = & jApp::config()->sessions;
  26. // do not start the session if the request is made from the command line or if sessions are disabled in configuration
  27. if (jApp::coord()->request instanceof jCmdLineRequest || !$params['start']) {
  28. return false;
  29. }
  30. //make sure that the session cookie is only for the current application
  31. if (!$params['shared_session'])
  32. session_set_cookie_params ( 0 , jApp::config()->urlengine['basePath']);
  33. if ($params['storage'] != '') {
  34. /* on debian/ubuntu (maybe others), garbage collector launch probability is set to 0
  35. and replaced by a simple cron job which is not enough for jSession (different path, db storage, ...),
  36. so we set it to 1 as PHP's default value */
  37. if(!ini_get('session.gc_probability'))
  38. ini_set('session.gc_probability','1');
  39. switch($params['storage']){
  40. case 'dao':
  41. session_set_save_handler(
  42. array(__CLASS__,'daoOpen'),
  43. array(__CLASS__,'daoClose'),
  44. array(__CLASS__,'daoRead'),
  45. array(__CLASS__,'daoWrite'),
  46. array(__CLASS__,'daoDestroy'),
  47. array(__CLASS__,'daoGarbageCollector')
  48. );
  49. self::$_params = $params;
  50. break;
  51. case 'files':
  52. session_save_path($params['files_path']);
  53. break;
  54. }
  55. }
  56. if($params['name'] !=''){
  57. #ifnot ENABLE_OPTIMIZED_SOURCE
  58. if(!preg_match('#^[a-zA-Z0-9]+$#',$params['name'])){
  59. // regexp check because session name can only be alpha numeric according to the php documentation
  60. throw new jException('jelix~errors.jsession.name.invalid');
  61. }
  62. #endif
  63. session_name($params['name']);
  64. }
  65. if(isset($params['_class_to_load'])) {
  66. foreach($params['_class_to_load'] as $file) {
  67. require_once($file);
  68. }
  69. }
  70. session_start();
  71. return true;
  72. }
  73. /**
  74. * end a session
  75. */
  76. public static function end(){
  77. session_write_close();
  78. return true;
  79. }
  80. protected static function _getDao(){
  81. if(isset(self::$_params['dao_db_profile']) && self::$_params['dao_db_profile']){
  82. $dao = jDao::get(self::$_params['dao_selector'], self::$_params['dao_db_profile']);
  83. }
  84. else{
  85. $dao = jDao::get(self::$_params['dao_selector']);
  86. }
  87. return $dao;
  88. }
  89. /**
  90. * dao handler for session stored in database
  91. */
  92. public static function daoOpen ($save_path, $session_name) {
  93. return true;
  94. }
  95. /**
  96. * dao handler for session stored in database
  97. */
  98. public static function daoClose() {
  99. return true;
  100. }
  101. /**
  102. * dao handler for session stored in database
  103. */
  104. public static function daoRead ($id) {
  105. $session = self::_getDao()->get($id);
  106. if(!$session){
  107. return '';
  108. }
  109. return $session->data;
  110. }
  111. /**
  112. * dao handler for session stored in database
  113. */
  114. public static function daoWrite ($id, $data) {
  115. $dao = self::_getDao();
  116. $session = $dao->get($id);
  117. if(!$session){
  118. $session = jDao::createRecord(self::$_params['dao_selector']);
  119. $session->id = $id;
  120. $session->data = $data;
  121. $now = date('Y-m-d H:i:s');
  122. $session->creation = $now;
  123. $session->access = $now;
  124. $dao->insert($session);
  125. }
  126. else{
  127. $session->data = $data;
  128. $session->access = date('Y-m-d H:i:s');
  129. $dao->update($session);
  130. }
  131. return true;
  132. }
  133. /**
  134. * dao handler for session stored in database
  135. */
  136. public static function daoDestroy ($id) {
  137. if (isset($_COOKIE[session_name()])) {
  138. setcookie(session_name(), '', time()-42000, '/');
  139. }
  140. self::_getDao()->delete($id);
  141. return true;
  142. }
  143. /**
  144. * dao handler for session stored in database
  145. */
  146. public static function daoGarbageCollector ($maxlifetime) {
  147. $date = new jDateTime();
  148. $date->now();
  149. $date->sub(0,0,0,0,0,$maxlifetime);
  150. self::_getDao()->deleteExpired($date->toString(jDateTime::DB_DTFORMAT));
  151. return true;
  152. }
  153. }