template.php 8.9 KB

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