Location: PHPKode > projects > Fretsweb > fretsweb-1.2/fretsweb/forum.php
<?php

/*
Fretsweb - A Frets on Fire chart server
Copyright (C) 2009  Daan Sprenkels

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


// Include the common file
require_once 'admin/common.php';

// Send headers for content-type
header('Content-Type: text/html; charset=utf-8');

// Include language
require_once "lang/$language.php";


// Add a new post
if(isset($_POST['forumsubmit']))
{
	if(!isset($_COOKIE['fretsweb_dontpost']))
	{
		if(strlen($_POST['text']) < 1000)
		{
			if(strlen($_POST['text']) > 3)
			{
				require_once "admin/log.php";
				$name = addslashes($_POST['name']);
				$text = addslashes($_POST['text']);
				$sql = "INSERT INTO `contest_forum` (`name`, `text`) VALUES ('$name', '$text')";
				mysql_query($sql);
				// We set a cookie so they can't post again, don't tell them. They will disable their cookies
				setcookie('fretsweb_dontpost', 'true', time() + 180 /* Three minutes */, '/');
				$_COOKIE['fretsweb_dontpost'] = 'true';
				clog("$name posted message (".substr($text, 0, 16)."...) from IP " . $_SERVER['REMOTE_ADDR']);
				cfeed("Forum commit", "$name said:\n$text", 'forumchange');
				$info = 'Succesfully posted message.';
			}
			else
			{
				$info = sprintf($lang['forum_message_longer'], $_POST['text']);
			}
		}
		else
		{
			$info = $lang['forum_message_shorter'];
		}
	}
	else
	{
		$info = 'Please wait a moment before posting another message.';
	}
}


// Write header
print_header($lang['forum']);


// Write $info if needed
if(isset($info))
{
	echo "<p class=\"info\" align=\"center\">$info</p>";
}


// Set $start, so we know which posts we have to display
if(isset($_GET['start']))
	$start = $_GET['start'];
else
	$start = 0;
$end = $start + 10;


// Write left and right buttons
echo '<p align="right">';
$nextstart = $start + 10;
$prevstart = $start - 10;
if($prevstart < 0)
{
	$prevstart = 0;
}
$sql = "SELECT * FROM `contest_forum`";
$query = mysql_query($sql);
$num_rows = mysql_num_rows($query);
$laststart = floor($num_rows / 10) * 10;
if($start != 0)
{
	echo "<a href=\"{$_SERVER['PHP_THIS']}?start=0\"><input type=\"button\" value=\"<<\"></a>\n";
	echo "<a href=\"{$_SERVER['PHP_THIS']}?start=$prevstart\"><input type=\"button\" value=\"<\"></a>\n";
}
echo sprintf(" {$lang[page_x_of_x]} ", floor(($start + 1) / 10) + 1, floor(($laststart + 1) / 10) + 1);
if($nextstart < $num_rows)
{
	echo "<a href=\"{$_SERVER['PHP_THIS']}?start=$nextstart\"><input type=\"button\" value=\">\"></a>\n";
	echo "<a href=\"{$_SERVER['PHP_THIS']}?start=$laststart\"><input type=\"button\" value=\">>\"></a>\n";
}
echo '</p>';


// Show the forum
echo '<dl>';
$sql = "SELECT `name`, `text`, `time` FROM `contest_forum` ORDER BY `time` ASC LIMIT $start, $end";
$query = mysql_query($sql);
while($row = mysql_fetch_assoc($query))
{
	echo '<dt>' . sprintf($lang['name_at_time'], htmlspecialchars($row['name']), $row['time']) . '</dt>';
	$text = str_replace("\n", " <br> ", $row['text']);
	$textarr = explode(" ", $text);
	$newtextarr = array();
	foreach($textarr as $textitem)
	{
		if(substr($textitem, 0, 7) == 'http://' || substr($textitem, 0, 8) == 'https://')
		{
			$newtextarr[count($newtextarr)] = "<a href=\"$textitem\">$textitem</a>";
		}
		elseif ($textitem == '<br>')
		{
			$newtextarr[count($newtextarr)] = $textitem;
		}
		else
		{
			$newtextarr[count($newtextarr)] = htmlspecialchars($textitem);
		}
	}
	$newtext = implode(' ', $newtextarr);
	echo "<dd>$newtext</dd>";
}
echo '</dl>';


// Write the 'new post' form
if($allowforum)
{
	// Check if the person has posted already
	if(!isset($_COOKIE['fretsweb_dontpost']))
	{
		echo "<form action=\"{$_SERVER['PHP_SELF']}\" method=\"post\">
	<h2>{$lang['new_post']}</h2>
	<dl>
	<dt>{$lang['name:']}<dt><dd><input type=\"text\" name=\"name\"></dd>
	<dt>{$lang['text:']}<dt><dd><textarea name=\"text\" cols=\"64\" rows=\"6\"></textarea></dd>
	<dt><dd><input type=\"submit\" name=\"forumsubmit\" value=\"{$lang['send']}\"></dd>
	</dl>
	</form>";
	}
	else
	{
		echo "<p>{$lang['can\'t_post']}</p>";
	}
}

// Write footer
echo "</div>";
include "admin/pagefooter.php";
echo "
</div>
</body>
</html>";


// Close the database link
mysql_close( $db_link );

?>
Return current item: Fretsweb