1
|
|
|
<?php namespace EvolutionCMS\Legacy; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* logger class. |
5
|
|
|
* |
6
|
|
|
* Usage: |
7
|
|
|
* |
8
|
|
|
* include_once "log.class.inc.php"; // include_once the class |
9
|
|
|
* $log = new logHandler; // create the object |
10
|
|
|
* $log->initAndWriteLog($msg); // write $msg to log, and populate all other fields as best as possible |
11
|
|
|
* $log->initAndWriteLog($msg, $internalKey, $username, $action, $id, $itemname); // write $msg and other data to log |
12
|
|
|
*/ |
13
|
|
|
class LogHandler |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Single variable for a log entry |
17
|
|
|
* |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
public $entry = array(); |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $msg |
24
|
|
|
* @param string $internalKey |
25
|
|
|
* @param string $username |
26
|
|
|
* @param string $action |
27
|
|
|
* @param string $itemid |
28
|
|
|
* @param string $itemname |
29
|
|
|
* |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
|
|
public function initAndWriteLog( |
|
|
|
|
33
|
|
|
$msg = "", |
34
|
|
|
$internalKey = "", |
35
|
|
|
$username = "", |
36
|
|
|
$action = "", |
37
|
|
|
$itemid = "", |
38
|
|
|
$itemname = "" |
39
|
|
|
) { |
40
|
|
|
$modx = evolutionCMS(); |
41
|
|
|
$this->entry['msg'] = $msg; // writes testmessage to the object |
42
|
|
|
$this->entry['action'] = empty($action) ? $modx->manager->action : $action; // writes the action to the object |
43
|
|
|
|
44
|
|
|
// User Credentials |
45
|
|
|
$this->entry['internalKey'] = $internalKey == "" ? $modx->getLoginUserID() : $internalKey; |
46
|
|
|
$this->entry['username'] = $username == "" ? $modx->getLoginUserName() : $username; |
47
|
|
|
|
48
|
|
|
$this->entry['itemId'] = (empty($itemid) && isset($_REQUEST['id'])) ? (int)$_REQUEST['id'] : $itemid; // writes the id to the object |
49
|
|
|
if ($this->entry['itemId'] == 0) { |
50
|
|
|
$this->entry['itemId'] = "-"; |
51
|
|
|
} // to stop items having id 0 |
52
|
|
|
|
53
|
|
|
$this->entry['itemName'] = ($itemname == "" && isset($_SESSION['itemname'])) ? $_SESSION['itemname'] : $itemname; // writes the id to the object |
54
|
|
|
if ($this->entry['itemName'] == "") { |
55
|
|
|
$this->entry['itemName'] = "-"; |
56
|
|
|
} // to stop item name being empty |
57
|
|
|
|
58
|
|
|
$this->writeToLog(); |
59
|
|
|
|
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* function to write to the log collects all required info, and writes it to the logging table |
65
|
|
|
* |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
|
|
public function writeToLog() |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$modx = evolutionCMS(); |
71
|
|
|
$tbl_manager_log = $modx->getFullTableName('manager_log'); |
72
|
|
|
|
73
|
|
|
if ($this->entry['internalKey'] == "") { |
74
|
|
|
$modx->webAlertAndQuit("Logging error: internalKey not set."); |
75
|
|
|
} |
76
|
|
|
if (empty($this->entry['action'])) { |
77
|
|
|
$modx->webAlertAndQuit("Logging error: action not set."); |
78
|
|
|
} |
79
|
|
|
if ($this->entry['msg'] == "") { |
80
|
|
|
include_once "actionlist.inc.php"; |
81
|
|
|
$this->entry['msg'] = getAction($this->entry['action'], $this->entry['itemId']); |
82
|
|
|
if ($this->entry['msg'] == "") { |
83
|
|
|
$modx->webAlertAndQuit("Logging error: couldn't find message to write to log."); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$fields['timestamp'] = time(); |
|
|
|
|
88
|
|
|
$fields['internalKey'] = $modx->db->escape($this->entry['internalKey']); |
89
|
|
|
$fields['username'] = $modx->db->escape($this->entry['username']); |
90
|
|
|
$fields['action'] = $this->entry['action']; |
91
|
|
|
$fields['itemid'] = $this->entry['itemId']; |
92
|
|
|
$fields['itemname'] = $modx->db->escape($this->entry['itemName']); |
93
|
|
|
$fields['message'] = $modx->db->escape($this->entry['msg']); |
94
|
|
|
$fields['ip'] = $this->getUserIP(); |
95
|
|
|
$fields['useragent'] = $_SERVER['HTTP_USER_AGENT']; |
96
|
|
|
|
97
|
|
|
$insert_id = $modx->db->insert($fields, $tbl_manager_log); |
98
|
|
|
if (!$insert_id) { |
|
|
|
|
99
|
|
|
$modx->messageQuit("Logging error: couldn't save log to table! Error code: " . $modx->db->getLastError()); |
100
|
|
|
} else { |
101
|
|
|
$limit = (isset($modx->config['manager_log_limit'])) ? (int)$modx->config['manager_log_limit'] : 3000; |
102
|
|
|
$trim = (isset($modx->config['manager_log_trim'])) ? (int)$modx->config['manager_log_trim'] : 100; |
103
|
|
|
if (($insert_id % $trim) === 0) { |
104
|
|
|
$modx->rotate_log('manager_log', $limit, $trim); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function getUserIP() { |
|
|
|
|
110
|
|
|
if( array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER) && !empty($_SERVER['HTTP_X_FORWARDED_FOR']) ) { |
111
|
|
|
if (strpos($_SERVER['HTTP_X_FORWARDED_FOR'], ',')>0) { |
112
|
|
|
$addr = explode(",",$_SERVER['HTTP_X_FORWARDED_FOR']); |
113
|
|
|
return trim($addr[0]); |
114
|
|
|
} else { |
115
|
|
|
return $_SERVER['HTTP_X_FORWARDED_FOR']; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
else { |
119
|
|
|
return $_SERVER['REMOTE_ADDR']; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: