chamilo_session.class.php 4.2 KB

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