2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
13 Retrieve value of POST variable(s).
14 For $name passed as array $dflt is not used,
15 default values can be passed as values with non-numeric keys instead.
16 If some field have user formatted numeric value, pass float default value to
17 convert automatically to POSIX.
19 function get_post($name, $dflt='')
21 if (is_array($name)) {
23 foreach($name as $key => $dflt)
24 if (!is_numeric($key)) {
25 $ret[$key] = is_float($dflt) ? input_num($key, $dflt) : get_post($key, $dflt);
27 $ret[$dflt] = get_post($dflt, null);
31 return is_float($dflt) ? input_num($name, $dflt) :
32 ((!isset($_POST[$name]) /*|| $_POST[$name] === ''*/) ? $dflt : $_POST[$name]);
34 //---------------------------------------------------------------------------------
37 function start_form($multi=false, $dummy=false, $action="", $name="")
39 // $dummy - leaved for compatibility with 2.0 API
42 if (++$form_nested) return;
45 $name = "name='$name'";
47 $action = $_SERVER['PHP_SELF'];
50 echo "<form enctype='multipart/form-data' method='post' action='$action' $name>\n";
52 echo "<form method='post' action='$action' $name>\n";
57 Flush hidden fields buffer.
59 function output_hidden()
61 global $hidden_fields;
63 echo implode('', $hidden_fields);
64 $hidden_fields = array();
66 //---------------------------------------------------------------------------------
68 function end_form($breaks=0)
70 global $Ajax, $form_nested, $hidden_fields;
72 if ($form_nested-- > 0) return;
74 $_SESSION['csrf_token'] = hash('sha256', uniqid(mt_rand(), true));
78 hidden('_modified', get_post('_modified', 0));
79 hidden('_confirmed'); // helper for final form confirmation
80 hidden('_token', $_SESSION['csrf_token']);
84 $Ajax->activate('_token');
85 $Ajax->activate('_confirmed');
88 function check_csrf_token()
90 if ($_SESSION['csrf_token'] != @$_POST['_token'])
92 display_error(_("Request from outside of this page is forbidden."));
93 error_log(_("CSRF attack detected from: ").@$_SERVER['HTTP_HOST'].' ('.@$_SERVER['HTTP_REFERER'].')');
99 function start_table($class=false, $extra="", $padding='2', $spacing='0')
101 echo "<center><table";
102 if ($class == TABLESTYLE_NOBORDER)
103 echo " class='tablestyle_noborder'";
104 elseif ($class == TABLESTYLE2)
105 echo " class='tablestyle2'";
106 elseif ($class == TABLESTYLE)
107 echo " class='tablestyle'";
110 echo " cellpadding='$padding' cellspacing='$spacing'>\n";
113 function end_table($breaks=0)
115 echo "</table></center>\n";
121 function start_outer_table($class=false, $extra="", $padding='2', $spacing='0', $br=false)
125 start_table($class, $extra, $padding, $spacing);
126 echo "<tr valign=top><td>\n"; // outer table
129 function table_section($number=1, $width=false)
134 $width = ($width ? "width='$width'" : "");
135 //echo "</td><td class='tableseparator' $width>\n"; // outer table
136 echo "</td><td style='border-left:1px solid #cccccc;' $width>\n"; // outer table
138 echo "<table class='tablestyle_inner'>\n";
141 function end_outer_table($breaks=0, $close_table=true)
149 // outer table spacer
151 function vertical_space($params='')
153 echo "</td></tr><tr><td valign=center $params>";
156 function meta_forward($forward_to, $params="", $timeout=0)
159 echo "<meta http-equiv='Refresh' content='".$timeout."; url=$forward_to?$params'>\n";
160 echo "<center><br>" . _("You should automatically be forwarded.");
161 echo " " . _("If this does not happen") . " " . "<a href='$forward_to?$params'>" . _("click here") . "</a> " . _("to continue") . ".<br><br></center>\n";
162 if ($params !='') $params = '?'.$params;
163 $Ajax->redirect($forward_to.$params);
167 //-----------------------------------------------------------------------------------
168 // Find and replace hotkey marker.
169 // if $clean == true marker is removed and clean label is returned
170 // (for use in wiki help system), otherwise result is array of label
171 // with underlined hotkey letter and access property string.
173 function access_string($label, $clean=false)
178 if (preg_match('/(.*)&([a-zA-Z0-9])(.*)/', $label, $slices))
180 $label = $clean ? $slices[1].$slices[2].$slices[3] :
181 $slices[1].'<u>'.$slices[2].'</u>'.$slices[3];
182 $access = " accesskey='".strtoupper($slices[2])."'";
185 $label = str_replace( '&&', '&', $label);
187 return $clean ? $label : array($label, $access);
190 function hyperlink_back($center=true, $no_menu=true, $type_no=0, $trans_no=0, $final=false)
192 global $path_to_root;
197 if ($no_menu && $trans_no != 0)
199 include_once($path_to_root."/admin/db/attachments_db.inc");
200 $id = has_attachment($type_no, $trans_no);
201 $attach = get_attachment_string($type_no, $trans_no);
204 $width = ($id != 0 ? "30%" : "20%");
205 start_table(false, "width='$width'");
209 echo "<td align=center><a href='javascript:window.print();'>"._("Print")."</a></td>\n";
211 echo "<td align=center><a href='javascript:goBack(".($final ? '-2' : '').");'>".($no_menu ? _("Close") : _("Back"))."</a></td>\n";
219 function hyperlink_no_params($target, $label, $center=true)
221 $id = default_focus();
222 $pars = access_string($label);
224 $target = $_SERVER['PHP_SELF'];
227 echo "<a href='$target' id='$id' $pars[1]>$pars[0]</a>\n";
232 function hyperlink_no_params_td($target, $label)
235 hyperlink_no_params($target, $label);
239 function viewer_link($label, $url='', $class='', $id='', $icon=null)
241 global $path_to_root;
244 $class = " class='$class'";
247 $class = " id='$id'";
251 $pars = access_string($label);
252 if (user_graphic_links() && $icon)
253 $pars[0] = set_icon($icon, $pars[0]);
254 - $preview_str = "<a target='_blank' $class $id href='$path_to_root/$url' onclick=\"javascript:openWindow(this.href,this.target); return false;\"$pars[1]>$pars[0]</a>";
257 $preview_str = $label;
261 function menu_link($url, $label, $id=null)
263 $id = default_focus($id);
264 $pars = access_string($label);
265 return "<a href='$url' class='menu_option' id='$id' $pars[1]>$pars[0]</a>";
268 function submenu_option($title, $url, $id=null)
270 display_note( menu_link($url, $title, $id), 0, 1);
273 function submenu_view($title, $type, $number, $id=null)
275 display_note(get_trans_view_str($type, $number, $title, false, 'viewlink', $id), 0, 1);
278 function submenu_print($title, $type, $number, $id=null, $email=0, $extra=0)
280 display_note(print_document_link($number, $title, true, $type, false, 'printlink', $id, $email, $extra), 0, 1);
282 //-----------------------------------------------------------------------------------
284 function hyperlink_params($target, $label, $params, $center=true)
286 $id = default_focus();
288 $pars = access_string($label);
290 $target = $_SERVER['PHP_SELF'];
293 echo "<a id='$id' href='$target?$params'$pars[1]>$pars[0]</a>\n";
298 function hyperlink_params_td($target, $label, $params)
301 hyperlink_params($target, $label, $params, false);
305 //-----------------------------------------------------------------------------------
307 function hyperlink_params_separate($target, $label, $params, $center=false)
309 $id = default_focus();
311 $pars = access_string($label);
314 echo "<a target='_blank' id='$id' href='$target?$params' $pars[1]>$pars[0]</a>\n";
319 function hyperlink_params_separate_td($target, $label, $params)
322 hyperlink_params_separate($target, $label, $params);
326 //--------------------------------------------------------------------------------------------------
328 function alt_table_row_color(&$k, $extra_class=null)
330 $classes = $extra_class ? array($extra_class) : array();
333 array_push($classes, 'oddrow');
338 array_push($classes, 'evenrow');
341 echo "<tr class='".implode(' ', $classes)."'>\n";
344 function table_section_title($msg, $colspan=2)
346 echo "<tr><td colspan=$colspan class='tableheader'>$msg</td></tr>\n";
349 function table_header($labels, $params='')
352 foreach ($labels as $label)
353 labelheader_cell($label, $params);
356 //-----------------------------------------------------------------------------------
358 function start_row($param="")
361 echo "<tr $param>\n";
373 for ($i = 0; $i < $num; $i++)
377 $ajax_divs = array();
379 function div_start($id='', $trigger=null, $non_ajax=false)
383 if ($non_ajax) { // div for non-ajax elements
384 array_push($ajax_divs, array($id, null));
385 echo "<div style='display:none' class='js_only' ".($id !='' ? "id='$id'" : '').">";
386 } else { // ajax ready div
387 array_push($ajax_divs, array($id, $trigger===null ? $id : $trigger));
388 echo "<div ". ($id !='' ? "id='$id'" : '').">";
395 global $ajax_divs, $Ajax;
397 if (count($ajax_divs))
399 $div = array_pop($ajax_divs);
400 if ($div[1] !== null)
401 $Ajax->addUpdate($div[1], $div[0], ob_get_flush());
406 //-----------------------------------------------------------------------------
408 // $name - prefix for widget internal elements:
409 // Nth tab submit name: {$name}_N
410 // div id: _{$name}_div
411 // sel (hidden) name: _{$name}_sel
412 // $tabs - array of tabs; string: tab title or array(tab_title, enabled_status)
414 function tabbed_content_start($name, $tabs, $dft='') {
417 $selname = '_'.$name.'_sel';
418 $div = '_'.$name.'_div';
420 $sel = find_submit($name.'_', false);
422 $sel = get_post($selname, (string)($dft==='' ? key($tabs) : $dft));
424 if ($sel!==@$_POST[$selname])
425 $Ajax->activate($name);
427 $_POST[$selname] = $sel;
430 $str = "<ul class='ajaxtabs' rel='$div'>\n";
431 foreach($tabs as $tab_no => $tab) {
433 $acc = access_string(is_array($tab) ? $tab[0] : $tab);
434 $disabled = (is_array($tab) && !$tab[1]) ? 'disabled ' : '';
436 ."<button type='submit' name='{$name}_".$tab_no
437 ."' class='".((string)$tab_no===$sel ? 'current':'ajaxbutton')."' $acc[1] $disabled>"
438 ."<span>$acc[0]</span>"
444 $str .= "<div class='spaceBox'></div>\n";
445 $str .= "<input type='hidden' name='$selname' value='$sel'>\n";
446 $str .= "<div class='contentBox' id='$div'>\n";
450 function tabbed_content_end() {
451 echo "</div>"; // content box (don't change to div_end() unless div_start() is used above)
452 div_end(); // tabs widget
455 function tab_changed($name)
457 $to = find_submit("{$name}_", false);
458 if (!$to) return null;
460 return array('from' => $from = get_post("_{$name}_sel"),
464 Check whether tab has been just switched on
466 function tab_opened($name, $tab)
468 return (get_post('_'.$name.'_sel') != $tab) && (find_submit($name.'_', false) == $tab);
471 Check whether tab has been just switched off
473 function tab_closed($name, $tab)
475 return (get_post('_'.$name.'_sel') == $tab) && (find_submit($name.'_', false) != $tab);
478 Check whether tab is visible on current page
480 function tab_visible($name, $tab)
482 $new = find_submit($name.'_', false);
483 return (get_post('_'.$name.'_sel') == $tab && !$new) || $new==$tab;
486 /* Table editor interfaces. Key is editor type
487 0 => url of editor page
491 $popup_editors = array(
492 'customer' => array('/sales/manage/customers.php?debtor_no=',
493 113, _("Customers"), 900, 500),
494 'branch' => array('/sales/manage/customer_branches.php?SelectedBranch=',
495 114, _("Branches"), 900, 700),
496 'supplier' => array('/purchasing/manage/suppliers.php?supplier_id=',
497 113, _("Suppliers"), 900, 700),
498 'item' => array('/inventory/manage/items.php?stock_id=',
499 115, _("Items"), 800, 600)
502 Bind editors for various selectors.
503 $type - type of editor
504 $input - name of related input field
505 $caller - optional function key code (available values F1-F12: 112-123,
508 function set_editor($type, $input, $caller=true)
510 global $path_to_root, $Editors, $popup_editors, $Pagehelp;
512 $key = $caller===true ? $popup_editors[$type][1] : $caller;
514 $Editors[$key] = array( $path_to_root . $popup_editors[$type][0], $input,
515 $popup_editors[$type][3], $popup_editors[$type][4]);
517 $help = 'F' . ($key - 111) . ' - ';
518 $help .= $popup_editors[$type][2];
521 //------------------------------------------------------------------------------
522 // Procedures below are now obsolete. Preserved for eventual future use.
525 External page call with saving current context.
526 $call - url of external page
527 $ctx - optional. name of SESSION context object or array of names of POST
528 variables saved on call
530 function context_call($call, $ctx='')
534 foreach($ctx as $postname)
536 $context[$postname] = get_post($postname);
539 $context = isset($_SESSION[$ctx]) ? $_SESSION[$ctx] : null;
541 array_unshift($_SESSION['Context'], array('name' => $ctx,
543 'caller' => $_SERVER['PHP_SELF'],
548 Restores context after external page call and
549 returns array of data passed by external page.
551 function context_restore()
553 if ( count($_SESSION['Context'])) {
554 if ($_SERVER['PHP_SELF'] == $_SESSION['Context'][0]['caller']) {
555 $ctx = array_shift($_SESSION['Context']);
557 if (is_array($ctx['ctx'])) {
558 foreach($ctx['ctx'] as $name => $val)
560 $_POST[$name] = $val;
563 if ($ctx['name']!='')
564 $_SESSION[$ctx['name']] = $ctx['ctx'];
573 Return to caller page if the page was called from external context.
575 function context_return($ret)
577 if ( count($_SESSION['Context'])) {
578 $ctx = &$_SESSION['Context'][0];
580 meta_forward( $ctx['caller'] );
584 Clearing context stack after page cancel.
586 function context_reset()
588 $_SESSION['Context'] = array();
591 Context stack initialization
593 if (!isset($_SESSION['Context'])) {
597 Redirector for selector F4 calls.
598 $sel_editors is array of selname=>editor_page
600 function editor_redirect($sel_editors, $save_fun='') {
601 foreach ($sel_editors as $selname=>$editor)
602 if (isset($_POST['_'.$selname.'_editor'])) {
603 if (function_exists($save_fun))
605 unset($_POST['_'.$selname.'_editor']);
606 context_call($editor, array_keys($_POST));
610 Return procedure for selector F4 calls
612 function editor_return($vars, $restore_fun='') {
613 if (function_exists($restore_fun))
616 if ($ret = context_restore()) {
617 foreach ($vars as $postname=>$retname)
618 if (isset($ret[$retname])) {
619 $_POST[$postname] = $ret[$retname];
620 set_focus($postname);
625 function confirm_dialog($submit, $msg) {
626 if (find_post($submit)) {
627 display_warning($msg);
629 submit_center_first('DialogConfirm', _("Proceed"), '', true);
630 submit_center_last('DialogCancel', _("Cancel"), '', 'cancel');
633 return get_post('DialogConfirm', 0);
636 Confirm dialog to be used optionally in final form checking routine.
637 Displays warning conditionally unless it was displayed
639 function display_confirmation($msg)
643 if (!get_post('_confirmed'))
645 $_POST['_confirmed'] = 1;
646 display_warning($msg);
652 Block menu/shortcut links during transaction procesing.
654 function page_processing($msg = false)
659 $msg = _('Entered data has not been saved yet.\nDo you want to abandon changes?');
661 $js = "_validate._processing=" . (
662 $msg ? '\''.strtr($msg, array("\n"=>'\\n')) . '\';' : 'null;');
664 $Ajax->addScript(true, $js);
669 function page_modified($status = true)
673 $js = "_validate._modified=" . ($status ? 1:0).';';
675 $Ajax->addScript(true, $js);