<?php
$z = "b";
include ("../config.php");
if (((($_GET['load'] == "help") or ($_GET['load'] == "chat") or ($_GET['load'] == "login") or ($_GET['load'] == "logout")))) {
if (($_GET['load'] == "help") && ($_GET['id'] == "list3")) {
eval (" ?>" . admin_header() . " <?php ");
}
} else {
eval (" ?>" . admin_header() . " <?php ");
echo '<SCRIPT LANGUAGE="JavaScript">var checkflag = "false";function check(field) {if (checkflag == "false") {for (i = 0; i < field.length; i++) {field[i].checked = true;}checkflag = "true";return "Uncheck All"; }else {for (i = 0; i < field.length; i++) {field[i].checked = false; }checkflag = "false";return "Check All"; }}</script>';
}
if ($ipbancheck3 == "0") {if ($numv == "0"){
	if ($warn == $naum) {
	echo "Looks Like Everything Checks Out, Go Ahea....Wait, You Don't Have Access to the Admin Panel!";
} else {

if(!isset($_GET['page'])){
    $page = 1;
} else {
    $page = $_GET['page'];
}
}
}
}
$from = (($page * $setting[max_results]) - $setting[max_results]);

if ((($userlevel == "3") or ($userlevel == "4") or ($userlevel == "5"))) {
	echo "Sorry $username, but you do not have permission to manage affiliates. You are only a $level.";
} else {

echo "<center><a href='affiliates.php?load=af&view=add'>Add Affiliate</a> | <a href='affiliates.php?load=af'>Manage Affiliates</a></center><br><br>";
}

if (($_GET['view'] == "manage") && ($_POST['ver'])) {
echo '<SCRIPT LANGUAGE="JavaScript">
var agree=confirm("Are you sure you want to verify this affiliate(s)?");
if (agree)
document.write("");
else
history.go(-1);
</SCRIPT>';

while (list(, $vala) = each ($_POST['ver'])) {
	$deletea = mysql_query("UPDATE ".$pre."af SET verified = 'yes' WHERE id = '".$vala."'") or die("<font color='red'><b>A fatal MySQL error occured</b></font>.<br><br><b>Query:</b> ".$deletea."<br><b>Error:</b> (".mysql_errno().")".mysql_error());
}
if ($deletea == TRUE) {
echo re_direct("1500", "af.php?load=af");
echo "The affiliate(s) have been verified. <a href=\"affiliates.php?load=af\">Return to AF Manager Home</a>";
}
}

if ($_GET['view'] == "") {
echo "<title>OneCMS Admin Panel > Affiliate Manager</title><form action='affiliates.php?load=af&view=manage' name='form1' method='post'><table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" width=\"500\" align=\"center\"><tr><td><b>Site Name</b></td><td><b>Verified?</b></td><td><b><b>Edit</b></td><td><b><b>Verify</b></td><td><b>Delete</b></td></tr>";

$query="SELECT * FROM ".$pre."af ORDER BY `id` DESC LIMIT $from, $setting[max_results]";
	$result=mysql_query($query);
	while($row = mysql_fetch_array($result)) {
		$id = "$row[id]";
		$name2 = "$row[sitename]";
		$name = stripslashes($name2);
    	echo "<tr><td><a href='index.php?view=af_click&id=".$row[id]."' target='popup'>$name</a></td><td>$row[verified]</td><td><input type=\"checkbox\" name=\"id[]\" value=\"$id\"></td><td><input type=\"checkbox\" name=\"ver[]\" value=\"$id\"></td><td><input type=\"checkbox\" name=\"delete[]\" value=\"$id\"></td></tr>";
    }

echo "<tr><td><div align='right'><input type='submit' name='submit' value='Submit'></td><td><input type=button value='Check All' onClick='this.value=check(this.form)'></td></td></tr></form></table><br><br>";
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."af"),0);

$total_pages = ceil($total_results / $setting[max_results]);
if ($total_pages > 1) {
echo "<center>";

// Build Previous Link
if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"affiliates.php?load=af&page=$prev\"><<Previous</a>&nbsp;";
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i&nbsp;";
        } else {
            echo "<a href=\"affiliates.php?load=af&page=$i\">$i</a>&nbsp;";if (($i/25) == (int)($i/25)) {echo "<br>";}
    }
}

// Build Next Link
if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"affiliates.php?load=af&page=$next\">Next>></a>";
}
echo "</center>";
}
}

