<?php
$protect = "yes";
include ("config.php");
$admin_page = "";

if ((($_GET['view'] == "logout") or ($_GET['view'] == "login") or ($_GET['view'] == "login2"))) {
$admin_page = "yes";
if ($_GET['view'] == "login") {
session_start();
?>
<script language="JavaScript"><!--
ts = <?php echo time(); ?>;
--></script>
<?php
eval (" ?> ".stripslashes(layout("header"))." <?php ");
echo "<title>".$setting["sitename"]." - Login</title><a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;Login / Form - <a href='".$mrw_1["register"]."register'>Signup</a><br /><br />";
if ($_COOKIE[adaptcms_username]) {
echo "<center>Hello <b>".$_COOKIE[adaptcms_username]."</b>! Please enter your password to enter the administration area.</center><br />";
}
echo "<form action='admin.php?view=login2' method='post'><table><tr><td>Username</td><td><input type='text' name='username' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' size='16' value='".$_COOKIE[adaptcms_username]."'></td></tr><tr><td>Password</td><td><input type='password' name='password' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' size='16'></td></tr><tr><td><img id='captcha_img' src='includes/captcha.php?id=".time()."' style='border:1px solid #000000' /> <a href='no_matter' onclick='document.getElementById(\"captcha_img\").src = \"includes/captcha.php?id=\" + ++ts; return false'><img src='includes/refresh.png' border='0'></a></td><td><input type='text' name='captcha' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' size='10'></td></tr><tr><td><input type='submit' value='Login' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr></table></form>";
}
if ($_GET['view'] == "logout" && $_SESSION[adaptcms_username]) {
$_SESSION[adaptcms_username] = "";
$_SESSION[adaptcms_password] = "";

eval (" ?> ".admin_header()." <?php ");
$title = str_replace("_", " ", $_GET['view']);
echo "<title>".$setting["sitename"]." - ".ucwords($title)."</title>";
echo re_direct("1500", "index.php");
echo "<title>".$setting["sitename"]." - Logout</title>You are now logged out. <a href='index.php'>Home Page</a>";
}

if ($_GET['view'] == "login2") {
session_start();

if (md5(strtoupper($_POST['captcha'])) != $_SESSION['captcha']) {
eval (" ?> ".stripslashes(layout("header"))." <?php ");
echo "Sorry, but you entered the wrong code for the captcha. Go back and try again";
} else {
$prf = sprintf("SELECT * FROM ".$pre."users WHERE username = '%s' AND password = '%s' LIMIT 1",
mysql_real_escape_string(strip_tags($_POST['username'])),
mysql_real_escape_string(strip_tags(md5($_POST['password']))));

$login_check = mysql_num_rows(mysql_query($prf));

if ($login_check == "1") {
$_SESSION[adaptcms_username] = strip_tags($_POST['username']);
$_SESSION[adaptcms_password] = strip_tags(md5($_POST['password']));
if ($_COOKIE[adaptcms_username] == "" or $_COOKIE[adaptcms_password] == "") {
setcookie("adaptcms_username", strip_tags($_POST['username']), time()+60*60*24*30);
setcookie("adaptcms_password", strip_tags(md5($_POST['password'])), time()+60*60*24*30);
}

eval (" ?> ".stripslashes(layout("header"))." <?php ");
echo re_direct("1500", "admin.php");
echo "Welcome back ".strip_tags($_POST[username])."! Enjoy your stay. <a href='admin.php'>Return</a>";
} else {
eval (" ?> ".stripslashes(layout("header"))." <?php ");
echo "Sorry but that username and password combo is incorrect. Please try again.";
}
}

}
} else {
// ADMIN ACTUAL START
eval (" ?> ".admin_header()." <?php ");

if ($_GET['view'] == "") {
echo "<title>".$setting["sitename"]." - Admin Panel</title>";
} else {
$title = str_replace("_", " ", $_GET['view']);
echo "<title>".$setting["sitename"]." - ".ucwords($title)."</title>";
}

if ($_COOKIE['adaptcms_skin']) {
$skin = $_COOKIE['adaptcms_skin'];
} else {
$skin = "skin1";
}

if ($_GET['view'] == "file_releases") {
$admin_page = "yes";

echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=file_releases'>File Release System</a><br />";

if ($_POST['check'] == "") {
$files = explode(",", file_get_contents("http://www.adaptcms.com/latest_files_lite.txt"));
while (list(, $p) = each ($files)) {
$file[$p."_time"] = @date("U",filemtime($p));
$file[$p."_size"] = @filesize($p);
$array .= "&file_".$p."_time=".@date("U",filemtime($p));
$array .= "&file_".$p."_size=".@filesize($p);
}
echo file_get_contents("http://www.adaptcms.com/upgrade_lite.php?sitename=".urlencode($setting['sitename'])."&siteurl=".urlencode($siteurl)."&version=".urlencode($version)."&domain=".$domain.$array);
} else {
$var = "http://www.adaptcms.com/a/files/xx3twmzrny7szuczthq/";

echo "<p>You have selected files to be copied over to your website. Starting now...</p>";
while (list(, $i) = each ($_POST['check'])) {
$file1 = file_get_contents($var.$i."s");
if (strlen($file1) == 0) {
echo "The file <b>".$i."</b> could not be retrieved from the AdaptCMS website, please try again later.<br />";
} else {
$handle = fopen($sitepath.$i, 'w');
fwrite($handle, $file1);
fclose($handle);
if ($handle == TRUE) {
echo "The file <b>".$i."</b> has been successfully copied to your website.<br />";
} else {
echo "The file <b>".$i."</b> <font color='red'><u>could not</u></font> be copied to your website.<br />";
}
}
}
echo "<br />All file(s) have been copied over, return to the <a href='admin.php'>admin panel</a>.";
}
}

if ($_GET['view'] == "") {
$admin_page = "yes";
$setting['sitename'] = str_replace(" ", "_", $setting['sitename']);
echo file_get_contents("http://www.adaptcms.com/updates_lite.php?sitename=".urlencode($setting['sitename'])."&siteurl=".urlencode($siteurl)."&version=".urlencode($version)."&domain=".$domain);

echo "<br /><br /><table cellpadding='5' cellspacing='0' border='0' width='480' align='center'><tr><td width='300' valign='top'><table cellpadding='3' cellspacing='1' border='0'><tr><td><u><b>Most Active Articles</b></u></td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>";

$lev = mysql_fetch_row(mysql_query("SELECT value FROM ".$pre."levels WHERE name = '".$level."'"));
if ($lev[0] == "2") {
$perm = " WHERE username = '".mysql_real_escape_string(stripslashes($_SESSION[adaptcms_username]))."'";
}

$sql = mysql_query("SELECT * FROM ".$pre."articles".$perm." ORDER BY `views` DESC, `date` DESC LIMIT 5");
while($r = mysql_fetch_array($sql)) {
$p = "";
$p = mysql_fetch_row(mysql_query("SELECT padd,pedit FROM ".$pre."permissions WHERE level = '".$level."' AND name = '".$r[section]."'"));
$art = 1;
echo "<tr><td><a href='".$mrw_1["content"].fthrough($r[name], $r[section], $r[id])."'>".stripslashes($r[name])."</a></td><td>".ucwords($r[section])."</td><td><b>".number_format($r[views])."</b> views</td><td>";

if ($p[1] or $p[0] && $r[username] == $_SESSION[adaptcms_username]) {
echo "<a href='admin.php?view=edit_article&id=".$r[id]."&cat=".$r[section]."'>Edit</a><br /><a href='admin.php?view=delete_article&id=".$r[id]."&cat=".$r[section]."' onclick='return confirmDelete();'>Delete</a>";
}
if ($r[ver] == "1" && $p[1]) {
echo " | <a href='admin.php?view=verarticle&id=".$r[id]."'>Ver</a>";
}
echo "</td></tr>";
}
if ($art == "") {
echo "<tr><td align='center'>No Articles Added</td></tr>";
}
echo "</table></td><td width='150' valign='top' align='right'><table cellpadding='5' cellspacing='1' border='0'><tr><td><u><b>Miscellaneous</b></u></td></tr>";
$num1 = @mysql_num_rows(@mysql_query("SELECT * FROM ".$pre."articles"));
$users = @mysql_num_rows(@mysql_query("SELECT * FROM ".$pre."users"));
$files = @mysql_num_rows(@mysql_query("SELECT * FROM ".$pre."files"));
$polls = @mysql_num_rows(@mysql_query("SELECT * FROM ".$pre."polls WHERE type = 'poll'"));
$com = @mysql_num_rows(@mysql_query("SELECT * FROM ".$pre."comments WHERE aid > 0"));
echo "<tr><td><b>Users Online:</b></td><td>".online("", 60)."</td></tr><tr><td><b>Guests Online:</b></td><td>".online("guests", 15)."</td></tr><tr><td><a href='admin.php?view=articles'>Articles:</a></td><td>".$num1."</td></tr><tr><td><a href='admin.php?view=comments'>Comments:</a></td><td>".$com."</td></tr><tr><td><a href='admin.php?view=files'>Files:</a></td><td>".$files."</td></tr><tr><td><a href='admin.php?view=users'>Members:</a></td><td>".$users."</td></tr><tr><td><a href='admin.php?view=polls'>Polls:</a></td><td>".$polls."</td></tr>";

echo "</table></td></tr></table><br />";
echo "<table width='480' align='center' cellpadding='1' cellspacing='0' border='0'><tr><td>";
echo @file_get_contents("http://www.adaptcms.com/rating_lite.php?domain=".$domain."&version=".urlencode($version));
echo "</td></tr></table>";
}

$p = mysql_fetch_row(mysql_query("SELECT padd,pedit,pdelete FROM ".$pre."permissions WHERE level = '".$level."' AND name = 'users'"));
if ($p[0] && $p[1] && $p[2]) {
if ($_GET['view'] == "permissions") {
$admin_page = "yes";
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;Permissions / Manage Permissions - <a href='admin.php?view=add_level'>Add User Level</a><br /><br /><br />";
echo "<table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td><b>ID #</b></td><td><b>Level Name</b></td><td><b>Value</b></td><td><b># of Users</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."levels ORDER BY `id` DESC LIMIT $from, ".$setting["admin_limit"]);
while($r = mysql_fetch_array($sql)) {
$count = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."users WHERE level = '".$r[name]."'"));
echo "<tr><td>".$r[id]."</td><td><a href='admin.php?view=edit_permissions&id=".$r[name]."'>".stripslashes($r[name])."</a></td><td>".$r[value]."</td><td>".$count."</td></tr>";
}
echo "</table><br clear='all'>";
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."levels"),0);
$total_permissions = ceil($total_results / $setting["admin_limit"]);

if ($total_permissions > "1") {

echo "<center>";


if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"admin.php?view=permissions&page=$prev\"><<Previous</a>&nbsp;";
}

for($i = 1; $i <= $total_permissions; $i++){
    if(($page) == $i){
        echo "$i&nbsp;";
        } else {
            echo "<a href=\"admin.php?view=permissions&page=$i\">$i</a>&nbsp;";
			if (($i/25) == (int)($i/25)) {echo "<br />";}
    }
}


if($page < $total_permissions){
    $next = ($page + 1);
    echo "<a href=\"admin.php?view=permissions&page=$next\">Next>></a>";
}
echo "</center>";
}
echo "<tr><td><br /><br /><small>Permissions are set per user level. So adding a user and setting him to 'Staff' (or assinging a user) for example, would give him the permissions that the 'Staff' user level has.</small></td></tr>";
}

if ($_GET['view'] == "edit_permissions") {
$admin_page = "yes";
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=permissions'>Permissions</a> / Edit Permissions<br /><br /><br />";

echo "<form action='admin.php?view=edit_permissions2' method='post'><table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td><b>Level Name</b></td><td>".$_GET['id']."<input type='hidden' name='level' value='".$_GET['id']."'</td></tr><tr><td><b>Permissions</b></td><td><table cellpadding='2' cellspacing='0' border='0'>";
$array = array("fields","files","gallery","pages","plugins","polls","sections","templates","users");
while (list(, $i) = each ($array)) {
$count = "";
$count = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."permissions WHERE level = '".$_GET['id']."' AND name = '".$i."'"));
if ($count == 1) {
$q = mysql_fetch_row(mysql_query("SELECT padd,pedit,pdelete,id FROM ".$pre."permissions WHERE level = '".$_GET['id']."' AND name = '".$i."'"));
}
if ($count == 2) {
mysql_query("DELETE FROM ".$pre."permissions WHERE level = '".$_GET['id']."' AND name = '".$i."' LIMIT 1");
$q = mysql_fetch_row(mysql_query("SELECT padd,pedit,pdelete,id FROM ".$pre."permissions WHERE level = '".$_GET['id']."' AND name = '".$i."'"));
}

echo "<input type='hidden' name='count_".$i."' value='".$count."'><input type='hidden' name='id[]' value='".$i."'><tr><td>";

echo ucwords($i)."</td><td><input type='checkbox' name='add_".$i."' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' value='1'";
if ($q[0] == "1") {
echo " checked";
}
echo "></td>";
if ($i == "files") {
echo "<td>&nbsp;</td>";
} else {
echo "<td><input type='checkbox' name='edit_".$i."' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' value='1'";
if ($q[1] == "1") {
echo " checked";
}
echo "></td>";
}
if ($i == "templates") {
} else {
echo "<td><input type='checkbox' name='delete_".$i."' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' value='1'";
if ($q[2] == "1") {
echo " checked";
}
echo "></td>";
}
echo "</tr><input type='hidden' name='theid_".$i."' value='".$q[3]."'>";
}

echo "<tr><td><br /></td></tr>";

$sql = mysql_query("SELECT * FROM ".$pre."sections ORDER BY `id` DESC");
while($r = mysql_fetch_array($sql)) {
$count = "";
$count = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."permissions WHERE level = '".$_GET['id']."' AND name = '".$r[name]."'"));
if ($count == 1) {
$q = mysql_fetch_row(mysql_query("SELECT padd,pedit,pdelete,id FROM ".$pre."permissions WHERE level = '".$_GET['id']."' AND name = '".$r[name]."'"));
}
if ($count == 2) {
mysql_query("DELETE FROM ".$pre."permissions WHERE level = '".$_GET['id']."' AND name = '".$r[name]."' LIMIT 1");
$q = mysql_fetch_row(mysql_query("SELECT padd,pedit,pdelete,id FROM ".$pre."permissions WHERE level = '".$_GET['id']."' AND name = '".$r[name]."'"));
}

echo "<input type='hidden' name='count_".$r[name]."' value='".$count."'><input type='hidden' name='id[]' value='".$r[name]."'><tr><td>";

echo $r[name]."</td><td><input type='checkbox' name='add_".$r[name]."' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' value='1'";
if ($q[0] == "1") {
echo " checked";
}
echo "></td><td><input type='checkbox' name='edit_".$r[name]."' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' value='1'";
if ($q[1] == "1") {
echo " checked";
}
echo "></td><td><input type='checkbox' name='delete_".$r[name]."' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' value='1'";
if ($q[2] == "1") {
echo " checked";
}
echo "></td></tr><input type='hidden' name='theid_".$r[name]."' value='".$q[3]."'>";
}
echo "</table></td><td align='center'>Checkboxes - add/edit/delete<br /><br />For articles, setting it to only add will allow user to add and edit/delete there own articles</td></tr><tr><td><input type='submit' value='Update Permissions' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr></table></form>";
}

if ($_GET['view'] == "edit_permissions2") {
$admin_page = "yes";
while (@list(, $i) = @each ($_POST['id'])) {

if ($_POST["count_$i"] == "0") {
@mysql_query("INSERT INTO ".$pre."permissions VALUES (null, '".$_POST["level"]."', '".$i."', '".$_POST["add_$i"]."', '".$_POST["edit_$i"]."', '".$_POST["delete_$i"]."')");
} else {
@mysql_query("UPDATE ".$pre."permissions SET padd = '".$_POST["add_$i"]."', pedit = '".$_POST["edit_$i"]."', pdelete = '".$_POST["delete_$i"]."' WHERE id = '".$_POST["theid_$i"]."'");
}
}

echo re_direct("1500", "admin.php?view=permissions");
echo "The permissions have been updated. <a href='admin.php?view=permissions'>Return</a>";
}
}
$p = "";
$p = mysql_fetch_row(mysql_query("SELECT padd,pdelete FROM ".$pre."permissions WHERE level = '".$level."' AND name = 'files'"));
if ($p[0]) {
if ($_GET['view'] == "files") {
$admin_page = "yes";
echo '<script language="javascript">function jump(targ,selObj,restore){
eval(targ+".location=\'"+selObj.options[selObj.selectedIndex].value+"\'");
if (restore) selObj.selectedIndex=0;
}</script>';

echo "<form name='form1'><a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;Files / Manage Files - <a href='admin.php?view=upload";
if ($_GET['gallery']) {
echo "&gallery=".$_GET['gallery'];
}
echo "'>Upload File(s)</a>";
if ($_GET['gallery']) {
$gal = " WHERE gallery = '".$_GET['gallery']."'";
}
echo " - <a href='admin.php?view=mass_upload";
if ($_GET['gallery']) {
echo "&gallery=".$_GET['gallery'];
}
echo "'>Mass Upload</a>&nbsp;&nbsp;&nbsp;&nbsp;Gallery: <select name='gallery' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' onChange=\"jump('parent',this,0)\"><option value='' selected></option>";
$sqlst = mysql_query("SELECT * FROM ".$pre."gallery ORDER BY `name` ASC");
while($y = mysql_fetch_array($sqlst)) {
echo "<option value='admin.php?view=files&gallery=".$y[id]."'>".ucwords($y[name])."</option>";
}
echo "</select></form><br />";

echo "<table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td><b>ID #</b></td><td><b>File Name</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."files".$gal." ORDER BY `id` DESC LIMIT $from, ".$setting["admin_limit"]);
while($r = mysql_fetch_array($sql)) {
echo "<tr><td>".$r[id]."</td><td><a href='".$r[filedir].$r[filename]."'>";
if (strlen($r[filename]) > 40) {
echo substr(urldecode($r[filename]), 0, 37)."...";
} else {
echo substr(urldecode($r[filename]), 0, 40);
}
echo "</a></td><td>".date($setting["date_format"], $r[date])."</td><td><a href='admin.php?view=delete_file&id=".$r[id]."' onclick='return confirmDelete();'>Delete</a></td></tr>";
}
echo "</table><br clear='all'>";
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."files".$gal),0);
$total_pages = ceil($total_results / $setting["admin_limit"]);

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

if($levels > 1){
    $prev = ($levels - 1);
    echo "<a href=\"admin.php?view=files";
if ($_GET['gallery']) {
echo "&gallery=".$_GET['gallery'];
}
	echo "&page=$prev\"><<Previous</a>&nbsp;";
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i&nbsp;";
        } else {
            echo "<a href=\"admin.php?view=files";
if ($_GET['gallery']) {
echo "&gallery=".$_GET['gallery'];
}
			echo "&page=$i\">$i</a>&nbsp;";
			if (($i/25) == (int)($i/25)) {echo "<br />";}
    }
}

if($levels < $total_pages){
    $next = ($levels + 1);
    echo "<a href=\"admin.php?view=files";
if ($_GET['gallery']) {
echo "&gallery=".$_GET['gallery'];
}
	echo "&page=$next\">Next>></a>";
}
echo "</center>";
}
echo "<tr><td><br /><br /><small>Mass Upload is a feature where you can specify a folder to upload, the script then links to all the files in that folder.</small></td></tr>";
}

if ($_GET['view'] == "upload") {
$admin_page = "yes";
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;Files / <a href='admin.php?view=files'>Manage Files</a> - Upload File(s)";
if ($_GET['gallery']) {
echo " - <a href='admin.php?view=files&gallery=".$_GET['gallery']."'>Gallery</a>";
}
echo " - <a href='admin.php?view=mass_upload";
if ($_GET['gallery']) {
echo "&gallery=".$_GET['gallery'];
}
echo "'>Mass Upload</a><br /><br /><br />";

if ($_GET['gallery']) {
echo "<form action='admin.php?view=upload&gallery=".$_GET['gallery']."' method='post'>";
} else {
echo "<form action='admin.php?view=upload' method='post'>";
}

echo "<table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td>How many files to Upload?&nbsp;&nbsp;<input type='text' name='amount' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'>&nbsp;&nbsp;<input type='submit' value='Update' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'><br /><br /></td></tr></table></form>";

if ($_POST['amount']) {
if ($_GET['gallery']) {
echo "<form name='form1' method='post' action='admin.php?view=upload2&gallery=".$_GET['gallery']."' enctype='multipart/form-data'>";
} else {
echo "<form name='form1' method='post' action='admin.php?view=upload2' enctype='multipart/form-data'>";
}
echo "<table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'>";

for ($i = 1; $i <= $_POST['amount']; $i = $i + 1) {
echo "<tr><td>File #".$i."&nbsp;&nbsp;<input type='file' name='ss_".$i."' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'>&nbsp;&nbsp;...<i>or</i> link to a file&nbsp;&nbsp;<input type='text' name='ss2_".$i."' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'>&nbsp;&nbsp;&nbsp;&nbsp;Watermark <input type='checkbox' name='watermark_".$i."'>&nbsp;&nbsp;Re-Size <input type='text' name='resize1_".$i."' size='2' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'> / <input type='text' name='resize2_".$i."' size='2' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr>";
}
echo "<input type='hidden' name='amount' value='".$_POST['amount']."'><tr><td><input type='submit' value='Upload File";
if ($_POST['much'] > "1") {
echo "s";
}
echo "' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr></table></form>";
}
}

if ($_GET['view'] == "mass_upload") {
$admin_page = "yes";
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=files'>Manage Files</a> / Upload File(s)";
if ($_GET['gallery']) {
echo " - <a href='admin.php?view=files&gallery=".$_GET['gallery']."'>Gallery</a>";
}
echo "<br /><br /><br />";

if ($_GET['gallery']) {
echo "<form name='form1' method='post' action='admin.php?view=mass_upload2&gallery=".$_GET['gallery']."'>";
} else {
echo "<form name='form1' method='post' action='admin.php?view=mass_upload2'>";
}

echo "<tr><td>Path to Folder (no trailing slash) <input type='text' name='dir' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' value='".$sitepath."upload'></td></tr><tr><td>URL to folder (no trailing slash) <input type='text' name='url' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' value='http://".$_SERVER['HTTP_HOST'];
if (basename($_SERVER['PHP_SELF'], "admin.php")) {
echo "/".basename($_SERVER['PHP_SELF'], "admin.php");
}
echo "/upload'></td></tr><tr><td><input type='submit' value='Upload Files' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr></table></form>";
}

