<?php
if (!defined("ADMIN_FILE")) die("Illegal File Access");
require_once("function/secure.php");
require_once("admin/function.php");
get_lang('admin');

function CreateAdmin() {
	global $prefix, $db, $admin_file;
	if ($db->sql_numrows($db->sql_query("SELECT * FROM ".$prefix."_admins")) == 0) {
		$aname = $_POST['aname'];
		$aurl = ($_POST['aurl'] == "http://") ? "" : $_POST['aurl'];
		$aemail = $_POST['aemail'];
		$apwd = md5($_POST['apwd']);
		$apwd2 = md5($_POST['apwd2']);
		$auser_new = intval($_POST['auser_new']);
		$alang = $_COOKIE["lang"];
		$aip = getenv("REMOTE_ADDR");
		$nande = ($aname && $aemail) ? "&aname=$aname&aemail=$aemail" : "";
		if (!$aname || preg_match("/[^a-zA-Zà-ÿÀ-ß0-9_-]/", $aname)) $stop = "?stop=3".$nande."";
		if (strrpos($aname, " ") > 0) $stop = "?stop=4".$nande."";
		if (!$_POST['apwd'] && !$_POST['apwd2']) $stop = "?stop=5".$nande."";
		if ($apwd != $apwd2) $stop = "?stop=6".$nande."";
		if (!$stop) {
			$db->sql_query("INSERT INTO ".$prefix."_admins VALUES (NULL, '$aname', 'Admin', '$aurl', '$aemail', '$apwd', '1', '', '$alang', '$aip')");
			if ($auser_new == 1) {
				$auser_avatar = "00.gif";
				$user_exist = $db->sql_numrows($db->sql_query("SELECT * FROM ".$prefix."_users WHERE user_name='$aname'"));
				if ($user_exist) $db->sql_query("DELETE FROM ".$prefix."_users WHERE user_name='$aname'");
				$db->sql_query("INSERT INTO ".$prefix."_users (user_id, user_name, user_email, user_website, user_avatar, user_regdate, user_password, user_lang, user_last_ip) VALUES (NULL, '$aname', '$aemail', '$aurl', '$auser_avatar', now(), '$apwd', '$alang', '$aip')");
			}
		}
	}
	Header("Location: ".$admin_file.".php".$stop."");
}

function Check() {
	global $prefix, $db, $gfx_chk, $sitekey, $admin_cookie_t, $admin_file;
	if ($_POST['name'] != "" && $_POST['pwd'] != "") {
		session_start();
		$code = substr(hexdec(md5("".date("F j")."".$_SESSION['captcha']."".$conf['sitekey']."")), 2, 6);
		unset($_SESSION['captcha']);
		if (extension_loaded("gd") && $code != $_POST['check'] && ($gfx_chk == 1 || $gfx_chk == 5 || $gfx_chk == 6 || $gfx_chk == 7)) {
			Header("Location: ".$admin_file.".php?stop=1");
			exit;
		} else {
			$name = addslashes(trim(substr($_POST['name'], 0, 25)));
			$pwd = md5(addslashes(trim(substr($_POST['pwd'], 0, 10))));
			list($aid, $aname, $apwd) = $db->sql_fetchrow($db->sql_query("SELECT id, name, pwd FROM ".$prefix."_admins WHERE name='$name' AND pwd='$pwd'"));
			if ($aname == $name && $apwd == $pwd) {
				$info = base64_encode("$aid:$aname:$apwd");
				setcookie(ADMIN_COOKIE, "$info", time() + intval($admin_cookie_t));
				$ip = getenv("REMOTE_ADDR");
				$db->sql_query("DELETE FROM ".$prefix."_session WHERE uname='$ip' AND guest='1'");
				$db->sql_query("UPDATE ".$prefix."_admins SET ip='$ip' WHERE id='$aid'");
				Header("Location: ".$admin_file.".php");
			} else {
				Header("Location: ".$admin_file.".php?stop=2");
			}
		}
	} else {
		Header("Location: ".$admin_file.".php?stop=2");
	}
}

