]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/AnonymousFave/anondisfavor.php
Initial plugin for allowing anonymous favoriting
[quix0rs-gnu-social.git] / plugins / AnonymousFave / anondisfavor.php
1 <?php
2
3 /**
4  * Anonymous disfavor action
5  *
6  * PHP version 5
7  *
8  * @category Action
9  * @package  StatusNet
10  * @author   Zach Copley <zach@status.net>
11  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
12  * @link     http://status.net/
13  *
14  * StatusNet - the distributed open-source microblogging tool
15  * Copyright (C) 2010, StatusNet, Inc.
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU Affero General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU Affero General Public License for more details.
26  *
27  * You should have received a copy of the GNU Affero General Public License
28  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29  */
30
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 /**
36  * Anonymous disfavor class
37  *
38  * @category Action
39  * @package  StatusNet
40  * @author   Zach Copley <zach@status.net>
41  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
42  * @link     http://status.net/
43  */
44 class AnonDisfavorAction extends RedirectingAction
45 {
46     /**
47      * Class handler.
48      *
49      * @param array $args query arguments
50      *
51      * @return void
52      */
53     function handle($args)
54     {
55         parent::handle($args);
56
57         $anon = $_SESSION['anon_nickname'];
58
59         $profile = Profile::staticGet('nickname', $anon);
60
61         if (empty($profile)) {
62             common_debug(
63                 "AnonDisFavorAction - Anon user tried to disfave a notice but doesn't have a profile."
64             );
65         }
66
67         if (empty($profile) || $_SERVER['REQUEST_METHOD'] != 'POST') {
68             $this->clientError(
69                 _m('Could not disfavor notice! Please make sure your browser has cookies enabled.')
70             );
71             return;
72         }
73
74         $id     = $this->trimmed('notice');
75         $notice = Notice::staticGet($id);
76         $token  = $this->trimmed('token-' . $notice->id);
77
78         if (!$token || $token != common_session_token()) {
79             $this->clientError(_m('There was a problem with your session token. Try again, please.'));
80             return;
81         }
82
83         $fave            = new Fave();
84         $fave->user_id   = $profile->id;
85         $fave->notice_id = $notice->id;
86
87         if (!$fave->find(true)) {
88             $this->clientError(_m('This notice is not a favorite!'));
89             return;
90         }
91
92         $result = $fave->delete();
93
94         if (!$result) {
95             common_log_db_error($fave, 'DELETE', __FILE__);
96             $this->serverError(_m('Could not delete favorite.'));
97             return;
98         }
99
100         $profile->blowFavesCache();
101
102         if ($this->boolean('ajax')) {
103             $this->startHTML('text/xml;charset=utf-8');
104             $this->elementStart('head');
105             $this->element('title', null, _m('Add to favorites'));
106             $this->elementEnd('head');
107             $this->elementStart('body');
108             $favor = new AnonFavorForm($this, $notice);
109             $favor->show();
110             $this->elementEnd('body');
111             $this->elementEnd('html');
112         } else {
113             $this->returnToPrevious();
114         }
115     }
116
117     /**
118      * If returnto not set, return to the public stream.
119      *
120      * @return string URL
121      */
122     function defaultReturnTo()
123     {
124         $returnto = common_get_returnto();
125         if (empty($returnto)) {
126             return common_local_url('public');
127         } else {
128             return $returnto;
129         }
130     }
131 }
132