]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
FavorAction upgraded to extend FormAction
authorMikael Nordfeldth <mmn@hethane.se>
Mon, 23 Sep 2013 09:34:15 +0000 (11:34 +0200)
committerMikael Nordfeldth <mmn@hethane.se>
Mon, 23 Sep 2013 11:06:09 +0000 (13:06 +0200)
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)

actions/disfavor.php
actions/favor.php
lib/disfavorform.php
lib/favorform.php
plugins/AnonymousFave/actions/anondisfavor.php
plugins/AnonymousFave/actions/anonfavor.php
plugins/Realtime/realtimeupdate.js

index aa4f59857d792282fb76b300e5c9ea1e17f22a2a..40285be4cf3f9cd5da3a2d200aa881a4bf6683ea 100644 (file)
@@ -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.
  * 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;
         }
     }
 }
index 777fec57378456bc7bf44b6c414e3d444fe17729..05c95c5794b373bdd099b5162d4817dccd089233 100644 (file)
@@ -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.
  * 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);
     }
 
     /**
index 9754dfc83b8894da4bfc3a0924eac947a41de828..2a7e9ff9eb1279d67af48665555f4aeb84336875 100644 (file)
  * 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');
     }
 
     /**
index eab5ba6e9c84847fda0e31202bcfe4219547d822..46e19476a8404d2286aa514217fe824e7294fca7 100644 (file)
  * 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');
     }
 
     /**
index e5ae09679dfc2d50f4598ac5a73fd1258fa068ad..0060b7fa507df5f5cdd34826534c56df64345d96 100644 (file)
@@ -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();
index 401b6a855b1a07eb0a912d96ff1e3866fe7657bf..61868125c12f9b4478d743dfb6221016b33da67a 100644 (file)
@@ -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.
index e044f2f916c0fbe1e8e3bb9b1c37581d97093941..90d0a05b09f9fc48babcf89fcf12a938e0663651 100644 (file)
@@ -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>"+