You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

template.php 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. /**
  32. * @brief Constructor
  33. * @param string $app app providing the template
  34. * @param string $name of the template file (without suffix)
  35. * @param string $renderas = ""; produce a full page
  36. * @return OC_Template object
  37. *
  38. * This function creates an OC_Template object.
  39. *
  40. * If $renderas is set, OC_Template will try to produce a full page in the
  41. * according layout. For now, renderas can be set to "guest", "user" or
  42. * "admin".
  43. */
  44. public function __construct( $app, $name, $renderas = "" ) {
  45. // Read the selected theme from the config file
  46. $theme = OC_Util::getTheme();
  47. // Read the detected formfactor and use the right file name.
  48. $fext = self::getFormFactorExtension();
  49. $requesttoken = OC::$session ? OC_Util::callRegister() : '';
  50. $parts = explode('/', $app); // fix translation when app is something like core/lostpassword
  51. $l10n = OC_L10N::get($parts[0]);
  52. $themeDefaults = new OC_Defaults();
  53. list($path, $template) = $this->findTemplate($theme, $app, $name, $fext);
  54. // Set the private data
  55. $this->renderas = $renderas;
  56. $this->path = $path;
  57. parent::__construct($template, $requesttoken, $l10n, $themeDefaults);
  58. // Some headers to enhance security
  59. header('X-XSS-Protection: 1; mode=block'); // Enforce browser based XSS filters
  60. header('X-Content-Type-Options: nosniff'); // Disable sniffing the content type for IE
  61. // iFrame Restriction Policy
  62. $xFramePolicy = OC_Config::getValue('xframe_restriction', true);
  63. if($xFramePolicy) {
  64. header('X-Frame-Options: Sameorigin'); // Disallow iFraming from other domains
  65. }
  66. // Content Security Policy
  67. // If you change the standard policy, please also change it in config.sample.php
  68. $policy = OC_Config::getValue('custom_csp_policy',
  69. 'default-src \'self\'; '
  70. .'script-src \'self\' \'unsafe-eval\'; '
  71. .'style-src \'self\' \'unsafe-inline\'; '
  72. .'frame-src *; '
  73. .'img-src *; '
  74. .'font-src \'self\' data:; '
  75. .'media-src *');
  76. header('Content-Security-Policy:'.$policy); // Standard
  77. }
  78. /**
  79. * autodetect the formfactor of the used device
  80. * default -> the normal desktop browser interface
  81. * mobile -> interface for smartphones
  82. * tablet -> interface for tablets
  83. * standalone -> the default interface but without header, footer and
  84. * sidebar, just the application. Useful to use just a specific
  85. * app on the desktop in a standalone window.
  86. */
  87. public static function detectFormfactor() {
  88. // please add more useragent strings for other devices
  89. if(isset($_SERVER['HTTP_USER_AGENT'])) {
  90. if(stripos($_SERVER['HTTP_USER_AGENT'], 'ipad')>0) {
  91. $mode='tablet';
  92. }elseif(stripos($_SERVER['HTTP_USER_AGENT'], 'iphone')>0) {
  93. $mode='mobile';
  94. }elseif((stripos($_SERVER['HTTP_USER_AGENT'], 'N9')>0)
  95. and (stripos($_SERVER['HTTP_USER_AGENT'], 'nokia')>0)) {
  96. $mode='mobile';
  97. }else{
  98. $mode='default';
  99. }
  100. }else{
  101. $mode='default';
  102. }
  103. return($mode);
  104. }
  105. /**
  106. * @brief Returns the formfactor extension for current formfactor
  107. */
  108. static public function getFormFactorExtension()
  109. {
  110. if (!\OC::$session) {
  111. return '';
  112. }
  113. // if the formfactor is not yet autodetected do the
  114. // autodetection now. For possible formfactors check the
  115. // detectFormfactor documentation
  116. if (!\OC::$session->exists('formfactor')) {
  117. \OC::$session->set('formfactor', self::detectFormfactor());
  118. }
  119. // allow manual override via GET parameter
  120. if(isset($_GET['formfactor'])) {
  121. \OC::$session->set('formfactor', $_GET['formfactor']);
  122. }
  123. $formfactor = \OC::$session->get('formfactor');
  124. if($formfactor==='default') {
  125. $fext='';
  126. }elseif($formfactor==='mobile') {
  127. $fext='.mobile';
  128. }elseif($formfactor==='tablet') {
  129. $fext='.tablet';
  130. }elseif($formfactor==='standalone') {
  131. $fext='.standalone';
  132. }else{
  133. $fext='';
  134. }
  135. return $fext;
  136. }
  137. /**
  138. * @brief find the template with the given name
  139. * @param string $name of the template file (without suffix)
  140. *
  141. * Will select the template file for the selected theme and formfactor.
  142. * Checking all the possible locations.
  143. */
  144. protected function findTemplate($theme, $app, $name, $fext) {
  145. // Check if it is a app template or not.
  146. if( $app !== '' ) {
  147. $dirs = $this->getAppTemplateDirs($theme, $app, OC::$SERVERROOT, OC_App::getAppPath($app));
  148. } else {
  149. $dirs = $this->getCoreTemplateDirs($theme, OC::$SERVERROOT);
  150. }
  151. $locator = new \OC\Template\TemplateFileLocator( $fext, $dirs );
  152. $template = $locator->find($name);
  153. $path = $locator->getPath();
  154. return array($path, $template);
  155. }
  156. /**
  157. * @brief Add a custom element to the header
  158. * @param string $tag tag name of the element
  159. * @param array $attributes array of attributes for the element
  160. * @param string $text the text content for the element
  161. */
  162. public function addHeader( $tag, $attributes, $text='') {
  163. $this->headers[]=array('tag'=>$tag,'attributes'=>$attributes, 'text'=>$text);
  164. }
  165. /**
  166. * @brief Process the template
  167. * @return bool
  168. *
  169. * This function process the template. If $this->renderas is set, it
  170. * will produce a full page.
  171. */
  172. public function fetchPage() {
  173. $data = parent::fetchPage();
  174. if( $this->renderas ) {
  175. $page = new OC_TemplateLayout($this->renderas);
  176. // Add custom headers
  177. $page->assign('headers', $this->headers, false);
  178. foreach(OC_Util::$headers as $header) {
  179. $page->append('headers', $header);
  180. }
  181. $page->assign( "content", $data, false );
  182. return $page->fetchPage();
  183. }
  184. else{
  185. return $data;
  186. }
  187. }
  188. /**
  189. * @brief Include template
  190. * @return string returns content of included template
  191. *
  192. * Includes another template. use <?php echo $this->inc('template'); ?> to
  193. * do this.
  194. */
  195. public function inc( $file, $additionalparams = null ) {
  196. return $this->load($this->path.$file.'.php', $additionalparams);
  197. }
  198. /**
  199. * @brief Shortcut to print a simple page for users
  200. * @param string $application The application we render the template for
  201. * @param string $name Name of the template
  202. * @param array $parameters Parameters for the template
  203. * @return bool
  204. */
  205. public static function printUserPage( $application, $name, $parameters = array() ) {
  206. $content = new OC_Template( $application, $name, "user" );
  207. foreach( $parameters as $key => $value ) {
  208. $content->assign( $key, $value );
  209. }
  210. print $content->printPage();
  211. }
  212. /**
  213. * @brief Shortcut to print a simple page for admins
  214. * @param string $application The application we render the template for
  215. * @param string $name Name of the template
  216. * @param array $parameters Parameters for the template
  217. * @return bool
  218. */
  219. public static function printAdminPage( $application, $name, $parameters = array() ) {
  220. $content = new OC_Template( $application, $name, "admin" );
  221. foreach( $parameters as $key => $value ) {
  222. $content->assign( $key, $value );
  223. }
  224. return $content->printPage();
  225. }
  226. /**
  227. * @brief Shortcut to print a simple page for guests
  228. * @param string $application The application we render the template for
  229. * @param string $name Name of the template
  230. * @param string $parameters Parameters for the template
  231. * @return bool
  232. */
  233. public static function printGuestPage( $application, $name, $parameters = array() ) {
  234. $content = new OC_Template( $application, $name, "guest" );
  235. foreach( $parameters as $key => $value ) {
  236. $content->assign( $key, $value );
  237. }
  238. return $content->printPage();
  239. }
  240. /**
  241. * @brief Print a fatal error page and terminates the script
  242. * @param string $error_msg The error message to show
  243. * @param string $hint An optional hint message
  244. * Warning: All data passed to $hint needs to get sanitized using OC_Util::sanitizeHTML
  245. */
  246. public static function printErrorPage( $error_msg, $hint = '' ) {
  247. $content = new OC_Template( '', 'error', 'error' );
  248. $errors = array(array('error' => $error_msg, 'hint' => $hint));
  249. $content->assign( 'errors', $errors );
  250. $content->printPage();
  251. die();
  252. }
  253. /**
  254. * print error page using Exception details
  255. * @param Exception $exception
  256. */
  257. public static function printExceptionErrorPage(Exception $exception) {
  258. $error_msg = $exception->getMessage();
  259. if ($exception->getCode()) {
  260. $error_msg = '['.$exception->getCode().'] '.$error_msg;
  261. }
  262. if (defined('DEBUG') and DEBUG) {
  263. $hint = $exception->getTraceAsString();
  264. if (!empty($hint)) {
  265. $hint = '<pre>'.$hint.'</pre>';
  266. }
  267. $l = OC_L10N::get('lib');
  268. while (method_exists($exception, 'previous') && $exception = $exception->previous()) {
  269. $error_msg .= '<br/>'.$l->t('Caused by:').' ';
  270. if ($exception->getCode()) {
  271. $error_msg .= '['.$exception->getCode().'] ';
  272. }
  273. $error_msg .= $exception->getMessage();
  274. };
  275. } else {
  276. $hint = '';
  277. if ($exception instanceof \OC\HintException) {
  278. $hint = $exception->getHint();
  279. }
  280. }
  281. self::printErrorPage($error_msg, $hint);
  282. }
  283. }