Overview

Namespaces

  • Sleepy
  • Module
    • Authentication
    • CSV
    • DB
    • FormBuilder
    • FSDB
    • IP2Country
    • Mailer
    • MobiDetect
    • Navigation
    • StaticCache
  • PHP

Classes

  • Sleepy\Debug
  • Sleepy\Hook
  • Sleepy\Router
  • Sleepy\SM
  • Sleepy\Template

Exceptions

  • Sleepy\RouteNotFound
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: namespace Sleepy;
  3: 
  4: /**
  5:  * Provides custom debugging functions.
  6:  *
  7:  * This class can send emails, log to a database, or display on screen debug
  8:  * information. You can set the enabled flags to enable the debug functions or
  9:  * set them to false to quiet them down. This way you can leave them as a part
 10:  * of your code with little overhead. For email and database logging, don't
 11:  * forget to setup the public properties.
 12:  *
 13:  * ## Usage
 14:  *
 15:  * <code>
 16:  *   // Turn debugging to screen on
 17:  *   Debug::$enable_show = true;
 18:  *   Debug::out("This will goto the screen because $enable_show == true");
 19:  *
 20:  *   // Turn off debugging to screen
 21:  *   Debug::$enable_show = false;
 22:  * </code>
 23:  *
 24:  * ## Changelog
 25:  *
 26:  * ### Version 1.9
 27:  * * Updated private suffix (_) and documentation for consistency
 28:  *
 29:  * ### Version 1.8
 30:  * * Added namespacing
 31:  *
 32:  * ### Version 1.7
 33:  * * Added the date section to the documentation
 34:  *
 35:  * @date July 18, 2016
 36:  * @author Jaime A. Rodriguez <hi.i.am.jaime@gmail.com>
 37:  * @version 1.9
 38:  * @license  http://opensource.org/licenses/MIT
 39:  */
 40: 
 41: class Debug {
 42:     /**
 43:      * The single instance is stored here.
 44:      *
 45:      * @var Debug
 46:      * @private
 47:      */
 48:     private static $_instance = NULL;
 49: 
 50:     /**
 51:      * PDO Database object
 52:      *
 53:      * @var PDO
 54:      * @private
 55:      */
 56:     private static $_dbPDO;
 57: 
 58:     /**
 59:      * Enable output to screen
 60:      *
 61:      * @var bool
 62:      */
 63:     public static $enable_show = false;
 64: 
 65:     /**
 66:      * Enabled logging to a database
 67:      *
 68:      * @var bool
 69:      */
 70:     public static $enable_log = false;
 71: 
 72:     /**
 73:      * Enabled logging via email
 74:      *
 75:      * @var bool
 76:      */
 77:     public static $enable_send = false;
 78: 
 79:     /**
 80:      * Email address to send email to.
 81:      *
 82:      * @var string
 83:      */
 84:     public static $emailTo;
 85: 
 86:     /**
 87:      * Email address cc send email to.
 88:      *
 89:      * @var string
 90:      */
 91:     public static $emailCC;
 92: 
 93:     /**
 94:      * Email address bcc send email to.
 95:      *
 96:      * @var string
 97:      */
 98:     public static $emailBCC;
 99: 
100:     /**
101:      * Email address to send email from.
102:      *
103:      * @var string
104:      */
105:     public static $emailFrom;
106: 
107:     /**
108:      * The subject of the email.
109:      *
110:      * @var string
111:      */
112:     public static $emailSubject;
113: 
114:     /**
115:      * The body of the email.
116:      *
117:      * @var string[]
118:      */
119:     public static $emailBuffer;
120: 
121:     /**
122:      * Database Host
123:      *
124:      * @var string
125:      */
126:     public static $dbHost;
127: 
128:     /**
129:      * Database Name
130:      *
131:      * @var string
132:      */
133:     public static $dbName;
134: 
135:     /**
136:      * Database User Name
137:      *
138:      * @var string
139:      */
140:     public static $dbUser;
141: 
142:     /**
143:      * Database Password
144:      *
145:      * @var string
146:      */
147:     public static $dbPass;
148: 
149:     /**
150:      * Database Table to use for logging
151:      *
152:      * @var string
153:      */
154:     public static $dbTable;
155: 
156:     /**
157:      * Prevent class from being cloned
158:      *
159:      * @private
160:      */
161:     private function __clone() {}
162: 
163:     /**
164:      * The constructor is private to ensure we only have one instance
165:      *
166:      * @private
167:      */
168:     private function __construct() {
169:         // Setup email defaults
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:         // Setup logging defaults
188:         Debug::$dbHost  = DBHOST;
189:         Debug::$dbName  = DBNAME;
190:         Debug::$dbUser  = DBUSER;
191:         Debug::$dbPass  = DBPASS;
192:         Debug::$dbTable = 'log';
193:     }
194: 
195:     /**
196:      * Send the email when the page is unloaded
197:      */
198:     public function __destruct() {
199:         if (self::$enable_send) {
200:             self::sendEmail();
201:         }
202:     }
203: 
204:     /**
205:     * Return instance or create initial instance
206:     *
207:     * @return Debug
208:     * @private
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:      * sets the Exception Handler
220:      */
221:     public function setHandler() {
222:         self::_initialize();
223:         set_exception_handler(array('Debug', 'exceptionHandler'));
224:     }
225: 
226:     /**
227:      * Exception Handler
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:      * Writes to a database log table.  The table should be called log, or set
240:      * $this->dbTable. It should contain 2 columns: 'datetime, message'
241:      *
242:      * @param  mixed $var Anything you want to log
243:      * @return bool
244:      * @todo add a create for the log table
245:      * @private
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:             // MySQL with PDO_MYSQL
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:      * Displays debug information on screen
279:      *
280:      * @param mixed $var Anything you want to log
281:      * @return bool
282:      * @todo  create a hook so the dev can create custom views when outputting
283:      *        debug data.
284:      * @private
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:      * Iterates a buffer that gets emailed on __destruct()
306:      *
307:      * @param mixed $var
308:      *   Anything you want to log
309:      * @return bool
310:      * @private
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:      * Determines what output methods are enabled and passes $var to it.
328:      *
329:      * @param  mixed $var Anything you want to log
330:      * @return void
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:      * Sets all the enabled flags to false
360:      *
361:      * @return void
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:      * Sends the email.
371:      *
372:      * @return bool true if sent successfully
373:      * @todo  make this private, I cannot remember why this is public...
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: 
sleepyMUSTACHE v.0.8 API documentation generated by ApiGen