function Login() {
	global $prefix, $db, $gfx_chk, $admin_file;
	head();
	if ($db->sql_numrows($db->sql_query("SELECT * FROM ".$prefix."_admins")) == 0) {
		echo "<form action=\"".$admin_file.".php\" method=\"post\">"
		."<br /><br /><table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\"><tr><td>";
		if ($_GET['stop'] == 3) {
			echo "<table align=\"center\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\"><tr><td>";
			warning(""._ERRORINVNICK."", "", "", 1);
			echo "</td></tr></table><br /><br />";
		} elseif ($_GET['stop'] == 4) {
			echo "<table align=\"center\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\"><tr><td>";
			warning(""._NICKNOSPACES."", "", "", 1);
			echo "</td></tr></table><br /><br />";
		} elseif ($_GET['stop'] == 5) {
			echo "<table align=\"center\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\"><tr><td>";
			warning(""._NOPASS."", "", "", 1);
			echo "</td></tr></table><br /><br />";
		} elseif ($_GET['stop'] == 6) {
			echo "<table align=\"center\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\"><tr><td>";
			warning(""._PASSDIFFERENT."", "", "", 1);
			echo "</td></tr></table><br /><br />";
		}
		echo "<table align=\"center\" width=\"350\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\" class=\"bodyline\"><tr><th>"._ADMINLOGIN_NEW."</th></tr></table>"
		."<table align=\"center\" width=\"350\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\" class=\"blockline\">"
		."<tr><td>"._NICKNAME.":</td><td><input type=\"text\" name=\"aname\" value=\"".$_GET['aname']."\" size=\"25\" maxlength=\"25\"></td></tr>"
		."<tr><td>"._HOMEPAGE.":</td><td><input type=\"text\" name=\"aurl\" value=\"http://".$_SERVER['SERVER_NAME']."\" size=\"25\" maxlength=\"255\"></td></tr>"
		."<tr><td>"._EMAIL.":</td><td><input type=\"text\" name=\"aemail\" value=\"".$_GET['aemail']."\" size=\"25\" maxlength=\"255\"></td></tr>"
		."<tr><td>"._PASSWORD.":</td><td><input type=\"password\" name=\"apwd\" size=\"25\" maxlength=\"10\"></td></tr>"
		."<tr><td>"._RETYPEPASSWORD.":</td><td><input type=\"password\" name=\"apwd2\" size=\"25\" maxlength=\"10\"></td></tr>"
		."<tr><td>"._CREATEUSERDATA."</td><td><input type=\"radio\" name=\"auser_new\" value=\"1\" checked>"._YES." &nbsp;<input type=\"radio\" name=\"auser_new\" value=\"0\">"._NO."</td></tr>"
		."<tr><td colspan=\"2\" align=\"center\"><input type=\"hidden\" name=\"op\" value=\"CreateAdmin\">"
		."<input type=\"submit\" value=\""._SEND."\"></td></tr></table>"
		."</td></tr></form></table>";
	} else {
		echo "<form action=\"".$admin_file.".php\" method=\"post\">"
		."<br /><br /><table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\"><tr><td>";
		if ($_GET['stop'] == 1) {
			echo "<table align=\"center\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\"><tr><td>";
			warning(""._SECCODEINCOR."", "", "", 1);
			echo "</td></tr></table><br /><br />";
		} elseif ($_GET['stop'] == 2) {
			echo "<table align=\"center\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\"><tr><td>";
			warning(""._LOGININCOR."", "", "", 1);
			echo "</td></tr></table><br /><br />";
		}
		echo "<table align=\"center\" width=\"250\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\" class=\"bodyline\"><tr><th>"._ADMINLOGIN."</th></tr></table>"
		."<table align=\"center\" width=\"250\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\" class=\"blockline\">"
		."<tr><td width=\"50\" align=\"center\"><img src=\"images/admin/admins.gif\" border=\"0\" alt=\""._ADMINLOGIN."\"></td>"
		."<td align=\"center\"><table cellspacing=\"3\" cellpadding=\"0\" border=\"0\">"
		."<tr><td>"._NICKNAME.":</td><td><input type=\"text\" name=\"name\" size=\"10\" maxlength=\"25\"></td></tr>"
		."<tr><td>"._PASSWORD.":</td><td><input type=\"password\" name=\"pwd\" size=\"10\" maxlength=\"10\"></td></tr>";
		if (extension_loaded("gd") && ($gfx_chk == 1 || $gfx_chk == 5 || $gfx_chk == 6 || $gfx_chk == 7)) {
			echo "<tr><td>"._SECURITYCODE.":</td><td><img src=\"index.php?captcha=1\" border=\"1\" alt=\""._SECURITYCODE."\" title=\""._SECURITYCODE."\"></td></tr>"
			."<tr><td>"._TYPESECCODE.":</td><td><input type=\"text\" name=\"check\" size=\"10\" maxlength=\"6\"></td></tr>";
		}
		echo "</table></td></tr><tr><td colspan=\"2\" align=\"center\"><input type=\"hidden\" name=\"op\" value=\"Check\"><input type=\"submit\" value=\""._LOGIN."\"></td></tr></table>"
		."</td></tr></form></table>";
	}
	foot();
}

function adminmenu($url, $title, $image) {
    global $counter, $admin_panel, $content_am, $class;
	$ltitle = ($class) ? "".$title." - "._DEACT."" : $title;
	$image = "images/admin/$image";
    if ($admin_panel == 1) {
		echo "<td align=\"center\" valign=\"top\" width=\"15%\"><a href=\"$url\" title=\"$ltitle\"><img src=\"$image\" border=\"0\" ".$class." alt=\"$ltitle\" title=\"$ltitle\"><br /><b ".$class.">$title</b></a></td>";
		if ($counter == 5) {
			echo "</tr><tr>";
			$counter = 0;
		} else {
			$counter++;
		}
    } else {
		$content_am .= "<table><tr><td><img src=\"images/navi.gif\" border=\"0\"></td><td><a href=\"$url\" title=\"$ltitle\">$title</a></td></tr></table>";
	}
}

function GraphicAdmin() {
	global $aid, $admin_panel, $language, $admin, $prefix, $db, $counter, $admin_file, $currentlang, $admin_home, $paneldis, $class;
	if (file_exists("setup.php")) warning(""._DELSETUP."", "", "", 1);
	if (phpversion() < "4.3.0") warning(""._PHPSETUP."", "", "", 1);
	if ($admin_panel == 1) {
		$display = (!$admin_home && $paneldis) ? "style=\"display:none;\"" : "";
		echo "<script language=\"JavaScript\" type=\"text/javascript\" src=\"javascripts/SwitchMenu.js\"></script>";
		if (is_admin_god($aid)) {
			echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\"><tr><td>"
			."<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\" class=\"bodyline\">"
			."<div id=\"cont\"><tr id=\"tmenua\" onClick=\"SwitchMenu('tsuba')\" style=\"cursor:pointer;\"><th>"._ADMINMENU."</th></tr></div></table>"
			."<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\" id=\"tsuba\" ".$display." class=\"blockline\"><tr>";
			$dir = opendir("admin/links");
			while ($file = readdir($dir)) {
				if (substr($file, 0, 6) == "links.") include("admin/links/".$file."");
			}
			closedir($dir);
			adminmenu("".$admin_file.".php?op=LogOut", ""._ADMINLOGOUT."", "logout.gif");
			echo"</tr></table></td></tr></table>";
			$counter = "";
		}
		echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\"><tr><td>"
		."<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\" class=\"bodyline\">"
		."<div id=\"cont\"><tr id=\"tmenum\" onClick=\"SwitchMenu('tsubm')\" style=\"cursor:pointer;\"><th>"._MODULESADMIN."</th></tr></div></table>"
		."<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\" id=\"tsubm\" ".$display." class=\"blockline\"><tr>";
		$result = $db->sql_query("SELECT title, active FROM ".$prefix."_modules ORDER BY title ASC");
		while (list($title, $active) = $db->sql_fetchrow($result)) {
			if (is_admin_god($aid) || is_admin_modul($aid, $title)) {
				$class = (!$active) ? "class=\"hidden\"" : "";
				if (file_exists("modules/$title/admin/index.php") && file_exists("modules/$title/admin/links.php")) {
					include_once("modules/$title/admin/links.php");
					include_once("modules/$title/admin/language/lang-".$currentlang.".php");
				}
			}
		}
		$class = "";
		adminmenu("".$admin_file.".php?op=LogOut", ""._ADMINLOGOUT."", "logout.gif");
		echo"</tr></table></td></tr></table>";
	}
}

