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 $app app
  26. * @param $file file
  27. * @returns link to the file
  28. *
  29. * For further information have a look at OC_Helper::linkTo
  30. */
  31. function link_to( $app, $file ){
  32. return OC_Helper::linkTo( $app, $file );
  33. }
  34. /**
  35. * @brief make OC_Helper::imagePath available as a simple function
  36. * @param $app app
  37. * @param $image image
  38. * @returns link to the image
  39. *
  40. * For further information have a look at OC_Helper::imagePath
  41. */
  42. function image_path( $app, $image ){
  43. return OC_Helper::imagePath( $app, $image );
  44. }
  45. /**
  46. * @brief make OC_Helper::mimetypeIcon available as a simple function
  47. * @param $mimetype mimetype
  48. * @returns link to the image
  49. *
  50. * For further information have a look at OC_Helper::mimetypeIcon
  51. */
  52. function mimetype_icon( $mimetype ){
  53. return OC_Helper::mimetypeIcon( $mimetype );
  54. }
  55. /**
  56. * @brief make OC_Helper::humanFileSize available as a simple function
  57. * @param $bytes size in bytes
  58. * @returns size as string
  59. *
  60. * For further information have a look at OC_Helper::humanFileSize
  61. */
  62. function human_file_size( $bytes ){
  63. return OC_Helper::humanFileSize( $bytes );
  64. }
  65. function simple_file_size($bytes) {
  66. $mbytes = round($bytes/(1024*1024),1);
  67. if($bytes == 0) { return '0'; }
  68. else if($mbytes < 0.1) { return '&lt; 0.1'; }
  69. else if($mbytes > 1000) { return '&gt; 1000'; }
  70. else { return number_format($mbytes, 1); }
  71. }
  72. function relative_modified_date($timestamp) {
  73. $l=OC_L10N::get('template');
  74. $timediff = time() - $timestamp;
  75. $diffminutes = round($timediff/60);
  76. $diffhours = round($diffminutes/60);
  77. $diffdays = round($diffhours/24);
  78. $diffmonths = round($diffdays/31);
  79. $diffyears = round($diffdays/365);
  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 $app app providing the template
  133. * @param $file name of the template file (without suffix)
  134. * @param $renderas = ""; produce a full page
  135. * @returns 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. $this->l10n = OC_L10N::get($app);
  152. header('X-Frame-Options: Sameorigin');
  153. header('X-XSS-Protection: 1; mode=block');
  154. header('X-Content-Type-Options: nosniff');
  155. $this->findTemplate($name);
  156. }
  157. /**
  158. * autodetects the formfactor of the used device
  159. * default -> the normal desktop browser interface
  160. * mobile -> interface for smartphones
  161. * tablet -> interface for tablets
  162. * standalone -> the default interface but without header, footer and
  163. * sidebar, just the application. Useful to use just a specific
  164. * app on the desktop in a standalone window.
  165. */
  166. public static function detectFormfactor(){
  167. // please add more useragent strings for other devices
  168. if(isset($_SERVER['HTTP_USER_AGENT'])){
  169. if(stripos($_SERVER['HTTP_USER_AGENT'],'ipad')>0) {
  170. $mode='tablet';
  171. }elseif(stripos($_SERVER['HTTP_USER_AGENT'],'iphone')>0){
  172. $mode='mobile';
  173. }elseif((stripos($_SERVER['HTTP_USER_AGENT'],'N9')>0) and (stripos($_SERVER['HTTP_USER_AGENT'],'nokia')>0)){
  174. $mode='mobile';
  175. }else{
  176. $mode='default';
  177. }
  178. }else{
  179. $mode='default';
  180. }
  181. return($mode);
  182. }
  183. /**
  184. * @brief Returns the formfactor extension for current formfactor
  185. */
  186. static public function getFormFactorExtension()
  187. {
  188. // if the formfactor is not yet autodetected do the
  189. // autodetection now. For possible formfactors check the
  190. // detectFormfactor documentation
  191. if(!isset($_SESSION['formfactor'])){
  192. $_SESSION['formfactor'] = self::detectFormfactor();
  193. }
  194. // allow manual override via GET parameter
  195. if(isset($_GET['formfactor'])){
  196. $_SESSION['formfactor']=$_GET['formfactor'];
  197. }
  198. $formfactor=$_SESSION['formfactor'];
  199. if($formfactor=='default') {
  200. $fext='';
  201. }elseif($formfactor=='mobile') {
  202. $fext='.mobile';
  203. }elseif($formfactor=='tablet') {
  204. $fext='.tablet';
  205. }elseif($formfactor=='standalone') {
  206. $fext='.standalone';
  207. }else{
  208. $fext='';
  209. }
  210. return $fext;
  211. }
  212. /**
  213. * @brief find the template with the given name
  214. * @param $name of the template file (without suffix)
  215. *
  216. * Will select the template file for the selected theme and formfactor.
  217. * Checking all the possible locations.
  218. */
  219. protected function findTemplate($name)
  220. {
  221. // Read the selected theme from the config file
  222. $theme=OC_Config::getValue( "theme" );
  223. // Read the detected formfactor and use the right file name.
  224. $fext = self::getFormFactorExtension();
  225. $app = $this->application;
  226. // Check if it is a app template or not.
  227. if( $app != "" ){
  228. // Check if the app is in the app folder or in the root
  229. if( file_exists( OC::$APPSROOT."/apps/$app/templates/" )){
  230. // Check if the template is overwritten by the selected theme
  231. if ($this->checkPathForTemplate(OC::$SERVERROOT."/themes/$theme/apps/$app/templates/", $name, $fext)) {
  232. }elseif ($this->checkPathForTemplate(OC::$APPSROOT."/apps/$app/templates/", $name, $fext)) {
  233. }
  234. }else{
  235. // Check if the template is overwritten by the selected theme
  236. if ($this->checkPathForTemplate(OC::$SERVERROOT."/themes/$theme/$app/templates/", $name, $fext)) {
  237. }elseif ($this->checkPathForTemplate(OC::$SERVERROOT."/$app/templates/", $name, $fext)) {
  238. }else{
  239. echo('template not found: template:'.$name.' formfactor:'.$fext.' webroot:'.OC::$WEBROOT.' serverroot:'.OC::$SERVERROOT);
  240. die();
  241. }
  242. }
  243. }else{
  244. // Check if the template is overwritten by the selected theme
  245. if ($this->checkPathForTemplate(OC::$SERVERROOT."/themes/$theme/core/templates/", $name, $fext)) {
  246. } elseif ($this->checkPathForTemplate(OC::$SERVERROOT."/core/templates/", $name, $fext)) {
  247. }else{
  248. echo('template not found: template:'.$name.' formfactor:'.$fext.' webroot:'.OC::$WEBROOT.' serverroot:'.OC::$SERVERROOT);
  249. die();
  250. }
  251. }
  252. }
  253. /**
  254. * @brief check Path For Template with and without $fext
  255. * @param $path to check
  256. * @param $name of the template file (without suffix)
  257. * @param $fext formfactor extension
  258. * @return bool true when found
  259. *
  260. * Will set $this->template and $this->path if there is a template at
  261. * the specifief $path
  262. */
  263. protected function checkPathForTemplate($path, $name, $fext)
  264. {
  265. if ($name =='') return false;
  266. $template = null;
  267. if( is_file( $path.$name.$fext.'.php' )){
  268. $template = $path.$name.$fext.'.php';
  269. }elseif( is_file( $path.$name.'.php' )){
  270. $template = $path.$name.'.php';
  271. }
  272. if ($template) {
  273. $this->template = $template;
  274. $this->path = $path;
  275. return true;
  276. }
  277. return false;
  278. }
  279. /**
  280. * @brief Assign variables
  281. * @param $key key
  282. * @param $value value
  283. * @param $sanitizeHTML false, if data shouldn't get passed through htmlentities
  284. * @returns true
  285. *
  286. * This function assigns a variable. It can be accessed via $_[$key] in
  287. * the template.
  288. *
  289. * If the key existed before, it will be overwritten
  290. */
  291. public function assign( $key, $value, $sanitizeHTML=true ){
  292. if($sanitizeHTML == true) $value=OC_Util::sanitizeHTML($value);
  293. $this->vars[$key] = $value;
  294. return true;
  295. }
  296. /**
  297. * @brief Appends a variable
  298. * @param $key key
  299. * @param $value value
  300. * @returns true
  301. *
  302. * This function assigns a variable in an array context. If the key already
  303. * exists, the value will be appended. It can be accessed via
  304. * $_[$key][$position] in the template.
  305. */
  306. public function append( $key, $value ){
  307. if( array_key_exists( $key, $this->vars )){
  308. $this->vars[$key][] = $value;
  309. }
  310. else{
  311. $this->vars[$key] = array( $value );
  312. }
  313. }
  314. /**
  315. * @brief Add a custom element to the header
  316. * @param string tag tag name of the element
  317. * @param array $attributes array of attrobutes for the element
  318. * @param string $text the text content for the element
  319. */
  320. public function addHeader( $tag, $attributes, $text=''){
  321. $this->headers[]=array('tag'=>$tag,'attributes'=>$attributes,'text'=>$text);
  322. }
  323. /**
  324. * @brief Prints the proceeded template
  325. * @returns true/false
  326. *
  327. * This function proceeds the template and prints its output.
  328. */
  329. public function printPage(){
  330. $data = $this->fetchPage();
  331. if( $data === false ){
  332. return false;
  333. }
  334. else{
  335. print $data;
  336. return true;
  337. }
  338. }
  339. /**
  340. * @brief Proceeds the template
  341. * @returns content
  342. *
  343. * This function proceeds the template. If $this->renderas is set, it
  344. * will produce a full page.
  345. */
  346. public function fetchPage(){
  347. $data = $this->_fetch();
  348. if( $this->renderas ){
  349. $page = new OC_TemplateLayout($this->renderas);
  350. if($this->renderas == 'user') {
  351. $page->assign('requesttoken', $this->vars['requesttoken']);
  352. }
  353. // Add custom headers
  354. $page->assign('headers',$this->headers, false);
  355. foreach(OC_Util::$headers as $header){
  356. $page->append('headers',$header);
  357. }
  358. $page->assign( "content", $data, false );
  359. return $page->fetchPage();
  360. }
  361. else{
  362. return $data;
  363. }
  364. }
  365. /**
  366. * @brief doing the actual work
  367. * @returns content
  368. *
  369. * Includes the template file, fetches its output
  370. */
  371. private function _fetch(){
  372. // Register the variables
  373. $_ = $this->vars;
  374. $l = $this->l10n;
  375. // Execute the template
  376. ob_start();
  377. include( $this->template ); // <-- we have to use include because we pass $_!
  378. $data = ob_get_contents();
  379. @ob_end_clean();
  380. // return the data
  381. return $data;
  382. }
  383. /**
  384. * @brief Include template
  385. * @returns returns content of included template
  386. *
  387. * Includes another template. use <?php echo $this->inc('template'); ?> to
  388. * do this.
  389. */
  390. public function inc( $file, $additionalparams = null ){
  391. // $_ erstellen
  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 $application The application we render the template for
  408. * @param $name Name of the template
  409. * @param $parameters Parameters for the template
  410. * @returns true/false
  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 $application The application we render the template for
  422. * @param $name Name of the template
  423. * @param $parameters Parameters for the template
  424. * @returns true/false
  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 $application The application we render the template for
  436. * @param $name Name of the template
  437. * @param $parameters Parameters for the template
  438. * @returns true/false
  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. }