"-", 1 => "interesting", 2 => "considering", 3 => "ordering" ); $OPU_TYPES_S = array( 1 => "interesting", 2 => "considering", 3 => "ordering", 4 => "ordered" ); $OP_CURRENCIES = array( "CHF" => "CHF", "EUR" => "EUR", "USD" => "USD" ); $db_conn = null; function db_connect() { global $db_conn, $db_url; $db_conn = pg_pconnect( $db_url ); } function db_begin() { global $db_conn; return pg_exec( $db_conn, "begin work" ); } function db_commit() { global $db_conn; return pg_exec( $db_conn, "commit work" ); } function db_rollback() { global $db_conn; return pg_exec( $db_conn, "rollback work" ); } function db_escape_str( $str ) { return "'" . pg_escape_string( $str ) . "'"; } function db_escape_str_like( $str, $pfx=true, $sfx=true ) { if( $pfx ) { $pfx = "'%"; } else { $pfx = "'"; } if( $sfx ) { $sfx = "%'"; } else { $sfx = "'"; } return $pfx . pg_escape_string( $str ) . $sfx; } function db_query( $sql ) { global $db_conn; return pg_exec( $db_conn, $sql ); } function db_next( $ress ) { return pg_fetch_array( $ress ); } function db_update( $sql ) { global $db_conn; $ress = pg_exec( $db_conn, $sql ); if( $ress ) { return pg_affected_rows( $ress ); } return -1; } function db_next_nr( $table, $field, $where="" ) { $sql = "select max($field)+1 as NRNEXT, count(*) as NRCNT from $table"; if( $where != "" ) { $sql .= " where " . $where; } $ress = db_query( $sql ); if( $ress ) { $row = db_next( $ress ); if( $row ) { if( $row["nrcnt"] > 0 ) { return $row["nrnext"]; } else { return 1; } } } return 0; } function get_GET_var( $name, $dfl="", $trim=true ) { if( isset( $_GET[$name] ) ) { $dfl = ereg_replace( "\\([\\\"'])", "\\1", $_GET[$name] ); if( $trim ) { $dfl = trim( $dfl ); } } return $dfl; } function get_POST_var( $name, $dfl="", $trim=true ) { if( isset( $_POST[$name] ) ) { $dfl = ereg_replace( "\\\\([\\\\\"'])", "\\1", $_POST[$name] ); if( $trim ) { $dfl = trim( $dfl ); } } return $dfl; } function get_FORM_var( $name, $dfl="", $trim=true ) { if( isset( $_POST[$name] ) ) { $dfl = ereg_replace( "\\([\\\"'])", "\\1", $_POST[$name] ); if( $trim ) { $dfl = trim( $dfl ); } } else if( isset( $_GET[$name] ) ) { $dfl = ereg_replace( "\\([\\\"'])", "\\1", $_GET[$name] ); if( $trim ) { $dfl = trim( $dfl ); } } return $dfl; } function make_price_str( $price, $zerostr="" ) { if( $price ) { $neg = ""; if( $price < 0 ) { $neg = "-"; $price = -$price; } $i = $price / 100; $f = $price % 100; return sprintf( "%s%d.%02d", $neg, $i, $f ); } return $zerostr; } function print_select( $name, $array, $selected=null, $addarray=null, $onchange="" ) { if( $onchange != "" ) { $onchange = " onChange=\"" . $onchange . "\""; } echo ""; } function print_tab_select( $label, $name, $array, $selected=null, $addarray=null, $onchange="" ) { echo "$label "; print_select( $name, $array, $selected, $addarray, $onchange ); echo "\n"; } function print_tab_input( $label, $name, $size, $value="", $type="text" ) { if( $name ) { $name = "name=\"$name\""; } if( $size ) { $size = "size=\"$size\""; } echo "$label \n"; } function print_tab_textarea( $label, $name, $rows, $cols, $value="" ) { echo "$label \n"; } function print_error( $text ) { echo "

" . htmlentities($text) . "

\n"; } function debug_print_array( $array ) { $pfx = "

"; foreach( $array as $key => $value ) { echo $pfx."'".htmlentities($key)."'='".htmlentities($value)."'"; $pfx = "
"; } echo "

"; } function db_read_suppliers() { $suppliers = array(); $sql = "select os_nr, os_name from ordersupplier where os_del_flag = 0"; $ress = db_query( $sql ); if( $ress ) { $row = db_next( $ress ); while( $row ) { $suppliers[$row["os_nr"]] = $row["os_name"]; $row = db_next( $ress ); } } asort( $suppliers ); return $suppliers; } function db_read_users( $exclude=-1 ) { $users = array(); $sql = "select ou_nr, ou_name from orderuser where ou_del_flag = 0"; $ress = db_query( $sql ); if( $ress ) { $row = db_next( $ress ); while( $row ) { if( $row["ou_nr"] != $exclude ) { $users[$row["ou_nr"]] = $row["ou_name"]; } $row = db_next( $ress ); } } asort( $users ); return $users; } function get_currency_rate( $currency ) { if( $currency == $OS_LOCAL_CURRENCY ) { return 100; } $time = gmstrftime( "%d.%m.%Y 00:00:00" ); $sql = "select ocr_value_chf from ordercurrencyrate where " . "ocr_currency = '$currency' and ocr_date = " . "to_timestamp( '$time', 'DD.MM.YYYY HH24:MI:SS' )"; $ress = db_query( $sql ); if( $ress ) { $row = db_next( $ress ); if( $row ) { return $row["ocr_value_chf"]; } } $ifd = popen( "./currency.sh $currency", "r" ); $vstr = trim( fread( $ifd, 1024 ) ); fclose( $ifd ); if( !ereg( "^[0-9][0-9]*\$", $vstr ) ) { return 0; } $value = (int)$vstr; $sql = "insert into ordercurrencyrate ( " . "ocr_currency, ocr_date, ocr_value_chf " . ") values ( " . "'$currency', to_timestamp( '$time', 'DD.MM.YYYY HH24:MI:SS' ), $value " . ")"; db_update( $sql ); return $value; } function currency_to_chf( $price, $rate ) { return $price * $rate / 100; } function get_POST_var_price( $name, $dispname ) { $price = get_POST_var( $name ); if( !ereg( "^[0-9][0-9]*\\.[0-9][0-9]\$", $price ) ) { print_error( "invalid or missing $dispname ($price)" ); return -1; } return (int)str_replace( ".", "", $price ); } ?>