From: Mikael Nordfeldth <mmn@hethane.se>
Date: Mon, 23 Sep 2013 09:34:15 +0000 (+0200)
Subject: FavorAction upgraded to extend FormAction
X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=5f1fea14886450178a2c60f363b4f0358504433f;p=quix0rs-gnu-social.git

FavorAction upgraded to extend FormAction

Includes some minor changes to other things as well, such as the session
token input element now having the same 'name' attribute as everyone else.
(it still retains a 'token-'+noticeid 'id' attribute for clientside JS)
---

diff --git a/actions/disfavor.php b/actions/disfavor.php
index aa4f59857d..40285be4cf 100644
--- a/actions/disfavor.php
+++ b/actions/disfavor.php
@@ -5,11 +5,12 @@
  * PHP version 5
  *
  * @category Action
- * @package  StatusNet
+ * @package  GNUSocial
  * @author   Evan Prodromou <evan@status.net>
  * @author   Robin Millette <millette@status.net>
+ * @author   Mikael Nordfeldth <mmn@hethane.se>
  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
- * @link     http://status.net/
+ * @link     http://www.gnu.org/software/social/
  *
  * StatusNet - the distributed open-source microblogging tool
  * Copyright (C) 2008, 2009, StatusNet, Inc.
@@ -28,70 +29,52 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-if (!defined('STATUSNET') && !defined('LACONICA')) {
-    exit(1);
-}
-
-require_once INSTALLDIR.'/lib/favorform.php';
+if (!defined('GNUSOCIAL')) { exit(1); }
 
 /**
- * Disfavor class.
+ * DisfavorAction class.
  *
  * @category Action
- * @package  StatusNet
+ * @package  GNUSocial
  * @author   Evan Prodromou <evan@status.net>
  * @author   Robin Millette <millette@status.net>
+ * @author   Mikael Nordfeldth <mmn@hethane.se>
  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
- * @link     http://status.net/
+ * @link     http://www.gnu.org/software/social/
  */
-class DisfavorAction extends Action
+class DisfavorAction extends FormAction
 {
-    /**
-     * Class handler.
-     *
-     * @param array $args query arguments
-     *
-     * @return void
-     */
-    function handle($args)
+    public function showForm($msg=null, $success=false)
     {
-        parent::handle($args);
-        if (!common_logged_in()) {
-            // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
-            $this->clientError(_('Not logged in.'));
-            return;
-        }
-        $user = common_current_user();
-        if ($_SERVER['REQUEST_METHOD'] != 'POST') {
+        if ($success) {
             common_redirect(common_local_url('showfavorites',
-                array('nickname' => $user->nickname)));
-            return;
+                array('nickname' => $this->scoped->nickname)), 303);
         }
+        parent::showForm($msg, $success);
+    }
+
+    protected function handlePost()
+    {
         $id     = $this->trimmed('notice');
         $notice = Notice::getKV($id);
-        $token  = $this->trimmed('token-'.$notice->id);
-        if (!$token || $token != common_session_token()) {
-            // TRANS: Client error displayed when the session token does not match or is not given.
-            $this->clientError(_('There was a problem with your session token. Try again, please.'));
-            return;
+        if (!$notice instanceof Notice) {
+            $this->serverError(_('Notice not found'));
         }
+
         $fave            = new Fave();
-        $fave->user_id   = $user->id;
+        $fave->user_id   = $this->scoped->id;
         $fave->notice_id = $notice->id;
         if (!$fave->find(true)) {
-            // TRANS: Client error displayed when trying to remove favorite status for a notice that is not a favorite.
-            $this->clientError(_('This notice is not a favorite!'));
-            return;
+            throw new NoResultException($fave);
         }
         $result = $fave->delete();
         if (!$result) {
             common_log_db_error($fave, 'DELETE', __FILE__);
             // TRANS: Server error displayed when removing a favorite from the database fails.
             $this->serverError(_('Could not delete favorite.'));
-            return;
         }
-        $user->blowFavesCache();
-        if ($this->boolean('ajax')) {
+        $this->scoped->blowFavesCache();
+        if (StatusNet::isAjax()) {
             $this->startHTML('text/xml;charset=utf-8');
             $this->elementStart('head');
             // TRANS: Title for page on which favorites can be added.
@@ -102,10 +85,7 @@ class DisfavorAction extends Action
             $favor->show();
             $this->elementEnd('body');
             $this->elementEnd('html');
-        } else {
-            common_redirect(common_local_url('showfavorites',
-                                             array('nickname' => $user->nickname)),
-                            303);
+            exit;
         }
     }
 }
diff --git a/actions/favor.php b/actions/favor.php
index 777fec5737..05c95c5794 100644
--- a/actions/favor.php
+++ b/actions/favor.php
@@ -5,11 +5,12 @@
  * PHP version 5
  *
  * @category Action
- * @package  StatusNet
+ * @package  GNUSocial
  * @author   Evan Prodromou <evan@status.net>
  * @author   Robin Millette <millette@status.net>
+ * @author   Mikael Nordfeldth <mmn@hethane.se>
  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
- * @link     http://status.net/
+ * @link     http://www.gnu.org/software/social/
  *
  * StatusNet - the distributed open-source microblogging tool
  * Copyright (C) 2008, 2009, StatusNet, Inc.
@@ -28,68 +29,53 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-if (!defined('STATUSNET') && !defined('LACONICA')) {
-    exit(1);
-}
+if (!defined('GNUSOCIAL')) { exit(1); }
 
 require_once INSTALLDIR.'/lib/mail.php';
-require_once INSTALLDIR.'/lib/disfavorform.php';
 
 /**
- * Favor class.
+ * FavorAction class.
  *
  * @category Action
- * @package  StatusNet
+ * @package  GNUSocial
  * @author   Evan Prodromou <evan@status.net>
  * @author   Robin Millette <millette@status.net>
+ * @author   Mikael Nordfeldth <mmn@hethane.se>
  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
- * @link     http://status.net/
+ * @link     http://www.gnu.org/software/social/
  */
-class FavorAction extends Action
+class FavorAction extends FormAction
 {
-    /**
-     * Class handler.
-     *
-     * @param array $args query arguments
-     *
-     * @return void
-     */
-    function handle($args)
+    // We overload this because success should redirect
+    public function showForm($msg=null, $success=false)
     {
-        parent::handle($args);
-        if (!common_logged_in()) {
-            // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
-            $this->clientError(_('Not logged in.'));
-            return;
-        }
-        $user = common_current_user();
-        if ($_SERVER['REQUEST_METHOD'] != 'POST') {
+        if ($success) {
             common_redirect(common_local_url('showfavorites',
-                array('nickname' => $user->nickname)));
-            return;
+                array('nickname' => $user->nickname)), 303);
         }
+
+        parent::showForm($msg, $success);
+    }
+
+    protected function handlePost()
+    {
         $id     = $this->trimmed('notice');
         $notice = Notice::getKV($id);
-        $token  = $this->trimmed('token-'.$notice->id);
-        if (!$token || $token != common_session_token()) {
-            // TRANS: Client error displayed when the session token does not match or is not given.
-            $this->clientError(_('There was a problem with your session token. Try again, please.'));
-            return;
+        if (!($notice instanceof Notice)) {
+            $this->serverError(_('Notice not found'));
         }
-        if ($user->hasFave($notice)) {
+        if ($this->scoped->hasFave($notice)) {
             // TRANS: Client error displayed when trying to mark a notice as favorite that already is a favorite.
             $this->clientError(_('This notice is already a favorite!'));
-            return;
         }
-        $fave = Fave::addNew($user->getProfile(), $notice);
+        $fave = Fave::addNew($this->scoped, $notice);
         if (!$fave) {
             // TRANS: Server error displayed when trying to mark a notice as favorite fails in the database.
             $this->serverError(_('Could not create favorite.'));
-            return;
         }
-        $this->notify($notice, $user);
-        $user->blowFavesCache();
-        if ($this->boolean('ajax')) {
+        $this->notify($notice, $this->scoped->getUser());
+        $this->scoped->blowFavesCache();
+        if (StatusNet::isAjax()) {
             $this->startHTML('text/xml;charset=utf-8');
             $this->elementStart('head');
             // TRANS: Page title for page on which favorite notices can be unfavourited.
@@ -100,11 +86,11 @@ class FavorAction extends Action
             $disfavor->show();
             $this->elementEnd('body');
             $this->elementEnd('html');
-        } else {
-            common_redirect(common_local_url('showfavorites',
-                                             array('nickname' => $user->nickname)),
-                            303);
+            exit;
         }
+        common_redirect(common_local_url('showfavorites',
+                                         array('nickname' => $this->scoped->nickname)),
+                            303);
     }
 
     /**
diff --git a/lib/disfavorform.php b/lib/disfavorform.php
index 9754dfc83b..2a7e9ff9eb 100644
--- a/lib/disfavorform.php
+++ b/lib/disfavorform.php
@@ -20,29 +20,27 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  * @category  Form
- * @package   StatusNet
+ * @package   GNUSocial
  * @author    Evan Prodromou <evan@status.net>
  * @author    Sarven Capadisli <csarven@status.net>
+ * @author    Mikael Nordfeldth <mmn@hethane.se>
  * @copyright 2009 StatusNet, Inc.
  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
- * @link      http://status.net/
+ * @link      http://www.gnu.org/software/social/
  */
 
-if (!defined('STATUSNET') && !defined('LACONICA')) {
-    exit(1);
-}
-
-require_once INSTALLDIR.'/lib/form.php';
+if (!defined('GNUSOCIAL')) { exit(1); }
 
 /**
  * Form for disfavoring a notice
  *
  * @category Form
- * @package  StatusNet
+ * @package  GNUSocial
  * @author   Evan Prodromou <evan@status.net>
  * @author   Sarven Capadisli <csarven@status.net>
+ * @author   Mikael Nordfeldth <mmn@hethane.se>
  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
- * @link     http://status.net/
+ * @link     http://www.gnu.org/software/social/
  *
  * @see      FavorForm
  */
@@ -94,7 +92,8 @@ class DisfavorForm extends Form
     function sessionToken()
     {
         $this->out->hidden('token-' . $this->notice->id,
-                           common_session_token());
+                           common_session_token(),
+                           'token');
     }
 
     /**
diff --git a/lib/favorform.php b/lib/favorform.php
index eab5ba6e9c..46e19476a8 100644
--- a/lib/favorform.php
+++ b/lib/favorform.php
@@ -20,29 +20,27 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  * @category  Form
- * @package   StatusNet
+ * @package   GNUSocial
  * @author    Evan Prodromou <evan@status.net>
  * @author    Sarven Capadisli <csarven@status.net>
+ * @author    Mikael Nordfeldth <mmn@hethane.se>
  * @copyright 2009 StatusNet, Inc.
  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
- * @link      http://status.net/
+ * @link      http://www.gnu.org/software/social/
  */
 
-if (!defined('STATUSNET') && !defined('LACONICA')) {
-    exit(1);
-}
-
-require_once INSTALLDIR.'/lib/form.php';
+if (!defined('GNUSOCIAL')) { exit(1); }
 
 /**
  * Form for favoring a notice
  *
  * @category Form
- * @package  StatusNet
+ * @package  GNUSocial
  * @author   Evan Prodromou <evan@status.net>
  * @author   Sarven Capadisli <csarven@status.net>
+ * @author   Mikael Nordfeldth <mmn@hethane.se>
  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
- * @link     http://status.net/
+ * @link     http://www.gnu.org/software/social/
  *
  * @see      DisfavorForm
  */
@@ -94,7 +92,8 @@ class FavorForm extends Form
     function sessionToken()
     {
         $this->out->hidden('token-' . $this->notice->id,
-                           common_session_token());
+                           common_session_token(),
+                           'token');
     }
 
     /**
diff --git a/plugins/AnonymousFave/actions/anondisfavor.php b/plugins/AnonymousFave/actions/anondisfavor.php
index e5ae09679d..0060b7fa50 100644
--- a/plugins/AnonymousFave/actions/anondisfavor.php
+++ b/plugins/AnonymousFave/actions/anondisfavor.php
@@ -27,9 +27,7 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-if (!defined('STATUSNET')) {
-    exit(1);
-}
+if (!defined('GNUSOCIAL')) { exit(1); }
 
 /**
  * Anonymous disfavor class
@@ -65,22 +63,14 @@ class AnonDisfavorAction extends RedirectingAction
 
         $id     = $this->trimmed('notice');
         $notice = Notice::getKV($id);
-        $token  = $this->trimmed('token-' . $notice->id);
-
-        if (!$token || $token != common_session_token()) {
-            // TRANS: Client error.
-            $this->clientError(_m('There was a problem with your session token. Try again, please.'));
-            return;
-        }
+        $token  = $this->checkSessionToken();
 
         $fave            = new Fave();
         $fave->user_id   = $profile->id;
         $fave->notice_id = $notice->id;
 
         if (!$fave->find(true)) {
-            // TRANS: Client error.
-            $this->clientError(_m('This notice is not a favorite!'));
-            return;
+            throw new NoResultException($fave);
         }
 
         $result = $fave->delete();
diff --git a/plugins/AnonymousFave/actions/anonfavor.php b/plugins/AnonymousFave/actions/anonfavor.php
index 401b6a855b..61868125c1 100644
--- a/plugins/AnonymousFave/actions/anonfavor.php
+++ b/plugins/AnonymousFave/actions/anonfavor.php
@@ -27,9 +27,7 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-if (!defined('STATUSNET')) {
-    exit(1);
-}
+if (!defined('GNUSOCIAL')) { exit(1); }
 
 /**
  * Anonymous favor class
@@ -64,14 +62,7 @@ class AnonFavorAction extends RedirectingAction
 
         $id     = $this->trimmed('notice');
         $notice = Notice::getKV($id);
-        $token  = $this->trimmed('token-' . $notice->id);
-
-        if (empty($token) || $token != common_session_token()) {
-            // TRANS: Client error.
-            $this->clientError(_m('There was a problem with your session token. Try again, please.'));
-            return;
-        }
-
+        $token  = $this->checkSessionToken();
 
         if ($profile->hasFave($notice)) {
             // TRANS: Client error.
diff --git a/plugins/Realtime/realtimeupdate.js b/plugins/Realtime/realtimeupdate.js
index e044f2f916..90d0a05b09 100644
--- a/plugins/Realtime/realtimeupdate.js
+++ b/plugins/Realtime/realtimeupdate.js
@@ -301,7 +301,7 @@ RealtimeUpdate = {
           ff = "<form id=\"favor-"+id+"\" class=\"form_favor\" method=\"post\" action=\""+RealtimeUpdate._favorurl+"\">"+
                 "<fieldset>"+
                "<legend>Favor this notice</legend>"+
-               "<input name=\"token-"+id+"\" type=\"hidden\" id=\"token-"+id+"\" value=\""+session_key+"\"/>"+
+               "<input name=\"token\" type=\"hidden\" id=\"token-"+id+"\" value=\""+session_key+"\"/>"+
                "<input name=\"notice\" type=\"hidden\" id=\"notice-n"+id+"\" value=\""+id+"\"/>"+
                "<input type=\"submit\" id=\"favor-submit-"+id+"\" name=\"favor-submit-"+id+"\" class=\"submit\" value=\"Favor\" title=\"Favor this notice\"/>"+
                 "</fieldset>"+
@@ -348,7 +348,7 @@ RealtimeUpdate = {
           rf = "<form id=\"repeat-"+id+"\" class=\"form_repeat\" method=\"post\" action=\""+RealtimeUpdate._repeaturl+"\">"+
                "<fieldset>"+
                "<legend>Repeat this notice?</legend>"+
-               "<input name=\"token-"+id+"\" type=\"hidden\" id=\"token-"+id+"\" value=\""+session_key+"\"/>"+
+               "<input name=\"token\" type=\"hidden\" id=\"token-"+id+"\" value=\""+session_key+"\"/>"+
                "<input name=\"notice\" type=\"hidden\" id=\"notice-"+id+"\" value=\""+id+"\"/>"+
                "<input type=\"submit\" id=\"repeat-submit-"+id+"\" name=\"repeat-submit-"+id+"\" class=\"submit\" value=\"Yes\" title=\"Repeat this notice\"/>"+
                "</fieldset>"+