/gException.class.php

https://gitlab.com/karl3/gs_libs · PHP · 144 lines · 75 code · 19 blank · 50 comment · 7 complexity · 44292832067c67a22d471a7673896a7a MD5 · raw file

  1. <?php
  2. /**
  3. * Custom exception handler. Extends PHP Exception and adds a few new methods
  4. */
  5. class gException extends Exception
  6. {
  7. public $ExceptionQuery;
  8. /**
  9. * constructor. Operates exactly like parent constructor except explicitly sets $previous
  10. *
  11. * @param string $message the custom message you define
  12. * @param int $code the custom code you define
  13. * @param Exception $previous the previous exception thrown
  14. */
  15. public function __construct($message = null, $code = 0, $previous = null)
  16. {
  17. parent::__construct($message, (int)$code, $previous);
  18. }
  19. /**
  20. * Destructor. Unsets all object variables
  21. */
  22. public function __destruct()
  23. {
  24. $vars = get_object_vars($this);
  25. if (is_array($vars)) {
  26. foreach ($vars as $key => $val) {
  27. $this->$key = null;
  28. }
  29. }
  30. }
  31. /**
  32. * Assembles a message with more data than the default
  33. *
  34. * @param bool $toString whether or not to return a string or an array
  35. * @param string $delim delimiter to use, if returning a string
  36. *
  37. * @return string|array
  38. */
  39. public function gMessage($toString = true, $delim = PHP_EOL)
  40. {
  41. $message['time'] = date('Y-m-d G:i:s');
  42. $message['text'] = self::getmessage();
  43. $message['code'] = self::getCode();
  44. $message['file'] = self::getFile();
  45. $message['line'] = self::getLine();
  46. $message['trace'] = self::getTraceAsString();
  47. $message['fullURL'] = Network::getFullSelf();
  48. if (false === $toString) {
  49. return $message;
  50. } else {
  51. $output = '';
  52. foreach ($message AS $key => $val) {
  53. $output .= $key . ': ' . $val . $delim;
  54. }
  55. return $output;
  56. }
  57. }
  58. /**
  59. * sends an email with exception information
  60. *
  61. * @param string $to recipient of the email message
  62. * @param string $from sender of the email message
  63. *
  64. * @return bool
  65. */
  66. public function sendExceptionEmail($to, $from)
  67. {
  68. return gFormUtils::gFormEmail($to, $from, 'Exception Thrown on ' . Network::getThisDomain(), self::gMessage(true));
  69. }
  70. /**
  71. * logs exception information to database
  72. *
  73. * @param array $connection database connection details
  74. * @param string $table the table into which the info will be inserted
  75. *
  76. * @return bool
  77. */
  78. public function logToDatabase($connection, $table)
  79. {
  80. $db = new dbPDO($connection['dbName'], $connection['user'], $connection['pass']);
  81. $data = self::gMessage(false);
  82. $fields = implode(',', array_keys($data));
  83. foreach ($data AS $val) {
  84. $values[] = "'" . $db->EscapeString($val) . "'";
  85. }
  86. $values = implode(',', $values);
  87. $query = sprintf("INSERT INTO '%s' ($fields) VALUES($values)", $db->EscapeString($table));
  88. $this->ExceptionQuery = $query;
  89. return $db->InsertData($query);
  90. }
  91. /**
  92. * logs the exception to a file
  93. *
  94. * @param string $pathToFile full filesystem path to the file the log will be added to
  95. * @param string $delimChar the character that will delimit new lines
  96. * @param string $mode
  97. *
  98. * @return bool TRUE on success
  99. */
  100. public function logToFile($pathToFile, $delimChar = '-', $mode = 'a+')
  101. {
  102. $delimiter = str_pad('', 70, $delimChar);
  103. $handle = fopen($pathToFile, $mode);
  104. if (!$handle) {
  105. return false;
  106. } else {
  107. if (flock($handle, LOCK_EX)) {
  108. fwrite($handle, self::gMessage(true) . $delimiter . PHP_EOL);
  109. flock($handle, LOCK_UN);
  110. fclose($handle);
  111. return true;
  112. } else {
  113. return false;
  114. }
  115. }
  116. }
  117. /**
  118. * Simple function to print the message
  119. * return void Message is echoed to screen
  120. */
  121. public function printMessage()
  122. {
  123. echo self::gMessage(true);
  124. }
  125. }