Base.php 4.4 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 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. * @return boolean|null
  101. *
  102. * This function assigns a variable in an array context. If the key already
  103. * exists, the value will be appended. It can be accessed via
  104. * $_[$key][$position] in the template.
  105. */
  106. public function append( $key, $value ) {
  107. if( array_key_exists( $key, $this->vars )) {
  108. $this->vars[$key][] = $value;
  109. }
  110. else{
  111. $this->vars[$key] = array( $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. }
  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. ${$var} = $value;
  159. }
  160. }
  161. // Include
  162. ob_start();
  163. try {
  164. include $file;
  165. $data = ob_get_contents();
  166. } catch (\Exception $e) {
  167. @ob_end_clean();
  168. throw $e;
  169. }
  170. @ob_end_clean();
  171. // Return data
  172. return $data;
  173. }
  174. }