<?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(!isanyadmin($userID) OR substr(basename($_SERVER[REQUEST_URI]),0,15) != "admincenter.php") die('Access denied.');
$filepath = "../images/bannerrotation/";

if($_GET["action"]=="add") {
	echo'
		<h2>bannerrotation</h2>
		<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=bannerrotation\');return document.MM_returnValue" value="existing banners">
		<h2>new banner</h2>';
	echo'
		<form method="post" action="admincenter.php?site=bannerrotation" enctype="multipart/form-data">
		<table cellpadding="4" cellspacing="0">
			<tr>
				<td>Banner-Upload:</td>
				<td><input name="banner" type="file"></td>
			</tr>
			<tr>
				<td>Banner Name:</td>
				<td><input type="text" name="bannername" size="60" maxlength="255" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
			</tr>
			<tr>
				<td>Banner URL:</td>
				<td><input type="text" name="bannerurl" size="60" maxlength="255" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
			</tr>
			<tr>
				<td>Is displayed?</td>
				<td><input type="checkbox" name="displayed" value="1" selected></td>
			</tr>
			<tr>
				<td></td>
				<td><input type="submit" name="save" value="add banner"></td>
			</tr>
		</table>
		</form>';
}
elseif($_GET["action"]=="edit") {
	$ds=mysql_fetch_array(safe_query("SELECT * FROM ".PREFIX."bannerrotation WHERE bannerID='".$_GET["bannerID"]."'"));
	if(file_exists($filepath.$ds['bannerID'].'.gif'))	$pic='<img src="../images/bannerrotation/'.$ds['bannerID'].'.gif" border="0" alt="'.$ds['banner'].'">';
	elseif(file_exists($filepath.$ds['bannerID'].'.jpg'))	$pic='<img src="../images/bannerrotation/'.$ds['bannerID'].'.jpg" border="0" alt="'.$ds['banner'].'">';
	else $pic='no image uploaded';
	
	if($ds['displayed']=='1') $displayed='<input type="checkbox" name="displayed" value="1" checked>';
	else $displayed='<input type="checkbox" name="displayed" value="1">';
	
	echo'
		<h2>bannerrotation</h2>
		<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=bannerrotation\');return document.MM_returnValue" value="existing banners">
		<h2>edit banner - '.$ds['bannername'].'</h2>';
	echo'
		<form method="post" action="admincenter.php?site=bannerrotation" enctype="multipart/form-data">
		<input type="hidden" name="bannerID" value="'.$ds['bannerID'].'">
		<table cellpadding="4" cellspacing="0">
			<tr>
				<td>Present Banner:</td>
				<td>'.$pic.'</td>
			</tr>		
			<tr>
				<td>Banner Upload:</td>
				<td><input name="banner" type="file"></td>
			</tr>
			<tr>
				<td>Banner Name:</td>
				<td><input type="text" name="bannername" size="60" maxlength="255" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'" value="'.$ds['bannername'].'"></td>
			</tr>
			<tr>
				<td>Banner URL:</td>
				<td><input type="text" name="bannerurl" size="60" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'" value="'.$ds['bannerurl'].'"></td>
			</tr>
			<tr>
				<td>Is displayed?</td>
				<td>'.$displayed.'</td>
			</tr>
			<tr>
				<td></td>
				<td><input type="submit" name="saveedit" value="edit banner"></td>
			</tr>
		</table>
		</form>';
}
elseif($_POST["save"]) {
	$banner=$_FILES["banner"];
	$bannername=$_POST["bannername"];
	$bannerurl=$_POST["bannerurl"];
	$displayed=$_POST["displayed"];
	if(!$displayed) $displayed=0;
	
	echo'
		<h2>bannerrotation</h2>
		<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=bannerrotation&action=add\');return document.MM_returnValue" value="new banner">
		<h2>new banner</h2>';
	
	if($bannername AND $bannerurl AND $banner) {
		if(eregi('http://', $bannerurl)) $bannerurl=$bannerurl;
		else $bannerurl='http://'.$bannerurl;
		
		$file_ext=strtolower(substr($banner[name], strrpos($banner[name], ".")));
		if($file_ext==".gif" OR $file_ext==".jpg" OR $file_ext==".png") {
			safe_query("INSERT INTO ".PREFIX."bannerrotation (bannerID, bannername, bannerurl, displayed, date) values('', '".$bannername."', '".$bannerurl."', '".$displayed."', '".time()."')");
			$id=mysql_insert_id();
			if($banner[name] != "") {
				move_uploaded_file($banner[tmp_name], $filepath.$banner[name]);
				@chmod($filepath.$banner[name], 0755);
				$file=$id.$file_ext;
				rename($filepath.$banner[name], $filepath.$file);
				if(safe_query("UPDATE ".PREFIX."bannerrotation SET banner='".$file."' WHERE bannerID='".$id."'")) {
					redirect("admincenter.php?site=bannerrotation", "Banner created.", "3");
				} else {
					redirect("admincenter.php?site=bannerrotation", "Banner could not be created", "3");
				}
			}
		} else echo'<b>The format of the icon was incorrect. Please upload only banner in *.gif, *.jpg and *.png format.</b><br><br><a href="javascript:history.back()">&laquo; back</a>';
	} else echo'<b>Please fill in the form correctly.</b><br><br><a href="javascript:history.back()">&laquo; back</a>';
}
elseif($_POST["saveedit"]) {
	$banner=$_FILES["banner"];
	$bannername=$_POST["bannername"];
	$bannerurl=$_POST["bannerurl"];
	$displayed=$_POST["displayed"];
	if(!$displayed) $displayed=0;

	echo'<h2>banner</h2>
	<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=bannerrotation\');return document.MM_returnValue" value="existing banners">
	<h2>edit banner</h2>';	
	
	if($banner AND $bannername AND $bannerurl) {
		if(eregi('http://', $bannerurl)) $bannerurl=$bannerurl;
		else $bannerurl='http://'.$bannerurl;
		
		if($banner[name]=="") {
			if(safe_query("UPDATE ".PREFIX."bannerrotation SET bannername='".$bannername."', bannerurl='".$bannerurl."', displayed='".$displayed."' WHERE bannerID='".$_POST["bannerID"]."'"))
				redirect("admincenter.php?site=bannerrotation", "Banner updated.", "3");
		} else {
			$file_ext=strtolower(substr($banner[name], strrpos($banner[name], ".")));
			if($file_ext==".gif" OR $file_ext==".jpg" OR $file_ext==".png") {
				move_uploaded_file($banner[tmp_name], $filepath.$banner[name]);
				@chmod($filepath.$banner[name], 0755);
				$file=$_POST['bannerID'].$file_ext;
				rename($filepath.$banner[name], $filepath.$file);

				if(safe_query("UPDATE ".PREFIX."bannerrotation SET banner='".$file."', bannername='".$bannername."', bannerurl='".$bannerurl."', displayed='".$displayed."' WHERE bannerID='".$_POST["bannerID"]."'")) {
					header("Location: admincenter.php?site=bannerrotation");
				}
			} else echo'<b>The format of the icon was incorrect. Please upload only banner in *.gif, *.jpg and *.png format.</b><br><br><a href="javascript:history.back()">&laquo; back</a>';
		}
	} else echo'<b>Please fill in the form correctly.</b><br><br><a href="javascript:history.back()">&laquo; back</a>';
}
elseif($_GET["delete"]) {
	if(safe_query("DELETE FROM ".PREFIX."bannerrotation WHERE bannerID='".$_GET["bannerID"]."'")) {
		if(file_exists($filepath.$_GET["bannerID"].'.jpg')) unlink($filepath.$_GET["bannerID"].'.jpg');
		if(file_exists($filepath.$_GET["bannerID"].'.gif')) unlink($filepath.$_GET["bannerID"].'.gif');
		if(file_exists($filepath.$_GET["bannerID"].'.png')) unlink($filepath.$_GET["bannerID"].'.png');
		redirect("admincenter.php?site=bannerrotation", "Banner deleted.", "3");
	} else {
		redirect("admincenter.php?site=bannerrotation", "Banner could not be deleted", "3");
	}
}
else {
	echo'
		<h2>bannerrotation</h2>
		<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=bannerrotation&action=add\');return document.MM_returnValue" value="new banner">
		<h2>existing banners</h2>
		<form method="post" action="admincenter.php?site=bannerrotation">
		<table width="100%" cellpadding="4" cellspacing="1" bgcolor="#999999">
			<tr bgcolor="#CCCCCC">
				<td class="title">Banner:</td>
				<td class="title">Banner-URL:</td>
				<td class="title">Clicks (per day)</td>
				<td class="title">Is displayed?</td>
				<td class="title">Actions:</td>
			</tr>
			<tr bgcolor="#EEEEEE">
				<td colspan="5"></td>
			</tr>';
	$qry=safe_query("SELECT * FROM ".PREFIX."bannerrotation ORDER BY bannerID");
	$anz=mysql_num_rows($qry);
	if($anz) {
		while($ds = mysql_fetch_array($qry)) {
			$ds[displayed]==1 ? $displayed='<font color="green"><b>yes</b></font>' : $displayed='<font color="red"><b>no</b></font>';
			
			if(eregi('http://', $ds[bannerurl])) $bannerurl='<a href="'.$ds[bannerurl].'" target="_blank">'.$ds[bannerurl].'</a>';
			else $bannerurl='<a href="http://'.$ds[bannerurl].'" target="_blank">'.$ds[bannerurl].'</a>';
			
			$days=round((time()-$ds[date])/(60*60*24));
			if($days) $perday=round($ds[hits]/$days,2);
			else $perday=$ds[hits];

			echo'
				<tr bgcolor="#FFFFFF">
					<td>'.$ds[bannername].'</td>
					<td>'.$bannerurl.'</td>
					<td>'.$ds[hits].' ('.$perday.')</td>
					<td>'.$displayed.'</td>
					<td>
				   <input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=bannerrotation&action=edit&bannerID='.$ds[bannerID].'\');return document.MM_returnValue" value="edit">
				   <input type="button" class="button" onClick="MM_confirm(\'really delete this country?\', \'admincenter.php?site=bannerrotation&delete=true&bannerID='.$ds[bannerID].'\')" value="delete"></td>
					</td>
				</tr>
			';
		}
	} else echo'<tr><td colspan="4">no entries</td></tr>';
	echo '</table>';
}
?>
