<?php
include ("config.php");

if ($setting["online"] == "no") {
if ($setting["offline_message"] && $lev == 3 or $lev == "" && $_GET['view'] != "login" && $_GET['view'] != "login2") {
echo $setting["offline_message"];
die;
}
}

$_GET['view'] = str_replace("/","",$_GET['view']);
if ($_GET['view'] == "logout" or $_GET['view'] == "af_apply2" or $_GET['view'] == "af_apply" or $_GET['view'] == "af_click" or $_GET['view'] == "rss" or $_GET['view'] == "edit_profile2" or $_GET['view'] == "login" or $_GET['view'] == "login2" or $_GET['view'] == "register" or $_GET['view'] == "register2" or $_GET['view'] == "gallery_zip" or $_GET['view'] == "trackback" or $_GET['view'] == "re-direct" or $_GET['view'] == "search" && $_GET['search']) {
} else {
eval (" ?> ".stripslashes(layout("header"))." <?php ");
}

if ($_GET['title']) {
$title = str_replace("_", " ", check($_GET['title']));
echo "<title>".$title."</title>";
}

if ($_GET['view'] == "redirect" && $_GET['id'] == "") {
echo re_direct("1500", strip_tags(check($_GET['url'])));
echo "Poll vote has been recorded. Taking you back to the previous page now... <a href='".strip_tags(check($_GET['url']))."'>Return</a>";
}

include("includes/begin_caching.php");
if ($_GET['view'] == "error") {
echo "<h2>Page not Found</h2>Sorry, but we cannot find the page you were looking for (<i>".$_SERVER['REQUEST_URI']."</i>&nbsp;). Try using our <a href='".$mrw['search']."'>search</a> feature. Thank you!";
echo re_direct("2000", $siteurl);
}

if ($_GET['view'] == "list") {
$ex = explode("/", $_GET['cat']);
$_GET['cat'] = str_replace("_", " ", str_replace("-"," ", stripslashes(htmlspecialchars(check($ex[0])))));
if ($_GET['field'] or $_GET['data']) {
$_GET['field'] = str_replace("_", " ", str_replace("-"," ", stripslashes(htmlspecialchars(check($_GET['field'])))));
$_GET['data'] = str_replace("_", " ", str_replace("-"," ", stripslashes(htmlspecialchars(check($_GET['data'])))));
$ex = explode(",", $_GET['data']);

if (mysql_num_rows(mysql_query("SELECT * FROM ".$pre."sections WHERE name = '".$_GET['field']."'")) == 0) {
while (list($k,$i) = each($ex)) {
if ($k == 0) {
} else {
$fddata .= " OR";
}
$fddata .= " fname = '".$_GET['field']."' AND data LIKE '%". $i ."%'";
}
} else {
while (list($k,$i) = each($ex)) {
$gid = mysql_fetch_row(mysql_query("SELECT id FROM ".$pre."articles WHERE section = '".$_GET['field']."' AND name LIKE '%" . addslashes($i) . "%' AND ver = ''"));
if ($k == 0) {
} else {
$fddata .= " OR";
}
$fddata .= " fname = '".$_GET['field']."' AND data LIKE '%". $gid[0] ."%'";
}
unset($gid);
}
}

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

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

if ($_GET['cat']) {
$catt = " AND section = '".$_GET['cat']."'";
}

if ($_GET['field'] or $_GET['data']) {
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."fielddata WHERE ".$fddata),0);
} else {
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."articles WHERE ver = ''".$catt),0);
}
$total_pages = ceil($total_results / $setting["limit"]);

if ($total_pages > "1") {
$pg_list .= "<br /><center>";
if (strtolower($setting["mod_rewrite"]) == "yes") {
if ($_GET['cat']) {
$md = $siteurl."section/".$_GET['cat']."/page/";
} else {
$md = $siteurl."archive/page/";
}
} else {
if ($_GET['cat']) {
$md = $siteurl."index.php?view=list&cat=".$_GET['cat']."&page=";
} else {
$md = $siteurl."index.php?view=list&page=";
}
}

if($page > 1){
    $prev = ($page - 1);
    $pg_list .= "<a href=\"".$md."$prev\"><<Previous</a>&nbsp;";
}

for($i = 1; $i <= $total_pages; $i++){
	if ($i < $max_lim) {
    if(($page) == $i){
        $pg_list .= "$i&nbsp;";
        } else {
            $pg_list .= "<a href=\"".$md."$i\">$i</a>&nbsp;";
			if (($i/25) == (int)($i/25)) {$pg_list .= "<br />";}
    }
	}
}

if($page < $total_pages){
    $next = ($page + 1);
    $pg_list .= "<a href=\"".$md."$next\">Next>></a>";
}
$pg_list .= "</center>";
}

if (mysql_num_rows(mysql_query("SELECT * FROM ".$pre."sections WHERE name = '".$_GET['cat']."'")) == 1 && $_GET['cat'] or $_GET['cat'] == "") {
if ($_GET['cat']) {
echo "<title>".$setting["sitename"]." - ".$_GET['cat']." Archive</title>";
} else {
echo "<title>".$setting["sitename"]." - Archive</title>";
}
if ($_GET['cat']) {
echo "<table style='padding-left:5px'><tr><td><form action='".$siteurl."index.php' method='get'><input type='hidden' name='view' value='list'><input type='hidden' name='cat' value='".$_GET['cat']."'>";
} else {
echo "<table style='padding-left:5px'><tr><td><form action='".$siteurl."index.php' method='get'><input type='hidden' name='view' value='list'>";
}
if ($_GET['page']) {
$lim = " LIMIT $from, ".$setting["limit"];
} else {
if ($_GET['limit']) {
$lim = " LIMIT ".$_GET['limit'];
} else {
$limit = $setting["limit"] * 2;
$lim = " LIMIT ".$limit;
}
}
echo "<select name='mdate'>";
if (date("m") == "01") {
echo "<option value='01' selected>January</option>";
} else {
echo "<option value='01'>January</option>";
}
if (date("m") == "02") {
echo "<option value='02' selected>February</option>";
} else {
echo "<option value='02'>February</option>";
}
if (date("m") == "03") {
echo "<option value='03' selected>>March</option>";
} else {
echo "<option value='03'>March</option>";
}
if (date("m") == "04") {
echo "<option value='04' selected>>April</option>";
} else {
echo "<option value='04'>April</option>";
}
if (date("m") == "05") {
echo "<option value='05' selected>>May</option>";
} else {
echo "<option value='05'>May</option>";
}
if (date("m") == "06") {
echo "<option value='06' selected>>June</option>";
} else {
echo "<option value='06'>June</option>";
}
if (date("m") == "07") {
echo "<option value='07' selected>>July</option>";
} else {
echo "<option value='07'>July</option>";
}
if (date("m") == "08") {
echo "<option value='08' selected>>August</option>";
} else {
echo "<option value='08'>August</option>";
}
if (date("m") == "09") {
echo "<option value='09' selected>>September</option>";
} else {
echo "<option value='09'>September</option>";
}
if (date("m") == "10") {
echo "<option value='10' selected>>October</option>";
} else {
echo "<option value='10'>October</option>";
}
if (date("m") == "11") {
echo "<option value='11' selected>>November</option>";
} else {
echo "<option value='11'>November</option>";
}
if (date("m") == "12") {
echo "<option value='12' selected>>December</option>";
} else {
echo "<option value='12'>December</option>";
}
echo "</select>&nbsp;&nbsp;&nbsp;&nbsp;<select name='ydate'>";

$ft = mysql_fetch_row(mysql_query("SELECT ydate FROM ".$pre."articles WHERE ver = '' ORDER BY `id` ASC LIMIT 1"));
$fs = mysql_fetch_row(mysql_query("SELECT ydate FROM ".$pre."articles WHERE ver = '' ORDER BY `id` DESC LIMIT 1"));
for($i = $ft[0]; $i <= $fs[0]; $i = $i+1) {
echo "<option value='".$i."'";
if ($i == date("Y")) {
echo " selected";
}
echo ">".$i."</option>";
}
echo "</select>&nbsp;&nbsp;&nbsp;&nbsp;<input type='submit' value='Show'></form></td></tr></table>";
if ($_GET['cat']) {
echo abclist($mrw_1["archive"].$_GET['cat'], $_GET['cat']);
} else {
echo abclist($mrw["archive"],"");
}
$_GET['abc'] = str_replace("/", "", $_GET['abc']);
if ($_GET['abc'] == "0-9") {
for($i = 0; $i <= 9; $i = $i+1) {
$abc .= "AND name LIKE '".$i."%' ";
}
} else {
$abc = "AND name LIKE '".mysql_real_escape_string(check($_GET['abc']))."%' ";
}
if ((is_numeric($_GET['mdate'])) && (is_numeric($_GET['ydate']))) {
$adate = "AND mdate = '".stripslashes(htmlspecialchars(check($_GET['mdate'])))."' AND ydate = '".stripslashes(htmlspecialchars(check($_GET['ydate'])))."' ";
}
$temp = mysql_fetch_row(mysql_query("SELECT template FROM ".$pre."templates WHERE name = 'content_list'"));

if ($_GET['field'] or $_GET['data']) {
$sql = mysql_query("SELECT * FROM ".$pre."fielddata WHERE".$fddata." ORDER BY `id` DESC".$lim);
} else {
if ($_GET['abc']) {
if ($_GET['cat']) {
$sql = mysql_query("SELECT * FROM ".$pre."articles WHERE section = '".$_GET['cat']."' AND ver = '' ".$abc.$adate."ORDER BY `id` DESC".$lim);
} else {
$sql = mysql_query("SELECT * FROM ".$pre."articles WHERE ver = '' ".$abc.$adate."ORDER BY `id` DESC".$lim);
}
} else {
if ($_GET['cat']) {
$sql = mysql_query("SELECT * FROM ".$pre."articles WHERE section = '".$_GET['cat']."' AND ver = ''".$adate." ORDER BY `id` DESC".$lim);
} else {
$sql = mysql_query("SELECT * FROM ".$pre."articles WHERE ver = ''".$adate." ORDER BY `id` DESC".$lim);
}
}
}
while($r = mysql_fetch_array($sql)) {
unset($data, $datas, $pab, $rab, $name1, $link1, $n, $m, $y, $x, $i, $id, $name, $relations_id, $relations_sec, $s, $fetch, $get, $dats, $fname, $lid, $lids, $b, $sqlst, $k, $data23, $check);

if ($_GET['field'] or $_GET['data']) {
$get = mysql_fetch_row(mysql_query("SELECT section,id,username,name,views,date FROM ".$pre."articles WHERE id = '".$r[aid]."'"));
$r[section] = $get[0];
$r[id] = $get[1];
$r[username] = $get[2];
$r[name] = $get[3];
$r[views] = $get[4];
$r[date] = $get[5];
}

if (!$_GET['cat'] && $r[name] or $_GET['cat'] && $fddata && $_GET['cat'] == $r[section] && $r[name] or $_GET['cat'] && !$fddata && $r[name]) {
$s = mysql_query("SELECT * FROM ".$pre."fields WHERE type = 'textarea' AND cat = '".$r[section]."' ORDER BY `id` ASC LIMIT 1");
while($x = mysql_fetch_array($s)) {
$fetch = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE fname = '".$x[name]."' AND aid = '".$r[id]."'"));

if ($fetch[0]) {
$fetch[0] = str_replace('a href=\"article-', "a href=\"".$siteurl."/article-", $fetch[0]);
$ex = explode("<p>", $fetch[0]);
if ($ex[1] == "") {
$des = stripslashes(htmlentities($fetch[0]));
} else {
if (strlen($fetch[0]) > 300) {
$ex2 = explode("<br />", $fetch[0]);
$des = stripslashes(htmlentities($ex2[0]));
} else {
$des = stripslashes(htmlentities($ex[1]));
}
}
$des = str_replace("—", "-", $des);
}

if ($des) {
$datas[] = $des;
}
}
if ($datas) {
asort($datas);
while (list($k,$i) = each($datas)) {
if ($i) {
$data = $i;
}
}
} else {
$data = "No Description";
}

$cnum2 = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."comments WHERE aid = '".$r[id]."'"));

$pab[0] = "{link}";
$pab[1] = "{date}";
$pab[2] = "{story}";
$pab[3] = "{comments}";
$pab[4] = "{cnum}";
$pab[5] = "{pcomment}";
$pab[6] = "{author}";
$pab[7] = "{section}";
$pab[8] = "{cat}";
$pab[9] = "{url}";
$pab[10] = "{title}";
$pab[11] = "{subject}";
$pab[12] = "{name}";
$pab[13] = "{username}";
$pab[14] = "{id}";
$pab[15] = "{views}";
$pab[16] = "{votes}";
$pab[17] = "{social_icons}";

$pab[18] = "{".$r[section]."_link}";
$pab[19] = "{".$r[section]."_date}";
$pab[20] = "{".$r[section]."_story}";
$pab[21] = "{".$r[section]."_comments}";
$pab[22] = "{".$r[section]."_cnum}";
$pab[23] = "{".$r[section]."_pcomment}";
$pab[24] = "{".$r[section]."_author}";
$pab[25] = "{".$r[section]."_section}";
$pab[26] = "{".$r[section]."_cat}";
$pab[27] = "{".$r[section]."_url}";
$pab[28] = "{".$r[section]."_title}";
$pab[29] = "{".$r[section]."_subject}";
$pab[30] = "{".$r[section]."_name}";
$pab[31] = "{".$r[section]."_username}";
$pab[32] = "{".$r[section]."_id}";
$pab[33] = "{".$r[section]."_views}";
$pab[34] = "{".$r[section]."_votes}";
$pab[35] = "{".$r[section]."_social_icons}";

$rab[0] .= "<a href='".$mrw_1["content"].fthrough($r[name], $r[section], $r[id])."'>".stripslashes($r[name])."</a>";
if ($lev == 1 or $lev == 2 && $r[username] == $_COOKIE[adaptcms_username] && 1 == 2) {
$rab[0] .= "&nbsp;&nbsp;<a href='".$siteurl."admin.php?view=edit_article&id=".$r[id]."&cat=".$r[section]."'><img src='".$siteurl."includes/edit.gif' border='0'></a>&nbsp;<a href='".$siteurl."admin.php?view=delete_article&id=".$r[id]."&cat=".$r[section]."'><img src='".$siteurl."includes/delete.gif' border='0'></a>";
}
$rab[1] = date($setting["date_format"], $r[date]);
$rab[2] = stripslashes(html_entity_decode(stripslashes($data)));
$rab[3] = "<a href='".$mrw_1["content"].fthrough($r[name], $r[section], $r[id])."#comments'>Comments</a>";
$rab[4] = $cnum2;
$rab[5] = "<a href='".$mrw_1["content"].fthrough($r[name], $r[section], $r[id])."#comments'>Post a Comment</a>";
$rab[6] = "<a href='".$mrw_1["user"].fthrough($r[username],"","")."'>".$r[username]."</a>";
$rab[7] = $r[section];
$rab[8] = $r[section];
$rab[9] = $mrw_1["content"].fthrough($r[name], $r[section], $r[id]);
$rab[10] = stripslashes($r[name]);
$rab[11] = stripslashes($r[name]);
$rab[12] = stripslashes($r[name]);
$rab[13] = "<a href='".$mrw_1["user"].fthrough($r[username],"","")."'>".$r[username]."</a>";
$rab[14] = $r[id];
$rab[15] = stripslashes($r[views]);
$rab[16] = $r[rate];
$rab[17] = soc_bookmark($mrw_1["content"].fthrough($r[name], $r[cat], $r[id]), $r[name], "", $data, 'Y');

$rab[18] .= "<a href='".$mrw_1["content"].fthrough($r[name], $r[section], $r[id])."'>".stripslashes($r[name])."</a>";
if ($lev == 1 or $lev == 2 && $r[username] == $_COOKIE[adaptcms_username] && 1 == 2) {
$rab[18] .= "&nbsp;&nbsp;<a href='".$siteurl."admin.php?view=edit_article&id=".$r[id]."&cat=".$r[section]."'><img src='".$siteurl."includes/edit.gif' border='0'></a>&nbsp;<a href='".$siteurl."admin.php?view=delete_article&id=".$r[id]."&cat=".$r[section]."'><img src='".$siteurl."includes/delete.gif' border='0'></a>";
}
$rab[19] = date($setting["date_format"], $r[date]);
$rab[20] = stripslashes(html_entity_decode(stripslashes($data)));
$rab[21] = "<a href='".$mrw_1["content"].fthrough($r[name], $r[section], $r[id])."#comments'>Comments</a>";
$rab[22] = $cnum2;
$rab[23] = "<a href='".$mrw_1["content"].fthrough($r[name], $r[section], $r[id])."#comments'>Post a Comment</a>";
$rab[24] = "<a href='".$mrw_1["user"].fthrough($r[username],"","")."'>".$r[username]."</a>";
$rab[25] = $r[section];
$rab[26] = $r[section];
$rab[27] = $mrw_1["content"].fthrough($r[name], $r[section], $r[id]);
$rab[28] = stripslashes($r[name]);
$rab[29] = stripslashes($r[name]);
$rab[30] = stripslashes($r[name]);
$rab[31] = "<a href='".$mrw_1["user"].fthrough($r[username],"","")."'>".$r[username]."</a>";
$rab[32] = $r[id];
$rab[33] = stripslashes($r[views]);
$rab[34] = $r[rate];
$rab[35] = soc_bookmark($mrw_1["content"].fthrough($r[name], $r[cat], $r[id]), $r[name], "", $data, 'Y');

$m = 35;
$n = 35;

// start - custom fields
$name = "";$data = "";$row = "";
$sql_cf = mysql_query("SELECT * FROM ".$pre."fields WHERE cat = '".$r[section]."' OR cat = 'user-profile'");
while ($row = mysql_fetch_array($sql_cf)) {
$name = "$row[name]";

$data = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE fname = '".$name."' AND aid = '".$r[id]."'"));
$fdata[$name] = $data[0];

if ($data[0]) {
$n = $n + 1;
$pab[$n] = "{".$name."}";
$n = $n + 1;
$pab[$n] = "{".$r[section]."_".$name."}";
$m = $m + 1;
if ($row[type] == "textarea") {
$rab[$m] = parse_text($data[0]);
$m = $m + 1;
$rab[$m] = parse_text($data[0]);
} else {
$rab[$m] = stripslashes(html_entity_decode($data[0]));
$m = $m + 1;
$rab[$m] = stripslashes(html_entity_decode($data[0]));
}
} else {
$n = $n + 1;
$pab[$n] = "{".$name."}";
$n = $n + 1;
$pab[$n] = "{".$r[section]."_".$name."}";
$m = $m + 1;
$rab[$m] = "";
$m = $m + 1;
$rab[$m] = "";
}
}
// end - custom fields

// start - go through related sections...find similar item, if so then puts section and appropiate ID number in array
$name = "";
$sec_type = mysql_fetch_row(mysql_query("SELECT type FROM ".$pre."sections WHERE name = '".$r[section]."'"));
$type = $sec_type[0];
if ($type) {
$sql_rel = mysql_query("SELECT * FROM ".$pre."relations WHERE ".$type." = '".addslashes($r[section])."'");
while ($rel = mysql_fetch_array($sql_rel)) {
if ($type == "section") {
$name = $rel[sub];
} else {
$name = $rel[section];
}
$relations_sec[] = $name;

$check = mysql_fetch_row(mysql_query("SELECT id FROM ".$pre."articles WHERE section = '".$name."' AND name LIKE '%" . addslashes($r[name]) . "%' AND ver = ''"));

$sqlst = mysql_query("SELECT * FROM ".$pre."fielddata WHERE fname = '".$name."' AND aid = '".$r[id]."' ORDER BY `id` DESC");
while ($sr = mysql_fetch_array($sqlst)) {
if (preg_match("/".$sr[data]."/", $data23) == 0) {
$relations_id[$name][] = $sr[data];
$data23 .= $sr[data].",";
}
}

if ($check[0] && preg_match("/".$check[0]."/", $data23) == 0) {
$relations_id[$name][] = $check[0];
}
}
}
// end - go through related sections

$cn = 0;
if (count($relations_id) == 1) {
$dats = "yes";
while (list($idy,$x) = @each($relations_id)) {
$cn = $cn + 1;
if ($cn == 1) {
$sql_rels = mysql_query("SELECT * FROM ".$pre."sections");
while ($q = mysql_fetch_array($sql_rels)) {
if ($r[section] == $q[name]) {
} else {
if ($idy == $q[name]) {
} else {
$relations_sec[] = $q[name];
$rel_sec[] = $sr[fname];
$lid = $x[0];
$lids = $idy;

$fname .= " aid = '".$x[0]."' AND fname = '".$q[name]."' OR";
}
}
}
//echo $fname;
}
if ($fname) {
$sqlst = mysql_query("SELECT * FROM ".$pre."fielddata WHERE ".substr($fname,0,-3));
while ($sr = mysql_fetch_array($sqlst)) {
$ids = $sr[fname];
if ($sr[fname] == $idy) {
} else {
if (preg_match("/".$sr[fname].$sr[data]."/", $lst)) {
} else {
$relations_id[$ids][] = $sr[data];
$lst .= $sr[fname].$sr[data];
}
}
}
}
}
}

$sql_rels = mysql_query("SELECT * FROM ".$pre."sections");
while ($q = mysql_fetch_array($sql_rels)) {
if ($r[section] == $q[name]) {
} else {
$cat = $q[name];
$k = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE fname = '".$q[name]."' AND aid = '".$r[id]."'"));
if ($dats == "yes") {
if ($lids == $q[name]) {
$sqlst = array("a");
} else {
$sqlst = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE fname = '".$q[name]."' AND aid = '".$lid."'"));
}
}
if ($sqlst[0] == "" && $dats == "yes" or $k[0] == "" && $dats == "") {
$b[] = $cat;
$n = $n + 1;
$pab[$n] = "{".$q[name]."_name}";
$n = $n + 1;
$pab[$n] = "{".$q[name]."_subject}";
$n = $n + 1;
$pab[$n] = "{".$q[name]."_title}";
$n = $n + 1;
$pab[$n] = "{".$q[name]."_link}";
$n = $n + 1;
$pab[$n] = "{".$q[name]."_url}";
$n = $n + 1;
$pab[$n] = "{".$q[name]."_date}";
$n = $n + 1;
$pab[$n] = "{".$q[name]."_username}";
$n = $n + 1;
$pab[$n] = "{".$q[name]."_cat}";
$n = $n + 1;
$pab[$n] = "{".$q[name]."_category}";
$n = $n + 1;
$pab[$n] = "{".$q[name]."_section}";
$n = $n + 1;
$pab[$n] = "{".$q[name]."_views}";
$n = $n + 1;
$pab[$n] = "{".$q[name]."_user_rate_form}";
$n = $n + 1;
$pab[$n] = "{".$q[name]."_user_rate_text}";
$n = $n + 1;
$pab[$n] = "{".$q[name]."_user_rate_img}";
$n = $n + 1;
$pab[$n] = "{".$q[name].";";
$n = $n + 1;
$pab[$n] = ";".$q[name]."}";

$m = $m + 1;
$rab[$m] = "";
$m = $m + 1;
$rab[$m] = "";
$m = $m + 1;
$rab[$m] = "";
$m = $m + 1;
$rab[$m] = "";
$m = $m + 1;
$rab[$m] = "";
$m = $m + 1;
$rab[$m] = "";
$m = $m + 1;
$rab[$m] = "";
$m = $m + 1;
$rab[$m] = "";
$m = $m + 1;
$rab[$m] = "";
$m = $m + 1;
$rab[$m] = "";
$m = $m + 1;
$rab[$m] = "";
$m = $m + 1;
$rab[$m] = "";
$m = $m + 1;
$rab[$m] = "";
$m = $m + 1;
$rab[$m] = "";
$m = $m + 1;
$rab[$m] = "";
$m = $m + 1;
$rab[$m] = "";
}
}
}

// start - custom fields for related section(s)
while (list($k,$i) = @each($relations_sec)) {
$name = "";$data = "";$row = "";$sql_cf = "";$cn = "0";$fname = "";
if ($dats == "yes" && $relations_id[$i][0]) {
$sql_rels = mysql_query("SELECT * FROM ".$pre."sections");
while ($q = mysql_fetch_array($sql_rels)) {
if ($r[section] == $q[name]) {
} else {
if ($i == $q[name]) {
} else {
$cn = $cn + 1;
if (!$blacks) {
$fname .= " aid = '".$relations_id[$i][0]."' AND fname = '".$q[name]."'";
} else {
$fname .= " OR aid = '".$relations_id[$i][0]."' AND fname = '".$q[name]."'";
}
$blacks = 1;
}
}
}

}

$sql_cf = mysql_query("SELECT * FROM ".$pre."fields WHERE cat = '".$i."'");
while ($row = mysql_fetch_array($sql_cf)) {
$name = "$row[name]";

$data = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE fname = '".$name."' AND aid = '".$relations_id[$i][0]."' ORDER BY `id` ASC"));
if ($dats == "yes") {
$data2 = @mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE ".substr($fname,0,-3)));
//echo substr($fname,0,-3)."|||".$data[0];
}

if ($data[0]) {
$n = $n + 1;
$pab[$n] = "{".$i."_".$name."}";
$m = $m + 1;
if ($row[type] == "textarea") {
$rab[$m] = parse_text($data[0]);
} else {
$rab[$m] = stripslashes(html_entity_decode($data[0]));
}
} else {
if (($data2[0]) && ($dats == "yes")) {
$n = $n + 1;
$pab[$n] = "{".$i."_".$name."}";
$m = $m + 1;
if ($row[type] == "textarea") {
$rab[$m] = parse_text($data2[0]);
} else {
$rab[$m] = stripslashes(html_entity_decode($data2[0]));
}
} else {
$n = $n + 1;
$pab[$n] = "{".$i."_".$name."}";
$m = $m + 1;
$rab[$m] = "";
}
}
}

//echo "|".$i."|<br>";
//print_r($relations_id[$i]);

if (count($relations_id[$i]) > 0) {
while (list($idy,$x) = @each($relations_id[$i])) {
$y = mysql_fetch_row(mysql_query("SELECT name,date,username,views,rate FROM ".$pre."articles WHERE id = '".$x."' AND ver = ''"));
$y[0] = stripslashes($y[0]);

$name1 = stripslashes($y[0]);
$link1 = "<a href='".$mrw_1["content"].fthrough($y[0], $i, $x)."'>".stripslashes($y[0])."</a>";
$id = $x;
}
//echo "!".print_r($y)."!<br>";

$rating_img = "";$rate = "";$user_rate_form = "";
if ($y[4] == "0|0") {
$rating_img .= "<img src='".$siteurl."skins/".$skin."/starno.png'><img src='".$siteurl."skins/".$skin."/starno.png'><img src='".$siteurl."skins/".$skin."/starno.png'><img src='".$siteurl."skins/".$skin."/starno.png'><img src='".$siteurl."skins/".$skin."/starno.png'>";
$rate = 0;
} else {
$rx = explode("|", $y[4]);
@$rate = $rx[1] / $rx[0];
$rate = round(substr($rate, 0, 3));
for ($kl = 1; $kl <= $rate; $kl++) {
$rating_img .= "<img src='".$siteurl."skins/".$skin."/star.png'>";
}
$val = 5 - $rate;
if ($val > "0") {
for ($kl = 1; $kl <= $val; $kl++) {
$rating_img .= "<img src='".$siteurl."skins/".$skin."/starno.png'>";
}
}
}
$user_rate_form = "<form action='".$_SERVER['REQUEST_URI']."' method='post'><b>Vote for Article:</b> <select name='rating' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option></select> <input type='submit' value='Go' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444'><input type='hidden' name='cur_rating' value='".$y[4]."'><input type='hidden' name='aid' value='".$relations_id[$i]."'></form>";
if ($id) {
$n = $n + 1;
$pab[$n] = "{".$i."_name}";
$n = $n + 1;
$pab[$n] = "{".$i."_subject}";
$n = $n + 1;
$pab[$n] = "{".$i."_title}";
$n = $n + 1;
$pab[$n] = "{".$i."_link}";
$n = $n + 1;
$pab[$n] = "{".$i."_url}";
$n = $n + 1;
$pab[$n] = "{".$i."_date}";
$n = $n + 1;
$pab[$n] = "{".$i."_username}";
$n = $n + 1;
$pab[$n] = "{".$i."_cat}";
$n = $n + 1;
$pab[$n] = "{".$i."_category}";
$n = $n + 1;
$pab[$n] = "{".$i."_section}";
$n = $n + 1;
$pab[$n] = "{".$i."_views}";
$n = $n + 1;
$pab[$n] = "{".$i."_user_rate_form}";
$n = $n + 1;
$pab[$n] = "{".$i."_user_rate_text}";
$n = $n + 1;
$pab[$n] = "{".$i."_user_rate_img}";
$n = $n + 1;
$pab[$n] = "{".$i.";";
$n = $n + 1;
$pab[$n] = ";".$i."}";

$m = $m + 1;
$rab[$m] = $name1;
$m = $m + 1;
$rab[$m] = $name1;
$m = $m + 1;
$rab[$m] = $y[0];
$m = $m + 1;
$rab[$m] = $link1;
$m = $m + 1;
$rab[$m] = $mrw_1["content"].fthrough($y[0], $i, $id);
$m = $m + 1;
$rab[$m] = date($setting["date_format"], $y[1]);
$m = $m + 1;
$rab[$m] = $y[2];
$m = $m + 1;
$rab[$m] = $i;
$m = $m + 1;
$rab[$m] = $i;
$m = $m + 1;
$rab[$m] = $i;
$m = $m + 1;
$rab[$m] = $y[3];
$m = $m + 1;
$rab[$m] = $user_rate_form;
$m = $m + 1;
$rab[$m] = $rate;
$m = $m + 1;
$rab[$m] = $rating_img;
$m = $m + 1;
$rab[$m] = "<a href='".$mrw_1["content"].fthrough($y[0], $i, $id)."'>";
$m = $m + 1;
$rab[$m] = "</a>";
}
}
}

// end - custom fields for related section(s)
//print_r($rab);
eval (" ?>" . str_replace($pab, $rab, stripslashes($temp[0])) . " <?php ");
}
}
}
echo $pg_list;
}

