<?php
/**
 * sefbook - A joomla Guestbook Component
 * @version 1.6
 * @package sefbook
 * @copyright (C) 2003, 2004 by Arthur Konze - All rights reserved!
 * @license http://www.konze.de/content/view/8/26/ Copyrighted Commercial Software
 **/

defined( '_JEXEC' ) or die( 'Restricted access' );

JLoader::import('GuestBookPlusUtils', dirname( __FILE__ ));
JLoader::import('GuestBookSecurity', dirname( __FILE__ ));

# Variables - Don't change anything here!!!
require(JPATH_SITE.DS."administrator".DS."components".DS."com_sefbook".DS."config.sefbook.php");

# Check if sefbook is offline
if ($ab_offline == 1) {
	require(JPATH_SITE."/components/com_sefbook/offline.php");
} else {
	$option = JRequest::getVar('option', '');
	$Itemid = JRequest::getVar('Itemid', '');
	$func = JRequest::getVar('func', '');
	$gbid = JRequest::getVar('gbid', '');
	$captcha = JRequest::getVar('captcha', '');
	$gbname = JRequest::getVar('gbname', '');
	$gbmail = JRequest::getVar('gbmail', '');
	$gbloca = JRequest::getVar('gbloca', '');
	$gbpage = JRequest::getVar('gbpage', '');
	$gbvote = JRequest::getVar('gbvote', '');
	$gbtext = JRequest::getVar('gbtext', '');
	$gbicq = JRequest::getVar('gbicq', '');
	$gbaim = JRequest::getVar('gbaim', '');
	$gbmsn = JRequest::getVar('gbmsn', '');
	$gbcomment = JRequest::getVar('gbcomment', '');
	$submit = JRequest::getVar('submit', '');
	$startpage = JRequest::getVar('startpage', '');

	# Get the right language if it exists
	
	$lg = &JFactory::getLanguage();
	$language = $lg->getBackwardLang();
	
	if (file_exists(JPATH_SITE.DS.'components'.DS.'com_sefbook'.DS.'languages/'.$language.'.php')) {
		include(JPATH_SITE.DS.'components'.DS.'com_sefbook'.DS.'languages/'.$language.'.php');
	} else {
		include(JPATH_SITE.DS.'components'.DS.'com_sefbook'.DS.'languages'.DS.'english.php');
	}

	$is_editor = GuestBookPlusUtils::isEditor();
	$is_user   = GuestBookPlusUtils::isUser();

	switch ($func) {

		case 'deleteentry':
			GuestBookPlusUtils::GuestbookHeader(true);
			include(dirname( __FILE__ ).DS.'sub_deleteentry.php');
			break;

		case 'comment':
			GuestBookPlusUtils::GuestbookHeader(true);
			include(dirname( __FILE__ ).DS.'sub_commententry.php');
			break;
			#########################################################################################
		case 'entry':
			JRequest::checkToken() or die( 'Invalid Token' );

			# Clear any HTML
			$gbtext = strip_tags($gbtext);
			$gbname = strip_tags($gbname);
			$gbmail = strip_tags($gbmail);
			$gbloca = strip_tags($gbloca);
			$gbpage = strip_tags($gbpage);
			$gbvote = strip_tags($gbvote);
			$gbicq  = strip_tags($gbicq);
			$gbaim  = strip_tags($gbaim);
			$gbmsn  = strip_tags($gbmsn);
			$gbid  = intval(mysql_escape_string(strip_tags($gbid)));

			# Clear dangerous sql injections
			$gbname = mysql_escape_string($gbname);
			$gbmail = mysql_escape_string($gbmail);
			$gbloca = mysql_escape_string($gbloca);
			$gbpage = mysql_escape_string($gbpage);
			$gbvote = mysql_escape_string($gbvote);
			$gbtext = mysql_escape_string($gbtext);
			$gbicq  = mysql_escape_string($gbicq);
			$gbaim  = mysql_escape_string($gbaim);
			$gbmsn  = mysql_escape_string($gbmsn);

			$guestEntryIsAccepted = true;

			if ($acguest_recaptcha && !GuestBookPlusSecurity::isRecaptchaAcceptingPost()) {
				$guestEntryIsAccepted = false;
			}
			if ($acguest_securityimage && !GuestBookPlusSecurity::isSecurityImagesAcceptingPost()) {
				$guestEntryIsAccepted = false;
			}
			if ($acguest_akismet && !GuestBookPlusSecurity::isAkismetAcceptingPost($Itemid,$gbname, $gbmail, $gbpage, $gbtext)) {
				$guestEntryIsAccepted = false;
			}

			if ($guestEntryIsAccepted)
			{
				# Check if entry was edited by editor
				if (($is_editor) AND ($gbid)) {
					$query1 = "UPDATE #__sefbook SET gbname='$gbname', gbmail='$gbmail', gbloca='$gbloca', gbpage='$gbpage', gbvote='$gbvote', gbtext='$gbtext', gbicq='$gbicq', gbaim='$gbaim', gbmsn='$gbmsn' WHERE gbid=$gbid";
					$database = JFactory::getDBO();
					$database->setQuery( $query1 );
					$database->query();
					echo "<script> alert('"._GUESTBOOK_SAVED."'); document.location.href='index.php?option=com_sefbook&Itemid=$Itemid';</SCRIPT>";
				} else {
					$gbdate = time();
					$gbip   = getenv('REMOTE_ADDR');
					$query2 = "INSERT INTO #__sefbook SET gbname='$gbname',gbip='$gbip', gbdate='$gbdate', gbmail='$gbmail', gbloca='$gbloca', gbpage='$gbpage', gbvote='$gbvote', gbtext='$gbtext', gbicq='$gbicq', gbaim='$gbaim', gbmsn='$gbmsn'";
					if ($ab_autopublish) {
						$query2 .= ",published='1'";
					}
					$database = JFactory::getDBO();
					$database->setQuery( $query2 );
					$database->query();
					if ($ab_notify AND GuestBookPlusUtils::is_email($ab_notify_email) ) {
						$gbmailtext = _GUESTBOOK_ADMINMAIL."\r\n\r\nName: ".$gbname."\r\nText: ".$gbtext."\r\n\r\n"._GUESTBOOK_MAILFOOTER;
						mail($ab_notify_email,_GUESTBOOK_ADMINMAILHEADER,$gbmailtext,"From: ".$ab_notify_email);
					}
					if ($ab_thankuser AND GuestBookPlusUtils::is_email($gbmail) ) {
						$gbmailtext = _GUESTBOOK_USERMAIL."\r\n\r\nName: ".$gbname."\r\nText: ".$gbtext."\r\n\r\n"._GUESTBOOK_MAILFOOTER;
						mail($gbmail,_GUESTBOOK_USERMAILHEADER,$gbmailtext,"From: ".$ab_notify_email);
					}
					echo "<script> alert('"._GUESTBOOK_SAVED."'); document.location.href='index.php?option=com_sefbook&Itemid=$Itemid';</SCRIPT>";
				}
			}
			else
			echo "<script> alert('"._GUESTBOOK_REFUSED."'); document.location.href='index.php?option=com_sefbook&Itemid=$Itemid';</SCRIPT>";
			break;
			#########################################################################################
		case 'sign':
			if ($ab_allowentry) {
				GuestBookPlusUtils::GuestbookHeader(true);
				include(JPATH_SITE.'/components/com_sefbook/sub_writeentry.php');
				break;
			}
			#########################################################################################
		default:
			GuestBookPlusUtils::GuestbookHeader(false);
			# Feststellen der Anzahl der verf�gbaren Datens�tze
			$database = JFactory::getDBO();

			$database->setQuery( "SELECT COUNT(gbid) FROM #__sefbook WHERE published='1'" );
			$database->query();
			$count = $database->loadresult();

			# Berechnen der Gesamtseiten
			$gesamtseiten = floor($count / $ab_perpage);
			$seitenrest   = $count % $ab_perpage;
			if ($seitenrest>0) {
				$gesamtseiten++;
			}
			# Feststellen der aktuellen Seite
			if (isset($startpage)) {
				if ($startpage>$gesamtseiten) {
					$startpage = $gesamtseiten;
				} else if ($startpage<1) {
					$startpage = 1;
				}
			} else {
				$startpage = 1;
			}
			echo "<p>"._GUESTBOOK_BEFOREENTRIES." $count "._GUESTBOOK_AFTERENTRIES."<br />";
			echo _GUESTBOOK_PAGES." ";
			# Ausgeben der Seite zurueck Funktion
			$seiterueck = $startpage - 1;
			if ($seiterueck>0) {
				echo "<a href=\"".GuestBookPlusUtils::sefConverter("index.php?option=com_sefbook&Itemid=$Itemid&startpage=$seiterueck")."\"><b>&laquo;</b></a> ";
			}
			#Ausgeben der einzelnen Seiten
			for ($i=1; $i <= $gesamtseiten; $i++) {
				if ($i==$startpage) {
					echo "$i ";
				} else {
					echo "<a href=\"".GuestBookPlusUtils::sefConverter("index.php?option=com_sefbook&Itemid=$Itemid&startpage=$i")."\">$i</a> ";
				}
			}
			# Ausgeben der Seite vorw�rts Funktion
			$seitevor = $startpage + 1;
			if ($seitevor<=$gesamtseiten) {
				echo "<a href=\"".GuestBookPlusUtils::sefConverter("index.php?option=com_sefbook&Itemid=$Itemid&startpage=$seitevor")."\"><b>&raquo;</b></a> ";
			}
			# Limit und Seite Vor- & Rueckfunktionen
			$start = ( $startpage - 1 ) * $ab_perpage;
			echo "</p>";
			// Database Query
			echo "<table width='100%' border='0' cellspacing='1' cellpadding='4'>";
			echo "<tr><td width='30%' height='20' class='sectiontableheader'>"._GUESTBOOK_NAME."</td>";
			echo "<td width='70%' height='20' class='sectiontableheader'>"._GUESTBOOK_ENTRY."</td></tr>";
			$line=1;

			$database->setQuery( "SELECT * FROM #__sefbook"
			. "\nWHERE published = 1"
			. "\nORDER BY gbid $ab_sorting"
			. "\nLIMIT $start,$ab_perpage"
			);
			$rows = $database->loadObjectList();
			
			$middle = 5;
			
			$pubcounter = 0;
			$pubNumber = 2;
			$elementCount = count($rows);
			$pubIndex = (int)$elementCount /$pubNumber;
			
			foreach ( $rows AS $row1) {
				$linecolor = ($line % 2) + 1;
				
				//if (($line % $pubIndex == 0) && $pubcounter <= $pubNumber) {
				//	GuestBookPlusUtils::getPublicity($linecolor);
				//	$pubcounter++;
				//} else {
				$row1->gbtext = stripslashes($row1->gbtext);
				$row1->gbname = stripslashes($row1->gbname);
				$row1->gbloca = stripslashes($row1->gbloca);
				$row1->gbname = GuestBookPlusUtils::textwrap($row1->gbname,20);
				$row1->gbloca = GuestBookPlusUtils::textwrap($row1->gbloca,30);
				echo "<tr class='sectiontableentry".$linecolor."'><td width='30%' valign='top'><a name='gb$row1->gbid'></a><b>$row1->gbname</b>";
				if ($row1->gbloca<>"" AND $ab_showloca) echo "<br /><span class='small'>"._GUESTBOOK_FROM." $row1->gbloca</span>";
				echo "</td>";
				$signtime = strftime("%c",$row1->gbdate);
				$origtext = GuestBookPlusUtils::AkoParse($row1->gbtext);
				$origtext = GuestBookPlusUtils::textwrap($origtext,80);
				echo "<td width='70%' valign='top'><span class='small'>"._GUESTBOOK_SIGNEDON." $signtime</span><hr />$origtext";
				if ($row1->gbcomment<>"") {
					$origcomment = GuestBookPlusUtils::AkoParse($row1->gbcomment);
					echo "<hr /><span class='small'><b>"._GUESTBOOK_ADMINSCOMMENT.":</b> $origcomment</span>";
				}
				echo "</td></tr>";
				echo "<tr class='sectiontableentry".$linecolor."'><td width='30%' valign='top'>";
				if ($row1->gbmail<>"" AND $ab_showmail) {
					echo "<a href='mailto:$row1->gbmail'><img src='".JURI :: base()."/components/com_sefbook/images/email.gif' alt='$row1->gbmail' title='$row1->gbmail' hspace='3' border='0' /></a>";
				}
				if ($row1->gbpage<>"" AND $ab_showhome) {
					# Check if URL is in right format
					if (substr($row1->gbpage,0,7)!="http://") $row1->gbpage="http://$row1->gbpage";
					echo "<a href='$row1->gbpage' target='_blank'><img src='".JURI :: base()."/components/com_sefbook/images/homepage.gif' alt='$row1->gbpage' title='$row1->gbpage' hspace='3' border='0' /></a>";
				}
				if ($row1->gbicq<>"" AND $ab_showicq) {
					echo "<a href='mailto:$row1->gbicq@pager.icq.com'><img src='".JURI :: base()."/components/com_sefbook/images/icq.gif' alt='$row1->gbicq' title='$row1->gbicq' hspace='3' border='0' /></a>";
				}
				if ($row1->gbaim<>"" AND $ab_showaim) {
					echo "<a href='aim:goim?screenname=$row1->gbaim'><img src='".JURI :: base()."/components/com_sefbook/images/aim.gif' alt='$row1->gbaim' title='$row1->gbaim' hspace='3' border='0' /></a>";
				}
				if ($row1->gbmsn<>"" AND $ab_showmsn) {
					echo "<a href='aim:goim?screenname=$row1->gbmsn'><img src='".JURI :: base()."/components/com_sefbook/images/msn.gif' alt='$row1->gbmsn' title='$row1->gbmsn' hspace='3' border='0' /></a>";
				}
				if ($is_editor) {
					echo "<img src='".JURI :: base()."/components/com_sefbook/images/ip.gif' alt='$row1->gbip' title='$row1->gbip' hspace='3' border='0' />";
				}
				echo "</td>";
				echo "<td width='70%' valign='top'><table width='100%' border='0' cellspacing='0' cellpadding='0'><tr>";
				if ($is_editor) {
					echo "<td align='left'><b>"._GUESTBOOK_ADMIN.":</b> ";
					$token = JUtility::getToken();
					echo "<a href='".GuestBookPlusUtils::sefConverter("index.php?option=com_sefbook&Itemid=$Itemid&func=sign&gbid=$row1->gbid"."&$token=1")."'>"._GUESTBOOK_AEDIT."</a> - ";
					echo "<a href='".GuestBookPlusUtils::sefConverter("index.php?option=com_sefbook&Itemid=$Itemid&func=comment&gbid=$row1->gbid"."&$token=1")."'>"._GUESTBOOK_ACOMMENT."</a> - ";
					echo "<a href='".GuestBookPlusUtils::sefConverter("index.php?option=com_sefbook&Itemid=$Itemid&func=deleteentry&gbid=$row1->gbid"."&$token=1")."'>"._GUESTBOOK_ADELETE."</a></td>";
				}
				echo "<td align='right'>";
				GuestBookPlusutils::echoRatingPanel($ab_showrating,$ab_maxvoting, $row1);
				echo "</td></tr></table></td></tr>";
				//}
				$line++;
			}
			echo "</table>";
			break;
	}
	GuestBookPlusUtils::GuestbookFooter();

}

?>