123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
- class nsGenerateContent {
-
- // Resource path normally same as component
- private $resPath;
- // Content path is resPath + /content
- private $contentPath;
- // Content path is resPath + /posts
- private $postsPath;
- // Skin path is resPath + /skin
- private $skinPath;
- // Holds the component template
- private $template;
- // Holds the page content
- private $content;
- // Holds the page metadata
- private $metadata;
- // Temporarily stores the final output which is template + content + processing
- // This is then sent to the utils to be output'd
- private $finalOutput;
- /********************************************************************************************************************
- * Class constructor that sets initial state of things
- ********************************************************************************************************************/
- function __construct() {
- $this->resPath = gGetConfig('constant.components.' . gGetConfig('app.component'));
- $this->resPath = dirname(str_replace('/src', kEmptyString, $this->resPath));
- $this->skinPath = gBuildPath($this->resPath, 'skin');
- $this->contentPath = gBuildPath($this->resPath, 'content');
- $this->postsPath = gBuildPath($this->resPath, 'posts');
- $this->template = gReadFile(gBuildPath($this->skinPath, 'template.html'));
- if (!$this->template) {
- gError('Unable to load skin resources.');
- }
- }
- /********************************************************************************************************************
- * Load static content from component resources
- ********************************************************************************************************************/
- public function LoadStaticContent() {
- $requestURI = gGetConfig('network.requestPath');
- if (str_contains($requestURI, kDotDot)) {
- gError('Reference Code: ID-10-T');
- }
- $contentFilename = 'index.mtz';
- $contentFilepath = gBuildPath($this->contentPath, $requestURI, $contentFilename);
- if (!file_exists($contentFilepath)) {
- gNotFound('Content could not be loaded.');
- }
- $metacontent = $this->_ParseYaml(gReadFile($contentFilepath));
- $this->metadata = $metacontent['data'];
- $this->content = $metacontent['content'];
- $this->Display();
- }
- /********************************************************************************************************************
- * Display (Output) the final content
- ********************************************************************************************************************/
- public function Display() {
- $this->content = $this->_ParseCodeTags($this->metadata['type'] ?? 'html', $this->content);
- $substs = array(
- 'PAGE_CONTENT' => $this->content,
- 'SOFTWARE_NAME' => kEmptyString,
- 'SOFTWARE_VERSION' => kEmptyString,
- 'SOFTWARE_DEBUG' => kEmptyString,
- 'CONTENT_PATH' => nsUtils::StripStr($this->contentPath, kRootPath) . kSlash . gGetConfig('network.requestPath'),
- 'BASE_SKIN_PATH' => kDefaultSkinPath,
- 'COMPONENT_SKIN_PATH' => nsUtils::StripStr($this->skinPath, kRootPath),
- 'SITE_NAME' => gGetConfig('console.ui.siteName', kAppName),
- 'SITE_MENU' => kEmptyString,
- 'SITE_SECTION' => gGetConfig('app.path.0', kEmptyString),
- 'PAGE_TITLE' => $this->metadata['title'],
- 'PAGE_HEADING' => $this->metadata['heading'],
- 'PAGE_TAGLINE' => $this->metadata['tagline'],
- 'PAGE_MENU' => kEmptyString,
- 'PAGE_SIDEBAR' => kEmptyString,
- 'PAGE_PATH' => gGetConfig('network.requestPath'),
- 'PAGE_PATH_0' => gGetConfig('app.path.0', kEmptyString),
- 'PAGE_PATH_1' => gGetConfig('app.path.1', kEmptyString),
- 'PAGE_PATH_2' => gGetConfig('app.path.2', kEmptyString),
- 'PAGE_PATH_3' => gGetConfig('app.path.3', kEmptyString),
- 'CURRENT_YEAR' => date("Y"),
- 'CACHE_EPOCH' => time(),
- );
- $this->finalOutput = nsUtils::SubstEx($this->template, $substs, kLeftBrace . kDollar);
- gSetConfig('console.output.responseBody', $this->finalOutput);
- gOutput(true, 'html');
- }
- /********************************************************************************************************************
- * Get yaml header as an array and strip the content of the yaml header
- ********************************************************************************************************************/
- private function _ParseYaml($aContent) {
- return ['data' => gEnsureValue(@yaml_parse($aContent)),
- 'content' => preg_replace(nsUtils::REGEX_PATTERNS['yaml'], kEmptyString, $aContent)];
- }
- /********************************************************************************************************************
- * Translates content with bbCode-like tags to HTML or returns already used HTML depending on type
- ********************************************************************************************************************/
- private function _ParseCodeTags($aType, $aContent) {
- if (!gEnsureValue($aContent)) {
- return null;
- }
- switch ($aType) {
- case 'phoebus':
- // This is the phoebusCode mangling that needs to die in a fire
- $aContent = htmlentities($aContent, ENT_XHTML);
- // Replace new lines with <br />
- $aContent = nl2br($aContent, true);
- $simpleTags = array(
- '[b]' => '<strong>',
- '[/b]' => '</strong>',
- '[i]' => '<em>',
- '[/i]' => '</em>',
- '[u]' => '<u>',
- '[/u]' => '</u>',
- '[ul]' => '</p><ul><fixme />',
- '[/ul]' => '</ul><p><fixme />',
- '[ol]' => '</p><ol><fixme />',
- '[/ol]' => '</ol><p><fixme />',
- '[li]' => '<li>',
- '[/li]' => '</li>',
- '[section]' => '</p><h3>',
- '[/section]' => '</h3><p><fixme />'
- );
- $regexTags = array(
- '\<(ul|\/ul|li|\/li|p|\/p)\><br \/>' => '<$1>',
- '\[url=(.*)\](.*)\[\/url\]' => '<a href="$1" target="_blank">$2</a>',
- '\[url\](.*)\[\/url\]' => '<a href="$1" target="_blank">$1</a>',
- '\[img(.*)\](.*)\[\/img\]' => kEmptyString,
- );
- // Process the substs
- $aContent = gSubst(gSubst($aContent, $simpleTags), $regexTags, true);
- // Less hacky than what is in funcReadManifest
- // Remove linebreak special cases
- $aContent = str_replace('<fixme /><br />', kEmptyString, $aContent);
- break;
- case 'selene':
- case 'phobos':
- case 'mtz':
- $aContent = htmlentities($aContent, ENT_XHTML);
- $htmlTags = implode(kPipe, ['p', 'span', 'small', 'br', 'hr', 'ul', 'ol', 'li', 'table', 'th', 'tr', 'td',
- 'caption', 'col', 'colgroup', 'thead', 'tbody', 'tfoot']);
- $regexTags = array(
- "\[\/(" . $htmlTags . ")\]" => '</$1>',
- "\[(" . $htmlTags . ")\]" => '<$1>',
- "\[break\]" => '<br />',
- "\[dblbreak\]" => '<br /><br/>',
- "\[separator\]" => '<hr />',
- "\[header=\"(.*)\"\]" => '<h2>$1</h2>',
- "\[header](.*)\[\/header\]" => '<h2>$1</h2>',
- "\[section=\"(.*)\"\]" => '<h3>$1</h3>',
- "\[section](.*)\[\/section\]" => '<h3>$1</h3>',
- "\[b](.*)\[\/b\]" => '<strong>$1</strong>',
- "\[i](.*)\[\/i\]" => '<em>$1</em>',
- "\[u](.*)\[\/u\]" => '<u>$1</u>',
- "\[anchor=(.*)\]" => '<a name="$1"></a>',
- "\[link=(.*)\](.*)\[\/link\]" => '<a href="$1">$2</a>',
- "\[url=(.*)\](.*)\[\/url\]" => '<a href="$1" target="_blank">$2</a>',
- "\[url\](.*)\[\/url\]" => '<a href="$1" target="_blank">$1</a>',
- '\[img(.*)\](.*)\[\/img\]' => '<img src="$2"$1 />',
- );
- // Finally process the regex substs
- $aContent = gSubst($aContent, $regexTags, true);
-
- break;
- }
- // And return
- return $aContent;
- }
- }
- ?>
|