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.

189 lines
4.5 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Christopher Schäpers <kondou@ts.unde.re>
  8. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  9. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  10. * @author Julius Härtl <jus@bitgrid.net>
  11. * @author Lukas Reschke <lukas@statuscode.ch>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OC\Template;
  31. use OCP\Defaults;
  32. class Base {
  33. private $template; // The template
  34. private $vars; // Vars
  35. /** @var \OCP\IL10N */
  36. private $l10n;
  37. /** @var Defaults */
  38. private $theme;
  39. /**
  40. * @param string $template
  41. * @param string $requestToken
  42. * @param \OCP\IL10N $l10n
  43. * @param Defaults $theme
  44. */
  45. public function __construct($template, $requestToken, $l10n, $theme) {
  46. $this->vars = [];
  47. $this->vars['requesttoken'] = $requestToken;
  48. $this->l10n = $l10n;
  49. $this->template = $template;
  50. $this->theme = $theme;
  51. }
  52. /**
  53. * @param string $serverRoot
  54. * @param string|false $app_dir
  55. * @param string $theme
  56. * @param string $app
  57. * @return string[]
  58. */
  59. protected function getAppTemplateDirs($theme, $app, $serverRoot, $app_dir) {
  60. // Check if the app is in the app folder or in the root
  61. if (file_exists($app_dir.'/templates/')) {
  62. return [
  63. $serverRoot.'/themes/'.$theme.'/apps/'.$app.'/templates/',
  64. $app_dir.'/templates/',
  65. ];
  66. }
  67. return [
  68. $serverRoot.'/themes/'.$theme.'/'.$app.'/templates/',
  69. $serverRoot.'/'.$app.'/templates/',
  70. ];
  71. }
  72. /**
  73. * @param string $serverRoot
  74. * @param string $theme
  75. * @return string[]
  76. */
  77. protected function getCoreTemplateDirs($theme, $serverRoot) {
  78. return [
  79. $serverRoot.'/themes/'.$theme.'/core/templates/',
  80. $serverRoot.'/core/templates/',
  81. ];
  82. }
  83. /**
  84. * Assign variables
  85. * @param string $key key
  86. * @param array|bool|integer|string $value value
  87. * @return bool
  88. *
  89. * This function assigns a variable. It can be accessed via $_[$key] in
  90. * the template.
  91. *
  92. * If the key existed before, it will be overwritten
  93. */
  94. public function assign($key, $value) {
  95. $this->vars[$key] = $value;
  96. return true;
  97. }
  98. /**
  99. * Appends a variable
  100. * @param string $key key
  101. * @param mixed $value value
  102. *
  103. * This function assigns a variable in an array context. If the key already
  104. * exists, the value will be appended. It can be accessed via
  105. * $_[$key][$position] in the template.
  106. */
  107. public function append($key, $value) {
  108. if (array_key_exists($key, $this->vars)) {
  109. $this->vars[$key][] = $value;
  110. } else {
  111. $this->vars[$key] = [ $value ];
  112. }
  113. }
  114. /**
  115. * Prints the proceeded template
  116. * @return bool
  117. *
  118. * This function proceeds the template and prints its output.
  119. */
  120. public function printPage() {
  121. $data = $this->fetchPage();
  122. if ($data === false) {
  123. return false;
  124. } else {
  125. print $data;
  126. return true;
  127. }
  128. }
  129. /**
  130. * Process the template
  131. *
  132. * @param array|null $additionalParams
  133. * @return string This function processes the template.
  134. *
  135. * This function processes the template.
  136. */
  137. public function fetchPage($additionalParams = null) {
  138. return $this->load($this->template, $additionalParams);
  139. }
  140. /**
  141. * doing the actual work
  142. *
  143. * @param string $file
  144. * @param array|null $additionalParams
  145. * @return string content
  146. *
  147. * Includes the template file, fetches its output
  148. */
  149. protected function load($file, $additionalParams = null) {
  150. // Register the variables
  151. $_ = $this->vars;
  152. $l = $this->l10n;
  153. $theme = $this->theme;
  154. if (!is_null($additionalParams)) {
  155. $_ = array_merge($additionalParams, $this->vars);
  156. foreach ($_ as $var => $value) {
  157. ${$var} = $value;
  158. }
  159. }
  160. // Include
  161. ob_start();
  162. try {
  163. include $file;
  164. $data = ob_get_contents();
  165. } catch (\Exception $e) {
  166. @ob_end_clean();
  167. throw $e;
  168. }
  169. @ob_end_clean();
  170. // Return data
  171. return $data;
  172. }
  173. }