Base.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 Jörn Friedrich Dreyer <jfd@butonic.de>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OC\Template;
  29. class Base {
  30. private $template; // The template
  31. private $vars; // Vars
  32. /** @var \OCP\IL10N */
  33. private $l10n;
  34. /** @var \OC_Defaults */
  35. private $theme;
  36. /**
  37. * @param string $template
  38. * @param string $requestToken
  39. * @param \OCP\IL10N $l10n
  40. * @param \OC_Defaults $theme
  41. */
  42. public function __construct($template, $requestToken, $l10n, $theme ) {
  43. $this->vars = array();
  44. $this->vars['requesttoken'] = $requestToken;
  45. $this->l10n = $l10n;
  46. $this->template = $template;
  47. $this->theme = $theme;
  48. }
  49. /**
  50. * @param string $serverRoot
  51. * @param string|false $app_dir
  52. * @param string $theme
  53. * @param string $app
  54. * @return string[]
  55. */
  56. protected function getAppTemplateDirs($theme, $app, $serverRoot, $app_dir) {
  57. // Check if the app is in the app folder or in the root
  58. if( file_exists($app_dir.'/templates/' )) {
  59. return [
  60. $serverRoot.'/themes/'.$theme.'/apps/'.$app.'/templates/',
  61. $app_dir.'/templates/',
  62. ];
  63. }
  64. return [
  65. $serverRoot.'/themes/'.$theme.'/'.$app.'/templates/',
  66. $serverRoot.'/'.$app.'/templates/',
  67. ];
  68. }
  69. /**
  70. * @param string $serverRoot
  71. * @param string $theme
  72. * @return string[]
  73. */
  74. protected function getCoreTemplateDirs($theme, $serverRoot) {
  75. return [
  76. $serverRoot.'/themes/'.$theme.'/core/templates/',
  77. $serverRoot.'/core/templates/',
  78. ];
  79. }
  80. /**
  81. * Assign variables
  82. * @param string $key key
  83. * @param array|bool|integer|string $value value
  84. * @return bool
  85. *
  86. * This function assigns a variable. It can be accessed via $_[$key] in
  87. * the template.
  88. *
  89. * If the key existed before, it will be overwritten
  90. */
  91. public function assign( $key, $value) {
  92. $this->vars[$key] = $value;
  93. return true;
  94. }
  95. /**
  96. * Appends a variable
  97. * @param string $key key
  98. * @param mixed $value value
  99. * @return boolean|null
  100. *
  101. * This function assigns a variable in an array context. If the key already
  102. * exists, the value will be appended. It can be accessed via
  103. * $_[$key][$position] in the template.
  104. */
  105. public function append( $key, $value ) {
  106. if( array_key_exists( $key, $this->vars )) {
  107. $this->vars[$key][] = $value;
  108. }
  109. else{
  110. $this->vars[$key] = array( $value );
  111. }
  112. }
  113. /**
  114. * Prints the proceeded template
  115. * @return bool
  116. *
  117. * This function proceeds the template and prints its output.
  118. */
  119. public function printPage() {
  120. $data = $this->fetchPage();
  121. if( $data === false ) {
  122. return false;
  123. }
  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. }
  157. // Include
  158. ob_start();
  159. try {
  160. include $file;
  161. $data = ob_get_contents();
  162. } catch (\Exception $e) {
  163. @ob_end_clean();
  164. throw $e;
  165. }
  166. @ob_end_clean();
  167. // Return data
  168. return $data;
  169. }
  170. }