session_handler.class.php 4.0 KB

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