if ($_GET['view'] == "upload2") {
$admin_page = "yes";

for ($i = 1; $i <= $_POST['amount']; $i = $i + 1) {
if ($_FILES["ss_$i"]["name"]) {
if ($_POST["watermark_$i"]) {
copy ($_FILES["ss_$i"]["tmp_name"], $sitepath."upload/".$_FILES["ss_$i"]["name"]);
createThumbs($sitepath."upload/",$sitepath."upload/thumbs/", $_FILES["ss_$i"]["name"]);

$ex = explode(".", $_FILES["ss_$i"]["name"]);
$filet = $ex[0]."_wm.".$ex[1];
watermark($sitepath."upload/".$_FILES["ss_$i"]["name"], $sitepath."upload/watermark.png", $sitepath."upload/".$_FILES["ss_$i"]["name"]);

$filename = $_FILES["ss_$i"]["name"];
$filedir = "upload/";
} else {
copy ($_FILES["ss_$i"]["tmp_name"], $sitepath."upload/".$_FILES["ss_$i"]["name"]);
$filename = $_FILES["ss_$i"]["name"];
$filedir = "upload/";
createThumbs("upload/","upload/thumbs/", $filename);
}
} else {
$filename = basename($_POST["ss2_$i"]);
$filedir = str_replace($filename, "", $_POST["ss2_$i"]);
}
$query = @mysql_query("INSERT INTO ".$pre."files VALUES (null, '".$filename."', '".$filedir."', '".$_GET['gallery']."', '0|0', '".time()."')");

if ($_POST["resize1_$i"] && $_POST["resize2_$i"]) {
$src = imagecreatefromjpeg($filedir.$filename);
list($width,$height)=getimagesize($filedir.$filename);

$max_width = $_POST["resize1_$i"];
$max_height = $_POST["resize2_$i"];

$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;

    $tn_width = $max_width;
    $tn_height = $max_height;

$tmp=imagecreatetruecolor($tn_width,$tn_height);
imagecopyresampled($tmp,$src,0,0,0,0,$tn_width, $tn_height,$width,$height);

imagejpeg($tmp,$filedir.$filename,100);
imagedestroy($src);
imagedestroy($tmp);
}
}

if ($query == TRUE) {
if ($_GET['gallery']) {
echo re_direct("1500", "admin.php?view=files&gallery=".$_GET['gallery']);
} else {
echo re_direct("1500", "admin.php?view=files");
}
echo "The file(s) have been uploaded. <a href='admin.php?view=files";
if ($_GET['gallery']) {
echo "&gallery=".$_GET['gallery'];
}
echo "'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The file(s) could not be uploaded. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}

if ($_GET['view'] == "mass_upload2") {
$admin_page = "yes";

$mass = 0;
$dir = dir($_POST["dir"]); 
while($entry = $dir->read()) { 
if (($entry!= ".") && ($entry!= "..")) { 
$filename = $entry;
$mass = $mass + 1;
$query = @mysql_query("INSERT INTO ".$pre."files VALUES (null, '".$entry."', '".$_POST["url"]."/', '".$_GET['gallery']."', '0|0', '".time()."')");
}
}

if ($mass > 0) {
if ($_GET['gallery']) {
echo re_direct("1500", "admin.php?view=files&gallery=".$_GET['gallery']);
} else {
echo re_direct("1500", "admin.php?view=files");
}
echo "The file(s) have been uploaded. <a href='admin.php?view=files";
if ($_GET['gallery']) {
echo "&gallery=".$_GET['gallery'];
}
echo "'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The file(s) could not be uploaded. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}
}

if ($p[1]) {
if ($_GET['view'] == "delete_file") {
$admin_page = "yes";

$r = mysql_fetch_row(mysql_query("SELECT filename,filedir FROM ".$pre."files WHERE id = '".$_GET['id']."'"));

if (@file_exists($sitepath.$r[1].$r[0])) {
@unlink($sitepath.$r[1].$r[0]);
@unlink($sitepath.$r[1]."thumbs/".$r[0]);
}

$query = @mysql_query("DELETE FROM ".$pre."files WHERE id = '".addslashes($_GET["id"])."'");
mysql_query("ALTER TABLE ".$pre."files  AUTO_INCREMENT =".$_GET['id']);

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=files");
echo "The file has been deleted. <a href='admin.php?view=files'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The file could not be deleted. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}
}
$p = "";
$p = mysql_fetch_row(mysql_query("SELECT padd,pedit,pdelete FROM ".$pre."permissions WHERE level = '".$level."' AND name = 'users'"));
if ($p[0] && $p[1] && $p[2]) {
if ($_GET['view'] == "levels") {
$admin_page = "yes";
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;Level / Manage Level - <a href='admin.php?view=add_level'>Add Level</a><br /><br /><br />";
echo "<table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td><b>ID #</b></td><td><b>Level Name</b></td><td><b># of Users</b></td><td><b>Value</b></td><td><b>Default?</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."levels ORDER BY `id` DESC LIMIT $from, ".$setting["admin_limit"]);
while($r = mysql_fetch_array($sql)) {
$count = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."users WHERE level = '".$r[name]."'"));
echo "<tr><td>".$r[id]."</td><td><a href='admin.php?view=edit_permissions&id=".$r[name]."'>".stripslashes($r[name])."</a></td><td>".$count."</td><td>".$r[value]."</td><td>";
if ($r['options']) {
echo "Yes";
} else {
echo "No";
}
echo "</td><td><a href='admin.php?view=edit_level&id=".$r[id]."'>Edit</a> | <a href='admin.php?view=delete_level&id=".$r[id]."' onclick='return confirmDelete();'>Delete</a></td></tr>";
}
echo "</table><br clear='all'>";
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."levels"),0);
$total_pages = ceil($total_results / $setting["admin_limit"]);

if ($total_pages > "1") {

echo "<center>";


if($levels > 1){
    $prev = ($levels - 1);
    echo "<a href=\"admin.php?view=levels&page=$prev\"><<Previous</a>&nbsp;";
}

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


if($levels < $total_pages){
    $next = ($levels + 1);
    echo "<a href=\"admin.php?view=levels&page=$next\">Next>></a>";
}
echo "</center>";
}
echo "<tr><td><br /><br /><small>Setting a level value to '1' will give them access to the admin panel. This does not have to do with permissions but is a way to block out non-staff without risking security. Setting it above 1 will dissalow entrance to admin panel. Setting it to '3' would be like a members level whereas 2 would be like staff.</small></td></tr>";
}

if ($_GET['view'] == "add_level") {
$admin_page = "yes";
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=levels'>Level</a> / Add Level<br /><br /><br />";
echo "<form action='admin.php?view=add_level2' method='post'><table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td>Level Name</td><td><input type='text' name='levelname' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr><tr><td>Value</td><td><select name='value' size='3' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'><option value='1'>Level 1 (Administrator)</option><option value='2'>Level 2 (Staff)</option><option value='3'>Level 3 (Member)</option></select></td></tr><tr><td>Default?</td><td><input type='checkbox' name='default' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' value='yes'></td></tr><tr><td><input type='submit' value='Add Level' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr></table></form>";
}

if ($_GET['view'] == "edit_level") {
$admin_page = "yes";
$r = mysql_fetch_row(mysql_query("SELECT name,value,options FROM ".$pre."levels WHERE id = '".$_GET['id']."'"));
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=levels'>Level</a> / Edit Level<br /><br /><br />";
echo "<form action='admin.php?view=edit_level2&id=".$_GET['id']."' method='post'><input type='hidden' name='oldname' value='".$r[0]."'><tr><td>Level Name</td><td><input type='text' name='levelname' size='16' value='".$r[0]."' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr><tr><td>Value</td><td><select name='value' size='3' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'>";
if ($r[1] == 1) {
echo "<option value='1' selected>-- Level 1 (Administrator) --</option><option value='2'>Level 2 (Staff)</option><option value='3'>Level 3 (Member)</option>";
}
if ($r[1] == 2) {
echo "<option value='1'>Level 1 (Administrator)</option><option value='2' selected>-- Level 2 (Staff) --</option><option value='3'>Level 3 (Member)</option>";
}
if ($r[1] == 3) {
echo "<option value='1'>Level 1 (Administrator)</option><option value='2'>Level 2 (Staff)</option><option value='3' selected>-- Level 3 (Member) --</option>";
}
echo "</select></td></tr><tr><td>Default?</td><td><input type='checkbox' name='default' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' value='yes'";
if ($r[2]) {
echo " checked";
}
echo "></td></tr><tr><td><input type='submit' value='Update Level' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr></table></form>";
}

if ($_GET['view'] == "delete_level") {
$admin_page = "yes";

$query = @mysql_query("DELETE FROM ".$pre."levels WHERE id = '".addslashes($_GET["id"])."'");
$fetch = mysql_fetch_row(mysql_query("SELECT name FROM ".$pre."levels ORDER BY `id` ASC"));
$query2 = mysql_query("UPDATE ".$pre."users SET level = '".$fetch[0]."' WHERE level = '".$_GET['name']."'");
$query3 = mysql_query("DELETE FROM ".$pre."permissions WHERE level = '".$_GET['name']."'");
mysql_query("ALTER TABLE ".$pre."levels AUTO_INCREMENT =".$_GET['id']);

if ($query == TRUE && $query2 == TRUE && $query3 == TRUE) {
echo re_direct("1500", "admin.php?view=levels");
echo "The level has been deleted. <a href='admin.php?view=levels'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The level could not be deleted. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}

if ($_GET['view'] == "edit_level2") {
$admin_page = "yes";
$query = @mysql_query("UPDATE ".$pre."levels SET name = '".addslashes($_POST["levelname"])."', value = '".$_POST['value']."', options = '".$_POST['default']."' WHERE id = '".$_GET['id']."'");
if ($_POST['levelname'] == $_POST['oldname']) {
} else {
$query2 = mysql_query("UPDATE ".$pre."users SET level = '".addslashes($_POST['levelname'])."' WHERE level = '".$_POST['oldname']."'");
$query3 = mysql_query("UPDATE ".$pre."permissions SET level = '".addslashes($_POST['levelname'])."' WHERE level = '".$_POST['oldname']."'");
}

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=levels");
echo "The level <b>".stripslashes($_POST['levelname'])."</b> has been updated. <a href='admin.php?view=levels'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The level could not be updated. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}

if ($_GET['view'] == "add_level2") {
$admin_page = "yes";
$query = @mysql_query("INSERT INTO ".$pre."levels VALUES (null, '".addslashes($_POST["levelname"])."', '".$_POST['value']."', '".$_POST['default']."')");
$array = array("fields","files","gallery","pages","plugins","polls","sections","templates","users");
while (list(, $i) = each ($array)) {
if ($_POST['value'] == "1") {
$var2 = "1";
}
mysql_query("INSERT INTO ".$pre."permissions VALUES (null, '".$_POST["levelname"]."', '".$i."', '".$var2."', '".$var2."', '".$var2."')");
}

$sql = mysql_query("SELECT * FROM ".$pre."sections ORDER BY `id` DESC");
while($r = mysql_fetch_array($sql)) {
if ($_POST['value'] < "3") {
$var = "1";
}
if ($_POST['value'] == "1") {
$var2 = "1";
}
mysql_query("INSERT INTO ".$pre."permissions VALUES (null, '".$_POST["levelname"]."', '".$r[name]."', '".$var."', '".$var2."', '".$var2."')");
}

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=levels");
echo "The level <b>".stripslashes($_POST['levelname'])."</b> has been added. <a href='admin.php?view=levels'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The level could not be added. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}
}

if ($_GET['view'] == "tool_mass_email2") {
$admin_page = "yes";
$sub = stripslashes($_POST['subject']);
$msg = stripslashes($_POST['message']);
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: ".$setting["sitename"]." <".$email.">"."\r\n";
$headers .= "Reply-To: ".$email."\r\n";

if ($_POST['receiver'] == "all") {
} else {
$data = " WHERE level = '".$_POST['receiver']."'";
}

$query = mysql_query("SELECT * FROM ".$pre."users".$data." ORDER BY `id` DESC");
while($r = mysql_fetch_array($query)) {
$mail = mail($r[email], $sub, $msg, $headers);
}
if ($mail == TRUE) {
echo re_direct("1500", "admin.php?view=tool_mass_email");
echo "The mass e-mail has been sent out. <a href='admin.php?view=levels'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The mass e-mail could not be sent out. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}

if ($_GET['view'] == "tool_mass_email") {
$admin_page = "yes";
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=tools'>Tools</a> / Mass E-Mail<br /><br /><br />";
echo "<form action='admin.php?view=tool_mass_email2' method='post'><table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" align=\"left\"><tr><td>To</td><td><select name='receiver' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'><option value='all'>All Users</a>";

$sql = mysql_query("SELECT * FROM ".$pre."levels ORDER BY `id` DESC");
while($r = mysql_fetch_array($sql)) {
echo "<option value='".$r[name]."'>".ucwords($r[name])." Level</option>";
}

echo "</select></td></tr><tr><td>Subject</td><td><input type='text' name='subject' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr><tr><td>Message</td><td><textarea name='message' cols='55' rows='12' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></textarea></td></tr><tr><td><input type='submit' value='Send E-Mail' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td><td><input type='reset' value='Reset E-Mail' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr></table></form>";
}

$p = "";
$p = mysql_fetch_row(mysql_query("SELECT padd,pedit,pdelete FROM ".$pre."permissions WHERE level = '".$level."' AND name = 'users'"));

if ($_GET['view'] == "users") {
$admin_page = "yes";
if ((($p[0]) or ($p[1]) or ($p[2]))) {
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;Users / Manage Users";
if ($p[0]) {
echo "- <a href='admin.php?view=add_user'>Add User</a>";
}
echo "<br /><br /><br />";
echo "<table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td><b>Username</b></td><td><b>E-Mail</b></td><td><b>Level</b></td><td><b>Rank</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."users ORDER BY `id` DESC LIMIT $from, ".$setting["admin_limit"]);
while($r = mysql_fetch_array($sql)) {
$color = mysql_fetch_row(mysql_query("SELECT color FROM ".$pre."ranks WHERE name = '".$r[rank]."'"));
echo "<tr><td><a href='".$mrw_1["user"].fthrough($r[username],"","")."'><font color='".$color[0]."'>".stripslashes($r[username])."</font></a></td><td>".$r[email]."</td><td>".$r[level]."</td><td><font color='".$color[0]."'>".$r[rank]."</font></td><td>";
if ($p[1]) {
echo "<a href='admin.php?view=edit_users&id=".$r[id]."'>Edit</a> | ";
}
if ($p[2]) {
echo "<a href='admin.php?view=delete_users&id=".$r[id]."' onclick='return confirmDelete();'>Delete</a>";
}
echo "</td></tr>";
}
echo "</table><br clear='all'>";
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."users"),0);
$total_pages = ceil($total_results / $setting["admin_limit"]);

if ($total_pages > "1") {

echo "<center>";

if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"admin.php?view=users&page=$prev\"><<Previous</a>&nbsp;";
}

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

if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"admin.php?view=users&page=$next\">Next>></a>";
}
echo "</center>";
}
echo "<tr><td><br /><br /><small>Changing the password for a user can also be done in the frontend area (for normal users they can change there own password). When editing a user you can edit there password, e-mail and level.</small></td></tr>";
}
}

if ($_GET['view'] == "add_user") {
$admin_page = "yes";
if ($p[0]) {
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=users'>Users</a> / Add User<br /><br /><br />";
echo "<form action='admin.php?view=add_user2' method='post'><table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td>Username</td><td><input type='text' name='username1' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr><tr><td>Password</td><td><input type='text' name='password1' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr><tr><td>E-Mail</td><td><input type='text' name='email1' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr><tr><td>Level</td><td><select name='level' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'>";
$sql = mysql_query("SELECT * FROM ".$pre."levels ORDER BY `name` ASC");
while($r = mysql_fetch_array($sql)) {
echo "<option value='".$r[name]."'>".ucwords($r[name])." - Level ".$r[value]."</option>";
}
echo "</select></td></tr><tr><td><input type='submit' value='Add User' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr></table></form>";
}
}

if ($_GET['view'] == "edit_users") {
$admin_page = "yes";
if ($p[1]) {
$q = mysql_fetch_row(mysql_query("SELECT username,email,level FROM ".$pre."users WHERE id = '".$_GET['id']."'"));
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=users'>Users</a> / Edit User<br /><br /><br />";
echo "<form action='admin.php?view=edit_users2&id=".$_GET['id']."' method='post'><table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td>Username</td><td><input type='text' name='username1' size='16' value='".$q[0]."' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr><tr><td>New Password?</td><td><input type='text' name='password1' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr><tr><td>E-Mail</td><td><input type='text' name='email1' size='16' value='".$q[1]."' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr><tr><td>Level</td><td><select name='level' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'>";
$sql = mysql_query("SELECT * FROM ".$pre."levels ORDER BY `name` ASC");
while($r = mysql_fetch_array($sql)) {
echo "<option value='".$r[name]."'";
if ($q[2] == $r[name]) {
echo " selected";
}
echo ">".ucwords($r[name])." - Level ".$r[value]."</option>";
}
echo "</select></td></tr><tr><td><input type='submit' value='Update User' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr></table></form>";
}
}

if ($_GET['view'] == "delete_users") {
$admin_page = "yes";
if ($p[2]) {
$query = @mysql_query("DELETE FROM ".$pre."users WHERE id = '".addslashes($_GET["id"])."'");
mysql_query("ALTER TABLE ".$pre."users  AUTO_INCREMENT =".$_GET['id']);

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=users");
echo "The user has been deleted. <a href='admin.php?view=users'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The user could not be deleted. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}
}

if ($_GET['view'] == "edit_users2") {
$admin_page = "yes";
if ($p[1]) {
$query2 .= "UPDATE ".$pre."users SET username = '".addslashes($_POST["username1"])."', email = '".addslashes($_POST["email1"])."', level = '".addslashes($_POST["level"])."'";
if ($_POST['password1']) {
$query2 .= ", password = '".md5($_POST['password1'])."'";
}
$query2 .= " WHERE id = '".$_GET['id']."'";
$query = @mysql_query($query2);

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=users");
echo "The user <b>".stripslashes($_POST['name'])."</b> has been updated. <a href='admin.php?view=users'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The user could not be updated. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}
}

if ($_GET['view'] == "add_user2") {
$admin_page = "yes";
if ($p[0]) {
$rank = mysql_fetch_row(mysql_query("SELECT name FROM ".$pre."ranks ORDER BY `prequired` ASC LIMIT 1"));
$query = @mysql_query("INSERT INTO ".$pre."users VALUES (null, '".addslashes($_POST["username1"])."', '".md5($_POST["password1"])."', '".addslashes($_POST["email1"])."', '".addslashes($_POST["level"])."', '".$rank[0]."', '0', '".time()."')");

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=users");
echo "The user <b>".stripslashes($_POST['name'])."</b> has been added. <a href='admin.php?view=users'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The user could not be added. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}
}

$p = "";
$p = mysql_fetch_row(mysql_query("SELECT padd,pedit,pdelete FROM ".$pre."permissions WHERE level = '".$level."' AND name = 'users'"));
if ($p[0] && $p[1] && $p[2]) {
if ($_GET['view'] == "ranks") {
$admin_page = "yes";
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;Ranks / Manage Ranks - <a href='admin.php?view=add_rank'>Add Rank</a><br /><br /><br />";
echo "<table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td><b>ID #</b></td><td><b>Rank Name</b></td><td><b>Image</b></td><td><b>Points Required</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."ranks ORDER BY `id` DESC LIMIT $from, ".$setting["admin_limit"]);
while($r = mysql_fetch_array($sql)) {
echo "<tr><td>".$r[id]."</td><td><font color='".$r[color]."'>".stripslashes($r[name])."</font></td><td>";
if ($r[image]) {
echo "<img src='".$r[image]."'>";
} else {
echo "No Image";
}
echo "</td><td>".$r[prequired]."</td><td><a href='admin.php?view=edit_ranks&id=".$r[id]."'>Edit</a> | <a href='admin.php?view=delete_ranks&id=".$r[id]."' onclick='return confirmDelete();'>Delete</a></td></tr>";
}
echo "</table><br clear='all'>";
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."ranks"),0);
$total_pages = ceil($total_results / $setting["admin_limit"]);

if ($total_pages > "1") {

echo "<center>";


if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"admin.php?view=ranks&page=$prev\"><<Previous</a>&nbsp;";
}

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


if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"admin.php?view=ranks&page=$next\">Next>></a>";
}
echo "</center>";
}
echo "<br /><br /><small>Points are based on the amount of days you are registered (.5 per day, or 5 per day if signed up for a week or less), amount of articles you have contributed (3 per article verified), 1 per comment posted and .1 per page visited on the website.</small>";
}

if ($_GET['view'] == "add_rank") {
$admin_page = "yes";
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=ranks'>Ranks</a> / Add Rank<br /><br /><br />";
echo "<form action='admin.php?view=add_rank2' method='post'><table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td>Rank Name</td><td><input type='text' name='rankname' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr><tr><td>Color</td><td><input type='text' name='color' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr><tr><td>Image</td><td><input type='text' name='image' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr><tr><td>Points Required</td><td><input type='text' name='prequired' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr><tr><td><input type='submit' value='Add Rank' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr></table></form>";
}

if ($_GET['view'] == "edit_ranks") {
$admin_page = "yes";
$r = mysql_fetch_row(mysql_query("SELECT name,color,image,prequired FROM ".$pre."ranks WHERE id = '".$_GET['id']."'"));
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=ranks'>Ranks</a> / Edit Rank<br /><br /><br />";
echo "<form action='admin.php?view=edit_ranks2' method='post'><input type='hidden' name='id' value='".$_GET['id']."'><tr><td>Rank Name</td><td><input type='text' name='rankname' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' value='".stripslashes($r[0])."'></td></tr><tr><td>Color</td><td><input type='text' name='color' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' value='".stripslashes($r[1])."'></td></tr><tr><td>Image</td><td><input type='text' name='image' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' value='".stripslashes($r[2])."'></td></tr><tr><td>Points Required</td><td><input type='text' name='prequired' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' value='".stripslashes($r[3])."'></td></tr><tr><td><input type='submit' value='Update Rank' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr></table></form>";
}

if ($_GET['view'] == "delete_ranks") {
$admin_page = "yes";
$query = @mysql_query("DELETE FROM ".$pre."ranks WHERE id = '".addslashes($_GET["id"])."'");
mysql_query("ALTER TABLE ".$pre."ranks  AUTO_INCREMENT =".$_GET['id']);

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=ranks");
echo "The rank has been deleted. <a href='admin.php?view=ranks'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The rank could not be deleted. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}

if ($_GET['view'] == "edit_ranks2") {
$admin_page = "yes";
$query = @mysql_query("UPDATE ".$pre."ranks SET name = '".addslashes($_POST["rankname"])."', color = '".addslashes($_POST["color"])."', image = '".addslashes($_POST["image"])."', prequired = '".addslashes($_POST["prequired"])."' WHERE id = '".$_POST['id']."'");

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=ranks");
echo "The rank <b>".stripslashes($_POST['name'])."</b> has been updated. <a href='admin.php?view=ranks'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The rank could not be updated. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}

if ($_GET['view'] == "add_rank2") {
$admin_page = "yes";
$query = @mysql_query("INSERT INTO ".$pre."ranks VALUES (null, '".addslashes($_POST["rankname"])."', '".addslashes($_POST["color"])."', '".addslashes($_POST["image"])."', '".addslashes($_POST["prequired"])."')");

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=ranks");
echo "The rank <b>".stripslashes($_POST['name'])."</b> has been added. <a href='admin.php?view=ranks'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The rank could not be added. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}
}

$p = "";
$p = mysql_fetch_row(mysql_query("SELECT padd,pedit,pdelete FROM ".$pre."permissions WHERE level = '".$level."' AND name = 'gallery'"));
if ($_GET['view'] == "gallery") {
$admin_page = "yes";
if ((($p[0]) or ($p[1]) or ($p[2]))) {
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;Gallery / Manage Gallery";
if ($p[0]) {
echo "- <a href='admin.php?view=add_gallery'>Add Gallery</a>";
}
echo "<br /><br /><br />";
echo "<table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td><b>ID #</b></td><td><b>Gallery Name</b></td><td><b># of Files</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."gallery ORDER BY `id` DESC LIMIT $from, ".$setting["admin_limit"]);
while($r = mysql_fetch_array($sql)) {
$count = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."files WHERE gallery = '".$r[id]."'"));
echo "<tr><td>".$r[id]."</td><td><a href='admin.php?view=upload&gallery=".$r[id]."'>".stripslashes($r[name])."</a></td><td><a href='admin.php?view=upload&gallery=".$r[id]."'>".$count."</a></td><td>";
if ($p[1]) {
echo "<a href='admin.php?view=edit_gallery&id=".$r[id]."&name=".$r[name]."'>Edit</a> | ";
}
if ($p[2]) {
echo "<a href='admin.php?view=delete_gallery&id=".$r[id]."' onclick='return confirmDelete();'>Delete</a>";
}
echo "</td></tr>";
}
echo "</table><br clear='all'>";
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."gallery"),0);
$total_pages = ceil($total_results / $setting["admin_limit"]);

if ($total_pages > "1") {

echo "<center>";


if($gallery > 1){
    $prev = ($gallery - 1);
    echo "<a href=\"admin.php?view=gallery&page=$prev\"><<Previous</a>&nbsp;";
}

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


if($gallery < $total_pages){
    $next = ($gallery + 1);
    echo "<a href=\"admin.php?view=gallery&page=$next\">Next>></a>";
}
echo "</center>";
}
echo "<tr><td><br /><br /><small>Click on the gallery name to add files to it or you can click onto the # of files in the gallery.</small></td></tr>";
}
}

if ($_GET['view'] == "add_gallery") {
$admin_page = "yes";
if ($p[0]) {
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=gallery'>Gallery</a> / Add Gallery<br /><br /><br />";
echo "<form action='admin.php?view=add_gallery2' method='post'><table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td>Gallery Name</td><td><input type='text' name='galleryname' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr><tr><td><input type='submit' value='Add Gallery' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr></table></form>";
}
}

if ($_GET['view'] == "edit_gallery") {
$admin_page = "yes";
if ($p[1]) {
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=gallery'>Gallery</a> / Edit Gallery<br /><br /><br />";
echo "<form action='admin.php?view=edit_gallery2&id=".$_GET['id']."' method='post'><table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td>Gallery Name</td><td><input type='text' name='galleryname' size='16' value='".$_GET['name']."' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr><tr><td><input type='submit' value='Update Gallery' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr></table></form>";
}
}