if ($_GET['view'] == "") {
$archive = "";
$temp = mysql_fetch_row(mysql_query("SELECT template FROM ".$pre."templates WHERE name = 'homepage_content'"));
$temp2 = mysql_fetch_row(mysql_query("SELECT template FROM ".$pre."templates WHERE name = 'homepage'"));
$_GET['cat'] = check($_GET['cat']);

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

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

$archive .= "<br /><br /><center><a href='";
if (strtolower($setting["mod_rewrite"]) == "yes") {
if ($_GET['cat']) {
$archive .= $mrw_1["archive"].$_GET['cat'];
} else {
$archive .= str_replace("-", "", $mrw_1["archive"]);
}
} else {
$archive .= $mrw_1["archive"];
if ($_GET['cat']) {
$archive .= "&cat=".check($_GET['cat']);
}
}
$archive .= "'>[ <u>Archive</u> ]</a></center>";

if ($setting['home_section']) {
$catss = explode(",", $setting['home_section']);
while (list($i, $mb) = each ($catss)) {
if ($i == 0) {
} else {
$cats .= " OR";
}
$cats .= " section = '".$mb."' AND ver = ''";
}
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."articles WHERE".$cats." AND ver = ''"),0);
} else {
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."articles WHERE ver = ''"),0);
}
$total_pages = ceil($total_results / $setting["limit"]);

if ($total_pages > "1") {
$pg_list .= "<br /><center>";

if($page > 1){
    $prev = ($page - 1);
    $pg_list .= "<a href=\"".$siteurl."?page=$prev\"><<Previous</a>&nbsp;";
}

for($i = 1; $i <= $total_pages; $i++){
	if ($i < $max_lim) {
    if(($page) == $i){
        $pg_list .= "$i&nbsp;";
        } else {
            $pg_list .= "<a href=\"".$siteurl."?page=$i\">$i</a>&nbsp;";
			if (($i/25) == (int)($i/25)) {$pg_list .= "<br />";}
    }
	}
}

if($page < $total_pages){
    $next = ($page + 1);
    $pg_list .= "<a href=\"".$siteurl."?page=$next\">Next>></a>";
}
$pg_list .= "</center>";
}

if ($_GET['page']) {
$lim = "$from, ".$setting["limit"];
} else {
$lim = $setting['limit'];
}

$pab[0] = "{sitename}";
$pab[1] = "{content}";
$pab[2] = "{archive}";

$rab[0] = $setting["sitename"];
$rab[1] = parse_text(content($temp[0], $setting['home_section'], $lim, "", ""));
$rab[2] = $archive;

eval (" ?>" . str_replace($pab, $rab, stripslashes($temp2[0])) . " <?php ");

echo $pg_list;
}

if (($_GET['view'] == "profile") &&  ($_GET['user'])) {
$user = str_replace("/","",str_replace("_", " ", str_replace("-"," ", stripslashes(htmlspecialchars(check($_GET['user'])))))); //removes bad stuff incase of hackers
$r = mysql_fetch_row(mysql_query("SELECT id,email,level,rank,date FROM ".$pre."users WHERE username = '".$user."'")); // gets basic info of user
if ($r[0] == "") {
echo re_direct("500", $siteurl);
echo "This user account does not exist... <a href='".$siteurl."'>Return</a>";
} else {
$temp = mysql_fetch_row(mysql_query("SELECT template FROM ".$pre."templates WHERE name = 'profile'")); //grabs data of profile template
$pab[] = "{username}";
$pab[] = "{userid}";
$pab[] = "{id}";
$pab[] = "{email}";
$pab[] = "{level}";
$pab[] = "{rank}";
$pab[] = "{date}";
$pab[] = "{join_date}";
$pab[] = "{edit_profile}";
$pab[] = "{content_posted}";
$pab[] = "{content}";
$pab[] = "{posted}";
$pab[] = "{sitename}";
$rab[] = $user;
$rab[] = $r[0];
$rab[] = $r[0];
$rab[] = $r[1];
$rab[] = $r[2];
$rab[] = $r[3];
$rab[] = $r[4];
$rab[] = $r[4];
if ($_COOKIE[adaptcms_username]) {
$rab[] = "<a href='".$mrw["edit_profile"]."'>Edit Profile</a>";
} else {
$rab[] = "";
}
$rab[] = user_content("- {link}<br />", "", "5", "", "", $user);
$rab[] = user_content("- {link}<br />", "", "5", "", "", $user);
$rab[] = user_content("- {link}<br />", "", "5", "", "", $user);
$rab[] = $setting["sitename"];

// start - custom fields
$name = "";$data = "";$row = "";
$sql_cf = mysql_query("SELECT * FROM ".$pre."fields WHERE cat = 'user-profile'");
while ($row = mysql_fetch_array($sql_cf)) {
$name = "$row[name]";

$data = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE fname = '".$name."' AND aid = '".$user."'  ORDER BY `id` DESC"));
$fdata[$name] = $data[0];

if ($data[0]) {
$pab[] = "{".$name."}";
if ($row[type] == "textarea") {
$rab[] = parse_text($data[0]);
} else {
$rab[] = stripslashes(html_entity_decode($data[0]));
}
} else {
$pab[] = "{".$name."}";
$rab[] = "";
}
}
// end - custom fields
eval (" ?>" . str_replace($pab, $rab, stripslashes($temp[0])) . " <?php ");
}
}

if (($_GET['view'] == "article") && (is_numeric($_GET['id']))) {
?>
<script language="JavaScript"><!--
ts = <?php echo time(); ?>;
--></script>
<?php
if ($lev == 3 or $lev == "") {
$sql = mysql_query("SELECT * FROM ".$pre."articles WHERE id = '".$_GET['id']."' AND ver = ''");
} else {
$sql = mysql_query("SELECT * FROM ".$pre."articles WHERE id = '".$_GET['id']."'");
}
$see = mysql_num_rows($sql);
if ($see == 0) {
echo re_direct("1", $siteurl);
echo "Sorry but this article either no longer exists or you have the wrong URL. Taking to to the homepage... <a href='".$siteurl."'>Go there now</a>";
} else {
while($r = mysql_fetch_array($sql)) {
echo "<!-- <rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#' 
	xmlns:dc='http://purl.org/dc/elements/1.1/'
	xmlns:trackback='http://madskills.com/public/xml/rss/module/trackback/'>
<rdf:Description rdf:about='".$pageurl."'
	dc:identifier='".$pageurl."'
	dc:title='".$r[name]."'
	trackback:ping='".$pageurl."trackback' />
</rdf:RDF> -->\r\n
<STYLE type='text/css'>
.quote blockquote {
    border-left: 2px solid #4a4a4a;
    padding-left: 4px; 
    margin: 5px;
    margin-right: 15px;
	marg-bottom: 2px;
}
</style>\r\n";
if ($r[ver] == "") {
mysql_query("UPDATE ".$pre."articles SET views=views+1 WHERE id = '".$_GET['id']."'");
}
$r[views] = $r[views] + 1;
$temp = mysql_fetch_row(mysql_query("SELECT template FROM ".$pre."templates WHERE name = '".$r[section]."'"));
$temp2 = mysql_fetch_row(mysql_query("SELECT template FROM ".$pre."templates WHERE name = 'comments'"));

$sec_type = mysql_fetch_row(mysql_query("SELECT type FROM ".$pre."sections WHERE name = '".$r[section]."'"));

$user_rate_form = "<form action='".$_SERVER['REQUEST_URI']."' method='post'><b>Vote for Article:</b> <select name='rating' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option></select> <input type='submit' value='Go' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444'><input type='hidden' name='cur_rating' value='".$r[rate]."'><input type='hidden' name='aid' value='".$_GET['id']."'></form>";

$cnum2 = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."comments WHERE aid = '".$_GET['id']."'"));
$sql2 = mysql_query("SELECT * FROM ".$pre."comments WHERE aid = '".$_GET['id']."' ORDER BY `id` ASC");
while($s = mysql_fetch_array($sql2)) {
$comment = str_replace("&lt;br /&gt;", "<br />", stripslashes($s[comment]));
$comment = str_replace("\r\n","<br>", $comment);
$comment = str_replace("\n","<br>", $comment);

$comment2 = str_replace("<br>","", $s[comment]);
$comment2 = str_replace("
", "<br>", $comment2);
$comment2 = str_replace("\r\n","<br>", $comment2);
$comment2 = str_replace("\n","<br>", $comment2);

echo "<script type='text/javascript' language='javascript'>
function quote_".$s[id]."() {
var quote = document.getElementById('comment');
quote.value = '[quote]".badwords(trim($comment2))."[/quote]';
quote.focus();
}
</script>\r\n";

$pab2[0] = "{date}";
$pab2[1] = "{comment}";
$pab2[2] = "{author}";
$pab2[3] = "{url}";
$pab2[4] = "{email}";
$pab2[5] = "{ip}";
$pab2[6] = "{quote}";

$rab2[0] = date($setting["date_format"], $s[date]);
$rab2[1] = "<div class=\'quote\'>".parse_text($comment)."</div>";
if ($s[author]) {
$rab2[2] = "<a href='".$mrw_1["user"].fthrough($s[author],"","")."'>".$s[author]."</a>";
} else {
$rab2[2] = "Guest";
}
$rab2[3] = $s[url];
$rab2[4] = $s[email];
if ($lev == 1) {
$rab2[5] = "(IP: ".$s[ip].")";
} else {
$rab2[5] = "";
}
$rab2[6] = "<a href='#comments' onClick='quote_".$s[id]."();'>Quote</a>";
$n = "6";


// start - custom fields
$name = "";$data = "";$row = "";
$sql_cf = mysql_query("SELECT * FROM ".$pre."fields WHERE cat = 'user-profile'");
while ($row = mysql_fetch_array($sql_cf)) {
$name = "$row[name]";
$n = $n + 1;
$data = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE fname = '".$name."' AND aid = '".$s[author]."'  ORDER BY `id` DESC"));
$fdata[$name] = $data[0];

if ($data[0]) {
$pab2[$n] = "{".$name."}";
if ($row[type] == "textarea") {
$rab2[$n] = parse_text($data[0]);
} else {
$rab2[$n] = stripslashes(html_entity_decode($data[0]));
}
} else {
$pab2[$n] = "{".$name."}";
$rab2[$n] = "";
}
}
// end - custom fields
$name = "";$data = "";$row = "";$n = "";

$comments .= badwords(str_replace($pab2, $rab2, stripslashes($temp2[0])));
}
if (($_COOKIE[adaptcms_username] == "") && (strtolower($setting["guestsc"]) == "no")) {
$cform = "Sorry but guests cannot post comments! Please <a href='".$mrw_1["register"]."register'>register</a> or <a href='".$mrw_1["login"]."login'>login</a>.";
} else {
if (strtolower($setting["captcha_comments"]) == "yes") {
$cform = "<a name='comments'></a><form action='".$_SERVER['REQUEST_URI']."#comments' method='post'><input name='ip' type='hidden' value='".$_SERVER['REMOTE_ADDR']."'><input type='text' name='email' value='".$email."' /> &nbsp;email<br /><br /><input name='website' type='text' value='http://' /> &nbsp;website<br /><br /><textarea name='comment' id='comment' cols='40' rows='10'></textarea><br /><br /><img id='captcha_img' src='".$siteurl."includes/captcha.php?id=".time()."' style='border:1px solid #000000' /> <a href='no_matter' onclick='document.getElementById(\"captcha_img\").src = \"".$siteurl."includes/captcha.php?id=\" + ++ts; return false'><img src='".$siteurl."includes/refresh.png' border='0'></a> <input type='text' name='captcha'><br /><br /><input type='hidden' name='aid' value='".$_GET['id']."' /><input type='submit' value='Post Comment' /></form>";
} else {
$cform = "<a name='comments'></a><form action='".$_SERVER['REQUEST_URI']."#comments' method='post'><input name='ip' type='hidden' value='".$_SERVER['REMOTE_ADDR']."'><input type='text' name='email' value='".$email."' /> &nbsp;email<br /><br /><input name='website' type='text' value='http://' /> &nbsp;website<br /><br /><textarea name='comment' id='comment' cols='40' rows='10'></textarea><br /><br /><input type='hidden' name='aid' value='".$_GET['id']."' /><input type='submit' value='Post Comment' /></form>";
}
}

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

$gallery = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE fname = 'Gallery' AND aid = '".$_GET['id']."'"));
if (is_numeric($gallery[0])) {
$gal = mysql_fetch_row(mysql_query("SELECT name FROM ".$pre."gallery WHERE id = '".$gallery[0]."'"));
$gal_num = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."files WHERE gallery = '".$gallery[0]."'"));
}

// start - normal fields
$pab[] = "{name}";
$pab[] = "{subject}";
$pab[] = "{title}";
$pab[] = "{link}";
$pab[] = "{url}";
$pab[] = "{trackback_url}";
$pab[] = "{trackback_link}";
$pab[] = "{date}";
$pab[] = "{username}";
$pab[] = "{author}";
$pab[] = "{user}";
$pab[] = "{cat}";
$pab[] = "{category}";
$pab[] = "{section}";
$pab[] = "{views}";
$pab[] = "{sitename}";
$pab[] = "{social_icons}";
$rab[] = stripslashes($r[name]);
$rab[] = stripslashes($r[name]);
$rab[] = stripslashes($r[name]);
$rab[] = "<a href='".$mrw_1["content"].fthrough($r[name], $r[cat], $r[id])."'>".stripslashes($r[name])."</a>";
$rab[] = $mrw_1["content"].fthrough($r[name], $r[cat], $r[id]);
$rab[] = $mrw_1["content"].fthrough($r[name], $r[cat], $r[id])."trackback/";
$rab[] = "<a href='".$mrw_1["content"].fthrough($r[name], $r[cat], $r[id])."trackback/' rel='trackback'>Trackback</a>";
$rab[] = date($setting["date_format"], $r[date]);
$rab[] = "<a href='".$mrw_1["user"].fthrough($r[username],"","")."'>".$r[username]."</a>";
$rab[] = "<a href='".$mrw_1["user"].fthrough($r[username],"","")."'>".$r[username]."</a>";
$rab[] = "<a href='".$mrw_1["user"].fthrough($r[username],"","")."'>".$r[username]."</a>";
$rab[] = $r[section];
$rab[] = $r[section];
$rab[] = $r[section];
$rab[] = number_format($r[views]);
$rab[] = $setting["sitename"];
$rab[] = soc_bookmark($mrw_1["content"].fthrough($r[name], $r[cat], $r[id]), $r[name], "", "", 'Y');

$pab[] = "{gallery_name}";
$pab[] = "{gallery_link}";
$pab[] = "{gallery_link;";
$pab[] = ";gallery_link}";
$pab[] = "{gallery_num}";
if (is_numeric($gallery[0])) {
for ($im = 1; $im <= $gal_num; $im++) {
$pab[] = "{gallery_image_".$im."}";
}
$rab[] = $gallery[0];
$rab[] = "<a href='".$mrw_1["gallery"].fthrough($gal[0], "", $gallery[0])."'>".stripslashes($gal[0])."</a>";
$rab[] = "<a href='".$mrw_1["gallery"].fthrough($gal[0], "", $gallery[0])."'>";
$rab[] = "</a>";
$rab[] = $gal_num;
$galquery = mysql_query("SELECT * FROM ".$pre."files WHERE gallery = '".$gallery[0]."'");
while($ga = mysql_fetch_array($galquery)) {
if (preg_match("/http/", $ga[filedir])) {
$rab[] = "<a href='".$ga[filedir].$ga[filename]."'><img src='".$ga[filedir].$ga[filename]."' width='".$setting["gallery_width"]."' height='".$setting["gallery_height"]."'></a>";
} else {
$rab[] = "<a href='".$siteurl.$ga[filedir].$ga[filename]."'><img src='".$siteurl.$ga[filedir]."thumbs/".$ga[filename]."' width='".$setting["gallery_width"]."' height='".$setting["gallery_height"]."'></a>";
}
}
} else {
for ($im = 1; $im <= 10; $im++) {
$pab[] = "{gallery_image_".$im."}";
}
$rab[] = "";
$rab[] = "";
$rab[] = "";
$rab[] = "";
$rab[] = "";
for ($sc = 1; $sc <= 10; $sc++) {
$rab[] = "";
}
}

$pab[] = "{comments}";
$pab[] = "{cnum}";
$pab[] = "{cform}";
$pab[] = "{commentsform}";
$pab[] = "{comments_form}";
$rab[] = "<a name='comments'></a>".parse_text($comments);
$rab[] = $cnum2;
$rab[] = $cform;
$rab[] = $cform;
$rab[] = $cform;

$pab[] = "{user_rate_form}";
$pab[] = "{user_rate_text}";
$pab[] = "{user_rate_img}";
$rab[] = $user_rate_form;
$rab[] = $rate;
$rab[] = $rating_img;
// end - normal fields

// start - custom fields
$name = "";$data = "";$row = "";
$sql_cf = mysql_query("SELECT * FROM ".$pre."fields WHERE cat = '".$r[section]."' OR cat = 'user-profile'");
while ($row = mysql_fetch_array($sql_cf)) {
$name = "$row[name]";

$data = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE fname = '".$name."' AND aid = '".$_GET['id']."'"));
$fdata[$name] = $data[0];

if ($data[0]) {
$pab[] = "{".$name."}";
if ($row[type] == "textarea") {
$rab[] = parse_text($data[0]);
} else {
$rab[] = stripslashes(html_entity_decode($data[0]));
}
} else {
$pab[] = "{".$name."}";
$rab[] = "";
}
}
// end - custom fields

// start - go through related sections...find similar item, if so then puts section and appropiate ID number in array
$name = "";
$type = $sec_type[0];
if ($type) {
$sql_rel = mysql_query("SELECT * FROM ".$pre."relations WHERE ".$type." = '".addslashes($r[section])."'");
while ($rel = mysql_fetch_array($sql_rel)) {
if ($type == "section") {
$name = $rel[sub];
} else {
$name = $rel[section];
}
$relations_sec[] = $name;

$check = mysql_fetch_row(mysql_query("SELECT id FROM ".$pre."articles WHERE section = '".$name."' AND name LIKE '%" . addslashes($r[name]) . "%' AND ver = ''"));

$sqlst = mysql_query("SELECT * FROM ".$pre."fielddata WHERE fname = '".$name."' AND aid = '".$r[id]."' ORDER BY `id` DESC");
while ($sr = mysql_fetch_array($sqlst)) {
if (preg_match("/".$sr[data]."/", $data23) == 0) {
$relations_id[$name][] = $sr[data];
$data23 .= $sr[data].",";
}
}

if ($check[0] && preg_match("/".$check[0]."/", $data23) == 0) {
$relations_id[$name][] = $check[0];
}
}
}
// end - go through related sections

$cn = "0";
if (count($relations_id) == 1) {
$dats = "yes";
while (list($idy,$x) = each($relations_id)) {
$cn = $cn + 1;
if ($cn == 1) {
$sql_rels = mysql_query("SELECT * FROM ".$pre."sections");
while ($q = mysql_fetch_array($sql_rels)) {
if ($r[section] == $q[name]) {
} else {
if ($idy == $q[name]) {
} else {
$relations_sec[] = $q[name];
$rel_sec[] = $sr[fname];
$lid = $x[0];
$lids = $idy;
$fname .= " aid = '".$x[0]."' AND fname = '".$q[name]."' OR";
}
}
}
//echo $fname;
}
if ($fname) {
$sqlst = mysql_query("SELECT * FROM ".$pre."fielddata WHERE ".substr($fname,0,-3));
while ($sr = mysql_fetch_array($sqlst)) {
$ids = $sr[fname];
if ($sr[fname] == $idy) {
} else {
if (preg_match("/".$sr[fname].$sr[data]."/", $lst)) {
} else {
$relations_id[$ids][] = $sr[data];
$lst .= $sr[fname].$sr[data];
}
}
}
}
}
}

$sql_rels = mysql_query("SELECT * FROM ".$pre."sections");
while ($q = mysql_fetch_array($sql_rels)) {
if ($r[section] == $q[name]) {
} else {
$cat = $q[name];
$k = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE fname = '".$q[name]."' AND aid = '".$r[id]."'"));
if ($dats == "yes") {
if ($lids == $q[name]) {
$sqlst = array("a");
} else {
$sqlst = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE fname = '".$q[name]."' AND aid = '".$lid."'"));
}
}
if (((($sqlst[0] == "") && ($dats == "yes") or ($k[0] == "") && ($dats == "")))) {
$b[] = $cat;
$pab[] = "{".$q[name]."_name}";
$pab[] = "{".$q[name]."_subject}";
$pab[] = "{".$q[name]."_title}";
$pab[] = "{".$q[name]."_link}";
$pab[] = "{".$q[name]."_url}";
$pab[] = "{".$q[name]."_date}";
$pab[] = "{".$q[name]."_username}";
$pab[] = "{".$q[name]."_cat}";
$pab[] = "{".$q[name]."_category}";
$pab[] = "{".$q[name]."_section}";
$pab[] = "{".$q[name]."_views}";
$pab[] = "{".$q[name]."_user_rate_form}";
$pab[] = "{".$q[name]."_user_rate_text}";
$pab[] = "{".$q[name]."_user_rate_img}";
$pab[] = "{".$q[name].";";
$pab[] = ";".$q[name]."}";

$rab[] = "";
$rab[] = "";
$rab[] = "";
$rab[] = "";
$rab[] = "";
$rab[] = "";
$rab[] = "";
$rab[] = "";
$rab[] = "";
$rab[] = "";
$rab[] = "";
$rab[] = "";
$rab[] = "";
$rab[] = "";
$rab[] = "";
$rab[] = "";
}
}
}

// start - custom fields for related section(s)
while (list($k,$i) = each($relations_sec)) {
$name = "";$data = "";$row = "";$sql_cf = "";$cn = "0";$fname = "";
if (($dats == "yes") && ($relations_id[$i][0])) {
$sql_rels = mysql_query("SELECT * FROM ".$pre."sections");
while ($q = mysql_fetch_array($sql_rels)) {
if ($r[section] == $q[name]) {
} else {
if ($i == $q[name]) {
} else {
$cn = $cn + 1;
$fname .= " aid = '".$relations_id[$i][0]."' AND fname = '".$q[name]."' OR";
}
}
}

}

$sql_cf = mysql_query("SELECT * FROM ".$pre."fields WHERE cat = '".$i."'");
while ($row = mysql_fetch_array($sql_cf)) {
$name = "$row[name]";

$data = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE fname = '".$name."' AND aid = '".$relations_id[$i][0]."' ORDER BY `id` ASC"));

if ($dats == "yes") {
$data2 = @mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE ".substr($fname,0,-3)));
}

if ($data[0]) {
$pab[] = "{".$i."_".$name."}";
if ($row[type] == "textarea") {
$rab[] = parse_text($data[0]);
} else {
$rab[] = stripslashes(html_entity_decode($data[0]));
}
} else {
if (($data2[0]) && ($dats == "yes")) {
$pab[] = "{".$i."_".$name."}";
if ($row[type] == "textarea") {
$rab[] = parse_text($data2[0]);
} else {
$rab[] = stripslashes(html_entity_decode($data2[0]));
}
} else {
$pab[] = "{".$i."_".$name."}";
$rab[] = "";
}
}
}

$name1 = "";$link1 = "";
if (count($relations_id[$i]) > 0) {
while (list($idy,$x) = each($relations_id[$i])) {
$y = mysql_fetch_row(mysql_query("SELECT name,date,username,views,rate FROM ".$pre."articles WHERE id = '".$x."' AND ver = ''"));
$y[0] = stripslashes($y[0]);

if (count($relations_id[$i]) > 1) {
if ($idy == 0) {
$name1 .= template("related_articlelist_start", "");
$link1 .= template("related_articlelist_start", "");
}
$name1 .= template("related_articlelist_entry", $y[0]);
$link1 .= template("related_articlelist_entry", "<a href='".$mrw_1["content"].fthrough($y[0], $i, $x)."'>".stripslashes($y[0])."</a>");

$idt = $idy + 1;
if (count($relations_id[$i]) == $idt) {
$name1 .= template("related_articlelist_end", "");
$link1 .= template("related_articlelist_end", "");
}
} else {
$name1 = stripslashes($y[0]);
$link1 = "<a href='".$mrw_1["content"].fthrough($y[0], $i, $x)."'>".stripslashes($y[0])."</a>";
}
$id = $x;
}

$rating_img = "";$rate = "";$user_rate_form = "";
if ($y[4] == "0|0") {
$rating_img .= "<img src='".$siteurl."skins/".$skin."/starno.png'><img src='".$siteurl."skins/".$skin."/starno.png'><img src='".$siteurl."skins/".$skin."/starno.png'><img src='".$siteurl."skins/".$skin."/starno.png'><img src='".$siteurl."skins/".$skin."/starno.png'>";
$rate = 0;
} else {
$rx = explode("|", $y[4]);
@$rate = $rx[1] / $rx[0];
$rate = round(substr($rate, 0, 3));
for ($kl = 1; $kl <= $rate; $kl++) {
$rating_img .= "<img src='".$siteurl."skins/".$skin."/star.png'>";
}
$val = 5 - $rate;
if ($val > "0") {
for ($kl = 1; $kl <= $val; $kl++) {
$rating_img .= "<img src='".$siteurl."skins/".$skin."/starno.png'>";
}
}
}
$user_rate_form = "<form action='".$_SERVER['REQUEST_URI']."' method='post'><b>Vote for Article:</b> <select name='rating' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option></select> <input type='submit' value='Go' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444'><input type='hidden' name='cur_rating' value='".$y[4]."'><input type='hidden' name='aid' value='".$relations_id[$i]."'></form>";
if ($id) {

$pab[] = "{".$i."_name}";
$pab[] = "{".$i."_subject}";
$pab[] = "{".$i."_title}";
$pab[] = "{".$i."_link}";
$pab[] = "{".$i."_url}";
$pab[] = "{".$i."_date}";
$pab[] = "{".$i."_username}";
$pab[] = "{".$i."_cat}";
$pab[] = "{".$i."_category}";
$pab[] = "{".$i."_section}";
$pab[] = "{".$i."_views}";
$pab[] = "{".$i."_user_rate_form}";
$pab[] = "{".$i."_user_rate_text}";
$pab[] = "{".$i."_user_rate_img}";
$pab[] = "{".$i.";";
$pab[] = ";".$i."}";

$rab[] = $name1;
$rab[] = $name1;
$rab[] = $y[0];
$rab[] = $link1;
$rab[] = $mrw_1["content"].fthrough($y[0], $i, $id);
$rab[] = date($setting["date_format"], $y[1]);
$rab[] = $y[2];
$rab[] = $i;
$rab[] = $i;
$rab[] = $i;
$rab[] = $y[3];
$rab[] = $user_rate_form;
$rab[] = $rate;
$rab[] = $rating_img;
$rab[] = "<a href='".$mrw_1["content"].fthrough($y[0], $i, $id)."'>";
$rab[] = "</a>";
}
}
}

// end - custom fields for related section(s)

eval (" ?>" . str_replace($pab, $rab, stripslashes($temp[0])) . " <?php ");
}
}
}

if (($_GET['view'] == "page") && ($_GET['id'])) {
$temp = mysql_fetch_row(mysql_query("SELECT template FROM ".$pre."templates WHERE name = 'page'"));
$_GET['id'] = str_replace("/","",stripslashes(check($_GET['id'])));

$sql = mysql_query("SELECT * FROM ".$pre."pages WHERE url = '".$_GET['id']."'");
while($r = mysql_fetch_array($sql)) {
$pab[] = "{sitename}";
$pab[] = "{name}";
$pab[] = "{title}";
$pab[] = "{subject}";
$pab[] = "{page}";
$pab[] = "{username}";
$pab[] = "{date}";

$rab[] = $setting["sitename"];
$rab[] = "<a href='".$mrw_1["page"].$r[url]."'>".stripslashes($r[name])."</a>";
$rab[] = stripslashes($r[name]);
$rab[] = stripslashes($r[name]);
$rab[] = parse_text(stripslashes($r[content]));
$rab[] = "<a href='".$mrw_1["user"].fthrough($r[username],"","")."'>".$r[username]."</a>";
$rab[] = date($setting["date_format"], $r[date]);
}
eval (" ?>" . str_replace($pab, $rab, stripslashes($temp[0])) . " <?php ");
}

if ($_GET['view'] == "memberslist") {
echo "<table cellspacing=\"0\" cellpadding=\"10\" border=\"0\" width=\"100%\"><tr><td><tr><td><b>Username</b></td><td><b>Level</b></td><td><b>Rank</b></td><td><b>Registered</b></td><td><b>Last Login</b></td><td><b>Points</b></td></tr>";

$sql = mysql_query("SELECT * FROM ".$pre."users ORDER BY `username` ASC");
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"].stripslashes($r[username])."'>".stripslashes($r[username])."</a></td><td>".$r[level]."</td><td><font color='".$color[0]."'>".$r[rank]."</font></td><td>".date($setting["date_format"], $r[date])."</td><td>".date($setting["date_format"], $r[logged])."</td><td>".pointcount($r[username])."</td></tr>";
}

echo "</table>";
}

if (($_GET['view'] == "gallery") && ($_GET['id'] == "")) {
if (is_numeric($_GET['p'])) {
  $page = $_GET['p'];
} else {
  $page = 1;
}
$albpage = 15;
$from = (($page * $albpage) - $albpage);

$temp = mysql_fetch_row(mysql_query("SELECT template FROM ".$pre."templates WHERE name = 'gallery'"));
$temp2 = "<td align='center'><a href='{url}'><img src='{thumb}' border='1'></a><br /><b>{name}</b></td>";

$sql = mysql_query("SELECT * FROM ".$pre."gallery ORDER BY `id` DESC LIMIT $from, 15");
for($i = 0; $row = mysql_fetch_assoc($sql); $i++) {
$dataa = "";
$fetchn = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."files WHERE gallery = '".$row[id]."' ORDER BY `id` DESC"));

	if ($fetchn == "0") {
    $filename = "nopreview.jpg";
	$filedir = "includes/";
	$type = "ss3";
	} else {
	$fetch = mysql_fetch_row(mysql_query("SELECT filename,filedir FROM ".$pre."files WHERE gallery = '".$row[id]."' ORDER BY `id` DESC LIMIT 1"));
	$filename = $fetch[0];
	$filedir = $fetch[1];
	if (preg_match("/http/", $filedir)) {
	$type = "ss2";
	} else {
	$type = "ss";
	}
	}

	$f1[0] = "{image}";
	$f1[1] = "{thumb}";
	$f1[2] = "{name}";
	$f1[3] = "{url}";
	$f1[4] = "{imagenum}";

	$r1[0] = $siteurl.$filedir.$filename;
	if (($type == "ss3") or ($type == "ss2")) {
	$r1[1] = $filedir.$filename."' width='".$setting["gallery_width"]."' height='".$setting["gallery_height"];
	}
	if ($type == "ss") {
	$r1[1] = $siteurl.$filedir."thumbs/".$filename."' width='".$setting["gallery_width"]."' height='".$setting["gallery_height"];
	}
    $r1[2] = $row[name];
	$r1[3] = $mrw_1["gallery"].fthrough($row[name], "", $row[id]);
	$r1[4] = $fetchn;

	if ($i > 0) {
	if (($i % 3) === 0) {
	$dataa = "</tr><tr>";
	}
	}
	$imgs .= $dataa.str_replace($f1, $r1, stripslashes($temp2));
}

$pab[0] = "{images}";
$pab[1] = "{sitename}";
$rab[0] = $imgs;
$rab[1] = $setting[sitename];
eval (" ?>" . str_replace($pab, $rab, stripslashes($temp[0])) . " <?php ");

$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."gallery"),0);

$total_pages = ceil($total_results / $albpage);

if ($total_pages > "1") {
echo "<br /><br /><center>Page:<br />";

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

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

// Build Next Link
if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"".$siteurl."index.php?view=gallery&p=$next\">Next>></a>";
}
echo "</center>";
}
}

if (($_GET['view'] == "gallery") && (is_numeric($_GET['id']))) {
if (is_numeric($_GET['p'])) {
  $page = $_GET['p'];
} else {
  $page = 1;
}
$albpage = 15;
$from = (($page * $albpage) - $albpage);

$temp = mysql_fetch_row(mysql_query("SELECT template FROM ".$pre."templates WHERE name = 'gallery_view'"));
$temp2 = mysql_fetch_row(mysql_query("SELECT template FROM ".$pre."templates WHERE name = 'gallery_image'"));

$imagenum = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."files WHERE gallery = '".$_GET['id']."'"));
$title = mysql_fetch_row(mysql_query("SELECT name FROM ".$pre."gallery WHERE id = '".$_GET['id']."'"));

echo "<script type='text/javascript' src='".$siteurl."includes/lightbox/js/prototype.js'></script>
<script type='text/javascript' src='".$siteurl."includes/lightbox/js/scriptaculous.js?load=effects,builder'></script>
<script type='text/javascript' src='".$siteurl."includes/lightbox/js/lightbox.js'></script>
<link rel='stylesheet' href='".$siteurl."includes/lightbox/css/lightbox.css' type='text/css' media='screen' />";

