template.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /**
  3. * @author Adam Williamson <awilliam@redhat.com>
  4. * @author Bart Visscher <bartv@thisnet.nl>
  5. * @author Björn Schießle <schiessle@owncloud.com>
  6. * @author Brice Maron <brice@bmaron.net>
  7. * @author drarko <drarko@users.noreply.github.com>
  8. * @author Frank Karlitschek <frank@owncloud.org>
  9. * @author Jakob Sack <mail@jakobsack.de>
  10. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  11. * @author Lukas Reschke <lukas@owncloud.com>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Robin Appelman <icewind@owncloud.com>
  14. * @author Thomas Müller <thomas.mueller@tmit.eu>
  15. * @author Vincent Petry <pvince81@owncloud.com>
  16. *
  17. * @copyright Copyright (c) 2015, ownCloud, Inc.
  18. * @license AGPL-3.0
  19. *
  20. * This code is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU Affero General Public License, version 3,
  22. * as published by the Free Software Foundation.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU Affero General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Affero General Public License, version 3,
  30. * along with this program. If not, see <http://www.gnu.org/licenses/>
  31. *
  32. */
  33. require_once __DIR__.'/template/functions.php';
  34. /**
  35. * This class provides the templates for ownCloud.
  36. */
  37. class OC_Template extends \OC\Template\Base {
  38. private $renderas; // Create a full page?
  39. private $path; // The path to the template
  40. private $headers = array(); //custom headers
  41. protected $app; // app id
  42. /**
  43. * Constructor
  44. * @param string $app app providing the template
  45. * @param string $name of the template file (without suffix)
  46. * @param string $renderas = ""; produce a full page
  47. * @param bool $registerCall = true
  48. * @return OC_Template object
  49. *
  50. * This function creates an OC_Template object.
  51. *
  52. * If $renderas is set, OC_Template will try to produce a full page in the
  53. * according layout. For now, renderas can be set to "guest", "user" or
  54. * "admin".
  55. */
  56. public function __construct( $app, $name, $renderas = "", $registerCall = true ) {
  57. // Read the selected theme from the config file
  58. $theme = OC_Util::getTheme();
  59. $requesttoken = (OC::$server->getSession() and $registerCall) ? OC_Util::callRegister() : '';
  60. $parts = explode('/', $app); // fix translation when app is something like core/lostpassword
  61. $l10n = \OC::$server->getL10N($parts[0]);
  62. $themeDefaults = new OC_Defaults();
  63. list($path, $template) = $this->findTemplate($theme, $app, $name);
  64. // Set the private data
  65. $this->renderas = $renderas;
  66. $this->path = $path;
  67. $this->app = $app;
  68. parent::__construct($template, $requesttoken, $l10n, $themeDefaults);
  69. }
  70. /**
  71. * find the template with the given name
  72. * @param string $name of the template file (without suffix)
  73. *
  74. * Will select the template file for the selected theme.
  75. * Checking all the possible locations.
  76. * @param string $theme
  77. * @param string $app
  78. * @return array
  79. */
  80. protected function findTemplate($theme, $app, $name) {
  81. // Check if it is a app template or not.
  82. if( $app !== '' ) {
  83. $dirs = $this->getAppTemplateDirs($theme, $app, OC::$SERVERROOT, OC_App::getAppPath($app));
  84. } else {
  85. $dirs = $this->getCoreTemplateDirs($theme, OC::$SERVERROOT);
  86. }
  87. $locator = new \OC\Template\TemplateFileLocator( $dirs );
  88. $template = $locator->find($name);
  89. $path = $locator->getPath();
  90. return array($path, $template);
  91. }
  92. /**
  93. * Add a custom element to the header
  94. * @param string $tag tag name of the element
  95. * @param array $attributes array of attributes for the element
  96. * @param string $text the text content for the element. If $text is null then the
  97. * element will be written as empty element. So use "" to get a closing tag.
  98. */
  99. public function addHeader($tag, $attributes, $text=null) {
  100. $this->headers[]= array(
  101. 'tag' => $tag,
  102. 'attributes' => $attributes,
  103. 'text' => $text
  104. );
  105. }
  106. /**
  107. * Process the template
  108. * @return boolean|string
  109. *
  110. * This function process the template. If $this->renderas is set, it
  111. * will produce a full page.
  112. */
  113. public function fetchPage() {
  114. $data = parent::fetchPage();
  115. if( $this->renderas ) {
  116. $page = new OC_TemplateLayout($this->renderas, $this->app);
  117. // Add custom headers
  118. $headers = '';
  119. foreach(OC_Util::$headers as $header) {
  120. $headers .= '<'.OC_Util::sanitizeHTML($header['tag']);
  121. foreach($header['attributes'] as $name=>$value) {
  122. $headers .= ' '.OC_Util::sanitizeHTML($name).'="'.OC_Util::sanitizeHTML($value).'"';
  123. }
  124. if ($header['text'] !== null) {
  125. $headers .= '>'.OC_Util::sanitizeHTML($header['text']).'</'.OC_Util::sanitizeHTML($header['tag']).'>';
  126. } else {
  127. $headers .= '/>';
  128. }
  129. }
  130. $page->assign('headers', $headers, false);
  131. $page->assign('content', $data, false );
  132. return $page->fetchPage();
  133. }
  134. else{
  135. return $data;
  136. }
  137. }
  138. /**
  139. * Include template
  140. * @return string returns content of included template
  141. *
  142. * Includes another template. use <?php echo $this->inc('template'); ?> to
  143. * do this.
  144. */
  145. public function inc( $file, $additionalParams = null ) {
  146. return $this->load($this->path.$file.'.php', $additionalParams);
  147. }
  148. /**
  149. * Shortcut to print a simple page for users
  150. * @param string $application The application we render the template for
  151. * @param string $name Name of the template
  152. * @param array $parameters Parameters for the template
  153. * @return boolean|null
  154. */
  155. public static function printUserPage( $application, $name, $parameters = array() ) {
  156. $content = new OC_Template( $application, $name, "user" );
  157. foreach( $parameters as $key => $value ) {
  158. $content->assign( $key, $value );
  159. }
  160. print $content->printPage();
  161. }
  162. /**
  163. * Shortcut to print a simple page for admins
  164. * @param string $application The application we render the template for
  165. * @param string $name Name of the template
  166. * @param array $parameters Parameters for the template
  167. * @return bool
  168. */
  169. public static function printAdminPage( $application, $name, $parameters = array() ) {
  170. $content = new OC_Template( $application, $name, "admin" );
  171. foreach( $parameters as $key => $value ) {
  172. $content->assign( $key, $value );
  173. }
  174. return $content->printPage();
  175. }
  176. /**
  177. * Shortcut to print a simple page for guests
  178. * @param string $application The application we render the template for
  179. * @param string $name Name of the template
  180. * @param array|string $parameters Parameters for the template
  181. * @return bool
  182. */
  183. public static function printGuestPage( $application, $name, $parameters = array() ) {
  184. $content = new OC_Template( $application, $name, "guest" );
  185. foreach( $parameters as $key => $value ) {
  186. $content->assign( $key, $value );
  187. }
  188. return $content->printPage();
  189. }
  190. /**
  191. * Print a fatal error page and terminates the script
  192. * @param string $error_msg The error message to show
  193. * @param string $hint An optional hint message - needs to be properly escaped
  194. */
  195. public static function printErrorPage( $error_msg, $hint = '' ) {
  196. $content = new \OC_Template( '', 'error', 'error', false );
  197. $errors = array(array('error' => $error_msg, 'hint' => $hint));
  198. $content->assign( 'errors', $errors );
  199. $content->printPage();
  200. die();
  201. }
  202. /**
  203. * print error page using Exception details
  204. * @param Exception $exception
  205. */
  206. public static function printExceptionErrorPage(Exception $exception) {
  207. $request = \OC::$server->getRequest();
  208. $content = new \OC_Template('', 'exception', 'error', false);
  209. $content->assign('errorClass', get_class($exception));
  210. $content->assign('errorMsg', $exception->getMessage());
  211. $content->assign('errorCode', $exception->getCode());
  212. $content->assign('file', $exception->getFile());
  213. $content->assign('line', $exception->getLine());
  214. $content->assign('trace', $exception->getTraceAsString());
  215. $content->assign('debugMode', defined('DEBUG') && DEBUG === true);
  216. $content->assign('remoteAddr', $request->getRemoteAddress());
  217. $content->assign('requestID', $request->getId());
  218. $content->printPage();
  219. die();
  220. }
  221. /**
  222. * @return bool
  223. */
  224. public static function isAssetPipelineEnabled() {
  225. // asset management enabled?
  226. $config = \OC::$server->getConfig();
  227. $useAssetPipeline = $config->getSystemValue('asset-pipeline.enabled', false);
  228. if (!$useAssetPipeline) {
  229. return false;
  230. }
  231. // assets folder exists?
  232. $assetDir = $config->getSystemValue('assetdirectory', \OC::$SERVERROOT) . '/assets';
  233. if (!is_dir($assetDir)) {
  234. if (!mkdir($assetDir)) {
  235. \OCP\Util::writeLog('assets',
  236. "Folder <$assetDir> does not exist and/or could not be generated.", \OCP\Util::ERROR);
  237. return false;
  238. }
  239. }
  240. // assets folder can be accessed?
  241. if (!touch($assetDir."/.oc")) {
  242. \OCP\Util::writeLog('assets',
  243. "Folder <$assetDir> could not be accessed.", \OCP\Util::ERROR);
  244. return false;
  245. }
  246. return $useAssetPipeline;
  247. }
  248. }