if ($_GET['view'] == "delete_gallery") {
$admin_page = "yes";
if ($p[2]) {
$query = @mysql_query("DELETE FROM ".$pre."gallery WHERE id = '".addslashes($_GET["id"])."'");
mysql_query("ALTER TABLE ".$pre."gallery  AUTO_INCREMENT =".$_GET['id']);

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=gallery");
echo "The gallery has been deleted. <a href='admin.php?view=gallery'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The gallery could not be deleted. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}
}

if ($_GET['view'] == "edit_gallery2") {
$admin_page = "yes";
if ($p[1]) {
$query = @mysql_query("UPDATE ".$pre."gallery SET name = '".addslashes($_POST["galleryname"])."' WHERE id = '".$_GET['id']."'");

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=gallery");
echo "The gallery <b>".stripslashes($_POST['name'])."</b> has been updated. <a href='admin.php?view=gallery'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The gallery could not be updated. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}
}

if ($_GET['view'] == "add_gallery2") {
$admin_page = "yes";
if ($p[0]) {
$query = @mysql_query("INSERT INTO ".$pre."gallery VALUES (null, '".addslashes($_POST["galleryname"])."')");

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=gallery");
echo "The gallery <b>".stripslashes($_POST['name'])."</b> has been added. <a href='admin.php?view=gallery'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The gallery could not be added. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}
}

$p = "";
$p = mysql_fetch_row(mysql_query("SELECT padd,pedit FROM ".$pre."permissions WHERE level = '".$level."' AND name = 'templates'"));
if (($p[0]) or ($p[1])) {
if ($_GET['view'] == "templates") {
$admin_page = "yes";
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;Templates / Manage Templates";
if ($p[0] or $lev == 1) {
echo " - <a href='admin.php?view=add_template'>Add Template</a>";
}
echo "<br /><br /><br />";
echo "<table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px'><tr><td><b>ID #</b></td><td><b>Template Name</b></td><td><b>Last Edited</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."templates ORDER BY `name` ASC LIMIT $from, ".$setting["admin_limit"]);
while($r = mysql_fetch_array($sql)) {
echo "<tr><td>".$r[id]."</td><td>".ucwords($r[name])."</td><td>".date($setting["date_format"], $r[date])."</td><td><a href='admin.php?view=edit_template&id=".$r[id]."'>Edit</a> | <a href='admin.php?view=del_template&name=".urlencode($r[name])."' onclick='return confirmDelete();'>Delete</a></td></tr>";
}
echo "</table><br clear='all'>";
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."templates"),0);
$total_templates = ceil($total_results / $setting["admin_limit"]);

if ($total_templates > "1") {

echo "<center>";


if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"admin.php?view=templates&page=$prev\"><<Previous</a>&nbsp;";
}

for($i = 1; $i <= $total_templates; $i++){
    if(($page) == $i){
        echo "$i&nbsp;";
        } else {
            echo "<a href=\"admin.php?view=templates&page=$i\">$i</a>&nbsp;";
			if (($i/25) == (int)($i/25)) {echo "<br />";}
    }
}


if($page < $total_templates){
    $next = ($page + 1);
    echo "<a href=\"admin.php?view=templates&page=$next\">Next>></a>";
}
echo "</center>";
}
echo "<tr><td><br /><br /><small>The 'layout' template is where you will edit the layout of your frontend design.</small></td></tr>";
}

if ($_GET['view'] == "del_template") {
$admin_page = "yes";

$sql = mysql_query("SELECT * FROM ".$pre."sections ORDER BY `name` ASC");
while($s = mysql_fetch_array($sql)) {
$name[] = $s[name];
}

while (@list(, $i) = @each ($name)) {
if ($i == html_entity_decode($_GET['name'])) {
$newtemp = "";
}
}

if (html_entity_decode($_GET['name']) == "related_articlelist_start") {
$newtemp = "<ul>";
}
if (html_entity_decode($_GET['name']) == "related_articlelist_entry") {
$newtemp = "<li>{name}</li>";
}
if (html_entity_decode($_GET['name']) == "related_articlelist_end") {
$newtemp = "</ul>";
}
if (html_entity_decode($_GET['name']) == "comments") {
$newtemp = '<br /><table class="newstxt" cellpadding="5" cellspacing="2" border="0" style="border: 2px solid #868585" width="480"><tr><td bgcolor="#868585"> <a href="/user-{author}.html">{author}</a>, {date}</td></tr><tr><td bgcolor="white"> {comment}</td></tr></table><br />';
}
if (html_entity_decode($_GET['name']) == "page") {
$newtemp = "<b>{name}</b><br />\r\n\r\n<p>{page}</p>";
}
if (html_entity_decode($_GET['name']) == "poll_options") {
$newtemp = "{name} {data}<br />";
}
if (html_entity_decode($_GET['name']) == "poll_results") {
$newtemp = "<table cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><b>{question}</b><br />\r\n{options}\r\n<b>Votes:</b> {vote_total}</td></tr></table><br clear='all'>";
}
if (html_entity_decode($_GET['name']) == "poll_vote") {
$newtemp = "<table cellpadding='0' cellspacing='0' border='0' align='right'><tr><td><b>{question}</b></td></tr></table><br clear='all'>
\r\n<table cellpadding='0' cellspacing='0' border='0' align='right'>
\r\n{options}
\r\n<tr><td><a href=\"".$mrw['poll_results']."\">Results</a></td><td>{submit}</td></tr></table><br clear='all'>";
}
if (html_entity_decode($_GET['name']) == "layout") {
$newtemp = '<html>
<head>
<script language=\'javascript\'>function awindow(towhere, newwinname, properties) {window.open(towhere,newwinname,properties);}</script>
<STYLE type=\'text/css\'>body { font-family: tahoma;font-size: 11px;color: #4a4a4a; }td { font-family: tahoma;font-size: 11px;color: #4a4a4a;}a:link {color: #4a4a4a; text-decoration: none;font-weight: bold}a:visited {color: #4a4a4a; text-decoration: none;font-weight: bold} a:active {color: #4a4a4a; text-decoration: none;font-weight: bold} a:hover {text-decoration: none; color: #4a4a4a;font-weight: bold}a:visited:hover {text-decoration: none; color: #4a4a4a;font-weight: bold</style>
</head>
<body>

<table cellpadding=\"0\" cellspacing=\"0\" width=\"757\" border=\"0\" align=\"center\"><tr><td>

<table cellpadding=\"0\" cellspacing=\"0\" width=\"757\" border=\"0\" align=\"center\"><tr><td><img src=\"<?php echo $siteurl."skins/".$skin; ?>/top.png\"></td></tr></table><br />

<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><td align=\"left\" valign=\"top\">

<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><td background=\"<?php echo $siteurl; ?>includes/leftbg.png\" height=\"281\" width=\"168\" align=\"left\" valign=\"top\"><img src=\"<?php echo $siteurl."skins/".$skin; ?>/menu1.png\">

<table cellpadding=\"10\" cellspacing=\"0\" border=\"0\" width=\"center\"><tr><td>
<a href=\"<?php echo $siteurl; ?>index.php\">Home</a>
{sections}<br />
<a href=\"<?php echo $mrw_1[gallery]; ?>\">Gallery</a><br /><br />
{pages}<br /><br />
<?php echo translate(); ?>
</td></tr></table>
</td></tr></table><br />

<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><td background=\"<?php echo $siteurl; ?>includes/leftbg.png\" height=\"281\" width=\"168\" align=\"left\" valign=\"top\"><img src=\"<?php echo $siteurl."skins/".$skin; ?>/menu2.png\">

<table cellpadding=\"10\" cellspacing=\"0\" border=\"0\" width=\"center\"><tr><td>
<?php echo show_poll(\"1\", \"Your impression of AdaptCMS, so far?\"); ?><br clear=\'all\'><b>
<center>
<?php echo affiliates(\"\"); ?>
<br /><br />
<?php
if ($_SESSION[adaptcms_username]) {
//echo \"MEMBERS ONLY CONTENT HERE, REMOVE THE 2 SLASHES TO TEST OUT\";
}
?></center>
</td></tr></table>
</td></tr></table>


</td><td valign=\"top\" width=\"591\" align=\"center\"><table cellpadding=\"1\" cellspacing=\"0\" border=\"0\" width=\"480\"><tr><td align=\"center\">
<?php
if ($_COOKIE[adaptcms_username]) {
?>
Welcome back <a href=\"<?php echo $mrw[profile]; ?>\"><?php echo $_COOKIE[adaptcms_username]; ?></a>. There are <b><?php echo $time; ?></b> users online and <b><?php echo $num1; ?></b> articles at <a href=\'http://www.<?php echo $domain; ?>\'><?php echo $domain; ?></a> - <a href=\'<?php echo $mrw[logout]; ?>logout\'>Logout</a>
<?php
} else {
?>
Welcome <b>Guest</b>. Please <a href=\'<?php echo $mrw[login]; ?>login\'>Login</a> or <a href=\'<?php echo $mrw[register]; ?>register\'>Sign-Up</a> to use some features of <a href=\'http://www.<?php echo $domain; ?>\'><?php echo $domain; ?></a>
<?php
}
?>

</td><td align=\"right\">
<form action=\'<?php echo $_SERVER[REQUEST_URI]; ?>\' method=\'post\'><select name=\'skin\' style=\'background-color: <?php echo $color; ?>;color: <?php echo $color2; ?>;font-size: 11px;font-face: tahoma;font-weight: bold\' onchange=\'this.form.submit()\'><option value=\'skin1\' style=\'background-color: <?php echo $color; ?>;color: <?php echo $color2; ?>;font-size: 11px;font-face: tahoma;font-weight: bold\' selected> - Choose Skin -</option><option value=\'skin1\' style=\'background-color: #feb011;color: #4a4a4a;font-size: 11px;font-face: tahoma;font-weight: bold\'>Orange Skin</option><option value=\'skin2\' style=\'background-color: #db1c1c;color: white;font-size: 11px;font-face: tahoma;font-weight: bold\'>Red Skin</option><option value=\'skin3\' style=\'background-color: #5400ff;color: white;font-size: 11px;font-face: tahoma;font-weight: bold\'>Blue Skin</option><option value=\'skin4\' style=\'background-color: #176b12;color: white;font-size: 11px;font-face: tahoma;font-weight: bold\'>Green Skin</option><option value=\'skin5\' style=\'background-color: #64126b;color: white;font-size: 11px;font-face: tahoma;font-weight: bold\'>Dark Skin</option></select></form>
</td></tr></table>

<?php //BANNER SPOT HERE// ?><br /><br />

<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"center\"><tr><td background=\'<?php echo $siteurl; ?>includes/contentbg.png\' height=\'480\' width=\'518\' valign=\'top\' align=\'left\'><img src=\"<?php echo $siteurl."skins/".$skin; ?>/content.png\">

<table cellpadding=\"10\" cellspacing=\"0\" border=\"0\" width=\"center\"><tr><td>{content}</td></tr></table>

</td></tr></table>
<?php //ENDING JAVASCRIPT AND/OR STATS CODE(S)// ?>
<img src=\"<?php echo $siteurl; ?>includes/footer1.png\">

</td></tr></table>
</td></tr></table>

</body>
</html>';
}
if (html_entity_decode($_GET['name']) == "search_results") {
$newtemp = "<tr><td>{link}</td><td>{cat}</td><td>{date}</td></tr>";
}
if (html_entity_decode($_GET['name']) == "content_list") {
$newtemp = "{link} - {cat}<br />";
}
if (html_entity_decode($_GET['name']) == "gallery") {
$newtemp = "<title><?php echo $setting[sitename]; ?> - Gallery</title>\r\n\r\n<table align=\'center\' border=\'0\' cellpadding=\'10\' cellspacing=\'5\' width=\'480\'><tr>{images}</tr></table>";
}
if (html_entity_decode($_GET['name']) == "gallery_view") {
$newtemp = "<title><?php echo $setting[sitename]; ?> - {name} Gallery</title>\r\n\r\n<h2>{name}</h2>\r\n\r\n<table width=\'480\'><tr><td align=\"left\">{download_zip}</td><td align=\"right\">{sortby}</td></tr></table>\r\n\r\n<table align=\'center\' border=\'0\' cellpadding=\'5\' cellspacing=\'3\' width=\'480\'><tr>{image}</tr></table>";
}
if (html_entity_decode($_GET['name']) == "gallery_image") {
$newtemp = "<td><a href=\'{image}\' rel=\'lightbox[{name}]\'><img src=\'{thumb}\' border=\'1\'></a></td>";
}
if (html_entity_decode($_GET['name']) == "welcome_message") {
$newtemp = "Welcome back {username}. You have {articles} articles and there are {online} users online at {domain} - {logout}.";
}
if (html_entity_decode($_GET['name']) == "section_page") {
$newtemp = "{name}";
}
if (html_entity_decode($_GET['name']) == "homepage") {
$newtemp = "<title>{sitename} - Homepage</title>\r\n\r\n{content}<br />{archive}";
}
if (html_entity_decode($_GET['name']) == "homepage_content") {
$newtemp = '<table cellpadding="0" cellspacing="0" border="0" align="center" width="394"><tr><td><font face="Tahoma" size="4" color="#3A3A3A"><b>{link}</b></font><br />Posted: {date} (<b>{section}</b>)</td></tr><tr><td><br />{story}<br /><br /></td></tr><tr><td align="right">{comments} (<font color="red"><b>{cnum}</b></font>) <b>| {pcomment}</b><br /><br /></td></tr><tr><td align="center">------------------------------<br /><br /></td></tr></table>';
}
if (html_entity_decode($_GET['name']) == "profile") {
$newtemp = "<table cellspacing='0' cellpadding='2' border='0' align='center' width='480' style='padding-left:5px'><tr><td align='center'><b><a href='mailto:{email}'>{username}</a></b><br /><img src='{user_avatar}' style='border: 1px solid black'><br />{level}</td><td align='left'><table cellspacing='0' cellpadding='3' border='0' align='center'><tr><td><b>Name</b></td><td>{user_name}</td></tr><tr><tr><td><b>Birthday</b></td><td>{user_birthday_month} {user_birthday_day}, {user_birthday_year}</td></tr><tr><td><b>Website</b></td><td><a href='{user_website}' target='popup'>{user_website}</a></td></tr><td><b>AIM</b></td><td><a href='aim:goim?screenname={user_aim}&message=Hello+Are+you+there?'>{user_aim}</a></td></tr><tr><td><b>MSN</b></td><td>{user_aim}</td></tr><tr><td><b>YIM</b></td><td><a href='http://edit.yahoo.com/config/send_webmesg?.target={user_yahoo}&.src=pg'>{user_yahoo}</a></td></tr><tr><td><b>ICQ</b></td><td><a href='http://www.icq.com/whitepages/cmd.php?uin={user_icq}&action=message'>{user_icq}</a></td></tr><tr><td><b>Google Talk</b></td><td>{user_gtalk}</td></tr></table></td></tr></table><table><tr><td align='right'>{content_posted}</td></tr></table><br /><br /><center>{edit_profile}</center>";
}

if ($newtemp == "") {
$query = @mysql_query("DELETE FROM ".$pre."templates WHERE name = '".html_entity_decode(addslashes($_GET["name"]))."'");
} else {
$autobr = addslashes($newtemp);
$autobr = html_entity_decode($autobr);

$query2 = @mysql_query("UPDATE ".$pre."templates SET template = '".$autobr."', date = '".time()."' WHERE name = '".html_entity_decode($_GET['name'])."'");
}

if (($query == TRUE) or ($query2 == TRUE)) {
echo re_direct("1500", "admin.php?view=templates");
echo "The template has been deleted. <a href='admin.php?view=templates'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The template could not be deleted. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}

if ($_GET['view'] == "edit_template") {
$admin_page = "yes";
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=templates'>Templates</a> / Edit Template<br /><br /><br />";

$r = mysql_fetch_row(mysql_query("SELECT name,template FROM ".$pre."templates WHERE id = '".$_GET['id']."'"));
$r[1] = str_replace("</textarea>", "&lt;/textarea&gt;", $r[1]);
echo "<form action='admin.php?view=edit_template2&id=".$_GET['id']."' method='post'><table cellpadding='5' cellspacing='0' width='480' style='padding-left:5px' align='left'><tr><td>Template Name</td><td><input type='hidden' name='name' value='".$r[0]."'>".ucwords($r[0])."</td></tr><tr><td>Template</td><td><textarea name='template' cols='55' rows='12' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'>".htmlentities(stripslashes($r[1]))."</textarea></td></tr><tr><td>Description</td><td><small>";

$sql = mysql_query("SELECT * FROM ".$pre."sections ORDER BY `name` ASC");
while($s = mysql_fetch_array($sql)) {
if ($r[0] == $s[name]) {
echo "<u>Content Tags</u> - {name}/{title}/{subject}, {link}, {url}, {date}, {author}, {username}, {cat}/{category}/{section}, {views}, {sitename}<br /><br /><u>Gallery Tags</u> - {gallery_name}, {gallery_link}, {gallery_link; / ;gallery_link}, {gallery_num}, {gallery_image_number} (ex. {gallery_image_1} )<br /><br /><u>Comment Tags</u> - {cform}, {cnum}, {comments}<br /><u>Rating Tags</u> - {user_rate_form}, {user_rate_text}, {user_rate_img}<br /><br /><u><b>Custom fields</b></u> - ";
$data = "";
$sql2 = mysql_query("SELECT * FROM ".$pre."fields WHERE cat = '".$s[name]."' ORDER BY `name` ASC");
while($i = mysql_fetch_array($sql2)) {
$data .= "{".$i[name]."}, ";
}
echo substr($data, 0, -2);
}
}
if ($r[0] == "related_articlelist_start") {
echo "This is for when linking up items. So that the script knows how to output multiple related articles for an item. Only HTML for this template. (no tags to use)";
}
if ($r[0] == "related_articlelist_entry") {
echo "This is for when linking up items. So that the script knows how to output multiple related articles for an item. Tag - {name}";
}
if ($r[0] == "related_articlelist_end") {
echo "This is for when linking up items. So that the script knows how to output multiple related articles for an item. Only HTML for this template. (no tags to use)";
}
if ($r[0] == "comments") {
echo "Tags - {author}, {date}, {comment}, {website}, {email}<br /><br /><u><b>Custom fields</b></u> - ";
$data = "";
$sql2 = mysql_query("SELECT * FROM ".$pre."fields WHERE cat = 'user-profile' ORDER BY `name` ASC");
while($i = mysql_fetch_array($sql2)) {
$data .= "{".$i[name]."}, ";
}
echo substr($data, 0, -2);
}
if ($r[0] == "page") {
echo "Static Page template - {name}, {page}, {username}, {date}";
}
if ($r[0] == "poll_bottom") {
echo "Tags - {vote_total}, {submit}";
}
if ($r[0] == "poll_options") {
echo "Tags - {name}, {data}";
}
if ($r[0] == "poll_results") {
echo "Tags - {question}, {bottom}, {options}";
}
if ($r[0] == "poll_vote") {
echo "Tags - {question}, {bottom}, {options}";
}
if ($r[0] == "layout") {
echo "This is the main template where you put in the frontend design. Simply put in the HTML/PHP of your layout and where content should display insert this tag - {content}. Yes that's it!<br /><br />You can also use two other tags, {sections} and {pages}. {sections} will build a link list to the different sections you have, {pages} doing the same but for static pages.";
}
if ($r[0] == "search_results") {
echo "The template for the search results page, you can use such tags as {link}, {relevance}, {cat}, {date} and {username}.";
}
if ($r[0] == "content_list") {
echo "The template for the page that lists content, you can use such tags as {link}, {cat}, {date} and {username}.";
}
if ($r[0] == "welcome_message") {
echo "The welcome message for users near the banner, you can use the tags {username}, {domain}, {articles} - number of articles, {users} - number of users and {logout} - a logout link.";
}
if ($r[0] == "section_page") {
echo "For a multi-media site for example, you could see the movie area.";
}
if ($r[0] == "homepage") {
echo "This is what the main page will display, you can use {archive} to link to the archive section and {content} to show the latest content.";
}
if ($r[0] == "homepage_content") {
echo "On the homepage, whatever content you set to display (ex. news), you can use many tags such as:<br /><br />{link}, {date}, {story}, {comments}, {cnum}, {author}, {section}, {url}, {title}, {subject}, {pcomment}, {name}, {username}, {id}<br /><br />As well, any custom fields for the section (example still being news), can be used.";
}
if ($r[0] == "gallery") {
echo "For the main gallery page, the tags you can use are: {images}";
}
if ($r[0] == "gallery_view") {
echo "For an actual gallery page, the tags you can use are: {name}, {image}, {title}, {date}, {imagenum}, {id}, {sortby}, {download_zip}";
}
if ($r[0] == "gallery_image") {
echo "The template for an image, the tags you can use are: {image}, {thumb}, {date}";
}
if ($r[0] == "profile") {
echo "Tags - {username}, {email}, {level}, {join_date}, {rank}<br /><br /><u><b>Custom fields</b></u> - ";
$data = "";
$sql2 = mysql_query("SELECT * FROM ".$pre."fields WHERE cat = 'user-profile' ORDER BY `name` ASC");
while($i = mysql_fetch_array($sql2)) {
$data .= "{".$i[name]."}, ";
}
echo substr($data, 0, -2);
}
echo "</small></td></tr><tr><td><input type='submit' value='Update Template' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr></table></form>";
}

if ($_GET['view'] == "edit_template2") {
$admin_page = "yes";
$autobr = addslashes($_POST['template']);
$autobr = html_entity_decode($autobr);

$query = mysql_query("UPDATE ".$pre."templates SET template = '".$autobr."', date = '".time()."' WHERE id = '".$_GET['id']."'");

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=templates");
echo "Template '<b>".addslashes($_POST["name"])."</b>' has been updated. <a href='admin.php?view=templates'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The template could not be updated. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}
}

if ($p[0] or $lev == 1) {
if ($_GET['view'] == "add_template") {
$admin_page = "yes";
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=templates'>Templates</a> / Add Template<br /><br /><br />";
echo "<form action='admin.php?view=add_template2' method='post'><table cellpadding='5' cellspacing='0' width='480' style='padding-left:5px' align='left'><tr><td>Template Name</td><td><input type='text' name='name' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr><tr><td>Template</td><td><textarea name='template' cols='55' rows='12' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></textarea></td></tr><tr><td><input type='submit' value='Add Template' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr></table></form>";
}

if ($_GET['view'] == "add_template2") {
$admin_page = "yes";

$autobr = addslashes($_POST['template']);
$autobr = html_entity_decode($autobr);

$query = @mysql_query("INSERT INTO ".$pre."templates VALUES (null, '".addslashes($_POST["name"])."', '".$autobr."', '".time()."')");

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=templates");
echo "The template <b>".stripslashes($_POST['name'])."</b> has been added. <a href='admin.php?view=templates'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The template could not be added. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}
}

if ($_GET['view'] == "articles") {
$admin_page = "yes";
echo '<script language="javascript">function jump(targ,selObj,restore){
eval(targ+".location=\'"+selObj.options[selObj.selectedIndex].value+"\'");
if (restore) selObj.selectedIndex=0;
}</script>';

echo "<form name='form1'><a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=articles'>Articles</a> / Manage Articles&nbsp;&nbsp;&nbsp;&nbsp;Sort: <select name='sort' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' onChange=\"jump('parent',this,0)\"><option value='' selected></option>";
$sqlst = mysql_query("SELECT * FROM ".$pre."sections ORDER BY `name` ASC");
while($y = mysql_fetch_array($sqlst)) {
$ret = mysql_fetch_row(mysql_query("SELECT padd FROM ".$pre."permissions WHERE name = '".$y[name]."'"));
if ($ret[0] == "1") {
echo "<option value='admin.php?view=articles&section=".$y[name]."'>".ucwords($y[name])."</option>";
}
}
echo "</select>&nbsp;&nbsp;Add: <select name='addsection' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' onChange=\"jump('parent',this,0)\"><option value=''>-- Select Section --</option>";
$sql = mysql_query("SELECT * FROM ".$pre."sections ORDER BY `type` ASC, `name` ASC");
while($r = mysql_fetch_array($sql)) {
$p = "";
$p = mysql_fetch_row(mysql_query("SELECT padd FROM ".$pre."permissions WHERE level = '".$level."' AND name = '".$r[name]."'"));
if ($p[0]) {
echo "<option value='admin.php?view=add_article&cat=".$r[name]."'>".ucwords($r[name])." (";
if ($r[type] == "sub") {
echo "sub-";
}
echo "section)</option>";
$cat = $r[name];
}
}
echo "</select></form><br /><br />";
echo "<table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td><b>ID #</b></td><td><b>Article Name</b></td><td><b>Poster</b></td><td><b>Section</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"]);

$lev = mysql_fetch_row(mysql_query("SELECT value FROM ".$pre."levels WHERE name = '".$level."'"));
if (($lev[0] == "2") && ($_GET['section'] == "")) {
$perm = " WHERE username = '".mysql_real_escape_string(stripslashes($_SESSION[adaptcms_username]))."'";
}
if (($lev[0] == "2") && ($_GET['section'])) {
$perm = " WHERE username = '".mysql_real_escape_string(stripslashes($_SESSION[adaptcms_username]))."' AND section = '".$_GET['section']."'";
}
if (($perm == "") && ($_GET['section'])) {
$perm = " WHERE section = '".$_GET['section']."'";
}

$sql = mysql_query("SELECT * FROM ".$pre."articles".$perm." ORDER BY `id` DESC LIMIT $from, ".$setting["admin_limit"]);
while($r = mysql_fetch_array($sql)) {
$p = "";
$p = mysql_fetch_row(mysql_query("SELECT padd,pedit FROM ".$pre."permissions WHERE level = '".$level."' AND name = '".$r[section]."'"));

echo "<tr><td>".$r[id]."</td><td><a href='".$mrw_1["content"].fthrough($r[name], $r[section], $r[id])."'>".stripslashes($r[name])."</a></td><td><a href='".$mrw_1["user"].$r[username]."'>".$r[username]."</a></td><td>".ucwords($r[section])."</td><td>".date($setting["date_format"], $r[date])."</td><td>";

if ((($p[1]) or ($p[0]) && ($r[username] == $_SESSION[adaptcms_username]))) {
echo "<a href='admin.php?view=edit_article&id=".$r[id]."&cat=".$r[section]."'>Edit</a> | <a href='admin.php?view=delete_article&id=".$r[id]."&cat=".$r[section]."' onclick='return confirmDelete();'>Delete</a>";
}
if (($r[ver] == "1") && ($p[1])) {
echo " | <a href='admin.php?view=verarticle&id=".$r[id]."' onclick='return confirmVerify();'>Verify</a>";
}
echo "</td></tr>";
}
echo "</table><br clear='all'>";
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."articles".$perm),0);
$total_articles = ceil($total_results / $setting["admin_limit"]);

if ($total_articles > "1") {

echo "<center>";


if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"admin.php?view=articles";
	if ($_GET['section']) {
	echo "&section=".$_GET['section'];
	}
	echo "&page=$prev\"><<Previous</a>&nbsp;";
}

for($i = 1; $i <= $total_articles; $i++){
    if(($page) == $i){
        echo "$i&nbsp;";
        } else {
            echo "<a href=\"admin.php?view=articles";
	if ($_GET['section']) {
	echo "&section=".$_GET['section'];
	}
	echo "&page=$i\">$i</a>&nbsp;";
			if (($i/25) == (int)($i/25)) {echo "<br />";}
    }
}


if($page < $total_articles){
    $next = ($page + 1);
    echo "<a href=\"admin.php?view=articles";
	if ($_GET['section']) {
	echo "&section=".$_GET['section'];
	}
	echo "&page=$next\">Next>></a>";
}
echo "</center>";
}
echo "<tr><td><br /><br /><small>Articles a.k.a. content is where all sites strive and succeed or die, this is where you will spend most of your time.</small></td></tr>";
}

if ($_GET['view'] == "quick_add_articles2") {
$admin_page = "yes";
$p = "";
$p = mysql_fetch_row(mysql_query("SELECT padd,pedit FROM ".$pre."permissions WHERE level = '".$level."' AND name = '".$_GET['cat']."'"));
if ($p[1]) {
for($i = 0; $i < $_POST['i']; $i = $i+1) {
$query = @mysql_query("INSERT INTO ".$pre."articles VALUES (null, '".addslashes($_POST["articlename_$i"])."', '".mysql_real_escape_string(stripslashes($_SESSION[adaptcms_username]))."', '".$_GET['cat']."', '', '".time()."', '".date("m")."', '".date("Y")."', '0|0', '0')");

$id = mysql_fetch_row(mysql_query("SELECT id FROM ".$pre."articles ORDER BY `date` DESC LIMIT 1"));
mysql_query("INSERT INTO ".$pre."fielddata VALUES (null, 'name', '".addslashes($_POST["articlename_$i"])."', '".$id[0]."')");
}

$rank = mysql_fetch_row(mysql_query("SELECT prequired FROM ".$pre."ranks WHERE name = '".$rank."'"));

$ranks = mysql_query("SELECT * FROM ".$pre."ranks ORDER BY `prequired` ASC");
while($s = mysql_fetch_array($ranks)) {
if ((pointcount("") > $s[points]) or (pointcount("") == $s[points])) {
if ($s[points] > $rank[0]) {
$upd = sprintf("UPDATE ".$pre."users SET rank = '".$s[name]."' WHERE username = '%s' AND password = '%s'",
mysql_real_escape_string($_SESSION[adaptcms_username]),
mysql_real_escape_string($_SESSION[adaptcms_password]));
mysql_query($upd);
}
}
}

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=articles");
echo "The articles have been added. <a href='admin.php?view=articles'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The article could not be added. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}
}

if ($_GET['view'] == "quick_add_articles") {
$admin_page = "yes";
$p = "";
$p = mysql_fetch_row(mysql_query("SELECT padd FROM ".$pre."permissions WHERE level = '".$level."' AND name = '".$_GET['cat']."'"));
if ($p[0]) {
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=articles'>Articles</a> / Quick Add Article<br /><br /><br />";
echo "<form action='admin.php?view=quick_add_articles&cat=".$_GET['cat']."' method='post'><table cellspacing='0' cellpadding='3' border='0' align='center'><tr><td>How many items to add?</td><td><input type='text' name='many'></td><td><input type='submit' value='Submit'></td></tr></table></form>";

if ($_POST['many']) {
echo "<form action='admin.php?view=quick_add_articles2&cat=".$_GET['cat']."' method='post'><table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><input type='hidden' name='i' value='".$_POST['many']."'>";

for($i = 0; $i < $_POST['many']; $i = $i+1) {
echo "<tr><td>Article Name <b>#".$i."</b></td><td><input type='text' name='articlename_".$i."' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr>";
}
echo "<tr><td>&nbsp;</td><td><input type='submit' value='Add Article(s)' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr></table></form>";
}
}
}

if ($_GET['view'] == "add_article") {
$admin_page = "yes";
$p = "";
$p = mysql_fetch_row(mysql_query("SELECT padd FROM ".$pre."permissions WHERE level = '".$level."' AND name = '".$_GET['cat']."'"));
if ($p[0]) {
echo "<link rel='stylesheet' type='text/css' media='all' href='includes/jscripts/calendar/calendar-win2k-cold-1.css' title='win2k-cold-1' />
<script type='text/javascript' src='includes/jscripts/calendar/calendar.js'></script>
<script type='text/javascript' src='includes/jscripts/calendar/lang/calendar-en.js'></script>
<script type='text/javascript' src='includes/jscripts/calendar/calendar-setup.js'></script>";
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=articles'>Articles</a> / Add Article<br /><br /><br />";
echo "<form action='admin.php?view=add_articles2&cat=".$_GET['cat']."' method='post'><table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td>Article Name</td><td><input type='text' name='articlename' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr><tr><td>Rating</td><td><img src='skins/".$skin."/starno.png'><img src='skins/".$skin."/starno.png'><img src='skins/".$skin."/starno.png'><img src='skins/".$skin."/starno.png'><img src='skins/".$skin."/starno.png'></td></tr>";
$sqlst = mysql_query("SELECT * FROM ".$pre."relations WHERE sub = '".$_GET['cat']."' OR section = '".$_GET['cat']."'");
while($y = mysql_fetch_array($sqlst)) {
$content = "";$xnum = "0";
echo "<tr><td>Relate ";
if ($y[sub] == $_GET['cat']) {
echo ucwords($y[section]);
$name = $y[section];
} else {
echo ucwords($y[sub]);
$name = $y[sub];
}

$sqlsts = mysql_query("SELECT * FROM ".$pre."articles WHERE section = '".$name."' ORDER BY `name` ASC");
while($x = mysql_fetch_array($sqlsts)) {
$content.= "<option value='".$x[id]."'>".stripslashes($x[name])."</option>";
$xnum = $xnum + 1;
}

if ($xnum == 0) {
$sz = 1;
}
if (($xnum > 0) && ($xnum < 6)) {
$sz = $xnum + 1;
}
if ($xnum > 5) {
$sz = 5;
}
echo "</td><td><input type='hidden' name='rels[]' value='".$name."'><select name='rel_".$name."[]' size='".$sz."' multiple><option value='' selected></option>".$content."</select></td></tr>";
}

$sqlsts2 = mysql_query("SELECT * FROM ".$pre."gallery ORDER BY `id` DESC");
while($x2 = mysql_fetch_array($sqlsts2)) {
$content2.= "<option value='".$x2[id]."'>".stripslashes($x2[name])."</option>";
$xnum2 = $xnum2 + 1;
}

if ($xnum2 == 0) {
$sz2 = 1;
}
if (($xnum2 > 0) && ($xnum2 < 6)) {
$sz2 = $xnum2 + 1;
}
if ($xnum2 > 5) {
$sz2 = 5;
}

echo "<tr><td>Relate Gallery</td><td><select name='rel_gallery' size='".$sz2."'><option value='' selected></option>".$content2."</select></td></tr>";

$sql = mysql_query("SELECT * FROM ".$pre."fields WHERE cat = '".$_GET['cat']."' OR cat = 'global' ORDER BY `type` ASC");
while($i = mysql_fetch_array($sql)) {
if ($i[type] == "textfield") {
echo "<input type='hidden' name='id[]' value='".$i[name]."'><tr><td>".ucwords($i[name])."</td><td><input type='text' name='cf_".$i[name]."' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td><td>".stripslashes($i[des])."</td></tr>";
}
if ($i[type] == "textarea") {
echo "<input type='hidden' name='cftype_".$i[name]."' value='textarea'><input type='hidden' name='id[]' value='".$i[name]."'><tr><td>".ucwords($i[name])."</td><td><textarea cols='60' rows='16' id='cf_".$i[name]."' name='cf_".$i[name]."' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' class='mceSimple'></textarea></td><td>".stripslashes($i[des])."</td></tr>";
}
if ($i[type] == "radio") {
echo "<input type='hidden' name='id[]' value='".$i[name]."'><tr><td>".ucwords($i[name])."</td><td>";
$ex = explode(",", $i[info]);
sort($ex);
while (list(, $a) = each ($ex)) {
echo $a." <input type='radio' name='cf_".$i[name]."' value='".$a."' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'>";
}
echo "</td><td>".stripslashes($i[des])."</td></tr>";
}
if ($i[type] == "checkbox") {
echo "<input type='hidden' name='id[]' value='".$i[name]."'><tr><td>".ucwords($i[name])."</td><td>";
$ex = explode(",", $i[info]);
sort($ex);
while (list(, $a) = each ($ex)) {
echo $a." <input type='checkbox' name='cf_".$i[name]."' value='".$a."' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'>";
}
echo "</td><td>".stripslashes($i[des])."</td></tr>";
}
if ($i[type] == "select") {
echo "<input type='hidden' name='id[]' value='".$i[name]."'><tr><td>".ucwords($i[name])."</td><td><select name='cf_".$i[name]."' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'><option value='' selected>-- Select --</option>";
$ex = explode(",", $i[info]);
sort($ex);
while (list(, $a) = each ($ex)) {
echo "<option value='".$a."'>".ucwords($a)."</option>";
}
echo "</select></td><td>".stripslashes($i[des])."</td></tr>";
}
if ($i[type] == "file") {
echo "<input type='hidden' name='id[]' value='".$i[name]."'><tr><td>".ucwords($i[name])."</td><td><select name='cf_".$i[name]."' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'><option value='' selected>-- Select --</option>";
$sqlfl = mysql_query("SELECT * FROM ".$pre."files ORDER BY `id` DESC");
while($row = mysql_fetch_array($sqlfl)) {
if (preg_match("/http/", $row[filedir])) {
$filedir = $row[filedir];
} else {
$filedir = $siteurl;
}
echo "<option value='".$filedir.$row[filename]."'>";
if (strlen($row[filename]) > 40) {
echo substr(urldecode($row[filename]), 0, 37)."...";
} else {
echo substr(urldecode($row[filename]), 0, 40);
}
echo "</option>";
}
echo "</select></td><td>".stripslashes($i[des])."</td></tr>";
}

}
echo "<tr><td></td><td></td></tr>
<tr><td></td><td><b>Options</b></td></tr>
<input type='hidden' name='postpone' id='f_date_e' />
<tr><td>Publish Later?:</td><td><span id=\"show_e\">-- not entered --</span> <img
src=\"includes/jscripts/calendar/img.gif\" id=\"f_trigger_e\" style=\"cursor: pointer; border: 1px solid
red;\" title=\"Date selector\" onmouseover=\"this.style.background='red';\"
onmouseout=\"this.style.background=''\" /></td></tr>

<script type='text/javascript'>
    Calendar.setup({
        inputField     :    'f_date_e',     // id of the input field
        ifFormat       :    '%s',     // format of the input field (even if hidden, this format will be honored)
		showsTime      :    true,            // will display a time selector
		timeFormat     :	12,
        displayArea    :    'show_e',       // ID of the span where the date is to be shown
        daFormat       :    '%A, %B %d, %Y - %I:%M %p',// format of the displayed date
        button         :    'f_trigger_e',  // trigger button (well, IMG in our case)
        align          :    'Tl',           // alignment (defaults to 'Bl')
        singleClick    :    true
    });
</script>
<tr><td>Ping Website(s)?:</td><td><input type='checkbox' name='ping' value='yes' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr><tr><td><input type='submit' value='Add Article' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' name='type'></td><td><input type='submit' value='Publish Later' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' name='type'></td></tr></table></form>";
}
}

if ($_GET['view'] == "edit_article") {
$admin_page = "yes";
$r = mysql_fetch_row(mysql_query("SELECT name,section,rate,username,ver FROM ".$pre."articles WHERE id = '".$_GET['id']."'"));
$cat = mysql_fetch_row(mysql_query("SELECT type FROM ".$pre."sections WHERE name = '".$r[1]."'"));
$p = "";
$p = mysql_fetch_row(mysql_query("SELECT padd,pedit FROM ".$pre."permissions WHERE level = '".$level."' AND name = '".$r[1]."'"));
if ((($p[1]) or ($p[0]) && ($r[3] == $_SESSION[adaptcms_username]))) {
echo "<link rel='stylesheet' type='text/css' media='all' href='includes/jscripts/calendar/calendar-win2k-cold-1.css' title='win2k-cold-1' />
<script type='text/javascript' src='includes/jscripts/calendar/calendar.js'></script>
<script type='text/javascript' src='includes/jscripts/calendar/lang/calendar-en.js'></script>
<script type='text/javascript' src='includes/jscripts/calendar/calendar-setup.js'></script>";
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=articles'>Articles</a> / Edit Article&nbsp;&nbsp;&nbsp;&nbsp;<select name='addsection' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' onChange=\"jump('parent',this,0)\"><option value=''>-- Add Article --</option>";
$sql = mysql_query("SELECT * FROM ".$pre."sections ORDER BY `type` ASC, `name` ASC");
while($row = mysql_fetch_array($sql)) {
$p = "";
$p = mysql_fetch_row(mysql_query("SELECT padd FROM ".$pre."permissions WHERE level = '".$level."' AND name = '".$row[name]."'"));
if ($p[0]) {
echo "<option value='admin.php?view=add_article&cat=".$row[name]."'>".ucwords($row[name])." (";
if ($r[type] == "sub") {
echo "sub-";
}
echo "section)</option>";
$cat = $row[name];
}
}
echo "</select></form><br /><br /><br />";

echo "<form action='admin.php?view=edit_article2&id=".$_GET['id']."' method='post'><table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><input type='hidden' name='cat' value='".$r[1]."'><input type='hidden' name='username' value='".$r[3]."'>";

echo "<tr><td>Article Name</td><td><input type='text' name='articlename' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' value=\"".htmlspecialchars(stripslashes($r[0]))."\"></td></tr><tr><td>Rating</td><td>";

if ($r[2] == "0|0") {
echo "<img src='skins/".$skin."/starno.png'><img src='skins/".$skin."/starno.png'><img src='skins/".$skin."/starno.png'><img src='skins/".$skin."/starno.png'><img src='skins/".$skin."/starno.png'>";
} else {
$rx = explode("|", $r[2]);
$rate = $rx[1] / $rx[0];
$rate = round(substr($rate, 0, 3));
for ($i = 1; $i <= $rate; $i++) {
echo "<img src='skins/".$skin."/star.png'>";
}
$val = 5 - $rate;
if ($val > "0") {
for ($i = 1; $i <= $val; $i++) {
echo "<img src='skins/".$skin."/starno.png'>";
}
}
}
echo "</td></tr>";

$sqlst = mysql_query("SELECT * FROM ".$pre."relations WHERE sub = '".$r[1]."' OR section = '".$r[1]."'");
while($y = mysql_fetch_array($sqlst)) {
$content = "";$xnum = "0";
echo "<tr><td>Relate ";
if ($y[sub] == $r[1]) {
echo ucwords($y[section]);
$name = $y[section];
} else {
echo ucwords($y[sub]);
$name = $y[sub];
}

$sqlsts = mysql_query("SELECT * FROM ".$pre."articles WHERE section = '".$name."' ORDER BY `id` DESC");
while($x = mysql_fetch_array($sqlsts)) {
$xnum = $xnum + 1;
$datas = mysql_fetch_row(mysql_query("SELECT id FROM ".$pre."fielddata WHERE fname = '".$name."' AND aid = '".$_GET['id']."' AND data = '".$x[id]."'"));
if ($datas[0]) {
echo "<input type='hidden' name='reldata_".$x[id]."' value='".$x[id]."'>";
$content.= "<option value='".$x[id]."' selected>-- ".stripslashes($x[name])." --</option>";
} else {
$content.= "<option value='".$x[id]."'>".stripslashes($x[name])."</option>";
}
}

if ($xnum == 0) {
$sz = 1;
}
if (($xnum > 0) && ($xnum < 6)) {
$sz = $xnum + 1;
}
if ($xnum > 5) {
$sz = 5;
}
echo "</td><td><input type='hidden' name='rels[]' value='".$name."'><select name='rel_".$name."[]' size='".$sz."' multiple><option value=''></option>".$content."</select>";
}

$sqlsts2 = mysql_query("SELECT * FROM ".$pre."gallery ORDER BY `id` DESC");
while($x2 = mysql_fetch_array($sqlsts2)) {
$xnum2 = $xnum2 + 1;
$datas2 = mysql_fetch_row(mysql_query("SELECT id FROM ".$pre."fielddata WHERE fname = 'Gallery' AND aid = '".$_GET['id']."' AND data = '".$x2[id]."'"));
if ($datas2[0]) {
echo "<input type='hidden' name='relgal' value='".$x2[id]."'>";
$content2.= "<option value='".$x2[id]."' selected>-- ".stripslashes($x2[name])." --</option>";
} else {
$content2.= "<option value='".$x2[id]."'>".stripslashes($x2[name])."</option>";
}
}

if ($xnum2 == 0) {
$sz2 = 1;
}
if (($xnum2 > 0) && ($xnum2 < 6)) {
$sz2 = $xnum2 + 1;
}
if ($xnum2 > 5) {
$sz2 = 5;
}

echo "</td></tr><tr><td>Relate Gallery</td><td><select name='rel_gallery' size='".$sz2."'><option value=''></option>".$content2."</select></td></tr>";

$sql = mysql_query("SELECT * FROM ".$pre."fields WHERE cat = '".$r[1]."' OR cat = 'global' ORDER BY `type` ASC");
while($i = mysql_fetch_array($sql)) {
$data = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE aid = '".$_GET['id']."' AND fname = '".$i[name]."'"));
echo "<input type='hidden' name='id[]' value='".$i[name]."'><input type='hidden' name='cfdata_".$i[name]."' value='".htmlentities(stripslashes($data[0]))."'>";

if ($i[type] == "textfield") {
echo "<tr><td>".ucwords($i[name])."</td><td><input type='text' name='cf_".$i[name]."' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' value='".stripslashes($data[0])."'></td><td>".stripslashes($i[des])."</td></tr>";
}
if ($i[type] == "textarea") {
echo "<tr><td>".ucwords($i[name])."</td><td><textarea cols='60' rows='16' id='cf_".$i[name]."' name='cf_".$i[name]."' class='mceSimple' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'>".stripslashes($data[0])."</textarea></td><td>".stripslashes($i[des])."</td></tr>";
}
if ($i[type] == "radio") {
echo "<tr><td>".ucwords($i[name])."</td><td>";
$ex = explode(",", $i[info]);
sort($ex);
while (list(, $a) = each ($ex)) {
echo $a." <input type='radio' name='cf_".$i[name]."' value='".$a."' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'";
if ($a == stripslashes($data[0])) {
echo " checked";
}
echo ">";
}
echo "</td><td>".stripslashes($i[des])."</td></tr>";
}
if ($i[type] == "checkbox") {
echo "<tr><td>".ucwords($i[name])."</td><td>";
$ex = explode(",", $i[info]);
sort($ex);
while (list(, $a) = each ($ex)) {
echo $a." <input type='checkbox' name='cf_".$i[name]."' value='".$a."' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'";
if ($a == stripslashes($data[0])) {
echo " checked";
}
echo ">";
}
echo "</td><td>".stripslashes($i[des])."</td></tr>";
}
if ($i[type] == "select") {
echo "<tr><td>".ucwords($i[name])."</td><td><select name='cf_".$i[name]."' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'><option value='' selected>-- Select --</option>";
$ex = explode(",", $i[info]);
sort($ex);
while (list(, $a) = each ($ex)) {
echo "<option value='".$a."'";
if ($a == stripslashes($data[0])) {
echo " selected";
}
echo ">".ucwords($a)."</option>";
}
echo "</select></td><td>".stripslashes($i[des])."</td></tr>";
}

if ($i[type] == "file") {
echo "<tr><td>".ucwords($i[name])."</td><td><select name='cf_".$i[name]."' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'><option value='' selected>-- Select --</option>";
$sqlfl = mysql_query("SELECT * FROM ".$pre."files ORDER BY `filename` ASC");
while($row = mysql_fetch_array($sqlfl)) {
if (preg_match("/http/", $row[filedir])) {
$filedir = $row[filedir];
} else {
$filedir = $siteurl;
}
echo "<option value='".$filedir.$row[filename]."'";
if ($filedir.$row[filename] == $data[0]) {
echo " selected>-- ";
if (strlen($row[filename]) > 40) {
echo substr(urldecode($row[filename]), 0, 37)."...";
} else {
echo substr(urldecode($row[filename]), 0, 40);
}
echo " --";
} else {
echo ">";
if (strlen($row[filename]) > 40) {
echo substr(urldecode($row[filename]), 0, 37)."...";
} else {
echo substr(urldecode($row[filename]), 0, 40);
}

}
echo "</option>";
}
echo "</select></td><td>".stripslashes($i[des])."</td></tr>";
}

}
echo "<tr><td></td><td></td></tr><tr><td></td><td><b>Postpone</b></td></tr>";
if (is_numeric($r[4])) {
echo "<tr><td>Publish Date:</td><td>".date("l, F d, Y - h:i A", $r[4])."</td></tr>";
}
echo "<input type='hidden' name='postpone' id='f_date_e' />
<tr><td>";
if (is_numeric($r[4])) {
echo "New Publish Date?";
} else {
echo "Publish Later?";
}
echo ":</td><td><span id=\"show_e\">-- not entered --</span> <img
src=\"includes/jscripts/calendar/img.gif\" id=\"f_trigger_e\" style=\"cursor: pointer; border: 1px solid
red;\" title=\"Date selector\" onmouseover=\"this.style.background='red';\"
onmouseout=\"this.style.background=''\" /></td></tr>

<script type='text/javascript'>
    Calendar.setup({
        inputField     :    'f_date_e',     // id of the input field
        ifFormat       :    '%s',     // format of the input field (even if hidden, this format will be honored)
		showsTime      :    true,            // will display a time selector
		timeFormat     :	12,
        displayArea    :    'show_e',       // ID of the span where the date is to be shown
        daFormat       :    '%A, %B %d, %Y - %I:%M %p',// format of the displayed date
        button         :    'f_trigger_e',  // trigger button (well, IMG in our case)
        align          :    'Tl',           // alignment (defaults to 'Bl')
        singleClick    :    true
    });
</script>";
if (is_numeric($r[4])) {
echo "<tr><td>Publish Now?:</td><td><input type='checkbox' name='publish_now' value='yes' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr>";
}
echo "<input type='hidden' name='art_status' value='".$r[4]."'><tr><td><input type='submit' name='type' value='Update Article' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td>";
if ($r[4] == "saved") {
echo "<td><input type='submit' name='type' value='Publish to Site' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td>";
} else {
echo "<td><input type='submit' value='Publish Later' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' name='type'></td>";
}
echo "</tr></form>";
}
}

if ($_GET['view'] == "verarticle") {
$admin_page = "yes";
$p = "";
$r = mysql_fetch_row(mysql_query("SELECT section FROM ".$pre."articles WHERE id = '".$_GET['id']."'"));
$p = mysql_fetch_row(mysql_query("SELECT padd,pedit FROM ".$pre."permissions WHERE level = '".$level."' AND name = '".$r[0]."'"));
if ($p[1]) {
$query = mysql_query("UPDATE ".$pre."articles SET ver = '' WHERE id = '".addslashes($_GET["id"])."'");

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=articles");
echo "The article has been verified. <a href='admin.php?view=articles'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The article could not be verified. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}
}

if ($_GET['view'] == "delete_article") {
$admin_page = "yes";
$p = "";
$p = mysql_fetch_row(mysql_query("SELECT padd,pedit FROM ".$pre."permissions WHERE level = '".$level."' AND name = '".$_GET['cat']."'"));
if (($p[1]) or ($p[0])) {
$query = @mysql_query("DELETE FROM ".$pre."articles WHERE id = '".addslashes($_GET["id"])."'");
$query2 = @mysql_query("DELETE FROM ".$pre."fielddata WHERE aid = '".addslashes($_GET["id"])."'");
mysql_query("ALTER TABLE ".$pre."articles  AUTO_INCREMENT =".$_GET['id']);

if (($query == TRUE) && ($query2 == TRUE)) {
echo re_direct("1500", "admin.php?view=articles");
echo "The article has been deleted. <a href='admin.php?view=articles'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The article could not be deleted. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}
}

if ($_GET['view'] == "edit_article2") {
$admin_page = "yes";
$p = "";
$p = mysql_fetch_row(mysql_query("SELECT padd,pedit FROM ".$pre."permissions WHERE level = '".$level."' AND name = '".$_POST['cat']."'"));
if ((($p[1]) or ($p[0]) && ($_POST['username'] == $_SESSION[adaptcms_username]))) {
if ($_POST['publish_now']) {
$ver = "";
} else {
if ($_POST['postpone']) {
$ver = $_POST['postpone'];
} else {
if ((($_POST['type'] == "Publish to Site") or ($_POST['art_status'] == "") && ($_POST['type'] == "Update Article"))) {
$ver = "";
} else {
$ver = "saved";
}
}
}
$query = @mysql_query("UPDATE ".$pre."articles SET name = '".addslashes($_POST["articlename"])."', ver = '".$ver."' WHERE id = '".$_GET['id']."'");
mysql_query("UPDATE ".$pre."fielddata SET data = '".addslashes($_POST["articlename"])."' WHERE fname = 'name' AND aid = '".$_GET['id']."'");

while (@list(, $i) = @each ($_POST['id'])) {
if ($i) {
if ((strtolower($setting["wysiwyg"]) == "no") && ($_POST["cftype_$i"])) {
$text = preg_replace("/\n/", "<br />\n", addslashes($_POST["cf_$i"]));
} else {
$text = addslashes($_POST["cf_$i"]);
}
if ($text) {
if ($_POST["cfdata_$i"] == "") {
@mysql_query("INSERT INTO ".$pre."fielddata VALUES (null, '".$i."', '".$text."', '".$_GET['id']."')");
} else {
@mysql_query("UPDATE ".$pre."fielddata SET data = '".$text."' WHERE fname = '".$i."' AND aid = '".$_GET['id']."'");
}
}
}
}

if ($_POST['rel_gallery']) {
if ($_POST["relgal"] == "") {
@mysql_query("INSERT INTO ".$pre."fielddata VALUES (null, 'Gallery', '".$_POST['rel_gallery']."', '".$_GET['id']."')");
} else {
@mysql_query("UPDATE ".$pre."fielddata SET data = '".$_POST['rel_gallery']."' WHERE fname = 'Gallery' AND aid = '".$_GET['id']."'");
}
}

while (@list(, $y) = @each ($_POST['rels'])) {
while (@list(, $x) = @each ($_POST["rel_$y"])) {
if ($x && $y) {
if ($_POST["reldata_$x"] == "") {
@mysql_query("INSERT INTO ".$pre."fielddata VALUES (null, '".$y."', '".$x."', '".$_GET['id']."')");
@mysql_query("INSERT INTO ".$pre."fielddata VALUES (null, '".$_POST['cat']."', '".$_GET['id']."', '".$x."')");
} else {
if ($x != $_POST["reldata_$x"]) {
@mysql_query("UPDATE ".$pre."fielddata SET data = '".$x."' WHERE fname = '".$y."' AND aid = '".$_GET['id']."'");
@mysql_query("UPDATE ".$pre."fielddata SET data = '".$_GET['id']."' WHERE fname = '".$_POST['cat']."' AND aid = '".$x."'");
}
}
}
}

}

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=articles");
echo "The article <b>".stripslashes($_POST['articlename'])."</b> has been updated. <a href='admin.php?view=articles'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The article could not be updated. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}
}

if ($_GET['view'] == "add_articles2") {
$admin_page = "yes";
$p = "";
$p = mysql_fetch_row(mysql_query("SELECT padd,pedit FROM ".$pre."permissions WHERE level = '".$level."' AND name = '".$_GET['cat']."'"));
if ($p[1]) {
if ($_POST['postpone']) {
$ver = $_POST['postpone'];
} else {
if ($_POST['type'] == "Publish Later") {
$ver = "saved";
} else {
$ver = "";
}
}

$query = @mysql_query("INSERT INTO ".$pre."articles VALUES (null, '".addslashes($_POST["articlename"])."', '".mysql_real_escape_string(stripslashes($_SESSION[adaptcms_username]))."', '".$_GET['cat']."', '".$ver."', '".time()."', '".date("m")."', '".date("Y")."', '0|0', '0')");

$id = mysql_fetch_row(mysql_query("SELECT id FROM ".$pre."articles ORDER BY `date` DESC LIMIT 1"));
mysql_query("INSERT INTO ".$pre."fielddata VALUES (null, 'name', '".addslashes($_POST["articlename"])."', '".$id[0]."')");


while (@list(, $i) = @each ($_POST['id'])) {
if ($i) {
if ((strtolower($setting["wysiwyg"]) == "no") && ($_POST["cftype_$i"])) {
$text = preg_replace("/\n/", "<br />\n", addslashes($_POST["cf_$i"]));
if ($contents == "") {
$contents = $text;
}
$contents2 .= $text;
} else {
$text = addslashes($_POST["cf_$i"]);
}
if ($_POST["cf_$i"] && $text) {
@mysql_query("INSERT INTO ".$pre."fielddata VALUES (null, '".$i."', '".$text."', '".$id[0]."')");
}
}
}

if ($_POST['rel_gallery']) {
@mysql_query("INSERT INTO ".$pre."fielddata VALUES (null, 'Gallery', '".$_POST['rel_gallery']."', '".$id[0]."')");
}

while (@list(, $y) = @each ($_POST['rels'])) {
while (@list(, $x) = @each ($_POST["rel_$y"])) {
if ($x && $y) {
@mysql_query("INSERT INTO ".$pre."fielddata VALUES (null, '".$y."', '".$x."', '".$id[0]."')");
@mysql_query("INSERT INTO ".$pre."fielddata VALUES (null, '".$_GET['cat']."', '".$id[0]."', '".$x."')");
}
}
}

$rank = mysql_fetch_row(mysql_query("SELECT prequired FROM ".$pre."ranks WHERE name = '".$rank."'"));

$ranks = mysql_query("SELECT * FROM ".$pre."ranks ORDER BY `prequired` ASC");
while($s = mysql_fetch_array($ranks)) {
if ((pointcount("") > $s[points]) or (pointcount("") == $s[points])) {
if ($s[points] > $rank[0]) {
$upd = sprintf("UPDATE ".$pre."users SET rank = '".$s[name]."' WHERE username = '%s' AND password = '%s'",
mysql_real_escape_string($_SESSION[adaptcms_username]),
mysql_real_escape_string($_SESSION[adaptcms_password]));
mysql_query($upd);
}
}
}

if ($setting["trackback_urls"] && $_POST['ping']) {
include("includes/trackback.php");
$urls = explode(",", $setting["trackback_urls"]);
while (list(, $zz) = each ($urls)) {
if (send_trackback($mrw_1["content"].fthrough($_POST['articlename'], $_GET['cat'], $id[0]), $_POST['articlename'], $contents, $setting['sitename'], $zz) == 1) {
echo "<a href='".$zz."'><b>".$zz."</b></a> pinged<br />";
} else {
echo "<a href='".$zz."'><b>".$zz."</b></a> <font color='red'>could not be</font> pinged<br />";
}
}
if ($tb_array = auto_discovery($contents2)) {
     	foreach($tb_array as $tb_key => $tb_url) {
     		if (send_trackback($mrw_1["content"].fthrough($_POST['articlename'], $_GET['cat'], $id[0]), $_POST['articlename'], $contents, $setting['sitename'], $tb_url)) {
     			// Successful ping...
     			echo "Trackback sent to <a href='".$tb_url."'><b>".$tb_url."</b></a><br />";
     		} else {
     			// Error pinging...
     			echo "Trackback to <a href='".$tb_url."'><b>".$tb_url."</b></a><br />";
     		}
     	}
     } else {
     	// No trackbacks in TEXT...
     	echo "No trackbacks were auto-discovered...<br />";
     }

}
echo "<br /><br />";

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=articles");
echo "The article <b>".stripslashes($_POST['articlename'])."</b> has been added. <a href='admin.php?view=articles'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The article could not be added. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}
}

$p = "";
$p = mysql_fetch_row(mysql_query("SELECT pedit FROM ".$pre."permissions WHERE level = '".$level."' AND name = 'templates'"));
if ($p[0]) {
if ($_GET['view'] == "fields") {
$admin_page = "yes";
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;Fields / Manage Fields - <a href='admin.php?view=add_field'>Add Field</a><br /><br /><br />";
echo "<table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td><b>ID #</b></td><td><b>Field Name</b></td><td><b>Field Type</b></td><td><b>Section</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."fields ORDER BY `id` DESC LIMIT $from, ".$setting["admin_limit"]);
while($r = mysql_fetch_array($sql)) {
echo "<tr><td>".$r[id]."</td><td>".stripslashes($r[name])."</td><td>".$r[type]."</td><td>".$r[cat]."</td><td><a href='admin.php?view=edit_field&id=".$r[id]."'>Edit</a> | <a href='admin.php?view=delete_field&id=".$r[id]."' onclick='return confirmDelete();'>Delete</a></td></tr>";
}
echo "</table><br clear='all'>";
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."fields"),0);
$total_fields = ceil($total_results / $setting["admin_limit"]);

if ($total_fields > "1") {

echo "<center>";


if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"admin.php?view=fields&page=$prev\"><<Previous</a>&nbsp;";
}

for($i = 1; $i <= $total_fields; $i++){
    if(($page) == $i){
        echo "$i&nbsp;";
        } else {
            echo "<a href=\"admin.php?view=fields&page=$i\">$i</a>&nbsp;";
			if (($i/25) == (int)($i/25)) {echo "<br />";}
    }
}


if($page < $total_fields){
    $next = ($page + 1);
    echo "<a href=\"admin.php?view=fields&page=$next\">Next>></a>";
}
echo "</center>";
}
echo "<tr><td><br /><br /><small></small></td></tr>";
}

if ($_GET['view'] == "add_field") {
$admin_page = "yes";
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=fields'>Fields</a> / Add Field<br /><br /><br />";
echo "<form action='admin.php?view=add_fields2' method='post'><table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td>Field Name</td><td><input type='text' name='fieldname' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr><tr><td>Section</td><td><select name='cat[]' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' size='5' multiple>";
$sql = mysql_query("SELECT * FROM ".$pre."sections ORDER BY `type` ASC");
while($r = mysql_fetch_array($sql)) {
echo "<option value='".$r[name]."'>".$r[name]." (";
if ($r[type] == "sub") {
echo "sub-";
}
echo "section)</option>";
}
echo "<option value='user-profile'>User Profiles</option></select></td></tr><tr><td>Field Type</td><td><select name='type' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'><option value='textfield'>Textfield</option><option value='textarea'>Textarea</option><option value='select'>Dropdown Box</option><option value='radio'>Radiobox(s)</option><option value='checkbox'>Checkbox(s)</option><option value='file'>File</option></select></td></tr><tr><td>Field Data</td><td><textarea name='info' cols='60' rows='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></textarea></td><td><small>For example, a dropdown box you would put in 'option1,option2' and the same with radio/checkbox<br /><br />Not used for textfield or textarea</small></td></tr><tr><td>Description</td><td><textarea name='des' cols='60' rows='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></textarea></td></tr><tr><td><input type='submit' value='Add Field' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr></table></form>";
}

if ($_GET['view'] == "edit_field") {
$admin_page = "yes";
$r = mysql_fetch_row(mysql_query("SELECT name,cat,type,info,des FROM ".$pre."fields WHERE id = '".$_GET['id']."'"));
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=fields'>Fields</a> / Edit Field<br /><br /><br />";

echo "<form action='admin.php?view=edit_field2&id=".$_GET['id']."' method='post'><table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td>Field Name</td><td><input type='text' name='fieldname' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' value='".$r[0]."'></td></tr><tr><td>Section</td><td><select name='cat[]' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' multiple>";
$sql = mysql_query("SELECT * FROM ".$pre."sections ORDER BY `type` ASC");
while($i = mysql_fetch_array($sql)) {
echo "<option value='".$i[name]."'";
if ($r[1] == $i[name]) {
echo " selected";
}
echo ">".$i[name]." (";
if ($i[type] == "sub") {
echo "sub-";
}
echo "section)</option>";
}
echo "<option value='user-profile'>User Profiles</option></select></td></tr><tr><td>Field Type</td><td><select name='type' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'><option value='textfield'";
if ($r[2] == "textfield") {
echo " selected";
}
echo ">Textfield</option><option value='textarea'";
if ($r[2] == "textarea") {
echo " selected";
}
echo ">Textarea</option><option value='select'";
if ($r[2] == "select") {
echo " selected";
}
echo ">Dropdown Box</option><option value='radio'";
if ($r[2] == "radio") {
echo " selected";
}
echo ">Radiobox(s)</option><option value='checkbox'";
if ($r[2] == "checkbox") {
echo " selected";
}
echo ">Checkbox(s)</option><option value='file'";
if ($r[2] == "file") {
echo " selected";
}
echo ">File</option></select></td></tr><tr><td>Field Data</td><td><textarea name='info' cols='60' rows='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'>".stripslashes($r[3])."</textarea></td><td><small>For example, a dropdown box you would put in 'option1,option2' and the same with radio/checkbox<br /><br />Not used for textfield or textarea</small></td></tr><tr><td>Description</td><td><textarea name='des' cols='60' rows='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'>".stripslashes($r[4])."</textarea></td></tr><tr><td><input type='submit' value='Update Field' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr></table></form>";
}

if ($_GET['view'] == "delete_field") {
$admin_page = "yes";
$query = @mysql_query("DELETE FROM ".$pre."fields WHERE id = '".addslashes($_GET["id"])."'");
mysql_query("ALTER TABLE ".$pre."fields  AUTO_INCREMENT =".$_GET['id']);
if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=fields");
echo "The field has been deleted. <a href='admin.php?view=fields'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The field could not be deleted. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}

if ($_GET['view'] == "edit_field2") {
$admin_page = "yes";
while (list(, $i) = each ($_POST['cat'])) {
$fname = strtolower(str_replace(" ", "_", addslashes($_POST["fieldname"])));
$query = @mysql_query("UPDATE ".$pre."fields SET name = '".$fname."', cat = '".$i."', type = '".addslashes($_POST["type"])."', des = '".addslashes($_POST["des"])."', info = '".addslashes($_POST["info"])."' WHERE id = '".$_GET['id']."'");
}

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=fields");
echo "The field <b>".$fname."</b> has been updated. <a href='admin.php?view=fields'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The field could not be updated. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}

if ($_GET['view'] == "add_fields2") {
$admin_page = "yes";
while (list(, $i) = each ($_POST['cat'])) {
$fname = strtolower(str_replace(" ", "_", addslashes($_POST["fieldname"])));
$query = @mysql_query("INSERT INTO ".$pre."fields VALUES (null, '".$fname."', '".$i."', '".addslashes($_POST["type"])."', '".addslashes($_POST["des"])."', '".addslashes($_POST["info"])."')");
}

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=fields");
echo "The field <b>".$fname."</b> has been added. <a href='admin.php?view=fields'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The field could not be added. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}
}

$p = "";
$p = mysql_fetch_row(mysql_query("SELECT padd,pedit,pdelete FROM ".$pre."permissions WHERE level = '".$level."' AND name = 'sections'"));
if ($_GET['view'] == "sections") {
$admin_page = "yes";
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;Sections / Manage Sections";
if ($p[0]) {
echo "- <a href='admin.php?view=add_section'>Add Section</a>";
}
echo "<br /><br /><br />";
echo "<table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td><b>ID #</b></td><td><b>Section Name</b></td><td><b>Section Type</b></td><td><b># of Relations</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."sections ORDER BY `id` DESC LIMIT $from, ".$setting["admin_limit"]);
while($r = mysql_fetch_array($sql)) {
if ($r[type] == "sub") {
$count = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."relations WHERE sub = '".$r[name]."'"));
} else {
$count = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."relations WHERE section = '".$r[name]."'"));
}
echo "<tr><td>".$r[id]."</td><td>".stripslashes($r[name])."</td><td>".ucwords($r[type])."</td><td>".$count."</td><td>";
if ($p[1]) {
echo "<a href='admin.php?view=edit_section&id=".$r[id]."'>Edit</a> | ";
}
if ($p[2]) {
echo "<a href='admin.php?view=delete_section&id=".$r[id]."&type=".$r[type]."&name=".$r[name]."' onclick='return confirmDelete();'>Delete</a>";
}
echo "</td></tr>";
}
echo "</table><br clear='all'>";
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."sections"),0);
$total_sections = ceil($total_results / $setting["admin_limit"]);

if ($total_sections > "1") {

echo "<center>";


if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"admin.php?view=sections&page=$prev\"><<Previous</a>&nbsp;";
}

for($i = 1; $i <= $total_sections; $i++){
    if(($page) == $i){
        echo "$i&nbsp;";
        } else {
            echo "<a href=\"admin.php?view=sections&page=$i\">$i</a>&nbsp;";
			if (($i/25) == (int)($i/25)) {echo "<br />";}
    }
}


if($page < $total_sections){
    $next = ($page + 1);
    echo "<a href=\"admin.php?view=sections&page=$next\">Next>></a>";
}
echo "</center>";
}
echo "<tr><td><br /><br /><small>When adding a section you will be presented a choice between a 'section' and 'sub-section'. A section for example is 'Reviews' and a sub-section 'Games' or 'Movies' for example.</small></td></tr>";
}

if ($_GET['view'] == "add_section") {
$admin_page = "yes";
if ($p[0]) {
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=sections'>Sections</a> / Add Section<br /><br /><br />";
echo "<a href='admin.php?view=add_section1'>Add Section</a><br /><a href='admin.php?view=add_section2'>Add Sub-Section</a>";
}
}

if ($_GET['view'] == "add_section1") {
$admin_page = "yes";
if ($p[0]) {
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=sections'>Sections</a> / Add Section<br /><br /><br />";
echo "<form action='admin.php?view=add_sections2' method='post'><input type='hidden' name='type' value='section'><tr><td>Section Name</td><td><input type='text' name='sectionname' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr><tr><td>Relations</td><td>";
$sql = mysql_query("SELECT * FROM ".$pre."sections WHERE type = 'sub' ORDER BY `id` DESC");
while($r = mysql_fetch_array($sql)) {
echo "<input type='hidden' name='id[]' value='".$r[id]."'>";
echo $r[name]." <input type='checkbox' name='sec_".$r[id]."' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' value='".$r[name]."' checked><br />";
}
echo "</td></tr><tr><td><input type='submit' value='Add Section' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr></table></form>";
}
}

if ($_GET['view'] == "add_section2") {
$admin_page = "yes";
if ($p[0]) {
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=sections'>Sections</a> / Add Sub-Section<br /><br /><br />";
echo "<form action='admin.php?view=add_sections2' method='post'><input type='hidden' name='type' value='sub'><tr><td>Section Name</td><td><input type='text' name='sectionname' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr><tr><td>Relations</td><td>";
$sql = mysql_query("SELECT * FROM ".$pre."sections WHERE type = 'section' ORDER BY `id` DESC");
while($r = mysql_fetch_array($sql)) {
echo "<input type='hidden' name='id[]' value='".$r[id]."'>";
echo $r[name]." <input type='checkbox' name='sec_".$r[id]."' value='".$r[name]."' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' checked><br />";
}
echo "</td></tr><tr><td><input type='submit' value='Add Sub-Section' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr></table></form>";
}
}

if ($_GET['view'] == "edit_section") {
$admin_page = "yes";
if ($p[1]) {
$i = mysql_fetch_row(mysql_query("SELECT name,type FROM ".$pre."sections WHERE id = '".$_GET['id']."'"));
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=sections'>Sections</a> / Edit ";
if ($i[1] == "sub") {
$section = "section";
echo "Sub-";
} else {
$section = "sub";
}
echo "Section<br />";

echo "<form action='admin.php?view=edit_section2&id=".$_GET['id']."' method='post'><input type='hidden' name='type' value='".$i[1]."'><input type='hidden' name='oldname' value='".$i[0]."'><tr><td>Section Name</td><td><input type='text' name='sectionname' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' value='".stripslashes($i[0])."'></td></tr><tr><td>Relations</td><td>";
$sql = mysql_query("SELECT * FROM ".$pre."sections WHERE type = '".$section."' ORDER BY `id` DESC");
while($r = mysql_fetch_array($sql)) {
if ($i[1] == "sub") {
$count = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."relations WHERE sub = '".$i[0]."' AND section = '".$r[name]."'"));
} else {
$count = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."relations WHERE sub = '".$r[name]."' AND section = '".$i[0]."'"));
}
echo "<input type='hidden' name='id[]' value='".$r[id]."'><input type='hidden' name='count_".$r[id]."' value='".$count."'>";
echo $r[name]." <input type='checkbox' name='sec_".$r[id]."' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' value='".$r[name]."'";
if ($count == "1") {
echo " checked>";
if ($i[1] == "sub") {
$q = mysql_fetch_row(mysql_query("SELECT id FROM ".$pre."relations WHERE sub = '".$i[0]."' AND section = '".$r[name]."'"));
} else {
$q = mysql_fetch_row(mysql_query("SELECT id FROM ".$pre."relations WHERE sub = '".$r[name]."' AND section = '".$i[0]."'"));
}
echo "<input type='hidden' name='theid_".$r[id]."' value='".$q[0]."'>";
}
echo "<br />";
}
echo "</td></tr><tr><td><input type='submit' value='Update ";
if ($i[1] == "sub") {
echo "Sub-";
}
echo "Section' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr></table></form>";
}
}

if ($_GET['view'] == "delete_section") {
$admin_page = "yes";
if ($p[2]) {
$query = @mysql_query("DELETE FROM ".$pre."sections WHERE id = '".addslashes($_GET["id"])."'");
@mysql_query("DELETE FROM ".$pre."fields WHERE cat = '".addslashes($_GET["name"])."'");
@mysql_query("DELETE FROM ".$pre."articles WHERE section = '".addslashes($_GET["name"])."'");
@mysql_query("DELETE FROM ".$pre."templates WHERE name = '".addslashes($_GET["name"])."'");
if ($_GET['type'] == "sub") {
@mysql_query("DELETE FROM ".$pre."relations WHERE sub = '".$_GET['name']."'");
} else {
@mysql_query("DELETE FROM ".$pre."relations WHERE section = '".$_GET['name']."'");
}
@mysql_query("DELETE FROM ".$pre."permissions WHERE name = '".addslashes($_GET["name"])."'");
mysql_query("ALTER TABLE ".$pre."sections  AUTO_INCREMENT =".$_GET['id']);

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=sections");
echo "The section has been deleted. <a href='admin.php?view=sections'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The section could not be deleted. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}
}

if ($_GET['view'] == "edit_section2") {
$admin_page = "yes";
if ($p[1]) {
$query = @mysql_query("UPDATE ".$pre."sections SET name = '".addslashes($_POST["sectionname"])."' WHERE id = '".$_GET['id']."'");
@mysql_query("UPDATE ".$pre."fields SET cat = '".addslashes($_POST["sectionname"])."' WHERE cat = '".$_POST["oldname"]."'");
@mysql_query("UPDATE ".$pre."articles SET section = '".addslashes($_POST["sectionname"])."' WHERE section = '".$_POST["oldname"]."'");
@mysql_query("UPDATE ".$pre."templates SET name = '".addslashes($_POST["sectionname"])."' WHERE name = '".$_POST["oldname"]."'");
if ($_POST['type'] == "sub") {
@mysql_query("UPDATE ".$pre."relations SET sub = '".addslashes($_POST["sectionname"])."' WHERE sub = '".$_POST["oldname"]."'");
} else {
@mysql_query("UPDATE ".$pre."relations SET section = '".addslashes($_POST["sectionname"])."' WHERE section = '".$_POST["oldname"]."'");
}

@mysql_query("UPDATE ".$pre."permissions SET name = '".addslashes($_POST["sectionname"])."' WHERE name = '".$_POST["oldname"]."'");

while (@list(, $i) = @each ($_POST['id'])) {

if ($_POST["sec_$i"]) {

if ($_POST["count_$i"] == "0") {
if ($_POST['type'] == "sub") {
@mysql_query("INSERT INTO ".$pre."relations VALUES (null, '".$_POST["sec_$i"]."', '".$_POST["sectionname"]."')");
} else {
@mysql_query("INSERT INTO ".$pre."relations VALUES (null, '".$_POST["sectionname"]."', '".$_POST["sec_$i"]."')");
}
}

} else {

if ($_POST["count_$i"] == "1") {
if ($_POST['type'] == "sub") {
@mysql_query("DELETE FROM ".$pre."relations WHERE id = '".$_POST["theid_$i"]."'");
} else {
@mysql_query("DELETE FROM ".$pre."relations WHERE id = '".$_POST["theid_$i"]."'");
}
}

}

}

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=sections");
echo "The section <b>".stripslashes($_POST['sectionname'])."</b> has been updated. <a href='admin.php?view=sections'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The section could not be updated. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}
}

if ($_GET['view'] == "add_sections2") {
$admin_page = "yes";
if ($p[0]) {
$query = @mysql_query("INSERT INTO ".$pre."sections VALUES (null, '".addslashes($_POST["sectionname"])."', '".$_POST['type']."')");
@mysql_query("INSERT INTO ".$pre."templates VALUES (null, '".addslashes($_POST["sectionname"])."', '', '".time()."')");

$sql = mysql_query("SELECT * FROM ".$pre."levels ORDER BY `id` DESC");
while($r = mysql_fetch_array($sql)) {
$q .= "INSERT INTO ".$pre."permissions VALUES (null, '".$r[name]."', '".addslashes($_POST["sectionname"])."', ";
if (($r[value] == "1") or ($r[value] == "2")) {
$q .= "1, 1, 1";
}
if ($r[value] > "2") {
$q .= "null, null, null";
}
$q .= ")";
@mysql_query($q);
$q = "";
}

while (@list(, $i) = @each ($_POST['id'])) {
if ($_POST["sec_$i"]) {
if ($_POST['type'] == "sub") {
@mysql_query("INSERT INTO ".$pre."relations VALUES (null, '".$_POST["sec_$i"]."', '".$_POST["sectionname"]."')");
} else {
@mysql_query("INSERT INTO ".$pre."relations VALUES (null, '".$_POST["sectionname"]."', '".$_POST["sec_$i"]."')");
}
}
}

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=sections");
echo "The section <b>".stripslashes($_POST['sectionname'])."</b> has been added. <a href='admin.php?view=sections'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The section could not be added. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}
}

if ($_GET['view'] == "denied") {
$admin_page = "yes";
echo "Sorry but it looks like you are not an admin or author, meaning you cannot access this area of the site";
}

if ($_GET['view'] == "support") {
$admin_page = "yes";
$user = $_SESSION[adaptcms_username];
echo "<center><a href='readme.php'>Read Me</a> | <a href='admin.php?view=support'>Support</a> | <a href='admin.php?view=help'>Help Files</a> | <a href='admin.php?view=faq'>FAQ</a> | <a href='admin.php?view=s2smessages'><u>S2S Messages</u></a><br /><br /><iframe src='http://www.insanevisions.com/support4.php?domain=".$domain."' width='500' height='500' frameborder='0'></iframe></center>";
}

if ($_GET['view'] == "support_search") {
$admin_page = "yes";
$user = $_SESSION[adaptcms_username];
echo "<center><a href='readme.php'>Read Me</a> | <a href='admin.php?view=support'>Support</a> | <a href='admin.php?view=help'>Help Files</a> | <a href='admin.php?view=faq'>FAQ</a> | <a href='admin.php?view=s2smessages'><u>S2S Messages</u></a><br /><br /><iframe src='http://www.adaptcms.com/support_search_lite.php?domain=".$domain."&search=".urlencode($_POST['search'])."' width='500' height='500' frameborder='0'></iframe></center>";
}

if ($_GET['view'] == "s2smessages") {
$admin_page = "yes";
$user = $_SESSION[adaptcms_username];
echo "<center><a href='readme.php'>Read Me</a> | <a href='admin.php?view=support'>Support</a> | <a href='admin.php?view=help'>Help Files</a> | <a href='admin.php?view=faq'>FAQ</a> | <a href='admin.php?view=s2smessages'><u>S2S Messages</u></a><br /><br /><iframe src='http://www.adaptcms.com/s2smessages_lite.php?domain=".$domain."' width='500' height='500' frameborder='0'></iframe></center>";
}

if ($_GET['view'] == "help") {
$admin_page = "yes";
$user = $_SESSION[adaptcms_username];
echo "<center><a href='readme.php'>Read Me</a> | <a href='admin.php?view=support'>Support</a> | <a href='admin.php?view=help'>Help Files</a> | <a href='admin.php?view=faq'>FAQ</a> | <a href='admin.php?view=s2smessages'><u>S2S Messages</u></a><br /><br /><iframe src='http://www.adaptcms.com/help4.php?domain=".$domain."' width='500' height='500' frameborder='0'></iframe></center>";
}

if ($_GET['view'] == "faq") {
$admin_page = "yes";
$user = $_SESSION[adaptcms_username];
echo "<center><a href='readme.php'>Read Me</a> | <a href='admin.php?view=support'>Support</a> | <a href='admin.php?view=help'>Help Files</a> | <a href='admin.php?view=faq'>FAQ</a> | <a href='admin.php?view=s2smessages'><u>S2S Messages</u></a><br /><br /><iframe src='http://www.adaptcms.com/faq4.php?domain=".$domain."' width='500' height='500' frameborder='0'></iframe></center>";
}

if ($_GET['view'] == "settings") {
$admin_page = "yes";
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;Settings / Manage Sections - <a href='admin.php?view=adds_section'>Add Section</a> - <a href='admin.php?view=adds_setting'>Add Setting</a><br />";
$sql = mysql_query("SELECT * FROM ".$pre."settings WHERE type = 'section' ORDER BY `name` ASC");
while($r = mysql_fetch_array($sql)) {
echo "<tr><td><a href='admin.php?view=setting&name=".$r[name]."'>".ucwords($r[name])."</a></td></tr>";
}
}

if ($_GET['view'] == "adds_section") {
$admin_page = "yes";
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=settings'>Settings</a> / Add Section<br /><br /><br />";
echo "<form action='admin.php?view=adds_section2' method='post'><table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td>Name</td><td><input type='text' name='name' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr><tr><td><tr><td><input type='submit' value='Add Section' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr></table></form>";
}

if ($_GET['view'] == "adds_section2") {
$admin_page = "yes";
$query = @mysql_query("INSERT INTO ".$pre."settings VALUES (null, '".addslashes($_POST["name"])."', '', '', 'section', '')");

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=setting&name=".addslashes($_POST["name"]));
echo "Section <b>".$_GET['name']."</b> has been added. <a href='admin.php?view=setting&name=".addslashes($_POST["name"])."'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "Section could not be added. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}

if ($_GET['view'] == "add_setting") {
$admin_page = "yes";
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=setting&name=".$_GET['name']."'>Settings</a> / Add Setting<br /><br /><br />";
echo "<form action='admin.php?view=add_setting2' method='post'><table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td>Section</td><td><select name='section'>";
$sql = mysql_query("SELECT * FROM ".$pre."settings WHERE type = 'section' ORDER BY `name` ASC");
while($r = mysql_fetch_array($sql)) {
if ($_GET['section'] == $r[name]) {
echo "<option value='".$r[name]."' selected>-- ".$r[name]." --</option>";
} else {
echo "<option value='".$r[name]."'>".$r[name]."</option>";
}
}
echo "</select></td></tr><tr><td>Name</td><td><input type='text' name='name' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr><tr><td>Data</td><td><input type='text' name='data' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr><tr><td>Description</td><td><textarea name='des' cols='60' rows='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></textarea></td></tr><tr><td><tr><td><input type='submit' value='Add ".ucwords($_GET['name'])." Setting' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr></table></form>";
}

if ($_GET['view'] == "add_setting2") {
$admin_page = "yes";
$_POST["name"] = str_replace(" ", "_", strtolower($_POST["name"]));
$query = @mysql_query("INSERT INTO ".$pre."settings VALUES (null, '".addslashes($_POST["name"])."', '".addslashes($_POST["des"])."', '".addslashes($_POST["data"])."', 'setting', '".addslashes($_POST["section"])."')");

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=setting&name=".$_GET['name']);
echo "Setting for <b>".$_GET['name']."</b> have been added. <a href='admin.php?view=setting&name=".$_GET['name']."'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "Setting could not be added. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}

if ($_GET['view'] == "setting") {
$admin_page = "yes";
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;Settings / <a href='admin.php?view=settings'>Manage Settings</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='admin.php?view=add_setting&section=".$_GET['name']."'>Add Setting</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='admin.php?view=adds_section'>Add Section</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='admin.php?view=del_section&id=".$_GET['name']."' onclick='return confirmDelete();'>Delete Section</a><br /><br /><br />";
echo "<form action='admin.php?view=edit_setting&name=".$_GET['name']."' method='post'><table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td><b>Setting Name</b></td><td><b>Data</b></td><td><b>Description</b></td><td><b>Delete?</b></tr>";
$sql = mysql_query("SELECT * FROM ".$pre."settings WHERE type = 'setting' AND section = '".$_GET['name']."' ORDER BY `name` ASC");
while($r = mysql_fetch_array($sql)) {
echo "<tr><td><input type='text' name='name_".$r[id]."' value='".stripslashes($r[name])."' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td><td>
<textarea name='data_".$r[id]."' cols='20' rows='2' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'>".stripslashes($r[data])."</textarea>
<input type='hidden' name='id[]' value='".$r[id]."'></td><td><textarea name='des_".$r[id]."' cols='23' rows='7' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'>".stripslashes($r[des])."</textarea></td><td><input type='checkbox' name='del_".$r[id]."' value='delete'></td></tr>";
}
echo "<tr><td><tr><td><input type='submit' value='Update ".ucwords($_GET['name'])." Settings' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr></table></form>";
}

if ($_GET['view'] == "del_section") {
$admin_page = "yes";
$query = @mysql_query("DELETE FROM ".$pre."settings WHERE name = '".addslashes($_GET["id"])."' AND type = 'section'");

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=settings");
echo "The setting section has been deleted. <a href='admin.php?view=settings'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The setting section could not be deleted. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}

if ($_GET['view'] == "edit_setting") {
$admin_page = "yes";
while (@list(, $i) = @each ($_POST['id'])) {
if ($_POST["del_$i"]) {
$query = @mysql_query("DELETE FROM ".$pre."settings WHERE id = '".$i."'");
} else {
$query = @mysql_query("UPDATE ".$pre."settings SET data = '".addslashes($_POST["data_$i"])."', name = '".addslashes($_POST["name_$i"])."', des = '".addslashes($_POST["des_$i"])."' WHERE id = '".$i."'");
}
}

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=setting&name=".$_GET['name']);
echo "Settings for <b>".$_GET['name']."</b> have been updated/deleted. <a href='admin.php?view=setting&name=".$_GET['name']."'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "Settings could not be updated/deleted. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}

$p = "";
$p = mysql_fetch_row(mysql_query("SELECT padd,pedit,pdelete FROM ".$pre."permissions WHERE level = '".$level."' AND name = 'gallery'"));
if ((($p[0]) or ($p[1]) or ($p[2]))) {
if ($_GET['view'] == "comments") {
$admin_page = "yes";
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;Comments / Manage Comments<br /><br /><br />";
echo "<table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td><b>Article</b></td><td><b>Username</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."comments ORDER BY `id` DESC LIMIT $from, ".$setting["admin_limit"]);
while($r = mysql_fetch_array($sql)) {
$fetch = mysql_fetch_row(mysql_query("SELECT name,section FROM ".$pre."articles WHERE id = '".$r[aid]."'"));
echo "<tr><td>";
if ($r[aid] > 0) {
echo "<a href='".$mrw_1["content"].fthrough($fetch[0], $fetch[1], $r[aid])."'>".stripslashes($fetch[0])."</a>";
} else {
echo "<a href='index.php?view=shoutbox'>Shoutbox</a>";
}
echo "</td><td><a href='".$mrw_1["user"].$r[author]."'>".$r[author]."</a></td><td>".date($setting["date_format"], $r[date])."</td><td><a href='admin.php?view=edit_comments&id=".$r[id]."'>Edit</a> | <a href='admin.php?view=delete_comments&id=".$r[id]."' onclick='return confirmDelete();'>Delete</a></td></tr>";
}
echo "</table><br clear='all'>";
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."comments WHERE aid > 0"),0);
$total_comments = ceil($total_results / $setting["admin_limit"]);

if ($total_comments > "1") {

echo "<center>";


if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"admin.php?view=comments&page=$prev\"><<Previous</a>&nbsp;";
}

for($i = 1; $i <= $total_comments; $i++){
    if(($page) == $i){
        echo "$i&nbsp;";
        } else {
            echo "<a href=\"admin.php?view=comments&page=$i\">$i</a>&nbsp;";
			if (($i/25) == (int)($i/25)) {echo "<br />";}
    }
}


if($page < $total_comments){
    $next = ($page + 1);
    echo "<a href=\"admin.php?view=comments&page=$next\">Next>></a>";
}
echo "</center>";
}
echo "<tr><td><br /><br /><small>To view the article that this comment is shown on, click onto the article name.</small></td></tr>";
}
}

if ($_GET['view'] == "edit_comments") {
$admin_page = "yes";
$data = mysql_fetch_row(mysql_query("SELECT comment,author,aid FROM ".$pre."comments WHERE id = '".$_GET['id']."'"));
$fetch = mysql_fetch_row(mysql_query("SELECT name,section FROM ".$pre."articles WHERE id = '".$data[2]."'"));

$data[0] = stripslashes(str_replace("<br />", "", stripslashes($data[0])));
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=comments'>Comments</a> / Edit Comment<br /><br /><br />";
echo "<form action='admin.php?view=edit_comments2&id=".$_GET['id']."' method='post'><table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td>Article</td><td>";

if ($data[2] > 0) {
echo "<a href='".$mrw_1["content"].fthrough($fetch[0], $fetch[1], $r[aid])."'>".stripslashes($fetch[0])."</a>";
} else {
echo "<a href='index.php?view=shoutbox'>Shoutbox</a>";
}

echo "</td></tr><tr><td>Commenter</td><td><a href='".$mrw_1["user"].$data[1]."'>".$data[1]."</a></td></tr><tr><td>Comment</td><td><textarea name='comment' cols='60' rows='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'>".$data[0]."</textarea></td></tr><tr><td><input type='submit' value='Update Comment' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr></table></form>";
}

if ($_GET['view'] == "edit_comments2") {
$admin_page = "yes";
$autobr = preg_replace("/\n/", "<br />\n", addslashes($_POST['comment']));

$query = mysql_query("UPDATE ".$pre."comments SET comment = '".$autobr."' WHERE id = '".$_GET['id']."'");

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=comments");
echo "Comment has been updated. <a href='admin.php?view=comments'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "Comment could not be updated. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}

if ($_GET['view'] == "delete_comments") {
$admin_page = "yes";
$query = @mysql_query("DELETE FROM ".$pre."comments WHERE id = '".addslashes($_GET["id"])."'");
mysql_query("ALTER TABLE ".$pre."comments  AUTO_INCREMENT =".$_GET['id']);

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=comments");
echo "The comment has been deleted. <a href='admin.php?view=comments'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The comment could not be deleted. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}

$p = "";
$p = mysql_fetch_row(mysql_query("SELECT padd,pedit,pdelete FROM ".$pre."permissions WHERE level = '".$level."' AND name = 'pages'"));
if ($_GET['view'] == "pages") {
$admin_page = "yes";
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;Pages / Manage Pages";
if ($p[0]) {
echo " - <a href='admin.php?view=add_page'>Add Page</a>";
}
echo "<br /><br /><br />";
echo "<table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td><b>Page Name</b></td><td><b>Username</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."pages ORDER BY `id` DESC LIMIT $from, ".$setting["admin_limit"]);
while($r = mysql_fetch_array($sql)) {
echo "<tr><td><a href='".$mrw_1["page"].stripslashes($r[url])."'>".stripslashes($r[name])."</a></td><td><a href='".$mrw_1["user"].$r[username]."'>".$r[username]."</a></td><td>".date($setting["date_format"], $r[date])."</td><td>";
if ($p[1]) {
echo "<a href='admin.php?view=edit_pages&id=".$r[id]."'>Edit</a> | ";
}
if ($p[2]) {
echo "<a href='admin.php?view=delete_pages&id=".$r[id]."' onclick='return confirmDelete();'>Delete</a>";
}
echo "</td></tr>";
}
echo "</table><br clear='all'>";
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."pages"),0);
$total_pages = ceil($total_results / $setting["admin_limit"]);

if ($total_pages > "1") {

echo "<center>";


if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"admin.php?view=pages&page=$prev\"><<Previous</a>&nbsp;";
}

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


if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"admin.php?view=pages&page=$next\">Next>></a>";
}
echo "</center>";
}
echo "<tr><td><br /><br /><small>To view the static page click onto the name. Clicking onto the username will take you to that users profile page.</small></td></tr>";
}

if ($_GET['view'] == "add_page") {
$admin_page = "yes";
if ($p[0]) {
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=pages'>Pages</a> / Add Page<br /><br /><br />";
echo "<form action='admin.php?view=add_pages2' method='post'><table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td>Page Name</td><td><input type='text' name='pagename' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr><tr><td>URL</td><td><input type='text' name='url' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'>&nbsp;&nbsp;(ex. contactus)</td></tr><tr><td>Page Content</td><td><textarea name='content' cols='56' rows='14' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></textarea></td></tr><tr><td><input type='submit' value='Add Page' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td><td><a href=\"javascript:;\" onmousedown=\"tinyMCE.get('content').show();\">[Show]</a><a href=\"javascript:;\" onmousedown=\"tinyMCE.get('content').hide();\">[Hide]</a></td></tr></form>";
}
}

if ($_GET['view'] == "edit_pages") {
$admin_page = "yes";
if ($p[1]) {
$r = mysql_fetch_row(mysql_query("SELECT name,url,content FROM ".$pre."pages WHERE id = '".$_GET['id']."'"));
$r[2] = str_replace("<br />", "", $r[2]);
$r[2] = str_replace("</textarea>", "&lt;/textarea&gt;", $r[2]);
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=pages'>Pages</a> / Edit Page<br /><br /><br />";
echo "<form action='admin.php?view=edit_pages2' method='post'><table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td>Page Name</td><td><input type='text' name='pagename' size='16' value='".$r[0]."' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr><tr><td>URL</td><td><input type='text' name='url' size='16' value='".$r[1]."' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'>&nbsp;&nbsp;(ex. contactus)</td></tr><tr><td>Page Content</td><td><textarea name='content' cols='60' rows='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'>".stripslashes($r[2])."</textarea><input type='hidden' name='id' value='".$_GET['id']."'></td></tr><tr><td><input type='submit' value='Update Page' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td><td><a href=\"javascript:;\" onmousedown=\"tinyMCE.get('content').show();\">[Show]</a><a href=\"javascript:;\" onmousedown=\"tinyMCE.get('content').hide();\">[Hide]</a></td></tr></form>";
}
}

if ($_GET['view'] == "delete_pages") {
$admin_page = "yes";
if ($p[2]) {
$query = @mysql_query("DELETE FROM ".$pre."pages WHERE id = '".addslashes($_GET["id"])."'");
mysql_query("ALTER TABLE ".$pre."pages  AUTO_INCREMENT =".$_GET['id']);

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=pages");
echo "The page has been deleted. <a href='admin.php?view=pages'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The page could not be deleted. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}
}

if ($_GET['view'] == "edit_pages2") {
$admin_page = "yes";
if ($p[1]) {
if ($_POST['autobr']) {
$content = preg_replace("/\n/", "<br />\n", addslashes($_POST['content']));
} else {
$content = addslashes($_POST['content']);
}

$query = @mysql_query("UPDATE ".$pre."pages SET name = '".addslashes($_POST["pagename"])."', url = '".addslashes($_POST["url"])."', content = '".$content."' WHERE id = '".$_POST['id']."'");

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=pages");
echo "The page <b>".stripslashes($_POST['name'])."</b> has been updated. <a href='admin.php?view=pages'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The page could not be updated. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}
}

if ($_GET['view'] == "add_pages2") {
$admin_page = "yes";
if ($p[0]) {
if ($_POST['autobr']) {
$content = preg_replace("/\n/", "<br />\n", addslashes($_POST['content']));
} else {
$content = addslashes($_POST['content']);
}

$query = @mysql_query("INSERT INTO ".$pre."pages VALUES (null, '".addslashes($_POST["pagename"])."', '".addslashes($_POST["url"])."', '".$content."', '".mysql_real_escape_string(stripslashes($_SESSION[adaptcms_username]))."', '".time()."')");

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=pages");
echo "The page <b>".stripslashes($_POST['name'])."</b> has been added. <a href='admin.php?view=pages'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The page could not be added. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}
}

$p = "";
$p = mysql_fetch_row(mysql_query("SELECT padd,pedit,pdelete FROM ".$pre."permissions WHERE level = '".$level."' AND name = 'polls'"));
if ($_GET['view'] == "polls") {
$admin_page = "yes";
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;Polls / Manage Polls";
if ($p[0]) {
echo "- <a href='admin.php?view=add_poll'>Add Poll</a>";
}
echo "<br /><br /><br />";
echo "<table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td><b>Poll Name</b></td><td><b>Date</b></td><td align='center'><b>Options</b></td><td align='center'><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."polls WHERE type = 'poll' ORDER BY `id` DESC LIMIT $from, ".$setting["admin_limit"]);
while($r = mysql_fetch_array($sql)) {
$ex = @explode(",",$r[options]);
echo "<tr><td><a href='admin.php?view=add_polls&id=".urlencode($r[name])."'>".stripslashes($r[name])."</a></td><td>".date($setting["date_format"], $r[date])."</td><td>";
if ($p[1]) {
echo "<a href='admin.php?view=add_polls&id=".urlencode($r[name])."'>Add Options</a></td><td>";
}
if ($p[1]) {
echo "<a href='admin.php?view=edit_poll&id=".urlencode($r[id])."&name=".urlencode($r[name])."&multiple=".$ex[0]."&custom=".$ex[1]."'>Edit</a> | ";
}
if ($p[2]) {
echo "<a href='admin.php?view=delete_poll&id=".urlencode($r[name])."' onclick='return confirmDelete();'>Delete</a> | ";
}
echo "<a href='admin.php?view=poll_code&pname=".urlencode($r[name])."'>Code</a></td></tr>";
}
echo "</table><br clear='all'>";
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."polls WHERE type = 'poll'"),0);
$total_pages = ceil($total_results / $setting["admin_limit"]);

if ($total_pages > "1") {

echo "<center>";


if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"admin.php?view=polls&page=$prev\"><<Previous</a>&nbsp;";
}

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


if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"admin.php?view=polls&page=$next\">Next>></a>";
}
echo "</center>";
}
}

if ($_GET['view'] == "add_polls") {
$admin_page = "yes";
if ($p[0]) {
echo "<script>
  function forminsertrow(theForm) {
    var insertHere = document.getElementById('options');
    var newElement = document.getElementById('options').cloneNode(true);
    insertHere.parentNode.insertBefore(newElement, insertHere);
}
</script>";
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;Polls / Add Poll Option(s)&nbsp;&nbsp;&nbsp;&nbsp;<a href='admin.php?view=poll_code&pname=".stripslashes(urlencode(stripslashes($_GET['id'])))."'>Get Code</a><br /><br /><br />";

echo "<form action='admin.php?view=add_polls2' method='post'><input type='hidden' name='poll' value='".stripslashes(urlencode(stripslashes($_GET['id'])))."'><table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td><b>".stripslashes(urldecode(stripslashes($_GET['id'])))."</b><br /><br clear='all' />";

echo "<p id='options'>Option <input type='text' name='options[]' value=''></p>";

echo "<br /></td></tr><tr><td><input type='submit' value='Submit' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'>&nbsp;&nbsp;<input name='anotherbutton' id='anotherbutton' value='Add Option' onclick='forminsertrow(this.form)' type='button' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' /></td></tr></table></form>";
}
}

if ($_GET['view'] == "poll_code") {
$admin_page = "yes";
if ((($p[0]) or ($p[1]) or ($p[2]))) {
$id = mysql_fetch_row(@mysql_query("SELECT id FROM ".$pre."polls WHERE name = '".urldecode($_GET['pname'])."'"));
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;Polls / Poll Code";
echo "<tr><td>Copy the below code into any template (layout, reviews, whichever) and the poll will show up:<br /><br /><textarea style='width:458px; height:45px; font-family: tahoma;font-size: 11px;color: #4a4a4a; font-weight: bold' rows='3' readonly='readonly'>&lt;?php echo show_poll(\"".$id[0]."\", \"".stripslashes(urldecode(stripslashes($_GET['pname'])))."\"); ?&gt;</textarea></td></tr>";
}
}

if ($_GET['view'] == "add_poll") {
$admin_page = "yes";
if ($p[0]) {
echo "<script>
  function forminsertrow(theForm) {
    var insertHere = document.getElementById('options');
    var newElement = document.getElementById('options').cloneNode(true);
    insertHere.parentNode.insertBefore(newElement, insertHere);
}
</script>";
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;Polls / Add Poll<br /><br /><br />";
echo "<form action='admin.php?view=add_poll2' method='post'><table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td>Poll</td><td><input type='text' name='poll' size='25' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr><tr><td>Multiple options selectable?</td><td><input type='checkbox' name='multiple' value='yes' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td><td><small>Checked, a visitor can select more than one option for the poll</small></td></tr><tr><td>Custom options?</td><td><input type='checkbox' name='custom' value='yes' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td><td><small>Checked, a user can enter in a custom poll option. (see <a href='admin.php?view=setting&name=Polls'>here</a> for settings)</small></td></tr></table><br clear='all' /><br /><br /><table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td>";

echo "<p id='options'>Option <input type='text' name='options[]' value=''></p>";

echo "<br /></td></tr><tr><td><input type='submit' value='Add Poll' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'>&nbsp;&nbsp;<input name='anotherbutton' id='anotherbutton' value='Add Option' onclick='forminsertrow(this.form)' type='button' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' /></td></tr></table></form>";
}
}

if ($_GET['view'] == "edit_poll") {
$admin_page = "yes";
if ($p[1]) {
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;Polls / Edit Poll&nbsp;&nbsp;&nbsp;&nbsp;<a href='admin.php?view=poll_code&pname=".$_GET['name']."'>Get Code</a><br /><br /><br />";
echo "<form action='admin.php?view=edit_poll2' method='post'><table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td>Poll</td><td><input type='text' name='poll' size='25' value='".stripslashes(urldecode($_GET['name']))."' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr><tr><td>Multiple options selectable?</td><td><input type='checkbox' name='multiple' value='yes' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'";
if ($_GET['multiple']) {
echo " checked";
}
echo "></td><td><small>Checked, a visitor can select more than one option for the poll</small></td></tr><tr><td>Custom options?</td><td><input type='checkbox' name='custom' value='yes' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'";
if ($_GET['custom']) {
echo " checked";
}
echo "></td><td><small>Checked, a user can enter in a custom poll option. (see <a href='admin.php?view=setting&name=Polls'>here</a> for settings)</small></td></tr><input type='hidden' name='id' value='".stripslashes(urldecode(stripslashes($_GET['id'])))."'><input type='hidden' name='oldpoll' value='".stripslashes(urldecode($_GET['name']))."'></td></tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>";

$sql = mysql_query("SELECT * FROM ".$pre."polls WHERE type = 'option' AND pname = '".urldecode($_GET['name'])."' ORDER BY `id` DESC");
for($i = 1; $r = mysql_fetch_assoc($sql); $i++) {
echo "<input type='hidden' name='ids[]' value='".$r[id]."'><tr><td>Poll Option #".$i."</td><td><input type='text' name='option_".$r[id]."' value=\"".htmlspecialchars(stripslashes($r[name]))."\" style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td><td>Delete? <input type='checkbox' name='del_".$r[id]."' value='delete' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr>";
}

echo "<tr><td><input type='submit' value='Update Poll' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr></table></form>";
}
}

if ($_GET['view'] == "delete_poll") {
$admin_page = "yes";
if ($p[2]) {
$query = @mysql_query("DELETE FROM ".$pre."polls WHERE pname = '".urldecode($_GET["id"])."'");
mysql_query("ALTER TABLE ".$pre."polls  AUTO_INCREMENT =".$_GET['id']);

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=polls");
echo "The poll <b>".stripslashes(urldecode(stripslashes($_POST['poll'])))."</b> has been deleted. <a href='admin.php?view=polls'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The poll could not be deleted. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}
}

if ($_GET['view'] == "edit_poll2") {
$admin_page = "yes";
if ($p[1]) {
while (@list(, $i) = @each ($_POST['ids'])) {
if ($_POST["del_$i"]) {
$query = @mysql_query("DELETE FROM ".$pre."polls WHERE id = '".$i."'");
} else {
$query = @mysql_query("UPDATE ".$pre."polls SET name = '".addslashes($_POST["option_$i"])."' WHERE id = '".$i."'");
}
}

$opt = $_POST['multiple'].",".$_POST['custom'];
$query = @mysql_query("UPDATE ".$pre."polls SET name = '".addslashes($_POST["poll"])."', options = '".$opt."' WHERE id = '".$_POST['id']."'");
$query2 = @mysql_query("UPDATE ".$pre."polls SET pname = '".addslashes($_POST["poll"])."' WHERE pname = '".addslashes($_POST["oldpoll"])."'");

if (($query == TRUE) && ($query2 == TRUE)) {
echo re_direct("1500", "admin.php?view=polls");
echo "The poll <b>".stripslashes(urldecode(stripslashes($_POST['poll'])))."</b> has been updated. <a href='admin.php?view=polls'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The poll could not be updated. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}
}

if ($_GET['view'] == "add_polls2") {
$admin_page = "yes";
if ($p[0]) {
while (list(, $i) = each ($_POST['options'])) {
$query = @mysql_query("INSERT INTO ".$pre."polls VALUES (null, '".$i."', 'option', '', '".urldecode($_POST["poll"])."', '0', '".time()."')");
}

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=polls");
echo "Poll options for <b>".stripslashes(urldecode(stripslashes($_POST['poll'])))."</b> have been added. <a href='admin.php?view=polls'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "Poll options could not be added. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}
}

if ($_GET['view'] == "add_poll2") {
$admin_page = "yes";
if ($p[0]) {
$opt = $_POST['multiple'].",".$_POST['custom'];
$query = @mysql_query("INSERT INTO ".$pre."polls VALUES (null, '".addslashes($_POST["poll"])."', 'poll', '".$opt."', '".addslashes($_POST["poll"])."', '0', '".time()."')");
while (list(, $i) = each ($_POST['options'])) {
$query = @mysql_query("INSERT INTO ".$pre."polls VALUES (null, '".$i."', 'option', '', '".urldecode($_POST["poll"])."', '0', '".time()."')");
}

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=polls");
echo "The poll <b>".stripslashes(urldecode(stripslashes($_POST['poll'])))."</b> has been added. <a href='admin.php?view=polls'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The poll could not be added. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}
}

if ($_GET['view'] == "tools") {
$admin_page = "yes";

echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;Tools / List of Tools<br /><br /><br />";
echo "<table cellpadding='2' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'>";

echo "<tr><td><a href='admin.php?view=tool_url_list'>URL List</a></td><td>AdaptCMS is a fairly large script and has a lot to it. With this tool you can see all the frontend url's, from content to an RSS Feed by section.</td></tr><tr><td><a href='admin.php?view=tool_mod_rewrite'>Mod Rewrite</a></td><td>With this tool, you can easily and quickly edit your .htaccess file. (mod_rewrite must be enabled on your server)</td></tr><tr><td><a href='admin.php?view=tool_php'>PHP Tags</a></td><td>This will list PHP tags that you can use in your layout or templates. Such as listing content and showing a poll.</td></tr><tr><td><a href='admin.php?view=tool_mass_email'>Mass E-Mail</a></td><td>With this Mass E-Mail tool, you can easily send an email to all users or by user level.</td></tr><tr><td><a href='admin.php?view=tool_backup'>Backup</a></td><td>This backup tool lets you easily backup your AdaptCMS SQL in a easy-to-use 3-step feature.</td></tr><tr><td><a href='admin.php?view=tool_delete_cache'>Delete Cache</a></td><td>If you have the cache option turned on, then the size of cache may get big, click here to delete all of it.</td></tr></table>";

$setting['sitename'] = str_replace(" ", "_", $setting['sitename']);
echo file_get_contents("http://www.adaptcms.com/tools.php?sitename=".urlencode($setting['sitename'])."&siteurl=".urlencode($siteurl)."&version=".urlencode($version)."&domain=".$domain);
}

if ($_GET['view'] == "tool_url_list") {
$admin_page = "yes";
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=tools'>Tools</a> / URL List<br />";
echo "<center><h2>Table of Contents</h2></center><ul><li><a href='#articles'>Articles</a></li><li><a href='#sections'>Sections</a></li>
<ul><li><a href='#pages'>Pages</a></li><li><a href='#users'>Users</a></li><li><a href='#gallery'>Gallery</a></li><li><a href='#misc'>Misc</a></li></ul><li><a href='#bottom'>Bottom of Page</a></li></ul>";
echo "<center><a name='articles'></a><h2>Articles</h2></center><table cellpadding='5' cellspacing='0' border='0' width='490' style='padding-left:5px' align='center'><tr><td valign='top'>";

$tot_art2 = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."articles WHERE ver = ''"));
$tot_art = $tot_art2 / 2;
$tot_art = round($tot_art);

$i = 0;
$sql = mysql_query("SELECT * FROM ".$pre."articles WHERE ver = '' ORDER BY `name` ASC");
while($r = mysql_fetch_array($sql)) {
$i = $i + 1;
if ($i == $tot_art) {
echo "</td><td valign='top'>";
}
echo "<a href='".$mrw_1["content"].fthrough($r[name], $r[section], $r[id])."'>";
if (strlen($r[name]) > 33) {
echo substr(stripslashes($r[name]),0,30)."...";
} else {
echo substr(stripslashes($r[name]),0,33);
}
echo " (".ucfirst($r[section]).")</a><br />- <a href='".$mrw_1["content"].fthrough($r[name], $r[section], $r[id])."trackback/' rel='trackback'><font color='green'>Trackback</font></a>&nbsp;&nbsp;<a href='".$mrw_1["content"].fthrough($r[name], $r[section], $r[id])."#comments'><font color='red'>Comments</font></a><br />";
}
$i = 0;
echo "</td></tr></table><br clear='all' /><center><a name='sections'></a><h2>Sections</h2></center><table cellpadding='5' cellspacing='0' border='0' width='490' style='padding-left:5px' align='left'><tr><td>";
$sql = mysql_query("SELECT * FROM ".$pre."sections ORDER BY `name` ASC");
while($r = mysql_fetch_array($sql)) {
$i = $i + 1;
if ($i == 6) {
echo "</tr><tr><td>&nbsp;</td>";
}
echo "<td><a href='".$mrw_1["section"].$r[name]."/'>".stripslashes($r[name])."</a><br />";
echo "<a href='".$mrw_1["archive"].$r[name]."/'>Archive</a><br /><a href='".$mrw["rss"].$r[name]."/'>RSS Feed</a><br /><br />";
for ($char = 65; $char < 91; $char++) {
echo "<a href='".$mrw_1["archive"].$r[name]."/letter/".chr($char)."/'>Archive (".chr($char).")</a><br />";
}
echo "</td>";
}
echo "</td></tr></table><br /><br clear='all' /><table cellpadding='5' cellspacing='0' border='0' width='490' style='padding-left:5px' align='center'><tr><td valign='top'><a name='pages'></a><h2>Pages</h2>";
$sql = mysql_query("SELECT * FROM ".$pre."pages ORDER BY `name` ASC");
while($r = mysql_fetch_array($sql)) {
echo "<a href='".$mrw_1["page"].$r[url]."/'>".stripslashes($r[name])."</a><br />";
}
echo "</td><td valign='top'><a name='users'></a><h2>Users</h2>- <a href='".$mrw_1["login"]."login'>Login</a><br />- <a href='".$mrw_1["register"]."register'>Register</a><br />- <a href='".$mrw_1["logout"]."logout'>Logout</a><br />- <a href='".$mrw["edit_profile"]."'>Edit Profile</a><br />- <a href='".$mrw["members_list"]."'>Members List</a><br /><br />";
$sql = mysql_query("SELECT * FROM ".$pre."users ORDER BY `username` ASC");
while($r = mysql_fetch_array($sql)) {
echo "<a href='".$mrw_1["user"].fthrough($r[username],"","")."/'>".stripslashes($r[username])."</a><br />";
}
echo "</td><td valign='top'><a name='gallery'></a><h2>Gallery</h2>- <a href='".$mrw_1["gallery"]."'>Gallery</a><br /><br />";
$sql = mysql_query("SELECT * FROM ".$pre."gallery ORDER BY `name` ASC");
while($r = mysql_fetch_array($sql)) {
echo "<a href='".$mrw_1["gallery"].fthrough($r[name], "", $r[id])."'>".stripslashes($r[name])."</a><br />";
}
echo "</td><td valign='top'><a name='misc'></a><h2>Misc</h2><a href='".$mrw["affiliates"]."'>Affiliates</a><br /><a href='".$mrw["af_apply"]."'>Affiliate Application</a><br /><a href='".$mrw["archive"]."'>Articles Archive</a><br /><a href='".$siteurl."polls/'>Polls Archive</a><br /><a href='".$mrw["polls"]."'>Poll Results</a><br /><a href='".$mrw["rss"]."'>RSS Feed</a><br /><a href='".$mrw["search"]."'>Search</a><br /><a href='".$mrw["poll_results"]."'>Submit Article</a><br /></td></tr></table><a name='bottom'></a>";
}

if ($_GET['view'] == "tool_mod_rewrite") {
$admin_page = "yes";

echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=tools'>Tools</a> / Mod Rewrite<br /><br /><br />";

echo "<form action='admin.php?view=tool_mod_rewrite2' method='post'><table cellpadding='2' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'>";

$handle = fopen($sitepath.".htaccess", "r");
$contents = fread($handle, filesize($sitepath.".htaccess"));
fclose($handle);

echo "<tr><td><b>.htaccess File</b></td><td><textarea name='contents' cols='65' rows='20' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'>".$contents."</textarea></td></tr><tr><td><input type='submit' value='Update File' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td><td>&nbsp;</td></tr></table></form>";
}


if ($_GET['view'] == "tool_mod_rewrite2") {
$admin_page = "yes";

echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=tools'>Tools</a> / Mod Rewrite<br /><br /><br />";

$handle = fopen($sitepath.".htaccess", 'w');
fwrite($handle, stripslashes($_POST['contents']));
fclose($handle);

if ($handle == TRUE) {
echo re_direct("1500", "admin.php?view=tools");
echo "The .htaccess file was modified. <a href='admin.php?view=tools'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The .htaccess file was not modified. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}

if ($_GET['view'] == "tool_backup") {
$admin_page = "yes";

echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=tools'>Tools</a> / Backup<br /><br /><br />";

echo "<form action='admin.php?view=tool_backup2' method='post'><center><select name='type[]' multiple><option value='".$pre."articles,".$pre."fielddata,".$pre."fields,".$pre."comments,".$pre."sections'>Articles</option><option value='".$pre."settings,".$pre."levels,".$pre."relations,".$pre."templates'>Config</option><option value='".$pre."gallery,".$pre."polls,".$pre."files,".$pre."pages,".$pre."ranks'>Miscellaneous</option><option value='".$pre."plugins'>Plugins</option><option value='".$pre."users,".$pre."permissions'>Users</option></select><br /><br /><input type='submit' value='Backup Data'></center></form>";
}

if ($_GET['view'] == "tool_backup3") {
$admin_page = "yes";

echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=tools'>Tools</a> / Backup<br /><br /><br />";

if (unlink($sitepath."upload/backup.sql") == TRUE) {
echo re_direct("1500", "admin.php?view=tools");
echo "Your backup has been deleted. <a href='admin.php?view=tools'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "Your backup has not been deleted. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}

if ($_GET['view'] == "tool_backup2") {
$admin_page = "yes";

echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=tools'>Tools</a> / Backup<br /><br /><br />";

$bdir = $sitepath."upload/";
while (list(, $i) = each ($_POST['type'])) {
if (preg_match("/,/", $i)) {
$x = explode(",", $i);

while (list(, $v) = each ($x)) {

$vnum = 0;
$sql = mysql_query("SELECT * FROM ".$v." ORDER BY `id` ASC");
while($r = mysql_fetch_array($sql)) {
$vnum = $vnum + 1;
if ($vnum == 1) {
if ($v == $pre."articles") {
$content .= "
CREATE TABLE `".$pre."articles` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `name` text NOT NULL,
  `username` text NOT NULL,
  `section` text NOT NULL,
  `ver` text NOT NULL,
  `date` int(8) NOT NULL,
  `mdate` int(8) NOT NULL,
  `ydate` int(8) NOT NULL,
  `rate` varchar(30) NOT NULL default '0|0',
  `views` int(11) NOT NULL default '0',
  PRIMARY KEY  (`id`));
  
";
}
if ($v == $pre."comments") {
$content .= "
CREATE TABLE `".$pre."comments` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `aid` text NOT NULL,
  `comment` text NOT NULL,
  `author` text NOT NULL,
  `email` text NOT NULL,
  `url` text NOT NULL,
  `ip` text NOT NULL,
  `date` int(12) NOT NULL,
  PRIMARY KEY  (`id`));
  
";
}
if ($v == $pre."fielddata") {
$content .= "
CREATE TABLE `".$pre."fielddata` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `fname` text NOT NULL,
  `data` text NOT NULL,
  `aid` text NOT NULL,
  PRIMARY KEY  (`id`));
  
";
}
if ($v == $pre."fields") {
$content .= "
CREATE TABLE `".$pre."fields` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `name` text NOT NULL,
  `cat` text NOT NULL,
  `type` text NOT NULL,
  `des` text NOT NULL,
  `info` text NOT NULL,
  PRIMARY KEY  (`id`));
  
";
}
if ($v == $pre."files") {
$content .= "
CREATE TABLE `".$pre."files` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `filename` text NOT NULL,
  `filedir` text NOT NULL,
  `gallery` text NOT NULL,
  `rate` varchar(30) NOT NULL default '0|0',
  `date` int(12) NOT NULL,
  PRIMARY KEY  (`id`));
  
";
}
if ($v == $pre."gallery") {
$content .= "
CREATE TABLE `".$pre."gallery` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `name` text NOT NULL,
  PRIMARY KEY  (`id`));
  
";
}
if ($v == $pre."levels") {
$content .= "
CREATE TABLE `".$pre."levels` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `name` text NOT NULL,
  `value` text NOT NULL,
  `options` text NOT NULL,
  PRIMARY KEY  (`id`));
  
";
}
if ($v == $pre."pages") {
$content .= "
CREATE TABLE `".$pre."pages` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `name` text NOT NULL,
  `url` text NOT NULL,
  `content` text NOT NULL,
  `username` text NOT NULL,
  `date` int(12) NOT NULL,
  PRIMARY KEY  (`id`));
  
";
}
if ($v == $pre."permissions") {
$content .= "
CREATE TABLE `".$pre."permissions` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `level` text NOT NULL,
  `name` text NOT NULL,
  `padd` text NOT NULL,
  `pedit` text NOT NULL,
  `pdelete` text NOT NULL,
  PRIMARY KEY  (`id`));
  
";
}
if ($v == $pre."plugins") {
$content .= "
CREATE TABLE `".$pre."plugins` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `name` text NOT NULL,
  `url` text NOT NULL,
  `version` varchar(5) NOT NULL,
  `status` varchar(5) NOT NULL,
  PRIMARY KEY  (`id`));
  
";
}
if ($v == $pre."polls") {
$content .= "
CREATE TABLE `".$pre."polls` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `name` text NOT NULL,
  `type` text NOT NULL,
  `options` text NOT NULL,
  `pname` text NOT NULL,
  `votes` text NOT NULL,
  `date` int(12) NOT NULL,
  PRIMARY KEY  (`id`));
  
";
}
if ($v == $pre."ranks") {
$content .= "
CREATE TABLE `".$pre."ranks` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `name` text NOT NULL,
  `color` text NOT NULL,
  `image` text NOT NULL,
  `prequired` text NOT NULL,
  PRIMARY KEY  (`id`));
  
";
}
if ($v == $pre."relations") {
$content .= "
CREATE TABLE `".$pre."relations` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `section` text NOT NULL,
  `sub` text NOT NULL,
  PRIMARY KEY  (`id`));
  
";
}
if ($v == $pre."sections") {
$content .= "
CREATE TABLE `".$pre."sections` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `name` text NOT NULL,
  `type` text NOT NULL,
  PRIMARY KEY  (`id`));
  
";
}
if ($v == $pre."settings") {
$content .= "
CREATE TABLE `".$pre."settings` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `name` text NOT NULL,
  `des` text NOT NULL,
  `data` text NOT NULL,
  `type` text NOT NULL,
  `section` text NOT NULL,
  PRIMARY KEY  (`id`));
  
";
}
if ($v == $pre."templates") {
$content .= "
CREATE TABLE `".$pre."templates` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `name` text NOT NULL,
  `template` text NOT NULL,
  `date` int(12) NOT NULL,
  PRIMARY KEY  (`id`));
  
";
}
if ($v == $pre."users") {
$content .= "
CREATE TABLE `".$pre."users` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `username` text NOT NULL,
  `password` text NOT NULL,
  `email` text NOT NULL,
  `level` text NOT NULL,
  `rank` text NOT NULL,
  `logged` text NOT NULL,
  `date` int(12) NOT NULL,
  PRIMARY KEY  (`id`));
  
";
}
}
$content .= "INSERT INTO `".$v."` VALUES (".$r[id]."";

$sql2 = mysql_query("SHOW COLUMNS FROM ".$v);
while ($row = mysql_fetch_assoc($sql2)) {
$name = $row[Field];

if ($name == "id") {
} else {
$content .= ", '".addslashes($r["$name"])."'";
}
}
$content .= ");
";
}


}
}
}

$content .= "
CREATE TABLE `".$pre."stats` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `page` text NOT NULL,
  `referral` text NOT NULL,
  `type` text NOT NULL,
  `ptype` text NOT NULL,
  `username` text NOT NULL,
  `ip` text NOT NULL,
  `info` text NOT NULL,
  `day` text NOT NULL,
  `tday` text NOT NULL,
  `month` text NOT NULL,
  `week` text NOT NULL,
  `year` text NOT NULL,
  `date` INT( 11 ) NOT NULL,
  PRIMARY KEY  (`id`));
  
CREATE TABLE `".$pre."stats_archive` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `name` text NOT NULL,
  `data` text NOT NULL,
  `week` int(2) NOT NULL,
  `month` int(2) NOT NULL,
  `year` int(4) NOT NULL,
  `views` int(20) NOT NULL,
  `uniques` int(20) NOT NULL,
  `date` int(11) NOT NULL,
  PRIMARY KEY  (`id`));
  
";

$handle = fopen($sitepath."upload/backup.sql", 'w');
fwrite($handle, $content);
fclose($handle);

echo "<p>Your SQL backup is now available to download, right click the following link and choose 'Save Link As' - <a href='upload/backup.sql'><u>Click Here!</u></a></p><p>The last step of backing up is to have the backup file deleted (security reasons). Once you have downloaded it, please click the following link - <a href='admin.php?view=tool_backup3'>Proceed</a></p>";
}

if ($_GET['view'] == "tool_delete_cache") {
$admin_page = "yes";

echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=tools'>Tools</a> / Delete Cache<br /><br /><br />";

if ($handle = @opendir("cache")) {
while (false !== ($file = @readdir($handle))) {
if ($file != '.' and $file != '..' and $file != 'index.html') {
$caches .= 1;
echo "<b>".$file."</b> deleted.<br />";
@unlink("cache/".$file);
}
}
@closedir($handle);
}
if ($caches == "") {
echo "No Cache Files deleted";
}
echo "<br /><br /><a href='admin.php?view=tools'>Tools Page</a>";
}

if ($_GET['view'] == "tool_delete_stats") {
$admin_page = "yes";

echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=tools'>Tools</a> / Delete Stats<br /><br /><br />";
echo "<form action='admin.php?view=tool_delete_stats2' method='post'><table cellpadding='2' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'>";

echo "<tr><td><b>Delete Stats</b></td><td><select name='month'><option value=''>-- Month --</option>";

for ($i = 1; $i <= 12; $i++) {
if ($i < 10) {
$i = "0".$i;
}
echo "<option value='".$i."'>".$i."</option>";
}
echo "</select></td><td><select name='year'><option value=''>-- Year --</option>";

for ($i = 6; $i <= 50; $i++) {
if ($i < 10) {
$i = "0".$i;
}
echo "<option value='20".$i."'>20".$i."</option>";
}
echo "</select></td></tr><tr><td><input type='submit' value='Delete'></td></tr>";

echo "</table></form>";
}

