<?php
$protect = "yes";
$plugin_name = "Affiliates";
$plugin_url = "admin.php?view=load_plugin&plugin=affiliates";
$plugin_version = "1.2";

$apage = basename($_SERVER['PHP_SELF']);
$siteurl = "http://".$_SERVER['HTTP_HOST'].str_replace($apage, "", $_SERVER['PHP_SELF']);
$url = $_GET['url'];

if ($_GET['check'] == "status") {
echo 1;
}

if ($module == "install_".$url) {
$tot = 0;
if (mysql_num_rows(mysql_query("SELECT * FROM ".$pre."plugins WHERE name = '".$plugin_name."'")) > 1) {
echo "<b>".$plugin_name."</b> Plugin already installed. <a href='admin.php?view=install_plugins'>Return</a>";
} else {
$data .= "Beginning to install the <b>".$plugin_name."</b> Plugin...<br /><br />";
$query1 = mysql_query("INSERT INTO ".$pre."fielddata VALUES (null, 'AdaptCMS', 'http://www.adaptcms.com|yes|http://www.adaptcms.com/button.png|1|".time()."|0', 'plugin_affiliates')");
$query2 = mysql_query("INSERT INTO ".$pre."fielddata VALUES (null, 'Insane Visions', 'http://www.insanevisions.com|yes||1|".time()."|0', 'plugin_affiliates')");
$query3 = mysql_query("INSERT INTO ".$pre."templates VALUES (null, 'plugin_".$plugin_name."', '{affiliate}<br>', '".time()."')");
$query4 = mysql_query("INSERT INTO ".$pre."plugins VALUES (null, '".$plugin_name."', '".$_GET['url']."', '".$plugin_version."', 'On')");
$query5 = mysql_query("INSERT INTO ".$pre."settings VALUES (null, 'affiliate_email', 'If there is an email entered, you will be notified when someone applies to be an affiliate.', 'webmaster@".$_SERVER['HTTP_HOST']."', 'setting', 'Modules')");
if ($query1 == TRUE) {
$tot = $tot + 1;
$data .= "`".$pre."fielddata` MySQL data row #1 Inserted? <font color='green'>True</font><br />";
} else {
$data .= "`".$pre."fielddata` MySQL data row #1 Inserted? <font color='red'>False</font><br />";
}
if ($query2 == TRUE) {
$tot = $tot + 1;
$data .= "`".$pre."fielddata` MySQL data row #2 Inserted? <font color='green'>True</font><br />";
} else {
$data .= "`".$pre."fielddata` MySQL data row #2 Inserted? <font color='red'>False</font><br />";
}
if ($query3 == TRUE) {
$tot = $tot + 1;
$data .= "`".$pre."templates` MySQL data row Inserted? <font color='green'>True</font><br />";
} else {
$data .= "`".$pre."templates` MySQL data row Inserted? <font color='red'>False</font><br />";
}
if ($query4 == TRUE) {
$tot = $tot + 1;
$data .= "`".$pre."plugins` MySQL data row Inserted? <font color='green'>True</font><br />";
} else {
$data .= "`".$pre."plugins` MySQL data row Inserted? <font color='red'>False</font><br />";
}
if ($query5 == TRUE) {
$tot = $tot + 1;
$data .= "`".$pre."settings` MySQL data row Inserted? <font color='green'>True</font><br />";
} else {
$data .= "`".$pre."settings` MySQL data row Inserted? <font color='red'>False</font><br />";
}
if ($tot == 5) {
$data .= "<br /><b>".$plugin_name."</b> Plugin installed <font color='green'>Sucessfully!</font>. <a href='".$plugin_url."'>".$plugin_name." Plugin</a>";
echo $data;
} else {
$data .= "<br /><b>".$plugin_name."</b> Plugin installed <font color='red'>Un-Sucessfully!</font>. Please check mysql settings and if need be, please submit a <a href='admin.php?view=support'>support ticket</a> or <a href='javascript:awindow(\"http://www.adaptcms.com/s2smessages.php?domain=".$domain."&license_number=".$license_number."\", \"\", \"width=600,height=400,scroll=yes,toolbar=no,directories=no,status=no,menubar=no,resizable=yes\")'>S2S messages</a> ticket.";
echo $data;
}
}
}

