<?php
//v1.0.4 www.bcoos.net 2006/10 GPL
// www.bcoos.net 2006-09 1.0.2 GPL
//v1.9 www.exoops.info 2006/03 
// $Id: index.php,v 1.8 2003/03/19 06:59:51 half-dead Exp $
// ------------------------------------------------------------------------- //
//               E-Xoops: Content Management for the Masses                  //
//                       < http://www.e-xoops.com >                          //
// ------------------------------------------------------------------------- //
// Original Author: Franzisco Burzi
// Author Website : http://www.phpnuke.org
// License Type   : GPL: See /manual/LICENSES/GPL.txt
// ------------------------------------------------------------------------- //
include_once("../../mainfile.php");
//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
/********************************************/
/* Function to let your client login to see */
/* the stats                                */
/********************************************/
function clientlogin() {
global $db,$bcoosConfig;
$xoopsOption['show_rblock'] = 1;
include_once("./header.php");
OpenTable();
echo "
<form action='index.php' method='POST'>
<table width='100%'><tr>
<td colspan='2' align='center' class='bg2'>
<b>"._BN_STATS."</b></td>
</tr><tr>
<td align='right'><b>"._BN_LOGIN."</b></td>
<td><input type='text' class='text' name='login' size='11' maxlength='10' /></td>
</tr><tr>
<td align='right'><b>"._BN_PASS."</b></td>
<td><input type='password' class='text' name='pass' size='11' maxlength='10' /></td>
</tr><tr>
<td align='center' colspan='2'><input class='text' type='hidden' name='op' value='bannerstats' />
<input type='submit' class='button' value='Login'></td>
</tr><tr>
<td colspan='2' align='center' class='bg2'>"._BN_INFO."</td>
</tr></table></form>";
CloseTable();
include_once("footer.php");
}
//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
/*********************************************/
/* Function to display the banners stats for */
/* each client                               */
/*********************************************/
function bannerstats($login, $pass) {
global $db, $myts, $bcoosConfig;
$sqlpass = md5($pass);
$result  = $db->query("SELECT cid, name, login, passwd  FROM ".$db->prefix("banner_clients")." WHERE login='$login' AND passwd='$sqlpass'");
list($cid, $name, $login, $passwd) = $db->fetchRow($result);
if ($sqlpass == $passwd) {
	$xoopsOption['show_rblock'] = 0;
	include_once("./header.php");
	OpenTable();
echo "
<h4 style='text-align:center;'><b>".sprintf(_BN_ACTIVE, $name)."</b><br /></h4>
<table width='100%' border='0' cellpadding='3' cellspacing='0'><tr class='bg2'>
<td align='center'><b>"._BN_ID."</b></td>
<td align='center'><b>"._BN_MADE."</b></td>
<td align='center'><b>"._BN_TOTAL."</b></td>
<td align='center'><b>"._BN_LEFT."</b></td>
<td align='center'><b>"._BN_CLICKS."</b></td>
<td align='center'><b>% "._BN_CLICKS."</b></td>
<td align='center'><b>"._BN_FUNCTIONS."</b></td><tr>";
$result = $db->query("SELECT bid, imptotal, impmade, clicks, datestart FROM ".$db->prefix("banner_items")." WHERE cid=$cid");
while ( list($bid, $imptotal, $impmade, $clicks, $date) = $db->fetchRow($result) ) {
	if ( ($impmade == 0) || ($clicks == 0) ) {
		$percent = 0;
		} else {
			$percent = round(100 * ($clicks/$impmade), 2);
		}
if ( $imptotal == 0 ) {
	$left = _BN_UNLIMITED;
	} else {
		$left = ($imptotal-$impmade);
	}
echo "
<td align='center'>$bid</td>
<td align='center'>$impmade</td>
<td align='center'>$imptotal</td>
<td align='center'>$left</td>
<td align='center'>$clicks</td>
<td align='center'>$percent%</td>
<td align='center'><a href='index.php?op=EmailStats&amp;login=$login&amp;pass=$pass&amp;cid=$cid&amp;bid=$bid'>"._BN_MAIL."</a></td><tr>";
}
echo "</table><br /><br />".sprintf(_BN_RUNNING, $bcoosConfig['title'])."<br /><br />";
$result = $db->query("SELECT bid, imageurl, imagealt, clickurl from ".$db->prefix("banner_items")." WHERE cid=$cid");
while ( list($bid, $imageurl, $imagealt, $clickurl) = $db->fetchRow($result) ) {
	$numrows = $db->getRowsNum($result);
	if ($numrows > 1) {
		echo "<hr /><br />";
	}
echo "
<a href='".$imageurl."'><img src='".formatURL(XOOPS_URL . "/modules/banners/cache/banners/", $imageurl)."' alt='".$myts->makeTboxData4Show($imagealt)."' class='banner' /></a><br />
"._BN_ID." $bid<br />
<a href='index.php?op=EmailStats&amp;login=$login&amp;cid=$cid&amp;bid=$bid&amp;pass=$pass'>"._BN_MAIL."</a><br />
<form action='index.php' method='post'>
<input type='text' class='text' name='url' size='50' maxlength='255' value='".$myts->makeTboxData4Edit($clickurl)."' /> :"._URL."<br />
<input type='text' class='text' name='alt' size='50' maxlength='60' value='".$myts->makeTboxData4Edit($imagealt)."' /> :"._BN_ALT."
<input type='hidden' name='login' value='$login' />
<input type='hidden' name='bid' value='$bid' />
<input type='hidden' name='pass' value='$pass' />
<input type='hidden' name='cid' value='$cid' /><br /><br />
<input type='submit' class='button' name='op' value='Change' /></form>";
}
CloseTable();
/* Finnished Banners */
echo "<br />";
OpenTable();
echo "
<h4 style='text-align:center;'>".sprintf(_BN_FINISHED, $name)."</h4>
<table width='100%' border='0' cellpadding='3' cellspacing='0'><tr class='bg2'>
<td align='center'><b>"._BN_ID."</b></td>
<td align='center'><b>"._BN_IMPRESSIONS."</b></td>
<td align='center'><b>"._BN_CLICKS."</b></td>
<td align='center'><b>% "._BN_CLICKS."</b></td>
<td align='center'><b>"._BN_START."</b></td>
<td align='center'><b>"._BN_END."</b></td></tr>";
$result = $db->query("SELECT bid, impmade, clicks, datestart, dateend FROM ".$db->prefix("banner_items")." WHERE dateend > 1 AND cid=$cid");
while ( list($bid, $impmade, $clicks, $date, $dateend) = $db->fetchRow($result) ) {
	if ( ($impmade == 0) || ($clicks == 0) ) {
		$percent = 0;
		} else {
			$percent = round(100 * ($clicks/$impmade), 2);
		}
	echo "
	<tr>
	<td align='center'>$bid</td>
	<td align='center'>$impmade</td>
	<td align='center'>$clicks</td>
	<td align='center'>$percent%</td>
	<td align='center'>".formatTimestamp($date, 's')."</td>
	<td align='center'>".formatTimestamp($dateend, 's')."</td></tr>";
}
	echo '</table>';
	CloseTable();
	include_once("./footer.php");
	} else {
		redirect_header("index.php", 2, _BN_BADPASS);
		exit();
	}
}
//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
/*********************************************/
/* Function to let the client E-mail his     */
/* banner Stats                              */
/*********************************************/
function EmailStats($login, $cid, $bid, $pass) {
global $db, $bcoosConfig;
$result2 = $db->query("SELECT name, email FROM ".$db->prefix("banner_clients")." WHERE cid=$cid");
list($name, $email) = $db->fetchRow($result2);
if ( !checkEmail($email) ) {
	redirect_header("index.php", 3, sprintf(_BN_NOEMAIL, $name));
	exit();
	} else {
		$result = $db->query("SELECT bid, imptotal, impmade, clicks, imageurl, clickurl FROM ".$db->prefix("banner_items")." WHERE bid=$bid AND cid=$cid");
		list($bid, $imptotal, $impmade, $clicks, $imageurl, $clickurl, $date) = $db->fetchRow($result);
		if ( ($impmade == 0) || ($clicks == 0) ) {
			$percent = 0;
			} else {
				$percent = round(100 * ($clicks/$impmade), 2);
			}
		if ($imptotal == 0) {
			$left     = _BN_UNLIMITED;
			$imptotal = _BN_UNLIMITED;
			} else {
				$left = ($imptotal-$impmade);
			}
		$xoopsMailer =& getMailer();
		$xoopsMailer->setTemplateDir(XOOPS_ROOT_PATH."/modules/banners/");
		$xoopsMailer->setTemplate("bannerstats.tpl");
		$xoopsMailer->assign("SITE_NAME", $bcoosConfig['title']);
		$xoopsMailer->assign("ADMIN_MAIL", $bcoosConfig['adminmail']);
		$xoopsMailer->assign("SITE_URL", XOOPS_URL."/");
		$xoopsMailer->assign("CLIENT_NAME", $name);
		$xoopsMailer->assign("BANNER_ID", $bid);
		$xoopsMailer->assign("IMAGE_URL", $imageurl);
		$xoopsMailer->assign("CLICK_URL", $clickurl);
		$xoopsMailer->assign("IMP_TOTAL", $imptotal);
		$xoopsMailer->assign("IMP_MADE", $impmade);
		$xoopsMailer->assign("IMP_LEFT", $left);
		$xoopsMailer->assign("CLICKS", $clicks);
		$xoopsMailer->assign("PERCENT", $percent);
		$xoopsMailer->assign("RPT_DATE", formatTimestamp(time(), 'm'));
		$xoopsMailer->useMail();
		$xoopsMailer->setToEmails($email);
		$xoopsMailer->setFromEmail($bcoosConfig['adminmail']);
		$xoopsMailer->setFromName($bcoosConfig['title']);
		$xoopsMailer->setSubject(sprintf(_BN_STATSAT, $bcoosConfig['title']));
		if (!$xoopsMailer->send()) {
			$message = $xoopsMailer->getErrors();
			} else {
				$message = _BN_STATSMAILED;
			}
		redirect_header("index.php?op=Ok&amp;login=$login&amp;pass=$pass", 3, $message);
		exit();
	}
}
//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
/*********************************************/
/* Function to let the client to change the  */
/* url for his banner                        */
/*********************************************/
function change_banner_url_by_client($login, $pass, $cid, $bid, $url, $alt) {
global $db, $myts;
$result = $db->query("SELECT passwd FROM ".$db->prefix("banner_clients")." WHERE cid=$cid");
list($passwd) = $db->fetchRow($result);
if (md5($pass) == $passwd) {
	$alt    = $myts->makeTboxData4Save($alt);
	$update = $db->query("UPDATE ".$db->prefix("banner_items")." SET clickurl='$url', imagealt='$alt' WHERE bid=$bid");
}
if ($update) {
	redirect_header("index.php?op=Ok&amp;login=$login&amp;pass=$pass", 3, _BN_URLCHANGED);
	} else {
		redirect_header("index.php?op=Ok&amp;login=$login&amp;pass=$pass", 1, _NOTUPDATED);
	}
exit();
}
//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function clickbanner($bid) {
global $db;
$bresult = $db->query("SELECT clickurl FROM ".$db->prefix("banner_items")." WHERE bid=$bid");
list($clickurl) = $db->fetchRow($bresult);
$db->query("UPDATE ".$db->prefix("banner_items")." SET clicks=clicks+1 WHERE bid=$bid");
echo "<html><head><meta http-equiv='Refresh' content='0; URL=".$clickurl."'></head><body></body></html>";
exit();
}
//---------------------------------------------------------------------------------------//
$op = !empty($_POST['op']) ? $_POST['op'] : $_GET['op'];
switch ($op) {
	case "click":
		clickbanner($_GET['bid']);
		break;
	case "bannerstats":
		bannerstats($_POST['login'], $_POST['pass']);
		break;
	case "Ok":
		bannerstats($_GET['login'], $_GET['pass']);
		break;
	case "Change":
		change_banner_url_by_client($_POST['login'], $_POST['pass'], $_POST['cid'], $_POST['bid'], $_POST['url'], $_POST['alt']);
		break;
	case "EmailStats":
		EmailStats($_GET['login'], $_GET['cid'], $_GET['bid'], $_GET['pass']);
		break;
	default:
		clientlogin();
		break;
}
?>