if ($_GET['view'] == "tool_delete_stats2") {
$admin_page = "yes";

$query = mysql_query("DELETE FROM ".$pre."stats WHERE month = '".$_POST['month']."' AND year = '".$_POST['year']."'");

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=tools");
echo "The stats have been deleted. <a href='admin.php?view=tools'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The stats could not be deleted. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}

if ($_GET['view'] == "tool_php") {
$admin_page = "yes";
echo "<script language='javascript'>
function transferphp() {
textbox = document.tform.coded;
textbox.value = document.tform.code.options[document.tform.code.selectedIndex].value;
}
function copyit(theField) {
var tempval=eval('document.'+theField)
tempval.focus()
tempval.select()
therange=tempval.createTextRange()
therange.execCommand('Copy')
}
</script>";

echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=tools'>Tools</a> / PHP Tags<br /><br /><br />";
echo "<h2>PHP Functions</h2><table cellpadding='5' cellspacing='0' border='0' width='490' style='padding-left:5px' align='left'>";

echo "<form name='tform'><tr><td><b>Poll</b></td><td><select name='code' size='1' onChange='transferphp()'><option value='' selected>--- Polls ---</option>";
$sql = mysql_query("SELECT * FROM ".$pre."polls WHERE type = 'poll' ORDER BY `id` DESC");
while($r = mysql_fetch_array($sql)) {
echo "<option value='&lt;?php echo show_poll(\"".$r[id]."\", \"".$r[name]."\"); ?&gt;'>".$r[name]."</option>";
}
echo "</select><br /><textarea name='coded' style='width:260px; height:50px; font-family: tahoma;font-size: 11px;color: #4a4a4a; font-weight: bold' rows='3' readonly='readonly'>".$code."</textarea></td></tr><tr><td><b>List Content</b></td><td><textarea style='width:260px; height:50px; font-family: tahoma;font-size: 11px;color: #4a4a4a; font-weight: bold' rows='3' readonly='readonly'>&lt;?php echo content(\"- {link}<br />\", \"Reviews\", \"5\", \"id\", \"DESC\"); ?&gt;</textarea></td><td width='20%'>Template, Section, # to show, Sort by and ASC or DESC</td></tr><tr><td><b>List Content<br>(by user)</b></td><td><textarea style='width:260px; height:50px; font-family: tahoma;font-size: 11px;color: #4a4a4a; font-weight: bold' rows='3' readonly='readonly'>&lt;?php echo user_content(\"- {link}<br />\", \"Reviews\", \"5\", \"id\", \"DESC\", \"username\"); ?&gt;</textarea></td></tr><tr><td><b>Search Box</b></td><td><textarea style='width:260px; height:50px; font-family: tahoma;font-size: 11px;color: #4a4a4a; font-weight: bold' rows='3' readonly='readonly'>&lt;?php echo search_box(); ?&gt;</textarea></td></tr>";

echo "<tr><td><b>List of Users Online</b></td><td><textarea style='width:260px; height:50px; font-family: tahoma;font-size: 11px;color: #4a4a4a; font-weight: bold' rows='3' readonly='readonly'>&lt;?php echo users_online(\"15\"); ?&gt;</textarea></td><td>15 = 15 minutes, lists all registered users that are online in X amount of minutes</td></tr><tr><tr><td><b># of Users Online</b></td><td><textarea style='width:260px; height:50px; font-family: tahoma;font-size: 11px;color: #4a4a4a; font-weight: bold' rows='3' readonly='readonly'>&lt;?php echo online(\"\",\"15\"); ?&gt;</textarea></td><td>15 = 15 minutes</td></tr><tr><td><b># of Guests Online</b></td><td><textarea style='width:260px; height:50px; font-family: tahoma;font-size: 11px;color: #4a4a4a; font-weight: bold' rows='3' readonly='readonly'>&lt;?php echo online(\"guests\",\"15\"); ?&gt;</textarea></td><td>15 = 15 minutes</td></tr><tr><td><b>Display Template</b></td><td><textarea style='width:260px; height:50px; font-family: tahoma;font-size: 11px;color: #4a4a4a; font-weight: bold' rows='3' readonly='readonly'>&lt;?php echo template(\"templatename\"); ?&gt;</textarea></td></tr><tr><td><b>Gallery</b></td><td><textarea style='width:260px; height:50px; font-family: tahoma;font-size: 11px;color: #4a4a4a; font-weight: bold' rows='3' readonly='readonly'>&lt;?php echo gallery(\"5\"); ?&gt;</textarea></td><td>5 = amount of images to display, latest files uploaded</td></tr>";

//echo "<tr><td><b>Point Total (by user)</b></td><td><textarea style='width:260px; height:50px; font-family: tahoma;font-size: 11px;color: #4a4a4a; font-weight: bold' rows='3' readonly='readonly'>&lt;?php echo pointcount(\"username\"); ?&gt;</textarea></td></tr>";

echo "</table></form><br clear='all' /><h2>Plugin Functions</h2><table cellpadding='5' cellspacing='0' border='0' width='490' style='padding-left:5px' align='left'>";

echo "<tr><td width='20%'><a href='admin.php?view=load_plugin&plugin=affiliates'>Affiliates</a></td><td><textarea style='width:230px; height:50px; font-family: tahoma;font-size: 11px;color: #4a4a4a; font-weight: bold' rows='3' readonly='readonly'>&lt;?php echo affiliates(\"10\"); ?&gt;</textarea></td><td>10 = amount of affiliates to display</td></tr><tr><td><a href='admin.php?view=load_plugin&plugin=shoutbox'>Shoutbox</a></td><td><textarea style='width:230px; height:50px; font-family: tahoma;font-size: 11px;color: #4a4a4a; font-weight: bold' rows='3' readonly='readonly'>&lt;?php echo shoutbox(\"10\"); ?&gt;</textarea></td><td>10 = amount of shoutbox messages to display</td></tr>";

echo "</table>";
}

$p = "";
$p = mysql_fetch_row(mysql_query("SELECT padd,pedit,pdelete FROM ".$pre."permissions WHERE level = '".$level."' AND name = 'plugins'"));
if ($_GET['view'] == "plugins") {
$admin_page = "yes";
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;Plugins / Manage Plugins - <a href='admin.php?view=install_plugins'>Install Plugin</a> - <a href='admin.php?view=view_plugins'>Buy/Download Plugins</a><br /><br /><br />";
echo "<table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td><b>ID #</b></td><td><b>Plugin Name</b></td><td><b>Version</b></td><td><b>Status</b></td><td><b>Actions</b></td></tr>";
$sql = mysql_query("SELECT * FROM ".$pre."plugins ORDER BY `name` ASC");
while($r = mysql_fetch_array($sql)) {
echo "<tr><td>".$r[id]."</td><td><a href='admin.php?view=load_plugin&plugin=".strtolower(str_replace(" ", "_", $r[name]))."'>".ucwords($r[name])."</a></td><td>v".$r[version]."</td><td><font color='";
if ($r[status] == "On") {
echo "blue'>On";
} else {
echo "red'>Off";
}
echo "</font></td><td><a href='admin.php?view=edit_plugin&id=".urlencode($r[id])."'>Edit</a> | <a href='admin.php?view=uninstall_plugin&id=".urlencode($r[id])."'>Un-Install</a></td></tr>";
}
echo "</table><br clear='all'>";
}

if ($_GET['view'] == "view_plugins") {
$admin_page = "yes";
echo "<center><iframe src='http://www.adaptcms.com/plugins.php?sitename=".urlencode($setting['sitename'])."&siteurl=".urlencode($siteurl)."&version=".urlencode($version)."&domain=".$domain."&license_number=".$license_number."' width='500' height='500' frameborder='0'></iframe></center>";
}

if ($_GET['view'] == "load_plugin") {
$admin_page = "yes";
$plugin = mysql_fetch_row(mysql_query("SELECT url,status FROM ".$pre."plugins WHERE name = '".strtolower(str_replace("_", " ", $_GET['plugin']))."'"));
if ($plugin[1] == "Off") {
echo "Sorry, but the <b>".ucwords($_GET['plugin'])."</b> Plugin is offline";
} else {
$module = $_GET['module'];
include ("plugins/".$plugin[0]);
}
}

if ($p[1]) {
if ($_GET['view'] == "edit_plugin") {
$admin_page = "yes";
$r = mysql_fetch_row(mysql_query("SELECT name,url,version,status FROM ".$pre."plugins WHERE id = '".$_GET['id']."'"));
echo "<form action='admin.php?view=edit_plugin2' method='post'><input type='hidden' name='id' value='".$_GET['id']."'><table cellpadding='5' cellspacing='0' border='0' width='490' align='left'><tr><td>Plugin Name</td><td><input type='text' name='pluginname' value='".$r[0]."' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr><tr><td>URL</td><td><input type='text' name='url' value='".$r[1]."' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr><tr><td>Version</td><td><input type='text' name='version' value='".$r[2]."' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px' value='popup'></td></tr><tr><td>Status</td><td><select name='status' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'><option value='".$r[3]."'>-- ".$r[3]." --</option><option value='On'>On</option><option value='Off'>Off</option></select></td></tr><tr><td><input type='submit' value='Update Plugin' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444;padding-left:1px'></td></tr></table></form>";
}

if ($_GET['view'] == "edit_plugin2") {
$admin_page = "yes";
$query = @mysql_query("UPDATE ".$pre."plugins SET name = '".addslashes($_POST["pluginname"])."', url = '".addslashes($_POST["url"])."', version = '".addslashes($_POST["version"])."', status = '".addslashes($_POST["status"])."' WHERE id = '".$_POST['id']."'");

if ($query == TRUE) {
echo re_direct("1500", "admin.php?view=plugins");
echo "The Plugin <b>".stripslashes($_POST['pluginname'])."</b> has been updated. <a href='admin.php?view=plugins'>Return</a>";
} else {
echo reporterror(urlencode($siteurl).$cpage, mysql_error(@mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "The Plugin could not be updated. This error has been sent to the <b>AdaptCMS</b> support team and you will be contacted soon.";
}
}
}

if ($p[0] && $p[1] && $p[2]) {
if ($_GET['view'] == "uninstall_plugin") {
$admin_page = "yes";
$r = mysql_fetch_row(mysql_query("SELECT name,url,version,status FROM ".$pre."plugins WHERE id = '".$_GET['id']."'"));
echo '<script>
var checkobj
function agreesubmit(el){
checkobj=el
if (document.all||document.getElementById){
for (i=0;i<checkobj.form.length;i++){  //hunt down submit button
var tempobj=checkobj.form.elements[i]
if(tempobj.type.toLowerCase()=="submit")
tempobj.disabled=!checkobj.checked
}
}
}
function defaultagree(el){
if (!document.all&&!document.getElementById){
if (window.checkobj&&checkobj.checked)
return true
else{
alert("Please confirm the box in order to un-install this plugin.")
return false
}
}
}
</script>';
echo "<form action='admin.php?view=uninstall_plugin2' name='agreeform' onSubmit='return defaultagree(this)' method='post'><input type='hidden' name='id' value='".$_GET['id']."'><input type='hidden' name='url' value='".$r[1]."'>Un-Installing the <a href='admin.php?view=load_plugin&plugin=".strtolower($r[0])."'>".ucwords($r[0])."</a> Plugin will remove it from the Plugin List as well as all MySQL data for it. The file will <b>NOT</b> be deleted, you may choose to do that yourself, especially if you want to re-install it. Please confirm to un-install the plugin below:<br /><br />";

echo '<input name="agreecheck" type="checkbox" style="font-family: tahoma; font-size: 11px; border: 1px solid #444444" onClick="agreesubmit(this)"><b>I want to proceed to un-install the ';
echo "<a href='admin.php?view=load_plugin&plugin=".strtolower($r[0])."'>".ucwords($r[0])."</a> ";
echo 'Plugin</b><br /><input type="Submit" value="Submit!" style="font-family: tahoma; font-size: 11px; border: 1px solid #444444" disabled></form>

<script>
document.forms.agreeform.agreecheck.checked=false
</script>';
}

if ($_GET['view'] == "uninstall_plugin2") {
$admin_page = "yes";
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;Plugins / Un-Install Plugin - <a href='admin.php?view=plugins'>Manage Plugins</a> - <a href='admin.php?view=view_plugins'>Buy/Download Plugins</a><br /><br /><br />";
$module = "uninstall_".$_POST['url'];
include ("plugins/".$_POST['url']);
}

if ($_GET['view'] == "install_plugin") {
$admin_page = "yes";
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;Plugins / Install Plugin - <a href='admin.php?view=plugins'>Manage Plugins</a> - <a href='admin.php?view=view_plugins'>Buy/Download Plugins</a><br /><br /><br />";
$module = "install_".$_GET['url'];
include ("plugins/".$_GET['url']);
}

if ($_GET['view'] == "install_plugins") {
$admin_page = "yes";
echo "<a href='admin.php'>Directory</a>&nbsp;&nbsp;-&nbsp;&nbsp;Plugins / Install Plugins - <a href='admin.php?view=plugins'>Manage Plugins</a> - <a href='admin.php?view=view_plugins'>Buy/Download Plugins</a><br /><br /><br />";
echo "<table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td><b>Plugin Name</b></td><td><b>Size</b></td></tr>";

function size($size) {
  $i=0;
  $iec = array("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB");
  while (($size/1024)>1) {
   $size=$size/1024;
   $i++;
  }
  return substr($size,0,strpos($size,'.')+4)." ".$iec[$i];
}

if ($handle = @opendir("plugins")) {
while (false !== ($file = @readdir($handle))) {
if ($file != '.' and $file != '..' and $file != 'index.html') {
if (preg_match("/functions/", $file)) {
} else {
$plg_check = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."plugins WHERE url = '".$file."'"));
if ($plg_check == 0) {
$ex = explode(".", $file);
$check = file_get_contents($siteurl."plugins/".$file."?check=status");
if ($check == 1) {
echo "<tr><td><a href='admin.php?view=install_plugin&url=".$file."'>".ucwords($ex[0])."</a></td><td>".size(filesize("plugins/".$file))."</td></tr>";
}
}
}
}
}
@closedir($handle);
}
echo "</table><br clear='all'>";
}
}

if ($_GET['view'] == "search" && $_GET['search']) {
$admin_page = "yes";

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

$tbl = $_GET['cat'];
if ($tbl == "articles") {
$fld = "name";
$cat = "article";
} elseif ($tbl == "comments") {
$fld = "comment";
$cat = "comments";
} elseif ($tbl == "fields") {
$fld = "name";
$cat = "field";
} elseif ($tbl == "files") {
$fld = "filename";
$cat = "file";
} elseif ($tbl == "gallery") {
$fld = "name";
$cat = "gallery";
} elseif ($tbl == "pages") {
$fld = "name";
$cat = "pages";
} elseif ($tbl == "settings") {
$fld = "name";
$cat = "setting";
} elseif ($tbl == "templates") {
$fld = "name";
$cat = "template";
} elseif ($tbl == "users") {
$fld = "username";
$cat = "users";
}

echo "<table cellpadding='5' cellspacing='0' border='0' width='480' style='padding-left:5px' align='left'><tr><td><b>ID #</b></td><td><b>".ucwords($tbl)." Name</b></td><td><b>Date</b></td><td><b>Actions</b></td></tr>";

$sql = mysql_query("SELECT * FROM ".$pre.$tbl." WHERE ".$fld." LIKE '%". $_GET['search'] ."%'");
for($i = 1; $r = mysql_fetch_assoc($sql); $i++) {
		$n1 = $_GET['search'];
		$n2 = hilite($_GET['search']);
		$name = str_replace($n1, $n2, $r[$fld]);

$ex = explode(" ", $_GET['search']);

while (list($k,$i) = each($ex)) {
$n1[$k] = $i;
$n2[$k] = hilite($i);
}

echo "<tr><td>".$r[id]."</td><td>".$name."</td><td>";
if ($r[date]) {
echo date($setting['date_format'], $r[date]);
} else {
echo "N/A";
}
echo "</td><td>";
if ($tbl != "files") {
echo "<a href='admin.php?view=";
if ($tbl != "settings") {
echo "edit_".$cat."&id=".$r[id];
} else {
echo $cat."&name=".$r[section];
}
echo "'>Edit</a> | ";
}
if ($tbl != "settings") {
echo "<a href='admin.php?view=delete_".$cat."&id=".$r[id]."' onclick='return confirmDelete();'>Delete</a>";
} else {
echo "<a href='admin.php?view=".$cat."&name=".$r[section]."' onclick='return confirmDelete();'>Delete</a>";
}
echo "</td></tr>";
}
echo "</table>";
}

if ($_GET['view'] == "stats" && $_GET['detailed'] == "top_pages") {
$admin_page = "yes";
if(!isset($_GET['page'])){
    $page = 1;
} else {
    $page = $_GET['page'];
}
$admin_limit = $setting["admin_limit"] * 1.5;
$from = (($page * $admin_limit) - $admin_limit);

echo "<table cellspacing='0' cellpadding='0' border='0' align='center' width='498'><tr><td><a href='admin.php?view=stats'>Stats</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=stats&detailed=archive'><u>Archive</u></a></td><td><a href='admin.php?view=stats&detailed=top_pages'>[Pages]</a></td><td><a href='admin.php?view=stats&detailed=top_referrals'>[Referrals]</a></td><td><a href='admin.php?view=stats&detailed=top_keywords'>[Keywords]</a></td><td><a href='admin.php?view=stats&detailed=top_bots'>[Bots]</a></td><td><a href='admin.php?view=stats&detailed=top_os'>[Operating Systems]</a></td><td><a href='admin.php?view=stats&detailed=top_browsers'>[Browsers]</a></td></tr></table><br />";

echo "<table cellspacing='0' cellpadding='3' border='0' align='center' width='480'><tr><td>Page</td><td>Views</td><td>%</td></tr>\n";
$tot = 0;
$sql2 = mysql_query("SELECT * FROM ".$pre."stats_archive WHERE name = 'page' AND views > 5 ORDER BY `views` DESC");
while($row = mysql_fetch_array($sql2)) {
$tot = $tot + $row[views];
}
$sql = mysql_query("SELECT * FROM ".$pre."stats_archive WHERE name = 'page' ORDER BY `views` DESC LIMIT $from, ".$admin_limit);
while($r = mysql_fetch_array($sql)) {
@$diff = $r[views] / $tot;
$diff = substr($diff, 2, 2);
if ($diff{0} == "0") {
$diff = str_replace("0", "", $diff);
}
if (strlen($diff) == "3") {
$diff = substr($diff, 0, 2);
}
if ($diff == "") {
$diff = 0;
}

echo "<tr><td><a href='".$r[data]."'>";
$r[data] = str_replace($siteurl,"/",$r[data]);
if (strlen($r[data]) > 64) {
echo substr(urldecode($r[data]), 0, 61)."...";
} else {
echo substr(urldecode($r[data]), 0, 64);
}
echo "</a></td><td>".number_format($r[views])."</td><td>".$diff."%</td></tr>
";
}
echo "</table><br clear='all' />";

$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."stats_archive WHERE name = 'page'"),0);
$total_pages = ceil($total_results / $admin_limit);
if ($total_pages > "1") {
echo "<center>";
if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"admin.php?view=stats&detailed=top_pages&page=$prev\"><<Previous</a>&nbsp;";
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i&nbsp;";
        } else {
            echo "<a href=\"admin.php?view=stats&detailed=top_pages&page=$i\">$i</a>&nbsp;";
			if (($i/25) == (int)($i/25)) {echo "<br />";}if ($i > 100 && ($i/15) == (int)($i/15)) {echo "<br />";}
    }
}


if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"admin.php?view=stats&detailed=top_pages&page=$next\">Next>></a>";
}
echo "</center>";
}
}

if ($_GET['view'] == "stats" && $_GET['detailed'] == "top_referrals") {
$admin_page = "yes";
if(!isset($_GET['page'])){
    $page = 1;
} else {
    $page = $_GET['page'];
}
$admin_limit = $setting["admin_limit"] * 1.5;
$from = (($page * $admin_limit) - $admin_limit);

echo "<table cellspacing='0' cellpadding='0' border='0' align='center' width='498'><tr><td><a href='admin.php?view=stats'>Stats</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=stats&detailed=archive'><u>Archive</u></a></td><td><a href='admin.php?view=stats&detailed=top_pages'>[Pages]</a></td><td><a href='admin.php?view=stats&detailed=top_referrals'>[Referrals]</a></td><td><a href='admin.php?view=stats&detailed=top_keywords'>[Keywords]</a></td><td><a href='admin.php?view=stats&detailed=top_bots'>[Bots]</a></td><td><a href='admin.php?view=stats&detailed=top_os'>[Operating Systems]</a></td><td><a href='admin.php?view=stats&detailed=top_browsers'>[Browsers]</a></td></tr></table><br />";

echo "<table cellspacing='0' cellpadding='3' border='0' align='center' width='480'><tr><td>Page</td><td>Views</td><td>%</td></tr>";
$tot = 0;
$sql2 = mysql_query("SELECT * FROM ".$pre."stats_archive WHERE name = 'referral' AND views > 5 ORDER BY `views` DESC");
while($row = mysql_fetch_array($sql2)) {
$tot = $tot + $row[views];
}
$sql = mysql_query("SELECT * FROM ".$pre."stats_archive WHERE name = 'referral' ORDER BY `views` DESC LIMIT $from, ".$admin_limit);
while($r = mysql_fetch_array($sql)) {
@$diff = $r[views] / $tot;
$diff = substr($diff, 2, 2);
if ($diff{0} == "0") {
$diff = str_replace("0", "", $diff);
}
if (strlen($diff) == "3") {
$diff = substr($diff, 0, 2);
}
if ($diff == "") {
$diff = 0;
}

echo "<tr><td><a href='".$r[data]."'>";
$r[data] = str_replace($siteurl,"/",$r[data]);
if (strlen($r[data]) > 64) {
echo substr(urldecode($r[data]), 0, 61)."...";
} else {
echo substr(urldecode($r[data]), 0, 64);
}
echo "</a></td><td>".number_format($r[views])."</td><td>".$diff."%</td></tr>";
}
echo "</table><br clear='all'>";
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."stats_archive WHERE name = 'referral'"),0);
$total_pages = ceil($total_results / $admin_limit);
if ($total_pages > "1") {
echo "<center>";
if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"admin.php?view=stats&detailed=top_referrals&page=$prev\"><<Previous</a>&nbsp;";
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i&nbsp;";
        } else {
            echo "<a href=\"admin.php?view=stats&detailed=top_referrals&page=$i\">$i</a>&nbsp;";
			if (($i/25) == (int)($i/25)) {echo "<br />";}if ($i > 100 && ($i/15) == (int)($i/15)) {echo "<br />";}
    }
}


if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"admin.php?view=stats&detailed=top_referrals&page=$next\">Next>></a>";
}
echo "</center>";
}
}

if ($_GET['view'] == "stats" && $_GET['detailed'] == "top_os") {
$admin_page = "yes";
if(!isset($_GET['page'])){
    $page = 1;
} else {
    $page = $_GET['page'];
}
$admin_limit = $setting["admin_limit"] * 1.5;
$from = (($page * $admin_limit) - $admin_limit);

echo "<table cellspacing='0' cellpadding='0' border='0' align='center' width='498'><tr><td><a href='admin.php?view=stats'>Stats</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=stats&detailed=archive'><u>Archive</u></a></td><td><a href='admin.php?view=stats&detailed=top_pages'>[Pages]</a></td><td><a href='admin.php?view=stats&detailed=top_referrals'>[Referrals]</a></td><td><a href='admin.php?view=stats&detailed=top_keywords'>[Keywords]</a></td><td><a href='admin.php?view=stats&detailed=top_bots'>[Bots]</a></td><td><a href='admin.php?view=stats&detailed=top_os'>[Operating Systems]</a></td><td><a href='admin.php?view=stats&detailed=top_browsers'>[Browsers]</a></td></tr></table><br />";

echo "<table cellspacing='0' cellpadding='3' border='0' align='center' width='350'><tr><td>Name</td><td>Views</td><td>Percent</td></tr>";
$tot = 0;
$sql2 = mysql_query("SELECT * FROM ".$pre."stats_archive WHERE name = 'operating_system' ORDER BY `views` DESC");
while($row = mysql_fetch_array($sql2)) {
$tot = $tot + $row[views];
}
$sql = mysql_query("SELECT * FROM ".$pre."stats_archive WHERE name = 'operating_system' ORDER BY `views` DESC LIMIT $from, ".$admin_limit);
while($r = mysql_fetch_array($sql)) {
@$diff = $r[views] / $tot;
$diff = substr($diff, 2, 2);
if ($diff{0} == "0") {
$diff = str_replace("0", "", $diff);
}
if (strlen($diff) == "3") {
$diff = substr($diff, 0, 2);
}
if ($diff == "") {
$diff = 0;
}
echo "<tr><td>".$r[data]."</td><td>".number_format($r[views])."</td><td>".$diff."%</td></tr>";
}
echo "</table><br clear='all'>";
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."stats_archive WHERE name = 'operating_system'"),0);
$total_pages = ceil($total_results / $admin_limit);
if ($total_pages > "1") {
echo "<center>";
if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"admin.php?view=stats&detailed=top_os&page=$prev\"><<Previous</a>&nbsp;";
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i&nbsp;";
        } else {
            echo "<a href=\"admin.php?view=stats&detailed=top_os&page=$i\">$i</a>&nbsp;";
			if (($i/25) == (int)($i/25)) {echo "<br />";}
    }
}


if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"admin.php?view=stats&detailed=top_os&page=$next\">Next>></a>";
}
echo "</center>";
}
}

if ($_GET['view'] == "stats" && $_GET['detailed'] == "top_browsers") {
$admin_page = "yes";
if(!isset($_GET['page'])){
    $page = 1;
} else {
    $page = $_GET['page'];
}
$admin_limit = $setting["admin_limit"] * 1.5;
$from = (($page * $admin_limit) - $admin_limit);

echo "<table cellspacing='0' cellpadding='0' border='0' align='center' width='498'><tr><td><a href='admin.php?view=stats'>Stats</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=stats&detailed=archive'><u>Archive</u></a></td><td><a href='admin.php?view=stats&detailed=top_pages'>[Pages]</a></td><td><a href='admin.php?view=stats&detailed=top_referrals'>[Referrals]</a></td><td><a href='admin.php?view=stats&detailed=top_keywords'>[Keywords]</a></td><td><a href='admin.php?view=stats&detailed=top_bots'>[Bots]</a></td><td><a href='admin.php?view=stats&detailed=top_os'>[Operating Systems]</a></td><td><a href='admin.php?view=stats&detailed=top_browsers'>[Browsers]</a></td></tr></table><br />";

echo "<table cellspacing='0' cellpadding='3' border='0' align='center' width='350'><tr><td>Name</td><td>Views</td><td>Percent</td></tr>";
$tot = 0;
$sql2 = mysql_query("SELECT * FROM ".$pre."stats_archive WHERE name = 'browser' ORDER BY `views` DESC");
while($row = mysql_fetch_array($sql2)) {
$tot = $tot + $row[views];
}
$sql = mysql_query("SELECT * FROM ".$pre."stats_archive WHERE name = 'browser' ORDER BY `views` DESC LIMIT $from, ".$admin_limit);
while($r = mysql_fetch_array($sql)) {
@$diff = $r[views] / $tot;
$diff = substr($diff, 2, 2);
if ($diff{0} == "0") {
$diff = str_replace("0", "", $diff);
}
if (strlen($diff) == "3") {
$diff = substr($diff, 0, 2);
}
if ($diff == "") {
$diff = 0;
}
echo "<tr><td>".$r[data]."</td><td>".number_format($r[views])."</td><td>".$diff."%</td></tr>";
}
echo "</table><br clear='all'>";
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."stats_archive WHERE name = 'browser'"),0);
$total_pages = ceil($total_results / $admin_limit);
if ($total_pages > "1") {
echo "<center>";
if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"admin.php?view=stats&detailed=top_browsers&page=$prev\"><<Previous</a>&nbsp;";
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i&nbsp;";
        } else {
            echo "<a href=\"admin.php?view=stats&detailed=top_browsers&page=$i\">$i</a>&nbsp;";
			if (($i/25) == (int)($i/25)) {echo "<br />";}
    }
}


if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"admin.php?view=stats&detailed=top_browsers&page=$next\">Next>></a>";
}
echo "</center>";
}
}

if ($_GET['view'] == "stats" && $_GET['detailed'] == "top_keywords") {
$admin_page = "yes";
if(!isset($_GET['page'])){
    $page = 1;
} else {
    $page = $_GET['page'];
}
$admin_limit = $setting["admin_limit"] * 1.5;
$from = (($page * $admin_limit) - $admin_limit);

echo "<table cellspacing='0' cellpadding='0' border='0' align='center' width='498'><tr><td><a href='admin.php?view=stats'>Stats</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=stats&detailed=archive'><u>Archive</u></a></td><td><a href='admin.php?view=stats&detailed=top_pages'>[Pages]</a></td><td><a href='admin.php?view=stats&detailed=top_referrals'>[Referrals]</a></td><td><a href='admin.php?view=stats&detailed=top_keywords'>[Keywords]</a></td><td><a href='admin.php?view=stats&detailed=top_bots'>[Bots]</a></td><td><a href='admin.php?view=stats&detailed=top_os'>[Operating Systems]</a></td><td><a href='admin.php?view=stats&detailed=top_browsers'>[Browsers]</a></td></tr></table><br />";

echo "<table cellspacing='0' cellpadding='3' border='0' align='center' width='350'><tr><td>Name</td><td>Views</td><td>Percent</td></tr>";
$tot = 0;
$sql2 = mysql_query("SELECT * FROM ".$pre."stats_archive WHERE name = 'keyword' ORDER BY `views` DESC");
while($row = mysql_fetch_array($sql2)) {
$tot = $tot + $row[views];
}
$sql = mysql_query("SELECT * FROM ".$pre."stats_archive WHERE name = 'keyword' ORDER BY `views` DESC LIMIT $from, ".$admin_limit);
while($r = mysql_fetch_array($sql)) {
@$diff = $r[views] / $tot;
$diff = substr($diff, 2, 2);
if ($diff{0} == "0") {
$diff = str_replace("0", "", $diff);
}
if (strlen($diff) == "3") {
$diff = substr($diff, 0, 2);
}
if ($diff == "") {
$diff = 0;
}
echo "<tr><td>".$r[data]."</td><td>".number_format($r[views])."</td><td>".$diff."%</td></tr>";
}
echo "</table><br clear='all'>";
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."stats_archive WHERE name = 'keyword'"),0);
$total_pages = ceil($total_results / $admin_limit);
if ($total_pages > "1") {
echo "<center>";
if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"admin.php?view=stats&detailed=top_keywords&page=$prev\"><<Previous</a>&nbsp;";
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i&nbsp;";
        } else {
            echo "<a href=\"admin.php?view=stats&detailed=top_keywords&page=$i\">$i</a>&nbsp;";
			if (($i/25) == (int)($i/25)) {echo "<br />";}
    }
}


if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"admin.php?view=stats&detailed=top_keywords&page=$next\">Next>></a>";
}
echo "</center>";
}
}

if ($_GET['view'] == "stats" && $_GET['detailed'] == "top_bots") {
$admin_page = "yes";

echo "<table cellspacing='0' cellpadding='0' border='0' align='center' width='498'><tr><td><a href='admin.php?view=stats'>Stats</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=stats&detailed=archive'><u>Archive</u></a></td><td><a href='admin.php?view=stats&detailed=top_pages'>[Pages]</a></td><td><a href='admin.php?view=stats&detailed=top_referrals'>[Referrals]</a></td><td><a href='admin.php?view=stats&detailed=top_keywords'>[Keywords]</a></td><td><a href='admin.php?view=stats&detailed=top_bots'>[Bots]</a></td><td><a href='admin.php?view=stats&detailed=top_os'>[Operating Systems]</a></td><td><a href='admin.php?view=stats&detailed=top_browsers'>[Browsers]</a></td></tr></table><br />";

echo "<table cellspacing='0' cellpadding='3' border='0' align='center' width='350'><tr><td>Name</td><td>Views</td><td>Percent</td></tr>";
$i = 0;
foreach($botlist as $bot) {
if ($i == 0) {
} else {
$data .= " OR";
}
$data .= " username = '".$bot."'";
$i = $i + 1;
}

$tot = 0;
$sql2 = mysql_query("SELECT * FROM ".$pre."stats WHERE".$data." ORDER BY `username` ASC");
while($row = mysql_fetch_array($sql2)) {
$tot = $tot + 1;
}

$sql = mysql_query("SELECT * FROM ".$pre."stats WHERE".$data." ORDER BY `username` ASC");
while($r = mysql_fetch_array($sql)) {
$user = $r[username];

if (preg_match("/".$user."/", $sdata) == 0) {
$sdata .= $user;

$amount = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."stats WHERE username = '".$user."'"));
@$diff = $amount / $tot;
$diff = substr($diff, 2, 2);
if ($diff{0} == "0") {
$diff = str_replace("0", "", $diff);
}
if (strlen($diff) == "3") {
$diff = substr($diff, 0, 2);
}
if ($diff == "") {
$diff = 0;
}

$abc[$user] = $amount."|".$diff;
}
}

