Oppaitime's version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

view.class.php 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. class View
  3. {
  4. /**
  5. * @var string Path relative to where (P)HTML templates reside
  6. */
  7. const IncludePath = './design/views/';
  8. /**
  9. * This function is to include the header file on a page.
  10. *
  11. * @param $PageTitle the title of the page
  12. * @param $JSIncludes is a comma-separated list of JS files to be included on
  13. * the page. ONLY PUT THE RELATIVE LOCATION WITHOUT '.js'
  14. * example: 'somefile,somedir/somefile'
  15. */
  16. public static function show_header($PageTitle = '', $JSIncludes = '', $CSSIncludes = '')
  17. {
  18. global $Document, $Mobile, $Classes;
  19. if ($PageTitle != '') {
  20. $PageTitle .= ' :: ';
  21. }
  22. $PageTitle .= SITE_NAME;
  23. $PageID = array(
  24. $Document, // Document
  25. empty($_REQUEST['action']) ? false : $_REQUEST['action'], // Action
  26. empty($_REQUEST['type']) ? false : $_REQUEST['type'] // Type
  27. );
  28. if (!is_array(G::$LoggedUser) || empty(G::$LoggedUser['ID']) || $PageTitle == 'Recover Password :: ' . SITE_NAME) {
  29. require(SERVER_ROOT.'/design/publicheader.php');
  30. } else {
  31. // HTTP/2 Server Push headers for cloudflare
  32. $Scripts = array_merge(['jquery', 'global', 'ajax.class', 'jquery.autocomplete', 'autocomplete', 'tooltipster'], explode(',', $JSIncludes));
  33. $Styles = array_merge(['tooltipster'], explode(',', $CSSIncludes));
  34. foreach ($Scripts as $Script) {
  35. if (trim($Script) == '') {
  36. continue;
  37. }
  38. header('Link: <'.STATIC_SERVER.'functions/'.$Script.'.js?v='.filemtime(SERVER_ROOT.STATIC_SERVER.'functions/'.$Script.'.js').'>; rel=preload; as=script;', false);
  39. }
  40. header('Link: <'.STATIC_SERVER.'styles/global.css?v='.filemtime(SERVER_ROOT.STATIC_SERVER.'styles/global.css').'>; rel=preload; as=style', false);
  41. foreach ($Styles as $Style) {
  42. if (trim($Style) == '') {
  43. continue;
  44. }
  45. header('Link: <'.STATIC_SERVER.'styles/'.$Style.'/style.css?v='.filemtime(SERVER_ROOT.STATIC_SERVER.'styles/'.$Style.'/style.css').'>; rel=preload; as=style;', false);
  46. }
  47. require(SERVER_ROOT.'/design/privateheader.php');
  48. }
  49. }
  50. /**
  51. * This function is to include the footer file on a page.
  52. *
  53. * @param $Options an optional array that you can pass information to the
  54. * header through as well as setup certain limitations
  55. * Here is a list of parameters that work in the $Options array:
  56. * ['disclaimer'] = [boolean] (False) Displays the disclaimer in the footer
  57. */
  58. public static function show_footer($Options = [])
  59. {
  60. global $ScriptStartTime, $SessionID, $UserSessions, $Debug, $Time, $Mobile;
  61. if (!is_array(G::$LoggedUser) || (isset($Options['recover']) && $Options['recover'] === true)) {
  62. require(SERVER_ROOT.'/design/publicfooter.php');
  63. } else {
  64. require(SERVER_ROOT.'/design/privatefooter.php');
  65. }
  66. }
  67. /**
  68. * This is a generic function to load a template fromm /design and render it.
  69. * The template should be in /design/my_template_name.php, and have a class
  70. * in it called MyTemplateNameTemplate (my_template_name transformed to
  71. * MixedCase, with the word 'Template' appended).
  72. * This class should have a public static function render($Args), where
  73. * $Args is an associative array of the template variables.
  74. * You should note that by "Template", we mean "php file that outputs stuff".
  75. *
  76. * This function loads /design/$TemplateName.php, and then calls
  77. * render($Args) on the class.
  78. *
  79. * @param string $TemplateName The name of the template, in underscore_format
  80. * @param array $Args the arguments passed to the template.
  81. */
  82. public static function render_template($TemplateName, $Args)
  83. {
  84. static $LoadedTemplates; // Keep track of templates we've already loaded.
  85. $ClassName = '';
  86. if (isset($LoadedTemplates[$TemplateName])) {
  87. $ClassName = $LoadedTemplates[$TemplateName];
  88. } else {
  89. include(SERVER_ROOT.'/design/' . $TemplateName . '.php');
  90. // Turn template_name into TemplateName
  91. $ClassNameParts = explode('_', $TemplateName);
  92. foreach ($ClassNameParts as $Index => $Part) {
  93. $ClassNameParts[$Index] = ucfirst($Part);
  94. }
  95. $ClassName = implode($ClassNameParts). 'Template';
  96. $LoadedTemplates[$TemplateName] = $ClassName;
  97. }
  98. $ClassName::render($Args);
  99. }
  100. /**
  101. * This method is similar to render_template, but does not require a
  102. * template class.
  103. *
  104. * Instead, this method simply renders a PHP file (PHTML) with the supplied
  105. * variables.
  106. *
  107. * All files must be placed within {self::IncludePath}. Create and organize
  108. * new paths and files. (e.g.: /design/views/artist/, design/view/forums/, etc.)
  109. *
  110. * @static
  111. * @param string $TemplateFile A relative path to a PHTML file
  112. * @param array $Variables Assoc. array of variables to extract for the template
  113. * @param boolean $Buffer enables Output Buffer
  114. * @return boolean|string
  115. *
  116. * @example <pre><?php
  117. * // box.phtml
  118. * <p id="<?=$id?>">Data</p>
  119. *
  120. * // The variable $id within box.phtml will be filled by $some_id
  121. * View::parse('section/box.phtml', array('id' => $some_id));
  122. *
  123. * // Parse a template without outputing it
  124. * $SavedTemplate = View::parse('sec/tion/eg.php', $DataArray, true);
  125. * // later . . .
  126. * echo $SavedTemplate; // Output the buffer
  127. * </pre>
  128. */
  129. public static function parse($TemplateFile, array $Variables = [], $Buffer = false)
  130. {
  131. $Template = self::IncludePath . $TemplateFile;
  132. if (file_exists($Template)) {
  133. extract($Variables);
  134. if ($Buffer) {
  135. ob_start();
  136. include $Template;
  137. $Content = ob_get_contents();
  138. ob_end_clean();
  139. return $Content;
  140. }
  141. return include $Template;
  142. }
  143. }
  144. }