function renderer_json($mode) { if (strtolower($mode) == 'administration') { throw new Lib\Exceptions\InvalidModeException('JSON Renderer launcher is only available on the frontend'); } $renderer = Frontend::instance(); // Check if we should enable exception debug information $exceptionDebugEnabled = Symphony::isLoggedIn(); // Use the JSON exception and error handlers instead of the Symphony one. Lib\ExceptionHandler::initialise($exceptionDebugEnabled); Lib\ErrorHandler::initialise($exceptionDebugEnabled); // #1808 if (isset($_SERVER['HTTP_MOD_REWRITE'])) { throw new Exception("mod_rewrite is required, however is not enabled."); } $output = $renderer->display(getCurrentPage()); cleanup_session_cookies(); if (in_array('JSON', Frontend::Page()->pageData()['type'])) { // Load the output into a SimpleXML Container and convert to JSON try { $xml = new SimpleXMLElement($output, LIBXML_NOCDATA); // Convert the XML to a plain array. This step is necessary as we cannot // use JSON_PRETTY_PRINT directly on a SimpleXMLElement object $outputArray = json_decode(json_encode($xml), true); // Get the transforer object ready. Other extensions will // add their transormations to this. $transformer = new Lib\Transformer(); /** * Allow other extensions to add their own transformers */ Symphony::ExtensionManager()->notifyMembers('APIFrameworkJSONRendererAppendTransformations', '/frontend/', ['transformer' => &$transformer]); // Apply transformations $outputArray = $transformer->run($outputArray); // Now put the array through a json_encode $output = json_encode($outputArray, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); } catch (\Exception $e) { // This happened because the input was not valid XML. This could // occur for a few reasons, but there are two scenarios // we are interested in. // 1) This is a devkit page (profile, debug etc). We want the data // to be passed through and displayed rather than converted into // JSON. There is no easy way in Symphony to tell if a devkit has // control over the page, so instead lets inspect the output for // any signs a devkit is rendering the page. // 2) It is actually bad XML. In that case we need to let the error // bubble through. // Currently the easiest method is to check for the devkit.min.css // in the output. This may fail in the furture if this file is // renamed or moved. if (!preg_match("@\\/symphony\\/assets\\/css\\/devkit.min.css@", $output)) { throw $e; } } } echo $output; return $renderer; }
/** * Responsible for launching a standard symphony instance and * sending output to the browser. * * @param string $mode (optional) * @return integer */ function symphony_launcher($mode) { if (strtolower($mode) == 'administration') { $renderer = Administration::instance(); } else { $renderer = Frontend::instance(); } $output = $renderer->display(getCurrentPage()); // #1808 if (isset($_SERVER['HTTP_MOD_REWRITE'])) { $output = file_get_contents(GenericExceptionHandler::getTemplate('fatalerror.rewrite')); $output = str_replace('{ASSETS_URL}', ASSETS_URL, $output); $output = str_replace('{SYMPHONY_URL}', SYMPHONY_URL, $output); $output = str_replace('{URL}', URL, $output); echo $output; exit; } cleanup_session_cookies(); echo $output; return $renderer; }
/** * 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; }
<?php define('DOCROOT', rtrim(dirname(__FILE__), '\\/')); define('PATH_INFO', isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : NULL); define('DOMAIN_PATH', dirname(rtrim($_SERVER['PHP_SELF'], PATH_INFO))); define('DOMAIN', rtrim(rtrim($_SERVER['HTTP_HOST'], '\\/') . DOMAIN_PATH, '\\/')); require DOCROOT . '/symphony/lib/boot/bundle.php'; function renderer($mode = 'frontend') { if (!in_array($mode, array('frontend', 'administration'))) { throw new Exception('Invalid Symphony Renderer mode specified. Must be either "frontend" or "administration".'); } require_once CORE . "/class.{$mode}.php"; return $mode == 'administration' ? Administration::instance() : Frontend::instance(); } $renderer = isset($_GET['mode']) && strtolower($_GET['mode']) == 'administration' ? 'administration' : 'frontend'; $output = renderer($renderer)->display(getCurrentPage()); // #1808 if (isset($_SERVER['HTTP_MOD_REWRITE'])) { $output = file_get_contents(GenericExceptionHandler::getTemplate('fatalerror.rewrite')); $output = str_replace('{SYMPHONY_URL}', SYMPHONY_URL, $output); $output = str_replace('{URL}', URL, $output); echo $output; exit; } cleanup_session_cookies(); echo $output; exit;