]> git.datanom.net - qtadmin.git/blame - lib/session_handler.inc.php
Revert change of vim tags
[qtadmin.git] / lib / session_handler.inc.php
CommitLineData
630144db 1<?php
6b3d5ba9 2/* vim: set ts=4 tw=0 sw=4 noet: */
62511f34
MR
3require_once $CFG->root .'config.php';
4
630144db
MR
5class FileSessionHandler {
6 private $savePath;
62511f34
MR
7 private $log_level;
8 private $log_method;
9
10 public function __construct() {
11 global $CFG;
12
13 if (isset($CFG->log_level)) {
14 $this->log_level = $CFG->log_level;
15 } else {
16 $this->log_level = 1;
17 }
18
19 if (isset($CFG->log_method)) {
20 $this->log_method = $CFG->log_method;
21 } else {
22 $this->log_level = 'syslog';
23 }
24
25 $this->log("Init FileSessionHandler", 4);
26
27 }
28
29 private function log($message, $level = 1) {
30 global $CFG;
31
32 if ($level > $this->log_level)
33 return;
34
35 $time = date('c');
36
37 $priority = LOG_INFO;
38 switch ($level) {
39 case 1: $priority = LOG_ERR; break;
40 case 2: $priority = LOG_WARNING; break;
41 case 3: $priority = LOG_INFO; break;
42 case 4: $priority = LOG_DEBUG; break;
43 }
44
45 switch ($this->log_method) {
46 case 'file':
47 if (isset($CFG->log_file)) {
48 if ($CFG->log_file[0] == '/') {
49 $file = $CFG->log_file;
50 } else {
51 $file = $CFG->root.$CFG->log_file;
52 }
53 } else {
54 $file = $CFG->root.'qtadmin.log';
55 }
56 file_put_contents($file, "[$time]: $message\n", FILE_APPEND | LOCK_EX);
57 chmod($file, 0600);
58 break;
59 case 'stderr':
60 file_put_contents('php://stderr', "[$time]: $message\n");
61 break;
62 case 'syslog':
63 syslog($priority, $message);
64 break;
65 }
66 }
630144db 67
62511f34
MR
68 public function open($savePath, $sessionName) {
69 $this->log("FileSessionHandler->open", 4);
630144db
MR
70 $this->savePath = $savePath;
71 if (!is_dir($this->savePath)) {
72 mkdir($this->savePath, 0777);
73 }
74
75 return true;
76 }
77
78 function close() {
62511f34 79 $this->log("FileSessionHandler->close", 4);
630144db
MR
80 return true;
81 }
82
83 function read($id) {
84 return (string)@file_get_contents("$this->savePath/qt_sess_$id");
85 }
86
87 function write($id, $data) {
62511f34 88 $this->log("FileSessionHandler->write : $id->$data", 4);
e7f73cf6
MR
89 $result = file_put_contents("$this->savePath/qt_sess_$id", $data);
90 chmod("$this->savePath/qt_sess_$id", 0600);
91
92 return ($result === false) ? false : true;
630144db
MR
93 }
94
95 function destroy($id) {
62511f34 96 $this->log("FileSessionHandler->destroy : $id", 4);
630144db
MR
97 $file = "$this->savePath/qt_sess_$id";
98 if (file_exists($file)) {
99 unlink($file);
100 }
101
102 return true;
103 }
104
105 function gc($maxlifetime) {
62511f34 106 $this->log("FileSessionHandler->gc : $maxlifetime", 4);
630144db
MR
107 foreach (glob("$this->savePath/qt_sess_*") as $file) {
108 if (filemtime($file) + $maxlifetime < time() && file_exists($file)) {
109 unlink($file);
110 }
111 }
112
113 return true;
114 }
115}
116
117$handler = new FileSessionHandler();
118session_set_save_handler(
119 array($handler, 'open'),
120 array($handler, 'close'),
121 array($handler, 'read'),
122 array($handler, 'write'),
123 array($handler, 'destroy'),
124 array($handler, 'gc')
125);
126
127// the following prevents unexpected effects when using objects as save handlers
128register_shutdown_function('session_write_close');
129?>
This page took 0.045937 seconds and 5 git commands to generate.