template.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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. $page->assign( "navigation", OC_App::getNavigation());
  296. $page->assign( "settingsnavigation", OC_App::getSettingsNavigation());
  297. }else{
  298. $page = new OC_Template( "core", "layout.guest" );
  299. }
  300. // Read the selected theme from the config file
  301. $theme=OC_Config::getValue( "theme" );
  302. // Read the detected formfactor and use the right file name.
  303. $formfactor=$_SESSION['formfactor'];
  304. if($formfactor=='default') {
  305. $fext='';
  306. }elseif($formfactor=='mobile') {
  307. $fext='.mobile';
  308. }elseif($formfactor=='tablet') {
  309. $fext='.tablet';
  310. }elseif($formfactor=='standalone') {
  311. $fext='.standalone';
  312. }else{
  313. $fext='';
  314. }
  315. // Add the core js files or the js files provided by the selected theme
  316. foreach(OC_Util::$scripts as $script){
  317. if(is_file(OC::$SERVERROOT."/themes/$theme/apps/$script$fext.js" )){
  318. $page->append( "jsfiles", OC::$WEBROOT."/themes/$theme/apps/$script$fext.js" );
  319. }elseif(is_file(OC::$SERVERROOT."/themes/$theme/apps/$script.js" )){
  320. $page->append( "jsfiles", OC::$WEBROOT."/themes/$theme/apps/$script.js" );
  321. }elseif(is_file(OC::$SERVERROOT."/apps/$script$fext.js" )){
  322. $page->append( "jsfiles", OC::$WEBROOT."/apps/$script$fext.js" );
  323. }elseif(is_file(OC::$SERVERROOT."/apps/$script.js" )){
  324. $page->append( "jsfiles", OC::$WEBROOT."/apps/$script.js" );
  325. }elseif(is_file(OC::$SERVERROOT."/themes/$theme/$script$fext.js" )){
  326. $page->append( "jsfiles", OC::$WEBROOT."/themes/$theme/$script$fext.js" );
  327. }elseif(is_file(OC::$SERVERROOT."/themes/$theme/$script.js" )){
  328. $page->append( "jsfiles", OC::$WEBROOT."/themes/$theme/$script.js" );
  329. }elseif(is_file(OC::$SERVERROOT."/$script$fext.js" )){
  330. $page->append( "jsfiles", OC::$WEBROOT."/$script$fext.js" );
  331. }elseif(is_file(OC::$SERVERROOT."/$script.js" )){
  332. $page->append( "jsfiles", OC::$WEBROOT."/$script.js" );
  333. }elseif(is_file(OC::$SERVERROOT."/themes/$theme/core/$script$fext.js" )){
  334. $page->append( "jsfiles", OC::$WEBROOT."/themes/$theme/core/$script$fext.js" );
  335. }elseif(is_file(OC::$SERVERROOT."/themes/$theme/core/$script.js" )){
  336. $page->append( "jsfiles", OC::$WEBROOT."/themes/$theme/core/$script.js" );
  337. }elseif(is_file(OC::$SERVERROOT."/core/$script$fext.js" )){
  338. $page->append( "jsfiles", OC::$WEBROOT."/core/$script$fext.js" );
  339. }else{
  340. $page->append( "jsfiles", OC::$WEBROOT."/core/$script.js" );
  341. }
  342. }
  343. // Add the css files
  344. foreach(OC_Util::$styles as $style){
  345. if(is_file(OC::$SERVERROOT."/apps/$style$fext.css" )){
  346. $page->append( "cssfiles", OC::$WEBROOT."/apps/$style$fext.css" );
  347. }elseif(is_file(OC::$SERVERROOT."/apps/$style.css" )){
  348. $page->append( "cssfiles", OC::$WEBROOT."/apps/$style.css" );
  349. }elseif(is_file(OC::$SERVERROOT."/$style$fext.css" )){
  350. $page->append( "cssfiles", OC::$WEBROOT."/$style$fext.css" );
  351. }elseif(is_file(OC::$SERVERROOT."/$style.css" )){
  352. $page->append( "cssfiles", OC::$WEBROOT."/$style.css" );
  353. }elseif(is_file(OC::$SERVERROOT."/core/$style$fext.css" )){
  354. $page->append( "cssfiles", OC::$WEBROOT."/core/$style$fext.css" );
  355. }else{
  356. $page->append( "cssfiles", OC::$WEBROOT."/core/$style.css" );
  357. }
  358. }
  359. // Add the theme css files. you can override the default values here
  360. if(!empty($theme)) {
  361. foreach(OC_Util::$styles as $style){
  362. if(is_file(OC::$SERVERROOT."/themes/$theme/apps/$style$fext.css" )){
  363. $page->append( "cssfiles", OC::$WEBROOT."/themes/$theme/apps/$style$fext.css" );
  364. }elseif(is_file(OC::$SERVERROOT."/themes/$theme/apps/$style.css" )){
  365. $page->append( "cssfiles", OC::$WEBROOT."/themes/$theme/apps/$style.css" );
  366. }elseif(is_file(OC::$SERVERROOT."/themes/$theme/$style$fext.css" )){
  367. $page->append( "cssfiles", OC::$WEBROOT."/themes/$theme/$style$fext.css" );
  368. }elseif(is_file(OC::$SERVERROOT."/themes/$theme/$style.css" )){
  369. $page->append( "cssfiles", OC::$WEBROOT."/themes/$theme/$style.css" );
  370. }elseif(is_file(OC::$SERVERROOT."/themes/$theme/core/$style$fext.css" )){
  371. $page->append( "cssfiles", OC::$WEBROOT."/themes/$theme/core/$style$fext.css" );
  372. }elseif(is_file(OC::$SERVERROOT."/themes/$theme/core/$style.css" )){
  373. $page->append( "cssfiles", OC::$WEBROOT."/themes/$theme/core/$style.css" );
  374. }
  375. }
  376. }
  377. // Add custom headers
  378. $page->assign('headers',$this->headers);
  379. foreach(OC_Util::$headers as $header){
  380. $page->append('headers',$header);
  381. }
  382. // Add css files and js files
  383. $page->assign( "content", $data );
  384. return $page->fetchPage();
  385. }
  386. else{
  387. return $data;
  388. }
  389. }
  390. /**
  391. * @brief doing the actual work
  392. * @returns content
  393. *
  394. * Includes the template file, fetches its output
  395. */
  396. private function _fetch(){
  397. // Register the variables
  398. $_ = $this->vars;
  399. $l = $this->l10n;
  400. // Execute the template
  401. ob_start();
  402. include( $this->template ); // <-- we have to use include because we pass $_!
  403. $data = ob_get_contents();
  404. @ob_end_clean();
  405. // return the data
  406. return $data;
  407. }
  408. /**
  409. * @brief Include template
  410. * @returns returns content of included template
  411. *
  412. * Includes another template. use <?php echo $this->inc('template'); ?> to
  413. * do this.
  414. */
  415. public function inc( $file, $additionalparams = null ){
  416. // $_ erstellen
  417. $_ = $this->vars;
  418. $l = $this->l10n;
  419. if( !is_null($additionalparams)){
  420. $_ = array_merge( $additionalparams, $this->vars );
  421. }
  422. // Einbinden
  423. ob_start();
  424. include( $this->path.$file.'.php' );
  425. $data = ob_get_contents();
  426. @ob_end_clean();
  427. // Daten zurückgeben
  428. return $data;
  429. }
  430. /**
  431. * @brief Shortcut to print a simple page for users
  432. * @param $application The application we render the template for
  433. * @param $name Name of the template
  434. * @param $parameters Parameters for the template
  435. * @returns true/false
  436. */
  437. public static function printUserPage( $application, $name, $parameters = array() ){
  438. $content = new OC_Template( $application, $name, "user" );
  439. foreach( $parameters as $key => $value ){
  440. $content->assign( $key, $value );
  441. }
  442. print $content->printPage();
  443. }
  444. /**
  445. * @brief Shortcut to print a simple page for admins
  446. * @param $application The application we render the template for
  447. * @param $name Name of the template
  448. * @param $parameters Parameters for the template
  449. * @returns true/false
  450. */
  451. public static function printAdminPage( $application, $name, $parameters = array() ){
  452. $content = new OC_Template( $application, $name, "admin" );
  453. foreach( $parameters as $key => $value ){
  454. $content->assign( $key, $value );
  455. }
  456. return $content->printPage();
  457. }
  458. /**
  459. * @brief Shortcut to print a simple page for guests
  460. * @param $application The application we render the template for
  461. * @param $name Name of the template
  462. * @param $parameters Parameters for the template
  463. * @returns true/false
  464. */
  465. public static function printGuestPage( $application, $name, $parameters = array() ){
  466. $content = new OC_Template( $application, $name, "guest" );
  467. foreach( $parameters as $key => $value ){
  468. $content->assign( $key, $value );
  469. }
  470. return $content->printPage();
  471. }
  472. }