<?php

//####################################################
// Copyright (c) 2003 by netblazer tomicki(at)o2.pl
// See more of my work at http://tomicki.tk
// ###################################################

//############################################
// EXPANDABLE FUNCTIONS ( a form of macros ;)
//############################################

function ExpandForgetPassword()
{
	?>
	Je¶li chcesz odzyskać hasło podaj login.
	<form ACTION="<?php echo $php_self;?>?get_passwd=1" method="post">
	<input type="text" name="login">
	<input TYPE="submit" name="submit" value="Odzyskaj">
	</form>
	Hasło zostanie ci wysłane emailem. Je¶li przy rejestracji konta nie podałe¶ prawidłowego adresu email to masz przer±bane. Pozostaje ci napisać <a HREF="mailto:tomicki@o2.pl?subject=Zapomniałem Hasła. Jestem Downem">do mnie z pro¶b± o zmianę hasła</a>.
	<?php
}

$indent = 10;

function ShowPostTree($data)
{
	global $php_self;
	global $user;
	global $_OPTION;
	
	$size = count($data);
	
	global $indent;
	//echo "<span class=\"list\">";
	
	for ($i = 0; $i < $size; ++$i)
	{
		$post_banned = 0;
		
		if ($data[$i]['options'] & $_OPTION['banned'])
		{
			if (!$user->Check($_OPTION['admin']))
				continue;
			
			else
				$post_banned = 1;
		}
			
		if ($data[$i]['more'])
		{
			$indent += 10;
			ShowPostTree($data[$i]);
			$indent -= 10;
		}
		else if ($data[$i])
		{
			echo "<span class=\"list\" style=\"padding-left: ",$indent,"px\">";
			$userdata = getUserData($data[$i]['author'], "id,name");
			echo "<A HREF=\"#",$data[$i]['id'],"\">";
			
			
			echo "<span class=\"ptime", isPostNew($data[$i]['posted']) ,"\">";
	
			if ($post_banned)
				echo "<span class=\"red\">";
				
			echo $data[$i]['title'];
			
			if ($post_banned)
				echo "</span>";
				
			echo "</span>";
				
			echo "</A> - by <A HREF=\"$php_self?user_profile=",$userdata['id'],"\">", $userdata['name'],"</A><br>";
		echo "</span>";
		}
	}
}

function ShowPostBodies($data)
{
	global $php_self;
	global $user;
	global $_OPTION;
	
	$size = count($data);

	echo "<table border=0 cellpadding=0 cellspacing=0 width=100%>";
	for ($i = 0; $i < $size; ++$i)
	{
		$post_banned = 0;
		if ($data[$i]['options'] & $_OPTION['banned'])
		{
			if (!$user->Check($_OPTION['admin']))
				continue;
			
			else
				$post_banned = 1;
		}
		
		if ($data[$i]['more'])
		{
			echo "<tr><td bgcolor=\"#EEEEEE\" style=\"padding-left:15px\">";
			echo "<br>";
			ShowPostBodies($data[$i]);
			echo "</td></tr>";
		}
		else if ($data[$i])
		{
			$time = $data[$i]['posted'];
			$userdata = getUserData($data[$i]['author'], "id,name");
			echo "<tr><td bgcolor=\"#CCCCCC\" style='padding-bottom:8px;padding-top:8px;padding-right:8px;padding-left:8px;font-size: 8pt;' CLASS=\"body-title\">";
			echo "<A HREF=\"$php_self?post=",$data[$i]['id'],"&show_forum=",$_COOKIE['show_forum'],"\"><b>";
			
			echo "<span class=\"ptime", isPostNew($data[$i]['posted']) ,"\">";
				
			if ($post_banned)
				echo "<span class=\"red\">";
				
			echo $data[$i]['title'];
			
			if ($post_banned)
				echo "</span>";
				
			echo "</span>";
				
			echo "</b></A><A NAME=\"",$data[$i]['id'],"\"></A> - posted on $time by <A HREF=\"$php_self?user_profile=",$userdata['id'],"\">", $userdata['name'],"</A><br>";
			
			echo "</td></tr>";
			echo "<tr><td bgcolor=\"#EEEEEE\"style='padding-bottom:8px;padding-top:8px;padding-right:8px;padding-left:8px' CLASS=\"body\">";
			echo $data[$i]['body'], "<br><br>";
			
			echo "[ <A HREF=\"#top\">Top</A> ]&nbsp;&nbsp;&nbsp;";

			if ($user->Check($_OPTION['posting_allowed']))
				echo "[ <A HREF=\"$php_self?reply=",$data[$i]['id'],"&show_forum=",$_COOKIE['show_forum'],"\">Reply</A> ]&nbsp;&nbsp;&nbsp;";
			
			if ($user->Check($_OPTION['admin']))
			{
				echo "[ <A HREF=\"$php_self?moderate=",$data[$i]['id'],"&show_forum=",$_COOKIE['show_forum'],"\">Moderate</A> ]&nbsp;&nbsp;&nbsp;";
				
				if (!($data[$i]['options'] & $_OPTION['banned']))
					echo "[ <A HREF=\"$php_self?bann=",$data[$i]['id'],"&show_forum=",$_COOKIE['show_forum'],"\">Bann</A> ]&nbsp;&nbsp;&nbsp;";
				else 
					echo "[ <A HREF=\"$php_self?unbann=",$data[$i]['id'],"&show_forum=",$_COOKIE['show_forum'],"\">UnBann</A> ]&nbsp;&nbsp;&nbsp;";
				
				echo "[ <A HREF=\"$php_self?delete=",$data[$i]['id'],"&show_forum=",$_COOKIE['show_forum'],"\">Delete</A> ]&nbsp;&nbsp;&nbsp;";
			}
				echo "</td></tr>";
		}
	}
	echo "</table>";
}

