function policy_domain_save()
{
    $q = new amavisdb();
    $sqladd = "INSERT IGNORE INTO `users` (policy_id,email,uid,fullname,local) VALUES\n\t('{$_POST["policy_id"]}','@{$_POST["policy_domain"]}','{$_SESSION["uid"]}','{$_SESSION["ou"]}',1);";
    $sqledit = "UPDATE `users` SET\n\tpolicy_id='{$_POST["policy_id"]}',\n\t\t\tfullname='{$_POST["{$user->DisplayName}"]}',\n\t\t\tWHERE id='{$_POST["email_id"]}'";
    if ($_POST["email_id"] == 0) {
        $q->QUERY_SQL($sqladd);
        if (!$q->ok) {
            echo $q->mysql_error;
        }
    } else {
        $q->QUERY_SQL($sqledit);
        if (!$q->ok) {
            echo $q->mysql_error . "\n{$sqledit}\n";
        }
    }
}
function wbl_search()
{
    $amavis = new amavisdb();
    $boot = new boostrap_form();
    $page = CurrentPageName();
    $tpl = new templates();
    $searchstring = string_to_flexquery("wbl-search");
    $user = new user($_SESSION["uid"]);
    $mails = $user->HASH_ALL_MAILS;
    while (list($b, $email) = each($mails)) {
        $rid = $amavis->emailid_from_email($email);
        $f[] = "(rid={$rid})";
    }
    if (!$amavis->TABLE_EXISTS("wblist")) {
        $sql = "CREATE TABLE IF NOT EXISTS wblist (rid integer unsigned NOT NULL,sid integer unsigned NOT NULL,wb varchar(10)  NOT NULL,PRIMARY KEY (rid,sid));";
        $amavis->QUERY_SQL($sql);
        if (!$q->ok) {
            echo "<p class=text-error>{$amavis->mysql_error}<hr><code>{$sql}</code></p>";
            return;
        }
    }
    $table = "(SELECT * FROM `wblist` WHERE (" . @implode(" OR ", $f) . ") ) as t";
    $table = "(SELECT `mailaddr`.email,`users`.email as rcpt, `t`.* FROM {$table},`mailaddr`,`wblist` ,`users`\n\tWHERE \n\t`mailaddr`.id=`t`.sid AND \n\t`users`.id=`t`.rid AND\n\tt.wb='{$_GET["type"]}') as y";
    $sql = "SELECT * FROM {$table} WHERE 1 {$searchstring} ORDER BY email";
    $results = $amavis->QUERY_SQL($sql);
    if (!$amavis->ok) {
        echo "<p class=text-error>{$amavis->mysql_error}<hr><code>{$sql}</code></p>";
    }
    while ($ligne = mysql_fetch_assoc($results)) {
        $jshost = "Loadjs('{$page}?wbl-js=yes&rid={$ligne["rid"]}&sid={$ligne["sid"]}&type={$_GET["type"]}');";
        $link = $boot->trswitch($jshost);
        $tr[] = "\n\t\t<tr id='{$id}'>\n\t\t<td {$link}><i class='icon-globe'></i> {$ligne["email"]}</a></td>\n\t\t<td {$link}><i class='icon-globe'></i> {$ligne["rcpt"]}</a></td>\n\t\t<td style='text-align:center'>{$delete}</td>\n\t\t</tr>";
    }
    echo $tpl->_ENGINE_parse_body("\n\t<table class='table table-bordered table-hover'><thead><tr>\n\t\t\t<th>{sender}</th>\n\t\t\t<th>{email}</th>\n\t\t\t\n\t\t\t<th>&nbsp;</th></tr></thead><tbody>") . @implode("\n", $tr) . " </tbody></table>";
}
function SearchQuery()
{
    $SearchQuery = string_to_flexquery();
    $q = new amavisdb();
    $page = CurrentPageName();
    $usersZ = new usersMenus();
    $tpl = new templates();
    $boot = new boostrap_form();
    $sql = "SELECT * FROM `policy` WHERE ou='{$_SESSION["ou"]}' {$SearchQuery} ORDER BY policy_name LIMIT 0,150 ";
    $results = $q->QUERY_SQL($sql);
    if (!$q->ok) {
        echo "<p class=text-error>{$q->mysql_error}<br>{$sql}<hr></p>";
    }
    $t = time();
    while ($ligne = mysql_fetch_assoc($results)) {
        $id = $ligne["id"];
        $policy_name = $ligne["policy_name"];
        $tr[] = "\n\t\t<tr id='{$id}' " . $boot->trswitch("Loadjs('{$page}?policy-id-js={$id}')") . ">\n\t\t<td width=30%><i class='icon-filter'></i> {$policy_name}</td>\n\t\t<td width=1%><i class='icon-eye-open'></i> {$ligne["spam_tag_level"]}</td>\n\t\t<td width=1%><i class='icon-fire'></i> {$ligne["spam_tag2_level"]}</td>\n\t\t<td width=1%><i class='icon-trash'></i> {$ligne["spam_kill_level"]}</td>\n\t\t<td style='text-align:center'>" . imgsimple("delete-32.png", null, "Delete{$t}('{$id}')") . "</td>\n\t\t</tr>\n\t\t";
    }
    $page = CurrentPageName();
    $ouF = null;
    $delete = $tpl->javascript_parse_text("{delete} {rule} ?");
    echo $tpl->_ENGINE_parse_body("<table class='table table-bordered table-hover'>\n\n\t\t\t<thead>\n\t\t\t<tr>\n\t\t\t<th>{policy}</th>\n\t\t\t<th width=1% nowrap>TAG {level}</th>\n\t\t\t<th width=1% nowrap>QUAR {level}</th>\n\t\t\t<th width=1% nowrap>KILL {level}</th>\n\t\t\t<th width=1% nowrap>&nbsp;</th>\n\t\t\t</tr>\n\t\t\t</thead>\n\t\t\t<tbody>\n\t\t\t") . @implode("\n", $tr) . " </tbody>\n\t\t\t</table>\n<script>\n\tvar meme{$t}='';\n\tvar xDelete{$t}= function (obj) {\n\t\t\tvar results=obj.responseText;\n\t\t\tif(results.length>3){alert(results);return;}\n\t\t\t\$('#'+meme{$t}).remove();\n\t}\n\n\n\tfunction Delete{$t}(xmd5){\n\t\tmeme{$t}=xmd5;\n\t\tif(!confirm('{$delete}')){return;}\n\t\t\tvar XHR = new XHRConnection();\n\t\t\tXHR.appendData('delete-md5',xmd5);\n\t\t\tXHR.sendAndLoad('{$page}', 'POST',xDelete{$t});\n\t}\n\t\t\t\t\t\t\n</script>";
}