base.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 Morris Jobke <hey@morrisjobke.de>
  8. * @author Scrutinizer Auto-Fixer <auto-fixer@scrutinizer-ci.com>
  9. *
  10. * @copyright Copyright (c) 2015, 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. private $l10n; // The l10n-Object
  31. private $theme; // theme defaults
  32. /**
  33. * @param string $template
  34. * @param \OC_L10N $l10n
  35. * @param \OC_Defaults $theme
  36. */
  37. public function __construct( $template, $requesttoken, $l10n, $theme ) {
  38. $this->vars = array();
  39. $this->vars['requesttoken'] = $requesttoken;
  40. $this->l10n = $l10n;
  41. $this->template = $template;
  42. $this->theme = $theme;
  43. }
  44. /**
  45. * @param string $serverroot
  46. * @param string|false $app_dir
  47. * @param string $theme
  48. * @param string $app
  49. */
  50. protected function getAppTemplateDirs($theme, $app, $serverroot, $app_dir) {
  51. // Check if the app is in the app folder or in the root
  52. if( file_exists($app_dir.'/templates/' )) {
  53. return array(
  54. $serverroot.'/themes/'.$theme.'/apps/'.$app.'/templates/',
  55. $app_dir.'/templates/',
  56. );
  57. }
  58. return array(
  59. $serverroot.'/themes/'.$theme.'/'.$app.'/templates/',
  60. $serverroot.'/'.$app.'/templates/',
  61. );
  62. }
  63. /**
  64. * @param string $serverroot
  65. * @param string $theme
  66. */
  67. protected function getCoreTemplateDirs($theme, $serverroot) {
  68. return array(
  69. $serverroot.'/themes/'.$theme.'/core/templates/',
  70. $serverroot.'/core/templates/',
  71. );
  72. }
  73. /**
  74. * Assign variables
  75. * @param string $key key
  76. * @param array|bool|integer|string $value value
  77. * @return bool
  78. *
  79. * This function assigns a variable. It can be accessed via $_[$key] in
  80. * the template.
  81. *
  82. * If the key existed before, it will be overwritten
  83. */
  84. public function assign( $key, $value) {
  85. $this->vars[$key] = $value;
  86. return true;
  87. }
  88. /**
  89. * Appends a variable
  90. * @param string $key key
  91. * @param mixed $value value
  92. * @return boolean|null
  93. *
  94. * This function assigns a variable in an array context. If the key already
  95. * exists, the value will be appended. It can be accessed via
  96. * $_[$key][$position] in the template.
  97. */
  98. public function append( $key, $value ) {
  99. if( array_key_exists( $key, $this->vars )) {
  100. $this->vars[$key][] = $value;
  101. }
  102. else{
  103. $this->vars[$key] = array( $value );
  104. }
  105. }
  106. /**
  107. * Prints the proceeded template
  108. * @return bool
  109. *
  110. * This function proceeds the template and prints its output.
  111. */
  112. public function printPage() {
  113. $data = $this->fetchPage();
  114. if( $data === false ) {
  115. return false;
  116. }
  117. else{
  118. print $data;
  119. return true;
  120. }
  121. }
  122. /**
  123. * Process the template
  124. * @return string
  125. *
  126. * This function processes the template.
  127. */
  128. public function fetchPage() {
  129. return $this->load($this->template);
  130. }
  131. /**
  132. * doing the actual work
  133. * @param string $file
  134. * @return string content
  135. *
  136. * Includes the template file, fetches its output
  137. */
  138. protected function load( $file, $additionalparams = null ) {
  139. // Register the variables
  140. $_ = $this->vars;
  141. $l = $this->l10n;
  142. $theme = $this->theme;
  143. if( !is_null($additionalparams)) {
  144. $_ = array_merge( $additionalparams, $this->vars );
  145. }
  146. // Include
  147. ob_start();
  148. include $file;
  149. $data = ob_get_contents();
  150. @ob_end_clean();
  151. // Return data
  152. return $data;
  153. }
  154. }