function ShowUserProfile($data, $long = 1)
{
	global $php_self;
	global $user;
	global $_OPTION;
	
	$userdata = new UserOptions();
	
	$userdata->SetTo($data['options']);
	
	echo "<span class=\"main-heading\">",$data['name'],"</span> profile<br><br>";
	
	if ($userdata->Check($_OPTION['public_email']) || $user->Check($_OPTION['admin']))
		echo "<b>email:</b> <A HREF=\"mailto:", $data['email'] ,"\">", $data['email'],"</A><br>";

	if ($data['www'])
	{
		echo "<b>www:</b> <A HREF=\"";
		if (strpos($data['www'], "http://") === false)
			echo "http://";
		
		echo $data['www'];
		
		echo "\">", $data['www'], "</A><BR>";
	}
	
	$posts_perday = postPerDay($data['created'], $data['posts']);
	$topics_perday = postPerDay($data['created'], $data['topics_started']);
	$percent_posts = getPercentData($data['posts'], $data['topics_started']);
	
	echo "<b>posting statistics:</b> ";
	echo $data['posts']," posts so far (",$percent_posts['posts'],"% of all posts - ",$posts_perday," per day) - started ",$data['topics_started']," ";
	echo $data['topics_started'] == 1 ? "topic" : "topics";
	echo " (",$percent_posts['topics'],"% of all topics - ",$topics_perday," per day)<BR><br>";	
	
	if ($userdata->Check($_OPTION['account_disabled']))
		echo "Account disabled<br>";
		
	if (!$userdata->Check($_OPTION['posting_allowed']))
		echo "Account banned<br>";
	
	if ($long)
		echo "<br>";
		
	if ($data['comment'] && $long)
		echo $data['comment'];
	
		
	if ($long) 
	{
		echo "<br>";
		echo "<br>";
	}
		
		if ($user->Check($_OPTION['admin']))
		{
			if ($userdata->Check($_OPTION['posting_allowed']))
				echo "<A HREF=\"$php_self?bann_account=",$data['id'],"\">[ Bann Account ]</A>";
				
			else 	
				echo "<A HREF=\"$php_self?unbann_account=",$data['id'],"\">[ UnBann Account ]</A>";
				
			echo "&nbsp;&nbsp;&nbsp;";
			
			if (!$userdata->Check($_OPTION['account_disabled']))
				echo "<A HREF=\"$php_self?disable_account=",$data['id'],"\">[ Disable Account ]</A>";
			
			else
				echo "<A HREF=\"$php_self?enable_account=",$data['id'],"\">[ Enable Account ]</A>";
				
			echo "&nbsp;&nbsp;&nbsp;";
			echo "<A HREF=\"$php_self?delete_account=",$data['id'],"\">[ Delete Account ]</A>";
			echo "&nbsp;&nbsp;&nbsp;";
			echo "<A HREF=\"$php_self?bann_posts=",$data['id'],"\">[ Bann All Posts ]</A>";
			echo "&nbsp;&nbsp;&nbsp;";
			echo "<A HREF=\"$php_self?unbann_posts=",$data['id'],"\">[ UnBann All Posts ]</A>";
			echo "&nbsp;&nbsp;&nbsp;";
			echo "<A HREF=\"$php_self?delete_posts=",$data['id'],"\">[ Delete All Posts ]</A>";
		}
	
	echo "<br><br>";
}

