1
0

functions.core.tmpl.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. /* $Id: functions.core.tmpl.php 3295 2007-12-03 00:11:03Z danez $ */
  3. /*******************************************************************************
  4. LICENSE
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License (GPL)
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. To read the license please visit http://www.gnu.org/copyleft/gpl.html
  14. *******************************************************************************/
  15. /**
  16. * initialize global template-instance "$tmpl"
  17. *
  18. * @param $theme
  19. * @param $template
  20. */
  21. function tmplInitializeInstance($theme, $template) {
  22. global $cfg, $tmpl;
  23. // theme-switch
  24. $path = ((strpos($theme, '/')) === false)
  25. ? "themes/".$theme."/tmpl/"
  26. : "themes/tf_standard_themes/tmpl/";
  27. // template-cache-switch
  28. $tmpl = ($cfg['enable_tmpl_cache'] != 0)
  29. ? new vlibTemplateCache($path.$template)
  30. : new vlibTemplate($path.$template);
  31. // set common template-vars
  32. $tmpl->setvar('theme', $theme);
  33. $tmpl->setvar('pagetitle', @ $cfg["pagetitle"]);
  34. $tmpl->setvar('main_bgcolor', @ $cfg["main_bgcolor"]);
  35. $tmpl->setvar('table_border_dk', @ $cfg["table_border_dk"]);
  36. $tmpl->setvar('table_header_bg', @ $cfg["table_header_bg"]);
  37. $tmpl->setvar('table_data_bg', @ $cfg["table_data_bg"]);
  38. $tmpl->setvar('body_data_bg', @ $cfg["body_data_bg"]);
  39. $tmpl->setvar('isAdmin', @ $cfg['isAdmin']);
  40. $tmpl->setvar('_CHARSET', @ $cfg['_CHARSET']);
  41. }
  42. /**
  43. * template-factory.
  44. *
  45. * @param $theme
  46. * @param $template
  47. * @return vlib-template-instance
  48. */
  49. function tmplGetInstance($theme, $template) {
  50. global $cfg;
  51. // theme-switch
  52. $path = ((strpos($theme, '/')) === false)
  53. ? "themes/".$theme."/tmpl/"
  54. : "themes/tf_standard_themes/tmpl/";
  55. // template-cache-switch
  56. $_tmpl = ($cfg['enable_tmpl_cache'] != 0)
  57. ? new vlibTemplateCache($path.$template)
  58. : new vlibTemplate($path.$template);
  59. // set common template-vars
  60. $_tmpl->setvar('theme', $theme);
  61. $_tmpl->setvar('pagetitle', @ $cfg["pagetitle"]);
  62. $_tmpl->setvar('main_bgcolor', @ $cfg["main_bgcolor"]);
  63. $_tmpl->setvar('table_border_dk', @ $cfg["table_border_dk"]);
  64. $_tmpl->setvar('table_header_bg', @ $cfg["table_header_bg"]);
  65. $_tmpl->setvar('table_data_bg', @ $cfg["table_data_bg"]);
  66. $_tmpl->setvar('body_data_bg', @ $cfg["body_data_bg"]);
  67. $_tmpl->setvar('isAdmin', @ $cfg['isAdmin']);
  68. $_tmpl->setvar('_CHARSET', @ $cfg['_CHARSET']);
  69. // return template-instance
  70. return $_tmpl;
  71. }
  72. /**
  73. * set Title Bar vars.
  74. *
  75. * @param $pageTitleText
  76. * @param $showButtons
  77. */
  78. function tmplSetTitleBar($pageTitleText, $showButtons = true) {
  79. global $cfg, $db, $tmpl;
  80. // set some vars
  81. $tmpl->setvar('titleBar_title', $pageTitleText);
  82. $tmpl->setvar('titleBar_showButtons', $showButtons);
  83. $tmpl->setvar('_TORRENTS', $cfg['_TORRENTS']);
  84. $tmpl->setvar('_DIRECTORYLIST', $cfg['_DIRECTORYLIST']);
  85. $tmpl->setvar('_UPLOADHISTORY', $cfg['_UPLOADHISTORY']);
  86. $tmpl->setvar('_MYPROFILE', $cfg['_MYPROFILE']);
  87. $tmpl->setvar('_MESSAGES', $cfg['_MESSAGES']);
  88. $tmpl->setvar('_ADMINISTRATION', $cfg['_ADMINISTRATION']);
  89. if ($showButtons)
  90. $tmpl->setvar('titleBar_number_messages', $db->GetOne("select count(*) from tf_messages where to_user=".$db->qstr($cfg["user"])." and IsNew = 1"));
  91. }
  92. /**
  93. * set sub-foot vars
  94. *
  95. * @param $showReturn
  96. */
  97. function tmplSetFoot($showReturn = true) {
  98. global $cfg, $tmpl;
  99. // set some vars
  100. $tmpl->setvar('_RETURNTOTRANSFERS', $cfg['_RETURNTOTRANSFERS']);
  101. $tmpl->setvar('subfoot_showReturn', $showReturn);
  102. }
  103. /**
  104. * set iid vars
  105. */
  106. function tmplSetIidVars() {
  107. global $cfg, $tmpl;
  108. // set some vars
  109. $_iid = tfb_getRequestVar('iid');
  110. $tmpl->setvar('iid', $_iid);
  111. $tmpl->setvar('mainMenu', (isset($cfg['mainMenu'][$_iid])) ? $cfg['mainMenu'][$_iid] : "home");
  112. $tmpl->setvar('fluxlink_display', $cfg['ui_displayfluxlink']);
  113. $fluxlink = getTorrentFluxLink();
  114. $tmpl->setvar( 'fluxlink_url', $fluxlink['address'] );
  115. $tmpl->setvar( 'fluxlink_name', $fluxlink['name'] );
  116. // The width should be used on all sites
  117. $tmpl->setvar('ui_dim_main_w', $cfg["ui_dim_main_w"]);
  118. }
  119. /**
  120. * set vars for Search Engine Drop Down List
  121. *
  122. * @param $selectedEngine
  123. * @param $autoSubmit
  124. */
  125. function tmplSetSearchEngineDDL($selectedEngine = 'TorrentSpy', $autoSubmit = false) {
  126. global $cfg, $tmpl;
  127. // set some vars
  128. $tmpl->setvar('autoSubmit', $autoSubmit);
  129. $handle = opendir("./inc/searchEngines");
  130. while($entry = readdir($handle))
  131. $entrys[] = $entry;
  132. natcasesort($entrys);
  133. $Engine_List = array();
  134. foreach($entrys as $entry) {
  135. if ($entry != "." && $entry != ".." && substr($entry, 0, 1) != "." && strpos($entry,"Engine.php")) {
  136. $tmpEngine = str_replace("Engine",'',substr($entry,0,strpos($entry,".")));
  137. if (array_key_exists($tmpEngine,$cfg['searchEngineLinks'])) {
  138. $hreflink = $cfg['searchEngineLinks'][$tmpEngine];
  139. $settings['searchEngineLinks'][$tmpEngine] = $hreflink;
  140. } else {
  141. $hreflink = getEngineLink($tmpEngine);
  142. $settings['searchEngineLinks'][$tmpEngine] = $hreflink;
  143. $settingsNeedsSaving = true;
  144. }
  145. array_push($Engine_List, array(
  146. 'selected' => ($selectedEngine == $tmpEngine) ? 1 : 0,
  147. 'Engine' => $tmpEngine,
  148. 'hreflink' => $hreflink,
  149. )
  150. );
  151. }
  152. }
  153. return $Engine_List;
  154. }
  155. /**
  156. * drivespace bar
  157. *
  158. */
  159. function tmplSetDriveSpaceBar() {
  160. global $cfg, $tmpl;
  161. $tmpl->setvar('_STORAGE', $cfg['_STORAGE']);
  162. $tmpl->setvar('drivespacebar_type', $cfg['drivespacebar']);
  163. $tmpl->setvar('drivespacebar_space', $cfg['driveSpace']);
  164. $tmpl->setvar('drivespacebar_space2', (100 - $cfg['driveSpace']));
  165. $tmpl->setvar('drivespacebar_freeSpace', $cfg['freeSpaceFormatted']);
  166. // color for xfer
  167. switch ($cfg['drivespacebar']) {
  168. case "xfer":
  169. $bgcolor = '#';
  170. $bgcolor .= str_pad(dechex(256 - 256 * ((100 - $cfg['driveSpace']) / 100)), 2, 0, STR_PAD_LEFT);
  171. $bgcolor .= str_pad(dechex(256 * ((100 - $cfg['driveSpace']) / 100)), 2, 0, STR_PAD_LEFT);
  172. $bgcolor .= '00';
  173. $tmpl->setvar('drivespacebar_bgcolor', $bgcolor);
  174. break;
  175. }
  176. }
  177. /**
  178. * bandwidth bars
  179. *
  180. */
  181. function tmplSetBandwidthBars() {
  182. global $cfg, $tmpl;
  183. $tmpl->setvar('bandwidthbars_type', $cfg['bandwidthbar']);
  184. // upload
  185. $max_upload = $cfg["bandwidth_up"] / 8;
  186. $percent_upload = ($max_upload > 0)
  187. ? @number_format(($cfg["total_upload"] / $max_upload) * 100, 0)
  188. : 0;
  189. $tmpl->setvar('bandwidthbars_upload_text',
  190. ($percent_upload > 0)
  191. ? @number_format($cfg["total_upload"], 2)
  192. : "0.00");
  193. $percent_upload = ($percent_upload >= 100)? 100:$percent_upload;
  194. $tmpl->setvar('bandwidthbars_upload_percent', $percent_upload);
  195. $tmpl->setvar('bandwidthbars_upload_percent2', (100 - $percent_upload));
  196. // download
  197. $max_download = $cfg["bandwidth_down"] / 8;
  198. $percent_download = ($max_download > 0)
  199. ? @number_format(($cfg["total_download"] / $max_download) * 100, 0)
  200. : 0;
  201. $tmpl->setvar('bandwidthbars_download_text',
  202. ($percent_download > 0)
  203. ? @number_format($cfg["total_download"], 2)
  204. : "0.00");
  205. $percent_download = ($percent_download >= 100)? 100:$percent_download;
  206. $tmpl->setvar('bandwidthbars_download_percent', $percent_download);
  207. $tmpl->setvar('bandwidthbars_download_percent2', (100 - $percent_download));
  208. // colors for xfer
  209. switch ($cfg['bandwidthbar']) {
  210. case "xfer":
  211. // upload
  212. $bgcolor = '#';
  213. $bgcolor .= str_pad(dechex(255 - 255 * ((100 - $percent_upload) / 150)), 2, 0, STR_PAD_LEFT);
  214. $bgcolor .= str_pad(dechex(255 * ((100 - $percent_upload) / 150)), 2, 0, STR_PAD_LEFT);
  215. $bgcolor .='00';
  216. $tmpl->setvar('bandwidthbars_upload_bgcolor', $bgcolor);
  217. // download
  218. $bgcolor = '#';
  219. $bgcolor .= str_pad(dechex(255 - 255 * ((100 - $percent_download) / 150)), 2, 0, STR_PAD_LEFT);
  220. $bgcolor .= str_pad(dechex(255 * ((100 - $percent_download) / 150)), 2, 0, STR_PAD_LEFT);
  221. $bgcolor .='00';
  222. $tmpl->setvar('bandwidthbars_download_bgcolor', $bgcolor);
  223. }
  224. }
  225. /**
  226. * gets xfer percentage bar
  227. *
  228. * @param $total
  229. * @param $used
  230. * @param $title
  231. * @return string
  232. */
  233. function tmplGetXferBar($total, $used, $title, $type='xfer') {
  234. global $cfg;
  235. // create template-instance
  236. $tmpl = tmplGetInstance($cfg["theme"], "component.xferBar.tmpl");
  237. $remaining = $total - ($used / 1048576);
  238. $remaining = max(0, min($total, $remaining));
  239. $percent = round(($remaining / $total) * 100,0);
  240. $text = ' ('.formatFreeSpace($remaining).') '.$cfg['_REMAINING'];
  241. if($type=='xfer')
  242. {
  243. $bgcolor = '#';
  244. $bgcolor .= str_pad(dechex(255 - 255 * ($percent / 150)), 2 ,0, STR_PAD_LEFT);
  245. $bgcolor .= str_pad(dechex(255 * ($percent / 150)), 2, 0, STR_PAD_LEFT);
  246. $bgcolor .='00';
  247. $tmpl->setvar('bgcolor', $bgcolor);
  248. }
  249. $tmpl->setvar('title', $title);
  250. $tmpl->setvar('percent', $percent);
  251. $tmpl->setvar('text', $text);
  252. $tmpl->setvar('type', $type);
  253. $percent_100 = 100 - $percent;
  254. $tmpl->setvar('percent_100', $percent_100);
  255. // grab the template
  256. $output = $tmpl->grab();
  257. return $output;
  258. }
  259. /**
  260. * get TF Link and Version
  261. *
  262. * @return string
  263. */
  264. function getTorrentFluxLink() {
  265. global $cfg;
  266. $torrentFluxLink['address'] = "http://tf-b4rt.berlios.de/";
  267. $torrentFluxLink['name'] = "torrentflux-b4rt ".$cfg["version"];
  268. return $torrentFluxLink;
  269. }
  270. /**
  271. * get Engine Link
  272. *
  273. * @param $searchEngine
  274. * @return string
  275. */
  276. function getEngineLink($searchEngine) {
  277. $tmpLink = '';
  278. $engineFile = 'inc/searchEngines/'.$searchEngine.'Engine.php';
  279. if (is_file($engineFile)) {
  280. $fp = @fopen($engineFile,'r');
  281. if ($fp) {
  282. $tmp = @fread($fp, filesize($engineFile));
  283. @fclose( $fp );
  284. $tmp = substr($tmp,strpos($tmp,'$this->mainURL'),100);
  285. $tmp = substr($tmp,strpos($tmp,"=")+1);
  286. $tmp = substr($tmp,0,strpos($tmp,";"));
  287. $tmpLink = trim(str_replace(array("'","\""),"",$tmp));
  288. }
  289. }
  290. return $tmpLink;
  291. }
  292. /**
  293. * Returns a string "file name" of the status image icon
  294. *
  295. * @param $sf
  296. * @return string
  297. */
  298. function getStatusImage($sf) {
  299. $hd = new HealthData();
  300. $hd->image = "black.gif";
  301. $hd->title = "";
  302. if ($sf->running == "1") {
  303. // running
  304. if ($sf->seeds < 2)
  305. $hd->image = "yellow.gif";
  306. if ($sf->seeds == 0)
  307. $hd->image = "red.gif";
  308. if ($sf->seeds >= 2)
  309. $hd->image = "green.gif";
  310. }
  311. if ($sf->percent_done >= 100) {
  312. $hd->image = (trim($sf->up_speed) != "" && $sf->running == "1")
  313. ? "green.gif" /* seeding */
  314. : "black.gif"; /* finished */
  315. }
  316. if ($hd->image != "black.gif")
  317. $hd->title = "S:".$sf->seeds." P:".$sf->peers." ";
  318. if ($sf->running == "3") {
  319. // queued
  320. $hd->image = "black.gif";
  321. }
  322. return $hd;
  323. }
  324. /**
  325. * get path to images of current theme
  326. *
  327. * @return string
  328. */
  329. function getImagesPath() {
  330. global $cfg;
  331. return "themes/".$cfg['theme']."/images/";
  332. }
  333. ?>