base.php 4.0 KB

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