while (list($user, $val) = each($abc)) {
$ex = explode("|", $val);
echo "<tr><td>".$user."</td><td>".$ex[0]."</td><td>".$ex[1]."%</td></tr>";
}

echo "</table><br clear='all'>";
}

if ($_GET['view'] == "stats" && $_GET['detailed'] == "archive_compare") {
$admin_page = "yes";

echo "<table cellspacing='0' cellpadding='0' border='0' align='center' width='498'><tr><td><a href='admin.php?view=stats'>Stats</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=stats&detailed=archive'><u>Archive</u></a></td><td><a href='admin.php?view=stats&detailed=top_pages'>[Pages]</a></td><td><a href='admin.php?view=stats&detailed=top_referrals'>[Referrals]</a></td><td><a href='admin.php?view=stats&detailed=top_keywords'>[Keywords]</a></td><td><a href='admin.php?view=stats&detailed=top_bots'>[Bots]</a></td><td><a href='admin.php?view=stats&detailed=top_os'>[Operating Systems]</a></td><td><a href='admin.php?view=stats&detailed=top_browsers'>[Browsers]</a></td></tr></table><br />";

echo "<table cellspacing='1' cellpadding='4' border='0' align='center' width='480'><tr><td>&nbsp;</td><td><b>Uniques</b></td><td><b>Views</b></td></tr>";

if ($_POST['type'] == "month") {
$month1 = explode("|", $_POST['month1']);
$month2 = explode("|", $_POST['month2']);
$sql = mysql_query("SELECT * FROM ".$pre."stats_archive WHERE name = 'last_month' AND year = '".$month1[1]."' AND month = '".$month1[0]."' OR name = 'last_month' AND year = '".$month2[1]."' AND month = '".$month2[0]."' ORDER BY `month` ASC");
while($r = mysql_fetch_array($sql)) {

if ($lmonth1) {
if ($r[uniques] > $lmonth1) {
$diffa = $lmonth1 / $r[uniques];
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_month1 = " (<font color='green'>+ ".$diff."%</font>)";
} else {
$diffa = $r[uniques] / $lmonth1;
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_month1 = " (<font color='red'>- ".$diff."%</font>)";
}
}
if ($lmonth2) {
if ($r[views] > $lmonth2) {
$diffa = $lmonth2 / $r[views];
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_month2 = " (<font color='green'>+ ".$diff."%</font>)";
} else {
$diffa = $r[views] / $lmonth2;
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_month2 = " (<font color='red'>- ".$diff."%</font>)";
}
}

if ($lmonth1) {
echo "<tr><td style='padding-top:15px'><b>".date("F", mktime(0, 0, 0, $r[month], 25, $r[year]))." ".$r[year]."</b></td><td style='padding-top:15px'>".number_format($r[uniques]).$last_month1."</td><td style='padding-top:15px'>".number_format($r[views]).$last_month2."</td></tr>";
} else {
echo "<tr><td><b>".date("F", mktime(0, 0, 0, $r[month], 25, $r[year]))." ".$r[year]."</b></td><td>".number_format($r[uniques]).$last_month1."</td><td>".number_format($r[views]).$last_month2."</td></tr>";
}

$sql2 = mysql_query("SELECT * FROM ".$pre."stats_archive WHERE name = 'last_week' AND year = '".$r[year]."' AND month = '".$r[month]."' ORDER BY `week` ASC");
while($row = mysql_fetch_array($sql2)) {
if ($lweek1) {
if ($row[uniques] > $lweek1) {
$diffa = $lweek1 / $row[uniques];
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_week1 = " (<font color='green'>+ ".$diff."%</font>)";
} else {
$diffa = $row[uniques] / $lweek1;
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_week1 = " (<font color='red'>- ".$diff."%</font>)";
}
}
if ($lweek2) {
if ($row[views] > $lweek2) {
$diffa = $lweek2 / $row[views];
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_week2 = " (<font color='green'>+ ".$diff."%</font>)";
} else {
$diffa = $row[views] / $lweek2;
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_week2 = " (<font color='red'>- ".$diff."%</font>)";
}
}
echo "<tr><td style='padding-left:25px'><b>Week ".$row[week]."</b></td><td>".number_format($row[uniques]).$last_week1."</td><td>".number_format($row[views]).$last_week2."</td></tr>";
$lweek1 = $row[uniques];
$lweek2 = $row[views];
}
$lmonth1 = $r[uniques];
$lmonth2 = $r[views];
}

if ($_POST['month1'] == date("m") or $_POST['month2'] == date("m")) {
$views = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."stats WHERE type = 'view' AND month = '".date("m")."' AND year = '".date("Y")."'"));
$uniques = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."stats WHERE type = 'unique' AND month = '".date("m")."' AND year = '".date("Y")."'"));
$sql = mysql_query("SELECT * FROM ".$pre."stats_archive WHERE month = '".date("m")."' AND year = '".date("Y")."' AND name = 'last_week' ORDER BY `week` ASC");
while($row = mysql_fetch_array($sql)) {
$uniques = $uniques + $row[uniques];
$views = $views + $row[views];
if ($lweek1) {
if ($row[uniques] > $lweek1) {
$diffa = $lweek1 / $row[uniques];
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_week1 = " (<font color='green'>+ ".$diff."%</font>)";
} else {
$diffa = $row[uniques] / $lweek1;
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_week1 = " (<font color='red'>- ".$diff."%</font>)";
}
}
if ($lweek2) {
if ($row[views] > $lweek2) {
$diffa = $lweek2 / $row[views];
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_week2 = " (<font color='green'>+ ".$diff."%</font>)";
} else {
$diffa = $row[views] / $lweek2;
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_week2 = " (<font color='red'>- ".$diff."%</font>)";
}
}
$week_data .= "<tr><td style='padding-left:25px'><b>Week ".$row[week]."</b></td><td>".number_format($row[uniques]).$last_week1."</td><td>".number_format($row[views]).$last_week2."</td></tr>";
$lweek1 = $row[uniques];
$lweek2 = $row[views];
}


if ($lmonth1) {
if ($uniques > $lmonth1) {
$diffa = $lmonth1 / $uniques;
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_month1 = " (<font color='green'>+ ".$diff."%</font>)";
} else {
$diffa = $uniques / $lmonth1;
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_month1 = " (<font color='red'>- ".$diff."%</font>)";
}
}
if ($lmonth2) {
if ($views > $lmonth2) {
$diffa = $lmonth2 / $views;
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_month2 = " (<font color='green'>+ ".$diff."%</font>)";
} else {
$diffa = $views / $lmonth2;
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_month2 = " (<font color='red'>- ".$diff."%</font>)";
}
}

echo "<tr><td style='padding-top:25px'><b>".date("F")." ".date("Y")."</b></td><td style='padding-top:25px'>".number_format($uniques).$last_month1."</td><td style='padding-top:25px'>".number_format($views).$last_month2."</td></tr>";
echo $week_data;
}
} elseif ($_POST['type'] == "week") {
$week1 = explode("|", $_POST['week1']);
$week2 = explode("|", $_POST['week2']);
$sql2 = mysql_query("SELECT * FROM ".$pre."stats_archive WHERE name = 'last_week' AND year = '".$week1[1]."' AND week = '".$week1[0]."' OR year = '".$week2[1]."' AND week = '".$week2[0]."' AND name = 'last_week' ORDER BY `week` ASC");
while($row = mysql_fetch_array($sql2)) {

$week .= "<option value='".$row[week]."'>Week ".$row[week]." (".date("M y", mktime(0, 0, 0, $row[month], 25, $row[year])).")</option>";
if ($lweek1) {
if ($row[uniques] > $lweek1) {
$diffa = $lweek1 / $row[uniques];
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_week1 = " (<font color='green'>+ ".$diff."%</font>)";
} else {
$diffa = $row[uniques] / $lweek1;
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_week1 = " (<font color='red'>- ".$diff."%</font>)";
}
}
if ($lweek2) {
if ($row[views] > $lweek2) {
$diffa = $lweek2 / $row[views];
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_week2 = " (<font color='green'>+ ".$diff."%</font>)";
} else {
$diffa = $row[views] / $lweek2;
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_week2 = " (<font color='red'>- ".$diff."%</font>)";
}
}
echo "<tr><td style='padding-left:25px'><b>Week ".$row[week]."</b></td><td>".number_format($row[uniques]).$last_week1."</td><td>".number_format($row[views]).$last_week2."</td></tr>";
$lweek1 = $row[uniques];
$lweek2 = $row[views];
}

}

echo "</table>";
}

if ($_GET['view'] == "stats" && $_GET['detailed'] == "archive") {
$admin_page = "yes";

echo "<table cellspacing='0' cellpadding='0' border='0' align='center' width='498'><tr><td><a href='admin.php?view=stats'>Stats</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=stats&detailed=archive'><u>Archive</u></a></td><td><a href='admin.php?view=stats&detailed=top_pages'>[Pages]</a></td><td><a href='admin.php?view=stats&detailed=top_referrals'>[Referrals]</a></td><td><a href='admin.php?view=stats&detailed=top_keywords'>[Keywords]</a></td><td><a href='admin.php?view=stats&detailed=top_bots'>[Bots]</a></td><td><a href='admin.php?view=stats&detailed=top_os'>[Operating Systems]</a></td><td><a href='admin.php?view=stats&detailed=top_browsers'>[Browsers]</a></td></tr></table><br />";

$sql = mysql_query("SELECT * FROM ".$pre."stats_archive WHERE name = 'last_year' ORDER BY `year` DESC LIMIT 2");
while($r = mysql_fetch_array($sql)) {
$year[] = $r[year];
}
$year[] = date("Y");

while (list(, $i) = each ($year)) {
echo "<center><h2>".$i." Statistics</h2></center><table cellspacing='1' cellpadding='4' border='0' align='center' width='480'><tr><td>&nbsp;</td><td><b>Uniques</b></td><td><b>Views</b></td></tr>";

$sql = mysql_query("SELECT * FROM ".$pre."stats_archive WHERE name = 'last_month' AND year = '".$i."' ORDER BY `month` ASC");
while($r = mysql_fetch_array($sql)) {
$month .= "<option value='".$r[month]."|".$r[year]."'>".date("F (Y)", mktime(0, 0, 0, $r[month], 25, $r[year]))."</option>";

if ($lmonth1) {
if ($r[uniques] > $lmonth1) {
$diffa = $lmonth1 / $r[uniques];
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_month1 = " (<font color='green'>+ ".$diff."%</font>)";
} else {
$diffa = $r[uniques] / $lmonth1;
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_month1 = " (<font color='red'>- ".$diff."%</font>)";
}
}
if ($lmonth2) {
if ($r[views] > $lmonth2) {
$diffa = $lmonth2 / $r[views];
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_month2 = " (<font color='green'>+ ".$diff."%</font>)";
} else {
$diffa = $r[views] / $lmonth2;
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_month2 = " (<font color='red'>- ".$diff."%</font>)";
}
}

if ($lmonth1) {
echo "<tr><td style='padding-top:15px'><b>".date("F", mktime(0, 0, 0, $r[month], 25, $r[year]))." ".$r[year]."</b></td><td style='padding-top:15px'>".number_format($r[uniques]).$last_month1."</td><td style='padding-top:15px'>".number_format($r[views]).$last_month2."</td></tr>";
} else {
echo "<tr><td><b>".date("F", mktime(0, 0, 0, $r[month], 25, $r[year]))." ".$r[year]."</b></td><td>".number_format($r[uniques]).$last_month1."</td><td>".number_format($r[views]).$last_month2."</td></tr>";
}

$sql2 = mysql_query("SELECT * FROM ".$pre."stats_archive WHERE name = 'last_week' AND year = '".$r[year]."' AND month = '".$r[month]."' ORDER BY `week` ASC");
while($row = mysql_fetch_array($sql2)) {

$week .= "<option value='".$row[week]."|".$row[year]."'>Week ".$row[week]." (".date("M y", mktime(0, 0, 0, $row[month], 25, $row[year])).")</option>";
if ($lweek1) {
if ($row[uniques] > $lweek1) {
$diffa = $lweek1 / $row[uniques];
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_week1 = " (<font color='green'>+ ".$diff."%</font>)";
} else {
$diffa = $row[uniques] / $lweek1;
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_week1 = " (<font color='red'>- ".$diff."%</font>)";
}
}
if ($lweek2) {
if ($row[views] > $lweek2) {
$diffa = $lweek2 / $row[views];
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_week2 = " (<font color='green'>+ ".$diff."%</font>)";
} else {
$diffa = $row[views] / $lweek2;
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_week2 = " (<font color='red'>- ".$diff."%</font>)";
}
}
echo "<tr><td style='padding-left:25px'><b>Week ".$row[week]."</b></td><td>".number_format($row[uniques]).$last_week1."</td><td>".number_format($row[views]).$last_week2."</td></tr>";
$lweek1 = $row[uniques];
$lweek2 = $row[views];
}
$lmonth1 = $r[uniques];
$lmonth2 = $r[views];
}

$month .= "<option value='".date("m")."|".date("Y")."'>".date("F (Y)")."</option>";
$views = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."stats WHERE type = 'view' AND month = '".date("m")."' AND year = '".date("Y")."'"));
$uniques = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."stats WHERE type = 'unique' AND month = '".date("m")."' AND year = '".date("Y")."'"));
$sql = mysql_query("SELECT * FROM ".$pre."stats_archive WHERE month = '".date("m")."' AND year = '".date("Y")."' AND name = 'last_week' ORDER BY `week` ASC");
while($row = mysql_fetch_array($sql)) {
$week .= "<option value='".$row[week]."|".$row[year]."'>Week ".$row[week]." (".date("M y", mktime(0, 0, 0, $row[month], 25, $row[year])).")</option>";
$uniques = $uniques + $row[uniques];
$views = $views + $row[views];
if ($lweek1) {
if ($row[uniques] > $lweek1) {
$diffa = $lweek1 / $row[uniques];
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_week1 = " (<font color='green'>+ ".$diff."%</font>)";
} else {
$diffa = $row[uniques] / $lweek1;
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_week1 = " (<font color='red'>- ".$diff."%</font>)";
}
}
if ($lweek2) {
if ($row[views] > $lweek2) {
$diffa = $lweek2 / $row[views];
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_week2 = " (<font color='green'>+ ".$diff."%</font>)";
} else {
$diffa = $row[views] / $lweek2;
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_week2 = " (<font color='red'>- ".$diff."%</font>)";
}
}
$week_data .= "<tr><td style='padding-left:25px'><b>Week ".$row[week]."</b></td><td>".number_format($row[uniques]).$last_week1."</td><td>".number_format($row[views]).$last_week2."</td></tr>";
$lweek1 = $row[uniques];
$lweek2 = $row[views];
}


if ($lmonth1) {
if ($uniques > $lmonth1) {
$diffa = $lmonth1 / $uniques;
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_month1 = " (<font color='green'>+ ".$diff."%</font>)";
} else {
$diffa = $uniques / $lmonth1;
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_month1 = " (<font color='red'>- ".$diff."%</font>)";
}
}
if ($lmonth2) {
if ($views > $lmonth2) {
$diffa = $lmonth2 / $views;
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_month2 = " (<font color='green'>+ ".$diff."%</font>)";
} else {
$diffa = $views / $lmonth2;
$diff = 100 - round(substr($diffa, 2, 2),1);
$last_month2 = " (<font color='red'>- ".$diff."%</font>)";
}
}

echo "<tr><td style='padding-top:25px'><b>".date("F")." ".date("Y")."</b></td><td style='padding-top:25px'>".number_format($uniques).$last_month1."</td><td style='padding-top:25px'>".number_format($views).$last_month2."</td></tr>";
echo $week_data;

echo "</table><br />";
}
echo "<br clear='all'><center>

<form action='admin.php?view=stats&detailed=archive_compare' method='post'><input type='hidden' name='type' value='week'>Compare Week: <select name='week1'>".$week."</select> <select name='week2'>".$week."</select> <input type='submit' value='Go'></form>

<form action='admin.php?view=stats&detailed=archive_compare' method='post'><input type='hidden' name='type' value='month'>Compare Month: <select name='month1'>".$month."</select> <select name='month2'>".$month."</select> <input type='submit' value='Go'></form></center>";
}

if ($_GET['view'] == "stats" && $_GET['detailed'] == "") {
$admin_page = "yes";
echo "<title>".$setting["sitename"]." - Statistics</title>";
$m= date("m");
$de= date("d");
$y= date("Y");
for($i=0; $i<=7; $i++){
$days .= "<option value='".date('d',mktime(0,0,0,$m,($de-$i),$y))."'>".date('d',mktime(0,0,0,$m,($de-$i),$y))."</option>";
if ($months2 == date('m',mktime(0,0,0,$m,($de-$i),$y))) {
} else {
$months .= "<option value='".date('m',mktime(0,0,0,$m,($de-$i),$y))."'>".date('F',mktime(0,0,0,$m,($de-$i),$y))."</option>";
}
if ($years2 == date('Y',mktime(0,0,0,$m,($de-$i),$y))) {
} else {
$years .= "<option value='".date('Y',mktime(0,0,0,$m,($de-$i),$y))."'>".date('Y',mktime(0,0,0,$m,($de-$i),$y))."</option>";
}
$months2 = date('m',mktime(0,0,0,$m,($de-$i),$y));
$years2 = date('Y',mktime(0,0,0,$m,($de-$i),$y));
}
echo "<table cellspacing='0' cellpadding='0' border='0' align='center' width='498'><tr><td><a href='admin.php?view=stats'>Stats</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='admin.php?view=stats&detailed=archive'><u>Archive</u></a></td><td><a href='admin.php?view=stats&detailed=top_pages'>[Pages]</a></td><td><a href='admin.php?view=stats&detailed=top_referrals'>[Referrals]</a></td><td><a href='admin.php?view=stats&detailed=top_keywords'>[Keywords]</a></td><td><a href='admin.php?view=stats&detailed=top_bots'>[Bots]</a></td><td><a href='admin.php?view=stats&detailed=top_os'>[Operating Systems]</a></td><td><a href='admin.php?view=stats&detailed=top_browsers'>[Browsers]</a></td></tr></table><br />

<form action='admin.php' method='get'><input type='hidden' name='view' value='stats'><center><b>Last 7 Days:</b>&nbsp;<select name='day'>".$days;
echo "</select>&nbsp;<select name='month'>".$months."</select>&nbsp;<select name='year'>".$years."</select>&nbsp;&nbsp;<input type='submit' value='Search'></center></form><br />";

if (date("m") == "01") {
$lmonth = "12";
$lyear = date("Y") -1;
} else {
$lmonth = date("m") - 1;
$lyear = date("Y");
}

if (date("W") == "01") {
$lweek = "52";
} else {
$lweek = date("W") - 1;
}

if (date("z") == "0") {
if (date("L") == "1") {
$lday = "366";
} else {
$lday = "365";
}
$lyear = date("Y") -1;
} else {
$lday = date("z") - 1;
$lyear = date("Y");
}

if ($lweek < "10") {
$lweek = "0".$lweek;
} else {
$lweek = $lweek;
}

if ($lmonth < "10") {
$lmonth = "0".$lmonth;
} else {
$lmonth = $lmonth;
}

if ($_GET['year']) {
$views = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."stats WHERE type = 'view' AND year = '".$_GET['year']."' AND month = '".$_GET['month']."' AND day = '".$_GET['day']."'"));
$uniques = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."stats WHERE type = 'unique' AND year = '".$_GET['year']."' AND month = '".$_GET['month']."' AND day = '".$_GET['day']."'"));
echo "<table cellspacing='0' cellpadding='3' border='0' align='center'><tr><td><b>Unique Visitors:</b> ".$uniques."<br /><b>Views:</b> ".$views."<br /><br /><br /></td></tr>";
$var[] = "";
$var2[] = "";
$var3[] = "";

$sql = mysql_query("SELECT * FROM ".$pre."stats WHERE year = '".$_GET['year']."' AND month = '".$_GET['month']."' AND day = '".$_GET['day']."' AND type = 'unique'");
while($r = mysql_fetch_array($sql)) {
$ex = explode("|", $r[info]);

$tvar1[] .= $ex[0];
$tvar2[] .= $ex[1];
if ($r[referral]) {
$tvar3[] .= $r[referral];
}
$avar .= $ex[0];
$bvar .= $ex[1];
if ($r[referral]) {
$cvar .= $r[referral];
}

if (array_search($ex[0], $var)) {
} else {
if ($ex[0] == " ") {
} else {
$var[] .= $ex[0];
}
}
if (array_search($ex[1], $var2)) {
} else {
if ($ex[1] == " ") {
} else {
$var2[] .= $ex[1];
}
}
if (array_search($r[referral], $var3)) {
} else {
if ($r[referral] == " ") {
} else {
$var3[] .= $r[referral];
}
}
}
$total1 = count($tvar1);
$total2 = count($tvar2);
$total3 = count($tvar3);
$count1 = count($var);
$count2 = count($var2);
$count3 = count($var3);
sort($var);
sort($var2);
sort($var3);
echo "<tr><td><b>Operating Systems:</b><br />";

while (list($i) = each($var)) {
if (strlen($var[$i]) < "5") {
$char = " ";
foreach(count_chars($avar, 1) as $chr => $hit) {
$tcount = $hit;
}
} else {
$tcount = substr_count($avar, $var[$i]);
}
if (($tcount == "0") && ($total1 == "0")) {
$total_1 = "0";
} else {
@$total_1 = $tcount / $total1;
}
$total_1 = substr($total_1, 2, 2);
$total_1 = round($total_1, 1);

if ($total_1{0} == "0") {
$total_1 = str_replace("0", "", $total_1);
}

if (strlen($total_1) == "3") {
$total_1 = substr($total_1, 0, 2);
}

if (strlen($var[$i]) < "5") {
echo "Unknown ($total_1%)<br />";
} else {
echo $var[$i]." ($total_1%)<br />";
}
}
echo "<br /></td></tr><tr><td><b>Browsers:</b><br />";
while (list($ia) = each($var2)) {
if (strlen($var2[$ia]) < "5") {
$char = " ";
foreach(count_chars($bvar, 1) as $chr => $hit) {
$tcount2 = $hit;
}
} else {
$tcount2 = substr_count($bvar, $var2[$ia]);
}
if (($tcount2 == "0") && ($total2 == "0")) {
$total_2 = "0";
} else {
@$total_2 = $tcount2 / $total2;
}
$total_2 = substr($total_2, 2, 2);
$total_2 = round($total_2, 1);

if ($total_2{0} == "0") {
$total_2 = str_replace("0", "", $total_2);
}

if (strlen($total_2) == "3") {
$total_2 = substr($total_2, 0, 2);
}

if (strlen($var2[$ia]) < "5") {
echo "Unknown ($total_2%)<br />";
} else {
echo $var2[$ia]." ($total_2%)<br />";
}
}
echo "</td><br /></td></tr><tr><td><b>Referrals:</b><br />";
while (list($ic) = each($var3)) {
if ($var3[$ic] == "") {
} else {
if (strlen($var3[$ic]) < "5") {
$char = " ";
foreach(count_chars($cvar, 1) as $chr => $hit) {
$tcount3 = $hit;
}
} else {
$tcount3 = substr_count($cvar, $var3[$ic]);
}
if (($tcount3 == "0") && ($total3 == "0")) {
$total_3 = "0";
} else {
$total_3 = $tcount3 / $total3;
}
$total_3 = substr($total_3, 2, 2);
$total_3 = round($total_3, 1);

if ($total_3{0} == "0") {
$total_3 = str_replace("0", "", $total_3);
}

if (strlen($total_3) == "3") {
$total_3 = substr($total_3, 0, 2);
}

if (strlen($var3[$ic]) < "5") {
echo "Unknown ($total_3%)<br />";
} else {
echo "<a href='".$var3[$ic]."'>";
if (strlen($var3[$ic]) > 70) {
echo substr($var3[$ic], 0, 67)."...";
} else {
echo substr($var3[$ic], 0, 70);
}
echo " ($total_3%/$tcount3)<br />";
}
}
}
echo "</tr></table>";
} else {
$viewsa9 = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."stats WHERE type = 'view' AND year = '".date("Y")."'"));
$uniquesa9 = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."stats WHERE type = 'unique' AND year = '".date("Y")."'"));

$viewsc9 = mysql_fetch_row(mysql_query("SELECT tday FROM ".$pre."stats WHERE type = 'view' ORDER BY `id` ASC LIMIT 1"));//total/avg this year
$abcv9 = date("z") - $viewsc9[0] + 1;
$views9 = round($viewsa9 / $abcv9);
$uniques9 = round($uniquesa9 / $abcv9);

$views1 = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."stats WHERE type = 'view' AND day = '".date("d")."' AND month = '".date("m")."' AND week = '".date("W")."' AND year = '".date("Y")."'"));
$uniques1 = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."stats WHERE type = 'unique' AND day = '".date("d")."' AND month = '".date("m")."' AND week = '".date("W")."' AND year = '".date("Y")."'"));

if ($abcv9 > 1) {
$views8 = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."stats WHERE type = 'view' AND tday = '".$lday."' AND year = '".$lyear."'"));
$uniques8 = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."stats WHERE type = 'unique' AND tday = '".$lday."' AND year = '".$lyear."'"));
}

$views2 = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."stats WHERE type = 'view' AND week = '".date("W")."' AND year = '".date("Y")."'"));
$uniques2 = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."stats WHERE type = 'unique' AND week = '".date("W")."' AND year = '".date("Y")."'"));

$lmonths = $lmonth - 1;
$lyears = $lyear - 1;

$views3 = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."stats WHERE type = 'view' AND month = '".date("m")."' AND year = '".date("Y")."'"));
$uniques3 = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."stats WHERE type = 'unique' AND month = '".date("m")."' AND year = '".date("Y")."'"));

$sql = mysql_query("SELECT * FROM ".$pre."stats_archive WHERE month = '".date("m")."' AND year = '".date("Y")."' AND name = 'last_week'");
while($r = mysql_fetch_array($sql)) {
$uniques3 = $uniques3 + $r[uniques];
$views3 = $views3 + $r[views];
}

$stats7 = mysql_fetch_row(mysql_query("SELECT uniques,views FROM ".$pre."stats_archive WHERE name = 'last_week' ORDER BY `id` DESC"));
if ($stats7[0] == "") {
$stats7[0] = 0;
$stats7[1] = 0;
}
$stats4 = mysql_fetch_row(mysql_query("SELECT uniques,views FROM ".$pre."stats_archive WHERE name = 'last_month' ORDER BY `id` DESC"));
if ($stats4[0] == "") {
$stats4[0] = 0;
$stats4[1] = 0;
}
$stats6 = mysql_fetch_row(mysql_query("SELECT uniques,views FROM ".$pre."stats_archive WHERE name = 'last_year' ORDER BY `id` DESC"));
if ($stats6[0] == "") {
$stats6[0] = 0;
$stats6[1] = 0;
}

echo "<table cellspacing='0' cellpadding='0' border='0' align='center' width='480'><tr><td>&nbsp;</td><td><b>Uniques</b></td><td><b>Views</b></td></tr>
<tr><td><b>Average:</b></td><td>".$uniques9."</td><td>".$views9."</td></tr>	
<tr><td><b>Today:</b></td><td>".$uniques1."</td><td>".$views1."</td></tr>";
if ($uniques8 > 0 && $uniques8) {
echo "<tr><td><b>Yesterday:</b></td><td>".$uniques8."</td><td>".$views8."</td></tr>";
}
echo "<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>

<tr><td><b>This Week:</b></td><td>".$uniques2."</td><td>".$views2."</td></tr>
<tr><td><b>This Month:</b></td><td>".$uniques3."</td><td>".$views3."</td></tr>

<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>

<tr><td><b>Last Week:</b></td><td>".$stats7[0]."</td><td>".$stats7[1]."</td></tr>
<tr><td><b>Last Month:</b></td><td>".$stats4[0]."</td><td>".$stats4[1]."</td></tr>";
if ($stats6[0] > 0) {
echo "<tr><td><b>Last Year:</b></td><td>".$stats6[0]."</td><td>".$stats6[1]."</td></tr></table><br /><br />";
} else {
echo "</table><br /><br />";
}
echo "<small>Statistics for today and the average are generated upon page load, refresh to update.</small>";
}
}
}


if ($admin_page == "") {
echo "Sorry but this admin page does not exist.";
}

if ((($_GET['view'] == "logout") or ($_GET['view'] == "login") or ($_GET['view'] == "login2"))) {
eval (" ?> ".stripslashes(layout("footer"))." <?php ");
} else {
eval (" ?> ".admin_footer()." <?php ");
}
?>