<?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="settings".$config["table_prefix"];
$table_sg="subscribers_group".$config["table_prefix"];
$table_smtp="smtp_accounts".$config["table_prefix"];
$table_pop3="pop3_accounts".$config["table_prefix"];
$access=check_level("Settings");

$message_list=array(
        1 => "{rs:MSG_SETTINGS} {rs:MSG_UPDATED}",
);

$mail_mode_list=array(
	0 => "{rs:MAIL_MODE_0}",
	1 => "{rs:MAIL_MODE_1}",
);

$date_format_list=array(
	0 => "{rs:DATE_FORMAT_0}",
	1 => "{rs:DATE_FORMAT_1}",
	2 => "{rs:DATE_FORMAT_2}",
);

$message=new Message();
$error=new Message("error");
if (isset($_r)) $message->add($message_list[$_r]);

$sql="select * from $table where owner_id='".$ui["id"]."'";
$rs=$db->select($sql);
$data=$rs[0];
$edit=(isset($data["id"]) ? $data["id"] : 0);
if (isset($save) and (isset($access["edit"]) or isset($access["admin"]))) {
	$r=check_array($r);
	if ($save) {
		if (!$r["autoresponder_smtp"] and $r["mail_mode"]) $error->add("{rs:ERROR_AUTORESPONDER_SMTP}");
		if (!$r["autoresponder_pop3"] and $r["mail_mode"]) $error->add("{rs:ERROR_AUTORESPONDER_POP3}");
		if (!$r["campaign_schedule_smtp"] and $r["mail_mode"]) $error->add("{rs:ERROR_CAMPAIGN_SCHEDULE_SMTP}");
		if (!$r["campaign_schedule_pop3"] and $r["mail_mode"]) $error->add("{rs:ERROR_CAMPAIGN_SCHEDULE_POP3}");
		if (!check_email($r["mail_from"])) $error->add("{rs:ERROR_FROM_MAIL}");
	}
	if (!$error->count and $save) {
		$r["owner_id"]=($edit ? $data["owner_id"] : $ui["id"]);
		$r["modify_user"]=$ui["id"];
		$r["modify_time"]=time();
		$r["mail_count"]=(isset($access["admin"]) ? $r["mail_count"] : get_val($table, 'mail_count', 'limit 1'));
		$fields="mail_mode, autoresponder_smtp, autoresponder_pop3, campaign_schedule_smtp, campaign_schedule_pop3, mail_from, mail_from_name, encoding, mail_count, date_format, is_quick_steps, email_activation, unsubscribe_confirm, show_encoding, owner_id, modify_user, modify_time";
		$sql=array();
		$sql[]=($edit ? get_sql_update($table, $fields, $r, "where id='$edit'") : get_sql_insert($table, $fields, $r));
		if (isset($access["admin"])) $sql[]="update $table set mail_count='".$r['mail_count']."'";
		$db->execute_all($sql);
		$db->close();
		$link->add("_r", "1");
		header("Location: ".$link->get());
		exit;
	} else $data=html_string($r, true);
	get_row_from_array("MAIL_MODE_SELECT", $mail_mode_list, $data["mail_mode"]);
	$sql="select id, title from $table_smtp $where order by title";
	get_row_from_sql("AUTORESPONDER_SMTP_SELECT", $sql, $data["autoresponder_smtp"]);
	get_row_from_sql("CAMPAIGN_SCHEDULE_SMTP_SELECT", $sql, $data["campaign_schedule_smtp"]);
	$sql="select id, title from $table_pop3 $where order by title";
	get_row_from_sql("AUTORESPONDER_POP3_SELECT", $sql, $data["autoresponder_pop3"]);
	get_row_from_sql("CAMPAIGN_SCHEDULE_POP3_SELECT", $sql, $data["campaign_schedule_pop3"]);
	get_row_from_table("ENCODING_SELECT", "encodes".$config["table_prefix"], $data["encoding"], "code", "title");
	get_row_from_array("DATE_FORMAT_SELECT", $date_format_list, $data["date_format"]);
	$data['show_mail_count']=isset($access["admin"]);
	$page->section("SETTINGS");
        $page->assign($data);
} elseif (isset($access["view"]) or isset($access["admin"])) {
        if (!isset($data["id"])) {
		$data=array(
			"mail_mode"		=> 0,
			"mail_from"		=> $db->get_val('users'.$config["table_prefix"], 'mail', "where id='".$ui['id']."'"),
			"mail_from_name"	=> "Mailing List Manager Pro",
			"encoding"		=> "UTF-8",
			"mail_count"		=> 60,
			"is_quick_steps"	=> 1,
			"email_activation"	=> 1,
			"unsubscribe_confirm"	=> 1,
			"show_encoding"		=> 1,
		);
	}
	$data=html_string($data);
	get_row_from_array("MAIL_MODE_SELECT", $mail_mode_list, $data["mail_mode"]);
	$sql="select id, title from $table_smtp $where order by title";
	get_row_from_sql("AUTORESPONDER_SMTP_SELECT", $sql, $data["autoresponder_smtp"]);
	get_row_from_sql("CAMPAIGN_SCHEDULE_SMTP_SELECT", $sql, $data["campaign_schedule_smtp"]);
	$sql="select id, title from $table_pop3 $where order by title";
	get_row_from_sql("AUTORESPONDER_POP3_SELECT", $sql, $data["autoresponder_pop3"]);
	get_row_from_sql("CAMPAIGN_SCHEDULE_POP3_SELECT", $sql, $data["campaign_schedule_pop3"]);
	get_row_from_table("ENCODING_SELECT", "encodes".$config["table_prefix"], $data["encoding"], "code", "title");
	get_row_from_array("DATE_FORMAT_SELECT", $date_format_list, $data["date_format"]);
	$data['show_mail_count']=isset($access["admin"]);
	$page->section("SETTINGS");
        $page->assign($data);
} else {
	$page->section("ACCESS_DENIED");
	$error->add("{rs:ERROR_ACCESS_RESTRICTED}");
}
add_to_tree("{rs:TITLE_SETTINGS}", "settings/");
$db->close();
$page->assign(array(
	"TITLE"			=> "{rs:TITLE_SETTINGS}",
	"ERROR"			=> ($error->count ? $error->get() : ""),
	"MESSAGE"		=> ($message->count ? $message->get() : ""),
	"LINK"			=> $link->get(),
	"is_settings"		=> 1,
	"IMG"			=> "settings.gif",
	"BACK_LINK"		=> $PATH,
	"BACK_TITLE"		=> "{rs:TITLE_MAIN_MENU}",
	"WINDOW_TITLE"		=> "{rs:EDIT_MAIN_SETTINGS}",
));
print $page->evaluate();
?>