PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/php/main/inc/lib/chamilo_session.class.php

https://bitbucket.org/frchico/chamilo_openshift
PHP | 215 lines | 86 code | 22 blank | 107 comment | 13 complexity | 5025ab5f19bea6357e779ae3e7b77e7c MD5 | raw file
  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. * ChamiloSession class definition
  22. */
  23. class ChamiloSession extends System\Session
  24. {
  25. const NAME = 'ch_sid';
  26. /**
  27. * Generate new session instance
  28. * @return ChamiloSession
  29. */
  30. static function instance()
  31. {
  32. static $result = null;
  33. if (empty($result)) {
  34. $result = new ChamiloSession();
  35. }
  36. return $result;
  37. }
  38. /**
  39. * Returns the session lifetime
  40. * @return int The session lifetime as defined in the config file, in seconds
  41. */
  42. static function session_lifetime()
  43. {
  44. global $_configuration;
  45. return $_configuration['session_lifetime'];
  46. }
  47. /**
  48. * Returns whether the sessions are stored in the database (or not)
  49. * @return bool True if session data are stored in the database, false if they're stored on disk
  50. * @assert (null) === false
  51. */
  52. static function session_stored_in_db()
  53. {
  54. return self::read('session_stored_in_db', false);
  55. }
  56. /**
  57. * Starts the Chamilo session.
  58. *
  59. * The default lifetime for session is set here. It is not possible to have it
  60. * as a database setting as it is used before the database connection has been made.
  61. * It is taken from the configuration file, and if it doesn't exist there, it is set
  62. * to 360000 seconds
  63. *
  64. * @author Olivier Brouckaert
  65. * @param string variable - the variable name to save into the session
  66. * @return void
  67. */
  68. static function start($already_installed = true)
  69. {
  70. global $_configuration;
  71. /* Causes too many problems and is not configurable dynamically.
  72. if ($already_installed) {
  73. $session_lifetime = 360000;
  74. if (isset($_configuration['session_lifetime'])) {
  75. $session_lifetime = $_configuration['session_lifetime'];
  76. }
  77. //session_set_cookie_params($session_lifetime,api_get_path(REL_PATH));
  78. }
  79. */
  80. if (self::session_stored_in_db() && function_exists('session_set_save_handler')) {
  81. $handler = new SessionHandler();
  82. @session_set_save_handler(array(& $handler, 'open'), array(& $handler, 'close'), array(& $handler, 'read'), array(& $handler, 'write'), array(& $handler, 'destroy'), array(& $handler, 'garbage'));
  83. }
  84. /*
  85. * Prevent Session fixation bug fixes
  86. * See http://support.chamilo.org/issues/3600
  87. * http://php.net/manual/en/session.configuration.php
  88. * @todo use session_set_cookie_params with some custom admin parameters
  89. */
  90. //session.cookie_lifetime
  91. //the session ID is only accepted from a cookie
  92. ini_set('session.use_only_cookies', 1);
  93. //HTTPS only if possible
  94. //ini_set('session.cookie_secure', 1);
  95. //session ID in the cookie is only readable by the server
  96. ini_set('session.cookie_httponly', 1);
  97. //Use entropy file
  98. //session.entropy_file
  99. //ini_set('session.entropy_length', 128);
  100. //Do not include the identifier in the URL, and not to read the URL for
  101. // identifiers.
  102. ini_set('session.use_trans_sid', 0);
  103. session_name(self::NAME);
  104. session_start();
  105. $session = self::instance();
  106. if ($already_installed) {
  107. if (!isset($session['checkChamiloURL'])) {
  108. $session['checkChamiloURL'] = api_get_path(WEB_PATH);
  109. } else if ($session['checkChamiloURL'] != api_get_path(WEB_PATH)) {
  110. self::clear();
  111. }
  112. }
  113. /*if (!$session->has('starttime') || $session->is_valid()) {
  114. $session->write('starttime', time());
  115. }*/
  116. // if the session time has expired, refresh the starttime value, so we're starting to count down from a later time
  117. if ( $session->has('starttime') && $session->is_valid()) {
  118. //error_log('Time expired, cancel session');
  119. $session->destroy();
  120. } else {
  121. //error_log('Time not expired, extend session for a bit more');
  122. $session->write('starttime', time());
  123. }
  124. }
  125. /**
  126. * Session start time: that is the last time the user loaded a page (before this time)
  127. * @return int timestamp
  128. */
  129. function start_time()
  130. {
  131. return self::read('starttime');
  132. }
  133. /**
  134. * Session end time: when the session expires. This is made of the last page
  135. * load time + a number of seconds
  136. * @return int UNIX timestamp (server's timezone)
  137. */
  138. function end_time()
  139. {
  140. $start_time = $this->start_time();
  141. $lifetime = self::session_lifetime();
  142. return $start_time + $lifetime;
  143. }
  144. /**
  145. * Returns true if the session is stalled. I.e. if session end time is
  146. * greater than now. Returns false otherwise.
  147. * @return bool True if the session is expired. False otherwise
  148. */
  149. function is_stalled()
  150. {
  151. return $this->end_time() >= time();
  152. }
  153. /**
  154. * Returns whether the session is not stalled
  155. * @return bool True if the session is still valid, false otherwise
  156. */
  157. public function is_valid()
  158. {
  159. return !$this->is_stalled();
  160. }
  161. /**
  162. * The current (logged in) user.
  163. * @return CurrentUser The current user instance
  164. */
  165. public function user()
  166. {
  167. static $result = null;
  168. if (empty($result)) {
  169. $result = CurrentUser::instance();
  170. }
  171. return $result;
  172. }
  173. /**
  174. * Returns the current (active) course
  175. * @return CurrentCourse The current course instance
  176. */
  177. public function course()
  178. {
  179. static $result = null;
  180. if (empty($result)) {
  181. $result = CurrentCourse::instance();
  182. }
  183. return $result;
  184. }
  185. /**
  186. * The current group for the current (logged in) user.
  187. * @return int the current group id
  188. */
  189. public function group_id()
  190. {
  191. return Session::read('_gid');
  192. }
  193. }