/** * @see AbstractModeHandler::handle * @throws RuntimeException * @return SiteModeHandler this */ public function handle() { $className = ucfirst($this->request->getPage()) . 'Page'; $nameSpace = 'area\\' . $this->request->getArea() . '\\logic\\site\\' . $className; $styleSheet = new File('private/src/area/' . $this->request->getArea() . '/app/view/site/' . $this->request->getPage() . '.xsl'); if (!class_exists($nameSpace)) { $reason404 = 'class `' . $nameSpace . '`'; } elseif (!$styleSheet->isFile()) { $reason404 = 'stylesheet `' . $styleSheet . '`'; } if (isset($reason404)) { Logger::debug('HTTP-404 because: ' . $reason404 . ' not found'); return $this->error(404); } $formStatus = $this->getFormStatus(); if ($formStatus === null) { $requestData = $_POST; } else { $requestData = array(); } $page = new $nameSpace($this->config, $requestData); if (!$page instanceof AbstractSitePage) { throw new RuntimeException('class `' . $nameSpace . '` isn\'t instance of `' . AbstractSitePage::class . '`'); } if ($page->access() !== true) { return $this->error(403); } if ($formStatus !== null) { if ($formStatus === AbstractSitePage::STATUS_RESTORED) { foreach ($this->getStoredFormData() as $name => $value) { $page->setFormData($name, $value); } } $page->setUp(); } else { if ($page->checkExpectation() === true && $page->check() === true) { $formStatus = AbstractSitePage::STATUS_ACCEPTED; $this->closeStoredForm($requestData['formId']); } else { $formStatus = AbstractSitePage::STATUS_REJECTED; $this->saveForm($requestData); } } $page->formData->setAttribute('status', $formStatus); $page->response->getRootNode()->addChild($this->getTextNode()); echo (new XSLProcessor())->setStyleSheetFile($styleSheet)->setDocumentDoc($page->response->toDomDoc())->transform(); # output: XML-Tree into temporary File if (isset($this->settings['tempXMLFile']) && $this->settings['tempXMLFile'] === true) { $xmlFile = new File(self::PATH_TEMP_XML); $xmlFile->putContents($page->response->getSource(true), false); } return $this; }
/** * @return RestModeHandler this */ public function handle() { $nameSpace = $this->getNameSpace(); if (!class_exists($nameSpace)) { return $this->error(404); } $requestMethod = strtolower($this->getRequestMethod()); if ($requestMethod === null) { return $this->error(405); } $object = new $nameSpace($this->config); if (!$object instanceof AbstractRestPage) { Logger::error('class `' . $nameSpace . '` is not instance of `' . AbstractRestPage::class . '`'); return $this->error(500); } if ($object->access() !== true) { return $this->error(403); } if (isset($this->request->getInputList()[0])) { $method = $requestMethod . ucfirst(strtolower($this->request->getInputList()[0])); if (method_exists($object, $method)) { $responseCode = $object->{$method}($this->request); } } if (!isset($responseCode)) { $method = $requestMethod . 'Index'; if (method_exists($object, $method)) { $responseCode = $object->{$method}($this->request); } } if (!isset($responseCode)) { return $this->error(501); } if ($responseCode === true) { $responseCode = 200; } elseif ($responseCode === false) { $responseCode = 400; } if (!HttpConst::isStatus($responseCode)) { $dataText = var_export($responseCode, true); $callText = get_class($object) . '::' . (isset($method) ? $method : ''); Logger::error('invalid response `' . $dataText . '` of `' . $callText . '`'); return $this->error(500); } echo $object->getResponse()->toJson(); http_response_code($responseCode); return $this; }
/** * initialize application and load configs */ public function __construct() { include_once self::AUTOLOAD; if (strtolower(ini_get('display_errors')) === 'off') { define('ENVIRONMENT', 'LIVE'); } else { define('ENVIRONMENT', 'DEV'); } if (!session_start()) { Logger::error('failed to start session'); exit; } # load public & private config $this->config = (new Bucket())->applyIni(new File(self::CONFIG_PUBLIC))->applyIni(new File(self::CONFIG_PRIVATE)); # load MAPUrl if (stripos($_SERVER['SERVER_PROTOCOL'], 'HTTPS') !== false) { $scheme = 'https'; } else { $scheme = 'http'; } $this->request = new MAPUrl($scheme . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], $this->config); Logger::debug('REQUEST (' . 'mode: `' . $this->request->getMode() . '` ' . 'area: `' . $this->request->getArea() . '` ' . 'page: `' . $this->request->getPage() . '`'); # load area & page config $configPathList = array('private/src/area/' . $this->request->getArea() . '/config/area.ini', 'private/src/area/' . $this->request->getArea() . '/config/page/' . $this->request->getPage() . '.ini'); foreach ($configPathList as $configPath) { $configFile = new File($configPath); if (!$configFile->isFile()) { if (constant('ENVIRONMENT') === 'DEV') { Logger::info('config-file `' . $configFile . '` not found'); } continue; } $this->config->applyIni($configFile); } # load session config if (!isset($_SESSION['config'])) { $_SESSION['config'] = array(); } $this->config->applyArray($_SESSION['config']); }
/** * @param null|string $content * @return Node this */ public final function setContent($content) { if (is_object($content) && method_exists($content, '__toString')) { $content = (string) $content; } if ($content === null || is_string($content) || is_int($content)) { $this->content = $content; } else { Logger::warning('ignored node content of type `' . gettype($content) . '`'); } return $this; }
/** * @param int $code * @return AbstractModeHandler this */ protected function error($code) { if (!HttpConst::isStatus($code)) { throw new RuntimeException('unknown HTTP-Status Code `' . $code . '`'); } http_response_code($code); # pipe to URL if (isset($this->settings['err' . $code . '-pipe'])) { $target = new MAPUrl($this->settings['err' . $code . '-pipe'], $this->config); if ($target->get() === $this->request->get()) { Logger::error('endless pipe-loop (status: `' . $code . '`) - interrupted with HTTP-Status `508`'); return $this->error(508); } $this->setLocation(new Url($this->settings['err' . $code . '-pipe'])); return $this; } # default error output if (defined('peer\\http\\HttpConst::STATUS_' . $code)) { $message = constant('peer\\http\\HttpConst::STATUS_' . $code); } else { $message = 'N/A'; } $this->setContentType('text/plain'); echo '[' . $code . '] ' . $message; return $this; }