Improves the XSRF protection by reducing it to a single assert on the existence of...
[platal.git] / modules / carnet.php
index 194d866..cf1cd16 100644 (file)
@@ -127,9 +127,8 @@ class CarnetModule extends PLModule
         $promo_sortie = $res->fetchOneCell();
         $page->assign('promo_sortie', $promo_sortie);
 
-        if ($action && !S::has_xsrf_token()) {
-            $page->trig("La mise à jour des notifications a échouée, merci de réessayer.");
-            $action = false;
+        if ($action) {
+            S::assert_xsrf_token();
         }
         switch ($action) {
           case 'add_promo':
@@ -146,24 +145,21 @@ class CarnetModule extends PLModule
             break;
         }
 
-        if (Env::has('subs') && S::has_xsrf_token()) {
+        if (Env::has('subs')) {
+            S::assert_xsrf_token();
             $watch->_subs->update('sub');
-        } elseif (Env::has('subs')) {
-            $page->trig("La mise à jour des notifications a échouée, merci de réessayer.");
         }
 
-        if (Env::has('flags_contacts') && S::has_xsrf_token()) {
+        if (Env::has('flags_contacts')) {
+            S::assert_xsrf_token();
             $watch->watch_contacts = Env::b('contacts');
             $watch->saveFlags();
-        } elseif (Env::has('flags_contacts')) {
-            $page->trig("La mise à jour des notifications a échouée, merci de réessayer.");
         }
 
-        if (Env::has('flags_mail') && S::has_xsrf_token()) {
+        if (Env::has('flags_mail')) {
+            S::assert_xsrf_token();
             $watch->watch_mail = Env::b('mail');
             $watch->saveFlags();
-        } elseif (Env::has('flags_mail')) {
-            $page->trig("La mise à jour des notifications a échouée, merci de réessayer.");
         }
 
         $page->assign_by_ref('watch', $watch);
@@ -214,9 +210,11 @@ class CarnetModule extends PLModule
 
         // For XSRF protection, checks both the normal xsrf token, and the special RSS token.
         // It allows direct linking to contact adding in the RSS feed.
-        if (Env::v('action') && (S::has_xsrf_token() || Env::v('token') === S::v('core_rss_hash'))) {
-            switch (Env::v('action')) {
-              case 'retirer':
+        if (Env::v('action') && Env::v('token') !== S::v('core_rss_hash')) {
+            S::assert_xsrf_token();
+        }
+        switch (Env::v('action')) {
+            case 'retirer':
                 if (is_numeric($user)) {
                     if (XDB::execute('DELETE FROM contacts
                                        WHERE uid = {?} AND contact = {?}',
@@ -236,7 +234,7 @@ class CarnetModule extends PLModule
                 }
                 break;
 
-              case 'ajouter':
+            case 'ajouter':
                 require_once('user.func.inc.php');
                 if (($login = get_user_login($user)) !== false) {
                     if (XDB::execute(
@@ -250,9 +248,6 @@ class CarnetModule extends PLModule
                         $page->trig('Contact déjà dans la liste !');
                     }
                 }
-            }
-        } elseif (Env::v('action')) {
-            $page->trig("La modification du contact a échouée, merci de réessayer.");
         }
 
         $search = false;