Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/recipe-fiddle.git
<?php
namespace com\whitemagicsoftware;

require "swift/swift_required.php";

use Swift_SmtpTransport;
use Swift_Mailer;
use Swift_Message;

/**
 * Responsible for sending e-mail notifications whenever an error happens
 * with the system.
 */
class Logger {
  public function __construct() {
  }

  /**
   * Configures PHP settings for error handling.
   */
  public static function init() {
    error_reporting( E_ALL ); // | E_STRICT );

    ini_set( "display_errors", 0 );
    ini_set( "html_errors", 0 );
    ini_set( "expose_php", 0 );
    ini_set( "magic_quotes_gpc", 0 );
    ini_set( "ignore_repeated_errors", 1 );
    ini_set( "ignore_repeated_source", 1 );

    ini_set( "log_errors", 1 );
    ini_set( "error_log", "/tmp/php.log" );

    //ini_set( "register_globals", 1 );
    //ini_set('max_execution_time', 1 ); 

    //set_error_handler( array( new Logger(), "handle" ) );
    //register_shutdown_function( "wms_fatal_handler" );
  }

  /**
   * Formats and sends an e-mail about the given error. This also logs
   * the error to the PHP log file.
   *
   * @param $message The error message.
   * @param $errfile The file where the error happened.
   * @param $errline The line number in the file that triggered the problem.
   */
  public static function error( $message, $errfile, $errline ) {
    $e = Logger::format( 0, $message, $errfile, $errline );
    Logger::mail( $e );
    Logger::log( $e );
  }
  
  /**
   * Logs a message to PHP's error_log.
   *
   * @param $message The content of the error message.
   */
  public static function log( $message ) {
    error_log( $message );
  }

  /**
   * Formats and sends an e-mail about the given error.
   *
   * @param $errno The error number.
   * @param $errstr The error message.
   * @param $errfile The file where the error happened.
   * @param $errline The line number in the file that triggered the problem.
   */
  public static function handle( $errno, $errstr, $errfile, $errline ) {
    Logger::mail(
      Logger::format( $errno, $errstr, $errfile, $errline ) );
  }

  /**
   * Formats the error message into an HTML table before sending an
   * HTML-based e-mail.
   *
   * @param $errno The error number.
   * @param $errstr The error message.
   * @param $errfile The file where the error happened.
   * @param $errline The line number in the file that triggered the problem.
   */
  public static function format( $errno, $errstr, $errfile, $errline ) {
    $trace = print_r( debug_backtrace( false ), true );

    $content  = "<table><thead bgcolor='#c8c8c8'><th>Item</th><th>Description</th></thead><tbody>";
    $content .= "<tr valign='top'><td><b>Error</b></td><td><pre>$errstr</pre></td></tr>";
    $content .= "<tr valign='top'><td><b>Errno</b></td><td><pre>$errno</pre></td></tr>";
    $content .= "<tr valign='top'><td><b>File</b></td><td>$errfile</td></tr>";
    $content .= "<tr valign='top'><td><b>Line</b></td><td>$errline</td></tr>";
    $content .= "<tr valign='top'><td><b>Trace</b></td><td><pre>$trace</pre></td></tr>";
    $content .= '</tbody></table>';

    return $content;
  }

  /**
   * Sends an e-mail to $DEFAULT_EMAIL_TO whenever an error occurs.
   */
  public static function mail( $content, $redirect = true ) {
    global $BASE_APP;
    global $DEFAULT_APP_TITLE;
    global $DEFAULT_EMAIL_TO;
    global $DEFAULT_EMAIL_FROM;
    global $DEFAULT_EMAIL_NAME;
    global $SERVER_EMAIL_HOST;
    global $SERVER_EMAIL_PORT;

    $transport = Swift_SmtpTransport::newInstance(
      $SERVER_EMAIL_HOST, $SERVER_EMAIL_PORT );
    $mailer = Swift_Mailer::newInstance( $transport );

    $message = Swift_Message::newInstance()
      ->setSubject( "$DEFAULT_APP_TITLE - Error" )
      ->setFrom( array( $DEFAULT_EMAIL_FROM => $DEFAULT_APP_TITLE ) )
      ->setTo( array( $DEFAULT_EMAIL_TO => $DEFAULT_EMAIL_NAME ) )
      ->setPriority( 1 )
      ->setBody( $content, 'text/html' );

    //$mailer->send( $message );

    // Redirect to the homepage.
    if( $redirect ) {
      header( "Location: $BASE_APP" );
    }
  }

  /**
   * Sends a notification if there was an error connecting to the NLP.
   */
  public static function curl( $errline, $curl ) {
    $errno = curl_errno( $curl );

    if( $errno ) {
      global $SERVICE_NLP;
      $errstr = curl_error( $curl ) . "\n$SERVICE_NLP";

      // Send an error message notification, but don't redirect.
      Logger::mail(
        Logger::format( $errno, $errstr, __FILE__, $errline ), false );
    }
  }

  /**
   * Called when a fatal error happens. In practice, this isn't used
   * because the "fatal" signal is called whenever the PHP script ends.
   * This includes fatal conditions, but also includes when the script
   * ends naturally.
   */
  private static function fatal() {
    $error = error_get_last();

    $errno   = E_CORE_ERROR;
    $errstr  = "shutdown";
    $errfile = "unknown file";
    $errline = 0;

    if( $error ) {
      $errno   = $error["type"];
      $errstr  = $error["message"];
      $errfile = $error["file"];
      $errline = $error["line"];
    }

    Logger::handle( $errno, $errstr, $errfile, $errline );
  }
}

Logger::init();