Base.php 4.6 KB

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