<?php
if (MING != "merciless") require ('index.php');
else {
/*
TODO: give the comment module a mailing feature, so it doubles as a formmail
*/
$comment_table_prefix = 'comment_';

$comment_theme_top = '';


$comment_anchor_format = '<a name="%title%%unix_date%"></a>';
$comment_empty_message = '<em>No comments have yet been added.</em>';

$comment_theme_format = '
<strong>%title%</strong><br />
<p>%contents%</p>
<p><i><small>Posted by %author% &nbsp; %email_obfuscated%, %posted_on%</small></i></p>
<hr />
';

$comment_size_limit = 100;

$comment_form_template = '
<div align="center">
<form name="%form_name%" enctype="multipart/form-data" method="post"  action="#commentconfirmationmessage" >
<input type="hidden" name="destination" value="%form_name%" />
<input type="hidden" name="comment_sub" value="yes" />
Name:  <input type="text" name="author" size="25" value="%user_name%"  /><br /> 
Email:   <input type="text" name="email" size="25" /><br />
Subject: <input type="text" name="title" size="50" /> <br />
<!--[[ captcha : invisible ]]-->
<textarea name="mesg" rows="2" cols="60"></textarea><br />
<input type="submit" value="Post comment" />
</form>
</div>
';


$comment_shoutbox_form_template = '
<form name="%form_name%" enctype="multipart/form-data" method="post"  action="#" >
<input type="hidden" name="destination" value="%form_name%" />
<input type="hidden" name="comment_sub" value="yes" />
<input type="text" name="author" size="10" value="%user_name%"  />
<input type="text" name="mesg" size="20" />
<input type="submit" value="Shout!" />
</div>
';


/*
$url = URL_REQUEST;
if (!URL_HAS_GET) $url .= '?'; else $url .='&';

$url = $url . 'bogus=foobar#%title%%unix_date%';*/
$comment_redirect_template = '
<SCRIPT LANGUAGE="JavaScript">
<!--

url="%url%"
setTimeout("location.href=url",3000)

// -->
</script>
';

$comment_confirmation_message = '
<a name="commentconfirmationmessage" />
<div align="center"><strong>Your comment has been added! If JavaScript is enabled, you will be redirected to your new comment shortly. Otherwise, click <a href="%url%">here</a> to see your comment.</strong></div>
';

// unset ($url);

$comment_shoutbox_prefix = 'sb';
$comment_shoutbox_default_power = POWER_TXT;
$comment_default_power = POWER_BB;
/*

I should add shoutbox functionality also eventually...

*/

function comment_shoutbox_doaction_unfinished ( $name = 'shoutbox' )
{
	
	global $comment_shoutbox_form_template, $comment_shoutbox_prefix, $comment_shoutbox_default_power, $db;
	main_set('comment_form_template', $comment_shoutbox_form_template);
	if ($db->isNameValid($name) === FALSE) $name = 'shoutbox';
	$name = $comment_shoutbox_prefix . $name;
	comment_form_doaction($name);
	comment_list_doaction($name);
}

// Just a simple captcha wrapper, to keep the captcha only activating on a page submit
function comment_captcha_catch_doaction() {
	if($_POST['comment_sub'] == 'yes') captcha_catch_doaction();
}

function comment_main_doaction ( $name )
{
	global $db;
	if (trim($name) == '' || $db->isNameValid($name) === FALSE )
	{
		outblurt('Error [comment_1]: Invalid comment/guestlist specified: ' . $name);
	}
	else
	{
		comment_show_doaction ($name);
		comment_form_doaction ($name);
		comment_captcha_catch_doaction();
		comment_commit_doaction ($name);
	}
}


function comment_outblurt_form ($name )
{
	global $comment_form_template, $user;
	$username = $user === FALSE ? '' : $user->get('real_name');
	$text = main_replace ($comment_form_template, Array( '%form_name%' => $name, '%user_name%' => $username));
	outblurt($text, POWER_DEFAULT, TRUE);
}

function comment_form_doaction($name) {
	global $db;
	if (strlen(trim($name)) < 1 || $db->isNameValid($name) === FALSE )
		outblurt('Error [comment_4]: Invalid comment/guestlist specified: ' . $name);
	else comment_outblurt_form( $name );
}

function comment_commit_doaction ($name, $skip_confirmation_message = FALSE ) { 
	global $db;
	if (trim($name) == '' || $db->isNameValid($name) === FALSE ) {
		outblurt('Error [comment_3]: Invalid comment/guestlist specified: ' . $name);
	} else if($_POST['comment_sub'] == 'yes') {
		global $comment_table_prefix, $comment_default_power, $user;
		// The user is submitting a comment
		$author = $_POST['author'];
		$email =  $_POST['email'];
		$title =  $_POST['title'];
		$contents = $_POST['mesg'];
		$unix_date = time();
		if ( strlen(trim($author)) < 1 ||  strlen(trim($email)) < 1 
			||  strlen(trim($title)) < 1  || strlen(trim($contents)) < 1  ) { // The fields aren't properly filled out.
			global $theme_use_javascript_for_back;
			if ($theme_use_javascript_for_back === FALSE) {
				global $content_request;
				$back_link = 'Click on your browser\'s "back" button and try again.';
			}
			else $back_link = 'Go <a href="javascript:history.go(-1)">back</a> and try again.';				
			outblurt ('<strong>Uh oh! You didn\'t properly fill out all of the fields! ' . $back_link . '</strong>');
			return FALSE;
		}
		$entry = new Entry('comment');
		$destination = $_POST['destination'];
		$entry->set('author', $_POST['author'] );
		$entry->set('email', $_POST['email']);
		$entry->set('title', $_POST['title']);
		$entry->set('contents', $_POST['mesg']);
		$entry->set('unixdate', $unix_date . '');
		$entry->set('ip', $user->ip);
		
		if ($comment_default_power == POWER_BB) $entry->set('type', 'bb');
		else if ($comment_default_power == POWER_TXT) $entry->set('type', 'txt');
		else $entry->set('type', 'bb');
		
		$db->selectTable($comment_table_prefix . $name);
		$db->add($entry);
		$db->applyChanges();
		if ($skip_confirmation_message === FALSE) {
			global $comment_redirect_template, $comment_confirmation_message;
			$replace_array = Array (
			'%title%' => urlencode($title),
			'%unix_date%' => urlencode($unix_date)
			);
			$url = URL_PREPARED;
			$url = $url . 'bogus=foobar#%title%%unix_date%';
			$redirect = str_replace('%url%', $url, $comment_redirect_template);
			$confirmation = str_replace('%url%', $url, $comment_confirmation_message);
			$redirect = main_replace($redirect, $replace_array);
			$confirmation = main_replace($confirmation, $replace_array);
			outblurt($redirect, POWER_DEFAULT);
			outblurt($confirmation, POWER_DEFAULT);
		}
	}
}

function comment_show_doaction ( $name )
{
	global $db;
	if (trim($name) == '' || $db->isNameValid($name) === FALSE )
	{
		outblurt('Error [comment_2]: Invalid comment/guestlist specified: ' . $name);
		return FALSE;
	}
	else
	{
		global $comment_table_prefix, $comment_theme_format, $comment_theme_top;
		$name = $comment_table_prefix . $name;
		if ($db->selectTable($name) === FALSE) $db->makeTable($name); // selects the table if the table exists, otherwise it makes the table.
		$entry_list = $db->getAll(); // get all of the entry list
		// $ordered_comment_list = Array();
		$exploded_format = explode('%contents%', $comment_theme_format, 2);
		outblurt($comment_theme_top, POWER_DEFAULT);
		if ($entry_list === FALSE)
		{
			global $comment_empty_message;
			outblurt($comment_empty_message, POWER_HTML);
			return FALSE;
		}
		else
		{
			global $comment_size_limit;
			$i = 0;
			foreach($entry_list as $entry)
			{
				if ($i < $comment_size_limit)
				{
					$title = $entry->get('title');
					$author = $entry->get('author');
					$contents = $entry->get('contents');
					$unix_date = trim($entry->get('unixdate'));
					$email = $entry->get('email');
					$strtype = $entry->getAttribute('type');
					
					if (ctype_digit($unix_date) === TRUE) {
						$date = intval($unix_date);
						$posted_on = theme_generate_date_line ($unix_date);
					}
					else $posted_on = '';
					
					if (main_extension_to_int($strtype) === TRUE)
					{
						$type = main_extension_to_int($strtype);
					}
					else
					{
						global $comment_default_power;
						$type = $comment_default_power;
					}
					
					
					$email_obfuscated = main_obfuscate_email ($email);
					$replace_array = Array (
						'%title%' => $title,
						'%author%' => $author,
						'%email%' => $email,
						'%email_obfuscated%' => $email_obfuscated, 
						'%posted_on%' => $posted_on,
						'%unix_date%' => $unix_date
					);
					
					$anchor_replace = Array (
						'%title%' => urlencode($title),
						'%unix_date%' => urlencode($unix_date)				
					);
					global $comment_anchor_format;
					$first = main_replace($exploded_format[0], $replace_array);
					$middle = $contents;
					$last = main_replace($exploded_format[1], $replace_array);
					
					$anchor = main_replace($comment_anchor_format, $anchor_replace);
					
					outblurt ($anchor, POWER_DEFAULT);
					outblurt ($first, POWER_DEFAULT );
					outblurt ($middle, $type);
					outblurt ($last, POWER_DEFAULT );
					$i++;
				}
				else
				{
					return TRUE;
				}
				
			}
			
			return TRUE;
		}
	}
}



} // for the bad request checker at the top of this file.
?>
