1: <?php
2: namespace Sleepy;
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: class Debug {
42: 43: 44: 45: 46: 47:
48: private static $_instance = NULL;
49:
50: 51: 52: 53: 54: 55:
56: private static $_dbPDO;
57:
58: 59: 60: 61: 62:
63: public static $enable_show = false;
64:
65: 66: 67: 68: 69:
70: public static $enable_log = false;
71:
72: 73: 74: 75: 76:
77: public static $enable_send = false;
78:
79: 80: 81: 82: 83:
84: public static $emailTo;
85:
86: 87: 88: 89: 90:
91: public static $emailCC;
92:
93: 94: 95: 96: 97:
98: public static $emailBCC;
99:
100: 101: 102: 103: 104:
105: public static $emailFrom;
106:
107: 108: 109: 110: 111:
112: public static $emailSubject;
113:
114: 115: 116: 117: 118:
119: public static $emailBuffer;
120:
121: 122: 123: 124: 125:
126: public static $dbHost;
127:
128: 129: 130: 131: 132:
133: public static $dbName;
134:
135: 136: 137: 138: 139:
140: public static $dbUser;
141:
142: 143: 144: 145: 146:
147: public static $dbPass;
148:
149: 150: 151: 152: 153:
154: public static $dbTable;
155:
156: 157: 158: 159: 160:
161: private function __clone() {}
162:
163: 164: 165: 166: 167:
168: private function __construct() {
169:
170: $server_ip = (isset($_SERVER['SERVER_ADDR'])) ? $_SERVER['SERVER_ADDR'] : '';
171: $user_ip = (isset($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : '';
172: $filename = (isset($_SERVER['SCRIPT_FILENAME'])) ? $_SERVER['SCRIPT_FILENAME'] : '';
173: $date = date(DATE_ATOM, mktime(date('G'), date('i'), 0, date('m'), date('d'), date('Y')));
174:
175: Debug::$emailBuffer = array();
176: Debug::$emailBuffer[] = "Date: {$date}";
177: Debug::$emailBuffer[] = "Server IP: {$server_ip}";
178: Debug::$emailBuffer[] = "Client IP: {$user_ip}";
179: Debug::$emailBuffer[] = "Filename: {$filename}";
180: Debug::$emailBuffer[] = '---';
181: Debug::$emailTo = EMAIL_TO;
182: Debug::$emailFrom = EMAIL_FROM;
183: Debug::$emailSubject = $date;
184: Debug::$emailCC = EMAIL_CC;
185: Debug::$emailBCC = EMAIL_BCC;
186:
187:
188: Debug::$dbHost = DBHOST;
189: Debug::$dbName = DBNAME;
190: Debug::$dbUser = DBUSER;
191: Debug::$dbPass = DBPASS;
192: Debug::$dbTable = 'log';
193: }
194:
195: 196: 197:
198: public function __destruct() {
199: if (self::$enable_send) {
200: self::sendEmail();
201: }
202: }
203:
204: 205: 206: 207: 208: 209:
210: private static function _initialize() {
211: if (!self::$_instance) {
212: self::$_instance = new Debug();
213: }
214:
215: return self::$_instance;
216: }
217:
218: 219: 220:
221: public function setHandler() {
222: self::_initialize();
223: set_exception_handler(array('Debug', 'exceptionHandler'));
224: }
225:
226: 227: 228:
229: public function exceptionHandler($e) {
230: if (headers_sent()) {
231: echo 'Error: ' , $e->getMessage(), "\n";
232: } else {
233: $_SESSION['exception'] = $e->getMessage() . '<br />' . str_replace("\n", '<br />', $e->getTraceAsString()) . '';
234: header('Location: /error/');
235: }
236: }
237:
238: 239: 240: 241: 242: 243: 244: 245: 246:
247: private function log($var) {
248: if (!self::$enable_log) {
249: return false;
250: }
251:
252: if (is_array($var) || is_object($var)) {
253: $buffer = print_r($var, true);
254: } else {
255: $buffer = $var;
256: }
257:
258: try {
259:
260: if (!is_object(self::$_dbPDO)) {
261: self::$_dbPDO = new \PDO('mysql:host=' . self::$dbHost . ';dbname=' . self::$dbName, self::$dbUser, self::$dbPass);
262: self::$_dbPDO->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
263: }
264: $query = self::$_dbPDO->prepare('INSERT INTO ' . self::$dbTable . ' (datetime, message) values (:datetime, :message)');
265: $datetime = date(DATE_ATOM, mktime(date('G'), date('i'), 0, date('m'), date('d'), date('Y')));
266: $query->bindParam(':datetime', $datetime);
267: $query->bindParam(':message', $buffer);
268: $query->execute();
269: } catch(\PDOException $e) {
270: self::show($e->getMessage());
271: return false;
272: }
273:
274: return true;
275: }
276:
277: 278: 279: 280: 281: 282: 283: 284: 285:
286: private static function show($var) {
287: if (!self::$enable_show) {
288: return false;
289: }
290:
291: echo '<pre>';
292:
293: if (is_array($var) || is_object($var)) {
294: print_r($var);
295: } else {
296: echo $var;
297: }
298:
299: echo '</pre>';
300:
301: return true;
302: }
303:
304: 305: 306: 307: 308: 309: 310: 311:
312: private static function send($var) {
313: if (!self::$enable_send) {
314: return false;
315: }
316:
317: if (is_array($var) || is_object($var)) {
318: self::$emailBuffer[] = print_r($var, true);
319: } else {
320: self::$emailBuffer[] = $var;
321: }
322:
323: return true;
324: }
325:
326: 327: 328: 329: 330: 331:
332: public static function out($var) {
333: $result = true;
334:
335: self::_initialize();
336:
337: if (self::$enable_send) {
338: $result = $result && self::$_instance->send($var);
339: }
340:
341: if (self::$enable_log) {
342: $result = $result && self::$_instance->log($var);
343: }
344:
345: if (self::$enable_show) {
346: $result = $result && self::$_instance->show($var);
347: }
348:
349: if (!self::$enable_show &&
350: !self::$enable_send &&
351: !self::$enable_log) {
352: $result = false;
353: }
354:
355: return $result;
356: }
357:
358: 359: 360: 361: 362:
363: public static function disable() {
364: self::$enable_send = false;
365: self::$enable_log = false;
366: self::$enable_show = false;
367: }
368:
369: 370: 371: 372: 373: 374:
375: public static function sendEmail() {
376: if (!self::$enable_send) {
377: return false;
378: }
379:
380: $headers = array();
381: $headers[] = 'From: ' . self::$emailFrom;
382: $headers[] = 'MIME-Version: 1.0';
383: $headers[] = 'Content-type: text/html; charset=iso-8859-1';
384: if (self::$emailCC != '') {
385: $headers[] = 'Cc: ' . self::$emailCC;
386: }
387: if (self::$emailBCC != '') {
388: $headers[] = 'Bcc: ' . self::$emailBCC;
389: }
390: return mail(self::$emailTo, self::$emailSubject, implode("<br />\n", self::$emailBuffer), implode("\n", $headers));
391: }
392: }
393: