<?php
include( "menu.inc" );
include( "func.inc" );

# open and select local database
if( empty( $xid ) || !read_session( $xid, $xpar )){
	echo "<head>\n<meta http-equiv='refresh' content='0; ";
	echo "URL=http://$fqdomain/?logout=1'\n</head>";
#	if( !head( $_x[16], '', $xid, $xpar )) exit;
#	echo "<font $_fontbulk>\n<p>$_x[17]</p></font>\n<br><br>";
#	foot();
	exit;
}
$t = getmsg( 18, $lang );
$mid = $xpar[ 'mid' ];
$e = $xpar[ 'xem' ];
$fn = $xpar[ 'xfn' ];
$ln = $xpar[ 'xln' ];
$m = 0;
$back = "<a href='setupform.phtml?xid=$xid";
$back .= '&ti='.urlencode( $ti );
$back .= '&ta='.urlencode( $ta );
$back .= '&kw='.urlencode( $kw );
$back .= '&ds='.urlencode( $ds );
$back .= "'><b>$t[4]</b></a>";

# check syntax of titlabb (project short title = pathname and url-part)
if( eregi( "[][\"äöüß°µ!§$%&/()=?{|}\´'*+~;,@# ^]", $ta ) || ( $ta == 'ot' ) || ( $ta == 'images' )){
	if( !head( $t[3], '', $xid, $xpar )) exit;
	echo "<font $_fontbulk>\n";
	if( $ta != 'ot' ){
		eval( "\$x = \"$t[5]\";" );
		echo "<p>$x</p>\n";
	} else {
		eval( "\$x = \"$t[6]\";" );
		echo "<p>$x</p>\n";
	}
	echo "<p>$back</p></font>\n";
	foot();
	exit;
}

