/** * Checks if debug is enabled (locally in rest.ini and globally in site.ini) * @return bool */ public static function isDebugEnabled() { if (self::$isDebugEnabled === null) { $isEnabled = false; $globalDebugEnabled = eZINI::instance()->variable('DebugSettings', 'DebugOutput') === 'enabled'; $localDebugEnabled = eZINI::instance('rest.ini')->variable('DebugSettings', 'Debug') === 'enabled'; if ($globalDebugEnabled && $localDebugEnabled) { $isEnabled = true; } self::$isDebugEnabled = $isEnabled; } return self::$isDebugEnabled; }
/** * @param array $settings * @param null $responseWriterClass Name of the ezpRestHttpResponseWriter implementation to use during request */ public function __construct(array $settings = array(), $responseWriterClass = null) { $this->responseWriterClass = $responseWriterClass; if (isset($settings['injected-settings'])) { $injectedSettings = array(); foreach ($settings['injected-settings'] as $keySetting => $injectedSetting) { list($file, $section, $setting) = explode('/', $keySetting); $injectedSettings[$file][$section][$setting] = $injectedSetting; } // Those settings override anything else in local .ini files and their overrides eZINI::injectSettings($injectedSettings); } if (isset($settings['injected-merge-settings'])) { $injectedSettings = array(); foreach ($settings['injected-merge-settings'] as $keySetting => $injectedSetting) { list($file, $section, $setting) = explode('/', $keySetting); $injectedSettings[$file][$section][$setting] = $injectedSetting; } // Those settings override anything else in local .ini files and their overrides eZINI::injectMergeSettings($injectedSettings); } $this->settings = $settings + array('use-cache-headers' => true, 'max-age' => 86400, 'siteaccess' => null, 'use-exceptions' => false); unset($settings, $injectedSettings, $file, $section, $setting, $keySetting, $injectedSetting); // lazy loaded database driver include __DIR__ . '/lazy.php'; $this->setUseExceptions($this->settings['use-exceptions']); // Tweaks ini filetime checks if not defined! // This makes ini system not check modified time so // that index_treemenu.php can assume that index.php does // this regular enough, set in config.php to override. if (!defined('EZP_INI_FILEMTIME_CHECK')) { define('EZP_INI_FILEMTIME_CHECK', false); } eZExecution::addFatalErrorHandler(function () { if (!headers_sent()) { header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error'); } }); eZDebug::setHandleType(eZDebug::HANDLE_FROM_PHP); // Trick to get eZSys working with a script other than index.php (while index.php still used in generated URLs): $_SERVER['SCRIPT_FILENAME'] = str_replace('/index_rest.php', '/index.php', $_SERVER['SCRIPT_FILENAME']); $_SERVER['PHP_SELF'] = str_replace('/index_rest.php', '/index.php', $_SERVER['PHP_SELF']); $ini = eZINI::instance(); $timezone = $ini->variable('TimeZoneSettings', 'TimeZone'); if ($timezone) { putenv("TZ={$timezone}"); } eZDebug::setHandleType(eZDebug::HANDLE_NONE); $GLOBALS['eZGlobalRequestURI'] = eZSys::serverVariable('REQUEST_URI'); $ini = eZINI::instance(); eZSys::init('index_rest.php', $ini->variable('SiteAccessSettings', 'ForceVirtualHost') == 'true'); $uri = eZURI::instance(eZSys::requestURI()); $GLOBALS['eZRequestedURI'] = $uri; // load extensions eZExtension::activateExtensions('default'); require_once __DIR__ . '/restkernel_functions.php'; // set siteaccess from X-Siteaccess header if given and exists if (isset($_SERVER['HTTP_X_SITEACCESS']) && eZSiteAccess::exists($_SERVER['HTTP_X_SITEACCESS'])) { $access = array('name' => $_SERVER['HTTP_X_SITEACCESS'], 'type' => eZSiteAccess::TYPE_STATIC); } else { $access = eZSiteAccess::match($uri, eZSys::hostname(), eZSys::serverPort(), eZSys::indexFile()); } eZSiteAccess::change($access); // load siteaccess extensions eZExtension::activateExtensions('access'); // Now that all extensions are activated and siteaccess has been changed, reset // all eZINI instances as they may not take into account siteaccess specific settings. eZINI::resetAllInstances(false); if (ezpRestDebug::isDebugEnabled()) { $debug = ezpRestDebug::getInstance(); $debug->updateDebugSettings(); } }
// This is a temporary measure. // We want PHP to deal with all errors here. eZDebug::setHandleType(eZDebug::HANDLE_TO_PHP); $GLOBALS['eZGlobalRequestURI'] = eZSys::serverVariable('REQUEST_URI'); $ini = eZINI::instance(); eZSys::init('index_rest.php', $ini->variable('SiteAccessSettings', 'ForceVirtualHost') == 'true'); $uri = eZURI::instance(eZSys::requestURI()); $GLOBALS['eZRequestedURI'] = $uri; // load extensions eZExtension::activateExtensions('default'); // setup for eZSiteAccess:change() needs some methods defined in old index.php // We disable it, since we dont' want any override settings to change the // debug settings here function eZUpdateDebugSettings() { } // load siteaccess $access = eZSiteAccess::match($uri, eZSys::hostname(), eZSys::serverPort(), eZSys::indexFile()); $access = eZSiteAccess::change($access); // load siteaccess extensions eZExtension::activateExtensions('access'); // Now that all extensions are activated and siteaccess has been changed, reset // all eZINI instances as they may not take into account siteaccess specific settings. eZINI::resetAllInstances(false); if (ezpRestDebug::isDebugEnabled()) { $debug = ezpRestDebug::getInstance(); $debug->updateDebugSettings(); } $mvcConfig = new ezpMvcConfiguration(); $frontController = new ezpMvcConfigurableDispatcher($mvcConfig); $frontController->run();
/** * Override to add the "requestedResponseGroups" variable for every REST requests * * @see lib/ezc/MvcTools/src/interfaces/ezcMvcController::createResult() */ public function createResult() { $debug = ezpRestDebug::getInstance(); $debug->startTimer('GeneratingRestResult', 'RestController'); if (!self::$isCacheCreated) { ezcCacheManager::createCache(self::CACHE_ID, $this->getCacheLocation(), 'ezpRestCacheStorageClusterObject', array('ttl' => $this->getActionTTL())); self::$isCacheCreated = true; } $cache = ezcCacheManager::getCache(self::CACHE_ID); $controllerCacheId = $this->generateCacheId(); $isCacheEnabled = $this->isCacheEnabled(); // Try to restore application cache. // If expired or not yet available, generate it and store it $cache->isCacheEnabled = $isCacheEnabled; if (($res = $cache->restore($controllerCacheId)) === false) { try { $debug->log('Generating cache', ezcLog::DEBUG); $debug->switchTimer('GeneratingCache', 'GeneratingRestResult'); $res = parent::createResult(); $resGroups = $this->getResponseGroups(); if (!empty($resGroups)) { $res->variables['requestedResponseGroups'] = $resGroups; } if ($res instanceof ezpRestMvcResult) { $res->responseGroups = $resGroups; } if ($isCacheEnabled) { $cache->store($controllerCacheId, $res); } $debug->stopTimer('GeneratingCache'); } catch (Exception $e) { $debug->log('Exception caught, aborting cache generation', ezcLog::DEBUG); if ($isCacheEnabled) { $cache->abortCacheGeneration(); } throw $e; } } // Add debug infos to output if debug is enabled $debug->stopTimer('GeneratingRestResult'); if (ezpRestDebug::isDebugEnabled()) { $res->variables['debug'] = $debug->getReport(); } return $res; }