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 5.7KB

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