/** * @param File $document * @throws Exception * @return XSLProcessor this */ public final function setDocumentFile(File $document) { if (!$document->isFile()) { throw new Exception('file `' . $document . '` not found'); } $this->document = new DOMDocument(); $this->document->load($document); return $this; }
/** * @param string $area * @return bool */ public function isArea($area) { if (!self::match(self::PATTERN_AREA, $area)) { return false; } if ($this->config === null) { return true; } $areaDir = new File('private/src/area/' . $area . '/'); return $areaDir->isDir(); }
/** * 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']); }
/** * write logs * * @param string $type * @param string $message * @throws RuntimeException if failed to create log-dir * @throws RuntimeException if log-file exists & is dir or link */ protected static function log($type, $message) { $now = new DateTime(); # create dir $logFile = new File(self::LOG_DIR . $now->format('Y-M')); if (!$logFile->isDir()) { $logFile->makeDir(); } # check file $logFile->attach($now->format('d') . '.log'); if (!$logFile->isFile() && $logFile->exists()) { throw new RuntimeException('log-file is not a file `' . $logFile . '`'); } # write log $time = $now->format('H:i:s'); $logFile->putContents('[' . $type . ' @ ' . $time . '] ' . $message . PHP_EOL); }
/** * @param File $iniFile * @throws RuntimeException if file not exist * @return Bucket */ public final function applyIni(File $iniFile) { if ($iniFile === null || !$iniFile->isFile()) { throw new RuntimeException('file not exists `' . $iniFile . '`'); } return $this->applyArray(parse_ini_file($iniFile, true, INI_SCANNER_TYPED)); }
/** * @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; }