session_handler.class.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This class allows to manage the session. Session is stored in the database.
  5. *
  6. * @package chamilo.library
  7. */
  8. /**
  9. * @todo use the SessionServiceProvider and not a "in house" class
  10. * @package chamilo.library *
  11. */
  12. class SessionHandler
  13. {
  14. // TODO: Hm, these variables are public.
  15. public $connection;
  16. public $connection_handler;
  17. public $lifetime;
  18. public $session_name;
  19. public function __construct() {
  20. global $_configuration;
  21. $this->lifetime = 60; // 60 minutes
  22. $this->connection = array(
  23. 'server' => $_configuration['db_host'],
  24. 'login' => $_configuration['db_user'],
  25. 'password' => $_configuration['db_password'],
  26. 'base' => $_configuration['main_database']
  27. );
  28. $this->connection_handler = false;
  29. }
  30. // @todo use a dbal connection
  31. public function sqlConnect() {
  32. if (!$this->connection_handler) {
  33. $this->connection_handler = @mysql_connect($this->connection['server'], $this->connection['login'], $this->connection['password'], true);
  34. // The system has not been designed to use special SQL modes that were introduced since MySQL 5
  35. @mysql_query("set session sql_mode='';", $this->connection_handler);
  36. @mysql_select_db($this->connection['base'], $this->connection_handler);
  37. // Initialization of the database connection encoding to be used.
  38. // The internationalization library should be already initialized.
  39. @mysql_query("SET SESSION character_set_server='utf8';", $this->connection_handler);
  40. @mysql_query("SET SESSION collation_server='utf8_general_ci';", $this->connection_handler);
  41. $system_encoding = api_get_system_encoding();
  42. if (api_is_utf8($system_encoding)) {
  43. // See Bug #1802: For UTF-8 systems we prefer to use "SET NAMES 'utf8'" statement in order to avoid a bizarre problem with Chinese language.
  44. @mysql_query("SET NAMES 'utf8';", $this->connection_handler);
  45. } else {
  46. @mysql_query("SET CHARACTER SET '" . Database::to_db_encoding($system_encoding) . "';", $this->connection_handler);
  47. }
  48. }
  49. return $this->connection_handler ? true : false;
  50. }
  51. public function sqlClose() {
  52. if ($this->connection_handler) {
  53. mysql_close($this->connection_handler);
  54. $this->connection_handler = false;
  55. return true;
  56. }
  57. return false;
  58. }
  59. public function sqlQuery($query, $die_on_error = true) {
  60. $result = mysql_query($query, $this->connection_handler);
  61. if ($die_on_error && !$result) {
  62. $this->sqlClose();
  63. return;
  64. }
  65. return $result;
  66. }
  67. public function open($path, $name) {
  68. $this->session_name = $name;
  69. return true;
  70. }
  71. public function close() {
  72. return $this->garbage(0) ? true : false;
  73. }
  74. public function read($sess_id) {
  75. if ($this->sqlConnect()) {
  76. $result = $this->sqlQuery("SELECT session_value FROM ".$this->connection['base'].".php_session WHERE session_id='$sess_id'");
  77. if ($row = mysql_fetch_assoc($result)) {
  78. return $row['session_value'];
  79. }
  80. }
  81. return '';
  82. }
  83. public function write($sess_id, $sess_value) {
  84. $time = time();
  85. if ($this->sqlConnect()) {
  86. $result = $this->sqlQuery("INSERT INTO ".$this->connection['base'].".php_session(session_id,session_name,session_time,session_start,session_value) VALUES('$sess_id','".$this->session_name."','$time','$time','".addslashes($sess_value)."')", false);
  87. if (!$result) {
  88. $this->sqlQuery("UPDATE ".$this->connection['base'].".php_session SET session_name='".$this->session_name."',session_time='$time',session_value='".addslashes($sess_value)."' WHERE session_id='$sess_id'");
  89. }
  90. return true;
  91. }
  92. return false;
  93. }
  94. public function destroy($sess_id) {
  95. if ($this->sqlConnect()) {
  96. $this->sqlQuery("DELETE FROM ".$this->connection['base'].".php_session WHERE session_id='$sess_id'");
  97. return true;
  98. }
  99. return false;
  100. }
  101. public function garbage($lifetime) {
  102. if ($this->sqlConnect()) {
  103. $result = $this->sqlQuery("SELECT COUNT(session_id) FROM ".$this->connection['base'].".php_session");
  104. list($nbr_results) = Database::fetch_row($result);
  105. if ($nbr_results > 5000) {
  106. $this->sqlQuery("DELETE FROM ".$this->connection['base'].".php_session WHERE session_time<'".strtotime('-'.$this->lifetime.' minutes')."'");
  107. }
  108. $this->sqlClose();
  109. return true;
  110. }
  111. return false;
  112. }
  113. }