function ShowTopics($data, $page)
{
	global $php_self;
	global $user;
	global $_OPTION;
	
	echo "<TABLE BORDER=\"0\" class=\"main\" cellpadding=\"5\">";
	echo "<TR>";
	echo "<TD><B>Topic name</B></TD>";
	echo "<TD><B>Started</B></TD>";
	echo "<TD><B>Replies</B></TD>";
	echo "<TD><B>Last Reply</B></TD>";
	echo "</TR>";

	$topics_per_page = 12;
	$result_start = $page * $topics_per_page;
	$result_number = ($page + 1) * $topics_per_page;
	
	$result_size = mysql_num_rows($data);
	$result_pages = 0;
	do 
	{
		++$result_pages;
		$result_size -= $topics_per_page;
	} while ($result_size > 0);
		
	for ($i = 0; $row = mysql_fetch_array($data); ++$i)
	{
		if ($result_start > $i)
			continue;
		
		if ($result_number == $i)
			break;
			
		$post_banned = 0;
		
		if ($row['options'] & $_OPTION['banned'])
		{
			if (!$user->Check($_OPTION['admin']))
				continue;
			
			else
				$post_banned = 1;
		}
		
	echo "<tr>";
	
	echo "<td>";
		$time = $row['posted'];
		echo "<A HREF=\"$php_self?post=",$row['id'],"&show_forum=",$_COOKIE['show_forum'],"\">";
		
		echo "<span class=\"ptime", isPostNew($row['posted']) ,"\">";
			
		if ($post_banned)
			echo "<span class=\"red\">";
				
			echo $row['title'];
			
		if ($post_banned)
			echo "</span>";
				
		echo "</span>";
				
	echo "</A></td>";
	
	$userdata = getUserData($row['author'], "id,name");
	echo "<td>";
		echo "$time by <A HREF=\"$php_self?user_profile=",$userdata['id'],"\">", $userdata['name'],"</A>";
	
	echo "</td>";
	
	echo "<td>";
	echo $row['replies'];
	echo "</td>";
	
	echo "<td>";
	
	$last_post = getPost($_COOKIE['show_forum'], $row['last_reply'], "id,author,posted,title");
	
	if ($last_post)
	{
			$parent_post = getTopMost($_COOKIE['show_forum'], $last_post['id'], 5);
			
			$userdata = getUserData($last_post['author'], "id,name");
			echo "<A HREF=\"$php_self?post=",$parent_post,"&show_forum=",$_COOKIE['show_forum'],"#",$last_post['id'],"\">";
			
			echo "<span class=\"ptime", isPostNew($last_post['posted']) ,"\">";
				
			echo $last_post['title'];
			
			echo "</span>";
			
			echo "</A><br><nobr>posted on ",$last_post['posted']," by <A HREF=\"$php_self?user_profile=",$userdata['id'],"\">", $userdata['name'],"</A></nobr>";
		}
		else
		{
			echo "No replies";
		}
	
	echo "</td>";
	echo "</tr>";
	}
	if ($result_pages > 1)
	{
		echo "<tr><td colspan=\"4\" align=\"right\">";
		echo "More Topics: ";
		for ($i = 0; $i < $result_pages; ++$i)
		{
			if ($i > 0)
				echo " | ";
				
			if ($i == $page)
			{
				echo $i + 1;
				continue;
			}
			
			echo " <A HREF=\"$php_self?show_forum=",$_COOKIE['show_forum'],"&page=",$i,"\">";
			echo $i + 1;
			echo "</a> ";
		}
		echo "</td></tr>";
	}
	echo "</table>";
}

function ShowUsers()
{
	$query = "select id from users order by name";
	
	$result = mysql_query($query);
	
	while ($data = mysql_fetch_array($result))
	{
		ShowUserProfile(getUserData($data['id'], "*"), 0);
	}
}

function ShowAllForums($data)
{
	global $php_self;
?>
<table border="0" cellpadding="5" class="main">
<tr>
	<td><B>Description</B></td>
	<td><B>Posts</B></td>
	<td><B>Topics</B></td>
	<td><B>Last Post</B></td>
</tr>	
	
<?php
	while ($row = mysql_fetch_array($data))
	{
	?>
	<tr>
	<td><A HREF="<?php echo $php_self,"?show_forum=",$row['name_id'] ?>"><?php echo $row['name']?></A><BR><?php echo $row['comment']?></td>
	<td><?php echo $row['posts']?></td>
	<td><?php echo $row['topics']?></td>
	<td><?php 
	
		$last_post = getLastPost($row['name_id']);
		
		if ($last_post)
		{
			$userdata = getUserData($last_post['author'], "id,name");
			echo "<A HREF=\"$php_self?post=",getTopMost($row['name_id'],$last_post['id'],5),"&show_forum=",$row['name_id'],"#",$last_post['id'],"\">";

			echo "<span class=\"ptime", isPostNew($last_post['posted']) ,"\">";
				
			echo $last_post['title'];
			
			echo "</span>";
			
			echo "</A><br><nobr>posted on ",$last_post['posted']," by <A HREF=\"$php_self?user_profile=",$userdata['id'],"\">", $userdata['name'],"</A></nobr>";
		}
		else
		{
			echo "No new post";
		}
	?>
	</td>
	</tr>	
	<?php
	}
?>	
</table>
<?php
}