function panelblock() {
	global $aid, $admin_panel, $language, $admin, $prefix, $db, $admin_file, $content_am, $currentlang;
	if ($admin_panel == 0) {
		if (is_admin_god($aid)) {
			$dir = opendir("admin/links");
			while ($file = readdir($dir)) {
				if (substr($file, 0, 6) == "links.") include("admin/links/".$file."");
			}
			closedir($dir);
			adminmenu("".$admin_file.".php?op=LogOut", ""._ADMINLOGOUT."", "logout.gif");
			$title_a = ""._ADMIN."";
			themesidebox($title_a, $content_am, 1, 1);
			$content_am = "";
		}
		$result = $db->sql_query("SELECT title, active FROM ".$prefix."_modules ORDER BY title ASC");
		while (list($title, $active) = $db->sql_fetchrow($result)) {
			if (is_admin_god($aid) || is_admin_modul($aid, $title)) {
				if (file_exists("modules/$title/admin/index.php") && file_exists("modules/$title/admin/links.php")) {
					include_once("modules/$title/admin/links.php");
					include_once("modules/$title/admin/language/lang-".$currentlang.".php");
				}
			}
		}
		adminmenu("".$admin_file.".php?op=LogOut", ""._ADMINLOGOUT."", "logout.gif");
		$title_m = ""._MODULES."";
		themesidebox($title_m, $content_am, 2, 1);
	}
}

