<?php
/*
 ########################################################################
#                                                                        #
#           Version 4       /                        /   /               #
#          -----------__---/__---__------__----__---/---/-               #
#           | /| /  /___) /   ) (_ `   /   ) /___) /   /                 #
#          _|/_|/__(___ _(___/_(__)___/___/_(___ _/___/___               #
#                       Free Content / Management System                 #
#                                   /                                    #
#                                                                        #
#                                                                        #
#   Copyright 2005-2006 by webspell.org                                  #
#                                                                        #
#   visit webSPELL.org, webspell.info to get webSPELL for free           #
#   - Script runs under the GNU GENERAL PUBLIC LICENSE                   #
#   - It's NOT allowed to remove this copyright-tag                      #
#   -- http://www.fsf.org/licensing/licenses/gpl.html                    #
#                                                                        #
#   Code based on WebSPELL Clanpackage (Michael Gruber - webspell.at),   #
#   Far Development by Development Team - webspell.org                   #
#                                                                        #
#   visit webspell.org                                                   #
#                                                                        #
 ########################################################################
*/

if($_GET['action']=="save") {
  include("_mysql.php");
	include("_settings.php");
	include("_functions.php");

  $message = trim($_POST['message']);
  $name = trim($_POST['name']);
  $run=0;
  if($userID) {
  	$run=1;
  	$name = getnickname($userID);
  }
  else {
  	$CAPCLASS = new Captcha;	
		if($CAPCLASS->check_captcha($_POST['captcha'], $_POST['captcha_hash'])) $run=1;
		
		if(mysql_num_rows(safe_query("SELECT * FROM ".PREFIX."user WHERE nickname = '$name' "))) $name = '*'.$name.'*';
	}

	if(!empty($name) && !empty($message) && $run) {
		$date=time();
		$ip = $_SERVER['REMOTE_ADDR'];
		$ergebnis = safe_query("SELECT * FROM ".PREFIX."shoutbox ORDER BY date DESC LIMIT 0,1");
		$ds=mysql_fetch_array($ergebnis);
    if(($ds[message] != $message) OR ($ds[name] != $name)) safe_query("INSERT INTO ".PREFIX."shoutbox (date, name, message, ip) VALUES ( '$date', '$name', '$message', '$ip' ) ");
	}
	redirect("shoutbox_content.php?action=show",'',0);
}
elseif($_GET['action']=="delete") {
  include("_mysql.php");
	include("_settings.php");
	include('_functions.php');
  if(!isfeedbackadmin($userID)) die('No access.');

	foreach($_POST['shoutID'] as $id) {
    	safe_query("DELETE FROM ".PREFIX."shoutbox WHERE shoutID='$id'");
	}
	header("Location: index.php?site=shoutbox_content&action=showall"); 
}

elseif($_GET['action']=="showall") {
    eval ("\$title_shoutbox = \"".gettemplate("title_shoutbox")."\";");
    echo $title_shoutbox;

	$all = safe_query("SELECT count(shoutID) FROM ".PREFIX."shoutbox ORDER BY date");
	$all = mysql_result($all, 0);
	$pages=1;
	if(!isset($page)) $page = 1;
	if(!isset($type)) $type = "DESC";

	$max=$maxsball;

    for ($n=$max; $n<=$all; $n+=$max) {
	    if($all>$n) $pages++;
	}

	if($pages>1) $page_link = makepagelink("index.php?site=shoutbox_content&action=showall&type=$type", $page, $pages);
	if ($page == "1") {
        $ergebnis = safe_query("SELECT * FROM ".PREFIX."shoutbox ORDER BY date $type LIMIT 0,$max");
		if($type=="DESC") $n=$all;
		else $n=1;
	}
	else {
	    $start=$page*$max-$max;
	    $ergebnis = safe_query("SELECT * FROM ".PREFIX."shoutbox ORDER BY date $type LIMIT $start,$max");
		if($type=="DESC") $n = $all-($page-1)*$max;
		else $n = ($page-1)*$max+1;
	}

    if($type=="ASC") 
	    $sorter='<a href="index.php?site=shoutbox_content&action=showall&page='.$page.'&type=DESC">Sort:</a> <img src="images/icons/asc.gif" width="9" height="7" border="0">&nbsp;&nbsp;&nbsp;';
	else 
	    $sorter='<a href="index.php?site=shoutbox_content&action=showall&page='.$page.'&type=ASC">Sort:</a> <img src="images/icons/desc.gif" width="9" height="7" border="0">&nbsp;&nbsp;&nbsp;';	

	echo'<table width="100%" cellspacing="0" cellpadding="0">
		  <tr>
		    <td>'.$sorter.' '.$page_link.'</td>
		  </tr>
		</table>';
		
	eval ("\$shoutbox_all_head = \"".gettemplate("shoutbox_all_head")."\";");
    echo $shoutbox_all_head;
		
	$i=1;
	while($ds=mysql_fetch_array($ergebnis)) {
	    $i%2 ? $bg1=BG_1 : $bg1=BG_2;
		$date=date("d.m - H:i", $ds[date]);
	    $name=cleartext($ds[name]);
		$message=cleartext($ds[message]);
		$ip='logged';
			
	    if(isfeedbackadmin($userID)) {
			$actions='<input class="input" type="checkbox" name="shoutID[]" value="'.$ds[shoutID].'">';
			$ip=$ds[ip];
		}
		else $actions='';
							  
		eval ("\$shoutbox_all_content = \"".gettemplate("shoutbox_all_content")."\";");
        echo $shoutbox_all_content;
		if($type=="DESC") $n--;
		else $n++;
		$i++;
    }
	eval ("\$shoutbox_all_foot = \"".gettemplate("shoutbox_all_foot")."\";");
    echo $shoutbox_all_foot;
		
	if(isfeedbackadmin($userID)) $submit='<input class="input" type="checkbox" name="ALL" value="ALL" onClick="SelectAll(this.form);"> select all
											  <input type="submit" value="delete selected">';
	echo'<table width="100%" border="0" cellspacing="0" cellpadding="0">
		<tr>
   		<td>'.$page_link.'</td>
   		<td align="right">'.$submit.'</td>
		</tr>
		</table></form>';
		
	if($pages>1) $page_link = makepagelink("index.php?site=shoutbox_content&action=showall", $page, $pages);
}
else {
    include("_mysql.php");
   	include("_settings.php");
    include("_functions.php");

    $pagebg=PAGEBG;
    $border=BORDER;
    $bghead=BGHEAD;
    $bgcat=BGCAT;
    $bg1=BG_1;

	if(!$sbrefresh) $sbrefresh = 60;
	eval ("\$shoutbox_head = \"".gettemplate("shoutbox_head")."\";");
    echo $shoutbox_head;
		
    $ergebnis=safe_query("SELECT * FROM ".PREFIX."shoutbox ORDER BY date DESC LIMIT 0,".$maxshoutbox."");
	while($ds=mysql_fetch_array($ergebnis)) {
		$date=date("H:i", $ds[date]);
		$name=clearfromtags($ds[name]);
		$message=cleartext($ds[message], false);
		
		eval ("\$shoutbox_content = \"".gettemplate("shoutbox_content")."\";");
        echo $shoutbox_content;
	}
	eval ("\$shoutbox_foot = \"".gettemplate("shoutbox_foot")."\";");
    echo $shoutbox_foot;
}



?>