if ($module == "uninstall_".$url) {
$tot = 0;
if (mysql_num_rows(mysql_query("SELECT * FROM ".$pre."plugins WHERE name = '".$plugin_name."'")) == 0) {
echo "<b>".$plugin_name."</b> Plugin already un-installed, or not yet installed. <a href='admin.php?view=install_plugins'>Return</a>";
} else {
$data .= "Beginning to un-install the <b>".$plugin_name."</b> Plugin...<br /><br />";
$query1 = mysql_query("DELETE FROM ".$pre."fielddata WHERE aid = 'plugin_affiliates'");
$query2 = mysql_query("DELETE FROM ".$pre."plugins WHERE name = '".$plugin_name."'");
$query3 = mysql_query("DELETE FROM ".$pre."settings WHERE name = 'affiliate_email'");
$query4 = mysql_query("DELETE FROM ".$pre."templates WHERE name = 'plugin_".$plugin_name."'");
if ($query1 == TRUE) {
$tot = $tot + 1;
$data .= "`".$pre."fielddata` MySQL data row(s) Deleted? <font color='green'>True</font><br />";
} else {
$data .= "`".$pre."fielddata` MySQL data row(s) Deleted? <font color='red'>False</font><br />";
}
if ($query2 == TRUE) {
$tot = $tot + 1;
$data .= "`".$pre."plugins` MySQL data row Deleted? <font color='green'>True</font><br />";
} else {
$data .= "`".$pre."plugins` MySQL data row Deleted? <font color='red'>False</font><br />";
}
if ($query3 == TRUE) {
$tot = $tot + 1;
$data .= "`".$pre."settings` MySQL data row Deleted? <font color='green'>True</font><br />";
} else {
$data .= "`".$pre."settings` MySQL data row Deleted? <font color='red'>False</font><br />";
}
if ($query4 == TRUE) {
$tot = $tot + 1;
$data .= "`".$pre."templates` MySQL data row Deleted? <font color='green'>True</font><br />";
} else {
$data .= "`".$pre."templates` MySQL data row Deleted? <font color='red'>False</font><br />";
}
if ($tot == 4) {
$data .= "<br /><b>".$plugin_name."</b> Plugin un-installed <font color='green'>Sucessfully!</font>. <a href='admin.php?view=plugins'>Plugins</a>";
echo $data;
} else {
$data .= "<br /><b>".$plugin_name."</b> Plugin un-installed <font color='red'>Un-Sucessfully!</font>. Please check mysql settings and if need be, please submit a <a href='admin.php?view=support'>support ticket</a> or <a href='javascript:awindow(\"http://www.adaptcms.com/s2smessages.php?domain=".$domain."&license_number=".$license_number."\", \"\", \"width=600,height=400,scroll=yes,toolbar=no,directories=no,status=no,menubar=no,resizable=yes\")'>S2S messages</a> ticket.";
echo $data;
}
}
}

if (basename($_SERVER['PHP_SELF']) == "admin.php") {
if ($module == "") {
echo "<b>Directory</b>&nbsp;&nbsp;-&nbsp;&nbsp;Plugins / ".ucwords($_GET['plugin'])." Plugin";
if ($p[0]) {
echo "- <a href='".$plugin_url."&module=add'>Add Affiliate</a>";
}
echo " - <a href='".$plugin_url."&module=code'>Get Code</a><br /><br /><br />";
echo "<table cellpadding='5' cellspacing='0' border='0' width='490' align='left'><tr><td><b>Site Name</b></td><td><b>Clicks</b></td><td><b>Status</b></td><td><b>Date</b></td><td><b>Actions</b></td></tr>";

if(!isset($_GET['page'])){
    $page = 1;
} else {
    $page = $_GET['page'];
}

$from = (($page * $setting["admin_limit"]) - $setting["admin_limit"]);

$sql = mysql_query("SELECT * FROM ".$pre."fielddata WHERE aid = 'plugin_affiliates' ORDER BY `id` DESC LIMIT $from, ".$setting["admin_limit"]);
while($rs = mysql_fetch_array($sql)) {
$r = explode("|", $rs[data]);
echo "<tr><td><a href='".$r[0]."' target='popup'>".stripslashes($rs[fname])."</a></td><td>".$r[5]."</td><td>";
if ($r[3] == 1) {
echo "<font color='blue'>Verified</font>";
} else {
echo "<a href='".$plugin_url."&module=verify&id=".urlencode($rs[id])."'><font color='red'>Not Verified</font></a>";
}
echo "</td><td>".date($setting["date_format"], $r[4])."</td>";
if ($p[1]) {
echo "<td><a href='".$plugin_url."&module=edit&id=".urlencode($rs[id])."'>Edit</a> | ";
}
if ($p[2]) {
echo "<a href='".$plugin_url."&module=delete&id=".urlencode($rs[id])."'>Delete</a>";
}
if ($r[3] == 0) {
echo "| <a href='".$plugin_url."&module=verify&id=".urlencode($rs[id])."'>Ver</a></td></tr>";
}
}
echo "</table>";
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."fielddata WHERE aid = 'plugin_affiliates'"),0);
$total_pages = ceil($total_results / $setting["admin_limit"]);

