chamilo_session.class.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * Chamilo session (i.e. the session that maintains the connection open after usr login)
  4. *
  5. * Usage:
  6. *
  7. *
  8. * use ChamiloSession as Session;
  9. *
  10. * Session::read('name');
  11. *
  12. * Or
  13. *
  14. * Chamilo::session()->...
  15. * session()->...
  16. *
  17. * @license see /license.txt
  18. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva
  19. */
  20. /**
  21. * @todo use session symfony component
  22. * @todo replace all $_SESSION calls with this class.
  23. * @todo remove System\Session class
  24. * ChamiloSession class definition
  25. */
  26. class ChamiloSession extends System\Session
  27. {
  28. const NAME = 'ch_sid';
  29. /**
  30. * Generate new session instance
  31. * @return ChamiloSession
  32. */
  33. static function instance()
  34. {
  35. static $result = null;
  36. if (empty($result)) {
  37. $result = new ChamiloSession();
  38. }
  39. return $result;
  40. }
  41. /**
  42. * Returns the session lifetime
  43. * @return int The session lifetime as defined in the config file, in seconds
  44. */
  45. static function session_lifetime()
  46. {
  47. global $_configuration;
  48. return $_configuration['session_lifetime'];
  49. }
  50. /**
  51. * Starts the Chamilo session.
  52. *
  53. * The default lifetime for session is set here. It is not possible to have it
  54. * as a database setting as it is used before the database connection has been made.
  55. * It is taken from the configuration file, and if it doesn't exist there, it is set
  56. * to 360000 seconds
  57. *
  58. * @author Olivier Brouckaert
  59. * @param string variable - the variable name to save into the session
  60. * @return void
  61. */
  62. static function start($already_installed = true)
  63. {
  64. global $_configuration;
  65. /*
  66. * Prevent Session fixation bug fixes
  67. * See http://support.chamilo.org/issues/3600
  68. * http://php.net/manual/en/session.configuration.php
  69. * @todo use session_set_cookie_params with some custom admin parameters
  70. */
  71. //session.cookie_lifetime
  72. //the session ID is only accepted from a cookie
  73. ini_set('session.use_only_cookies', 1);
  74. //HTTPS only if possible
  75. //ini_set('session.cookie_secure', 1);
  76. //session ID in the cookie is only readable by the server
  77. ini_set('session.cookie_httponly', 1);
  78. //Use entropy file
  79. //session.entropy_file
  80. //ini_set('session.entropy_length', 128);
  81. //Do not include the identifier in the URL, and not to read the URL for
  82. // identifiers.
  83. ini_set('session.use_trans_sid', 0);
  84. session_name(self::NAME);
  85. session_start();
  86. $session = self::instance();
  87. if ($already_installed) {
  88. if (!isset($session['checkChamiloURL'])) {
  89. $session['checkChamiloURL'] = api_get_path(WEB_PATH);
  90. } elseif ($session['checkChamiloURL'] != api_get_path(WEB_PATH)) {
  91. self::clear();
  92. }
  93. }
  94. /*if (!$session->has('starttime') && !$session->is_expired()) {
  95. $session->write('starttime', time());
  96. }*/
  97. // If the session time has expired, refresh the starttime value,
  98. // so we're starting to count down from a later time
  99. if ( $session->has('starttime') && $session->is_expired()) {
  100. $session->destroy();
  101. } else {
  102. //error_log('Time not expired, extend session for a bit more');
  103. $session->write('starttime', time());
  104. }
  105. }
  106. /**
  107. * Session start time: that is the last time the user loaded a page (before this time)
  108. * @return int timestamp
  109. */
  110. function start_time()
  111. {
  112. return self::read('starttime');
  113. }
  114. /**
  115. * Session end time: when the session expires. This is made of the last page
  116. * load time + a number of seconds
  117. * @return int UNIX timestamp (server's timezone)
  118. */
  119. function end_time()
  120. {
  121. $start_time = $this->start_time();
  122. $lifetime = self::session_lifetime();
  123. return $start_time + $lifetime;
  124. }
  125. /**
  126. * Returns whether the session is expired
  127. * @return bool True if the session is expired, false if it is still valid
  128. */
  129. public function is_expired()
  130. {
  131. return $this->end_time() < time();
  132. }
  133. }