\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_head $head_title $meta $aux_head $top_body $menu $motd

$title

$right HEAD; } function validator () { $scheme = Conf ( 'www_scheme' ) ; $uri = rawurlencode ( "$scheme://" . $_SERVER [ 'HTTP_HOST' ] . $_SERVER [ 'REQUEST_URI' ] ) ; return <<valid-html401 VAL; } function html_end () { $addr = Conf ( 'www_contact' ) ; $date = date ( 'r' ) ; $vali = validator () ; $aux_body = Conf ( 'aux_body' ) ; $vers = file_get_contents ( Conf ( 'dir_admin' ) . '/VERSION' ) ; echo <<
repocafeversion $vers

$vali$addr, $date FOOT; } function html_exit ( $msg ) { global $HTML_STARTED ; if ( ! $HTML_STARTED ) { html_start ( 'Error' ) ; } echo "\n

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' . $nl, $t, $c, $x, $t ) ; } else { return sprintf ( '<%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' . $nl, $t, $a, $c, $x, $t ) ; } else { return sprintf ( '<%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 "

$txt
" ; } function DIVa ( $atr) { return "
\n" ; } function DIVz () { return "
\n" ; } function TDA ( $atr, $txt ) { return "" . $txt . "\n" ; } function THA ( $atr, $txt ) { return "" . $txt . "\n" ; } function TD ( $txt ) { return "" . $txt . "\n" ; } function TDr ( $txt ) { return TDA ( 'ALIGN="RIGHT"', $txt ) ; } function TDc ( $txt ) { return TDA ( 'ALIGN="CENTER"', $txt ) ; } function TDx ( $x, $txt ) { return TDA ( "ROWSPAN=$x", $txt ) ; } function TDxc ( $x, $txt ) { return "" . $txt . "\n" ; } function TDxr ( $x, $txt ) { return "" . $txt . "\n" ; } function TDy ( $x, $txt ) { return TDA ( "BGCOLOR=YELLOW", $txt ) ; } function TD2 ( $txt ) { return "" . $txt . "\n" ; } function TD3 ( $txt ) { return "" . $txt . "\n" ; } function TD4 ( $txt ) { return "" . $txt . "\n" ; } function TH ( $txt ) { return "" . $txt . "\n" ; } function THr ( $txt ) { return THA ( 'ALIGN="RIGHT"', $txt ) ; } function THl ( $txt ) { return THA ( 'ALIGN="LEFT"', $txt ) ; } function THx ( $x, $txt ) { return "" . $txt . "\n" ; } function THxr ( $x, $txt ) { return "" . $txt . "\n" ; } function TH2 ( $txt ) { return "" . $txt . "\n" ; } function TH3 ( $txt ) { return "" . $txt . "\n" ; } function TH4 ( $txt ) { return "" . $txt . "\n" ; } function TH5 ( $txt ) { return "" . $txt . "\n" ; } function TR ( $txt ) { return "\n" . $txt . "\n" ; } function DL1 ( $txt ) { return "
\n" . $txt . "
\n" ; } function DT ( $txt ) { return "
\n" . $txt . "
\n" ; } function DD ( $txt ) { return "
\n" . $txt . "
\n" ; } function FORM ( $act, $meth, $txt ) { return "
\n" . $txt . "
\n" ; } function TABA ( $attr, $txt, $cls = '' ) { return "\n$txt
\n" ; } function TAB ( $txt, $cls = '' ) { return "\n$txt
\n" ; } function TAB0 ( $txt, $cls = '' ) { return "\n$txt
\n" ; } function PAR ( $txt ) { return "(" . $txt . ")" ; } function URL ( $ref, $txt ) { return sprintf ( '%s', $ref, $txt ) ; } function NAM ( $tag, $txt ) { return sprintf ( '%s', $tag, $txt ) ; } function IMGA ( $ref, $alt, $atr ) { return sprintf ( '%s', $ref, $alt, $atr ) ; } function IMG ( $ref, $alt ) { return IMGA ( $ref, $alt, '' ) ; } function BLD ( $x ) { return sprintf ( '%s', $x ) ; } function BIG ( $x ) { return sprintf ( '%s', $x ) ; } function ITA ( $x ) { return sprintf ( '%s', $x ) ; } function TT ( $x ) { return sprintf ( '%s', $x ) ; } function SM ( $x ) { return sprintf ( '%s', $x ) ; } function COL ( $c, $x ) { return sprintf ( '%s', $c, $x ) ; } function RED ( $x ) { return COL ( 'RED', $x ) ; } function YEL ( $x ) { return COL ( 'YELLOW', $x ) ; } function GRN ( $x ) { return COL ( 'GREEN', $x ) ; } function BLU ( $x ) { return COL ( '#00bbff',$x ) ; } function SIL ( $x ) { return COL ( 'SILVER',$x ) ; } function AQU ( $x ) { return COL ( 'AQUA',$x ) ; } function LIM ( $x ) { return COL ( 'LIME',$x ) ; } function GRY ( $x ) { return COL ( 'GRAY',$x ) ; } function FUC ( $x ) { return COL ( 'FUCHSIA',$x ) ; } function WHI ( $x ) { return COL ( 'WHITE',$x ) ; } function ORA ( $x ) { return COL ( 'ORANGE',$x ) ; } function NAV ( $x ) { return COL ( 'NAVY',$x ) ; } function BG_COL ( $col ) { return "BGCOLOR=\"$col\"" ; } function BG_BLU () { return BG_COL ( '#00bbff') ; } function BG_SIL () { return BG_COL ( 'SILVER') ; } function BG_GRY () { return BG_COL ( 'GRAY') ; } function BG_FUC () { return BG_COL ( 'FUCHSIA') ; } function BG_YEL () { return BG_COL ( 'YELLOW') ; } function BGI ( $x ) { return BLD(GRN(ITA($x))) ; } function BRI ( $x ) { return BLD(RED(ITA($x))) ; } function BRB ( $x ) { return BIG(RED(BLD($x))) ; } function BGB ( $x ) { return BIG(BLU(BLD($x))) ; } function PRE ( $x ) { return sprintf ( "
%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" ; } function LI ( $txt ) { return "
  • " . $txt . "
  • \n" ; } function LIc ( $c, $x ) { return ( $c ? LI ( $x ) : '' ) ; } function LI2 ( $c, $x, $y ) { return LI ( $c ? $x : $y ) ; } function LIcc ( $x ) { return LIc ( $x, $x ) ; } function ULc ( $c, $x ) { return ( $c ? UL ( $x ) : '' ) ; } function ULcc ( $x ) { return ULc ( $x, $x ) ; } function LImk ( $x ) { $res = '' ; if ( is_array ( $x ) and count ( x ) ) { $res = LIcc ( ULmk ( $x ) ) ; } elseif ( $x ) { $res = LIcc ( $x ) ; } return $res ; } function ULmk ( $x ) { $res = '' ; if ( is_array ( $x ) and count ( $x ) ) { $res = ULcc ( implode ( '', array_map ( 'LImk', $x ) ) ) ; } elseif ( ! is_array ( $x ) and $x ) { $res = UL ( LI ( $x ) ) ; } return $res ; } function ULor ( $x, $def ) { $res = '' ; if ( $x ) { $res = ULmk ( $x ) ; } if ( ! $res ) { $res = ULmk ( $def ) ; } return $res ; } function htmlrep ( $x ) { $tp = gettype ( $x ) ; switch ( $tp ) { case 'boolean' : $res = $x ? 'yes' : 'no' ; break; case 'string' : $res = strlen($x) ? $x : ' ' ; break; case 'integer' : $res = $x ; break; case 'NULL' : $res = ' ' ; break ; default : html_exit ( "can't htmlrep '$x' [$tp]" ) ; } return $res ; } function db_get ( $qwe ) { global $conn ; $res = array () ; DUMP ( 2, nl2br ( $qwe ) ) ; $sel = pg_Exec ($conn, $qwe) ; if ( ! $sel ) { html_exit ( "can't '$qwe'

    \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" ; } } ################################################################