if (($_GET['view'] == "manage") && ($_POST['id'])) {
if ($_POST['submitok'] == "") {
echo "<form action='affiliates.php?load=af&view=manage2' name='form1' method='post'><table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" align=\"left\">";

 while (list(, $val) = each ($_POST['id'])) {
$query="SELECT * FROM ".$pre."af WHERE id = '$val'";
	$result=mysql_query($query);
	while($row = mysql_fetch_array($result)) {
		$sitename = "$row[sitename]";
		$siteurl = "$row[siteurl]";
		$where = "$row[where2]";
		$type = "$row[type]";
		$width = "$row[width]";
		$height = "$row[height]";
		$ss = "$row[ss]";

echo "<tr><td><b><center>Affiliate # ".$val."</b></center></td></tr><tr><td>Site Name</td><td><input type=\"text\" name=\"sitename_$val\" value=\"$sitename\"></td></tr><tr><td>Site URL</td><td><input type=\"text\" name=\"siteurl_$val\" value=\"$siteurl\"></td></tr><tr><td>Open link in new window?</td><td><select name=\"where_$val\"><option value=\"$where\">";
if ($where == "popup") {
echo "- Yes";
} else {
echo "- No";
}
echo "</option><option value=\"popup\">Yes</option><option value=\"_blank\">No</option></select></td></tr><tr><td>Type of Affiliation</td><td><select name=\"type_$val\"><option value=\"$type\">";
if ($type == "link") {
echo "- Text Link";
} else {
echo "- Button";
}
echo "</option><option value=\"link\">Text Link</option><option value=\"button\">Button</option></select></td></tr><tr><td><input type=\"hidden\" name=\"id[]\" value=\"$val\"><b>If type of affiliation is button:</b></td></tr><tr><td>Width of Button</td><td><input type=\"text\" name=\"width_$val\" value=\"$width\"></td></tr><tr><td>Height of Button</td><td><input type=\"text\" name=\"height_$val\" value=\"$height\"></td></tr><tr><td>Button URL</td><td><input type=\"text\" name=\"ss_$val\" value=\"$ss\"></td></tr>";
}
}
echo "<tr><td><input type=\"submit\" value=\"Edit\"></td></tr></table>";
}
}

if (($_GET['view'] == "manage2") && ($_POST['delete'] == "")) {
$date = date("F j, Y");

while (list(, $val) = each ($_POST['id'])) {
if ($_POST["type_$val"] == "button") {
$r = mysql_query("UPDATE ".$pre."af SET sitename = '".$_POST["sitename_$val"]."', siteurl = '".$_POST["siteurl_$val"]."', where2 = '".$_POST["where_$val"]."', type = '".$_POST["type_$val"]."', width = '".$_POST["width_$val"]."', height = '".$_POST["height_$val"]."', ss = '".$_POST["ss_$val"]."', date = '".time()."' WHERE id = '".$val."'") or die("<font color='red'><b>A fatal MySQL error occured</b></font>.<br><br><b>Query:</b> ".$r."<br><b>Error:</b> (".mysql_errno().")".mysql_error());
} else {
$r = mysql_query("UPDATE ".$pre."af SET sitename = '".$_POST["sitename_$val"]."', siteurl = '".$_POST["siteurl_$val"]."', where2 = '".$_POST["where_$val"]."', type = '".$_POST["type_$val"]."', date = '".time()."' WHERE id = '".$val."'") or die("<font color='red'><b>A fatal MySQL error occured</b></font>.<br><br><b>Query:</b> ".$r."<br><b>Error:</b> (".mysql_errno().")".mysql_error());
}
}

if ($r == TRUE) {
echo re_direct("1500", "affiliates.php?load=af");
echo "The affiliate(s) has been updated.<br><a href=\"affiliates.php?load=af\">AF Manager Home</a>";
}
}

if (($_GET['view'] == "manage") && ($_POST['delete'])) {
echo '<SCRIPT LANGUAGE="JavaScript">
var agree=confirm("Confirm Deletion?");
if (agree)
document.write("");
else
history.go(-1);
</SCRIPT>';

while (list(, $val) = each ($_POST['delete'])) {
$delete = mysql_query("DELETE FROM ".$pre."af WHERE id = '$val'") or die("<font color='red'><b>A fatal MySQL error occured</b></font>.<br><br><b>Query:</b> ".$delete."<br><b>Error:</b> (".mysql_errno().")".mysql_error());
}
if ($delete == TRUE) {
echo re_direct("1500", "affiliates.php?load=af");
echo "The affiliate(s) have been deleted. <a href=\"affiliates.php?load=af\">Return to AF Manager Home</a>";
}
}

