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.8KB

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