template.php 18 KB

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