# check whether all variables are initialized
#if( !empty( $fn ) and !empty( $ln ) and !empty( $e ) and !empty( $wd )
if( !empty( $ti ) and !empty( $ta ) and !empty( $ds )){ 

	$fn = htmlentities( $fn );
	$ln = htmlentities( $ln );
	$ti = htmlentities( $ti );
	$ta = htmlentities( $ta );
	$ds = htmlentities( $ds );

	# check by memb_id and titlabb whether a metatext already exists
	if( !$id = mysql_query( "select memb_id, titlabb, title, descr
		from metatext where lcase( titlabb ) = '".strtolower( $ta )."'" )) {
		echo "Error No. 5: ".mysql_errno().": ".mysql_error();
		exit;
	}

	# if metatext does not exist insert a new metatext record
	if( !$row = mysql_fetch_array( $id )) {

		# select max proj_id
		if( !$id = mysql_query( "select max(proj_id) from metatext" )) {
			echo "Error No. 6: ".mysql_errno().": ".mysql_error();
			exit;
		}
		if( !$row = mysql_fetch_row( $id )) {
			$pid = 1;
		}
		else {
			$pid = $row[ 0 ] + 1;
		}

		# insert a new metatext record
		if( !mysql_query( "insert into metatext values( 
			$pid, 1, $mid, '$ti', '$ta', now(), 'ini', 'de', 0, 
			'".mlst( $ta )."' , null, '$kw', 1, '$ds' )" )){
			echo "Error No. 7: ".mysql_errno().": ".mysql_error();
			exit;
		}

		# insert first member of the new project (the maintainer)
		if( !mysql_query( "insert into projmemb values( 
			$pid, $mid, 1 )" )){
			echo "Error No. 8: ".mysql_errno().": ".mysql_error();
			exit;
		}

		# create a new working directory for the project
		mkdir( strtolower( $ta ), 0755 );

		# create the index file of the project
		$filename = strtolower($ta)."/index.phtml";
		$str = "<?php\ninclude(\"indx.inc\");\nindexfile($pid);\n?>";
		# chmod( strtolower($ta), 757);
		$fp = fopen( $filename, "w" );
		fputs( $fp, $str );
		fclose( $fp );
		# chmod( strtolower($ta), 755);
		$new_pj = 1;
	}

	# metatext already exists = project already exists
	else {
		$m = $row[ 'memb_id' ];
		$ta = $row[ 'titlabb' ];
		$ti = $row[ 'title' ];
		$ds = $row[ 'descr' ];
		if( !$id = mysql_query( "select first_n, last_n, email
			from member where memb_id = $m" )) {
			echo "Error No. 9: ".mysql_errno().": ".mysql_error();
			exit;
		}
		if( $row = mysql_fetch_array( $id )) {
			$fn = $row[ 'first_n' ];
			$ln = $row[ 'last_n' ];
			$e = $row[ 'email' ];
		}
		$new_pj = 0;
	}

	#################
	# display texts # 
	#################

	# submitted project is a new one
	if( $new_pj ){
		if( !head( $t[0], '', $xid, $xpar )) exit;
		echo "<font $_fontbulk>\n";
		echo "<p>$t[7]:\n";
	}

	# submitted project already exists
	else {
		if( !head( $t[1], '', $xid, $xpar )) exit;
		echo "<font $_fontbulk>\n";
		echo "<p>$t[8]:\n";
	}

	echo "<ul>\n<li><b>$t[9]:</b> $fn $ln </li>\n";
	echo "<li><b>$t[10]:</b> $e</li>\n";
	echo "<li><b>$t[11]:</b> $ti</li>\n";
	echo "<li><b>$t[12]:</b> $ta</li>\n";
	echo "<li><b>$t[13]:</b> $kw</li>\n";
	echo "<li><b>$t[14]:</b> $ds</li></ul></p>";
	if( $mid == $m ) echo "<p>".add_xid( $t[15] )."</p>\n<p>$back</p>\n";

	# a new project
	if( $new_pj ){
		echo "$t[16]\n";
		echo "<center><form action='modify.phtml?xid=$xid' method='POST'>\n";
		echo "<input type=submit value='$t[17]'>";
		echo "<input type=hidden name='ver' value='1'>";
		echo "<input type=hidden name='pid' value='$pid'>";
		echo "<input type=hidden name='mode' value='text_new'>";
		echo "</form></center>\n";

	}
	echo "</font>\n";

# not all variables are initialized
} else {
	if( !head( $t[3], '', $xid, $xpar )) exit;
	echo "<font $_fontbulk>\n";
	echo "<p>$t[18]:\n";
	echo "<ul>\n<li><b>$t[19]:</b> "; 
	echo "$fn <font color='$_fg3'>- $t[21].</font></li>\n";
	echo "<li><b>$t[20]:</b> "; 
	echo "$ln <font color='$_fg3'>- $t[21].</font></li>\n";
	echo "<li><b>$t[10]:</b> ";
	echo "$e <font color='$_fg3'>- $t[21].</font></li>\n";
	echo "<li><b>$t[11]:</b> ";
	echo "$ti <font color='$_fg3'>";
	if( !empty( $ti )) echo " - $t[21]."; else echo "- <b>$t[22]!</b>";
	echo "</font></li>\n";
	echo "<li><b>$t[12]:</b> "; 
	echo "$ta <font color='$_fg3'>";
	if( empty( $ta )){
		echo " - <b>$t[22]!</b>";
	} else {
		echo " - $t[21].";
	}
	echo "</font></li>\n";
	echo "<li><b>$t[13]:</b> ";
	echo "$kw <font color='$_fg3'>";
	if( !empty( $kw )) echo "- $t[21]"; else echo "- $t[23]";
	echo " ($t[24])</font></li>\n";
	echo "<li><b>$t[14]:</b> ";
	echo "$ds <font color='$_fg3'>";
	if( !empty( $ds )) echo "- $t[21]."; else echo "- <b>$t[22]!</b>";
	echo "</font></li></ul></p>\n<p>$back</p></font>\n";
}
foot()
?>
