Can't connect to database.

"; exit; } $cmd = ""; if( isset( $_REQUEST["cmd"] ) ) { $cmd = $_REQUEST["cmd"]; } $authenticated = false; $authlevel = 0; if( isset( $_SERVER["PHP_AUTH_USER"] ) && isset( $_SERVER["PHP_AUTH_PW"] ) ) { $authlevel = -1; $sql = "select hu_level, hu_pass_change from hindi_user where " . "hu_name = \$1 and hu_password = \$2"; $values[1] = $_SERVER["PHP_AUTH_USER"]; $values[2] = $_SERVER["PHP_AUTH_PW"]; $ress = pg_query_params( $G_conn, $sql, $values ); if( $ress ) { $row = pg_fetch_array( $ress, NULL, PGSQL_ASSOC ); if( $row ) { $authenticated = true; $authlevel = $row["hu_level"]; if( $row["hu_pass_change"] > 0 ) { if( $cmd != "savepwd" ) { $cmd = "editpwd"; } } } } } $loginnr = $_SESSION["loginnr"]; if( isset( $_POST["loginnr"] ) ) { $loginnr = $_POST["loginnr"]; } if( $authlevel < 0 || ($cmd == "logout" && $_SESSION["loginnr"] == $loginnr) ) { $_SESSION["loginnr"] += 1; header( "WWW-Authenticate: Basic realm=\"Hindi Dictionary (user 'guest' pass 'guest').\"" ); //header( "Location: " . $_SERVER["HTTP_REFERER"] ); header( "HTTP/1.0 401 Unauthorized" ); echo "sorry, no access."; exit; } function page_header() { header( "Cache-Control: no-cache, must-revalidate" ); header( "Expires: Sat, 26 Jul 1997 05:00:00 GMT" ); header( "Content-type: text/html; charset=utf-8" ); echo "\n"; ?> hindi.borg.ch

hindi.borg.ch