function ExpandNewTopicForm()
{
	global $php_self;
?>
<p align="right"><b>Create New Topic</b>
<form action="<?php echo $php_self,"?&show_forum=",$_COOKIE['show_forum'];?>" method="post">
<p align="right">
Title: <input type="text" name="title" size="80"><BR>
Body: <BR>
<textarea name="body" rows="15" cols="80"></textarea><BR><BR>
<input type="hidden" name="new_topic" value="1">
<input name="submit" value="creat topic" type="submit">
</p>
</form>
</p>
<?php
}

function ExpandEditAccountForm($user)
{
	global $php_self;
	global $_OPTION;
	
	$data = getUserData($user, "*");
	
	$userdata = new UserOptions($user);
	
	?>
	<p align="right">
	<form action="<?php echo $php_self; ?>" method="post">
	
	<input type="hidden" name="update_account" value="<?php echo $user; ?>">
	<p align="right">
	<b>Password</b><br><br>
	Old Password: <input type="password" name="oldpasswd"><br>
	New Password:<input type="password" name="passwd"><br>
	Confirm Password: <input type="password" name="passwd_confirm"><br>
	
	<br><b>Email</b><br><br>
	<input type="text" name="email" value="<?php echo $data['email']?>" size="20"><br>
	<br><b>WWW</b><br><br>
	<input type="text" name="www" value="<?php echo $data['www']?>" size="40"><br>
	
	<br><b>Preferences</b><br><br>
	Don't quote posts when replying: <input type="checkbox" name="dont_quote_replies" value="1" <?php if ($userdata->Check($_OPTION['dont_quote_replies'])) echo "checked"; ?>><br>
	Show email: <input type="checkbox" name="public_email" value="1" <?php if ($userdata->Check($_OPTION['public_email'])) echo "checked"; ?>><br>
	Sign posts: <input type="checkbox" name="sign_posts" value="1" <?php if ($userdata->Check($_OPTION['sign_posts'])) echo "checked"; ?>><br>
	Reply Notification: <input type="checkbox" name="reply_notification" value="1" <?php if ($userdata->Check($_OPTION['reply_notification'])) echo "checked"; ?>><br>
	
	<br><b>Signature</b><br>
	<SPAN CLASS="comment">'--%20' - is added automatically when signing posts</SPAN><BR><br>
	<textarea name="signature" rows="5" cols="80"><?php echo strip_tags($data['signature']) ?></textarea><br>

	<br><b>Comment</b><br><br>
	<textarea name="comment" rows="15" cols="80"><?php echo strip_tags($data['comment']) ?></textarea><br>
	<br><input name="submit" value="update account" type="submit">
	</p>
	</form>
	</p>	
	<?php
}

function ExpandModeratePost($forum, $post)
{
	global $php_self;
	$query = "select id from forums where name_id = '$forum' limit 1";

	$result = mysql_query($query);
	$forum_data = mysql_fetch_array($result);
	
	$id = $forum_data['id'];
	$posts_table = "posts$id";
	
	$query = "select title,body from $posts_table where id = $post limit 1";
	$result = mysql_query($query);
	$data = mysql_fetch_array($result);
?>
<p align="right"><b>Moderate Post</b>
<form action="<?php echo $php_self,"?&show_forum=",$_COOKIE['show_forum']; ?>" method="post">
<p align="right">
Title: <input type="text" name="title" size="80" value="<?php echo $data['title']; ?>"><BR>
Body: <BR>
<textarea name="body" rows="15" cols="80">
<?php 
	echo strip_tags($data['body'], '<b><u><i><pre><span>'); 
?>
</textarea><BR><BR>
<input type="hidden" name="moderate" value="<?php echo $post; ?>">
<input name="submit" value="moderate post" type="submit">
</p>
</form>
</p>
<?php
}