if (($_POST['sortby'] == "") or ($_POST['sortby'] == "id")) {
$sortby = "id";
} else {
if ($_POST['sortby'] == "filename") {
$sortby = "filename";
}
if ($_POST['sortby'] == "gallery") {
$sortby = "gallery";
}
if ($_POST['sortby'] == "date") {
$sortby = "date";
}
}

$sqls = mysql_query("SELECT * FROM ".$pre."files WHERE gallery = '".$_GET['id']."' ORDER BY `".$sortby."` DESC");
while($r = mysql_fetch_array($sqls)) {
$idm = $r[id];
$files[$idm] = $r[filename];
$dir[$idm] = $r[filedir];
}

$sql = mysql_query("SELECT * FROM ".$pre."files WHERE gallery = '".$_GET['id']."' ORDER BY `".$sortby."` DESC LIMIT $from, $albpage");
for($i = 0; $row = mysql_fetch_assoc($sql); $i++) {
$dataa = "";

	$f1[0] = "{image}";
	$f1[1] = "{thumb}";
	$f1[2] = "{date}";
	$f1[3] = "{name}";

	if (preg_match("/http/", $row[filedir])) {
	$r1[0] = $row[filedir].$row[filename];
	} else {
	$r1[0] = $siteurl.$row[filedir].$row[filename];
	}
	if (preg_match("/http/", $row[filedir])) {
    $r1[1] = $row[filedir].$row[filename]."' width='".$setting["gallery_width"]."' height='".$setting["gallery_height"];
	} else {
	$r1[1] = $siteurl.$row[filedir]."thumbs/".$row[filename]."' width='".$setting["gallery_width"]."' height='".$setting["gallery_height"];
	}
	$r1[2] = date($setting['date_format'], $row[date]);
	$r1[3] = $title[0];
    
	if (!$i == "0") {
	if (($i % 3) === 0) {
		$dataa = "</tr><tr>";
	}
	}

	$imgs .= $dataa.str_replace($f1, $r1, stripslashes($temp2[0]));
}

$alb21[0] = "{name}";
$alb21[1] = "{title}";
$alb21[2] = "{date}";
$alb21[3] = "{image}";
$alb21[4] = "{imagenum}";
$alb21[5] = "{id}";
$alb21[6] = "{sortby}";
$alb21[7] = "{download_zip}";
$alb21[8] = "{sitename}";
$alb22[0] = $title[0];
$alb22[1] = $title[0];
$alb22[2] = date($setting['date_format'], $date[0]);
$alb22[3] = $imgs;
$alb22[4] = $imagenum;
$alb22[5] = $_GET['id'];
$alb22[6] = "<form action='".$_SERVER['REQUEST_URI']."' method='post'><select name='sortby' onchange='this.form.submit()'><option value='' selected></option><option value='id'>id</option><option value='filename'>filename</option><option value='gallery'>gallery</option><option value='date'>date</option></select></form>";
$alb22[7] = "<img src='".$siteurl."includes/zip.jpg'> <a href='".$siteurl."index.php?view=gallery_zip&id=".$_GET['id']."'>Download</a>";
$alb22[8] = $setting[sitename];

eval (" ?>" . str_replace($alb21, $alb22, stripslashes($temp[0])) . " <?php ");

$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."files WHERE gallery = '".$_GET['id']."'"),0);

$total_pages = ceil($total_results / $albpage);

if ($total_pages > "1") {
echo "<br /><br /><center>Page:<br />";

// Build Previous Link
if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"".$siteurl."index.php?view=gallery&id=".$_GET['id']."&p=$prev\"><<Previous</a>&nbsp;";
}

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

// Build Next Link
if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"".$siteurl."index.php?view=gallery&id=".$_GET['id']."&p=$next\">Next>></a>";
}
echo "</center>";
}
}
include("includes/end_caching.php");

if (($_GET['view'] == "gallery_zip") && (is_numeric($_GET['id']))) {
$title = mysql_fetch_row(mysql_query("SELECT name FROM ".$pre."gallery WHERE id = '".$_GET['id']."'"));

$sqls = mysql_query("SELECT * FROM ".$pre."files WHERE gallery = '".$_GET['id']."' ORDER BY `id`");
while($r = mysql_fetch_array($sqls)) {
$idm = $r[id];
$files[$idm] = $r[filename];
$dir[$idm] = $r[filedir];
}

if ($files == "") {
echo "There are no applicable files that can be zipped from the selected Gallery. <a href='".$_SERVER['HTTP_REFERER']."'>Return</a>";
} else {
include ("includes/zip/ziplib.php");

$zipfile = new Ziplib;
while (list($y, $i) = each ($files)) {
if (file_get_contents($sitepath.$dir[$y].$i)) {
$zipfile->zl_add_file(file_get_contents($sitepath.$dir[$y].$i),$sitepath.$dir[$y].$i,"g9");
}
}
//You can stream the ZIP file or write it in a file on your server
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=\"".str_replace(" ", "_", htmlentities($title[0])).".zip\"");
echo $zipfile->zl_pack(str_replace(" ", "_", htmlentities($title[0])).".zip provided to you by ".$setting["sitename"]);
}
}

if ($_GET['view'] == "submit_article") {
if (($_COOKIE[adaptcms_username] == "") or ($_COOKIE[adaptcms_password] == "")) {
echo "Sorry but you must be logged in to submit an article. Please <a href='".$mrw_1["register"]."register'>register</a> or <a href='".$mrw_1["login"]."login'>login</a>.";
} else {
echo '<script language="javascript">function jump(targ,selObj,restore){
eval(targ+".location=\'"+selObj.options[selObj.selectedIndex].value+"\'");
if (restore) selObj.selectedIndex=0;
}</script>';
echo "<title>".$setting["sitename"]." - Submit Article</title><form name='form2'><center><p><select name='addsection' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444' 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='".$siteurl."index.php?view=submit_article2&cat=".$r[name]."'>".ucwords($r[name])."</option>";
}
}
echo "</select></form></p></center>";
}
}

if ($_GET['view'] == "submit_article2") {
if (($_COOKIE[adaptcms_username] == "") or ($_COOKIE[adaptcms_password] == "")) {
echo "Sorry but you must be logged in to submit an article. Please <a href='".$mrw_1["register"]."register'>register</a> or <a href='".$mrw_1["login"]."login'>login</a>.";
} else {
$_GET['cat'] = htmlentities(check($_GET['cat']));
if (mysql_num_rows(mysql_query("SELECT * FROM ".$pre."sections WHERE name = '".$_GET['cat']."'")) == 0) {
echo "You have manually tried to submit an article for an unkown section, possibly a hacking attempt. Stopping script.";
} else {
$p = "";
$p = mysql_fetch_row(mysql_query("SELECT padd FROM ".$pre."permissions WHERE level = '".$level."' AND name = '".$_GET['cat']."'"));
if ($p[0]) {
echo "<title>".$setting["sitename"]." - Submit Article</title><b>Directory</b>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='".$mrw_1["section"].$_GET['cat']."'>Articles</a> / Add Article<br /><br /><br />";
echo "<form action='".$siteurl."index.php?view=submit_article3&cat=".$_GET['cat']."' method='post'><table cellpadding='5' cellspacing='0' border='0' width='100%' 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'></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 `id` DESC");
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>";
}

$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(str_replace("_", " ", $i[name]))."</td><td><input type='text' name='cf_".$i[name]."' size='16' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444'></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(str_replace("_", " ", $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' 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(str_replace("_", " ", $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'>";
}
echo "</td><td>".stripslashes($i[des])."</td></tr>";
}
if ($i[type] == "checkbox") {
echo "<input type='hidden' name='id[]' value='".$i[name]."'><tr><td>".ucwords(str_replace("_", " ", $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'>";
}
echo "</td><td>".stripslashes($i[des])."</td></tr>";
}
if ($i[type] == "select") {
echo "<input type='hidden' name='id[]' value='".$i[name]."'><tr><td>".ucwords(str_replace("_", " ", $i[name]))."</td><td><select name='cf_".$i[name]."' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444'><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>";
}
}
echo "<tr><td><input type='submit' value='Add Article' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444' name='type'></td><td>&nbsp;</td></tr></table></form>";
}
}

}
}

if ($_GET['view'] == "submit_article3") {
if (($_COOKIE[adaptcms_username] == "") or ($_COOKIE[adaptcms_password] == "")) {
echo "Sorry but you must be logged in to submit an article. Please <a href='".$mrw_1["register"]."register'>register</a> or <a href='".$mrw_1["login"]."login'>login</a>.";
} else {
$_GET['cat'] = htmlentities(check($_GET['cat']));
if (mysql_num_rows(mysql_query("SELECT * FROM ".$pre."sections WHERE name = '".$_GET['cat']."'")) == 0) {
echo "You have manually tried to submit an article for an unkown section, possibly a hacking attempt. Stopping script.";
} else {
$p = "";
$p = mysql_fetch_row(mysql_query("SELECT padd FROM ".$pre."permissions WHERE level = '".$level."' AND name = '".$_GET['cat']."'"));
if ($p[0]) {

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

$id = mysql_fetch_row(mysql_query("SELECT id FROM ".$pre."articles WHERE name = '".mysql_real_escape_string(htmlentities(addslashes($_POST["articlename"])))."' ORDER BY `date` DESC LIMIT 1"));
mysql_query("INSERT INTO ".$pre."fielddata VALUES (null, 'name', '".mysql_real_escape_string(htmlentities(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", mysql_real_escape_string(addslashes($_POST["cf_$i"])));
} else {
$text = mysql_real_escape_string(addslashes($_POST["cf_$i"]));
}
if ($_POST["cf_$i"] && $text) {
mysql_query("INSERT INTO ".$pre."fielddata VALUES (null, '".$i."', '".badwords(check($text))."', '".$id[0]."')");
}
}
}

while (list(, $y) = each ($_POST['rels'])) {
while (list(, $x) = each ($_POST["rel_$y"])) {
if ($x) {
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($_COOKIE[adaptcms_username]),
mysql_real_escape_string($_COOKIE[adaptcms_password]));
mysql_query($upd);
}
}
}

if ($query == TRUE) {
echo re_direct("1500", $siteurl);
echo "<title>".$setting["sitename"]." - Submit Article</title>

The article <b>".mysql_real_escape_string(stripslashes($_POST['articlename']))."</b> has been added. It will be reviwed shortly and if it is accepted, will be accessible <a href='".$mrw_1["content"].fthrough(mysql_real_escape_string(stripslashes($_POST['articlename'])), $_GET['cat'], $id[0])."'>here</a>. <a href='".$siteurl."'>Homepage</a>";
} else {
echo reporterror($siteurl.$cpage, mysql_error(mysql_connect($dbhost, $dbuser, $dbpass)), $domain);
echo "<title>".$setting["sitename"]." - Submit Article</title>

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'] == "edit_profile2") {
if (($_COOKIE[adaptcms_username] == "") or ($_COOKIE[adaptcms_password] == "")) {
eval (" ?> ".stripslashes(layout("header"))." <?php ");
echo "Sorry but you must be logged in to edit your profile. Please <a href='".$mrw_1["register"]."register'>register</a> or <a href='".$mrw_1["login"]."login'>login</a>.";
} else {
session_start();
$_POST['npassword'] = check($_POST['npassword']);
$_POST['cpassword'] = check($_POST['cpassword']);
$_POST['email'] = check($_POST['email']);

if (($_POST['npassword']) && ($_POST['npassword'] == $_POST['cpassword'])) {
$pw = "";
$pw = ", password = '".mysql_real_escape_string(md5(stripslashes($_POST["cpassword"])))."'";
$_COOKIE[adaptcms_password] = mysql_real_escape_string(md5(stripslashes($_POST["cpassword"])));
}

$query = mysql_query("UPDATE ".$pre."users SET email = '".mysql_real_escape_string(stripslashes($_POST["email"]))."'".$pw." WHERE username = '".mysql_real_escape_string($_COOKIE[adaptcms_username])."'");

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."', '".check($text)."', '".mysql_real_escape_string($_COOKIE[adaptcms_username])."')");
} else {
mysql_query("UPDATE ".$pre."fielddata SET data = '".check($text)."' WHERE fname = '".$i."' AND aid = '".mysql_real_escape_string($_COOKIE[adaptcms_username])."'");
}
}
}
}

eval (" ?> ".stripslashes(layout("header"))." <?php ");

if ($query == TRUE) {
echo re_direct("1500", $mrw_1["user"].mysql_real_escape_string($_COOKIE[adaptcms_username]));
echo "Your profile has been updated. <a href='".$mrw_1["user"].mysql_real_escape_string($_COOKIE[adaptcms_username])."'>View Profile</a>";
}
if ($pw) {
echo "<br /><br /><i>Your password has been changed!</i>";
}
}
}

