template.php 8.2 KB

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