template.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. /**
  24. * @brief make OC_Helper::linkTo available as a simple function
  25. * @param string $app app
  26. * @param string $file file
  27. * @param array $args array with param=>value, will be appended to the returned url
  28. * @return string link to the file
  29. *
  30. * For further information have a look at OC_Helper::linkTo
  31. */
  32. function link_to( $app, $file, $args = array() ) {
  33. return OC_Helper::linkTo( $app, $file, $args );
  34. }
  35. /**
  36. * @brief make OC_Helper::imagePath available as a simple function
  37. * @param string $app app
  38. * @param string $image image
  39. * @return string link to the image
  40. *
  41. * For further information have a look at OC_Helper::imagePath
  42. */
  43. function image_path( $app, $image ) {
  44. return OC_Helper::imagePath( $app, $image );
  45. }
  46. /**
  47. * @brief make OC_Helper::mimetypeIcon available as a simple function
  48. * @param string $mimetype mimetype
  49. * @return string link to the image
  50. *
  51. * For further information have a look at OC_Helper::mimetypeIcon
  52. */
  53. function mimetype_icon( $mimetype ) {
  54. return OC_Helper::mimetypeIcon( $mimetype );
  55. }
  56. /**
  57. * @brief make OC_Helper::humanFileSize available as a simple function
  58. * @param int $bytes size in bytes
  59. * @return string size as string
  60. *
  61. * For further information have a look at OC_Helper::humanFileSize
  62. */
  63. function human_file_size( $bytes ) {
  64. return OC_Helper::humanFileSize( $bytes );
  65. }
  66. function simple_file_size($bytes) {
  67. $mbytes = round($bytes/(1024*1024),1);
  68. if($bytes == 0) { return '0'; }
  69. else if($mbytes < 0.1) { return '&lt; 0.1'; }
  70. else if($mbytes > 1000) { return '&gt; 1000'; }
  71. else { return number_format($mbytes, 1); }
  72. }
  73. function relative_modified_date($timestamp) {
  74. $l=OC_L10N::get('lib');
  75. $timediff = time() - $timestamp;
  76. $diffminutes = round($timediff/60);
  77. $diffhours = round($diffminutes/60);
  78. $diffdays = round($diffhours/24);
  79. $diffmonths = round($diffdays/31);
  80. if($timediff < 60) { return $l->t('seconds ago'); }
  81. else if($timediff < 120) { return $l->t('1 minute ago'); }
  82. else if($timediff < 3600) { return $l->t('%d minutes ago',$diffminutes); }
  83. //else if($timediff < 7200) { return '1 hour ago'; }
  84. //else if($timediff < 86400) { return $diffhours.' hours ago'; }
  85. else if((date('G')-$diffhours) > 0) { return $l->t('today'); }
  86. else if((date('G')-$diffhours) > -24) { return $l->t('yesterday'); }
  87. else if($timediff < 2678400) { return $l->t('%d days ago',$diffdays); }
  88. else if($timediff < 5184000) { return $l->t('last month'); }
  89. else if((date('n')-$diffmonths) > 0) { return $l->t('months ago'); }
  90. else if($timediff < 63113852) { return $l->t('last year'); }
  91. else { return $l->t('years ago'); }
  92. }
  93. function html_select_options($options, $selected, $params=array()) {
  94. if (!is_array($selected)) {
  95. $selected=array($selected);
  96. }
  97. if (isset($params['combine']) && $params['combine']) {
  98. $options = array_combine($options, $options);
  99. }
  100. $value_name = $label_name = false;
  101. if (isset($params['value'])) {
  102. $value_name = $params['value'];
  103. }
  104. if (isset($params['label'])) {
  105. $label_name = $params['label'];
  106. }
  107. $html = '';
  108. foreach($options as $value => $label) {
  109. if ($value_name && is_array($label)) {
  110. $value = $label[$value_name];
  111. }
  112. if ($label_name && is_array($label)) {
  113. $label = $label[$label_name];
  114. }
  115. $select = in_array($value, $selected) ? ' selected="selected"' : '';
  116. $html .= '<option value="' . $value . '"' . $select . '>' . $label . '</option>'."\n";
  117. }
  118. return $html;
  119. }
  120. /**
  121. * This class provides the templates for owncloud.
  122. */
  123. class OC_Template{
  124. private $renderas; // Create a full page?
  125. private $application; // template Application
  126. private $vars; // Vars
  127. private $template; // The path to the template
  128. private $l10n; // The l10n-Object
  129. private $headers=array(); //custom headers
  130. /**
  131. * @brief Constructor
  132. * @param string $app app providing the template
  133. * @param string $file name of the template file (without suffix)
  134. * @param string $renderas = ""; produce a full page
  135. * @return OC_Template object
  136. *
  137. * This function creates an OC_Template object.
  138. *
  139. * If $renderas is set, OC_Template will try to produce a full page in the
  140. * according layout. For now, renderas can be set to "guest", "user" or
  141. * "admin".
  142. */
  143. public function __construct( $app, $name, $renderas = "" ) {
  144. // Set the private data
  145. $this->renderas = $renderas;
  146. $this->application = $app;
  147. $this->vars = array();
  148. if($renderas == 'user') {
  149. $this->vars['requesttoken'] = OC_Util::callRegister();
  150. }
  151. $parts = explode('/', $app); // fix translation when app is something like core/lostpassword
  152. $this->l10n = OC_L10N::get($parts[0]);
  153. header('X-Frame-Options: Sameorigin');
  154. header('X-XSS-Protection: 1; mode=block');
  155. header('X-Content-Type-Options: nosniff');
  156. $this->findTemplate($name);
  157. }
  158. /**
  159. * autodetect the formfactor of the used device
  160. * default -> the normal desktop browser interface
  161. * mobile -> interface for smartphones
  162. * tablet -> interface for tablets
  163. * standalone -> the default interface but without header, footer and
  164. * sidebar, just the application. Useful to use just a specific
  165. * app on the desktop in a standalone window.
  166. */
  167. public static function detectFormfactor() {
  168. // please add more useragent strings for other devices
  169. if(isset($_SERVER['HTTP_USER_AGENT'])) {
  170. if(stripos($_SERVER['HTTP_USER_AGENT'],'ipad')>0) {
  171. $mode='tablet';
  172. }elseif(stripos($_SERVER['HTTP_USER_AGENT'],'iphone')>0) {
  173. $mode='mobile';
  174. }elseif((stripos($_SERVER['HTTP_USER_AGENT'],'N9')>0) and (stripos($_SERVER['HTTP_USER_AGENT'],'nokia')>0)) {
  175. $mode='mobile';
  176. }else{
  177. $mode='default';
  178. }
  179. }else{
  180. $mode='default';
  181. }
  182. return($mode);
  183. }
  184. /**
  185. * @brief Returns the formfactor extension for current formfactor
  186. */
  187. static public function getFormFactorExtension()
  188. {
  189. // if the formfactor is not yet autodetected do the
  190. // autodetection now. For possible formfactors check the
  191. // detectFormfactor documentation
  192. if(!isset($_SESSION['formfactor'])) {
  193. $_SESSION['formfactor'] = self::detectFormfactor();
  194. }
  195. // allow manual override via GET parameter
  196. if(isset($_GET['formfactor'])) {
  197. $_SESSION['formfactor']=$_GET['formfactor'];
  198. }
  199. $formfactor=$_SESSION['formfactor'];
  200. if($formfactor=='default') {
  201. $fext='';
  202. }elseif($formfactor=='mobile') {
  203. $fext='.mobile';
  204. }elseif($formfactor=='tablet') {
  205. $fext='.tablet';
  206. }elseif($formfactor=='standalone') {
  207. $fext='.standalone';
  208. }else{
  209. $fext='';
  210. }
  211. return $fext;
  212. }
  213. /**
  214. * @brief find the template with the given name
  215. * @param string $name of the template file (without suffix)
  216. *
  217. * Will select the template file for the selected theme and formfactor.
  218. * Checking all the possible locations.
  219. */
  220. protected function findTemplate($name)
  221. {
  222. // Read the selected theme from the config file
  223. $theme=OC_Config::getValue( "theme" );
  224. // Read the detected formfactor and use the right file name.
  225. $fext = self::getFormFactorExtension();
  226. $app = $this->application;
  227. // Check if it is a app template or not.
  228. if( $app != "" ) {
  229. // Check if the app is in the app folder or in the root
  230. if( file_exists(OC_App::getAppPath($app)."/templates/" )) {
  231. // Check if the template is overwritten by the selected theme
  232. if ($this->checkPathForTemplate(OC::$SERVERROOT."/themes/$theme/apps/$app/templates/", $name, $fext)) {
  233. }elseif ($this->checkPathForTemplate(OC_App::getAppPath($app)."/templates/", $name, $fext)) {
  234. }
  235. }else{
  236. // Check if the template is overwritten by the selected theme
  237. if ($this->checkPathForTemplate(OC::$SERVERROOT."/themes/$theme/$app/templates/", $name, $fext)) {
  238. }elseif ($this->checkPathForTemplate(OC::$SERVERROOT."/$app/templates/", $name, $fext)) {
  239. }else{
  240. echo('template not found: template:'.$name.' formfactor:'.$fext.' webroot:'.OC::$WEBROOT.' serverroot:'.OC::$SERVERROOT);
  241. die();
  242. }
  243. }
  244. }else{
  245. // Check if the template is overwritten by the selected theme
  246. if ($this->checkPathForTemplate(OC::$SERVERROOT."/themes/$theme/core/templates/", $name, $fext)) {
  247. } elseif ($this->checkPathForTemplate(OC::$SERVERROOT."/core/templates/", $name, $fext)) {
  248. }else{
  249. echo('template not found: template:'.$name.' formfactor:'.$fext.' webroot:'.OC::$WEBROOT.' serverroot:'.OC::$SERVERROOT);
  250. die();
  251. }
  252. }
  253. }
  254. /**
  255. * @brief check Path For Template with and without $fext
  256. * @param string $path to check
  257. * @param string $name of the template file (without suffix)
  258. * @param string $fext formfactor extension
  259. * @return bool true when found
  260. *
  261. * Will set $this->template and $this->path if there is a template at
  262. * the specific $path
  263. */
  264. protected function checkPathForTemplate($path, $name, $fext)
  265. {
  266. if ($name =='') return false;
  267. $template = null;
  268. if( is_file( $path.$name.$fext.'.php' )) {
  269. $template = $path.$name.$fext.'.php';
  270. }elseif( is_file( $path.$name.'.php' )) {
  271. $template = $path.$name.'.php';
  272. }
  273. if ($template) {
  274. $this->template = $template;
  275. $this->path = $path;
  276. return true;
  277. }
  278. return false;
  279. }
  280. /**
  281. * @brief Assign variables
  282. * @param string $key key
  283. * @param string $value value
  284. * @param bool $sanitizeHTML false, if data shouldn't get passed through htmlentities
  285. * @return bool
  286. *
  287. * This function assigns a variable. It can be accessed via $_[$key] in
  288. * the template.
  289. *
  290. * If the key existed before, it will be overwritten
  291. */
  292. public function assign( $key, $value, $sanitizeHTML=true ) {
  293. if($sanitizeHTML == true) $value=OC_Util::sanitizeHTML($value);
  294. $this->vars[$key] = $value;
  295. return true;
  296. }
  297. /**
  298. * @brief Appends a variable
  299. * @param string $key key
  300. * @param string $value value
  301. * @return bool
  302. *
  303. * This function assigns a variable in an array context. If the key already
  304. * exists, the value will be appended. It can be accessed via
  305. * $_[$key][$position] in the template.
  306. */
  307. public function append( $key, $value ) {
  308. if( array_key_exists( $key, $this->vars )) {
  309. $this->vars[$key][] = $value;
  310. }
  311. else{
  312. $this->vars[$key] = array( $value );
  313. }
  314. }
  315. /**
  316. * @brief Add a custom element to the header
  317. * @param string $tag tag name of the element
  318. * @param array $attributes array of attrobutes for the element
  319. * @param string $text the text content for the element
  320. */
  321. public function addHeader( $tag, $attributes, $text='') {
  322. $this->headers[]=array('tag'=>$tag,'attributes'=>$attributes,'text'=>$text);
  323. }
  324. /**
  325. * @brief Prints the proceeded template
  326. * @return bool
  327. *
  328. * This function proceeds the template and prints its output.
  329. */
  330. public function printPage() {
  331. $data = $this->fetchPage();
  332. if( $data === false ) {
  333. return false;
  334. }
  335. else{
  336. print $data;
  337. return true;
  338. }
  339. }
  340. /**
  341. * @brief Proceeds the template
  342. * @return bool
  343. *
  344. * This function proceeds the template. If $this->renderas is set, it
  345. * will produce a full page.
  346. */
  347. public function fetchPage() {
  348. $data = $this->_fetch();
  349. if( $this->renderas ) {
  350. $page = new OC_TemplateLayout($this->renderas);
  351. if($this->renderas == 'user') {
  352. $page->assign('requesttoken', $this->vars['requesttoken']);
  353. }
  354. // Add custom headers
  355. $page->assign('headers',$this->headers, false);
  356. foreach(OC_Util::$headers as $header) {
  357. $page->append('headers',$header);
  358. }
  359. $page->assign( "content", $data, false );
  360. return $page->fetchPage();
  361. }
  362. else{
  363. return $data;
  364. }
  365. }
  366. /**
  367. * @brief doing the actual work
  368. * @return string content
  369. *
  370. * Includes the template file, fetches its output
  371. */
  372. private function _fetch() {
  373. // Register the variables
  374. $_ = $this->vars;
  375. $l = $this->l10n;
  376. // Execute the template
  377. ob_start();
  378. include( $this->template ); // <-- we have to use include because we pass $_!
  379. $data = ob_get_contents();
  380. @ob_end_clean();
  381. // return the data
  382. return $data;
  383. }
  384. /**
  385. * @brief Include template
  386. * @return string returns content of included template
  387. *
  388. * Includes another template. use <?php echo $this->inc('template'); ?> to
  389. * do this.
  390. */
  391. public function inc( $file, $additionalparams = null ) {
  392. $_ = $this->vars;
  393. $l = $this->l10n;
  394. if( !is_null($additionalparams)) {
  395. $_ = array_merge( $additionalparams, $this->vars );
  396. }
  397. // Include
  398. ob_start();
  399. include( $this->path.$file.'.php' );
  400. $data = ob_get_contents();
  401. @ob_end_clean();
  402. // Return data
  403. return $data;
  404. }
  405. /**
  406. * @brief Shortcut to print a simple page for users
  407. * @param string $application The application we render the template for
  408. * @param string $name Name of the template
  409. * @param array $parameters Parameters for the template
  410. * @return bool
  411. */
  412. public static function printUserPage( $application, $name, $parameters = array() ) {
  413. $content = new OC_Template( $application, $name, "user" );
  414. foreach( $parameters as $key => $value ) {
  415. $content->assign( $key, $value, false );
  416. }
  417. print $content->printPage();
  418. }
  419. /**
  420. * @brief Shortcut to print a simple page for admins
  421. * @param string $application The application we render the template for
  422. * @param string $name Name of the template
  423. * @param array $parameters Parameters for the template
  424. * @return bool
  425. */
  426. public static function printAdminPage( $application, $name, $parameters = array() ) {
  427. $content = new OC_Template( $application, $name, "admin" );
  428. foreach( $parameters as $key => $value ) {
  429. $content->assign( $key, $value, false );
  430. }
  431. return $content->printPage();
  432. }
  433. /**
  434. * @brief Shortcut to print a simple page for guests
  435. * @param string $application The application we render the template for
  436. * @param string $name Name of the template
  437. * @param string $parameters Parameters for the template
  438. * @return bool
  439. */
  440. public static function printGuestPage( $application, $name, $parameters = array() ) {
  441. $content = new OC_Template( $application, $name, "guest" );
  442. foreach( $parameters as $key => $value ) {
  443. $content->assign( $key, $value, false );
  444. }
  445. return $content->printPage();
  446. }
  447. }