<?php
$protect = "yes";
$plugin_name = "TinyURL";
$plugin_url = "admin.php?view=load_plugin&plugin=tinyurl";
$plugin_version = "1.0";

$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'>echo</a>";
} else {
$data .= "Beginning to install the <b>".$plugin_name."</b> Plugin...<br /><br />";
$query1 = mysql_query("INSERT INTO ".$pre."plugins VALUES (null, '".$plugin_name."', '".$_GET['url']."', '".$plugin_version."', 'On')");
if ($query1 == 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 ($tot == 1) {
$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_lite.php?domain=".$domain."\", \"\", \"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."'")) > 1) {
echo "<b>".$plugin_name."</b> Plugin already un-installed, or not yet installed. <a href='admin.php?view=install_plugins'>echo</a>";
} else {
$data .= "Beginning to un-install the <b>".$plugin_name."</b> Plugin...<br /><br />";
$query1 = mysql_query("DELETE FROM ".$pre."plugins WHERE name = '".$plugin_name."'");
if ($query1 == 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 ($tot == 1) {
$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_lite.php?domain=".$domain."\", \"\", \"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 / ".$plugin_name." Plugin";
if ($p[0]) {
echo " - <a href='admin.php?view=load_plugin&plugin=tinyurl&module=add'>Add Redirect</a>";
}
echo "<br /><br /><br />";

echo "<table cellpadding='5' cellspacing='0' border='0' width='490' align='left'><tr><td><b>URL</b></td><td><b>Domain</b></td><td><b>Actions</b></td></tr>";

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

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

$sql = mysql_query("SELECT * FROM ".$pre."fielddata WHERE fname = 'plugin_tinyurl' ORDER BY `id` DESC LIMIT $from, ".$setting["limit"]);
while($r = mysql_fetch_array($sql)) {
echo "<tr><td><a href='".$siteurl."re-direct/".$r[id]."/' target='popup'>".$r[data]."</a></td><td><a href='".$r[data]."' target='popup'>".check_domain($r[data])."</a></td><td>";
if ($p[1] or $p[2]) {
echo "<a href='".$plugin_url."&module=delete&id=".urlencode($r[id])."'>Delete</a> | <a href='".$plugin_url."&module=code&id=".urlencode($r[id])."'>Code</a>";
}
echo "</td></tr>";
}
echo "</table>";
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."fielddata WHERE aid = 'plugin_tinyurl'"),0);
$total_pages = ceil($total_results / $setting["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 == "add") {
if ($p[0]) {
echo "<b>Directory</b>&nbsp;&nbsp;-&nbsp;&nbsp;Plugins / ".$plugin_name." Plugin - <a href='admin.php?view=load_plugin&plugin=tinyurl'>Manage URLs</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><b>URL Redirect</b></td><td><input type='text' name='url' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444'></td></tr><tr><td><input type='submit' value='Add URL' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444'></td></tr></table></form>";
}
}

if ($module == "code") {
if ($p[0]) {
$r = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE id = '".$_GET['id']."'"));
echo '<SCRIPT LANGUAGE="JavaScript">
<!-- Source:  http://dblast.cjb.net -->
function copyit(theField) {
var tempval=eval("document."+theField)
tempval.focus()
tempval.select()
therange=tempval.createTextRange()
therange.execCommand("Copy")
}
</script>';
echo "<b>Directory</b>&nbsp;&nbsp;-&nbsp;&nbsp;Plugins / ".$plugin_name." Plugin - <a href='admin.php?view=load_plugin&plugin=tinyurl'>Manage URLs</a><br /><br /><br />";
echo "<form name='form'><table cellpadding='5' cellspacing='0' border='0' width='490' align='left'><tr><td><b>Link Code</b></td><td><textarea name='code' style='width:300px; height:75px; font-family: tahoma;font-size: 11px;color: #4a4a4a; font-weight: bold' rows='5' readonly='readonly'>&lt;a href='".$siteurl."re-direct/".$_GET['id']."/'&gt;Click Here&lt/&gt;</textarea></td><td><input onclick='copyit(\"form.code\")' type='button' value='Select Code' name='cpy'></td></tr><tr><td><b>URL Code</b></td><td><a href='".$siteurl."re-direct/".$_GET['id']."/'>".$siteurl."re-direct/".$_GET['id']."/</a></td></tr></table></form>";
}
}

if ($module == "add2") {
if ($p[0]) {
echo "<b>Directory</b>&nbsp;&nbsp;-&nbsp;&nbsp;Plugins / ".$plugin_name." Plugin - <a href='admin.php?view=load_plugin&plugin=tinyurl'>Manage URLs</a><br /><br /><br />";

$query1 = mysql_query("INSERT INTO ".$pre."fielddata VALUES (null, 'plugin_tinyurl', '".addslashes($_POST['url'])."', '0')");

if ($query1 == TRUE) {
echo re_direct("1500", $plugin_url);
echo "The url redirect has been added. <a href='".$plugin_url."'>Return</a>";
} else {
echo reporterror($cpage, htmlspecialchars(mysql_error()), $domain);
echo "The redirect could not be added. 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 url?");
if (agree)
document.write("");
else
history.go(-1);
</SCRIPT>';

$query1 = mysql_query("DELETE FROM ".$pre."fielddata WHERE id = '".$_GET["id"]."'");

if ($query1 == TRUE) {
echo re_direct("1500", $plugin_url);
echo "The redirect has been deleted. <a href='".$plugin_url."'>Return</a>";
} else {
echo reporterror($cpage, htmlspecialchars(mysql_error()), $domain);
echo "The redirect could not be deleted. 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 == "re-direct") {
$r = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE id = '".$_GET['id']."'"));
mysql_query("UPDATE ".$pre."fielddata SET aid=aid+1 WHERE id = '".$_GET['id']."'");

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