public static function handleRequest() { // handle emergence request if (static::$pathStack[0] == 'emergence') { array_shift(static::$pathStack); return Emergence::handleRequest(); } // handle CORS headers if (isset($_SERVER['HTTP_ORIGIN'])) { $hostname = strtolower(parse_url($_SERVER['HTTP_ORIGIN'], PHP_URL_HOST)); if ($hostname == strtolower(static::$hostname) || static::$permittedOrigins == '*' || in_array($hostname, static::$permittedOrigins)) { header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']); header('Access-Control-Allow-Credentials: true'); //header('Access-Control-Max-Age: 86400') } else { header('HTTP/1.1 403 Forbidden'); exit; } } if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS' && isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) { header('Access-Control-Allow-Methods: ' . $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']); if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) { header('Access-Control-Allow-Headers: ' . $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']); } exit; } // try to resolve URL in site-root $rootNode = static::getRootCollection('site-root'); $resolvedNode = $rootNode; static::$resolvedPath = array(); // handle default page request if (empty(static::$pathStack[0]) && static::$defaultPage) { static::$pathStack[0] = static::$defaultPage; } // crawl down path stack until a handler is found while ($handle = array_shift(static::$pathStack)) { $scriptHandle = substr($handle, -4) == '.php' ? $handle : $handle . '.php'; if ($resolvedNode && method_exists($resolvedNode, 'getChild') && ($scriptHandle && ($childNode = $resolvedNode->getChild($scriptHandle)) || ($childNode = $resolvedNode->getChild($handle))) || $scriptHandle && ($childNode = Emergence::resolveFileFromParent('site-root', array_merge(static::$resolvedPath, array($scriptHandle)))) || ($childNode = Emergence::resolveFileFromParent('site-root', array_merge(static::$resolvedPath, array($handle))))) { $resolvedNode = $childNode; if (is_a($resolvedNode, 'SiteFile')) { static::$resolvedPath[] = $scriptHandle; break; } } else { $resolvedNode = false; //break; } static::$resolvedPath[] = $handle; } if ($resolvedNode) { // prevent caching by default header('Cache-Control: max-age=0, no-cache, no-store, must-revalidate'); header('Pragma: no-cache'); if (is_callable(static::$onRequestMapped)) { call_user_func(static::$onRequestMapped, $resolvedNode); } // switch collection result to its _index.php if found if (is_a($resolvedNode, 'SiteCollection') && (($indexNode = $resolvedNode->getChild('_index.php')) || ($indexNode = Emergence::resolveFileFromParent('site-root', array_merge(static::$resolvedPath, array('_index.php')))))) { $resolvedNode = $indexNode; } if ($resolvedNode->MIMEType == 'application/php') { // TODO: execute _all.php handlers, cache the list of them for the containing collection static::executeScript($resolvedNode); } elseif (is_callable(array($resolvedNode, 'outputAsResponse'))) { if (!is_a($resolvedNode, 'SiteFile') && !static::$listCollections) { static::respondNotFound(); } if (is_callable(static::$onBeforeStaticResponse)) { call_user_func(static::$onBeforeStaticResponse, $resolvedNode); } $resolvedNode->outputAsResponse(); } else { static::respondNotFound(); } } else { static::respondNotFound(); } }
public static function handleRequest() { // TODO: try global handle lookup? // resolve URL in root $resolvedNode = false; $rootNode = static::getRootCollection('site-root'); // handle root request - default page if (empty(static::$pathStack[0]) && static::$defaultPage) { static::$pathStack[0] = static::$defaultPage; } // route request if (static::$pathStack[0] == 'emergence') { array_shift(static::$pathStack); return Emergence::handleRequest(); } elseif (static::$pathStack[0] == 'parent-refresh' && $_REQUEST['key'] == static::$controlKey) { DB::nonQuery('DELETE FROM `%s` WHERE CollectionID IN (SELECT ID FROM `%s` WHERE SiteID != %u)', array(SiteFile::$tableName, SiteCollection::$tableName, Site::getSiteID())); die('Cleared ' . DB::affectedRows() . ' cached files'); } else { $resolvedNode = $rootNode; $resolvedPath = array(); while ($handle = array_shift(static::$pathStack)) { $scriptHandle = substr($handle, -4) == '.php' ? $handle : $handle . '.php'; //printf('%s: (%s)/(%s) - %s<br>', $resolvedNode->Handle, $handle, implode('/',static::$pathStack), $scriptHandle); if ($resolvedNode && method_exists($resolvedNode, 'getChild') && (($childNode = $resolvedNode->getChild($handle)) || $scriptHandle && ($childNode = $resolvedNode->getChild($scriptHandle))) || ($childNode = Emergence::resolveFileFromParent('site-root', array_merge($resolvedPath, array($handle)))) || $scriptHandle && ($childNode = Emergence::resolveFileFromParent('site-root', array_merge($resolvedPath, array($scriptHandle))))) { $resolvedNode = $childNode; if (is_a($resolvedNode, 'SiteFile')) { break; } } else { $resolvedNode = false; //break; } $resolvedPath[] = $handle; } } if ($resolvedNode) { // create session if (static::$autoCreateSession && $resolvedNode->MIMEType == 'application/php') { $GLOBALS['Session'] = UserSession::getFromRequest(); } if (is_callable(static::$onRequestMapped)) { call_user_func(static::$onRequestMapped, $resolvedNode); } if ($resolvedNode->MIMEType == 'application/php') { require $resolvedNode->RealPath; exit; } elseif (!is_callable(array($resolvedNode, 'outputAsResponse'))) { //throw new Exception('Node does not support rendering'); static::respondNotFound(); } else { $resolvedNode->outputAsResponse(); } } else { static::respondNotFound(); } }