"; } $G_types = array( 1 => "Nomen M", 2 => "Nomen W", 3 => "Nomen", 4 => "Verb", 5 => "Adjektiv", 6 => "Adverb", 7 => "Pronomen", 8 => "Praeposition", 9 => "Satz" ); $G_type_count = count($G_types); function show_env() { foreach( $_SERVER as $key => $value ) { echo "$key = '$value'
\n"; } } function text2html( $text, $cr2br ) { if( $text == "" ) { return " "; } $text = htmlspecialchars( $text ); if( $cr2br ) { $text = str_replace( "\n", "
", $text ); } return $text; } function markup2html( $text, $cr2br ) { if( $text == "" ) { return " "; } $text = htmlspecialchars( $text ); if( $cr2br ) { $text = str_replace( "\n", "
", $text ); } if( strpos( $text, "[" ) === false ) { } else { $parts = explode( "[", $text ); $partcnt = count($parts); $style = array(); $in_span = false; $text = $parts[0]; for( $i=1; $i<$partcnt; $i++ ) { $tt = explode( "]", $parts[$i], 2 ); $emit_tag = false; if( count($tt) == 2 ) { $tv = explode( "=", $tt[0], 2 ); $tag = $tv[0]; $value = ""; if( count($tv) > 0 ) { $value = $tv[1]; } switch( $tag ) { case "bc": if( preg_match( "/^[a-z0-9#]+\$/", $value ) ) { $style["background-color"] = $value; $emit_tag = true; } else if( $value == "" ) { unset( $style["background-color"] ); $emit_tag = true; } break; case "/bc": unset( $style["background-color"] ); $emit_tag = true; break; } } if( $emit_tag ) { if( $in_span ) { $text .= ""; } $stylestr = ""; foreach( $style as $n => $v ) { $stylestr .= "$n: $v; "; } if( $stylestr == "" ) { $in_span = false; $text .= $tt[1]; } else { $in_span = true; $text .= "" . $tt[1]; } } else { $text .= "[" . $parts[$i]; } } if( $in_span ) { $text .= ""; } } return $text; } function read_tags() { global $G_conn; global $G_tagids; global $G_tags; $G_tagids = array(); $G_tags = array(); $sql = "select " . "ht_id, " . "ht_name " . "from " . "hindi_tag " . "order by " . "ht_name "; $ress = pg_query( $G_conn, $sql ); if( !$ress ) { echo "

Query failed (tag).

\n"; return; } $G_tagids[0] = 0; $G_tags[0] = "-"; $G_tagids[1] = -1; $G_tags[-1] = "[keine-tags]"; $i = 2; $row = pg_fetch_array( $ress, NULL, PGSQL_ASSOC ); while( $row ) { $G_tagids[$i] = $row["ht_id"]; $G_tags[$row["ht_id"]] = $row["ht_name"]; $i++; $row = pg_fetch_array( $ress, NULL, PGSQL_ASSOC ); } } function print_option( $text, $value, $selvalue ) { $selected = ""; if( $value == $selvalue ) { $selected = " selected=\"1\""; } echo "" . htmlspecialchars( $text ) . "\n"; } function print_tag_select( $name, $value, $search ) { global $G_tagids; global $G_tags; echo ""; } function print_type_select( $name, $value, $search ) { global $G_type_count; global $G_types; echo ""; } function dict_show_table( $where, $params, $searchmsg, $authlevel ) { global $G_types, $G_type_count, $G_conn; $order = "d.hd_word_hindi, d.hd_type_hindi, d.hd_word_german"; switch( $_SESSION["sortcol"] ) { case "D": $order = "d.hd_word_german, d.hd_word_hindi, d.hd_type_hindi"; break; case "E": $order = "d.hd_word_english, d.hd_word_hindi, " . "d.hd_type_hindi, d.hd_word_german"; break; case "T": $order = "d.hd_type_hindi, d.hd_word_hindi, d.hd_word_german"; break; default: break; } $sql = "select " . "d.*, " . "t.* " . "from " . "hindi_dict d " . "left join hindi_dict_tag dt on " . "d.hd_id = dt.hd_id " . "left join hindi_tag t on " . "dt.ht_id = t.ht_id " . "where " . "d.hd_deleted = TIMESTAMP 'infinity' " . $where . "order by " . $order . ", d.hd_id, t.ht_name"; // echo "

" . htmlspecialchars( $sql ) . "

"; $ress = pg_query_params( $G_conn, $sql, $params ); if( !$ress ) { echo "

Query failed.

\n"; return; } $row = pg_fetch_array( $ress, NULL, PGSQL_ASSOC ); if( $row ) { if( $authlevel > 0 ) { ?>
\n"; } else { echo "\n"; } ?>
\n"; echo "\n"; echo "हिन्दी\n"; echo "Typ\n"; echo "Deutsch\n"; echo "English\n"; echo "Tag\n"; echo "Kommentar\n"; if( $authlevel > 0 ) { echo " \n"; } echo "\n"; while( $row ) { $id = $row["hd_id"]; $hindi = text2html( $row["hd_word_hindi"], false ); $type = $row["hd_type_hindi"]; $german = text2html( $row["hd_word_german"], false ); $english = text2html( $row["hd_word_english"], false ); $tag = text2html( $row["ht_name"], false ); $comment = markup2html( $row["hd_comment"], true ); $flags = $row["hd_flags"]; if( isset( $G_types[$type] ) ) { $type = $G_types[$type]; } else { $type = "-"; } echo "\n"; echo "" . $hindi . "\n"; echo "" . $type . "\n"; echo "" . $german . "\n"; echo "" . $english . "\n"; echo ""; if( $tag != "" ) { echo $tag; $row = pg_fetch_array( $ress, NULL, PGSQL_ASSOC ); while( $row && $row["hd_id"] == $id ) { echo "
" . text2html( $row["ht_name"], false ); $row = pg_fetch_array( $ress, NULL, PGSQL_ASSOC ); } } else { echo " "; $row = pg_fetch_array( $ress, NULL, PGSQL_ASSOC ); } echo "\n"; echo "" . $comment . "\n"; if( $authlevel > 0 ) { echo "edit"; } echo "\n"; } echo "\n"; } else if( $searchmsg != "" ) { echo "

" . htmlspecialchars( $searchmsg ) . " not found.

\n"; } } function dict_search( $authlevel ) { global $G_tags; global $G_types; $searchtext = $_SESSION["searchtext"]; $searchtype = $_SESSION["searchtype"]; $searchtag = $_SESSION["searchtag"]; $searchmsg = ""; $where = ""; $params = array(); $i = 1; if( $searchtext != "" ) { $st = str_replace( "*", "%", $searchtext ); if( $st == $searchtext ) { $where .= "and ( hd_word_hindi = \$$i or " . "hd_word_german = \$$i or hd_word_english = \$$i ) "; $params[$i] = $searchtext; } else { $where .= "and ( hd_word_hindi like \$$i or " . "hd_word_german like \$$i or hd_word_english like \$$i ) "; $params[$i] = $st; } $i++; $searchmsg .= "text '$searchtext' "; } if( $searchtype != 0 ) { if( $searchtype > 0 ) { // type $where .= "and hd_type_hindi = \$$i "; $params[$i] = $searchtype; $i++; $searchmsg .= "typ '" . $G_types[$searchtag] . "' "; } else { // any nomen $where .= "and hd_type_hindi between 1 and 3 "; $searchmsg .= "typ 'Nomen - alle' "; } } if( $searchtag != 0 ) { if( $searchtag > 0 ) { // tag $where .= "and exists ( select hd_id from hindi_dict_tag t where " . "d.hd_id = t.hd_id and t.ht_id = \$$i ) "; $params[$i] = $searchtag; $i++; $searchmsg .= "tag '" . $G_tags[$searchtag] . "' "; } else { // all tags $where .= "and not exists ( select hd_id from hindi_dict_tag t " . "where d.hd_id = t.hd_id ) "; $searchmsg .= "ohne tag "; } } if( $searchmsg != "" ) { dict_show_table( $where, $params, $searchmsg, $authlevel ); } } function dict_list( $authlevel ) { dict_show_table( "", array(), "", $authlevel ); } function dict_form( $fields ) { global $G_types, $G_type_count; $id = $fields["id"]; $hindi = htmlspecialchars( $fields["hindi"] ); $type = $fields["type"]; $german = htmlspecialchars( $fields["german"] ); $english = htmlspecialchars( $fields["english"] ); $comment = htmlspecialchars( $fields["comment"] ); $flags = $fields["flags"]; echo "
\n"; echo "
\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; // flags +++ echo "\n"; echo "\n"; echo "
हिन्दी" . "" . "
Typ"; print_type_select( "type", $type, false ); echo "
Deutsch" . "" . "
English" . "" . "
Kommentar " . "
Tags"; print_tag_select( "tag1", $fields["tag1"], false ); echo "
 "; print_tag_select( "tag2", $fields["tag2"], false ); echo "
 "; print_tag_select( "tag3", $fields["tag3"], false ); echo "
\n"; echo "
\n"; } function dict_new( $fields ) { dict_form( $fields ); } function dict_edit( $fields ) { global $G_conn; $sql = "select * from hindi_dict where " . "hd_deleted = TIMESTAMP 'infinity' and hd_id = \$1"; $params = array(); $params[1] = $fields["id"]; $ress = pg_query_params( $G_conn, $sql, $params ); if( !$ress ) { echo "

Query failed (dict).

\n"; return; } $row = pg_fetch_array( $ress, NULL, PGSQL_ASSOC ); if( !$row ) { echo "

Fetch failed (dict).

\n"; return; } $fields["hindi"] = $row["hd_word_hindi"]; $fields["type"] = $row["hd_type_hindi"]; $fields["german"] = $row["hd_word_german"]; $fields["english"] = $row["hd_word_english"]; $fields["comment"] = $row["hd_comment"]; $fields["flags"] = $row["hd_flags"]; if( pg_fetch_array( $ress, NULL, PGSQL_ASSOC ) ) { echo "

Fetched too many rows (dict).

\n"; return; } $sql = "select " . "t.ht_id, " . "t.ht_name " . "from " . "hindi_dict_tag d, " . "hindi_tag t " . "where " . "d.hd_id = \$1 and " . "d.ht_id = t.ht_id " . "order by " . "t.ht_name "; $ress = pg_query_params( $G_conn, $sql, $params ); if( !$ress ) { echo "

Query failed (tag).

\n"; return; } $tagnr = 1; $row = pg_fetch_array( $ress, NULL, PGSQL_ASSOC ); while( $row ) { switch( $tagnr ) { case 1: $fields["tag1"] = $row["ht_id"]; break; case 2: $fields["tag2"] = $row["ht_id"]; break; case 3: $fields["tag3"] = $row["ht_id"]; break; } $tagnr += 1; $row = pg_fetch_array( $ress, NULL, PGSQL_ASSOC ); } dict_form( $fields ); } function dict_save( $fields, $authlevel ) { global $G_conn; global $G_types; global $_SERVER; echo "
\n"; if( $authlevel <= 0 ) { echo "No access for guests."; return false; } if( $fields["hindi"] == "" ) { echo "hindi text darf nicht leer sein."; return false; } if( !isset( $G_types[$fields["type"]] ) ) { echo "ungueltiger typ '" . $fields["type"] . "'"; return false; } if( $fields["german"] == "" ) { echo "deutscher text darf nicht leer sein."; return false; } $sql = ""; $id = $fields["id"]; if( $id == "_" ) { $sql = "select max(hd_id) as max_id from hindi_dict"; $ress = pg_query( $G_conn, $sql ); if( !$ress ) { echo "

Query failed.

\n"; return; } $row = pg_fetch_array( $ress, NULL, PGSQL_ASSOC ); if( !$row ) { echo "

Fetch failed.

\n"; return; } $id = $row["max_id"] + 1; $fields["id"] = $id; $sql = ""; /* $sql = "insert into hindi_dict ( " . "hd_id, hd_word_hindi, hd_type_hindi, hd_word_german, " . "hd_word_english, hd_comment, hd_flags " . ") values ( " . "\$1, \$2, \$3, \$4, \$5, \$6, \$7 " . ")"; */ } else { /* $sql = "update " . "hindi_dict " . "set " . "hd_word_hindi = \$2, " . "hd_type_hindi = \$3, " . "hd_word_german = \$4, " . "hd_word_english = \$5, " . "hd_comment = \$6, " . "hd_flags = \$7 " . "where " . "hd_id = \$1"; */ $sql = "update " . "hindi_dict " . "set " . "hd_deleted = current_timestamp " . "where " . "hd_id = \$1"; } $fields["flags"] = 0; $values = array(); $values[1] = $fields["id"]; /* foreach( array( 1 => "id", 2 => "hindi", 3 => "type", 4 => "german", 5 => "english", 6 => "comment", 7 => "flags" ) as $idx => $name ) { $values[$idx] = $fields[$name]; } $ress = pg_query_params( $G_conn, $sql, $values ); */ $ress = pg_query( $G_conn, "begin" ); if( $ress && $sql != "" ) { $ress = pg_query_params( $G_conn, $sql, $values ); } $values[2] = $fields["hindi"]; $values[3] = $fields["type"]; $values[4] = $fields["german"]; $values[5] = $fields["english"]; $values[6] = $fields["comment"]; $values[7] = $fields["flags"]; $values[8] = $_SERVER["PHP_AUTH_USER"]; if( $ress ) { $sql = "insert into hindi_dict ( " . "hd_deleted, " . "hd_id, hd_word_hindi, hd_type_hindi, hd_word_german, " . "hd_word_english, hd_comment, hd_flags, hu_name " . ") values ( " . "TIMESTAMP 'infinity', " . "\$1, \$2, \$3, \$4, " . "\$5, \$6, \$7, \$8 " . ")"; $ress = pg_query_params( $G_conn, $sql, $values ); } if( $ress ) { $sql = "delete from hindi_dict_tag where hd_id = $id"; $ress = pg_query( $G_conn, $sql ); } $sql = "insert into hindi_dict_tag ( hd_id, ht_id ) " . "values ( \$1, \$2 )"; $values = array(); $values[1] = $id; if( $ress && $fields["tag1"] != 0 ) { $values[2] = $fields["tag1"]; $ress = pg_query_params( $G_conn, $sql, $values ); } if( $ress && $fields["tag2"] != 0 ) { $values[2] = $fields["tag2"]; $ress = pg_query_params( $G_conn, $sql, $values ); } if( $ress && $fields["tag3"] != 0 ) { $values[2] = $fields["tag3"]; $ress = pg_query_params( $G_conn, $sql, $values ); } if( $ress ) { $ress = pg_query( $G_conn, "commit" ); echo $fields["hindi"] . " - " . $fields["german"] . " saved."; return true; } else { $ress = pg_query( $G_conn, "rollback" ); echo "DB error."; return false; } } function edit_password() { echo "
\n"; echo "
"; echo ""; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "
Neues Passwort: "; echo ""; echo "
Nochmals: "; echo ""; echo "
 "; echo ""; echo "
\n"; echo "
\n"; } function save_password() { global $G_conn; global $_SERVER; echo "
\n"; $fields = array(); if( !get_req_var_str( $fields, "pwd1" ) ) { echo "no password 1"; return false; } if( !get_req_var_str( $fields, "pwd2" ) ) { echo "no password 2"; return false; } if( $fields["pwd1"] != $fields["pwd2"] ) { echo "passwords differ"; return false; } $values = array(); $values[1] = $fields["pwd1"]; $values[2] = $_SERVER["PHP_AUTH_USER"]; $sql = "update hindi_user set " . "hu_password = \$1, hu_pass_change = 0 " . "where hu_name = \$2 "; $ress = pg_query_params( $G_conn, $sql, $values ); echo "password updated."; return true; } function learn( $authlevel ) { if( $authlevel <= 0 ) { return; } echo "
\n"; echo "\n"; echo "
loading...
\n"; echo "\n"; } function learn_list() { global $G_conn; global $_SERVER; header( "Cache-Control: no-cache, must-revalidate" ); header( "Expires: Sat, 26 Jul 1997 05:00:00 GMT" ); header( "Content-type: application/json" ); $sql = "select " . "d.hd_id, d.hd_word_hindi, d.hd_type_hindi, " . "d.hd_word_german, " . "case " . "when l.hl_level is null then 0" . "else l.hl_level end as hl_level " . "from " . "hindi_dict d " . "left join hindi_learn l on " . "d.hd_id = l.hd_id and " . "l.hu_name = \$1 " . "order by " . "hl_level "; $values = array(); $values[1] = $_SERVER["PHP_AUTH_USER"]; $ress = pg_query_params( $G_conn, $sql, $values ); if( !ress ) { echo "({\"error\":true})"; return; } echo "({\"error\":false,\"words\":["; $row = pg_fetch_array( $ress, NULL, PGSQL_ASSOC ); $pfx = ""; while( $row ) { $id = $row["hd_id"]; $hi = $row["hd_word_hindi"]; $ty = $row["hd_type_hindi"]; $ge = $row["hd_word_german"]; $le = $row["hl_level"]; echo $pfx . '{"id":' . $id . ',"hi":"' . $hi . '","ty":' . $ty . ',"ge":"' . $ge . '","le":' . $le . '}'; $row = pg_fetch_array( $ress, NULL, PGSQL_ASSOC ); $pfx = ","; } echo "]})"; } function learn_upd() { global $G_conn; global $_SERVER; $fields = array(); if( !get_req_var_int( $fields, "id" ) ) { echo "no id"; return; } if( !get_req_var_int( $fields, "level" ) ) { echo "no level"; return; } $values = array(); $values[1] = $fields["id"]; $values[2] = $_SERVER["PHP_AUTH_USER"]; $values[3] = $fields["level"]; $sql = "update hindi_learn set " . "hl_level = \$3, hl_last_learned = current_timestamp " . "where hd_id = \$1 and hu_name = \$2 "; $ress = pg_query_params( $G_conn, $sql, $values ); if( pg_num_rows( $ress ) == 0 ) { $sql = "insert into hindi_learn ( " . "hd_id, hu_name, hl_level, hl_last_learned " . ") values ( " . "\$1, \$2, \$3, current_timestamp " . ")"; $ress = pg_query_params( $G_conn, $sql, $values ); } } function get_req_var_int( &$fields, $name, $allowneg=false ) { if( isset( $_REQUEST[$name] ) ) { $re = "^\d+\$"; if( $allowneg ) { $re = "^[+-]?\d+\$"; } if( preg_match( "/$re/", $_REQUEST[$name] ) ) { $fields[$name] = (int)$_REQUEST[$name]; return true; } } return false; } function get_req_var_str( &$fields, $name ) { if( isset( $_REQUEST[$name] ) ) { $fields[$name] = stripslashes( $_REQUEST[$name] ); return true; } return false; } function empty_fields() { $fields = array(); $fields["id"] = "_"; $fields["hindi"] = ""; $fields["type"] = 0; $fields["german"] = ""; $fields["english"] = ""; $fields["comment"] = ""; $fields["flags"] = 0; $fields["tag1"] = 0; $fields["tag2"] = 0; $fields["tag3"] = 0; return $fields; } function doit( $cmd, $authenticated, $authlevel ) { page_header(); get_req_var_str( $_SESSION, "searchtext" ); get_req_var_int( $_SESSION, "searchtype", true ); get_req_var_int( $_SESSION, "searchtag", true ); if( isset( $_REQUEST["sortcol"] ) ) { $sortcol = stripslashes( $_REQUEST["sortcol"] ); switch( $sortcol ) { case "H": case "D": case "E": case "T": $_SESSION["sortcol"] = $sortcol; break; default: break; } } $fields = empty_fields(); if( $cmd != "new" ) { get_req_var_int( $fields, "id" ); get_req_var_str( $fields, "hindi" ); get_req_var_int( $fields, "type" ); get_req_var_str( $fields, "german" ); get_req_var_str( $fields, "english" ); get_req_var_str( $fields, "comment" ); // get_req_var_int( $fields, "flags" ); get_req_var_int( $fields, "tag1" ); get_req_var_int( $fields, "tag2" ); get_req_var_int( $fields, "tag3" ); } read_tags(); echo "\n\n\n\n\n\n
"; echo "
"; echo ""; echo " "; print_type_select( "searchtype", $_SESSION["searchtype"], true ); echo " "; print_tag_select( "searchtag", $_SESSION["searchtag"], true ); echo " "; echo "
"; echo "
 "; echo "
"; echo ""; echo ""; echo " "; echo "
"; if( $authlevel > 0 ) { echo "
 "; echo "
"; echo ""; echo " "; echo "
"; echo "
 "; echo "
"; echo ""; echo " "; echo "
"; } echo "
 "; echo "
"; echo ""; echo ""; if( $authenticated ) { echo " "; } else { echo " "; } echo "
"; echo "
\n"; switch( $cmd ) { case "": case "logout": break; case "search": dict_search( $authenticated ); break; case "new": dict_new( $fields ); break; case "edit": dict_edit( $fields ); break; case "save": if( dict_save( $fields, $authlevel ) ) { $fields = empty_fields(); } dict_new( $fields ); break; case "list": dict_list( $authenticated ); break; case "editpwd": edit_password(); break; case "savepwd": if( !save_password() ) { edit_password(); } break; case "learn": learn( $authlevel ); break; default: echo "Humm? '$cmd'?"; break; } page_footer(); } if( substr($cmd,0,6) == "learn_" ) { switch( $cmd ) { case "learn_list": learn_list(); break; case "learn_upd": learn_upd(); break; default: doit( "", $authenticated, $authlevel ); break; } } else { doit( $cmd, $authenticated, $authlevel ); } // show_env(); // echo "
auth = $authenticated $authlevel"; ?>