function AdminMain() {
	global $language, $admin, $aid, $admin_panel, $prefix, $db, $admin_file, $pagenum, $admin_home, $multilingual;
	$admin_home = 1;
    head();
	GraphicAdmin();
	$radminarticle = (is_admin_modul($aid, "News")) ? 1 : 0;
	if ($admin_panel == 0) title(""._ADMINMENU."");
	$result = $db->sql_query("SELECT sid, aid, title, time, alanguage FROM ".$prefix."_stories WHERE status='2' ORDER BY sid DESC");
	if ($db->sql_numrows($result) > 0) {
		OpenTable();
		echo "<center><font class=\"option\">"._SUBMISSIONS."</font></center><br />";
	    echo "<table width=\"100%\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" class=\"bgcolor4\"><tr><th>"._ID."</th><th>"._AUTHOR."</th><th>"._TITLE."</th><th>"._DATE."</th>";
	    if ($multilingual == 1) echo "<th>"._LANGUAGE."</th>";
		echo "<th>"._FUNCTIONS."</th></tr>";
	    while (list($sid, $aid, $title, $time, $alanguage) = $db->sql_fetchrow($result)) {
			echo "<tr class=\"bgcolor1\"><td align=\"center\">".$sid."</td><td>".$aid."</td><td>".$title."</td><td align=\"center\">".$time."</td>";
			if ($multilingual == 1) {
				$alanguage = (!$alanguage) ? ""._ALL."" : $alanguage;
				echo "<td align=\"center\">".ucfirst($alanguage)."</td>";
			}
			if ($radminarticle == 1 || is_admin_god($aid)) {
				echo "<td align=\"center\"><a href=\"".$admin_file.".php?op=AddStory&id=$sid\" title=\""._EDIT."\"><img src=\"images/edit.gif\" border=\"0\" alt=\""._EDIT."\"></a> <a href=\"".$admin_file.".php?op=DeleteStory&id=$sid\" title=\""._DELETE."\"><img src=images/delete.gif border=\"0\" alt=\""._DELETE."\"></a></td>";
			} else {
				echo "<td align=\"center\">"._NO."</td>";
			}
			echo "</tr>";
	    }
		echo "</table>";
		CloseTable();
	}


	$result = $db->sql_query("SELECT sid, aid, title, time, alanguage FROM ".$prefix."_stories WHERE time >= NOW() AND status='1' ORDER BY time");
    if ($db->sql_numrows($result) > 0) {
		OpenTable();
		echo "<center><font class=\"option\">"._AUTOMATEDARTICLES."</font></center><br />";
		echo "<table width=\"100%\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" class=\"bgcolor4\"><tr><th>"._ID."</th><th>"._AUTHOR."</th><th>"._TITLE."</th><th>"._DATE."</th>";
	    if ($multilingual == 1) echo "<th>"._LANGUAGE."</th>";
		echo "<th>"._FUNCTIONS."</th></tr>";
		while (list($sid, $aid, $title, $time, $alanguage) = $db->sql_fetchrow($result)) {
			echo "<tr class=\"bgcolor1\"><td align=\"center\">".$sid."</td><td>".$aid."</td><td>".$title."</td><td align=\"center\">".$time."</td>";
			if ($multilingual == 1) {
				$alanguage = (!$alanguage) ? ""._ALL."" : $alanguage;
				echo "<td align=\"center\">".ucfirst($alanguage)."</td>";
			}
			if ($radminarticle == 1 || is_admin_god($aid)) {
				echo "<td align=\"center\"><a href=\"".$admin_file.".php?op=AddStory&id=$sid\" title=\""._EDIT."\"><img src=\"images/edit.gif\" border=\"0\" alt=\""._EDIT."\"></a> <a href=\"".$admin_file.".php?op=DeleteStory&id=$sid\" title=\""._DELETE."\"><img src=images/delete.gif border=\"0\" alt=\""._DELETE."\"></a></td>";
			} else {
				echo "<td align=\"center\">"._NO."</td>";
			}
			echo "</tr>";
	    }
		echo "</table>";
		CloseTable();
	}
	$storynum = 25;
	$pagenum = (!intval($pagenum)) ? 1 : intval($pagenum);
	$offset = ($pagenum-1) * $storynum;
	$offset = intval($offset);

######################
$b = 0;
echo "<center><font class=\"option\">"._ARTICLEADMIN."</font></center><br />";
	$result = $db->sql_query("SELECT s.sid, s.title, s.topic, s.alanguage, s.status, t.topicname FROM ".$prefix."_stories AS s LEFT JOIN ".$prefix."_topics AS t on (s.topic=t.topicid) WHERE time <= NOW() AND status!='2' ORDER BY time DESC LIMIT $offset, $storynum");
	if ($db->sql_numrows($result) > 0) {


		OpenTable();

###############
echo "<form name=\"comm\" action=\"".$admin_file.".php\" method=\"post\">";


		
		echo "<script type=\"text/javascript\" src=\"javascripts/global_func.js\"></script><table width=\"100%\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\"><tr><th>"._ID."</th><th>"._TITLE."</th><th>"._TOPIC."</th>";
	    if ($multilingual == 1) echo "<th>"._LANGUAGE."</th>";
		echo "<th>"._STATUS."</th><th>"._FUNCTIONS."</th><th><input type=\"checkbox\" name=\"markcheck\" onclick=\"MarkAll('".$storynum."', 'chk_', document.comm.markcheck.checked)\"></th></tr>";
		while (list($sid, $title, $topicid, $alanguage, $status, $topicname) = $db->sql_fetchrow($result)) {
			$active = ($status == 1) ? "<font color=\"#009900\">"._ACTIVATE."</font>" : "<font color=\"#FF0000\">"._INACTIVE."</font>";
			$change = ($status == 1) ? "title=\""._INACTIVE."\"><img src=\"images/inactive.gif\" border=\"0\" alt=\""._INACTIVE."\">" : "title=\""._ACTIVATE."\"><img src=\"images/activate.gif\" border=\"0\" alt=\""._ACTIVATE."\">";
			$act = ($status == 1) ? 0 : 1;
			$change_status = "<a href=\"".$admin_file.".php?op=ActiveStory&id=$sid&act=$act&pagenum=$pagenum\" $change</a>";

############################
$checkb = "<input type=\"checkbox\" id=\"chk_".$b."\"  name=\"id[]\"  value=\"".$sid."\">";

            $b++;

			echo "<tr class=\"bgcolor1\"><td align=\"center\">".$sid."</td>"
			."<td>".$title."</td>"
			."<td>".$topicname."</td>";
			if ($multilingual == 1) {
				$alanguage = (!$alanguage) ? ""._ALL."" : $alanguage;
				echo "<td align=\"center\">".ucfirst($alanguage)."</td>";
			}
			echo "<td align=\"center\">".$active."</td>";
			if ($radminarticle == 1 || is_admin_god($aid)) {
				echo "<td align=\"center\"><a href=\"".$admin_file.".php?op=AddStory&id=$sid\" title=\""._EDIT."\"><img src=\"images/edit.gif\" border=\"0\" alt=\""._EDIT."\"></a> <a href=\"".$admin_file.".php?op=DeleteStory&id=$sid\" title=\""._DELETE."\"><img src=images/delete.gif border=\"0\" alt=\""._DELETE."\"></a> ".$change_status."</td><td align=\"center\">".$checkb."</td>";
			} else {
				echo "<td align=\"center\">"._NO."</td>";
			}
			echo "</tr>";
	    }
		echo "</table>";

############################
	echo"<div class=\"button\"><input type=\"hidden\" name=\"op\" value=\"DeleteStory\">
<input type=\"submit\" value=\""._DELETE." îòìå÷åííûå\" class=\"fbutton\"></div></form>";



		CloseTable();
		list($numstories) = $db->sql_fetchrow($db->sql_query("SELECT Count(sid) FROM ".$prefix."_stories WHERE time <= NOW() AND status!='2'"));
		$numpages = ceil($numstories / $storynum);
		num_page($module_name, $numstories, $numpages, $storynum, "");
	}
	foot();
}