function ExpandAdminPanel()
{
	global $php_self;
?>
<p align="right"><A HREF="<?php echo $php_self;?>?users=1">List Users</A><BR>
<A HREF="<?php echo $php_self;?>?update_user_post_stats=1">Update User Post Stats</A><BR><BR
<b>Create New Forum</b>
<form action="<?php echo $php_self; ?>" method="post">
<p align="right">
Forum Name: <input type="text" name="name" size="80"><BR>
<SPAN CLASS="comment">required - up to 100 chars</SPAN><BR><BR>
Description: <input type="text" name="comment" size="80"><BR>
<SPAN CLASS="comment">required</SPAN><BR><BR>
Name_id: <input type="text" name="name_id"><BR>
<SPAN CLASS="comment">required - up to 50 chars - one word ( phrase ) - 'none' is reserved</SPAN><BR><BR>
<input type="hidden" name="create_forum" value="1">
<input name="submit" value="create forum" type="submit">
</p>
</form>
</p><BR><BR>
<p align="right"><b>Delete Forum</b><BR>
<SPAN CLASS="comment">be careful, deleting a forum is not undable<br>you will loose all posts<br>you have been warned!</SPAN></p>
<?php

	$data = getAllForumsData();
	
	while ($row = mysql_fetch_array($data))
	{
		echo "<form action=\"$php_self\" method=\"post\">";
		echo "<p align=\"right\">";
				echo $row['name'],"<br>";
				echo "<input type=\"hidden\" name=\"delete_forum\" value=\"",$row['name_id'],"\">";
				echo "<input name=\"submit\" value=\"delete forum\" type=\"submit\">";
		echo "</p>";
		echo "</form>";
	}
}

function ExpandReplyForm($forum, $post, $quote)
{
	global $php_self;
	
	$query = "select id from forums where name_id = '$forum' limit 1";

	$result = mysql_query($query);
	$forum_data = mysql_fetch_array($result);
	
	$id = $forum_data['id'];
	$posts_table = "posts$id";
	
	$query = "select id,title,body from $posts_table where id = $post limit 1";

	$result = mysql_query($query);
	
	$data = mysql_fetch_array($result);
	?>
	
	<P ALIGN="RIGHT"><B>Reply</B>
	
<form action="<?PHP echo $php_self,"?&show_forum=",$_COOKIE['show_forum'];?>" method="POST"><P ALIGN="RIGHT">
Title: <input type="text" name="title" size="80" value="Re: <?php echo $data['title'] ?>"><BR>
Body: <BR>
<textarea name="body" rows="15" cols="80">
<?php 

if ($quote)
{
	$data['body'] = cutSignature($data['body']);
	DisplayQuotedText($data['body']);
}
	
?></textarea><BR><BR>
<input type="hidden" name="parent" value="<?php echo $data['id']?>">
<input type="hidden" name="reply" value="1">
<input name="submit" value="post message" type="submit"><br></p>
</form></p>	
	<?php
}

function ExpandLoginForm()
{
	global $php_self;
?>
<P ALIGN="RIGHT"><B>Log In</B>
<form action="<?PHP echo $php_self;?>" method="POST"><P ALIGN="RIGHT">
Login: <input type="text" name="login"><br>
Passwd: <input type="PASSWORD" name="passwd"><br><BR>
<input name="submit" value="login" type="submit"><br></p>
</form></p>
<?php
}

function ExpandCreateAccountForm()
{
	global $php_self;
?>
<P ALIGN="RIGHT"><B>Create New Account</B>
	<FORM ACTION="<?PHP echo $php_self;?>" METHOD="POST"><P ALIGN="RIGHT">
Name:<INPUT TYPE="TEXT" NAME="name" SIZE="20"><BR>
<SPAN CLASS="comment">required - up to 50 chars</SPAN><BR><BR>

Password:<INPUT TYPE="PASSWORD" NAME="passwd" SIZE="20"><BR>
Confirm Password:<INPUT TYPE="PASSWORD" NAME="passwd_confirm" SIZE="20"><BR>
<SPAN CLASS="comment">required - min 6 chars, max 30</SPAN><BR><BR>
Email:<INPUT TYPE="TEXT" NAME="email" SIZE="20"><BR>
<SPAN CLASS="comment">required - must be valid</SPAN><BR><BR>
Personal webpage:<INPUT TYPE="TEXT" NAME="www" SIZE="40"><BR><BR>
<input type="hidden" name="create_account" value="1">
<INPUT TYPE="RESET" NAME="reset" VALUE="clear">&nbsp;&nbsp;&nbsp;
<INPUT TYPE="SUBMIT" NAME="submit" VALUE="create"></P>

</FORM></P>
<?php
}


?>