1
0

Base.php 3.6 KB

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