/
RTLog.php
200 lines (174 loc) · 6.56 KB
/
RTLog.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
<?php
/**
* An open source PHP library collection
*
* @category RoboTamer
* @author Dennis T Kaplan
* @copyright Copyright (c) 2008 - 2011, Dennis T Kaplan
* @license http://www.RoboTamer.com/license.html
* @link http://www.RoboTamer.com
**/
defined('DROOT') || define('DROOT','/tmp/');
/**
* RTLog
*
* RTLog::set('testle');
* echo Logger::get('15');
*
* @category RoboTamer
* @package RTLog
* @author Dennis T Kaplan
* @copyright Copyright (c) 2008 - 2011, Dennis T Kaplan
* @license http://www.RoboTamer.com/license.html
* @link http://www.RoboTamer.com
* @todo integrate php error_log()
* @todo find a better solution for dev email, maybe ini file
* @todo create a RT folder before using /tmp
**/
class RTLog {
private static $instance = NULL;
protected $db = NULL;
const developer_email = NULL;
/**
* String to be used as subject line
* of email notification
*
* @var string
*/
const EMAIL_SUBJECT = 'Error on your website';
private function __construct(){}
public function __destruct()
{
$log = self::getInstance();
if (isset($log->db))
{
unset($log->db);
}
}
public function __clone()
{
trigger_error('Cloning is not allowed. ', E_USER_ERROR);
}
// Logging Functions
public static function set($msg, $level = 'Notice')
{
if($level != 'Notice') self::emailError($msg);
loadFunc('randString');
$db = self::db();
$msg = $db->quote($msg);
defined('LOGRAND') || define('LOGRAND', randString('4'));
$sql = "INSERT INTO log (session, level, message) VALUES ('".LOGRAND."', '$level', $msg);";
//echo $sql;die;
$db->exec($sql);
}
protected static function gethtml($items = 20)
{
$db = self::db();
$sql = "SELECT * FROM log ORDER BY added DESC LIMIT $items";
$html = '<table border="1" cellpadding="5" cellspacing="0" class="center">';
$html .= '<tr><th>Date Time</th><th>Session</th><th>Level</th><th>Message</th></tr>';
$i = 0;
foreach ($db->query($sql, PDO::FETCH_OBJ) as $row) {
($i&1) ? $color = '#ffffff' : $color = '#d5d5d5';
$html .= '<tr style="background-color: '.$color.'">'."\n";
$html .= "\t<td>$row->added</td>\n";
$html .= "\t<td>$row->session</td>\n";
$html .= "\t<td>$row->level</td>\n";
$html .= "\t<td>$row->message</td>\n";
$html .= "</tr>\n";
$i++;
}
$html .= '</table>';
return $html;
}
protected static function getInstance() {
if(self::$instance === NULL) {
$class = __CLASS__;
self::$instance = new $class;
}
return self::$instance;
}
protected static function db(){
$log = self::getInstance();
if(isset($log->db) && $log->db !== NULL) return $log->db;
$dbname = DROOT.DIRECTORY_SEPARATOR.'tmp.db3';
try {
$log->db = new PDO('sqlite:'.$dbname);
if (DEBUG === TRUE) $log->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
trigger_error("PDO Sqlite3 Connection with config failed:<br />$dbname<br />" . $e->getMessage());
}
if(defined('DEBUG') && DEBUG !== FALSE) {
$sql = "SELECT name FROM sqlite_master WHERE type='table' UNION ALL SELECT name FROM sqlite_temp_master WHERE type='table' ORDER BY name";
$result = $log->db->query($sql);
foreach ($result as $row) $tables[] = current($row);
if( ! in_array('log', $tables)) {
$log->db->exec("CREATE TABLE log (added DATETIME, session TEXT, level TEXT, message TEXT)");
$log->db->exec("CREATE TRIGGER log_insert_time AFTER INSERT ON log BEGIN UPDATE log SET added = datetime('NOW','UTC') WHERE rowid = last_insert_rowid(); END;");
}
}
return $log->db;
}
/**
* Sends email message to developer
* if message contains error pattern
*/
protected static function emailError($msg){
$devEmail = self::getDevEmail();
if (empty($devEmail)) {
return;
}
if(isset($_SERVER) && is_array($_SERVER)){
$msg .= "\n".'-----------------------------------------------------';
$msg .= "\n".'HTTP_HOST: '.self::getServerVar('HTTP_HOST');
$msg .= "\n".'SCRIPT_NAME: '.self::getServerVar('SCRIPT_NAME');
$msg .= "\n".'REQUEST_METHOD: '.self::getServerVar('REQUEST_METHOD');
$msg .= "\n".'REQUEST_URI: '.self::getServerVar('REQUEST_URI');
$msg .= "\n".'SCRIPT_FILENAME: '.self::getServerVar('SCRIPT_FILENAME');
$msg .= "\n".'-----------------------------------------------------';
$msg .= "\n".'HTTP_USER_AGENT: '.self::getServerVar('HTTP_USER_AGENT');
$msg .= "\n".'HTTP_REFERER: '.self::getServerVar('HTTP_REFERER');
$msg .= "\n".'-----------------------------------------------------';
$msg .= "\n".'REMOTE_ADDR/IP: '.self::getServerVar('REMOTE_ADDR');
$msg .= "\n".'-----------------------------------------------------';
}
/**
* Add hight priority to email headers
* for error messages of certain types (real errors, no notices)
*/
$headers = '';
$headers .= 'X-Mailer: RoboTamer'.PHP_EOL;
$headers .= 'X-Priority: 1'.PHP_EOL;
$headers .= 'Importance: High'.PHP_EOL;
$headers .= 'X-MSMail-Priority: High';
mail( $devEmail, self::EMAIL_SUBJECT, $msg, $headers);
return;
}
/**
* Get value from global $_SERVER array
* if it exists, otherwise return 'Not Available'
*
* @param string $var
* @return string value of $var or empty string
*/
protected static function getServerVar($var){
return (array_key_exists($var, $_SERVER)) ? $_SERVER[$var] : 'Not Available';
}
public static function setDevEmail($email){
$logger = self::getInstance();
$logger->developer_email = $email;
}
/**
* Get email address of developer
* if global constant DEVELOPER_EMAIL exists
* then return it, otherwise return this class's
* self::DEVELOPER_EMAIL
*
*
* @return string email address of developer
*/
protected static function getDevEmail(){
return defined('DEVELOPER_EMAIL') ? DEVELOPER_EMAIL : self::developer_email;
}
}
?>