if ($_GET['view'] == "edit_profile") {
if (($_COOKIE[adaptcms_username] == "") or ($_COOKIE[adaptcms_password] == "")) {
echo "Sorry but you must be logged in to edit your profile. Please <a href='".$mrw_1["register"]."register'>register</a> or <a href='".$mrw_1["login"]."login'>login</a>.";
} else {
echo "<form action='".$siteurl."index.php?view=edit_profile2' method='post'><table cellpadding='5' cellspacing='1' border='0' align='left'>";

echo "<tr><td><b>Username:</b></td><td>".mysql_real_escape_string(stripslashes($_COOKIE[adaptcms_username]))."</td></tr><tr><td>* <b>New Password:</b></td><td><input type='password' name='npassword' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444'></td></tr><tr><td>* <b>Confirm New Password:</b></td><td><input type='password' name='cpassword' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444'></td></tr><tr><td><b>E-Mail:</b></td><td><input type='text' value='".$email."' name='email' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444'></td></tr><tr><td>&nbsp;</td><td>&nbsp;</td></tr>";

// start - custom fields
$name = "";$data = "";$row = "";
$sql_cf = mysql_query("SELECT * FROM ".$pre."fields WHERE cat = 'user-profile'");
while ($r = mysql_fetch_array($sql_cf)) {

$data = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE fname = '".$r[name]."' AND aid = '".mysql_real_escape_string(stripslashes($_COOKIE[adaptcms_username]))."' ORDER BY `id` DESC"));
$fdata = stripslashes(html_entity_decode($data[0]));

echo "<input type='hidden' name='id[]' value='".$r[name]."'><input type='hidden' name='cfdata_".$r[name]."' value='".htmlentities(stripslashes($fdata))."'>";

$s = str_replace("_", " ", $r[name]);
$s = str_replace("-", " ", $s);
$s = str_replace("user", "", $s);

echo "<tr><td><b>".ucwords($s).":</b></td><td>";

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

echo "</td></tr>";
}
// end - custom fields

echo "<tr><td><input type='submit' value='Update Profile' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444'></td></tr></table><br clear='all'/><br /><small>*= Only enter in these two password fields if you want to change your password.</small></form>";
}
}

if ($_GET['view'] == "trackback" && is_numeric($_GET['id'])) {
include("includes/trackback.php");
echo receive_trackback();
}

if ($_GET['view'] == "rss") {
header("Content-type: text/xml");

if ($_GET['section']) {
$section = str_replace("/","",str_replace("-"," ", mysql_real_escape_string(check(str_replace("_"," ", $_GET['section'])))));
}
if ($_GET['field'] or $_GET['data']) {
$_GET['field'] = str_replace("_", " ", str_replace("-"," ", stripslashes(htmlspecialchars(check($_GET['field'])))));
$_GET['data'] = str_replace("_", " ", str_replace("-"," ", stripslashes(htmlspecialchars(check($_GET['data'])))));
$ex = explode(",", $_GET['data']);

if (mysql_num_rows(mysql_query("SELECT * FROM ".$pre."sections WHERE name = '".$_GET['field']."'")) == 0) {
while (list($k,$i) = each($ex)) {
if ($k == 0) {
} else {
$fdata .= " OR";
}
$fdata .= " fname = '".$_GET['field']."' AND data LIKE '%". $i ."%'";
}
} else {
while (list($k,$i) = each($ex)) {
$gid = mysql_fetch_row(mysql_query("SELECT id FROM ".$pre."articles WHERE section = '".$_GET['field']."' AND name LIKE '%" . addslashes($i) . "%' AND ver = ''"));
if ($k == 0) {
} else {
$fdata .= " OR";
}
$fdata .= " fname = '".$_GET['field']."' AND data LIKE '%". $gid[0] ."%'";
}
unset($gid);
}
}

echo "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>
<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">
<channel>
<atom:link href=\"".$siteurl."rss\" rel=\"self\" type=\"application/rss+xml\" />";

echo "<title>".$setting["sitename"]."</title>
<link>".substr_replace($siteurl,"",-1)."</link>
<description>".$setting["sitename"]." - ".$siteurl."</description>
<webMaster>webmaster".$domain." (Webmaster)</webMaster>
";

if ($_GET['field'] or $_GET['data']) {
$sql = mysql_query("SELECT * FROM ".$pre."fielddata WHERE".$fdata." ORDER BY `id` DESC LIMIT ".$setting["limit"]);
} else {
if ($section == "") {
$sql = mysql_query("SELECT * FROM ".$pre."articles WHERE ver = '' ORDER BY `date` DESC LIMIT ".$setting["limit"]);
} else {
$sql = mysql_query("SELECT * FROM ".$pre."articles WHERE section = '".$section."' AND ver = '' ORDER BY `date` DESC LIMIT ".$setting["limit"]);
}
}
while($r = mysql_fetch_array($sql)) {

if ($_GET['field'] or $_GET['data']) {
$get = mysql_fetch_row(mysql_query("SELECT section,id,username,name,views,date FROM ".$pre."articles WHERE id = '".$r[aid]."'"));
$r[section] = $get[0];
$r[id] = $get[1];
$r[username] = $get[2];
$r[name] = $get[3];
$r[views] = $get[4];
$r[date] = $get[5];
}

$s = "";
$fetch = "";
$s = mysql_fetch_row(mysql_query("SELECT name FROM ".$pre."fields WHERE type = 'textarea' AND cat = '".$r[section]."' ORDER BY `id` ASC LIMIT 1"));
$fetch = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE fname = '".$s[0]."' AND aid = '".$r[id]."'"));

if ($fetch[0]) {
$fetch[0] = str_replace('a href=\"article-', "a href=\"".$siteurl."/article-", $fetch[0]);
$ex = explode("<p>", $fetch[0]);
if ($ex[1] == "") {
$des = stripslashes(htmlentities($fetch[0]));
} else {
if (strlen($fetch[0]) > 300) {
$ex2 = explode("<br />", $fetch[0]);
$des = stripslashes(htmlentities($ex2[0]));
} else {
$des = stripslashes(htmlentities($ex[1]));
}
}
$des = str_replace("—", "-", $des);
}

echo "
<item>
<title>".stripslashes($r[name])."</title>
<guid isPermaLink='true'>".$mrw_1["content"].fthrough($r[name], $r[section], $r[id])."</guid>
<link>".$mrw_1["content"].fthrough(rtrim($r[name]), $r[section], $r[id])."</link>
<description>".$des."&lt;/p&gt;</description>
<pubDate>".date("D, d M Y h:i:s T", $r[date])."</pubDate>
</item>
";
}
echo "
</channel></rss>";
}

if ($_GET['view'] == "login") {
if ($useridn) {
eval (" ?> ".stripslashes(layout("header"))." <?php ");
echo "Sorry ".$_COOKIE[adaptcms_username].", but you are already logged in. <a href='".$siteurl."'>Home Page</a>";
echo re_direct("1500", $siteurl);
} else {
session_start();
?>
<script language="JavaScript"><!--
ts = <?php echo time(); ?>;
--></script>
<?php
eval (" ?> ".stripslashes(layout("header"))." <?php ");
echo "<form action='index.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' size='16'></td></tr><tr><td>Password</td><td><input type='password' name='password' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444' size='16'></td></tr>";
if (strtolower($setting["captcha_login"]) == "yes") {
echo "<tr><td><img id='captcha_img' src='".$siteurl."includes/captcha.php?id=".time()."' style='border:1px solid #000000' /> <a href='no_matter' onclick='document.getElementById(\"captcha_img\").src = \"".$siteurl."includes/captcha.php?id=\" + ++ts; return false'><img src='".$siteurl."includes/refresh.png' border='0'></a></td><td><input type='text' name='captcha' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444' size='5'></td></tr>";
}
echo "<tr><td><input type='submit' value='Login' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444'></td></tr></table></form>";
}
}

$plugin_sql2 = mysql_query("SELECT * FROM ".$pre."plugins ORDER BY `name` ASC");
while($rplg = mysql_fetch_array($plugin_sql2)) {
if ($rplg[status] == "Off") {
if ($_GET['view'] == strtolower($rplg[name])) {
echo "Sorry, but the <b>".ucwords($rplg[name])."</b> Plugin is offline";
}
} else {
$module = $_GET['view'];
include_once ("plugins/".$rplg[url]);
$module = "";
}
}

if ($_GET['view'] == "register") {
if ($useridn) {
eval (" ?> ".stripslashes(layout("header"))." <?php ");
echo "Sorry ".$_COOKIE[adaptcms_username].", but you are already registered. <a href='".$siteurl."'>Home Page</a>";
echo re_direct("1500", $siteurl);
} else {
session_start();
?>
<script language="JavaScript"><!--
ts = <?php echo time(); ?>;
--></script>
<script src='<?php echo $siteurl; ?>includes/jquery.js' type='text/javascript' language='javascript'></script>
<script language="javascript">
//<!---------------------------------+
//  Developed by Roshan Bhattarai 
//  Visit http://roshanbh.com.np for this script and more.
//  This notice MUST stay intact for legal use
// --------------------------------->
$(document).ready(function()
{
	$("#username").blur(function()
	{
		//remove all the class add the messagebox classes and start fading
		$("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
		//check the username exists or not from ajax
		$.post("<?php echo $siteurl; ?>includes/check_user.php",{ user_name:$(this).val() } ,function(data)
        {
		  if(data=='no') //if username not avaiable
		  {
		  	$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('Sorry, this username is in use').addClass('messageboxerror').fadeTo(900,1);
			});		
          }
		  else
		  {
		  	$("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('Username available to register').addClass('messageboxok').fadeTo(900,1);	
			});
		  }
				
        });
 
	});
});
</script>
<style type="text/css">
.messagebox{
	position:absolute;
	width:100px;
	margin-left:30px;
	border:1px solid #c93;
	background:#ffc;
	padding:3px;
}
.messageboxok{
	position:absolute;
	width:auto;
	margin-left:30px;
	border:1px solid #349534;
	background:#C9FFCA;
	padding:3px;
	font-weight:bold;
	color:#008000;
	
}
.messageboxerror{
	position:absolute;
	width:auto;
	margin-left:30px;
	border:1px solid #CC0000;
	background:#F7CBCA;
	padding:3px;
	font-weight:bold;
	color:#CC0000;
}
</style>
<?php
eval (" ?> ".stripslashes(layout("header"))." <?php ");
echo "<title>".$setting['sitename']." - Register</title><form action='index.php?view=register2' method='post'><table><tr><td>Username</td><td><input type='text' name='username' id='username' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444' size='16'></td><td><span id='msgbox' style='display:none'></span></td></tr><tr><td>Password</td><td><input type='password' name='password' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444' size='16'></td></tr><tr><td>E-Mail</b></td><td><input type='text' name='email' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444' size='16'></td></tr><tr><td><img id='captcha_img' src='".$siteurl."includes/captcha.php?id=".time()."' style='border:1px solid #000000' /> <a href='no_matter' onclick='document.getElementById(\"captcha_img\").src = \"".$siteurl."includes/captcha.php?id=\" + ++ts; return false'><img src='".$siteurl."includes/refresh.png' border='0'></a></td><td><input type='text' name='captcha' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444' size='5'></td></tr><tr><td><input type='submit' value='Sign-Up' style='font-family: tahoma; font-size: 11px; border: 1px solid #444444'></td></tr></table></form>";
}
}

if (($_GET['view'] == "logout") or ($_GET['view'] == "login2")) {
if (($_GET['view'] == "logout") && ($_COOKIE[adaptcms_username])) {
$_SESSION[adaptcms_username] = "";
$_SESSION[adaptcms_password] = "";
setcookie("adaptcms_username", "", time()-60*60*24*30);
setcookie("adaptcms_password", "", time()-60*60*24*30);

eval (" ?> ".stripslashes(layout("header"))." <?php ");
echo re_direct("1500", "index.php");
echo "You are now logged out. <a href='index.php'>Home Page</a>";
}
}

if ($_GET['view'] == "login2") {
if ($useridn) {
eval (" ?> ".stripslashes(layout("header"))." <?php ");
echo "Sorry ".$_COOKIE[adaptcms_username].", but you are already logged in. <a href='".$siteurl."'>Home Page</a>";
echo re_direct("1500", $siteurl);
} else {
if (md5(strtoupper($_POST['captcha'])) != $_SESSION['captcha'] && strtolower($setting["captcha_login"]) == "yes") {
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(check($_POST['username']))),
mysql_real_escape_string(strip_tags(md5(check($_POST['password'])))));

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

if ($login_check == "1") {
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", "index.php");
echo "Welcome back ".strip_tags($_POST[username])."! Enjoy your stay. <a href='index.php'>Return</a>";
} else {
eval (" ?> ".stripslashes(layout("header"))." <?php ");
echo "Sorry but that username and password combo is incorrect. Please try again.";
}
}
}
}

