<?php

  //include_once "phorum/admin/settings/forums.php";
  include_once "../phorum/admin/settings/forums.php";
  //include_once "../phorum/common.php";

	function mikes_table_head($title, $class, $width="100%") {
		$string =  "<table align=center class=\"PhorumListTable\" width=\"$width\" cellspacing=\"0\" cellpadding=\"2\" border=\"0\">";
		$string .= "<tr><td colspan=10 class=tablehead width=\"100%\">&nbsp;$title</td></tr>";
		$string .= "<tr><td height=\"3\"></td></tr>";
		return $string;
	}
	function mikes_table_foot() {
		return "</table>";
	}
	
	function urltext($str) { 
		//$str = ereg_replace("(([url]www.[/url])([a-zA-Z0-9@:%_.~#-\?&]+[a-zA-Z0-9@:%_~#\?&/]))", "http://\\1", $str); 
		//$str = ereg_replace("(([url]ftp://[/url]|[url]http://[/url]|[url]https://[/url]){2})([a-zA-Z0-9@:%_.~#-\?&]+[a-zA-Z0-9@:%_~#\?&/])", "http://\\3", $str); 
		//$str = ereg_replace("((([url]ftp://[/url]|[url]http://[/url]|[url]https://[/url]){1})[a-zA-Z0-9@:%_.~#-\?&]+[a-zA-Z0-9@:%_~#\?&/])", "<A HREF=\"\\1\" TARGET=\"_blank\">\\1</A>", $str); 
		//$str = ereg_replace("([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})","<A HREF=\"mailto:\\1\">\\1</A>", $str); 

    $str = str_replace('[/URL]', '[/url]', $str);
    $str = str_replace('[URL]', '[url]', $str);
   	$str = ereg_replace('\[url="www.([a-zA-Z0-9@:%_.~#-\?&]+)"\]', '<a href="http://www.\1">', $str);
   	$str = ereg_replace('\[url="([a-zA-Z0-9@:%_.~#-\?&]+)"\]', '<a href="\1">', $str);
   	$str = ereg_replace('\[url\](www.[a-zA-Z0-9@:%_.~#-\?&]+)\[/url\]', '<a href="http://\1">\1</a>', $str);
   	$str = ereg_replace('\[url\]([a-zA-Z0-9@:%_.~#-\?&]+)\[\/url\]', '<a href="\1">\1</a>', $str);
    $str = str_replace('[/url]', '</a>', $str);

		return $str; 
	}
	function myFormat($str) {
		$string2 = str_replace("&", "&amp;", $str);
		$string2 = str_replace("'", "&acute;", $string2);
		$string2 = str_replace("<", "&lt;", $string2);
		$string2 = ereg_replace("(\r\n|\n|\r)", "<br>", $string2);
		
		$string2 = urltext($string2);
		
		// chop up huge words so they don't screw tables ...
		$inTag = 0;
		$string = "";
		$lastSpace = -1;
		for($i=0; $i<strlen($string2); $i++) {
			if($i - $lastSpace > 100 && $inTag == 0) {
				$string = "$string ";
				$lastSpace = $i;
			}
			$c = $string2{$i};
			
			// don't break up href links!
			if($c == '<') {
				$inTag = 1;
				$lastSpace = $i;
			}
			if($inTag > 0 && $c == '>') {
				$inTag = 0;
				$lastSpace = $i;
			}
			
			$string = "$string$c";
			if($c == ' ') $lastSpace = $i;
		}
		return $string;
	}

	// add to db, then close window and reload parent
	if (isset($_POST['name']) && isset($_POST['ok'])) {
	
		$server = mysql_connect($PHORUM['DatabaseServer'], $PHORUM['DatabaseUser'], $PHORUM['DatabasePassword']);
		
		if($server) {
			$connection = mysql_select_db($PHORUM['DatabaseName'], $server); 
			
			/*
				// get biggest id
				$result = mysql_query("SELECT * from COMMENTS");		
				$biggestid = -1;
				while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
		      if($row[0] > $biggestid) { $biggestid = $row[0]; }
				}
			*/
			
			$newid     = $_POST['topicid'];
			$date      = date("F d Y h:i" ,time());
			$name      = myFormat($_POST['name']);
			$email     = myFormat($_POST['email']);
			$comment   = myFormat($_POST['body']);
			$exactdate = time();
			
			if(strlen($name) == 0) $name = "anonymous";
			
			// do insert
			$sql = "INSERT into COMMENTS VALUES ('$newid', '$date', '$name', '$email', '', '$comment', '$exactdate');";
			//echo $sql;
			$result = mysql_query($sql);		
			mysql_close($server);
		}
		
?>
	<script language="javascript">
		orighr = opener.location.href;
		hr  = opener.location.href;
		pos = hr.indexOf('#');
		if(pos == -1) {
			hr = hr + '#comments_<?=$_POST['topicid']?>';
		}
		else {
			hr = hr.substring(0,pos) + '#comments_<?=$_POST['topicid']?>';
		}
		opener.location.href = hr;
		opener.location.reload();
		window.close();
	</script>
<?		
	}
	
	// show form
	else {
		$topicid = -1;
		$topicname = "";
		if (isset($_GET['topicid'])) { $topicid = $_GET['topicid']; }
		if (isset($_GET['topicname'])) { $topicname = $_GET['topicname']; }
?>
<html>
<head>	
	<title>Forgotten History</title>
	<link rel="stylesheet" type="text/css" href="addcomment.css">
</head>
<script language=javascript>
	function checkForm() {
		email = document.myform.email.value;
		if(email.length > 0 && email.indexOf('@') == -1) {
			alert("Please enter either a valid e-mail address or none at all.");
			return false;
		}
		body = document.myform.body.value
		if(body.length > 4000) {
			alert("Your message has exceeded the maximum allowed size. Please shorten it.");
			return false;
		}

		if(body.length == 0) {
			alert("Your comment doesn't contain any text.");
			return false;
		}
		
		newlines = 0;
		for(i=0; i<body.length; i++) {
			ss = body.charAt(i); //.substring(i, 1);
			if(ss == '\n') {
				newlines++;
			}
		}
		if(newlines > 25) {
			alert("Your message has exceeded the maximum allowed number of lines. Please shorten it.");
			return false;
		}
				
		return true;
	}
</script>
<body>
	<form method="POST" name="myform" onSubmit="return checkForm()">
		<?= mikes_table_head("<b>Adding Comment to Topic:</b> $topicname", "") ?>
		 <tr><td><table align="center">
			<tr><td colspan=2 height=3><img src="dot.gif" height=3"></td></tr>
			<tr><td>Name: </td><td><input type="textfield" name="name" size="50"></td></tr>
			<tr><td>E-mail (optional): </td><td><input type="textfield" name="email" size="50"></td></tr>
			<tr><td colspan=2><br><textarea class="PhorumBodyArea" name="body" cols="37" rows="20" wrap="virtual"></textarea></td></tr>
			<tr><td colspan=2 align=center><i>use [url="www.xxx.com"]linktext[/url] or [url]link[/url] to include links</i></td></tr>
			<input type="hidden" name="topicid" value="<?=$topicid?>">
			<tr><td colspan=2 height=3><img src="dot.gif" height=3"></td></tr>
			<tr><td colspan=2 align="center"><input type="submit" name="ok" value="Add Comment">&nbsp;&nbsp;<input type="submit" name="cancel" value="Cancel" onClick="window.close()"></td></tr>
		 </table></td></tr>
		<?= mikes_table_foot() ?>
	</form>
</body>
</html>	
<?	
	}

?>
