Base.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Template;
  8. use OCP\Defaults;
  9. use Throwable;
  10. class Base {
  11. private $template; // The template
  12. private $vars; // Vars
  13. /** @var \OCP\IL10N */
  14. private $l10n;
  15. /** @var Defaults */
  16. private $theme;
  17. /**
  18. * @param string $template
  19. * @param string $requestToken
  20. * @param \OCP\IL10N $l10n
  21. * @param string $cspNonce
  22. * @param Defaults $theme
  23. */
  24. public function __construct($template, $requestToken, $l10n, $theme, $cspNonce) {
  25. $this->vars = [
  26. 'cspNonce' => $cspNonce,
  27. 'requesttoken' => $requestToken,
  28. ];
  29. $this->l10n = $l10n;
  30. $this->template = $template;
  31. $this->theme = $theme;
  32. }
  33. /**
  34. * @param string $serverRoot
  35. * @param string|false $app_dir
  36. * @param string $theme
  37. * @param string $app
  38. * @return string[]
  39. */
  40. protected function getAppTemplateDirs($theme, $app, $serverRoot, $app_dir) {
  41. // Check if the app is in the app folder or in the root
  42. if ($app_dir !== false && file_exists($app_dir.'/templates/')) {
  43. return [
  44. $serverRoot.'/themes/'.$theme.'/apps/'.$app.'/templates/',
  45. $app_dir.'/templates/',
  46. ];
  47. }
  48. return [
  49. $serverRoot.'/themes/'.$theme.'/'.$app.'/templates/',
  50. $serverRoot.'/'.$app.'/templates/',
  51. ];
  52. }
  53. /**
  54. * @param string $serverRoot
  55. * @param string $theme
  56. * @return string[]
  57. */
  58. protected function getCoreTemplateDirs($theme, $serverRoot) {
  59. return [
  60. $serverRoot.'/themes/'.$theme.'/core/templates/',
  61. $serverRoot.'/core/templates/',
  62. ];
  63. }
  64. /**
  65. * Assign variables
  66. * @param string $key key
  67. * @param float|array|bool|integer|string|Throwable $value value
  68. * @return bool
  69. *
  70. * This function assigns a variable. It can be accessed via $_[$key] in
  71. * the template.
  72. *
  73. * If the key existed before, it will be overwritten
  74. */
  75. public function assign($key, $value) {
  76. $this->vars[$key] = $value;
  77. return true;
  78. }
  79. /**
  80. * Appends a variable
  81. * @param string $key key
  82. * @param mixed $value value
  83. *
  84. * This function assigns a variable in an array context. If the key already
  85. * exists, the value will be appended. It can be accessed via
  86. * $_[$key][$position] in the template.
  87. */
  88. public function append($key, $value) {
  89. if (array_key_exists($key, $this->vars)) {
  90. $this->vars[$key][] = $value;
  91. } else {
  92. $this->vars[$key] = [ $value ];
  93. }
  94. }
  95. /**
  96. * Prints the proceeded template
  97. * @return bool
  98. *
  99. * This function proceeds the template and prints its output.
  100. */
  101. public function printPage() {
  102. $data = $this->fetchPage();
  103. if ($data === false) {
  104. return false;
  105. } else {
  106. print $data;
  107. return true;
  108. }
  109. }
  110. /**
  111. * Process the template
  112. *
  113. * @param array|null $additionalParams
  114. * @return string This function processes the template.
  115. *
  116. * This function processes the template.
  117. */
  118. public function fetchPage($additionalParams = null) {
  119. return $this->load($this->template, $additionalParams);
  120. }
  121. /**
  122. * doing the actual work
  123. *
  124. * @param string $file
  125. * @param array|null $additionalParams
  126. * @return string content
  127. *
  128. * Includes the template file, fetches its output
  129. */
  130. protected function load($file, $additionalParams = null) {
  131. // Register the variables
  132. $_ = $this->vars;
  133. $l = $this->l10n;
  134. $theme = $this->theme;
  135. if (!is_null($additionalParams)) {
  136. $_ = array_merge($additionalParams, $this->vars);
  137. foreach ($_ as $var => $value) {
  138. if (!isset(${$var})) {
  139. ${$var} = $value;
  140. }
  141. }
  142. }
  143. // Include
  144. ob_start();
  145. try {
  146. include $file;
  147. $data = ob_get_contents();
  148. } catch (\Exception $e) {
  149. @ob_end_clean();
  150. throw $e;
  151. }
  152. @ob_end_clean();
  153. // Return data
  154. return $data;
  155. }
  156. }