if ($total_pages > "1") {
echo "<center>";

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

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i&nbsp;";
        } else {
            echo "<a href=\"".$plugin_url."&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=\"".$plugin_url."&page=$next\">Next>></a>";
}
echo "</center>";
}
}

if ($module == "code") {
if ((($p[0]) && ($p[1]) && ($p[2]))) {
echo "<b>Directory</b>&nbsp;&nbsp;-&nbsp;&nbsp;Plugins / ".ucwords($_GET['plugin'])." Plugin - <a href='".$plugin_url."'>Manage Affiliates</a>- <a href='".$plugin_url."&module=add'>Add Affiliate</a><br /><br /><br />";
echo "<table cellpadding='5' cellspacing='0' border='0' width='490' align='left'><tr><td><b>List Affiliates</b></td><td><textarea style='width:260px; height:50px; font-family: tahoma;font-size: 11px;color: #4a4a4a; font-weight: bold' rows='1' readonly='readonly'>&lt;?php echo affiliates(\"5\"); ?&gt;</textarea></td></tr></table>";
}
}

if ($module == "add") {
if ($p[0]) {
echo "<b>Directory</b>&nbsp;&nbsp;-&nbsp;&nbsp;Plugins / ".ucwords($_GET['plugin'])." Plugin - <a href='".$plugin_url."'>Manage Affiliates</a><br /><br /><br />";
echo "<form action='".$plugin_url."&module=add2' method='post'><table cellpadding='5' cellspacing='0' border='0' width='490' align='left'><tr><td>Site Name</td><td><input type='text' name='sitename' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444'></td></tr><tr><td>Site URL</td><td><input type='text' name='siteurl' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444'></td></tr><tr><td>Track Clicks?</td><td><select name='target' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444'>";
if ($r[target] == "yes") {
echo "<option value='yes' selected>Yes</option><option value='no'>No</option>";
} else {
echo "<option value='no' selected>No</option><option value='yes'>Yes</option>";
}
echo "</select></td></tr><tr><td>Image URL</td><td><input type='text' name='imageurl' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444'>&nbsp;&nbsp;(optional)</td></tr><tr><td><input type='submit' value='Add Affiliate' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444'></td></tr></table></form>";
}
}

