<?php
include_once "../../../tools/inc.php";
$page = include("../../common.php");
include("../tools/function.string.php");
include("../tools/function.sql.php");
$n=$settings["rec_count"];
$table="smtp_accounts".$config["table_prefix"];
$table_s="settings".$config["table_prefix"];
$access=check_level("SMTP Accounts");
$sort=(in_array($sort, array("title", "host")) ? $sort : "title");
$sort_d=(isset($sort_d) ? 1 : 0);
$IMG="smtp.gif";

$sort_link=new Link($link->get());
$link->add("sort");
$link->add("sort_d");
$filter_link=new Link($link->get());
$offset_link=new Link($link->get());
$link->add("offset");

$message_list=array(
        1 => "{rs:MSG_SMTP} {rs:MSG_CREATED}",
        2 => "{rs:MSG_SMTP} {rs:MSG_UPDATED}",
        3 => "{rs:MSG_SMTP} {rs:MSG_DELETED}",
);

$message=new Message();
$error=new Message("error");
if (isset($_r)) $message->add($message_list[$_r]);
if (isset($del) and (isset($access["delete"]) or isset($access["admin"]))) {
        $del=(is_array($del) ? implode($del, ", ") : $del);
        $where="where id in ($del)";
        if (!isset($access["admin"])) $where.=" and owner_id='".$ui["id"]."'";
        $sql="select * from $table $where";
        $rs=$db->select($sql);
        for ($i=0; $i<count($rs); $i++) {
                $sql=array();
                $sql[]="update $table_s set mail_mode=0 where autoresponder_smtp='".$rs[$i]["id"]."' or campaign_schedule_smtp='".$rs[$i]["id"]."'";
                $sql[]="delete from $table where id='".$rs[$i]["id"]."'";
                $db->execute_all($sql);
        }
        $link->add("_r", "3");
        $db->close();
        header("Location: ".$link->get());
        exit;
} elseif (isset($edit) and (isset($access["edit"]) or isset($access["view"]) or isset($access["admin"]))) {
        $IMG="smtp_edit.gif";
        $where="where id='$edit'";
        if (!isset($access["admin"])) $where.=" and owner_id='".$ui["id"]."'";
        $sql="select * from $table $where";
        $rs=$db->select($sql);
        $data=$rs[0];
        if (!isset($data["id"])) {
                $edit=0;
                $data=array(
                        "login"         => "",
                        "password"      => "",
                        "host"          => "",
                        "port"          => 25,
                );
                $r["owner_id"]=$ui["id"];
        } else {
                $edit=$data["id"];
                $r["owner_id"]=$data["owner_id"];
        }
        if (isset($save)) {
                $r=check_array($r);
                if (!$r["title"] or !$r["login"] or (!$edit and !$r["password"]) or !$r["host"] or !$r["port"]) $error->add("{rs:ERROR_FIELDS}");
		if (!$error->count and !isset($access['edit']) and !isset($access["admin"])) $error->add("{rs:ERROR_ACCESS_RESTRICTED}");
                if (!$error->count) {
                        $r["modify_user"]=$ui["id"];
                        $r["modify_time"]=time();
                        $r["password"]=($r["password"] ? $r["password"] : $data["password"]);
                        $fields="title, login, password, host, port, owner_id, modify_user, modify_time";
                        $sql=($edit ? get_sql_update($table, $fields, $r, "where id='$edit'") : get_sql_insert($table, $fields, $r));
                        $db->execute($sql);
                        $db->close();
                        $link->add("_r", ($edit ? 2 : 1));
                        header("Location: ".$link->get());
                        exit;
                } else {
                        $r["id"]=$edit;
                        $data=html_string($r, true);
                }
        } else $data=html_string($data);
        $page->section("SMTP_EDIT");
        $data["BACK_LINK"]=($link->get() ? $link->get() : './');
        $data["BACK_TITLE"]="{rs:TITLE_SMTP_ACCOUNTS}";
	$data["WINDOW_TITLE"]="{rs:EDIT_SMTP_ACCOUNTS}";
        $page->assign($data);
} elseif (isset($access["view"]) or isset($access["admin"])) {
        if (isset($edit) or isset($del) or isset($permission)) $error->add("{rs:ERROR_ACCESS_RESTRICTED}");
        $page->section("SMTP");
        $where=($access["admin"] ? "" : "where owner_id='".$ui["id"]."'");
        $sql="select count(*) from $table $where";
        $rs=$db->select($sql);
        $count=$rs[0][0];
        $i=ceil($count/$n)-1;
        $offset=(($offset>$i) ? $i : ($offset<0 ? 0 : $offset));
        $sql="select * from $table $where order by host".($sort_d ? " DESC" : "")." limit ".($offset*$n). ", $n";
        $rs = $db->select($sql);
        for ($i=0; $i<count($rs); $i++) {
                $data=$rs[$i];
                $page->addrow("LINE", $data);
        }
	$sort_link=$sort_link->get();
        $page->assign(array(
                "count"         	=> $count,
                "offset"        	=> get_new_offsets($count, $n, $offset_link->get(), $offset),
                "BACK_LINK"     	=> $PATH."settings/",
                "BACK_TITLE"    	=> "{rs:TITLE_SETTINGS}",
                "sort_by_title" 	=> $sort_link.($sort_link ? '&' : '?')."sort=title".($sort=="title" ? ($sort_d ? "" : "&sort_d=1") : ""),
                "sort_by_login" 	=> $sort_link.($sort_link ? '&' : '?')."sort=login".($sort=="login" ? ($sort_d ? "" : "&sort_d=1") : ""),
                "sort_by_host"  	=> $sort_link.($sort_link ? '&' : '?')."sort=host".($sort=="host" ? ($sort_d ? "" : "&sort_d=1") : ""),
                "sort_title_img"	=> ($sort=="title" ? ($sort_d ? "_asc" : "_desc") : ""),
                "sort_login_img"	=> ($sort=="login" ? ($sort_d ? "_asc" : "_desc") : ""),
                "sort_host_img" 	=> ($sort=="host" ? ($sort_d ? "_asc" : "_desc") : ""),
		"WINDOW_TITLE"		=> "{rs:TITLE_SMTP_ACCOUNTS}",
        ));
} else {
        $page->section("ACCESS_DENIED");
        $error->add("{rs:ERROR_ACCESS_RESTRICTED}");
}
add_to_tree("{rs:TITLE_SETTINGS}", "settings/");
add_to_tree("{rs:TITLE_SMTP_ACCOUNTS}", "settings/smtp/");
$db->close();
$page->assign(array(
        "TITLE"                 => "{rs:TITLE_SMTP_ACCOUNTS}",
        "ERROR"                 => ($error->count ? $error->get() : ""),
        "MESSAGE"               => ($message->count ? $message->get() : ""),
        "LINK"                  => $link->get(),
        "FILTER_LINK"           => $filter_link->get(),
        "is_settings"           => 1,
        "IMG"                   => $IMG,
));
print $page->evaluate();
?>