root .'config.php'; class FileSessionHandler { private $savePath; private $log_level; private $log_method; public function __construct() { global $CFG; if (isset($CFG->log_level)) { $this->log_level = $CFG->log_level; } else { $this->log_level = 1; } if (isset($CFG->log_method)) { $this->log_method = $CFG->log_method; } else { $this->log_level = 'syslog'; } $this->log("Init FileSessionHandler", 4); } private function log($message, $level = 1) { global $CFG; if ($level > $this->log_level) return; $time = date('c'); $priority = LOG_INFO; switch ($level) { case 1: $priority = LOG_ERR; break; case 2: $priority = LOG_WARNING; break; case 3: $priority = LOG_INFO; break; case 4: $priority = LOG_DEBUG; break; } switch ($this->log_method) { case 'file': if (isset($CFG->log_file)) { if ($CFG->log_file[0] == '/') { $file = $CFG->log_file; } else { $file = $CFG->root.$CFG->log_file; } } else { $file = $CFG->root.'qtadmin.log'; } file_put_contents($file, "[$time]: $message\n", FILE_APPEND | LOCK_EX); chmod($file, 0600); break; case 'stderr': file_put_contents('php://stderr', "[$time]: $message\n"); break; case 'syslog': syslog($priority, $message); break; } } public function open($savePath, $sessionName) { $this->log("FileSessionHandler->open", 4); $this->savePath = $savePath; if (!is_dir($this->savePath)) { mkdir($this->savePath, 0777); } return true; } function close() { $this->log("FileSessionHandler->close", 4); return true; } function read($id) { return (string)@file_get_contents("$this->savePath/qt_sess_$id"); } function write($id, $data) { $this->log("FileSessionHandler->write : $id->$data", 4); $result = file_put_contents("$this->savePath/qt_sess_$id", $data); chmod("$this->savePath/qt_sess_$id", 0600); return ($result === false) ? false : true; } function destroy($id) { $this->log("FileSessionHandler->destroy : $id", 4); $file = "$this->savePath/qt_sess_$id"; if (file_exists($file)) { unlink($file); } return true; } function gc($maxlifetime) { $this->log("FileSessionHandler->gc : $maxlifetime", 4); foreach (glob("$this->savePath/qt_sess_*") as $file) { if (filemtime($file) + $maxlifetime < time() && file_exists($file)) { unlink($file); } } return true; } } $handler = new FileSessionHandler(); session_set_save_handler( array($handler, 'open'), array($handler, 'close'), array($handler, 'read'), array($handler, 'write'), array($handler, 'destroy'), array($handler, 'gc') ); // the following prevents unexpected effects when using objects as save handlers register_shutdown_function('session_write_close'); ?>