session_handler.class.php 4.1 KB

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