if ($_GET['view'] == "register2") {
if ($useridn) {
eval (" ?> ".stripslashes(layout("header"))." <?php ");
echo "Sorry <b>".$_COOKIE[adaptcms_username]."</b>, but you are already registered. <a href='".$siteurl."'>Home Page</a>";
echo re_direct("1500", $siteurl);
} else {
$_POST['username'] = mysql_real_escape_string(check($_POST['username']));
$_POST['password']= md5(mysql_real_escape_string(check($_POST['password'])));

if ($_POST['username'] && $_POST['password'] && valid_email($_POST['email']) == TRUE) {
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 {

if (mysql_num_rows(mysql_query("SELECT * FROM ".$pre."users WHERE username = '".$_POST['username']."'")) > 0 or mysql_num_rows(mysql_query("SELECT * FROM ".$pre."users WHERE email = '".$_POST['email']."'")) > 0) {
eval (" ?> ".stripslashes(layout("header"))." <?php ");
echo "Sorry but that username and/or email is already in use! Please go back and try again.";
} else {
$fetch = mysql_fetch_row(mysql_query("SELECT name FROM ".$pre."levels WHERE value = '3' AND options = 'yes' OR value = '3' ORDER BY `id` DESC"));
$fetch2 = mysql_fetch_row(mysql_query("SELECT name FROM ".$pre."ranks WHERE prequired = '0' ORDER BY `id` DESC"));

$sql = mysql_query("INSERT INTO ".$pre."users VALUES (null, '".$_POST['username']."', '".$_POST['password']."', '".mysql_real_escape_string(check($_POST['email']))."', '".$fetch[0]."', '".$fetch2[0]."', '".time()."', '".time()."')");

if ($sql == TRUE) {
setcookie("adaptcms_username", $_POST['username'], time()+60*60*24*30);
setcookie("adaptcms_password", $_POST['password'], time()+60*60*24*30);

eval (" ?> ".stripslashes(layout("header"))." <?php ");
echo re_direct("1500", "index.php");

echo "Your now registered and logged in! <a href='index.php'>Return to where you were</a>";
}
}
}
} else {
if ($_POST['username'] == "") {
$err .= "<br /><b>Username</b> field empty";
}
if ($_POST['password'] == "") {
$err .= "<br /><b>Password</b> field empty";
}
if (valid_email($_POST['email']) == FALSE) {
$err .= "<br /><b>E-Mail</b> is not a valid address";
}
eval (" ?> ".stripslashes(layout("header"))." <?php ");
echo "Sorry, but the following errors occured while trying to sign you up:<br />".$err;
}
}
}

if ($_GET['view'] == "polls") {
if (is_numeric($_GET['p'])) {
    $page = $_GET['p'];
} else {
    $page = 1;
}
$from = (($page * $setting[limit]) - $setting[limit]);

echo "<title>Polls - ".$setting["sitename"]."</title>

<table cellpadding='5' cellspacing='5' border='0' width='100%' align='center'>";
$temp2 = mysql_fetch_row(mysql_query("SELECT template FROM ".$pre."templates WHERE name = 'poll_options'"));

$sqls2 = "";$row = "";
$sqls2 = mysql_query("SELECT * FROM ".$pre."polls WHERE type = 'poll' ORDER BY `id` DESC LIMIT $from, $setting[limit]");
while($row = mysql_fetch_array($sqls2)) {
echo "<p>";
$pid = "";$pname="";$total="";$num="";
$pid = $row[id];
$pname = $row[pname];
$total = $row[votes];
$num = $total;

echo "<tr><td>";

$sql = "";$r = "";$pab = "";$rab = "";$nu = "";$nua = "";$poll_options = "";
$sql = mysql_query("SELECT * FROM ".$pre."polls WHERE pname = '".$pname."' AND type = 'option' OR pname = '".$pname."' AND type = 'custom_option' ORDER BY `id` ASC");
while($r = mysql_fetch_array($sql)) {
$pab = "";$rab = "";

if ($num == "0") {
$num = "1";
}
$nu = $r[votes]/$num;

if (($num == $r[votes]) && ($r[votes] > "0")) {
$nua = "100";
} else {
if ($r[votes] == "0") {
$nua = "0";
} else {
$nua = substr($nu, 2, 2);
}
}

if (($nu < "10") && ($nu > "0")) {
$nua = $nu * 100;
}

if (($num == $r[votes]) && ($r[votes] == "0")) {
$nua = "0";
}

if ($setting["poll_type"] == "graphic") {
if ($nua > "0") {
$img = "<img src='".$siteurl."includes/poll.jpg' width='".substr($nua,0,4)."%' height='10'>";
} else {
$img = "<img src='".$siteurl."includes/poll.jpg' width='1%' height='10'>";
}
} else {
$img = substr($nua,0,4)."%";
}

$pab[] = "{name}";
$pab[] = "{data}";
$rab[] = stripslashes($r[name]);
$rab[] = substr($nua,0,4)."%<br>".$img;

$poll_options .= str_replace($pab, $rab, stripslashes($temp2[0]));
}$sab = "";$tab = "";

$temp1 = mysql_fetch_row(mysql_query("SELECT template FROM ".$pre."templates WHERE name = 'poll_results'"));
$sab[] = "{question}";
$sab[] = "{options}";
$sab[] = "{submit}";
$sab[] = "{vote_total}";

$tab[] = stripslashes($pname);
$tab[] = $poll_options;
$tab[] = "";
$tab[] = $total;

echo str_replace($sab, $tab, stripslashes($temp1[0]))."</td></tr>";
}

$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."polls WHERE type = 'poll'"),0);

$total_pages = ceil($total_results / $setting[limit]);

if ($total_pages > "1") {
echo "</table><br clear='all' /><br /><center>Page:<br />";

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

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

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

if (($_GET['view'] == "search") && ($_GET['search'] == "")) {
echo "<title>".$setting["sitename"]." - Search</title><form action='".$siteurl."index.php' method='get'><input type='hidden' name='view' value='search'><table cellpadding='2' cellspacing='2' border='0'><tr><td><input type='text' name='search'></td><td><select name='section'><option value=''>-------</option>";
$cat = mysql_query("SELECT * FROM ".$pre."sections ORDER BY `name` ASC");
for($i = 0; $r = mysql_fetch_assoc($cat); $i++) {
echo "<option value='".$r[name]."'>".ucwords($r[name])."</option>";
}
echo "</select></td></tr><tr><td><input type='submit' value='Submit Search'></td></tr></table></form>";
}

if (($_GET['view'] == "search") && ($_GET['search'])) {
if(!isset($_GET['page'])){
    $page = 1;
} else {
    $page = check($_GET['page']);
}
$from = (($page * $setting["limit"]) - $setting["limit"]);

if ($_GET['section']) {
$section = str_replace("/","",str_replace("-"," ", str_replace("_"," ", $_GET['section'])));
}

if ($_GET['search']) {
$searchentry = htmlentities(check($_GET['search']));
} else {
$searchentry = check($_POST['search']);
}

    $link = str_replace("", "", html_entity_decode(stripslashes($searchentry)));
    $url3 = str_replace("?", "", $link);
	$url3 = str_replace("&amp;", "", $url3);
    $url3 = str_replace("!", "", $url3);
    $url3 = str_replace(":", "", $url3);
    $url3 = str_replace("`", "", $url3);
    $url3 = str_replace("’", "", $url3);
    $url3 = str_replace("(", "", $url3);
    $url3 = str_replace(")", "", $url3);
    $url3 = str_replace("/", "-", $url3);
	$url3 = str_replace(",", "", $url3);
	$url3 = str_replace('"', '', $url3);
	$url3 = str_replace("%22", "", $url3);
	$url3 = str_replace("$", "", $url3);
	$url3 = str_replace(";", "", $url3);
	$url3 = str_replace("$", "", $url3);
	$url3 = str_replace("&#039", "", $link);
	$url3 = str_replace("&amp", "", $url3);
	$url3 = str_replace("&quot", "", $url3);
	$url3 = str_replace("--", "-", $url3);
	$url3 = str_replace("--", "", $url3);
	//$url3 = str_replace("'", "\'", $url3);
	$searchentry = str_replace("-
.", ".", $url3);
$search2 = $searchentry;

$searchentry = preg_replace("/[?']s/", "", $searchentry);
if ($search2 == $searchentry) {
$searchentry = preg_replace("/[?']/", "", $searchentry);
}
$searchentry = preg_replace("/[?:]/", "", $searchentry);
$searchentry = preg_replace("/[?.]/", "", $searchentry);

$ex = explode(" ", $searchentry);

if ($section) {
$cf = mysql_query("SELECT * FROM ".$pre."fields WHERE cat = '".$section."' OR cat = ''");
} else {
$cf = mysql_query("SELECT * FROM ".$pre."fields");
}
for($i = 0; $row = mysql_fetch_assoc($cf); $i++) {
$fields[] = $row[name];
}

while (list($k,$i) = each($ex)) {
while (list($y,$a) = each($fields)) {
if ($y == 0) {
} else {
$sdata .= " OR";
}
$sdata .= " fname = '".$a."' AND data LIKE '%". $i ."%'";
}
}
$results = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."fielddata WHERE".$sdata." ORDER BY `id` DESC"));
if ($results == "1") {
$fet = mysql_fetch_row(mysql_query("SELECT aid FROM ".$pre."fielddata WHERE".$sdata." ORDER BY `id` DESC LIMIT 1"));
$fets = mysql_fetch_row(mysql_query("SELECT name,section FROM ".$pre."articles WHERE id = '".$fet[0]."' AND ver = '' LIMIT 1"));

header("location: ".$mrw_1["content"].fthrough($fets[0], $fets[1], $fet[0]));
} else {
eval (" ?> ".stripslashes(layout("header"))." <?php ");
echo "<title>".$setting["sitename"]." - Search Results For - ".$searchentry."</title>";

echo "<a href='index.php'>Home</a> > <a href='".$mrw["search"]."'>Search</a> > ".$searchentry." - Results<br /><br /><table cellpadding='5' cellspacing='3' border='0' align='center'>";
$num = "0";
$temp = mysql_fetch_row(mysql_query("SELECT template FROM ".$pre."templates WHERE name = 'search_results'"));

$search = mysql_query("SELECT * FROM ".$pre."fielddata WHERE".$sdata." ORDER BY `id` DESC LIMIT $from, $setting[limit]");
$i = "";
for($i = 1; $r = mysql_fetch_assoc($search); $i++) {
if (preg_match("/".$r[aid]."/", $ids, $matches)) {
$ids .= $r[aid]." ";
} else {
$ids .= $r[aid]." ";
$num = $num + 1;
$pab = "";$rab = "";$url3 = "";$search_results = "";
$rs = mysql_fetch_row(mysql_query("SELECT name,section,date,username FROM ".$pre."articles WHERE id = '".$r[aid]."' AND ver = '' LIMIT 1"));
		$n1 = $searchentry;
		$n2 = hilite($searchentry);
		$name = str_replace($n1, $n2, $rs[0]);

$ex = explode(" ", $searchentry);

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

$pab[] = "{link}";
$pab[] = "{title}";
$pab[] = "{subject}";
$pab[] = "{name}";
$pab[] = "{cat}";
$pab[] = "{section}";
$pab[] = "{date}";
$pab[] = "{username}";
$pab[] = "{author}";

if ($_GET['username']) {
$rab[] = "<a href='".$mrw_1["content"].fthrough($rs[0], $rs[1], $r[aid])."'><u>".stripslashes($rs[0])."</u></a>";
} else {
$rab[] = "<a href='".$mrw_1["content"].fthrough($rs[0], $rs[1], $r[aid])."'>".stripslashes($rs[0])."</a>";
}
$rab[] = stripslashes($rs[0]);
$rab[] = stripslashes($rs[0]);
$rab[] = stripslashes($rs[0]);
$rab[] = $rs[1];
$rab[] = $rs[1];
$rab[] = date($setting["date_format"], $rs[2]);
if ($_GET['username']) {
$rab[] = "<u>".$rs[3]."</u>";
$rab[] = "<u>".$rs[3]."</u>";
} else {
$rab[] = $rs[3];
$rab[] = $rs[3];
}

eval (" ?>" . str_replace($pab, $rab, stripslashes($temp[0])) . " <?php ");
}

}
}

$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."fielddata WHERE".$sdata),0);
$total_pages = ceil($total_results / $setting[limit]);

echo "</table>";

if ($total_pages > "1") {
echo "<br /><div align='left'>Select a Page<br />";

// Build Previous Link
if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"".$siteurl."index.php?view=search&search=".$searchentry."&page=$prev\"><<Previous</a>&nbsp;";
}

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

// Build Next Link
if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"".$siteurl."index.php?view=search&search=".$searchentry."&page=$next\">Next>></a>&nbsp;";
}
echo "</div>";
}
}

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