<?php
$systemVersion='3.2.12';
Error_Reporting(4);
//session_set_cookie_params(1800);
session_start();
$HTTP_HOST=$_SERVER['HTTP_HOST'];
$PATH=$config["path"]."admin/";
chdir($DOCUMENT_ROOT.$PATH);
include "../tools/db.php";
include "../tools/tools.php";
include "../tools/template.php";
include "../tools/class.link.php";
include "../tools/class.message.php";

get_all_vars();
if (isset($_search) and get_magic_quotes_gpc()) $_search=stripslashes($_search);

function check_level($module="") {
        global $ui, $db, $config;
        $res=array();
        if ($ui["system_status"]==1) $res["admin"]=1;
        else {
                $sql="select a.title from users".$config["table_prefix"]." u, user_access".$config["table_prefix"]." ua, access".$config["table_prefix"]." a, modules".$config["table_prefix"]." m where
                        u.login='".$ui["login"]."' and
                        u.password='".$ui["password"]."' and
                        u.status='1' and
                        u.id=ua.user_id and
                        a.id=ua.access_id and
                        a.module_id=m.id and
                        m.title='$module'";
                $rs=$db->select($sql);
                for ($i=0; $i<count($rs); $i++) $res[$rs[$i]["title"]]=1;
        }
        return $res;
}

function not_empty($val) {
        return (trim($val));
}


function get_search_regular($keywords) {
        $keywords=strtolower(trim(strip_tags($keywords)));
        preg_match_all("/\"([^\"]+)\"/", $keywords, $st);
        $keywords=preg_replace("/(\"[^\"]+\")/", "", $keywords);
        $words=preg_split("/[^\w\d\-]+/", $keywords, -1, PREG_SPLIT_NO_EMPTY);
        $res=implode("|", array_filter(array_merge($st[1], $words), "not_empty"));
        return (!$res ? "" : ".*($res).*");
}

function get_keywords($keywords) {
        $keywords=strtolower(trim(strip_tags($keywords)));
        preg_match_all("/\"([^\"]+)\"/", $keywords, $st);
        $keywords=preg_replace("/(\"[^\"]+\")/", "", $keywords);
        $words=preg_split("/[^\w\d]+/", $keywords, -1, PREG_SPLIT_NO_EMPTY);
	$res=array_filter(array_merge($st[1], $words), "not_empty");
        return $res;
}

function get_search_where($fields="", $keywords="", $where="") {
        $fields=explode(", ", $fields);
        $keywords=get_keywords($keywords);
        $res="";
        for ($i=0; $i<count($fields); $i++) {
		$r='';
		for ($j=0; $j<count($keywords); $j++) {
			if (trim($keywords[$j])) $r.=($r ? ' and ' : "").$fields[$i]." like '%".$keywords[$j]."%'";
		}
		if ($r) $res.=($res ? ' or ' : '')."($r)";
	}
        if ($res) $res=($where ? "$where and ($res)" : "where $res");
	else $res=$where;
        return $res;
}

$db = new DB();
if (isset($_cp)) {
	$sql="update settings".$config["table_prefix"]." set rec_count='$_cp' where owner_id='".$ui["id"]."'";
	$db->execute($sql);
}
$settings=load_settings();
$array=pathinfo($_SERVER['SCRIPT_NAME']);
$link=new Link();

$page = new Template("templates/".(strtolower($_SERVER['SCRIPT_NAME'])==strtolower($PATH.'index.php') ? "first" : "index").".tpl");
$page->dictionary("common/dict.ru");
include "login/index.php";
$user_permissions=($ui["system_status"]!=1 ? get_permissions($ui['id']) : array());

$rec_count_list=array(10 => 10, 25 => 25, 50 => 50, 100 => 100, 500 => 500, 1000 => 1000);
$settings["rec_count"]=(in_array($settings["rec_count"], $rec_count_list) ? $settings["rec_count"] : $rec_count_list[10]);
get_row_from_array("REC_COUNT", $rec_count_list, $settings["rec_count"]);
$page->assign(array(
        "PATH"                  => $PATH,
        "cp"                    => $settings["rec_count"],
	"mailing_groups_count"  => $db->count("select count(*) from mailing_groups".$config["table_prefix"].(isset($user_permissions['groups_list']) ? " where id in (".$user_permissions['groups_list'].")" : ""), true),
        "subscribers_count"     => $db->count("select count(*) from subscribers".$config["table_prefix"].($ui["system_status"]!=1 ? " where owner_id='".$ui["id"]."'" : ""), true),
        "newsletters_count"     => $db->count("select count(*) from newsletters".$config["table_prefix"].(isset($user_permissions['newsletters_list']) ? " where id in (".$user_permissions['newsletters_list'].")" : ""), true),
        "users_count"           => $db->count("select count(*) from users".$config["table_prefix"], true),
        "last_login"            => "-",
        "is_quick_steps"        => $settings["is_quick_steps"],
	"date_format"		=> $settings["date_format"],
	'system_version'	=> $systemVersion,
));
return $page;
?>