template.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Frank Karlitschek
  6. * @author Jakob Sack
  7. * @copyright 2010 Frank Karlitschek karlitschek@kde.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=new OC_L10N('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 tempalte 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. // Read the selected theme from the config file
  145. $theme=OC_Config::getValue( "theme" );
  146. // Read the detected formfactor and use the right file name.
  147. $formfactor=$_SESSION['formfactor'];
  148. if($formfactor=='default') {
  149. $fext='';
  150. }elseif($formfactor=='mobile') {
  151. $fext='.mobile';
  152. }elseif($formfactor=='tablet') {
  153. $fext='.tablet';
  154. }elseif($formfactor=='standalone') {
  155. $fext='.standalone';
  156. }else{
  157. $fext='';
  158. }
  159. // Check if it is a app template or not.
  160. if( $app != "" ){
  161. // Check if the app is in the app folder or in the root
  162. if( file_exists( OC::$SERVERROOT."/apps/$app/templates/" )){
  163. // Check if the template is overwritten by the selected theme
  164. if( file_exists( OC::$SERVERROOT."/themes/$theme/apps/$app/templates/"."$name$fext.php" )){
  165. $template = OC::$SERVERROOT."/themes/$theme/apps/$app/templates/"."$name$fext.php";
  166. $path = OC::$SERVERROOT."/themes/$theme/apps/$app/templates/";
  167. }elseif( file_exists( OC::$SERVERROOT."/themes/$theme/apps/$app/templates/"."$name.php" )){
  168. $template = OC::$SERVERROOT."/themes/$theme/apps/$app/templates/"."$name.php";
  169. $path = OC::$SERVERROOT."/themes/$theme/apps/$app/templates/";
  170. }elseif( OC::$SERVERROOT."/apps/$app/templates/"."$name$fext.php" ){
  171. $template = OC::$SERVERROOT."/apps/$app/templates/"."$name$fext.php";
  172. $path = OC::$SERVERROOT."/apps/$app/templates/";
  173. }else{
  174. $template = OC::$SERVERROOT."/apps/$app/templates/"."$name.php";
  175. $path = OC::$SERVERROOT."/apps/$app/templates/";
  176. }
  177. }else{
  178. // Check if the template is overwritten by the selected theme
  179. if( file_exists( OC::$SERVERROOT."/themes/$theme/$app/templates/"."$name$fext.php" )){
  180. $template = OC::$SERVERROOT."/themes/$theme/$app/templates/"."$name$fext.php";
  181. $path = OC::$SERVERROOT."/themes/$theme/$app/templates/";
  182. }elseif( file_exists( OC::$SERVERROOT."/themes/$theme/$app/templates/"."$name.php" )){
  183. $template = OC::$SERVERROOT."/themes/$theme/$app/templates/"."$name.php";
  184. $path = OC::$SERVERROOT."/themes/$theme/$app/templates/";
  185. }elseif( file_exists( OC::$SERVERROOT."/$app/templates/"."$name$fext.php" )){
  186. $template = OC::$SERVERROOT."/$app/templates/"."$name$fext.php";
  187. $path = OC::$SERVERROOT."/$app/templates/";
  188. }else{
  189. $template = OC::$SERVERROOT."/$app/templates/"."$name.php";
  190. $path = OC::$SERVERROOT."/$app/templates/";
  191. }
  192. }
  193. }else{
  194. // Check if the template is overwritten by the selected theme
  195. if( file_exists( OC::$SERVERROOT."/themes/$theme/core/templates/"."$name$fext.php" )){
  196. $template = OC::$SERVERROOT."/themes/$theme/core/templates/"."$name$fext.php";
  197. $path = OC::$SERVERROOT."/themes/$theme/core/templates/";
  198. }elseif( file_exists( OC::$SERVERROOT."/themes/$theme/core/templates/"."$name.php" )){
  199. $template = OC::$SERVERROOT."/themes/$theme/core/templates/"."$name.php";
  200. $path = OC::$SERVERROOT."/themes/$theme/core/templates/";
  201. }elseif( file_exists( OC::$SERVERROOT."/core/templates/"."$name$fext.php" )){
  202. $template = OC::$SERVERROOT."/core/templates/"."$name$fext.php";
  203. $path = OC::$SERVERROOT."/core/templates/";
  204. }else{
  205. $template = OC::$SERVERROOT."/core/templates/"."$name.php";
  206. $path = OC::$SERVERROOT."/core/templates/";
  207. }
  208. }
  209. // Set the private data
  210. $this->renderas = $renderas;
  211. $this->application = $app;
  212. $this->template = $template;
  213. $this->path = $path;
  214. $this->vars = array();
  215. $this->l10n = new OC_L10N($app);
  216. }
  217. /**
  218. * @brief Assign variables
  219. * @param $key key
  220. * @param $value value
  221. * @returns true
  222. *
  223. * This function assigns a variable. It can be accessed via $_[$key] in
  224. * the template.
  225. *
  226. * If the key existed before, it will be overwritten
  227. */
  228. public function assign( $key, $value ){
  229. $this->vars[$key] = $value;
  230. return true;
  231. }
  232. /**
  233. * @brief Appends a variable
  234. * @param $key key
  235. * @param $value value
  236. * @returns true
  237. *
  238. * This function assigns a variable in an array context. If the key already
  239. * exists, the value will be appended. It can be accessed via
  240. * $_[$key][$position] in the template.
  241. */
  242. public function append( $key, $value ){
  243. if( array_key_exists( $key, $this->vars )){
  244. $this->vars[$key][] = $value;
  245. }
  246. else{
  247. $this->vars[$key] = array( $value );
  248. }
  249. }
  250. /**
  251. * @brief Add a custom element to the header
  252. * @param string tag tag name of the element
  253. * @param array $attributes array of attrobutes for the element
  254. * @param string $text the text content for the element
  255. */
  256. public function addHeader( $tag, $attributes, $text=''){
  257. $this->headers[]=array('tag'=>$tag,'attributes'=>$attributes,'text'=>$text);
  258. }
  259. /**
  260. * @brief Prints the proceeded template
  261. * @returns true/false
  262. *
  263. * This function proceeds the template and prints its output.
  264. */
  265. public function printPage(){
  266. $data = $this->fetchPage();
  267. if( $data === false ){
  268. return false;
  269. }
  270. else{
  271. print $data;
  272. return true;
  273. }
  274. }
  275. /**
  276. * @brief Proceeds the template
  277. * @returns content
  278. *
  279. * This function proceeds the template. If $this->renderas is set, it will
  280. * will produce a full page.
  281. */
  282. public function fetchPage(){
  283. $data = $this->_fetch();
  284. if( $this->renderas ){
  285. // Decide which page we show
  286. if( $this->renderas == "user" ){
  287. $page = new OC_Template( "core", "layout.user" );
  288. $page->assign('searchurl',OC_Helper::linkTo( 'search', 'index.php' ));
  289. if(array_search(OC_APP::getCurrentApp(),array('settings','admin','help'))!==false){
  290. $page->assign('bodyid','body-settings');
  291. }else{
  292. $page->assign('bodyid','body-user');
  293. }
  294. // Add navigation entry
  295. $navigation = OC_App::getNavigation();
  296. $page->assign( "navigation", $navigation);
  297. $page->assign( "settingsnavigation", OC_App::getSettingsNavigation());
  298. foreach($navigation as $entry) {
  299. if ($entry['active']) {
  300. $page->assign( 'application', $entry['name'] );
  301. break;
  302. }
  303. }
  304. }else{
  305. $page = new OC_Template( "core", "layout.guest" );
  306. }
  307. // Read the selected theme from the config file
  308. $theme=OC_Config::getValue( "theme" );
  309. // Read the detected formfactor and use the right file name.
  310. $formfactor=$_SESSION['formfactor'];
  311. if($formfactor=='default') {
  312. $fext='';
  313. }elseif($formfactor=='mobile') {
  314. $fext='.mobile';
  315. }elseif($formfactor=='tablet') {
  316. $fext='.tablet';
  317. }elseif($formfactor=='standalone') {
  318. $fext='.standalone';
  319. }else{
  320. $fext='';
  321. }
  322. // Add the core js files or the js files provided by the selected theme
  323. foreach(OC_Util::$scripts as $script){
  324. if(is_file(OC::$SERVERROOT."/themes/$theme/apps/$script$fext.js" )){
  325. $page->append( "jsfiles", OC::$WEBROOT."/themes/$theme/apps/$script$fext.js" );
  326. }elseif(is_file(OC::$SERVERROOT."/themes/$theme/apps/$script.js" )){
  327. $page->append( "jsfiles", OC::$WEBROOT."/themes/$theme/apps/$script.js" );
  328. }elseif(is_file(OC::$SERVERROOT."/apps/$script$fext.js" )){
  329. $page->append( "jsfiles", OC::$WEBROOT."/apps/$script$fext.js" );
  330. }elseif(is_file(OC::$SERVERROOT."/apps/$script.js" )){
  331. $page->append( "jsfiles", OC::$WEBROOT."/apps/$script.js" );
  332. }elseif(is_file(OC::$SERVERROOT."/themes/$theme/$script$fext.js" )){
  333. $page->append( "jsfiles", OC::$WEBROOT."/themes/$theme/$script$fext.js" );
  334. }elseif(is_file(OC::$SERVERROOT."/themes/$theme/$script.js" )){
  335. $page->append( "jsfiles", OC::$WEBROOT."/themes/$theme/$script.js" );
  336. }elseif(is_file(OC::$SERVERROOT."/$script$fext.js" )){
  337. $page->append( "jsfiles", OC::$WEBROOT."/$script$fext.js" );
  338. }elseif(is_file(OC::$SERVERROOT."/$script.js" )){
  339. $page->append( "jsfiles", OC::$WEBROOT."/$script.js" );
  340. }elseif(is_file(OC::$SERVERROOT."/themes/$theme/core/$script$fext.js" )){
  341. $page->append( "jsfiles", OC::$WEBROOT."/themes/$theme/core/$script$fext.js" );
  342. }elseif(is_file(OC::$SERVERROOT."/themes/$theme/core/$script.js" )){
  343. $page->append( "jsfiles", OC::$WEBROOT."/themes/$theme/core/$script.js" );
  344. }elseif(is_file(OC::$SERVERROOT."/core/$script$fext.js" )){
  345. $page->append( "jsfiles", OC::$WEBROOT."/core/$script$fext.js" );
  346. }else{
  347. $page->append( "jsfiles", OC::$WEBROOT."/core/$script.js" );
  348. }
  349. }
  350. // Add the css files
  351. foreach(OC_Util::$styles as $style){
  352. if(is_file(OC::$SERVERROOT."/apps/$style$fext.css" )){
  353. $page->append( "cssfiles", OC::$WEBROOT."/apps/$style$fext.css" );
  354. }elseif(is_file(OC::$SERVERROOT."/apps/$style.css" )){
  355. $page->append( "cssfiles", OC::$WEBROOT."/apps/$style.css" );
  356. }elseif(is_file(OC::$SERVERROOT."/$style$fext.css" )){
  357. $page->append( "cssfiles", OC::$WEBROOT."/$style$fext.css" );
  358. }elseif(is_file(OC::$SERVERROOT."/$style.css" )){
  359. $page->append( "cssfiles", OC::$WEBROOT."/$style.css" );
  360. }elseif(is_file(OC::$SERVERROOT."/core/$style$fext.css" )){
  361. $page->append( "cssfiles", OC::$WEBROOT."/core/$style$fext.css" );
  362. }else{
  363. $page->append( "cssfiles", OC::$WEBROOT."/core/$style.css" );
  364. }
  365. }
  366. // Add the theme css files. you can override the default values here
  367. if(!empty($theme)) {
  368. foreach(OC_Util::$styles as $style){
  369. if(is_file(OC::$SERVERROOT."/themes/$theme/apps/$style$fext.css" )){
  370. $page->append( "cssfiles", OC::$WEBROOT."/themes/$theme/apps/$style$fext.css" );
  371. }elseif(is_file(OC::$SERVERROOT."/themes/$theme/apps/$style.css" )){
  372. $page->append( "cssfiles", OC::$WEBROOT."/themes/$theme/apps/$style.css" );
  373. }elseif(is_file(OC::$SERVERROOT."/themes/$theme/$style$fext.css" )){
  374. $page->append( "cssfiles", OC::$WEBROOT."/themes/$theme/$style$fext.css" );
  375. }elseif(is_file(OC::$SERVERROOT."/themes/$theme/$style.css" )){
  376. $page->append( "cssfiles", OC::$WEBROOT."/themes/$theme/$style.css" );
  377. }elseif(is_file(OC::$SERVERROOT."/themes/$theme/core/$style$fext.css" )){
  378. $page->append( "cssfiles", OC::$WEBROOT."/themes/$theme/core/$style$fext.css" );
  379. }elseif(is_file(OC::$SERVERROOT."/themes/$theme/core/$style.css" )){
  380. $page->append( "cssfiles", OC::$WEBROOT."/themes/$theme/core/$style.css" );
  381. }
  382. }
  383. }
  384. // Add custom headers
  385. $page->assign('headers',$this->headers);
  386. foreach(OC_Util::$headers as $header){
  387. $page->append('headers',$header);
  388. }
  389. // Add css files and js files
  390. $page->assign( "content", $data );
  391. return $page->fetchPage();
  392. }
  393. else{
  394. return $data;
  395. }
  396. }
  397. /**
  398. * @brief doing the actual work
  399. * @returns content
  400. *
  401. * Includes the template file, fetches its output
  402. */
  403. private function _fetch(){
  404. // Register the variables
  405. $_ = $this->vars;
  406. $l = $this->l10n;
  407. // Execute the template
  408. ob_start();
  409. include( $this->template ); // <-- we have to use include because we pass $_!
  410. $data = ob_get_contents();
  411. @ob_end_clean();
  412. // return the data
  413. return $data;
  414. }
  415. /**
  416. * @brief Include template
  417. * @returns returns content of included template
  418. *
  419. * Includes another template. use <?php echo $this->inc('template'); ?> to
  420. * do this.
  421. */
  422. public function inc( $file, $additionalparams = null ){
  423. // $_ erstellen
  424. $_ = $this->vars;
  425. $l = $this->l10n;
  426. if( !is_null($additionalparams)){
  427. $_ = array_merge( $additionalparams, $this->vars );
  428. }
  429. // Einbinden
  430. ob_start();
  431. include( $this->path.$file.'.php' );
  432. $data = ob_get_contents();
  433. @ob_end_clean();
  434. // Daten zurückgeben
  435. return $data;
  436. }
  437. /**
  438. * @brief Shortcut to print a simple page for users
  439. * @param $application The application we render the template for
  440. * @param $name Name of the template
  441. * @param $parameters Parameters for the template
  442. * @returns true/false
  443. */
  444. public static function printUserPage( $application, $name, $parameters = array() ){
  445. $content = new OC_Template( $application, $name, "user" );
  446. foreach( $parameters as $key => $value ){
  447. $content->assign( $key, $value );
  448. }
  449. print $content->printPage();
  450. }
  451. /**
  452. * @brief Shortcut to print a simple page for admins
  453. * @param $application The application we render the template for
  454. * @param $name Name of the template
  455. * @param $parameters Parameters for the template
  456. * @returns true/false
  457. */
  458. public static function printAdminPage( $application, $name, $parameters = array() ){
  459. $content = new OC_Template( $application, $name, "admin" );
  460. foreach( $parameters as $key => $value ){
  461. $content->assign( $key, $value );
  462. }
  463. return $content->printPage();
  464. }
  465. /**
  466. * @brief Shortcut to print a simple page for guests
  467. * @param $application The application we render the template for
  468. * @param $name Name of the template
  469. * @param $parameters Parameters for the template
  470. * @returns true/false
  471. */
  472. public static function printGuestPage( $application, $name, $parameters = array() ){
  473. $content = new OC_Template( $application, $name, "guest" );
  474. foreach( $parameters as $key => $value ){
  475. $content->assign( $key, $value );
  476. }
  477. return $content->printPage();
  478. }
  479. }