Base.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. use OCP\Defaults;
  30. class Base {
  31. private $template; // The template
  32. private $vars; // Vars
  33. /** @var \OCP\IL10N */
  34. private $l10n;
  35. /** @var Defaults */
  36. private $theme;
  37. /**
  38. * @param string $template
  39. * @param string $requestToken
  40. * @param \OCP\IL10N $l10n
  41. * @param Defaults $theme
  42. */
  43. public function __construct($template, $requestToken, $l10n, $theme ) {
  44. $this->vars = array();
  45. $this->vars['requesttoken'] = $requestToken;
  46. $this->l10n = $l10n;
  47. $this->template = $template;
  48. $this->theme = $theme;
  49. }
  50. /**
  51. * @param string $serverRoot
  52. * @param string|false $app_dir
  53. * @param string $theme
  54. * @param string $app
  55. * @return string[]
  56. */
  57. protected function getAppTemplateDirs($theme, $app, $serverRoot, $app_dir) {
  58. // Check if the app is in the app folder or in the root
  59. if( file_exists($app_dir.'/templates/' )) {
  60. return [
  61. $serverRoot.'/themes/'.$theme.'/apps/'.$app.'/templates/',
  62. $app_dir.'/templates/',
  63. ];
  64. }
  65. return [
  66. $serverRoot.'/themes/'.$theme.'/'.$app.'/templates/',
  67. $serverRoot.'/'.$app.'/templates/',
  68. ];
  69. }
  70. /**
  71. * @param string $serverRoot
  72. * @param string $theme
  73. * @return string[]
  74. */
  75. protected function getCoreTemplateDirs($theme, $serverRoot) {
  76. return [
  77. $serverRoot.'/themes/'.$theme.'/core/templates/',
  78. $serverRoot.'/core/templates/',
  79. ];
  80. }
  81. /**
  82. * Assign variables
  83. * @param string $key key
  84. * @param array|bool|integer|string $value value
  85. * @return bool
  86. *
  87. * This function assigns a variable. It can be accessed via $_[$key] in
  88. * the template.
  89. *
  90. * If the key existed before, it will be overwritten
  91. */
  92. public function assign( $key, $value) {
  93. $this->vars[$key] = $value;
  94. return true;
  95. }
  96. /**
  97. * Appends a variable
  98. * @param string $key key
  99. * @param mixed $value value
  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. 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. }