$DEBUG = 1 ; function db_connect_or_die () { $conn = @pg_connect ( DB_CONNECT_STRING () ) ; if ( ! $conn ) { echo "Can't connect to database; sorry.\n" ; exit ; } return $conn ; } function DUMP ( $level, $msg ) { global $DEBUG ; if ( ( $level <= $DEBUG ) ) { echo "
\n", RED ( $msg), "\n" ;
if ( func_num_args() == 3 )
{ $tab = func_get_arg ( 2 ) ;
printf ( "
tab=(%s)\n", implode ( ',', $tab ) ) ;
}
echo "
\n" ;
}
}
$HTML_STARTED = 0 ;
function send_header ()
{ echo "Content-Type: text/html\n\n" ; flush() ; }
function meta_line ( $name, $content )
{ $txt = htmlspecialchars ( $content ) ;
return sprintf ( "\n" ) ;
}
function switch_actv ( $ADM )
{ assert_isroot () ;
if ( $ADM != 'ON' and $ADM != 'OFF' )
{ html_exit ( "bad ADM ($ADM)" ) ; }
actv_root ( $ADM == 'ON' ? 't' : 'f' ) ;
$uri = $_SERVER [ "REQUEST_URI" ] ;
$uri = preg_replace ( '/[\?&]?ADM=(ON|OFF)/', '', $uri ) ;
$uri = preg_replace ( '/\?$/', '', $uri ) ;
header ( 'Location: ' . $uri ) ;
exit ;
}
function menubox ()
{ global $USER ;
$left = TR
( TH ( repos_url () )
. TH ( about_url () )
. ( ( $USER and Conf ( 'has_groups' ) )
? TH ( repo_groups_url () )
: ''
)
. ( ( $USER and Conf () -> have_guests () )
? TH ( repo_guests_url () )
: ''
)
) ;
$xtras = array () ;
if ( $USER and is_guest () ) { $xtras [] = TH ( prefs_url () ) ; }
if ( isroot () )
{ $xtras = array
( TH ( stats_url () )
, TH ( conf_url () )
, TH ( ' - ' )
, TH ( switch_url () )
, TH ( ' ← ' )
) ;
}
if ( $xtras ) { $xtra = implode ( '', $xtras ) ; }
$right = TR
( ( $USER
? $xtra
. TH ( "$USER → " . logout_url () )
: TH ( login_url () )
)
) ;
$atr = "BORDER=0 CELLPADDING=5" ;
return DIV
( 'menubox'
, ( TABA ( "$atr STYLE='float:left'" , $left )
. TABA ( "$atr STYLE='float:right'", $right )
. "
\n"
)
) ;
}
function html_start ( $title, $right = '', $meta = '' )
{ global $HTML_STARTED ;
$HTML_STARTED = 1 ;
$head_title = preg_replace ( '/<[^>]*>/', '', $title ) ;
$motd = Conf ( 'motd' ) ;
if ( $motd ) { $motd = "
\n" . DIV ( 'motdbox', $motd ) ; } $menu = menubox () ; $top_head = Conf ( 'top_head' ) ; $aux_head = Conf ( 'aux_head' ) ; $top_body = Conf ( 'top_body' ) ; echo <<
$top_headversion $vers |
Error: $msg
\n" ; html_end () ; exit ; } $TAG_NLs = 'H1 H2 H3' ; $TAG_NL = array () ; foreach ( explode ( ' ', $TAG_NLs ) as $t ) { $TAG_NL [ $t ] = 1 ; } function TAG_NL ( $t ) { global $TAG_NL ; return ( array_key_exists ( $t, $TAG_NL ) ? "\n" : '' ) ; } # echo sprintf ( "(%s)", implode ( ',', array_keys ( $TAG_NL ) ) ) ; function _TAG1 ( $t, $x, $c ) { $nl = TAG_NL ( $t ) ; if ( $c ) { return sprintf ( '<%s CLASS="%s">%s%s>' . $nl, $t, $c, $x, $t ) ; } else { return sprintf ( '<%s>%s%s>' . $nl, $t, $x, $t ) ; } } function _TAG2 ( $t, $a, $x, $c ) { $nl = TAG_NL ( $t ) ; if ( $c ) { return sprintf ( '<%s %s CLASS="%s">%s%s>' . $nl, $t, $a, $c, $x, $t ) ; } else { return sprintf ( '<%s %s>%s%s>' . $nl, $t, $a, $x, $t ) ; } } function H2 ( $txt, $c = '' ) { return _TAG1 ( "H2", $txt, $c ) ; } function H3 ( $txt, $c = '' ) { return _TAG1 ( "H3", $txt, $c ) ; } function H4 ( $txt, $c = '' ) { return _TAG1 ( "H4", $txt, $c ) ; } function H5 ( $txt, $c = '' ) { return _TAG1 ( "H5", $txt, $c ) ; } function SPN ( $txt, $c = '' ) { return _TAG1 ( "SPAN", $txt, $c ) ; } function DIV ( $atr, $txt ) { return "
%s\n", $x ) ; } function BQ ( $x ) { return sprintf ( "
\n%s\n", $x ) ; } function nb ( $x ) { return ( ( $x == '' ) ? ' ' : $x ) ; } function uml2txt ( $x ) { return preg_replace ( '/&(.)uml;/', "\${1}e", $x ) ; } function UL ( $txt ) { return "
\n" . pg_errormessage() ) ; }
$num = pg_numrows ( $sel ) ;
for ( $i = 0 ; $i < $num ; $i++ )
{ $res [] = pg_fetch_object ( $sel, $i ) ; }
return $res ;
}
function db_fail ( $qwe, $tab )
{ html_exit
( sprintf
( "can't (%s)
\n%s
\nvalues (%s)
\n"
, pg_errormessage()
, $qwe
, ( $tab ? implode ( ',', $tab ) : '' )
)
) ;
}
function db_query_params ( $qwe, $tab )
{ global $conn ;
DUMP ( 2, $qwe, $tab ) ;
$sel = pg_query_params ( $conn, $qwe, $tab ) ;
if ( $sel === FALSE ) { db_fail ( $qwe, $tab ) ; }
return $sel ;
}
function form_radio ( $nam, $lst, $sel )
{ $res = '' ;
foreach ( $lst as $itm )
{ $chk = '' ; $txt = $itm ;
if ( $itm == $sel ) { $chk = 'CHECKED' ; $txt = BLD ( $itm ) ; }
$res .= "$txt \n" ;
}
return $res ;
}
function form_radio_tab ( $nam, $lst, $sel )
{ $res = '' ;
$sep = ( func_num_args() == 4 ) ? func_get_arg ( 3 ) : '
' ;
foreach ( $lst as $idx => $itm )
{ $chk = '' ; $txt = $itm ;
if ( $idx == $sel )
{ $chk = 'CHECKED' ; $txt = BLD ( $itm ) ; }
$res .=
" $txt$sep\n" ;
}
return $res ;
}
class Table
{ public $_tab ;
public $_pkey ;
function table_init ( $tab, $pkey )
{ $this -> _tab = $tab ;
$this -> _pkey = $pkey ;
}
function class_name () { return get_class ( $this ) ; }
function _next_val ()
{ global $conn ;
$tab = $this -> _tab ;
$pkey = $this -> _pkey ;
# $qwe = "SELECT $pkey FROM $tab WHERE 0 = 1" ;
# $sel = pg_Exec ( $conn, $qwe ) ;
# if ( ! $sel ) { html_exit ( "can't '$qwe'
\n" . pg_errormessage() ) ; } # $num = pg_field_num ( $sel, $pkey ) ; # $typ = pg_field_type ( $sel, $num ) ; # if ( ! preg_match ( '/^int/', $typ ) ) # { html_exit # ( "get_next_id : table $tab key $pkey has typ $typ (not int)" ) ; # } # # flush # $cnt = pg_numrows ( $sel ) ; # for ( $i = 0 ; $i < $cnt ; $i++ ) { pg_fetch_object ( $sel, $i ) ; } $qwe = sprintf ( "SELECT nextval ( '%s_%s_seq' ) as id", $tab, $pkey ) ; $tups = db_get ( $qwe ) ; if ( count ( $tups ) != 1 ) { html_exit ( "next_val : count != 1 in ($qwe)" ) ; } return $tups [ 0 ] -> id ; } function db_get_id ( $id ) { global $conn ; $qwe = "SELECT * FROM {$this->_tab} WHERE {$this->_pkey} = $1" ; $sel = db_query_params ( $qwe, array ( $id ) ) ; if ( ! $sel ) { html_exit ( "can't '$qwe'
\n" . pg_errormessage() ) ; } $num = pg_numrows ( $sel ) ; if ( $num == 1 ) { $tmp = pg_fetch_array ( $sel, 0 ) ; while ( list ( $key, $val ) = each ( $tmp ) ) { $this -> $key = $val ; } } return $num == 1 ; } function db_get_qwe ( $what, $from, $where, $order, $limit ) { global $conn ; $tab = $this -> _tab ; $pkey = $this -> _pkey ; if ( $from and ! $order ) { $distinct = "DISTINCT ON ( $tab.$pkey )" ; } else { $distinct = 'ALL' ; } if ( $what == '' ) { $what = "$distinct $tab.*" ; } if ( $from ) { $from = ", $from" ; } if ( $where ) { $where = "WHERE $where" ; } if ( $order ) { $order = "ORDER BY $order" ; } $limit = $limit ? "LIMIT $limit" : '' ; return "SELECT $what FROM $tab $from $where $order $limit" ; } function db_get_all ( $from, $where, $order, $limit = 0 ) { global $conn ; $tab = $this -> _tab ; $pkey = $this -> _pkey ; $qwe = $this -> db_get_qwe ( '', $from, $where, $order, $limit ) ; DUMP ( 2, $qwe ) ; $sel = pg_Exec ($conn, $qwe) ; if ( ! $sel ) { html_exit ( "can't '$qwe'
\n" . pg_errormessage() ) ; } $num = pg_numrows ( $sel ) ; $res = array () ; for ( $i = 0 ; $i < $num ; $i++ ) { $new = call_user_func ( array ( $this, 'inst' ) ) ; $tmp = pg_fetch_array ( $sel, $i ) ; while ( list ( $key, $val ) = each ( $tmp ) ) { $new -> $key = $val ; } if ( $limit == 1 ) { $res = $new ; } elseif ( $order ) { $res [] = $new ; } else { $res [ $new -> $pkey ] = $new ; } } return $res ; } function db_get1 ( $from, $where, $order ) { return $this -> db_get_all ( $from, $where, $order, 1 ) ; } function db_get_cnt ( $from, $where ) { global $conn ; $tab = $this -> _tab ; if ( $from ) { $from = ", $from" ; } if ( $where ) { $where = "WHERE $where" ; } $qwe = "SELECT count(*) FROM $tab $from $where" ; DUMP ( 2, $qwe ) ; $sel = pg_Exec ($conn, $qwe) ; if ( ! $sel ) { html_exit ( "can't '$qwe'
\n" . pg_errormessage() ) ; } $num = pg_numrows ( $sel ) ; return pg_fetch_object ( $sel, 0 ) -> count ; } function selectbox ( $name, $pval, $qwe ) { $tab = $this -> _tab ; $pkey = $this -> _pkey ; $vals = $this -> db_get_all ( '', $qwe, '' ) ; if ( count ( $vals ) == 1 and array_key_exists ( $pval, $vals ) ) { return $pval . "" ; } $res = "\n" ; return $res ; } function repr ( $fld ) { return $this -> $fld ; } function repr_bool ( $fld ) { return repr_bool ( $this -> $fld ) ; } function insert () { $tag4fld = func_num_args () ? func_get_arg ( 0 ) : $this -> fields () ; $fields = array_keys ( $tag4fld ) ; $tag = $this -> tag () ; $tab = array () ; $vls = array () ; $idx = 1 ; foreach ( $fields as $field ) { $tab [] = $this -> $field ; $vls [] = '$' . $idx ++ ; } $qwe = sprintf ( 'INSERT INTO %s ( %s ) VALUES ( %s ) ; ' , $this -> _tab , implode ( ',', $fields ) , implode ( ',', $vls ) ) ; db_query_params ( $qwe, $tab ) ; $cls = $this -> class_name () ; return "inserted $cls $tag" ; } function update () { $key = $this -> _pkey ; $cls = get_class ( $this ) ; $prev = new $cls ; if ( ! $prev -> db_get_id ( $this -> $key ) ) { html_exit ( "can't db_get_id {$this->_tab}($key)" ) ; } $res = array () ; $tag4fld = func_num_args () ? func_get_arg ( 0 ) : $this -> fields () ; $fields = array_keys ( $tag4fld ) ; $tab = array () ; $vls = array () ; $idx = 1 ; foreach ( $fields as $field ) { $val = $this -> $field ; $prv = $prev -> $field ; if ( $val != $prv ) { $tag = $tag4fld [ $field ] ; $rep_prv = $prev -> repr ( $field ) ; $rep_val = $this -> repr ( $field ) ; $res [] = "$tag [$rep_prv] → [$rep_val]" ; $tab [] = $val ; $vls [] = sprintf ( "%s = $%d", $field, $idx ) ; $idx ++ ; } # echo LI ( "$field : new=[$val] prev=[$prv]" ) ; } if ( $res ) { $qwe = sprintf ( "UPDATE %s SET %s WHERE %s = %s ; " , $this -> _tab , implode ( ',', $vls ) , $key , sprintf ( '$%s', $idx ) ) ; $tab [] = $this -> $key ; db_query_params ( $qwe, $tab ) ; } $nam = $this -> class_name () ; $tag = $prev -> tag () ; return $res ? "$nam $tag :" . ULmk ( $res ) : '' ; } function delete () { $key = $this -> _pkey ; $qwe = sprintf ( 'DELETE FROM %s WHERE %s = %s ; ' , $this -> _tab , $key , '$1' ) ; db_query_params ( $qwe, array ( $this -> $key ) ) ; $nam = $this -> class_name () ; $tag = $this -> tag () ; return "deleted $nam $tag" ; } } ################################################################