BioTorrents.de’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 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. declare(strict_types = 1);
  3. class View
  4. {
  5. /**
  6. * @var string Path relative to where (P)HTML templates reside
  7. */
  8. const IncludePath = './design/views/';
  9. /**
  10. * commonMeta
  11. */
  12. public function commonMeta()
  13. {
  14. $ENV = ENV::go();
  15. return $HTML = <<<HTML
  16. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  17. <meta name=viewport content="width=device-width, initial-scale=1">
  18. <!-- Default "index, follow" -->
  19. <meta name="robots" content="none" />
  20. <meta name="language" content="en-US" />
  21. <meta name="description" content="$ENV->DESCRIPTION" />
  22. <link rel="manifest" href="/manifest.php" />
  23. <link rel="shortcut icon" href="/static/common/icon.png" />
  24. <link rel="search" type="application/opensearchdescription+xml"
  25. title="$ENV->SITE_NAME"
  26. href="$ENV->STATIC_SERVER/opensearch.xml" />
  27. HTML;
  28. }
  29. /**
  30. * HTTP/2 Server Push headers for Cloudflare
  31. * @see https://blog.cloudflare.com/using-http-2-server-push-with-php/
  32. */
  33. public function pushAsset($uri, $type)
  34. {
  35. $ENV = ENV::go();
  36. # Bad URI or type
  37. if ((!$uri || !is_string($uri))
  38. || (!$type || !is_string($type))) {
  39. return error(404);
  40. }
  41. $filemtime = filemtime(SERVER_ROOT."/$uri");
  42. $integrity = base64_encode(hash_file($ENV->SRI, SERVER_ROOT."/$uri", true));
  43. # Send raw HTTP headers - preloading this way is bloat
  44. # 26 requests, 1.58 MB, 584ms
  45. # vs.
  46. # 10 requests, 730.8 KB, 460ms
  47. #header("Link: <$uri?v=$filemtime>; rel=preload; as=$type", false);
  48. switch ($type) {
  49. case 'script':
  50. $HTML = "<script src='$uri?v=$filemtime' integrity='$ENV->SRI-$integrity' crossorigin='anonymous'></script>";
  51. break;
  52. case 'style':
  53. $HTML = "<link rel='stylesheet' href='$uri?v$filemtime' integrity='$integrity' crossorigin='anonymous' />";
  54. break;
  55. case 'font':
  56. $HTML = "<link rel='preload' as='font' href='$uri?v$filemtime' integrity='$integrity' crossorigin='anonymous' />";
  57. break;
  58. default:
  59. break;
  60. }
  61. # Needs to echo into the page
  62. return $HTML;
  63. }
  64. /**
  65. * This function is to include the header file on a page.
  66. *
  67. * @param $PageTitle the title of the page
  68. * @param $JSIncludes is a comma-separated list of JS files to be included on
  69. * the page. ONLY PUT THE RELATIVE LOCATION WITHOUT '.js'
  70. * example: 'somefile,somedir/somefile'
  71. */
  72. public static function show_header($PageTitle = '', $JSIncludes = '', $CSSIncludes = '')
  73. {
  74. $ENV = ENV::go();
  75. global $Document, $Mobile, $Classes;
  76. if ($PageTitle !== '') {
  77. $PageTitle .= " $ENV->SEP ";
  78. }
  79. $PageTitle .= $ENV->SITE_NAME;
  80. $PageID = array(
  81. $Document, // Document
  82. empty($_REQUEST['action']) ? false : $_REQUEST['action'], // Action
  83. empty($_REQUEST['type']) ? false : $_REQUEST['type'] // Type
  84. );
  85. if (!is_array(G::$LoggedUser)
  86. || empty(G::$LoggedUser['ID'])
  87. || (isset($Options['recover']) && $Options['recover'] === true)) {
  88. require_once SERVER_ROOT.'/design/publicheader.php';
  89. } else {
  90. require_once SERVER_ROOT.'/design/privateheader.php';
  91. }
  92. }
  93. /**
  94. * This function is to include the footer file on a page.
  95. *
  96. * @param $Options an optional array that you can pass information to the
  97. * header through as well as setup certain limitations
  98. * Here is a list of parameters that work in the $Options array:
  99. * ['disclaimer'] = [boolean] (False) Displays the disclaimer in the footer
  100. */
  101. public static function show_footer($Options = [])
  102. {
  103. global $ScriptStartTime, $SessionID, $UserSessions, $Debug, $Time, $Mobile;
  104. if (!is_array(G::$LoggedUser)
  105. || empty(G::$LoggedUser['ID'])
  106. || (isset($Options['recover']) && $Options['recover'] === true)) {
  107. require_once SERVER_ROOT.'/design/publicfooter.php';
  108. } else {
  109. require_once SERVER_ROOT.'/design/privatefooter.php';
  110. }
  111. }
  112. /**
  113. * This is a generic function to load a template fromm /design and render it.
  114. * The template should be in /design/my_template_name.php, and have a class
  115. * in it called MyTemplateNameTemplate (my_template_name transformed to
  116. * MixedCase, with the word 'Template' appended).
  117. * This class should have a public static function render($Args), where
  118. * $Args is an associative array of the template variables.
  119. * You should note that by "Template", we mean "php file that outputs stuff".
  120. *
  121. * This function loads /design/$TemplateName.php, and then calls
  122. * render($Args) on the class.
  123. *
  124. * @param string $TemplateName The name of the template, in underscore_format
  125. * @param array $Args the arguments passed to the template.
  126. */
  127. public static function render_template($TemplateName, $Args)
  128. {
  129. static $LoadedTemplates; // Keep track of templates we've already loaded.
  130. $ClassName = '';
  131. if (isset($LoadedTemplates[$TemplateName])) {
  132. $ClassName = $LoadedTemplates[$TemplateName];
  133. } else {
  134. include SERVER_ROOT.'/design/'.$TemplateName.'.php';
  135. // Turn template_name into TemplateName
  136. $ClassNameParts = explode('_', $TemplateName);
  137. foreach ($ClassNameParts as $Index => $Part) {
  138. $ClassNameParts[$Index] = ucfirst($Part);
  139. }
  140. $ClassName = implode($ClassNameParts). 'Template';
  141. $LoadedTemplates[$TemplateName] = $ClassName;
  142. }
  143. $ClassName::render($Args);
  144. }
  145. /**
  146. * This method is similar to render_template, but does not require a
  147. * template class.
  148. *
  149. * Instead, this method simply renders a PHP file (PHTML) with the supplied
  150. * variables.
  151. *
  152. * All files must be placed within {self::IncludePath}. Create and organize
  153. * new paths and files. (e.g.: /design/views/artist/, design/view/forums/, etc.)
  154. *
  155. * @static
  156. * @param string $TemplateFile A relative path to a PHTML file
  157. * @param array $Variables Assoc. array of variables to extract for the template
  158. * @param boolean $Buffer enables Output Buffer
  159. * @return boolean|string
  160. *
  161. * @example <pre><?php
  162. * // box.phtml
  163. * <p id="<?=$id?>">Data</p>
  164. *
  165. * // The variable $id within box.phtml will be filled by $some_id
  166. * View::parse('section/box.phtml', array('id' => $some_id));
  167. *
  168. * // Parse a template without outputing it
  169. * $SavedTemplate = View::parse('sec/tion/eg.php', $DataArray, true);
  170. * // later . . .
  171. * echo $SavedTemplate; // Output the buffer
  172. * </pre>
  173. */
  174. public static function parse($TemplateFile, array $Variables = [], $Buffer = false)
  175. {
  176. $Template = self::IncludePath . $TemplateFile;
  177. if (file_exists($Template)) {
  178. extract($Variables);
  179. if ($Buffer) {
  180. ob_start();
  181. include $Template;
  182. $Content = ob_get_contents();
  183. ob_end_clean();
  184. return $Content;
  185. }
  186. return include $Template;
  187. }
  188. }
  189. }