<?php
include( 'menu.inc' );

$logout = empty( $REMOTE_USER );
if( !$logout ){
	$logout = TRUE;
	$m = explode( '#', $_admin );
	$qry = "select memb_id from member where email='$REMOTE_USER'";
	if(( $id = mysql_query( $qry )) && ( $row = mysql_fetch_row( $id ))){
		$k = count( $m );
		for( $i = 0; ( $i < $k ) && ( $m[$i] != $row[0] ); $i++ );
		$logout = $i == $k;
	}
}
if( $logout || ( $action == 'logout' )){
	header( "WWW-Authenticate: Basic realm='opentheory'" );
	header( "HTTP/1.0 401 Unauthorized" );
	echo "Not authenticated. Close all browser tasks and try again.";
	exit;
}

if( !empty( $sect )){
	$txt = array( implode( '|', $txt ));
	while( list( $k, $tx ) = each( $txt )) {
		list( $k, $m ) = each( $mid );
		$qry = "update msg set txt = '$tx' where msg_id = $m";
		if( !mysql_query( $qry )) {
			echo "Error No. 10: ".mysql_errno().": ".mysql_error();
			exit;
		}
	}
}

if( !empty( $sect_id )){
	if( !$id = mysql_query( "select text from msg_sect
		where sect_id = $sect_id" )) {
		echo "Error No. 11: ".mysql_errno().": ".mysql_error();
		exit;
	}
	if( $row = mysql_fetch_array( $id )){
		$text = $row[ 'text' ];
	}
}

$t = getmsg( 9, $lang );
if( empty( $text )) $text = $t[1];
htmlhead( $text, '' );
menu( '', 0 );
echo "<br><font $_fonthdln>\n";
echo "<center><b>$text</b></center></font>\n<br>";

if( empty( $sect_id )){
	$x = 'n.a.';
	if( $row = mysql_fetch_row( mysql_query( "select version()" ))){ 
		$x1 = "$row[0] / ".phpversion();
	}
	if( $row = mysql_fetch_row( mysql_query( "select count(*) from member" ))){ 
		$x2 = $row[0];
	}
	if( $row = mysql_fetch_row( mysql_query( "select count(*) from metatext where status='act'" ))){ 
		$x3 = $row[0];
	}
	echo "<table width=100% bgcolor='$_bg3'>\n";
	echo "<tr><td colspan=3 bgcolor='$_bg5'>\n";
	echo "<font $_fontbulk><b>$t[4]</b></font></td></tr>\n";
	echo "<tr><td width=150 bgcolor='$_bg2'><font $_fontbulk>$t[5]</font></td>\n";
	echo "<td colspan=2><font $_fontbulk>$x1</font></td></tr>\n";
	echo "<tr><td bgcolor='$_bg2'><font $_fontbulk>$t[6]</font></td>\n";
	echo "<td colspan=2><font $_fontbulk>$x2</font></td></tr>\n";
	echo "<tr><td bgcolor='$_bg2'><font $_fontbulk>$t[7]</font></td>\n";
	echo "<td colspan=2><font $_fontbulk>$x3</font></td></tr>\n";
	echo "<tr><td colspan=3 bgcolor='$_bg5'>\n";
	echo "<font $_fontbulk><b>$t[8]</b></font></td></tr>\n";
	echo "<tr><form action='$PHP_SELF' method='post'>\n";
	echo "<td bgcolor='$_bg2'><font $_fontbulk>$t[2]:</font></td>\n";
	echo "<td width=200><font $_fontbulk>\n";
	echo "<select name='sect_id' size=1>'>\n";
	if( !$id = mysql_query( "select sect_id, text from msg_sect 
		order by lang, text" )) {
		echo "Error No. 12: ".mysql_errno().": ".mysql_error();
		exit;
	}
	while( $row = mysql_fetch_array( $id )){
		$sect_id = $row[ 'sect_id' ];
		$text = $row[ 'text' ];
		echo "<option value='$sect_id'>$text\n";
	}
	echo "</select></td>\n<td width=400><font $_fontbulk>";
	echo "<input type='hidden' name='xid' value='$xid'>\n";
	echo "<input type='submit' value='$t[3]'>";
	echo "</font></td></tr></form>\n";
	echo "</table>\n";
	echo "<font $_fontbulk><center>\n";
	echo "<form action='$PHP_SELF' method='post'>\n";
	echo "<input type='hidden' name='action' value='logout'>\n";
	echo "<input type='hidden' name='xid' value='$xid'>\n";
	echo "<input type='submit' value='$t[11]'>\n";
	echo "</form></font>\n";
	foot();
	exit;
}
if( !$id = mysql_query( "select m.msg_id, m.sect, m.txt from msg m, msg_sect s
	where s.sect = m.sect and s.lang = m.lang and s.sect_id = $sect_id" )) {
	echo "Error No. 13: ".mysql_errno().": ".mysql_error();
	exit;
}
$txt = '';
while( $row = mysql_fetch_array( $id )){
	$txt .= '|'.$row[ 'txt' ];
	$mid[] = $row[ 'msg_id' ];
	$sect = $row[ 'sect' ];
}
$txt = explode( '|', substr( $txt, 1 ));
echo "<font $_fontbulk><center>\n";
echo "<form action='$PHP_SELF' method='post'>\n";
echo "<table bgcolor='$_bg3'>\n";
echo "<tr bgcolor='$_bg5'><td width=20><font $_fontbulk>";
echo "$t[9]</font></td>\n<td><font $_fontbulk>$t[10]</font></td></tr>\n";
$i = 0;
while( list( $k, $tx ) = each( $txt )) {
	echo "<tr><td bgcolor='$_bg2'><font $_fontbulk>$i";
	echo "</font></td>\n<td><font $_fontinpt>";
	if( list( $k, $m ) = each( $mid )){
		echo "<input type='hidden' name='mid[]' value='$m'>\n";
	}
#	$t = eregi_replace( '"', '&quot;', $t );
	$tx = htmlspecialchars( $tx );
	echo "<input type='text' name='txt[]' size=70 value=\"$tx\">\n";
	echo "</font></td></tr>\n";
	$i++;
}
echo "</table>\n<input type='hidden' name='sect' value='$sect'>\n";
echo "<input type='hidden' name='xid' value='$xid'>\n";
echo "<input type='submit' value='$t[12]'>";
echo "</form></center></font>\n";
foot();
?>
