/lib/private/template/base.php

https://gitlab.com/wuhang2003/core · PHP · 184 lines · 76 code · 16 blank · 92 comment · 4 complexity · b664d541ebcb44ca779ed997c24df4e1 MD5 · raw file

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