Dave Jarvis' Repositories

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

require "constants.php";
require "class.Logger.php";

/**
 * Superclass that abstracts the common functionality.
 */
class Obj {
  /**
   * Default (empty) constructor.
   */
  function __construct() {
  }

  /**
   * Database calls return 1 or 0 to indicate success or failure, respectively.
   * This function converts the result from a database call (known to be
   * a 1-or-0 value) into a PHP boolean.
   *
   * @return true iff $result[0][$column] > 0
   */
  protected function isTrue( $result, $column = "exists" ) {
    return isset( $result[0] ) ? $result[0][$column] > 0 : false;
  }

  /**
   * Writes a message to the error log.
   *
   * @param $message The message to write.
   */
  protected function log( $message ) {
    Logger::log( $message );
  }

  /**
   * Sends an e-mail notification to the system-configured e-mail address.
   *
   * @param $message The message to send.
   * @param $errline The line in the file that had an error.
   * @param $errfile The path to the file wherein the error occurred.
   */
  protected function notify( $message, $errline, $errfile ) {
    Logger::error( $message, $errline, $errfile );
  }

  /**
   * Returns true iff $haystack starts with $needle.
   *
   *- wms_starts_with( 'asdf asdf', 'as' ) === true
   *- wms_starts_with( 'asdf asdf', 'df' ) === false
   *
   * @param $haystack The string that might begin with $needle.
   * @param $needle The string that might be found at the start of $haystack.
   */
  protected function startsWith( $haystack, $needle ) {
    return strpos( $haystack, $needle ) === 0;
  }

  /**
   * Returns true iff $haystack ends with $needle.
   *
   *- wms_ends_with( 'asdf asdf', 'as' ) === false
   *- wms_ends_with( 'asdf asdf', 'df' ) === true
   *
   * @param $haystack The string that might end with $needle.
   * @param $needle The string that might be found at the end of $haystack.
   */
  protected function endsWith( $haystack, $needle ) {
    return strcmp( substr( $haystack, -strlen( $needle ) ), $needle ) === 0;
  }

  /**
   * Attempts to create the given path, provided it does not already exist.
   *
   * @param $path The directory path to create.
   */
  protected function createDirectory( $path ) {
    if( !empty( $path ) && !file_exists( $path ) ) {
      if( mkdir( $path, 0777, true ) === false ) {
        $this->log( "Cannot create: $path" );
      }
    }
  }

  /**
   * Returns a safe path to store uploaded files. This will return the
   * path with a trailing slash. This uses $DEFAULT_APP_TITLE (without
   * spaces and in lowercase).
   *
   * @param $subdir A directory below the system temporary directory; do
   * not include slashes.
   * @return Typically a string of the form: /tmp/$appdir/$subdir/
   */
  protected function getTemporaryDirectory( $subdir ) {
    global $DEFAULT_APP_TITLE;
    $appdir = strtolower( str_replace( " ", "", $DEFAULT_APP_TITLE ) );

    return sys_get_temp_dir() . "/$appdir/$subdir/";
  }

  /**
   * Logs the GET and POST parameters.
   */
  protected function logParameters() {
    $this->log( "POST parameters:" );
    $this->logParameterArray( $_POST );
    $this->log( "GET parameters:" );
    $this->logParameterArray( $_GET );
  }

  /**
   * Logs the values in the given array. This is used by the logParameters
   * function to log the GET and POST parameters.
   */
  private function logParameterArray( $a ) {
    foreach( $a as $k => $v ) {
      $this->log( "$k => $v" );
    }
  }

  /**
   * Logs a stacktrace.
   */
  protected function stacktrace() {
    $dbg = debug_backtrace();
    ob_start();
    var_dump( $dbg );
    $dbg = ob_get_clean();
    $this->log( $dbg );
  }
}