nsGenerateContent.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. class nsGenerateContent {
  3. // Resource path normally same as component
  4. private $resPath;
  5. // Content path is resPath + /content
  6. private $contentPath;
  7. // Content path is resPath + /posts
  8. private $postsPath;
  9. // Skin path is resPath + /skin
  10. private $skinPath;
  11. // Holds the component template
  12. private $template;
  13. // Holds the page content
  14. private $content;
  15. // Holds the page metadata
  16. private $metadata;
  17. // Temporarily stores the final output which is template + content + processing
  18. // This is then sent to the utils to be output'd
  19. private $finalOutput;
  20. /********************************************************************************************************************
  21. * Class constructor that sets initial state of things
  22. ********************************************************************************************************************/
  23. function __construct() {
  24. $this->resPath = gGetConfig('constant.components.' . gGetConfig('app.component'));
  25. $this->resPath = dirname(str_replace('/src', kEmptyString, $this->resPath));
  26. $this->skinPath = gBuildPath($this->resPath, 'skin');
  27. $this->contentPath = gBuildPath($this->resPath, 'content');
  28. $this->postsPath = gBuildPath($this->resPath, 'posts');
  29. $this->template = gReadFile(gBuildPath($this->skinPath, 'template.html'));
  30. if (!$this->template) {
  31. gError('Unable to load skin resources.');
  32. }
  33. }
  34. /********************************************************************************************************************
  35. * Load static content from component resources
  36. ********************************************************************************************************************/
  37. public function LoadStaticContent() {
  38. $requestURI = gGetConfig('network.requestPath');
  39. if (str_contains($requestURI, kDotDot)) {
  40. gError('Reference Code: ID-10-T');
  41. }
  42. $contentFilename = 'index.mtz';
  43. $contentFilepath = gBuildPath($this->contentPath, $requestURI, $contentFilename);
  44. if (!file_exists($contentFilepath)) {
  45. gNotFound('Content could not be loaded.');
  46. }
  47. $metacontent = $this->_ParseYaml(gReadFile($contentFilepath));
  48. $this->metadata = $metacontent['data'];
  49. $this->content = $metacontent['content'];
  50. $this->Display();
  51. }
  52. /********************************************************************************************************************
  53. * Display (Output) the final content
  54. ********************************************************************************************************************/
  55. public function Display() {
  56. $this->content = $this->_ParseCodeTags($this->metadata['type'] ?? 'html', $this->content);
  57. $substs = array(
  58. 'PAGE_CONTENT' => $this->content,
  59. 'SOFTWARE_NAME' => kEmptyString,
  60. 'SOFTWARE_VERSION' => kEmptyString,
  61. 'SOFTWARE_DEBUG' => kEmptyString,
  62. 'CONTENT_PATH' => nsUtils::StripStr($this->contentPath, kRootPath) . kSlash . gGetConfig('network.requestPath'),
  63. 'BASE_SKIN_PATH' => kDefaultSkinPath,
  64. 'COMPONENT_SKIN_PATH' => nsUtils::StripStr($this->skinPath, kRootPath),
  65. 'SITE_NAME' => gGetConfig('console.ui.siteName', kAppName),
  66. 'SITE_MENU' => kEmptyString,
  67. 'SITE_SECTION' => gGetConfig('app.path.0', kEmptyString),
  68. 'PAGE_TITLE' => $this->metadata['title'],
  69. 'PAGE_HEADING' => $this->metadata['heading'],
  70. 'PAGE_TAGLINE' => $this->metadata['tagline'],
  71. 'PAGE_MENU' => kEmptyString,
  72. 'PAGE_SIDEBAR' => kEmptyString,
  73. 'PAGE_PATH' => gGetConfig('network.requestPath'),
  74. 'PAGE_PATH_0' => gGetConfig('app.path.0', kEmptyString),
  75. 'PAGE_PATH_1' => gGetConfig('app.path.1', kEmptyString),
  76. 'PAGE_PATH_2' => gGetConfig('app.path.2', kEmptyString),
  77. 'PAGE_PATH_3' => gGetConfig('app.path.3', kEmptyString),
  78. 'CURRENT_YEAR' => date("Y"),
  79. 'CACHE_EPOCH' => time(),
  80. );
  81. $this->finalOutput = nsUtils::SubstEx($this->template, $substs, kLeftBrace . kDollar);
  82. gSetConfig('console.output.responseBody', $this->finalOutput);
  83. gOutput(true, 'html');
  84. }
  85. /********************************************************************************************************************
  86. * Get yaml header as an array and strip the content of the yaml header
  87. ********************************************************************************************************************/
  88. private function _ParseYaml($aContent) {
  89. return ['data' => gEnsureValue(@yaml_parse($aContent)),
  90. 'content' => preg_replace(nsUtils::REGEX_PATTERNS['yaml'], kEmptyString, $aContent)];
  91. }
  92. /********************************************************************************************************************
  93. * Translates content with bbCode-like tags to HTML or returns already used HTML depending on type
  94. ********************************************************************************************************************/
  95. private function _ParseCodeTags($aType, $aContent) {
  96. if (!gEnsureValue($aContent)) {
  97. return null;
  98. }
  99. switch ($aType) {
  100. case 'phoebus':
  101. // This is the phoebusCode mangling that needs to die in a fire
  102. $aContent = htmlentities($aContent, ENT_XHTML);
  103. // Replace new lines with <br />
  104. $aContent = nl2br($aContent, true);
  105. $simpleTags = array(
  106. '[b]' => '<strong>',
  107. '[/b]' => '</strong>',
  108. '[i]' => '<em>',
  109. '[/i]' => '</em>',
  110. '[u]' => '<u>',
  111. '[/u]' => '</u>',
  112. '[ul]' => '</p><ul><fixme />',
  113. '[/ul]' => '</ul><p><fixme />',
  114. '[ol]' => '</p><ol><fixme />',
  115. '[/ol]' => '</ol><p><fixme />',
  116. '[li]' => '<li>',
  117. '[/li]' => '</li>',
  118. '[section]' => '</p><h3>',
  119. '[/section]' => '</h3><p><fixme />'
  120. );
  121. $regexTags = array(
  122. '\<(ul|\/ul|li|\/li|p|\/p)\><br \/>' => '<$1>',
  123. '\[url=(.*)\](.*)\[\/url\]' => '<a href="$1" target="_blank">$2</a>',
  124. '\[url\](.*)\[\/url\]' => '<a href="$1" target="_blank">$1</a>',
  125. '\[img(.*)\](.*)\[\/img\]' => kEmptyString,
  126. );
  127. // Process the substs
  128. $aContent = gSubst(gSubst($aContent, $simpleTags), $regexTags, true);
  129. // Less hacky than what is in funcReadManifest
  130. // Remove linebreak special cases
  131. $aContent = str_replace('<fixme /><br />', kEmptyString, $aContent);
  132. break;
  133. case 'selene':
  134. case 'phobos':
  135. case 'mtz':
  136. $aContent = htmlentities($aContent, ENT_XHTML);
  137. $htmlTags = implode(kPipe, ['p', 'span', 'small', 'br', 'hr', 'ul', 'ol', 'li', 'table', 'th', 'tr', 'td',
  138. 'caption', 'col', 'colgroup', 'thead', 'tbody', 'tfoot']);
  139. $regexTags = array(
  140. "\[\/(" . $htmlTags . ")\]" => '</$1>',
  141. "\[(" . $htmlTags . ")\]" => '<$1>',
  142. "\[break\]" => '<br />',
  143. "\[dblbreak\]" => '<br /><br/>',
  144. "\[separator\]" => '<hr />',
  145. "\[header=\"(.*)\"\]" => '<h2>$1</h2>',
  146. "\[header](.*)\[\/header\]" => '<h2>$1</h2>',
  147. "\[section=\"(.*)\"\]" => '<h3>$1</h3>',
  148. "\[section](.*)\[\/section\]" => '<h3>$1</h3>',
  149. "\[b](.*)\[\/b\]" => '<strong>$1</strong>',
  150. "\[i](.*)\[\/i\]" => '<em>$1</em>',
  151. "\[u](.*)\[\/u\]" => '<u>$1</u>',
  152. "\[anchor=(.*)\]" => '<a name="$1"></a>',
  153. "\[link=(.*)\](.*)\[\/link\]" => '<a href="$1">$2</a>',
  154. "\[url=(.*)\](.*)\[\/url\]" => '<a href="$1" target="_blank">$2</a>',
  155. "\[url\](.*)\[\/url\]" => '<a href="$1" target="_blank">$1</a>',
  156. '\[img(.*)\](.*)\[\/img\]' => '<img src="$2"$1 />',
  157. );
  158. // Finally process the regex substs
  159. $aContent = gSubst($aContent, $regexTags, true);
  160. break;
  161. }
  162. // And return
  163. return $aContent;
  164. }
  165. }
  166. ?>