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;
46 $name = "name='$name'";
48 $action = $_SERVER['PHP_SELF'];
51 echo "<form enctype='multipart/form-data' method='post' action='$action' $name>\n";
53 echo "<form method='post' action='$action' $name>\n";
57 //---------------------------------------------------------------------------------
59 function end_form($breaks=0)
61 global $Ajax, $form_nested;
63 if ($form_nested-- > 0) return;
65 $_SESSION['csrf_token'] = hash('sha256', uniqid(mt_rand(), true));
69 hidden('_modified', get_post('_modified', 0));
70 hidden('_token', $_SESSION['csrf_token']);
72 $Ajax->activate('_token');
75 function check_csrf_token()
77 if ($_SESSION['csrf_token'] != @$_POST['_token'])
79 display_error(_("Request from outside of this page is forbidden."));
80 error_log(_("CSRF attack detected from: ").@$_SERVER['HTTP_HOST'].' ('.@$_SERVER['HTTP_REFERER'].')');
86 function start_table($class=false, $extra="", $padding='2', $spacing='0')
88 echo "<center><table";
89 if ($class == TABLESTYLE_NOBORDER)
90 echo " class='tablestyle_noborder'";
91 elseif ($class == TABLESTYLE2)
92 echo " class='tablestyle2'";
93 elseif ($class == TABLESTYLE)
94 echo " class='tablestyle'";
97 echo " cellpadding=$padding cellspacing=$spacing>\n";
100 function end_table($breaks=0)
102 echo "</table></center>\n";
107 function start_outer_table($class=false, $extra="", $padding='2', $spacing='0', $br=false)
111 start_table($class, $extra, $padding, $spacing);
112 echo "<tr valign=top><td>\n"; // outer table
115 function table_section($number=1, $width=false)
120 $width = ($width ? "width=$width" : "");
121 //echo "</td><td class='tableseparator' $width>\n"; // outer table
122 echo "</td><td style='border-left:1px solid #cccccc;' $width>\n"; // outer table
124 echo "<table class='tablestyle_inner'>\n";
127 function end_outer_table($breaks=0, $close_table=true)
135 // outer table spacer
137 function vertical_space($params='')
139 echo "</td></tr><tr><td valign=center $params>";
142 function meta_forward($forward_to, $params="")
145 echo "<meta http-equiv='Refresh' content='0; url=$forward_to?$params'>\n";
146 echo "<center><br>" . _("You should automatically be forwarded.");
147 echo " " . _("If this does not happen") . " " . "<a href='$forward_to?$params'>" . _("click here") . "</a> " . _("to continue") . ".<br><br></center>\n";
148 if ($params !='') $params = '?'.$params;
149 $Ajax->redirect($forward_to.$params);
153 //-----------------------------------------------------------------------------------
154 // Find and replace hotkey marker.
155 // if $clean == true marker is removed and clean label is returned
156 // (for use in wiki help system), otherwise result is array of label
157 // with underlined hotkey letter and access property string.
159 function access_string($label, $clean=false)
164 if (preg_match('/(.*)&([a-zA-Z0-9])(.*)/', $label, $slices))
166 $label = $clean ? $slices[1].$slices[2].$slices[3] :
167 $slices[1].'<u>'.$slices[2].'</u>'.$slices[3];
168 $access = " accesskey='".strtoupper($slices[2])."'";
171 $label = str_replace( '&&', '&', $label);
173 return $clean ? $label : array($label, $access);
176 function hyperlink_back($center=true, $no_menu=true, $type_no=0, $trans_no=0, $final=false)
178 global $path_to_root;
183 if ($no_menu && $trans_no != 0)
185 include_once($path_to_root."/admin/db/attachments_db.inc");
186 $id = has_attachment($type_no, $trans_no);
188 $width = ($id != 0 ? "30%" : "20%");
189 start_table(false, "width=$width");
194 echo "<td align=center><a href='$path_to_root/admin/attachments.php?vw=$id' target='blanc_'>"._("View Attachment")."</a></td>\n";
195 echo "<td align=center><a href='javascript:window.print();'>"._("Print")."</a></td>\n";
197 echo "<td align=center><a href='javascript:goBack(".($final ? '-2' : '').");'>".($no_menu ? _("Close") : _("Back"))."</a></td>\n";
205 function hyperlink_no_params($target, $label, $center=true)
207 $id = default_focus();
208 $pars = access_string($label);
210 $target = $_SERVER['PHP_SELF'];
213 echo "<a href='$target' id='$id' $pars[1]>$pars[0]</a>\n";
218 function hyperlink_no_params_td($target, $label)
221 hyperlink_no_params($target, $label);
225 function viewer_link($label, $url='', $class='', $id='', $icon=null)
227 global $path_to_root;
230 $class = " class='$class'";
233 $class = " id='$id'";
237 $pars = access_string($label);
238 if (user_graphic_links() && $icon)
239 $pars[0] = set_icon($icon, $pars[0]);
240 $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>";
243 $preview_str = $label;
247 function menu_link($url, $label, $id=null)
250 $id = default_focus($id);
251 $pars = access_string($label);
252 return "<a href='$url' class='menu_option' id='$id' $pars[1]>$pars[0]</a>";
255 function submenu_option($title, $url, $id=null)
257 global $path_to_root;
258 display_note(menu_link($path_to_root . $url, $title, $id), 0, 1);
261 function submenu_view($title, $type, $number, $id=null)
263 display_note(get_trans_view_str($type, $number, $title, false, 'viewlink', $id), 0, 1);
266 function submenu_print($title, $type, $number, $id=null, $email=0, $extra=0)
268 display_note(print_document_link($number, $title, true, $type, false, 'printlink', $id, $email, $extra), 0, 1);
270 //-----------------------------------------------------------------------------------
272 function hyperlink_params($target, $label, $params, $center=true)
274 $id = default_focus();
276 $pars = access_string($label);
278 $target = $_SERVER['PHP_SELF'];
281 echo "<a id='$id' href='$target?$params'$pars[1]>$pars[0]</a>\n";
286 function hyperlink_params_td($target, $label, $params)
289 hyperlink_params($target, $label, $params, false);
293 //-----------------------------------------------------------------------------------
295 function hyperlink_params_separate($target, $label, $params, $center=false)
297 $id = default_focus();
299 $pars = access_string($label);
302 echo "<a target='_blank' id='$id' href='$target?$params' $pars[1]>$pars[0]</a>\n";
307 function hyperlink_params_separate_td($target, $label, $params)
310 hyperlink_params_separate($target, $label, $params);
314 //--------------------------------------------------------------------------------------------------
316 function alt_table_row_color(&$k, $extra_class=null)
318 $classes = $extra_class ? array($extra_class) : array();
321 array_push($classes, 'oddrow');
326 array_push($classes, 'evenrow');
329 echo "<tr class='".implode(' ', $classes)."'>\n";
332 function table_section_title($msg, $colspan=2)
334 echo "<tr><td colspan=$colspan class='tableheader'>$msg</td></tr>\n";
337 function table_header($labels, $params='')
340 foreach ($labels as $label)
341 labelheader_cell($label, $params);
344 //-----------------------------------------------------------------------------------
346 function start_row($param="")
349 echo "<tr $param>\n";
361 for ($i = 0; $i < $num; $i++)
365 $ajax_divs = array();
367 function div_start($id='', $trigger=null, $non_ajax=false)
371 if ($non_ajax) { // div for non-ajax elements
372 array_push($ajax_divs, array($id, null));
373 echo "<div style='display:none' class='js_only' ".($id !='' ? "id='$id'" : '').">";
374 } else { // ajax ready div
375 array_push($ajax_divs, array($id, $trigger===null ? $id : $trigger));
376 echo "<div ". ($id !='' ? "id='$id'" : '').">";
383 global $ajax_divs, $Ajax;
385 if (count($ajax_divs))
387 $div = array_pop($ajax_divs);
388 if ($div[1] !== null)
389 $Ajax->addUpdate($div[1], $div[0], ob_get_flush());
394 //-----------------------------------------------------------------------------
396 // $name - prefix for widget internal elements:
397 // Nth tab submit name: {$name}_N
398 // div id: _{$name}_div
399 // sel (hidden) name: _{$name}_sel
400 // $tabs - array of tabs; string: tab title or array(tab_title, enabled_status)
402 function tabbed_content_start($name, $tabs, $dft='') {
405 $selname = '_'.$name.'_sel';
406 $div = '_'.$name.'_div';
408 $sel = find_submit($name.'_', false);
410 $sel = get_post($selname, (string)($dft==='' ? key($tabs) : $dft));
412 if ($sel!==@$_POST[$selname])
413 $Ajax->activate($name);
415 $_POST[$selname] = $sel;
418 $str = "<ul class='ajaxtabs' rel='$div'>\n";
419 foreach($tabs as $tab_no => $tab) {
421 $acc = access_string(is_array($tab) ? $tab[0] : $tab);
422 $disabled = (is_array($tab) && !$tab[1]) ? 'disabled ' : '';
424 ."<button type='submit' name='{$name}_".$tab_no
425 ."' class='".((string)$tab_no===$sel ? 'current':'ajaxbutton')."' $acc[1] $disabled>"
426 ."<span>$acc[0]</span>"
432 $str .= "<div class='spaceBox'></div>\n";
433 $str .= "<input type='hidden' name='$selname' value='$sel'>\n";
434 $str .= "<div class='contentBox' id='$div'>\n";
438 function tabbed_content_end() {
439 echo "</div>"; // content box (don't change to div_end() unless div_start() is used above)
440 div_end(); // tabs widget
443 function tab_changed($name)
445 $to = find_submit("{$name}_", false);
446 if (!$to) return null;
448 return array('from' => $from = get_post("_{$name}_sel"),
452 /* Table editor interfaces. Key is editor type
453 0 => url of editor page
457 $popup_editors = array(
458 'customer' => array('/sales/manage/customers.php?debtor_no=',
459 113, _("Customers"), 900, 500),
460 'branch' => array('/sales/manage/customer_branches.php?SelectedBranch=',
461 114, _("Branches"), 900, 700),
462 'supplier' => array('/purchasing/manage/suppliers.php?supplier_id=',
463 113, _("Suppliers"), 900, 700),
464 'item' => array('/inventory/manage/items.php?stock_id=',
465 115, _("Items"), 800, 600)
468 Bind editors for various selectors.
469 $type - type of editor
470 $input - name of related input field
471 $caller - optional function key code (available values F1-F12: 112-123,
474 function set_editor($type, $input, $caller=true)
476 global $path_to_root, $Editors, $popup_editors, $Pagehelp;
478 $key = $caller===true ? $popup_editors[$type][1] : $caller;
480 $Editors[$key] = array( $path_to_root . $popup_editors[$type][0], $input,
481 $popup_editors[$type][3], $popup_editors[$type][4]);
483 $help = 'F' . ($key - 111) . ' - ';
484 $help .= $popup_editors[$type][2];
487 //------------------------------------------------------------------------------
488 // Procedures below are now obsolete. Preserved for eventual future use.
491 External page call with saving current context.
492 $call - url of external page
493 $ctx - optional. name of SESSION context object or array of names of POST
494 variables saved on call
496 function context_call($call, $ctx='')
500 foreach($ctx as $postname)
502 $context[$postname] = get_post($postname);
505 $context = isset($_SESSION[$ctx]) ? $_SESSION[$ctx] : null;
507 array_unshift($_SESSION['Context'], array('name' => $ctx,
509 'caller' => $_SERVER['PHP_SELF'],
514 Restores context after external page call and
515 returns array of data passed by external page.
517 function context_restore()
519 if ( count($_SESSION['Context'])) {
520 if ($_SERVER['PHP_SELF'] == $_SESSION['Context'][0]['caller']) {
521 $ctx = array_shift($_SESSION['Context']);
523 if (is_array($ctx['ctx'])) {
524 foreach($ctx['ctx'] as $name => $val)
526 $_POST[$name] = $val;
529 if ($ctx['name']!='')
530 $_SESSION[$ctx['name']] = $ctx['ctx'];
539 Return to caller page if the page was called from external context.
541 function context_return($ret)
543 if ( count($_SESSION['Context'])) {
544 $ctx = &$_SESSION['Context'][0];
546 meta_forward( $ctx['caller'] );
550 Clearing context stack after page cancel.
552 function context_reset()
554 $_SESSION['Context'] = array();
557 Context stack initialization
559 if (!isset($_SESSION['Context'])) {
563 Redirector for selector F4 calls.
564 $sel_editors is array of selname=>editor_page
566 function editor_redirect($sel_editors, $save_fun='') {
567 foreach ($sel_editors as $selname=>$editor)
568 if (isset($_POST['_'.$selname.'_editor'])) {
569 if (function_exists($save_fun))
571 unset($_POST['_'.$selname.'_editor']);
572 context_call($editor, array_keys($_POST));
576 Return procedure for selector F4 calls
578 function editor_return($vars, $restore_fun='') {
579 if (function_exists($restore_fun))
582 if ($ret = context_restore()) {
583 foreach ($vars as $postname=>$retname)
584 if (isset($ret[$retname])) {
585 $_POST[$postname] = $ret[$retname];
586 set_focus($postname);
591 function confirm_dialog($submit, $msg) {
592 if (find_post($submit)) {
593 display_warning($msg);
595 submit_center_first('DialogConfirm', _("Proceed"), '', true);
596 submit_center_last('DialogCancel', _("Cancel"), '', 'cancel');
599 return get_post('DialogConfirm', 0);
603 Block menu/shortcut links during transaction procesing.
605 function page_processing($msg = false)
610 $msg = _('Entered data has not been saved yet.\nDo you want to abandon changes?');
612 $js = "_validate._processing=" . (
613 $msg ? '\''.strtr($msg, array("\n"=>'\\n')) . '\';' : 'null;');
615 $Ajax->addScript(true, $js);
620 function page_modified($status = true)
624 $js = "_validate._modified=" . ($status ? 1:0).';';
626 $Ajax->addScript(true, $js);