if ($module == "add2") {
if ($p[0]) {
$query = @mysql_query("INSERT INTO ".$pre."fielddata VALUES (null, '".addslashes($_POST["sitename"])."', '".addslashes($_POST["siteurl"])."|".addslashes($_POST["target"])."|".addslashes($_POST["imageurl"])."|1|".time()."|0', 'plugin_affiliates')");

if ($query == TRUE) {
echo re_direct("1500", $plugin_url);
echo "The affiliate <b>".stripslashes($_POST['sitename'])."</b> has been added. <a href='".$plugin_url."'>Return</a>";
} else {
echo reporterror($cpage, htmlspecialchars(mysql_error()), $domain);
echo "The affiliate could not be added. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}
}

if ($module == "edit") {
if ($p[1]) {
echo "<b>Directory</b>&nbsp;&nbsp;-&nbsp;&nbsp;Plugins / ".ucwords($_GET['plugin'])." Plugin - <a href='".$plugin_url."'>Manage Affiliates</a>";
if ($p[0]) {
echo "- <a href='".$plugin_url."&module=add'>Add Affiliate</a>";
}
echo "<br /><br /><br />";
$rs = mysql_fetch_row(mysql_query("SELECT fname,data FROM ".$pre."fielddata WHERE id = '".$_GET['id']."'"));
$r = explode("|", $rs[1]);
echo "<form action='".$plugin_url."&module=edit2' method='post'><input type='hidden' name='id' value='".$_GET['id']."'><input type='hidden' name='clicks' value='".$r[5]."'><table cellpadding='5' cellspacing='0' border='0' width='490' align='left'><tr><td>Site Name</td><td><input type='text' name='sitename' value='".$rs[0]."' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444'></td></tr><tr><td>Site URL</td><td><input type='text' name='siteurl' value='".$r[0]."' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444'></td></tr><tr><td>Track Clicks?</td><td><select name='target' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444'>";
if ($r[1] == "yes") {
echo "<option value='yes' selected>Yes</option><option value='no'>No</option>";
} else {
echo "<option value='no' selected>No</option><option value='yes'>Yes</option>";
}
echo "</select></td></tr><tr><td>Image URL</td><td><input type='text' name='imageurl' value='".$r[2]."' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444'>&nbsp;&nbsp;(optional)</td></tr><tr><td><input type='submit' value='Update Affiliate' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444'></td></tr></table></form>";
}
}

if ($module == "edit2") {
if ($p[1]) {
$query = @mysql_query("UPDATE ".$pre."fielddata SET fname = '".addslashes($_POST["sitename"])."', data = '".addslashes($_POST["siteurl"])."|".addslashes($_POST["target"])."|".addslashes($_POST["imageurl"])."|1|".time()."|".$_POST['clicks']."' WHERE id = '".$_POST['id']."'");

if ($query == TRUE) {
echo re_direct("1500", $plugin_url);
echo "The affiliate <b>".stripslashes($_POST['sitename'])."</b> has been updated. <a href='".$plugin_url."'>Return</a>";
} else {
echo reporterror($cpage, htmlspecialchars(mysql_error()), $domain);
echo "The affiliate could not be updated. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}
}

if ($module == "delete") {
if ($p[2]) {
echo '<SCRIPT LANGUAGE="JavaScript">
var agree=confirm("Are you sure you want to delete this affiliate?");
if (agree)
document.write("");
else
history.go(-1);
</SCRIPT>';
$query = @mysql_query("DELETE FROM ".$pre."fielddata WHERE id = '".addslashes($_GET["id"])."'");
mysql_query("ALTER TABLE ".$pre."fielddata  AUTO_INCREMENT =".$_GET['id']);

if ($query == TRUE) {
echo re_direct("1500", $plugin_url);
echo "The affiliate has been deleted. <a href='".$plugin_url."'>Return</a>";
} else {
echo reporterror($cpage, htmlspecialchars(mysql_error()), $domain);
echo "The affiliate could not be deleted. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}
}

if ($module == "verify") {
echo '<SCRIPT LANGUAGE="JavaScript">
var agree=confirm("Are you sure you want to verify this affiliate?");
if (agree)
document.write("");
else
history.go(-1);
</SCRIPT>';
$rs = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE id = '".$_GET['id']."'"));
$r = explode("|", $rs[0]);
$query = @mysql_query("UPDATE ".$pre."fielddata SET data = '".$r[0]."|".$r[1]."|".$r[2]."|1|".$r[4]."|".$r[5]."' WHERE id = '".addslashes($_GET["id"])."'");

if ($query == TRUE) {
echo re_direct("1500", $plugin_url);
echo "The affiliate has been verified. <a href='".$plugin_url."'>Return</a>";
} else {
echo reporterror($cpage, htmlspecialchars(mysql_error()), $domain);
echo "The affiliate could not be verified. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}
}

if (basename($_SERVER['PHP_SELF']) == "index.php") {
if ($module == "af_apply") {
session_start();
?>
<script language="JavaScript"><!--
ts = <?php echo time(); ?>;
--></script>
<?php
eval (" ?> ".stripslashes(layout("header"))." <?php ");
echo "<title>Affiliation Application - ".$setting["sitename"]."</title><form action='".$siteurl."index.php?view=af_apply2' method='post'><table cellspacing='0' cellpadding='3' border='0' align='center'><tr><td>Site Name</td><td><input type='text' name='sitename' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444'></td></tr><tr><td>Site URL</td><td><input type='text' name='siteurl' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444'></td></tr><tr><td>Image URL</td><td><input type='text' name='imageurl' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444'>&nbsp;&nbsp;(optional)</td></tr><tr><td><img id='captcha_img' src='".$siteurl."includes/captcha.php?id=".time()."' style='border:1px solid #000000' /> <a href='no_matter' onclick='document.getElementById(\"captcha_img\").src = \"".$siteurl."includes/captcha.php?id=\" + ++ts; return false'><img src='".$siteurl."includes/refresh.png' border='0'></a></td><td><input type='text' name='captcha' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444' size='10'></td></tr><tr><td>&nbsp;</td><td><input type='submit' value='Submit Website' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444'></td></tr></table></form>";
}

if ($module == "af_apply2") {
session_start();
eval (" ?> ".stripslashes(layout("header"))." <?php ");
if (($_POST['sitename'] == "") or ($_POST['siteurl'] == "")) {
echo "Sorry but the <b>Site Name</b> and/or <b>Site URL</b> field have not been entered with data, please go back and fill in both of those fields.";
} else {
if (md5(strtoupper($_POST['captcha'])) != $_SESSION['captcha']) {
echo "Sorry, but you entered the wrong code for the captcha. Go back and try again";
} else {
if ($setting["affiliate_email"]) {
mail($setting["affiliate_email"], $setting["sitename"]." - Affiliate Application", "Hello,\r\nThe website '".check($_POST['sitename'])."' has applied to become an affiliate. Here are details below:\r\n\r\nSite URL: ".check($_POST['siteurl'])."\r\nManage Affiliates: ".$siteurl."admin.php?view=load_plugin&plugin=affiliates\r\n\r\nThank you.");
}

$query = @mysql_query("INSERT INTO ".$pre."fielddata VALUES (null, '".check(addslashes($_POST["sitename"]))."', '".check($_POST["siteurl"])."|yes|".check($_POST["imageurl"])."||".time()."|0', 'plugin_affiliates')");


if ($query == TRUE) {
echo re_direct("1500", $mrw["affiliates"]);
echo "The website <b>".stripslashes($_POST['sitename'])."</b> has been submitted. <a href='".$mrw["affiliates"]."'>Return</a>";
} else {
echo reporterror($cpage, htmlspecialchars(mysql_error()), $domain);
echo "The website could not be submitted. This error has been sent to the <b>AdaptCMS</b> support team and the webmaster will be contacted shortly.";
}
}
}
}

if ($module == "affiliates") {
$temp = @mysql_fetch_row(@mysql_query("SELECT template FROM ".$pre."templates WHERE name = 'plugin_affiliates'"));

if ($_GET['limit'] == "") {
$sql = mysql_query("SELECT * FROM ".$pre."fielddata WHERE aid = 'plugin_affiliates' ORDER BY `id` DESC");
} else {
$sql = mysql_query("SELECT * FROM ".$pre."fielddata WHERE aid = 'plugin_affiliates' ORDER BY `id` DESC LIMIT ".$_GET['limit']);
}
while($rs = mysql_fetch_array($sql)) {
$pab = "";$rab = "";
$r = explode("|", $rs[data]);
if ($r[3] == 1) {
$pab[0] = "{affiliate}";
$pab[1] = "{date}";

$url = "";
if ($r[1] == "no") {
$url = $r[0];
} else {
$url = $siteurl."index.php?view=af_click&id=".$rs[id];
}
if ($r[2]) {
$rab[0] = "<a href='".$url."' target=popup'><img src='".$r[2]."' border='0' style='border:1px solid black'></a>";
} else {
$rab[0] = "<a href='".$url."' target='popup'>".$rs[fname]."</a>";
}
$rab[1] = date($setting["date_format"], $r[4]);

echo str_replace($pab, $rab, stripslashes($temp[0]));
}
}
echo "<br><center>[ <a href='".$mrw['af_apply']."'><b>Apply for Affiliation</b></a> ]</center>";
}

if ($module == "af_click" && is_numeric($_GET['id'])) {
$rs = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE id = '".$_GET['id']."'"));
$r = explode("|", $rs[0]);
$r[5] = $r[5] + 1;
mysql_query("UPDATE ".$pre."fielddata SET data = '".$r[0]."|".$r[1]."|".$r[2]."|".$r[3]."|".$r[4]."|".$r[5]."' WHERE id = '".$_GET['id']."'");

header("location: ".$r[0]);
}
}
?>