PageRenderTime 27ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vpsadmin/lib/xtemplate.lib.php

http://pef.googlecode.com/
PHP | 801 lines | 772 code | 3 blank | 26 comment | 4 complexity | e2c2e84b09ee9017e78f08646da28d92 MD5 | raw file
  1. <?php
  2. /*
  3. ./lib/xtemplate.lib.php
  4. vpsAdmin
  5. Web-admin interface for OpenVZ (see http://openvz.org)
  6. Copyright (C) 2008-2009 Pavel Snajdr, snajpa@snajpa.net
  7. This program is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation, either version 3 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program. If not, see <http://www.gnu.org/licenses/>
  17. Xtemplate
  18. Copyright (c) 2000-2001 Barnabas Debreceni [cranx@users.sourceforge.net] XTemplate
  19. Copyright (c) 2002-2007 Jeremy Coates [cocomp@users.sourceforge.net] XTemplate & CachingXTemplate
  20. */
  21. // if we are using xtemplate, this is not CLI application
  22. $_SESSION["cli_mode"] = false;
  23. class XTemplate {
  24. var $filecontents = '';
  25. var $blocks = array();
  26. var $parsed_blocks = array();
  27. var $preparsed_blocks = array();
  28. var $block_parse_order = array();
  29. var $sub_blocks = array();
  30. var $vars = array();
  31. var $filevars = array();
  32. var $filevar_parent = array();
  33. var $filecache = array();
  34. var $tpldir = '';
  35. var $files = null;
  36. var $filename = '';
  37. var $file_delim = '';//"/\{FILE\s*\"([^\"]+)\"\s*\}/m";
  38. var $filevar_delim = '';//"/\{FILE\s*\{([A-Za-z0-9\._]+?)\}\s*\}/m";
  39. var $filevar_delim_nl = '';//"/^\s*\{FILE\s*\{([A-Za-z0-9\._]+?)\}\s*\}\s*\n/m";
  40. var $block_start_delim = '<!-- '; /* block start delimiter */
  41. var $block_end_delim = '-->'; /* block end delimiter */
  42. var $block_start_word = 'BEGIN:'; /* block start word */
  43. var $block_end_word = 'END:'; /* block end word */
  44. var $tag_start_delim = '{';
  45. var $tag_end_delim = '}';
  46. var $mainblock = 'main';
  47. var $output_type = 'HTML';
  48. var $_null_string = array('' => ''); /* null string for unassigned vars */
  49. var $_null_block = array('' => ''); /* null string for unassigned blocks */
  50. var $_error = '';
  51. var $_autoreset = true; /* auto-reset sub blocks */
  52. var $_ignore_missing_blocks = true ; // NW 17 oct 2002 - Set to FALSE to
  53. var $_file_name_full_path = '';
  54. function XTemplate ($file, $tpldir = '', $files = null, $mainblock = 'main', $autosetup = true) {
  55. $this->filename = $file;
  56. $this->_file_name_full_path = realpath($file);
  57. $this->tpldir = $tpldir;
  58. if (is_array($files)) {
  59. $this->files = $files;
  60. }
  61. $this->mainblock = $mainblock;
  62. if ($autosetup) {
  63. $this->setup();
  64. }
  65. }
  66. function restart ($file, $tpldir = '', $files = null, $mainblock = 'main', $autosetup = true, $tag_start = '{', $tag_end = '}') {
  67. $this->filename = $file;
  68. $this->_file_name_full_path = realpath($file);
  69. $this->tpldir = $tpldir;
  70. if (is_array($files)) {
  71. $this->files = $files;
  72. }
  73. $this->mainblock = $mainblock;
  74. $this->tag_start_delim = $tag_start;
  75. $this->tag_end_delim = $tag_end;
  76. $this->filecontents = '';
  77. $this->blocks = array();
  78. $this->parsed_blocks = array();
  79. $this->preparsed_blocks = array();
  80. $this->block_parse_order = array();
  81. $this->sub_blocks = array();
  82. $this->vars = array();
  83. $this->filevars = array();
  84. $this->filevar_parent = array();
  85. $this->filecache = array();
  86. if ($autosetup) {
  87. $this->setup();
  88. }
  89. }
  90. function setup ($add_outer = false) {
  91. $this->tag_start_delim = preg_quote($this->tag_start_delim);
  92. $this->tag_end_delim = preg_quote($this->tag_end_delim);
  93. $this->file_delim = "/" . $this->tag_start_delim . "FILE\s*\"([^\"]+)\"\s*" . $this->tag_end_delim . "/m";
  94. $this->filevar_delim = "/" . $this->tag_start_delim . "FILE\s*" . $this->tag_start_delim . "([A-Za-z0-9\._]+?)" . $this->tag_end_delim . "\s*" . $this->tag_end_delim . "/m";
  95. $this->filevar_delim_nl = "/^\s*" . $this->tag_start_delim . "FILE\s*" . $this->tag_start_delim . "([A-Za-z0-9\._]+?)" . $this->tag_end_delim . "\s*" . $this->tag_end_delim . "\s*\n/m";
  96. if (empty($this->filecontents)) {
  97. $this->filecontents = $this->_r_getfile($this->filename);
  98. }
  99. if ($add_outer) {
  100. $this->_add_outer_block();
  101. }
  102. $this->blocks = $this->_maketree($this->filecontents, '');
  103. $this->filevar_parent = $this->_store_filevar_parents($this->blocks);
  104. $this->scan_globals();
  105. }
  106. function assign ($name, $val = '') {
  107. if (is_array($name)) {
  108. foreach ($name as $k => $v) {
  109. $this->vars[$k] = $v;
  110. }
  111. } else {
  112. $this->vars[$name] = $val;
  113. }
  114. }
  115. function assign_file ($name, $val = '') {
  116. if (is_array($name)) {
  117. foreach ($name as $k => $v) {
  118. $this->_assign_file_sub($k, $v);
  119. }
  120. } else {
  121. $this->_assign_file_sub($name, $val);
  122. }
  123. }
  124. function parse ($bname) {
  125. if (isset($this->preparsed_blocks[$bname])) {
  126. $copy = $this->preparsed_blocks[$bname];
  127. } elseif (isset($this->blocks[$bname])) {
  128. $copy = $this->blocks[$bname];
  129. } elseif ($this->_ignore_missing_blocks) {
  130. $this->_set_error("parse: blockname [$bname] does not exist");
  131. return;
  132. } else {
  133. $this->_set_error("parse: blockname [$bname] does not exist");
  134. }
  135. if (!isset($copy)) {
  136. die('Block: ' . $bname);
  137. }
  138. $copy = preg_replace($this->filevar_delim_nl, '', $copy);
  139. $var_array = array();
  140. preg_match_all("/" . $this->tag_start_delim . "([A-Za-z0-9\._]+? ?#?.*?)" . $this->tag_end_delim. "/", $copy, $var_array);
  141. $var_array = $var_array[1];
  142. foreach ($var_array as $k => $v) {
  143. $any_comments = explode('#', $v);
  144. $v = rtrim($any_comments[0]);
  145. if (sizeof($any_comments) > 1) {
  146. $comments = $any_comments[1];
  147. } else {
  148. $comments = '';
  149. }
  150. $sub = explode('.', $v);
  151. if ($sub[0] == '_BLOCK_') {
  152. unset($sub[0]);
  153. $bname2 = implode('.', $sub);
  154. $var = isset($this->parsed_blocks[$bname2]) ? $this->parsed_blocks[$bname2] : null;
  155. $nul = (!isset($this->_null_block[$bname2])) ? $this->_null_block[''] : $this->_null_block[$bname2];
  156. if ($var == '') {
  157. if ($nul == '') {
  158. $copy = preg_replace("/" . $this->tag_start_delim . $v . $this->tag_end_delim . "/m", '', $copy);
  159. } else {
  160. $copy = preg_replace("/" . $this->tag_start_delim . $v . $this->tag_end_delim . "/", "$nul", $copy);
  161. }
  162. } else {
  163. $var = trim($var);
  164. $var = str_replace('\\', '\\\\', $var);
  165. $var = str_replace('$', '\\$', $var);
  166. $var = str_replace('\\|', '|', $var);
  167. $copy = preg_replace("|" . $this->tag_start_delim . $v . $this->tag_end_delim . "|", "$var", $copy);
  168. }
  169. } else {
  170. $var = $this->vars;
  171. foreach ($sub as $v1) {
  172. if (!isset($var[$v1]) || (!is_array($var[$v1]) && strlen($var[$v1]) == 0)) {
  173. if (defined($v1)) {
  174. $var[$v1] = constant($v1);
  175. } else {
  176. $var[$v1] = null;
  177. }
  178. }
  179. $var = $var[$v1];
  180. }
  181. $nul = (!isset($this->_null_string[$v])) ? ($this->_null_string[""]) : ($this->_null_string[$v]);
  182. $var = (!isset($var)) ? $nul : $var;
  183. if ($var == '') {
  184. $copy=preg_replace("|\s*" . $this->tag_start_delim . $v . " ?#?" . $comments . $this->tag_end_delim . "\s*\n|m", '', $copy);
  185. }
  186. $var = trim($var);
  187. $var = str_replace('\\', '\\\\', $var);
  188. $var = str_replace('$', '\\$', $var);
  189. $var = str_replace('\\|', '|', $var);
  190. $copy=preg_replace("|" . $this->tag_start_delim . $v . " ?#?" . $comments . $this->tag_end_delim . "|", "$var", $copy);
  191. }
  192. }
  193. if (isset($this->parsed_blocks[$bname])) {
  194. $this->parsed_blocks[$bname] .= $copy;
  195. } else {
  196. $this->parsed_blocks[$bname] = $copy;
  197. }
  198. /* reset sub-blocks */
  199. if ($this->_autoreset && (!empty($this->sub_blocks[$bname]))) {
  200. reset($this->sub_blocks[$bname]);
  201. foreach ($this->sub_blocks[$bname] as $k => $v) {
  202. $this->reset($v);
  203. }
  204. }
  205. }
  206. function rparse ($bname) {
  207. if (!empty($this->sub_blocks[$bname])) {
  208. reset($this->sub_blocks[$bname]);
  209. foreach ($this->sub_blocks[$bname] as $k => $v) {
  210. if (!empty($v)) {
  211. $this->rparse($v);
  212. }
  213. }
  214. }
  215. $this->parse($bname);
  216. }
  217. function insert_loop ($bname, $var, $value = '') {
  218. $this->assign($var, $value);
  219. $this->parse($bname);
  220. }
  221. function array_loop ($bname, $var, &$values) {
  222. if (is_array($values)) {
  223. foreach($values as $v) {
  224. $this->assign($var, $v);
  225. $this->parse($bname);
  226. }
  227. }
  228. }
  229. function text ($bname = '') {
  230. $text = '';
  231. $bname = !empty($bname) ? $bname : $this->mainblock;
  232. $text .= isset($this->parsed_blocks[$bname]) ? $this->parsed_blocks[$bname] : $this->get_error();
  233. return $text;
  234. }
  235. function out ($bname) {
  236. $out = $this->text($bname);
  237. echo $out;
  238. }
  239. function out_file ($bname, $fname) {
  240. if (!empty($bname) && !empty($fname) && is_writeable($fname)) {
  241. $fp = fopen($fname, 'w');
  242. fwrite($fp, $this->text($bname));
  243. fclose($fp);
  244. }
  245. }
  246. function reset ($bname) {
  247. $this->parsed_blocks[$bname] = '';
  248. }
  249. function parsed ($bname) {
  250. return (!empty($this->parsed_blocks[$bname]));
  251. }
  252. function SetNullString ($str, $varname = '') {
  253. $this->_null_string[$varname] = $str;
  254. }
  255. function SetNullBlock ($str, $bname = '') {
  256. $this->_null_block[$bname] = $str;
  257. }
  258. function set_autoreset () {
  259. $this->_autoreset = true;
  260. }
  261. function clear_autoreset () {
  262. $this->_autoreset = false;
  263. }
  264. function scan_globals () {
  265. reset($GLOBALS);
  266. foreach ($GLOBALS as $k => $v) {
  267. $GLOB[$k] = $v;
  268. }
  269. $this->assign('PHP', $GLOB);
  270. }
  271. function get_error () {
  272. $retval = false;
  273. if ($this->_error != '') {
  274. switch ($this->output_type) {
  275. case 'HTML':
  276. case 'html':
  277. $retval = '<b>[XTemplate]</b><ul>' . nl2br(str_replace('* ', '<li>', str_replace(" *\n", "</li>\n", $this->_error))) . '</ul>';
  278. break;
  279. default:
  280. $retval = '[XTemplate] ' . str_replace(' *\n', "\n", $this->_error);
  281. break;
  282. }
  283. }
  284. return $retval;
  285. }
  286. function _maketree ($con, $parentblock='') {
  287. $blocks = array();
  288. $con2 = explode($this->block_start_delim, $con);
  289. if (!empty($parentblock)) {
  290. $block_names = explode('.', $parentblock);
  291. $level = sizeof($block_names);
  292. } else {
  293. $block_names = array();
  294. $level = 0;
  295. }
  296. foreach($con2 as $k => $v) {
  297. $patt = "($this->block_start_word|$this->block_end_word)\s*(\w+) ?#?.*?\s*$this->block_end_delim(.*)";
  298. $res = array();
  299. if (preg_match_all("/$patt/ims", $v, $res, PREG_SET_ORDER)) {
  300. $block_word = $res[0][1];
  301. $block_name = $res[0][2];
  302. $content = $res[0][3];
  303. if (strtoupper($block_word) == $this->block_start_word) {
  304. $parent_name = implode('.', $block_names);
  305. $block_names[++$level] = $block_name;
  306. $cur_block_name=implode('.', $block_names);
  307. $this->block_parse_order[] = $cur_block_name;
  308. $blocks[$cur_block_name] = isset($blocks[$cur_block_name]) ? $blocks[$cur_block_name] . $content : $content;
  309. $blocks[$parent_name] .= str_replace('\\', '', $this->tag_start_delim) . '_BLOCK_.' . $cur_block_name . str_replace('\\', '', $this->tag_end_delim);
  310. $this->sub_blocks[$parent_name][] = $cur_block_name;
  311. $this->sub_blocks[$cur_block_name][] = '';
  312. } else if (strtoupper($block_word) == $this->block_end_word) {
  313. unset($block_names[$level--]);
  314. $parent_name = implode('.', $block_names);
  315. $blocks[$parent_name] .= $res[0][3];
  316. }
  317. } else {
  318. $tmp = implode('.', $block_names);
  319. if ($k) {
  320. $blocks[$tmp] .= $this->block_start_delim;
  321. }
  322. $blocks[$tmp] = isset($blocks[$tmp]) ? $blocks[$tmp] . $v : $v;
  323. }
  324. }
  325. return $blocks;
  326. }
  327. function _assign_file_sub ($name, $val) {
  328. if (isset($this->filevar_parent[$name])) {
  329. if ($val != '') {
  330. $val = $this->_r_getfile($val);
  331. foreach($this->filevar_parent[$name] as $parent) {
  332. if (isset($this->preparsed_blocks[$parent]) && !isset($this->filevars[$name])) {
  333. $copy = $this->preparsed_blocks[$parent];
  334. } elseif (isset($this->blocks[$parent])) {
  335. $copy = $this->blocks[$parent];
  336. }
  337. $res = array();
  338. preg_match_all($this->filevar_delim, $copy, $res, PREG_SET_ORDER);
  339. if (is_array($res) && isset($res[0])) {
  340. foreach ($res[0] as $v) {
  341. $copy = preg_replace("/" . preg_quote($v) . "/", "$val", $copy);
  342. $this->preparsed_blocks = array_merge($this->preparsed_blocks, $this->_maketree($copy, $parent));
  343. $this->filevar_parent = array_merge($this->filevar_parent, $this->_store_filevar_parents($this->preparsed_blocks));
  344. }
  345. }
  346. }
  347. }
  348. }
  349. $this->filevars[$name] = $val;
  350. }
  351. function _store_filevar_parents ($blocks){
  352. $parents = array();
  353. foreach ($blocks as $bname => $con) {
  354. $res = array();
  355. preg_match_all($this->filevar_delim, $con, $res);
  356. foreach ($res[1] as $k => $v) {
  357. $parents[$v][] = $bname;
  358. }
  359. }
  360. return $parents;
  361. }
  362. function _set_error ($str) {
  363. $this->_error .= '* ' . $str . " *\n";
  364. }
  365. function _getfile ($file) {
  366. if (!isset($file)) {
  367. $this->_set_error('!isset file name!' . $file);
  368. return '';
  369. }
  370. if (isset($this->files)) {
  371. if (isset($this->files[$file])) {
  372. $file = $this->files[$file];
  373. }
  374. }
  375. if (!empty($this->tpldir)) {
  376. $file = $this->tpldir. '/' . $file;
  377. }
  378. if (isset($this->filecache[$file])) {
  379. $file_text=$this->filecache[$file];
  380. } else {
  381. if (is_file($file)) {
  382. if (!($fh = fopen($file, 'r'))) {
  383. $this->_set_error('Cannot open file: ' . $file);
  384. return '';
  385. }
  386. $file_text = fread($fh,filesize($file));
  387. fclose($fh);
  388. } else {
  389. $this->_set_error("[" . realpath($file) . "] ($file) does not exist");
  390. $file_text = "<b>__XTemplate fatal error: file [$file] does not exist__</b>";
  391. }
  392. $this->filecache[$file] = $file_text;
  393. }
  394. return $file_text;
  395. }
  396. function _r_getfile ($file) {
  397. $text = $this->_getfile($file);
  398. $res = array();
  399. while (preg_match($this->file_delim,$text,$res)) {
  400. $text2 = $this->_getfile($res[1]);
  401. $text = preg_replace("'".preg_quote($res[0])."'",$text2,$text);
  402. }
  403. return $text;
  404. }
  405. function _add_outer_block () {
  406. $before = $this->block_start_delim . $this->block_start_word . ' ' . $this->mainblock . ' ' . $this->block_end_delim;
  407. $after = $this->block_start_delim . $this->block_end_word . ' ' . $this->mainblock . ' ' . $this->block_end_delim;
  408. $this->filecontents = $before . "\n" . $this->filecontents . "\n" . $after;
  409. }
  410. function _pre_var_dump () {
  411. echo '<pre>';
  412. var_dump(func_get_args());
  413. echo '</pre>';
  414. }
  415. /* XTemplate extensions for vpsAdmin for lazy programmers */
  416. /**
  417. * Set title of the page
  418. * @param $title - title of page
  419. */
  420. function title ($title) {
  421. $this->assign('TITLE', $title);
  422. }
  423. function title2 ($title2) {
  424. $this->assign('S_TITLE', $title2);
  425. $this->parse('main.s_title');
  426. }
  427. function title3 ($title3) {
  428. $this->assign('S_S_TITLE', $title3);
  429. $this->parse('main.s_s_title');
  430. }
  431. /**
  432. * @param $code - lang code
  433. * @param $icon - lang icon code
  434. * @param $lang - language name
  435. * @param $class - class of image
  436. **/
  437. function lang_add($code, $icon, $lang, $class) {
  438. $this->assign('LANG_CODE', $code);
  439. $this->assign('LANG_ICON', $icon);
  440. $this->assign('LANG_IMG_CLASS', $class);
  441. $this->assign('LANG_LANG', $lang);
  442. $this->parse('main.langitem');
  443. }
  444. /**
  445. * Parse out the login box
  446. * @param $logged - is user logged in?
  447. * @param $user_name - if so, what is is nick?
  448. */
  449. function logbox ($logged = false, $user_name = 'none') {
  450. if ($logged) {
  451. $this->assign('USER_NAME', $user_name);
  452. $this->parse('main.loggedbox');
  453. }
  454. else
  455. $this->parse('main.logbox');
  456. }
  457. /**
  458. * Add item to menu
  459. * @param $title - title of the perex
  460. * @param $link - URL link of item
  461. * @param $active - Is user currently here?
  462. * @param $is_last - Is this item last in the menu?
  463. */
  464. function menu_add ($title = 'Titulek', $link = '#', $active = false, $is_last = false) {
  465. $this->assign('MENU_LINK', $link);
  466. $this->assign('MENU_TITLE', $title);
  467. $this->assign('MENU_ACTIVE', ($active) ? 'id="nav-active"' : '');
  468. $this->assign('MENU_LAST', ($is_last) ? 'class="last"' : '');
  469. $this->parse('main.menu_item');
  470. }
  471. /**
  472. * Add perex to page
  473. * @param $title - title of the perex
  474. * @param $content - HTML content of the perex
  475. */
  476. function perex ($title, $content) {
  477. $this->assign('PEREX_TITLE', $title);
  478. $this->assign('PEREX_CONTENT', $content);
  479. $this->parse('main.perex');
  480. }
  481. /**
  482. * Add perex - command output to page
  483. * @param $title - title
  484. * @param $output - array of lines
  485. */
  486. function perex_cmd_output ($title, $output) {
  487. $this->assign('PEREX_TITLE', $title);
  488. if (is_array($output))
  489. foreach ($output as $line) $content .= $line.' <br />';
  490. $this->assign('PEREX_CONTENT', $content);
  491. $this->parse('main.perex');
  492. }
  493. /**
  494. * Add link to sidebar
  495. * @param $title - link title
  496. * @param $link - link URL
  497. */
  498. function sbar_add ($title, $link) {
  499. $this->assign('SBI_TITLE' ,$title);
  500. $this->assign('SBI_LINK', $link);
  501. $this->parse('main.sidebar.sb_item');
  502. }
  503. /**
  504. * Parse out the sidebar
  505. * @param $title - tile for the sidebar
  506. */
  507. function sbar_out ($title) {
  508. $this->assign('SB_TITLE',$title);
  509. $this->parse('main.sidebar');
  510. }
  511. /**
  512. * Add table category to table header
  513. * @param $name - HTML content of the category header
  514. */
  515. function table_add_category ($name) {
  516. $this->assign('TABLE_CATEGORY', $name);
  517. $this->parse('main.table.category');
  518. }
  519. /**
  520. * Add table cell
  521. * @param $content - HTML content of the cell
  522. * @param $td_back_color - HTML background color
  523. * @param $toright - Right side text align
  524. */
  525. function table_td ($content, $td_back_color=false, $toright = false, $colspan = '1', $rowspan = '1') {
  526. $tdstyle = 'style="';
  527. if ($td_back_color) $tdstyle .= 'background:'.$td_back_color.';';
  528. if ($toright) $tdstyle .= 'text-align:right;';
  529. $tdstyle .='" colspan="'.$colspan.'" rowspan="'.$rowspan.'"';
  530. $this->assign('TDSTYLE',$tdstyle);
  531. $this->assign('TABLE_TD',$content);
  532. $this->parse('main.table.tr.td');
  533. $this->assign('TDSTYLE','');
  534. }
  535. /**
  536. * Parse out the table row
  537. * @param $tr_back_color - background color of
  538. * @param $tr_class - CSS class of row
  539. * @param $tr_class_hover - CSS class when mouse over
  540. */
  541. function table_tr ($tr_back_color=false, $tr_class=false, $tr_class_hover=false) {
  542. // UPDATED BY toms
  543. if ($tr_back_color)
  544. $this->assign('TRSTYLE','style="background:'.$tr_back_color.'"');
  545. elseif ($tr_class)
  546. $this->assign('TRSTYLE', 'class="'.$tr_class.'"');
  547. else
  548. $this->assign('TRSTYLE', '');
  549. if ($tr_class)
  550. $this->assign('TRCLASS', $tr_class);
  551. else
  552. $this->assign('TRCLASS', 'none');
  553. if ($tr_class_hover)
  554. $this->assign('TRCLASS_HOVER', $tr_class_hover);
  555. else
  556. $this->assign('TRCLASS_HOVER', 'bg');
  557. $this->parse('main.table.tr');
  558. }
  559. /**
  560. * Parse out the table
  561. */
  562. function table_out() {
  563. $this->parse('main.table');
  564. }
  565. /**
  566. * Add form to the page, begin with this
  567. * @param $action - action URL
  568. * @param $method - GET or POST
  569. */
  570. function form_create($action = '?page=', $method = 'post') {
  571. $this->assign('TABLE_FORM_BEGIN','<form action="'.$action.'" method="'.$method.'">');
  572. }
  573. /**
  574. * Add input to form
  575. * @param $label - label of textarea
  576. * @param $type - type of HTML input
  577. * @param $size - size of element
  578. * @param $name - $_RESULT[name]
  579. * @param $value - default value
  580. * @param $hint - helping hint
  581. */
  582. function form_add_input($label = 'popisek', $type = 'text', $size = '30', $name = 'input_fromgen', $value = '', $hint = '', $nchar = 0) {
  583. $uid = uniqid();
  584. $this->table_td($label);
  585. $maxlength = '';
  586. if ($nchar!=0) {
  587. $code = moo_inputremaining("input".$uid, "inputh".$uid, $nchar, $uid);
  588. if ($nchar>0) {
  589. $output = sprintf(_("<span id='inputh%s'>%d</span> chars remaining; "), $uid, $nchar-strlen($value));
  590. $maxlength = 'maxlength="'.$nchar.'"';
  591. }
  592. else
  593. $output = sprintf(_("<span id='inputh%s'>%d</span> chars needed; "), $uid, ($nchar-strlen($value))*-1);
  594. $hint = $code.$output.$hint;
  595. }
  596. $this->table_td('<input type="'.$type.'" size="'.$size.'" name="'.$name.'" id="input'. $uid .'" value="'.$value.'" '.$maxlength.' />');
  597. if ($hint != '') {
  598. $this->table_td( $hint );
  599. }
  600. $this->table_tr();
  601. }
  602. /**
  603. * Add select (combobox) to form
  604. * @param $label - label of textarea
  605. * @param $name - $_RESULT[name]
  606. * @param $options - array of options, $option[option_name] = "Option Label"
  607. * @param $selected_value - default selected value
  608. * @param $hint - helping hint
  609. */
  610. function form_add_select($label = 'popisek', $name = 'select_fromgen', $options, $selected_value = '', $hint = '') {
  611. $this->table_td($label);
  612. $code = ('<select name="'.$name.'" id="input">');
  613. if ($options)
  614. foreach ($options as $key=>$value) {
  615. if ($selected_value == $key)
  616. $code .= '<option selected="selected" value="'.$key.'" title="">'.$value.'</option> \n';
  617. else $code .= '<option value="'.$key.'" title="">'.$value.'</option> \n';
  618. }
  619. $code .= ('</select>');
  620. $this->table_td($code);
  621. if ($hint != '') $this->table_td($hint);
  622. $this->table_tr();
  623. }
  624. /**
  625. * Add textarea to form
  626. * @param $label - label of textarea
  627. * @param $cols - number of columns
  628. * @param $rows - number of rows
  629. * @param $name - $_RESULT[name]
  630. * @param $value - default value
  631. * @param $hint - helping hint
  632. */
  633. function form_add_textarea($label = 'popisek', $cols = 10, $rows = 4, $name = 'textarea_formgen', $value = '', $hint = '') {
  634. $this->table_td($label);
  635. $this->table_td('<textarea name="'.$name.'" cols="'.$cols.'" rows="'.$rows.'" id="input">'.$value.'</textarea>');
  636. if ($hint != '') $this->table_td($hint);
  637. $this->table_tr();
  638. }
  639. /**
  640. * Add checkobox to form
  641. * @param $label - label of checkbox
  642. * @param $name - $_RESULT[name]
  643. * @param $value - value if checked
  644. * @param $checked - if it is checked by default
  645. * @param $hint - helping hint
  646. */
  647. function form_add_checkbox($label = 'popisek', $name = 'input_fromgen', $value = '', $checked=false, $hint = '') {
  648. $this->table_td($label);
  649. $this->table_td('<input type="checkbox" name="'.$name.'" id="input" value="'.$value.'" '.(($checked) ? 'checked':'').' />');
  650. if ($hint != '') $this->table_td($hint);
  651. $this->table_tr();
  652. }
  653. /**
  654. * Parse out the form
  655. * @param $submit_label - label of submit button of the form
  656. */
  657. function form_out($submit_label) {
  658. $this->table_td('');
  659. $this->table_td('<input type="submit" value=" '.$submit_label.' " id="button"/>');
  660. $this->table_tr('');
  661. $this->assign('TABLE_FORM_END','</form>');
  662. $this->table_out();
  663. }
  664. /**
  665. * Add transaction line to rightside shortlog
  666. * @param $t_id - transaction ID
  667. * @param $t_server - server_id, where was transaction run
  668. * @param $t_vps - vps_id, which was transaction for
  669. * @param $t_class - background color class (CSS)
  670. */
  671. function transaction($t_id, $t_server, $t_vps, $t_label, $t_class = "pending") {
  672. $this->assign('T_ID', $t_id);
  673. $this->assign('T_SERVER', $t_server);
  674. $this->assign('T_VPS', $t_vps);
  675. $this->assign('T_LABEL', $t_label);
  676. $this->assign('T_CLASS', $t_class);
  677. switch ($t_class) {
  678. case "pending":
  679. $this->assign('T_ICO', '<img src="template/icons/transact_pending.gif"> ');
  680. break;
  681. case "ok":
  682. $this->assign('T_ICO', '<img src="template/icons/transact_ok.png"> ');
  683. break;
  684. default:
  685. $this->assign('T_ICO', '<img src="template/icons/transact_fail.png"> ');
  686. break;
  687. }
  688. $this->parse('main.transactions.item');
  689. }
  690. /**
  691. * Parse out rightside transact shortlog
  692. */
  693. function transactions_out() {
  694. $this->parse('main.transactions');
  695. }
  696. /**
  697. * @param $title - helpbox title
  698. * @param $content - helpbox content, must not have \<br\>'s
  699. */
  700. function helpbox($title, $content) {
  701. $this->assign('HELPBOX_TITLE', $title);
  702. $this->assign('HELPBOX_CONTENT', nl2br($content));
  703. $this->parse('main.helpbox');
  704. }
  705. /**
  706. * @param $data - HTML code, which will be added above the table
  707. */
  708. function table_begin($data) {
  709. $this->assign('TABLE_FORM_BEGIN', $data);
  710. }
  711. /**
  712. * @param $data - HTML code, which will be added below the table
  713. */
  714. function table_end($data) {
  715. $this->assign('TABLE_FORM_END', $data);
  716. }
  717. /**
  718. * @param $content - HTML content of adminbox
  719. */
  720. function adminbox($content) {
  721. $this->assign('ADMINBOX_CONTENT', $content);
  722. $this->parse('main.adminbox');
  723. }
  724. /**
  725. * @param $url - Where to redirect
  726. * @param $delay - Delay in msecs
  727. */
  728. function delayed_redirect($url, $delay = 1500) {
  729. $script ='
  730. <script language="JavaScript">
  731. function redirect () {
  732. setTimeout("top.location.href = \'' . $url . '\'", ' . $delay . ');
  733. }
  734. </script>
  735. ';
  736. $this->assign('SCRIPT', $script);
  737. $this->assign('SCRIPT_BODY', 'onload="redirect()"');
  738. }
  739. }
  740. /**
  741. * pages are indexed from 0, however they are displayed from 1, therefore there are some cases when -1 had to be added
  742. * @param $current integer - current page number
  743. * @param $pages integer - number of all possible pages
  744. * @param $offset integer - number of pages displayed left or right from current page
  745. * @param $display boolean - default false - returns HTML code, true - prints the code on STDO and also returns it;
  746. *
  747. * @return String - HTML code for the listing
  748. */
  749. function gen_pages_listing($current, $pages, $offset, $display=false)
  750. {
  751. $out = '<div class="page_listing">';
  752. $begin = 0;
  753. if ($current>$offset) {
  754. $begin = $current-$offset;
  755. $out .= '<a href="?page=transactions&amp;page_number=0">&laquo;</a> ';
  756. }
  757. $end = $pages;
  758. if (($pages-$current-1)>$offset)
  759. $end = $current+$offset;
  760. for ($page=$begin; $page<$end; $page++) {
  761. $out .= '&nbsp;';
  762. if ($page == $current)
  763. $out .= '<b>'.($page+1).'</b>';
  764. else
  765. $out .= '<a href="?page=transactions&amp;page_number='.$page.'">'.($page+1).'</a>';
  766. $out .= '&nbsp;';
  767. }
  768. if (($pages-1-$current)>$offset)
  769. $out .= ' <a href="?page=transactions&amp;page_number='.($pages-1).'">&raquo;</a> ';
  770. $out .= '</div>';
  771. if ($display)
  772. echo $out;
  773. return $out;
  774. }
  775. ?>