Example #1
0
 /**
  * The render function will take a `SymphonyErrorPage` exception and
  * output a HTML page. This function first checks to see if their is a custom
  * template for this exception otherwise it reverts to using the default
  * `usererror.generic.php`
  *
  * @param Exception $e
  *  The Exception object
  * @return string
  *  An HTML string
  */
 public static function render(Exception $e)
 {
     if ($e->getTemplate() === false) {
         Page::renderStatusCode($e->getHttpStatusCode());
         if (isset($e->getAdditional()->header)) {
             header($e->getAdditional()->header);
         }
         echo '<h1>Symphony Fatal Error</h1><p>' . $e->getMessage() . '</p>';
         exit;
     }
     include $e->getTemplate();
 }
function processParams($string, &$image_settings)
{
    $param = (object) array('mode' => 0, 'width' => 0, 'height' => 0, 'position' => 0, 'background' => 0, 'file' => 0, 'external' => false);
    // Check for matching recipes
    if (file_exists(WORKSPACE . '/jit-image-manipulation/recipes.php')) {
        include WORKSPACE . '/jit-image-manipulation/recipes.php';
    }
    // check to see if $recipes is even available before even checking if it is an array
    if (!empty($recipes) && is_array($recipes)) {
        foreach ($recipes as $recipe) {
            // Is the mode regex? If so, bail early and let not JIT process it.
            if ($recipe['mode'] === 'regex' && preg_match($recipe['url-parameter'], $string)) {
                // change URL to a "normal" JIT URL
                $string = preg_replace($recipe['url-parameter'], $recipe['jit-parameter'], $string);
                $is_regex = true;
                if (!empty($recipe['quality'])) {
                    $image_settings['quality'] = $recipe['quality'];
                }
                break;
            } elseif (!preg_match('/^' . $recipe['url-parameter'] . '\\//i', $string, $matches)) {
                continue;
            }
            // If we're here, the recipe name matches, so we'll go on to fill out the params
            // Is it an external image?
            $param->external = (bool) $recipe['external'];
            // Path to file
            $param->file = substr($string, strlen($recipe['url-parameter']) + 1);
            // Set output quality
            if (!empty($recipe['quality'])) {
                $image_settings['quality'] = $recipe['quality'];
            }
            // Specific variables based off mode
            // 0 is ignored (direct display)
            // regex is already handled
            switch ($recipe['mode']) {
                // Resize
                case '1':
                    // Resize to fit
                // Resize to fit
                case '4':
                    $param->mode = (int) $recipe['mode'];
                    $param->width = (int) $recipe['width'];
                    $param->height = (int) $recipe['height'];
                    break;
                    // Resize and crop
                // Resize and crop
                case '2':
                    // Crop
                // Crop
                case '3':
                    $param->mode = (int) $recipe['mode'];
                    $param->width = (int) $recipe['width'];
                    $param->height = (int) $recipe['height'];
                    $param->position = (int) $recipe['position'];
                    $param->background = $recipe['background'];
                    break;
            }
            return $param;
        }
    }
    // Check if only recipes are allowed.
    // We only have to check if we are using a `regex` recipe
    // because the other recipes already return `$param`.
    if ($image_settings['disable_regular_rules'] == 'yes' && $is_regex != true) {
        Page::renderStatusCode(Page::HTTP_STATUS_NOT_FOUND);
        trigger_error('Error generating image', E_USER_ERROR);
        echo 'Regular JIT rules are disabled and no matching recipe was found.';
        exit;
    }
    // Mode 2: Resize and crop
    // Mode 3: Crop
    if (preg_match_all('/^(2|3)\\/([0-9]+)\\/([0-9]+)\\/([1-9])\\/([a-fA-F0-9]{3,6}\\/)?(?:(0|1)\\/)?(.+)$/i', $string, $matches, PREG_SET_ORDER)) {
        $param->mode = (int) $matches[0][1];
        $param->width = (int) $matches[0][2];
        $param->height = (int) $matches[0][3];
        $param->position = (int) $matches[0][4];
        $param->background = trim($matches[0][5], '/');
        $param->external = (bool) $matches[0][6];
        $param->file = $matches[0][7];
    } elseif (preg_match_all('/^(1|4)\\/([0-9]+)\\/([0-9]+)\\/(?:(0|1)\\/)?(.+)$/i', $string, $matches, PREG_SET_ORDER)) {
        $param->mode = (int) $matches[0][1];
        $param->width = (int) $matches[0][2];
        $param->height = (int) $matches[0][3];
        $param->external = (bool) $matches[0][4];
        $param->file = $matches[0][5];
    } elseif (preg_match_all('/^(?:(0|1)\\/)?(.+)$/i', $string, $matches, PREG_SET_ORDER)) {
        $param->external = (bool) $matches[0][1];
        $param->file = $matches[0][2];
    }
    return $param;
}
 /**
  * The handler function is given an Exception and will call it's render
  * function to display the Exception to a user. After calling the render
  * function, the output is displayed and then exited to prevent any further
  * logic from occurring.
  *
  * @param Exception $e
  *  The Exception object
  * @return string
  *  The result of the Exception's render function
  */
 public static function handler(Exception $e)
 {
     try {
         // Instead of just throwing an empty page, return a 404 page.
         if (self::$enabled !== true) {
             require_once CORE . '/class.frontend.php';
             $e = new FrontendPageNotFoundException();
         }
         $exception_type = get_class($e);
         if (class_exists("{$exception_type}Handler") && method_exists("{$exception_type}Handler", 'render')) {
             $class = "{$exception_type}Handler";
         } else {
             $class = __CLASS__;
         }
         // Exceptions should be logged if they are not caught.
         if (self::$_Log instanceof Log) {
             self::$_Log->pushExceptionToLog($e, true);
         }
         if (!headers_sent()) {
             Page::renderStatusCode(Page::HTTP_STATUS_ERROR);
             header('Content-Type: text/html; charset=utf-8');
         }
         $output = call_user_func(array($class, 'render'), $e);
         echo $output;
         exit;
     } catch (Exception $e) {
         try {
             if (!headers_sent()) {
                 Page::renderStatusCode(Page::HTTP_STATUS_ERROR);
                 header('Content-Type: text/html; charset=utf-8');
             }
             $output = call_user_func(array('GenericExceptionHandler', 'render'), $e);
             echo $output;
             exit;
         } catch (Exception $e) {
             echo "<pre>";
             echo 'A severe error occurred whilst trying to handle an exception:' . PHP_EOL;
             echo $e->getMessage() . ' on ' . $e->getLine() . ' of file ' . $e->getFile();
             exit;
         }
     }
 }
 /**
  * The handler function is given an Exception and will call it's render
  * function to display the Exception to a user. After calling the render
  * function, the output is displayed and then exited to prevent any further
  * logic from occurring.
  *
  * @param Exception $e
  *  The Exception object
  * @return string
  *  The result of the Exception's render function
  */
 public static function handler(Exception $e)
 {
     $output = '';
     try {
         // Instead of just throwing an empty page, return a 404 page.
         if (self::$enabled !== true) {
             $e = new FrontendPageNotFoundException();
         }
         $exception_type = get_class($e);
         if (class_exists("{$exception_type}Handler") && method_exists("{$exception_type}Handler", 'render')) {
             $class = "{$exception_type}Handler";
         } else {
             $class = __CLASS__;
         }
         // Exceptions should be logged if they are not caught.
         if (self::$_Log instanceof Log) {
             self::$_Log->pushExceptionToLog($e, true);
         }
         cleanup_session_cookies();
         $output = call_user_func(array($class, 'render'), $e);
         // If an exception was raised trying to render the exception, fall back
         // to the generic exception handler
     } catch (Exception $e) {
         try {
             $output = call_user_func(array('GenericExceptionHandler', 'render'), $e);
             // If the generic exception handler couldn't do it, well we're in bad
             // shape, just output a plaintext response!
         } catch (Exception $e) {
             echo "<pre>";
             echo 'A severe error occurred whilst trying to handle an exception, check the Symphony log for more details' . PHP_EOL;
             echo $e->getMessage() . ' on ' . $e->getLine() . ' of file ' . $e->getFile() . PHP_EOL;
             exit;
         }
     }
     // Pending nothing disasterous, we should have `$e`
     // and `$output` values here.
     if (!headers_sent()) {
         $httpStatus = null;
         if ($e instanceof SymphonyErrorPage) {
             $httpStatus = $e->getHttpStatusCode();
         } else {
             if ($e instanceof FrontendPageNotFoundException) {
                 $httpStatus = Page::HTTP_STATUS_NOT_FOUND;
             }
         }
         if (!$httpStatus || $httpStatus == Page::HTTP_STATUS_OK) {
             $httpStatus = Page::HTTP_STATUS_ERROR;
         }
         Page::renderStatusCode($httpStatus);
         header('Content-Type: text/html; charset=utf-8');
     }
     echo $output;
     exit;
 }
 public function sendImageHeaders($parameters)
 {
     // if there is no `$last_modified` value, params should be NULL and headers
     // should not be set. Otherwise, set caching headers for the browser.
     if ($parameters['last_modified']) {
         $last_modified_gmt = gmdate('D, d M Y H:i:s', $parameters['last_modified']) . ' GMT';
         $etag = md5($parameters['last_modified'] . $image_path);
         $cacheControl = 'public';
         // Add no-transform in order to prevent ISPs to
         // serve image over http through a compressing proxy
         // See #79
         if ($this->settings['disable_proxy_transform'] == 'yes') {
             $cacheControl .= ', no-transform';
         }
         header('Last-Modified: ' . $last_modified_gmt);
         header(sprintf('ETag: "%s"', $etag));
         header('Cache-Control: ' . $cacheControl);
     } else {
         $last_modified_gmt = null;
         $etag = null;
     }
     // Check to see if the requested image needs to be generated or if a 304
     // can just be returned to the browser to use it's cached version.
     if ($this->caching === true && (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) || isset($_SERVER['HTTP_IF_NONE_MATCH']))) {
         if ($_SERVER['HTTP_IF_MODIFIED_SINCE'] == $last_modified_gmt || str_replace('"', null, stripslashes($_SERVER['HTTP_IF_NONE_MATCH'])) == $etag) {
             \Page::renderStatusCode(\Page::HTTP_NOT_MODIFIED);
             exit;
         }
     }
 }