if (($_GET['view'] == "add") && ($_GET['type'] == "")) {
echo '<center><a href="affiliates.php?load=af&type=link&view=add">Link</a><br><a href="affiliates.php?load=af&type=button&view=add">Button</a></center>';
}

if (($_GET['type'] == "link") && ($_GET['view'] == "add")) {
echo '<form action="affiliates.php?load=af&view=add2" method="POST"><table cellspacing="0" cellpadding="2" border="0"><tr><td>Site Name</td><td><input type="text" name="sitename"></td></tr><tr><td>Site URL</td><td><input type="text" name="siteurl" value="http://"></td></tr><tr><td>Open link in new window?</td><td><select name="where"><option value="popup">Yes</option><option value="_blank">No</option></select></td></tr><input type="hidden" name="type" value="link"><tr><td><input type="submit" name="submit" value="Proceed"></td></tr></table></form>';
}

if (($_GET['type'] == "button") && ($_GET['view'] == "add")) {
echo '<form action="admin.php?load=af&view=add3" method="POST" enctype="multipart/form-data"><table cellspacing="0" cellpadding="2" border="0"><tr><td>Site Name</td><td><input type="text" name="sitename"></td></tr><tr><td>Site URL</td><td><input type="text" name="siteurl" value="http://"></td></tr><tr><td>Open link in new window?</td><td><select name="where"><option value="popup">Yes</option><option value="_blank">No</option></select></td></tr><input type="hidden" name="type" value="button"><tr><td>Width of Button</td><td><input type="text" name="width" value="88"></td></tr><tr><td>Height of Button</td><td><input type="text" name="height" value="31"></td></tr><tr><td>Upload Button</td><td><input type="file" name="ss"></td></tr><tr><td><i>Or...</i> Provide the URL of the button here</td><td><input type="text" name="ss2" value="http://"></td></tr><tr><td><input type="submit" name="submit2" value="Finish"></td></tr></table></form>';
}

if ($_GET['view'] == "add2") {
$verified = "yes";
$date = date("F j, Y");
$sql = mysql_query("INSERT INTO ".$pre."af VALUES (null, '".$_POST['sitename']."', '".$_POST['siteurl']."', '".$_POST['where']."', '".$_POST['type']."', '', '', '', '$verified', '".time()."', '0')") or die(mysql_error());

if ($sql == TRUE) {
echo $_POST['sitename']." has been added to the list of your affiliates. <a href='admin.php?load=af'>AF Manager Home</a>";
} else {
echo "Sorry, but ".$_POST['sitename']." could not be added to the list of your affiliates.";
}
}

if ($_GET['view'] == "add3") {
if (($_POST['ss'] == "") && ($_POST['ss2'] = "")) {
echo 'Hey, go back!';
} else {
if ($_POST['ss2'] == "") {
copy ($_FILES["ss"]["tmp_name"], "$path".$_FILES["ss"]["name"]);

$ss3 = $_FILES["ss"]["name"];
$sql = mysql_query("INSERT INTO ".$pre."af VALUES (null, '".$_POST['sitename']."', '".$_POST['siteurl']."', '".$_POST['where']."', 'button', '".$_POST['width']."', '".$_POST['height']."', '$ss3', 'yes', '".time()."', '0')") or die(mysql_error());
if ($sql == TRUE) {
echo $_POST['sitename']." has been added to the list of your affiliates. <a href='affiliates.php?load=af'>AF Manager Home</a>";
} else {
echo "Sorry, but the affiliate could not be added to the list of your affiliates.";
}
} else {
$sql = mysql_query("INSERT INTO ".$pre."af VALUES (null, '".$_POST['sitename']."', '".$_POST['siteurl']."', '".$_POST['where']."', 'button', '".$_POST['width']."', '".$_POST['height']."', '".$_POST['ss2']."', 'yes', '".time()."', '0')") or die(mysql_error());
if ($sql == TRUE) {
echo $_POST['sitename']." has been added to the list of your affiliates. <a href='affiliates.php?load=af'>AF Manager Home</a>";
} else {
echo "Sorry, but ".$_POST['sitename']." could not be added to the list of your affiliates.";
}
}
}
}
if (($_GET['load'] == "help") or ($_GET['load'] == "chat")) {
if (($_GET['load'] == "help") && ($_GET['id'] == "list3")) {
eval (" ?>" . admin_footer() . " <?php ");
}
} else {
eval (" ?>" . admin_footer() . " <?php ");
}
?>