Allows antispam level choice for each redirection.
authorStéphane Jacob <sj@m4x.org>
Thu, 3 Mar 2011 18:15:52 +0000 (19:15 +0100)
committerStéphane Jacob <sj@m4x.org>
Thu, 3 Mar 2011 18:15:52 +0000 (19:15 +0100)
Signed-off-by: Stéphane Jacob <sj@m4x.org>
ChangeLog
configs/platal.ini
include/emails.inc.php
modules/email.php
templates/emails/antispam.tpl

index 51a1b29..e697fe5 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -7,6 +7,9 @@ Bug/Wish:
         - #1399: Displays all similar entreprises only on demand           -JAC
         - #1403: Displays grade if any in medal validation process         -JAC
 
+    * Emails:
+        - Allows antispam level choice for each redirection                -JAC
+
     * Payments:
         - #1398: Creates csv for payments                                  -JAC
 
index 458ef35..c7e159e 100644 (file)
@@ -288,6 +288,9 @@ blacklist_host_resolution_limit =
 ; domain is blacklisted by a spam filter.
 domain_whitelist = ""
 
+; $globals->mail->antispam
+; Default antispam level.
+antispam = ""
 
 ; The mailstorage section contains parameters describing the availability of email storage services
 [MailStorage]
index a48da50..c09a7b4 100644 (file)
@@ -296,10 +296,19 @@ function ids_from_mails(array $emails)
 // The Bogo class represents a spam filtering level in plat/al architecture.
 class Bogo
 {
-    private static $states = array('let_spams', 'tag_spams', 'tag_and_drop_spams', 'drop_spams');
+    private static $states = array(
+        0 => 'default',
+        1 => 'let_spams',
+        2 => 'tag_spams',
+        3 => 'tag_and_drop_spams',
+        4 => 'drop_spams'
+    );
 
     private $user;
-    private $state;
+    public $state;
+    public $single_state;
+    public $redirections;
+    public $single_redirection;
 
     public function __construct(User $user)
     {
@@ -308,28 +317,51 @@ class Bogo
         }
 
         $this->user = &$user;
-        $res = XDB::query('SELECT  action
-                             FROM  email_redirect_account
-                            WHERE  uid = {?} AND (type = \'smtp\' OR type = \'googleapps\')',
-                          $user->id());
-        if ($res->numRows() == 0) {
+        $res = XDB::fetchOneAssoc('SELECT  COUNT(DISTINCT(action)) AS action_count, COUNT(redirect) AS redirect_count, action
+                                     FROM  email_redirect_account
+                                    WHERE  uid = {?} AND (type = \'smtp\' OR type = \'googleapps\') AND flags = \'active\'',
+                                  $user->id());
+        if ($res['redirect_count'] == 0) {
             return;
         }
-        $this->state = $res->fetchOneCell();
+
+        $this->single_redirection = ($res['redirect_count'] == 1);
+        $this->redirections = XDB::fetchAllAssoc('SELECT  IF(type = \'googleapps\', type, redirect) AS redirect, type, action
+                                                    FROM  email_redirect_account
+                                                   WHERE  uid = {?} AND (type = \'smtp\' OR type = \'googleapps\')
+                                                ORDER BY  type, redirect',
+                                                 $user->id());
+
+        foreach ($this->redirections AS &$redirection) {
+            $redirection['filter'] = array_search($redirection['action'], self::$states);
+        }
+        if ($res['action_count'] == 1) {
+            $this->state = array_search($res['action'], self::$states);
+            $this->single_state = true;
+        } else {
+            $this->single_state = $this->state = false;
+        }
     }
 
-    public function change($state)
+    public function changeAll($state)
     {
-        $this->state = is_int($state) ? self::$states[$state] : $state;
+        Platal::assert($state >= 0 && $state < count(self::$states), 'Unknown antispam level.');
+
+        $this->state = $state;
         XDB::execute('UPDATE  email_redirect_account
                          SET  action = {?}
                        WHERE  uid = {?} AND (type = \'smtp\' OR type = \'googleapps\')',
-                     $this->state, $this->user->id());
+                     self::$states[$this->state], $this->user->id());
     }
 
-    public function level()
+    public function change($redirection, $state)
     {
-        return array_search($this->state, self::$states);
+        Platal::assert($state >= 0 && $state < count(self::$states), 'Unknown antispam level.');
+
+        XDB::execute('UPDATE  email_redirect_account
+                         SET  action = {?}
+                       WHERE  uid = {?} AND (type = {?} OR redirect = {?})',
+                     self::$states[$state], $this->user->id(), $redirection, $redirection);
     }
 }
 
index d70da2c..e080e48 100644 (file)
@@ -298,7 +298,7 @@ class EmailModule extends PLModule
         fill_email_combobox($page);
     }
 
-    function handler_antispam($page, $filter_status = null)
+    function handler_antispam($page, $filter_status = null, $redirection = null)
     {
         require_once 'emails.inc.php';
         $wp = new PlWikiPage('Xorg.Antispam');
@@ -308,10 +308,17 @@ class EmailModule extends PLModule
 
         $user = S::user();
         $bogo = new Bogo($user);
-        if (isset($filter_status)) {
-            $bogo->change($filter_status + 0);
+        if (!is_null($filter_status)) {
+            if (is_null($redirection)) {
+                $bogo->changeAll($filter_status);
+            } else {
+                $bogo->change($redirection, $filter_status);
+            }
         }
-        $page->assign('filter', $bogo->level());
+        $page->assign('filter', $bogo->state);
+        $page->assign('single_state', $bogo->single_state);
+        $page->assign('single_redirection', $bogo->single_redirection);
+        $page->assign('redirections', $bogo->redirections);
     }
 
     function handler_submit($page)
index e2af941..0c5e38b 100644 (file)
   {literal}
   $(function() {
       var url = '{/literal}{$globals->baseurl}/emails/antispam/{literal}';
-      var msg = "Le changement de réglage de l'antispam a bien été effectué.";
+      var msg = "Le changement de réglage de l'antispam a bien été effectué pour toutes tes redirections.";
       $(':radio[name=filter_status]').change(function() {
-          $("#bogo-msg").successMessage(url + $(this).val(), msg);
+          var val = $(this).val();
+          $(':radio[name*=filter_status_]').removeAttr('checked');
+          $(':radio[name*=filter_status_]').attr('checked', function(i, v) {
+              if ($(this).val() == val) {
+                  return 'checked';
+              }
+          });
+          $("#bogo-msg").successMessage(url + val, msg);
+      });
+  });
+  $(function() {
+      var url = '{/literal}{$globals->baseurl}/emails/antispam/{literal}';
+      var msg = "Le changement de réglage de l'antispam a bien été effectué pour ";
+      $(':radio[name*=filter_status_]').change(function() {
+          var id = $(this).attr('name').replace('filter_status_', '');
+          var redirection = $('#bogo_' + id).val();
+          $(':radio[name=filter_status]').removeAttr('checked');
+          $("#bogo-msg-mult").successMessage(url + $(this).val() + "/" + redirection, msg + redirection.replace('googleapps', 'ton compte Google Apps') + ".");
       });
   });
   {/literal}
 //]]></script>
-  <fieldset>
-    <legend><strong>Choisis ton propre réglage&nbsp;:</strong></legend>
-    <input id='s0' type='radio' name='filter_status' value='0' {if $filter eq 0}checked="checked"{/if} />
-    <label for='s0'>(1) le filtre anti-spam n'agit pas sur tes emails</label>
-    <br />
-    <input id='s1' type='radio' name='filter_status' value='1' {if $filter eq 1}checked="checked"{/if} />
-    <label for='s1'>(2) le filtre anti-spam marque les emails</label>
-    <br />
-    <input id='s2' type='radio' name='filter_status' value='2' {if $filter eq 2}checked="checked"{/if} />
-    <label for='s2'>(3) le filtre anti-spam marque les emails, et élimine les spams avec des notes les plus hautes</label>
-    <br />
-    <input id='s3' type='radio' name='filter_status' value='3' {if $filter eq 3}checked="checked"{/if} />
-    <label for='s3'>(4) le filtre anti-spam élimine les emails détectés comme spams</label>
-  </fieldset>
-
-  <div id="bogo-msg" style="position:absolute;"></div><br />
+<fieldset>
+  <legend><strong>Choisis ton propre réglage&nbsp;:</strong></legend>
+  {if !$single_state}<span class="erreur">
+    Attention, tu as actuellement un réglage spécifique pour chacune de tes redirections.
+    Les modifications dans ce cadre sont globales et entraineront une uniformisation de
+    l'antispam pour toutes tes redirections au niveau demandé.
+  </span><br />{/if}
+  <input id="s0" type="radio" name="filter_status" value="0" {if $single_state && $filter eq 0}checked="checked"{/if} />
+  <label for="s0"><strong>(0) fais confiance à Polytechnique.org et utilise le réglage préconisé par défaut</strong>
+  (actuellement, le niveau {#globals.mail.antispam#})</label>
+  <br />
+  <input id="s1" type="radio" name="filter_status" value="1" {if $single_state && $filter eq 1}checked="checked"{/if} />
+  <label for="s1">(1) le filtre anti-spam n'agit pas sur tes emails</label>
+  <br />
+  <input id="s2" type="radio" name="filter_status" value="2" {if $single_state && $filter eq 2}checked="checked"{/if} />
+  <label for="s2">(2) le filtre anti-spam marque les emails</label>
+  <br />
+  <input id="s3" type="radio" name="filter_status" value="3" {if $single_state && $filter eq 3}checked="checked"{/if} />
+  <label for="s3">(3) le filtre anti-spam marque les emails, et élimine les spams avec des notes les plus hautes</label>
+  <br />
+  <input id="s4" type="radio" name="filter_status" value="4" {if $single_state && $filter eq 4}checked="checked"{/if} />
+  <label for="s4">(4) le filtre anti-spam élimine les emails détectés comme spams</label>
+</fieldset>
+
+<div id="bogo-msg" style="position:absolute;"></div><br />
+
+{if !$single_redirection}
+<h1>Réglages avancés</h1>
+<p>
+  Si tu le souhaites, tu peux adapter le niveau de ton antispam pour chacune de tes redirections. Par exemple,
+  tu peux éliminer tous les spams (niveau 4) vers ton adresse professionnelle, mais ne faire que marquer comme
+  spams (niveau 2) de tels emails vers ton adresse personnelle.
+</p>
+
+<div id="bogo-msg-mult" style="position:absolute;"></div><br />
+
+{foreach from=$redirections key=i item=redirection}
+<fieldset>
+  <legend><strong>{$redirection.redirect|replace:'googleapps':'Compte Google Apps'}&nbsp;:</strong></legend>
+  <input id="bogo_{$i}" type="hidden" value="{$redirection.redirect}" />
+  <input id="s0_{$i}" type="radio" name="filter_status_{$i}" value="0" {if $redirection.filter eq 0}checked="checked"{/if} />
+  <label for="s0_{$i}"><strong>(0) fais confiance à Polytechnique.org et utilise le réglage préconisé par défaut</strong>
+  (actuellement, le niveau {#globals.mail.antispam#})</label>
+  <br />
+  <input id="s1_{$i}" type="radio" name="filter_status_{$i}" value="1" {if $redirection.filter eq 1}checked="checked"{/if} />
+  <label for="s1_{$i}">(1) le filtre anti-spam n'agit pas sur tes emails</label>
+  <br />
+  <input id="s2_{$i}" type="radio" name="filter_status_{$i}" value="2" {if $redirection.filter eq 2}checked="checked"{/if} />
+  <label for="s2_{$i}">(2) le filtre anti-spam marque les emails</label>
+  <br />
+  <input id="s3_{$i}" type="radio" name="filter_status_{$i}" value="3" {if $redirection.filter eq 3}checked="checked"{/if} />
+  <label for="s3_{$i}">(3) le filtre anti-spam marque les emails, et élimine les spams avec des notes les plus hautes</label>
+  <br />
+  <input id="s4_{$i}" type="radio" name="filter_status_{$i}" value="4" {if $redirection.filter eq 4}checked="checked"{/if} />
+  <label for="s4_{$i}">(4) le filtre anti-spam élimine les emails détectés comme spams</label>
+</fieldset>
+{/foreach}
+{/if}
 
 {include wiki=Xorg.Antispam part=2}