<?php
include_once "../../../tools/inc.php";
include "../../common.php";
include("../tools/function.string.php");
include("../tools/function.sql.php");

$n=$settings["rec_count"];
$max=100;
$table			= "subscribers".$config["table_prefix"];
$table_group		= "mailing_groups".$config["table_prefix"];
$table_black_list	= "black_list".$config["table_prefix"];
$table_sg		= "subscribers_group".$config["table_prefix"];
$table_cf		= "custom_fields".$config["table_prefix"];
$table_scf		= "scf".$config["table_prefix"];
$access=check_level("Export");

function get_groups($id=0) {
        global $db, $table_sg, $table_group, $access, $ui, $groups_list;
        $res="";
        $sql="select g.title from $table_sg sg, $table_group g where
                sg.subscribers_id='$id' and
                sg.group_id=g.id".($groups_list ? " and g.id in ($groups_list)" : "")."
        order by g.title";
        $rs=$db->select($sql);
        for ($i=0; $i<count($rs); $i++) $res.=($res ? "|" : "").$rs[$i]["title"];
        return $res;
}

if (isset($access["view"]) or isset($access["admin"])) {
        if (isset($l)) {
                if ($export["all"]) {
                        $sql="select * from $table ".(!isset($access["admin"]) ? "where owner_id='".$ui["id"]."'" : "")." order by name";
                } elseif (is_array($export["group_id"])) {
                        $sql="select s.*
                        from
                                $table s,
                                $table_group g,
                                $table_sg sg
                        where
                                s.id=sg.subscribers_id and
                                g.id=sg.group_id and
                                g.id in (".implode(", ", $export["group_id"]).")
                                ".($groups_list ? " and g.id in ($groups_list)" : "")."
                        group by s.id
                        order by s.name";
                } elseif (is_array($export["black_list_id"])) {
                        $sql="select rule from $table_black_list where id in (".implode(", ", $export["black_list_id"]).")";
                        if (!isset($access["admin"])) $sql.=" and owner_id='".$ui["id"]."'";
                        $rs=$db->select($sql);
                        $where="";
                        for ($i=0; $i<count($rs); $i++) $where.=($where ? " or " : "")."mail like '".str_replace("*", "%", $rs[$i]["rule"])."'";
                        $where=($where ? "where $where" : "");
                        if (!isset($access["admin"])) $where.=($where ? " and " : "where ")."owner_id='".$ui["id"]."'";
                        $sql="select * from $table $where order by name";
                } else {
                        $sql="select * from $table where
                                status='".$export["type"]."'
                                ".(!isset($access["admin"]) ? " and owner_id='".$ui["id"]."'" : "")."
                        order by name";
                }

		$cf_array=$db->get_array($table_cf, 'id', 'title', "where enabled='1' and owner_id='".$ui["id"]."' order by position");

                include "../tools/class.csv.php";
                $csv=new CSV($export["delimiter"], $export["enclosure"], "print", $fields);
                $csv->open('../files/export/'.$export['filename'], ($l ? 'a' : 'w'));
                if ($export["put_header"] and !$l) {
                        $csv->add_to_row("Name");
                        $csv->add_to_row("e-mail");
                        $csv->add_to_row("Receive Format");
                        $csv->add_to_row("Encoding");
                        $csv->add_to_row("Status");
                        $csv->add_to_row("Mail Group");
			foreach ($cf_array as $val) $csv->add_to_row($val);
                        $csv->save_row();
                }
                $sth=$db->execute($sql.' limit '.($l*$max).", $max");
                while ($rs=$db->get_row($sth)) {
                        $csv->add_to_row(trim($rs["name"]));
                        $csv->add_to_row(trim($rs["mail"]));
                        $csv->add_to_row(trim(($rs["receive_format"] ? "text" : "html")));
                        $csv->add_to_row(trim($rs["encoding"]));
                        $csv->add_to_row(trim($rs["status"]));
                        $csv->add_to_row(get_groups($rs["id"]));
			foreach ($cf_array as $key => $val) $csv->add_to_row($db->get_val($table_scf, 'value', "where custom_field_id='".$key."' and subscriber_id='".$rs["id"]."'"));
                        $csv->save_row();
                }
                $csv->close();
        }
}
print $PATH.'subscribers/export/?step=2';
$db->close();
exit;
?>