if (is_admin($admin) && $db->sql_numrows($db->sql_query("SELECT * FROM ".$prefix."_admins")) > 0) {
	$op = ($_POST['op']) ? $_POST['op'] : $_GET['op'];
	$op = (!isset($op)) ? "AdminMain" : $op;
	
	// Register globals On
	if ($old_modules == 1) {
		if (!ini_get("register_globals")) @import_request_variables('GPC');
	}

	$pagetitle = "$defis "._ADMINMENU."";
	$aid = intval(substr($admin[0], 0, 11));
	switch($op) {
		case "GraphicAdmin":
        GraphicAdmin();
        break;

        case "AdminMain":
        AdminMain();
        break;

        case "LogOut":
		setcookie(ADMIN_COOKIE, false);
		unset($admin);
		Header("Location: ".$admin_file.".php");
        break;
		
		default:
		if (is_admin_god($aid)) {
			$dir = opendir("admin/modules");
			while ($file = readdir($dir)) {
				if (preg_match("/(\.php)$/is", $file) && $file != "." && $file != "..") include("admin/modules/".$file."");
			}
			closedir($dir);
		}
		$result = $db->sql_query("SELECT title FROM ".$prefix."_modules ORDER BY title ASC");
        while (list($mtitle) = $db->sql_fetchrow($result)) {
			if (is_admin_god($aid) || is_admin_modul($aid, $mtitle)) {
				if (file_exists("modules/".$mtitle."/admin/index.php") && file_exists("modules/".$mtitle."/admin/links.php")) include("modules/".$mtitle."/admin/index.php");
			}
		}
        break;
	}
} else {
	switch($_POST['op']) {
		default:
		Login();
		break;
		
		case "CreateAdmin":
		CreateAdmin();
		break;
		
		case "Check";
		Check();
		break;
	}
}
?>