This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
/
class.textLogDriver.php
280 lines (255 loc) · 9.83 KB
/
class.textLogDriver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
/*
* Copyright 2007-2013 Charles du Jeu - Abstrium SAS <team (at) pyd.io>
* This file is part of Pydio.
*
* Pydio is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <http://pyd.io/>.
*/
defined('AJXP_EXEC') or die( 'Access not allowed');
/**
* Standard logger. Writes logs into text files
* @package AjaXplorer_Plugins
* @subpackage Log
*/
class textLogDriver extends AbstractLogDriver
{
/**
* @var Integer Default permissions, in chmod format.
*/
public $USER_GROUP_RIGHTS = 0770;
/**
* @var Integer File handle to currently open log file.
*/
public $fileHandle;
/**
* @var Array stack of log messages to be written when file becomes available.
*/
public $stack;
/**
* @var String full path to the directory where logs will be kept, with trailing slash.
*/
public $storageDir = "";
/**
* @var String name of the log file to write.
*/
public $logFileName = "";
/**
* Close file handle on objects destructor.
*/
public function __destruct()
{
if($this->fileHandle !== false) $this->close();
}
/**
* Initialise storage: check and/or make log folder and file.
*/
public function initStorage()
{
$storageDir = $this->storageDir;
if (!file_exists($storageDir)) {
@mkdir($storageDir);
}
$this->open();
}
/**
* Open log file for append, and flush out buffered messages to the file.
*/
public function open()
{
if ($this->storageDir!="") {
$create = false;
if (!file_exists($this->storageDir . $this->logFileName)) {
// file creation
$create = true;
}
$this->fileHandle = @fopen($this->storageDir . $this->logFileName, "at+");
if ($this->fileHandle === false) {
error_log("Cannot open log file ".$this->storageDir . $this->logFileName);
}
if ($this->fileHandle !== false && count($this->stack)) {
$this->stackFlush();
}
if ($create && $this->fileHandle !== false) {
$mainLink = $this->storageDir."ajxp_access.log";
if (file_exists($mainLink)) {
@unlink($mainLink);
}
@symlink($this->storageDir.$this->logFileName, $mainLink);
}
}
}
/**
* Initialise the text log driver.
*
* Sets the user defined options.
* Makes sure that the folder and file exist, and makes them if they don't.
*
* @param Array $options array of options specific to the logger driver.
* @access public
* @return null
*/
public function init($options)
{
parent::init($options);
$this->severityDescription = 0;
$this->stack = array();
$this->fileHandle = false;
$this->storageDir = isset($this->options['LOG_PATH']) ? $this->options['LOG_PATH'] : "";
$this->storageDir = AJXP_VarsFilter::filter($this->storageDir);
$this->storageDir = (rtrim($this->storageDir))."/";
$this->logFileName = isset($this->options['LOG_FILE_NAME']) ? $this->options['LOG_FILE_NAME'] : 'log_' . date('m-d-y') . '.txt';
$this->USER_GROUP_RIGHTS = isset($this->options['LOG_CHMOD']) ? $this->options['LOG_CHMOD'] : 0770;
if (preg_match("/(.*)date\('(.*)'\)(.*)/i", $this->logFileName, $matches)) {
$this->logFileName = $matches[1].date($matches[2]).$matches[3];
}
$this->initStorage();
}
/**
* Write text to the log file.
*
* If write is not allowed because the file is not yet open, the message is buffered until
* file becomes available.
*
* @param String $level Log severity: one of LOG_LEVEL_* (DEBUG,INFO,NOTICE,WARNING,ERROR)
* @param String $ip The client ip
* @param String $user The user login
* @param String $source The source of the message
* @param String $prefix The prefix of the message
* @param String $message The message to log
* @return void
*/
public function write2($level, $ip, $user, $source, $prefix, $message)
{
$textMessage = date("m-d-y") . " " . date("H:i:s") . "\t";
$textMessage .= "$ip\t".strtoupper((string) $level)."\t$user\t$source\t$prefix\t$message\n";
if ($this->fileHandle !== false) {
if(count($this->stack)) $this->stackFlush();
if (@fwrite($this->fileHandle, $textMessage) === false) {
throw new Exception("There was an error writing to log file ($this->logFileName)");
}
} else {
$this->stack[] = $textMessage;
}
}
/**
* Flush the stack/buffer of messages that couldn't be written earlier.
*
*/
public function stackFlush()
{
// Flush stack for messages that could have been written before the file opening.
foreach ($this->stack as $message) {
@fwrite($this->fileHandle, $message);
}
$this->stack = array();
}
/**
* closes the handle to the log file
*
* @access public
*/
public function close()
{
$success = @fclose($this->fileHandle);
if ($success === false) {
// Failure to close the log file
// error_log("AJXP_Logger failed to close the handle to the log file");
}
}
/**
* List available logs in XML format.
*
* This method prints the response.
*
* @param String $nodeName Name of the XML node to use as response.
* @param Integer $year The year to list.
* @param Integer $month The month to list.
* @return null
*/
public function xmlListLogFiles($nodeName="file", $year=null, $month=null, $rootPath = "/logs", $print = true)
{
$dir = $this->storageDir;
if(!is_dir($this->storageDir)) return ;
$logs = array();
$years = array();
$months = array();
if (($handle = opendir($this->storageDir))!==false) {
while ($file = readdir($handle)) {
if($file == "index.html" || $file == "ajxp_access.log") continue;
$split = explode(".", $file);
if(!count($split) || $split[0] == "") continue;
$split2 = explode("_", $split[0]);
$date = $split2[1];
$dSplit = explode("-", $date);
$time = mktime(0,0,1,intval($dSplit[0]), intval($dSplit[1]), intval($dSplit[2]));
$display = date("l d", $time);
$fullYear = date("Y", $time);
$fullMonth = date("F", $time);
$logY = $fullYear;
$logM = $fullMonth;
if($year != null && $fullYear != $year) continue;
if($month != null && $fullMonth != $month) continue;
$logs[$time] = "<$nodeName icon=\"toggle_log.png\" date=\"$display\" display=\"$display\" text=\"$date\" is_file=\"0\" filename=\"$rootPath/$fullYear/$fullMonth/$date\"/>";
$years[$logY] = "<$nodeName icon=\"x-office-calendar.png\" date=\"$fullYear\" display=\"$fullYear\" text=\"$fullYear\" is_file=\"0\" filename=\"$rootPath/$fullYear\"/>";
$months[$logM] = "<$nodeName icon=\"x-office-calendar.png\" date=\"$fullMonth\" display=\"$logM\" text=\"$fullMonth\" is_file=\"0\" filename=\"$rootPath/$fullYear/$fullMonth\"/>";
}
closedir($handle);
}
$result = $years;
if ($year != null) {
$result = $months;
if ($month != null) {
$result = $logs;
}
}
krsort($result, SORT_STRING);
if($print) foreach($result as $log) print($log);
return $result;
}
/**
* Get a log in XML format.
*
* @param String $date Date in m-d-y format.
* @param String $nodeName The name of the node to use for each log item.
* @return null
*/
public function xmlLogs($parentDir, $date, $nodeName = "log", $rootPath = "/logs")
{
$fName = $this->storageDir."log_".$date.".txt";
if(!is_file($fName) || !is_readable($fName)) return;
$res = "";
$lines = file($fName);
foreach ($lines as $line) {
$line = AJXP_Utils::xmlEntities($line);
$matches = explode("\t",$line,6);
if (count($matches) == 6) {
$fileName = $parentDir."/".$matches[0];
foreach ($matches as $key => $match) {
$match = AJXP_Utils::xmlEntities($match);
$match = str_replace("\"", "'", $match);
$matches[$key] = $match;
}
if(count($matches) < 3) continue;
// rebuild timestamp
$date = $matches[0];
list($m,$d,$Y,$h,$i,$s) = sscanf($date, "%i-%i-%i %i:%i:%i");
$tStamp = mktime($h,$i,$s,$m,$d,$Y);
print(SystemTextEncoding::toUTF8("<$nodeName is_file=\"1\" ajxp_modiftime=\"$tStamp\" filename=\"$fileName\" ajxp_mime=\"log\" date=\"$matches[0]\" ip=\"$matches[1]\" level=\"$matches[2]\" user=\"$matches[3]\" action=\"$matches[4]\" params=\"$matches[5]\" icon=\"toggle_log.png\" />", false));
}
}
return ;
}
}