modx = & $modx; $this->parameters['GET'] =& $_GET; $this->parameters['POST'] =& $_POST; $this->parameters['COOKIE'] =& $_COOKIE; $this->parameters['REQUEST'] =& $_REQUEST; } /** * The primary MODX request handler (a.k.a. controller). * * @return boolean True if a request is handled without interruption. */ public function handleRequest() { $this->loadErrorHandler(); // If enabled, send the X-Powered-By header to identify this site as running MODX, per discussion in #12882 if ($this->modx->getOption('send_poweredby_header', null, true)) { $version = $this->modx->getVersionData(); header("X-Powered-By: MODX {$version['code_name']}"); } $this->sanitizeRequest(); $this->modx->invokeEvent('OnHandleRequest'); if (!$this->modx->checkSiteStatus()) { header($_SERVER['SERVER_PROTOCOL'] . ' 503 Service Unavailable'); if (!$this->modx->getOption('site_unavailable_page',null,1)) { $this->modx->resource = $this->modx->newObject('modDocument'); $this->modx->resource->template = 0; $this->modx->resource->content = $this->modx->getOption('site_unavailable_message'); } else { $this->modx->resourceMethod = "id"; $this->modx->resourceIdentifier = $this->modx->getOption('site_unavailable_page',null,1); } } else { $this->checkPublishStatus(); $this->modx->resourceMethod = $this->getResourceMethod(); $this->modx->resourceIdentifier = $this->getResourceIdentifier($this->modx->resourceMethod); if ($this->modx->resourceMethod == 'id' && $this->modx->getOption('friendly_urls', null, false) && $this->modx->getOption('request_method_strict', null, false)) { $uri = $this->modx->context->getResourceURI($this->modx->resourceIdentifier); if (!empty($uri)) { if ((integer) $this->modx->resourceIdentifier === (integer) $this->modx->getOption('site_start', null, 1)) { $url = $this->modx->getOption('site_url', null, MODX_SITE_URL); } else { $url = $this->modx->getOption('site_url', null, MODX_SITE_URL) . $uri; } $this->modx->sendRedirect($url, array('responseCode' => $_SERVER['SERVER_PROTOCOL'] . ' 301 Moved Permanently')); } } } if (empty ($this->modx->resourceMethod)) { $this->modx->resourceMethod = "id"; } if ($this->modx->resourceMethod == "alias") { $this->modx->resourceIdentifier = $this->_cleanResourceIdentifier($this->modx->resourceIdentifier); } if ($this->modx->resourceMethod == "alias") { $found = $this->modx->findResource($this->modx->resourceIdentifier); if ($found) { $this->modx->resourceIdentifier = $found; $this->modx->resourceMethod = 'id'; } else { $this->modx->sendErrorPage(); } } $this->modx->beforeRequest(); $this->modx->invokeEvent("OnWebPageInit"); if (!is_object($this->modx->resource)) { if (!$this->modx->resource = $this->getResource($this->modx->resourceMethod, $this->modx->resourceIdentifier)) { $this->modx->sendErrorPage(); return true; } } return $this->prepareResponse(); } /** * Prepares the MODX response to a web request that is being handled. * * @param array $options An array of options * @return boolean True if the response is properly prepared. */ public function prepareResponse(array $options = array()) { $this->modx->beforeProcessing(); $this->modx->invokeEvent("OnLoadWebDocument"); if (!$this->modx->getResponse()) { $this->modx->log(modX::LOG_LEVEL_FATAL, 'Could not load response class.'); } $this->modx->response->outputContent($options); } /** * Gets the method used to request a resource. * * @return string 'alias', 'id', or an empty string. */ public function getResourceMethod() { $method = ''; $hasId = isset($_REQUEST[$this->modx->getOption('request_param_id',null,'id')]); $hasAlias = isset($_REQUEST[$this->modx->getOption('request_param_alias',null,'q')]); if ($hasId || $hasAlias) { if ($this->modx->getOption('request_method_strict', null, false)) { $method = $this->modx->getOption('friendly_urls', null, false) ? 'alias' : 'id'; } elseif ($hasAlias) { $method = "alias"; } elseif ($hasId) { $method = "id"; } } return $method; } /** * Gets a requested resource and all required data. * * @param string $method The method, 'id', or 'alias', by which to perform * the resource lookup. * @param string|integer $identifier The identifier with which to search. * @param array $options An array of options for the resource fetching * @return modResource The requested modResource instance or request * is forwarded to the error page, or unauthorized page. */ public function getResource($method, $identifier, array $options = array()) { $resource = null; if ($method == 'alias') { $resourceId = $this->modx->findResource($identifier); } else { $resourceId = $identifier; } if (!is_numeric($resourceId)) { $this->modx->sendErrorPage(); } $isForward = array_key_exists('forward', $options) && !empty($options['forward']); $fromCache = false; $cacheKey = $this->modx->context->get('key') . "/resources/{$resourceId}"; $cachedResource = $this->modx->cacheManager->get($cacheKey, array( xPDO::OPT_CACHE_KEY => $this->modx->getOption('cache_resource_key', null, 'resource'), xPDO::OPT_CACHE_HANDLER => $this->modx->getOption('cache_resource_handler', null, $this->modx->getOption(xPDO::OPT_CACHE_HANDLER)), xPDO::OPT_CACHE_FORMAT => (integer) $this->modx->getOption('cache_resource_format', null, $this->modx->getOption(xPDO::OPT_CACHE_FORMAT, null, xPDOCacheManager::CACHE_PHP)), )); if (is_array($cachedResource) && array_key_exists('resource', $cachedResource) && is_array($cachedResource['resource'])) { /** @var modResource $resource */ $resource = $this->modx->newObject($cachedResource['resourceClass']); if ($resource) { $resource->fromArray($cachedResource['resource'], '', true, true, true); $resource->_content = $cachedResource['resource']['_content']; $resource->_isForward = $isForward; if (isset($cachedResource['contentType'])) { $contentType = $this->modx->newObject('modContentType'); $contentType->fromArray($cachedResource['contentType'], '', true, true, true); $resource->addOne($contentType, 'ContentType'); } if (isset($cachedResource['resourceGroups'])) { $rGroups = array(); foreach ($cachedResource['resourceGroups'] as $rGroupKey => $rGroup) { $rGroups[$rGroupKey]= $this->modx->newObject('modResourceGroupResource', $rGroup); } $resource->addMany($rGroups); } if (isset($cachedResource['policyCache'])) $resource->setPolicies(array($this->modx->context->get('key') => $cachedResource['policyCache'])); if (isset($cachedResource['elementCache'])) $this->modx->elementCache = $cachedResource['elementCache']; if (isset($cachedResource['sourceCache'])) $this->modx->sourceCache = $cachedResource['sourceCache']; if ($resource->get('_jscripts')) $this->modx->jscripts = $this->modx->jscripts + $resource->get('_jscripts'); if ($resource->get('_sjscripts')) $this->modx->sjscripts = $this->modx->sjscripts + $resource->get('_sjscripts'); if ($resource->get('_loadedjscripts')) $this->modx->loadedjscripts = array_merge($this->modx->loadedjscripts, $resource->get('_loadedjscripts')); $isForward= $resource->_isForward; $resource->setProcessed(true); $fromCache = true; } } if (!$fromCache || !is_object($resource)) { $criteria = $this->modx->newQuery('modResource'); $criteria->select(array($this->modx->escape('modResource').'.*')); $criteria->where(array('id' => $resourceId, 'deleted' => '0')); if (!$this->modx->hasPermission('view_unpublished') || $this->modx->getSessionState() !== modX::SESSION_STATE_INITIALIZED) { $criteria->where(array('published' => 1)); } if ($resource = $this->modx->getObject('modResource', $criteria)) { if ($resource instanceof modResource) { if ($resource->get('context_key') !== $this->modx->context->get('key')) { if (!$isForward || ($isForward && !$this->modx->getOption('allow_forward_across_contexts', $options, false))) { if (!$this->modx->getCount('modContextResource', array($this->modx->context->get('key'), $resourceId))) { return null; } } } $resource->_isForward= $isForward; if (!$resource->checkPolicy('view')) { $this->modx->sendUnauthorizedPage(); } if ($tvs = $resource->getMany('TemplateVars', 'all')) { /** @var modTemplateVar $tv */ foreach ($tvs as $tv) { $resource->set($tv->get('name'), array( $tv->get('name'), $tv->getValue($resource->get('id')), $tv->get('display'), $tv->get('display_params'), $tv->get('type'), )); } } $this->modx->resourceGenerated = true; } } } elseif ($fromCache && $resource instanceof modResource && !$resource->get('deleted')) { if ($resource->checkPolicy('load') && ($resource->get('published') || ($this->modx->getSessionState() === modX::SESSION_STATE_INITIALIZED && $this->modx->hasPermission('view_unpublished')))) { if ($resource->get('context_key') !== $this->modx->context->get('key')) { if (!$isForward || ($isForward && !$this->modx->getOption('allow_forward_across_contexts', $options, false))) { if (!$this->modx->getCount('modContextResource', array($this->modx->context->get('key'), $resourceId))) { return null; } } } if (!$resource->checkPolicy('view')) { $this->modx->sendUnauthorizedPage(); } } else { return null; } $this->modx->invokeEvent('OnLoadWebPageCache', array( 'resource' => &$resource, )); } return $resource; } /** * Gets the idetifier used to request a resource. * * @param string $method 'alias' or 'id'. * @return string The identifier for the requested resource. */ public function getResourceIdentifier($method) { $identifier = ''; switch ($method) { case 'alias' : $rAlias = $this->modx->getOption('request_param_alias', null, 'q'); $identifier = isset ($_REQUEST[$rAlias]) ? $_REQUEST[$rAlias] : $identifier; break; case 'id' : $rId = $this->modx->getOption('request_param_id', null, 'id'); $identifier = isset ($_REQUEST[$rId]) ? $_REQUEST[$rId] : $identifier; break; default : $identifier = $this->modx->getOption('site_start', null, 1); } return $identifier; } /** * Cleans the resource identifier from the request params. * * @param string $identifier The raw identifier. * @return string|integer The cleansed identifier. */ public function _cleanResourceIdentifier($identifier) { if (empty ($identifier)) { if ($this->modx->getOption('base_url', null, MODX_BASE_URL) !== strtok($_SERVER["REQUEST_URI"],'?')) { $this->modx->sendRedirect($this->modx->getOption('site_url', null, MODX_SITE_URL), array('responseCode' => $_SERVER['SERVER_PROTOCOL'] . ' 301 Moved Permanently')); } $identifier = $this->modx->getOption('site_start', null, 1); $this->modx->resourceMethod = 'id'; } elseif ($this->modx->getOption('friendly_urls', null, false) && $this->modx->resourceMethod == 'alias') { $containerSuffix = trim($this->modx->getOption('container_suffix', null, '')); $found = $this->modx->findResource($identifier); if ($found === false && !empty ($containerSuffix)) { $suffixLen = strlen($containerSuffix); $identifierLen = strlen($identifier); if (substr($identifier, $identifierLen - $suffixLen) === $containerSuffix) { $identifier = substr($identifier, 0, $identifierLen - $suffixLen); $found = $this->modx->findResource($identifier); } else { $identifier = "{$identifier}{$containerSuffix}"; $found = $this->modx->findResource($identifier); } if ($found) { $parameters = $this->getParameters(); unset($parameters[$this->modx->getOption('request_param_alias')]); $url = $this->modx->makeUrl($found, $this->modx->context->get('key'), $parameters, 'full'); $this->modx->sendRedirect($url, array('responseCode' => $_SERVER['SERVER_PROTOCOL'] . ' 301 Moved Permanently')); } $this->modx->resourceMethod = 'alias'; } elseif ((integer) $this->modx->getOption('site_start', null, 1) === $found) { $parameters = $this->getParameters(); unset($parameters[$this->modx->getOption('request_param_alias')]); $url = $this->modx->makeUrl($this->modx->getOption('site_start', null, 1), $this->modx->context->get('key'), $parameters, 'full'); $this->modx->sendRedirect($url, array('responseCode' => $_SERVER['SERVER_PROTOCOL'] . ' 301 Moved Permanently')); } else { if ($this->modx->getOption('friendly_urls_strict', null, false)) { $requestUri = $_SERVER['REQUEST_URI']; $qsPos = strpos($requestUri, '?'); if ($qsPos !== false) $requestUri = substr($requestUri, 0, $qsPos); $fullId = $this->modx->getOption('base_url', null, MODX_BASE_URL) . $identifier; $requestUri = urldecode($requestUri); if ($fullId !== $requestUri && strpos($requestUri, $fullId) !== 0) { $parameters = $this->getParameters(); unset($parameters[$this->modx->getOption('request_param_alias')]); $url = $this->modx->makeUrl($found, $this->modx->context->get('key'), $parameters, 'full'); $this->modx->sendRedirect($url, array('responseCode' => $_SERVER['SERVER_PROTOCOL'] . ' 301 Moved Permanently')); } } $this->modx->resourceMethod = 'alias'; } } else { $this->modx->resourceMethod = 'id'; } return $identifier; } /** * Harden GPC variables by removing any MODX tags, Javascript, or entities. */ public function sanitizeRequest() { $modxtags = array_values($this->modx->sanitizePatterns); $depth = (int)ini_get('max_input_nesting_level'); $depth = ($depth <= 0) ? 99 : $depth + 1; modX :: sanitize($_GET, $modxtags, $depth); if ($this->modx->getOption('allow_tags_in_post',null,true)) { modX :: sanitize($_POST); } else { modX :: sanitize($_POST, $modxtags, $depth); } modX :: sanitize($_COOKIE, $modxtags, $depth); modX :: sanitize($_REQUEST, $modxtags, $depth); $rAlias = $this->modx->getOption('request_param_alias', null, 'q'); if (isset ($_GET[$rAlias])) { $_GET[$rAlias] = preg_replace("/[^A-Za-z0-9_\-\.\/]/", "", $_GET[$rAlias]); } } /** * Loads the error handling class for the request. * * @param string $class The class to use as the error handler. */ public function loadErrorHandler($class = 'modError') { if ($className = $this->modx->loadClass('error.'.$class,'',false,true)) { $this->modx->error = new $className($this->modx); } else { $this->modx->log(modX::LOG_LEVEL_FATAL,'Error handling class could not be loaded: '.$class); } } /** * Provides an easy way to initiate register logging. * * Through an array of options, you can have all calls to modX::log() * recorded in a topic of a modRegister instance. The options include: * *