]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Favorite/actions/atompubshowfavorite.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / plugins / Favorite / actions / atompubshowfavorite.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * Show a single favorite in Atom Activity Streams format
7  *
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  AtomPub
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2010 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 /**
38  * Show a single favorite in Atom Activity Streams format.
39  *
40  * Can also be used to delete a favorite.
41  *
42  * @category  Action
43  * @package   StatusNet
44  * @author    Evan Prodromou <evan@status.net>
45  * @copyright 2010 StatusNet, Inc.
46  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
47  * @link      http://status.net/
48  */
49 class AtompubshowfavoriteAction extends ApiAuthAction
50 {
51     private $_profile = null;
52     private $_notice  = null;
53     private $_fave    = null;
54
55     /**
56      * For initializing members of the class.
57      *
58      * @param array $args misc. arguments
59      *
60      * @return boolean true
61      */
62     protected function prepare(array $args=array())
63     {
64         parent::prepare($args);
65
66         $profileId = $this->trimmed('profile');
67         $noticeId  = $this->trimmed('notice');
68
69         $this->_profile = Profile::getKV('id', $profileId);
70
71         if (empty($this->_profile)) {
72             // TRANS: Client exception.
73             throw new ClientException(_('No such profile.'), 404);
74         }
75
76         $this->_notice = Notice::getKV('id', $noticeId);
77
78         if (empty($this->_notice)) {
79             // TRANS: Client exception thrown when referencing a non-existing notice.
80             throw new ClientException(_('No such notice.'), 404);
81         }
82
83         $this->_fave = Fave::pkeyGet(array('user_id' => $profileId,
84                                            'notice_id' => $noticeId));
85
86         if (empty($this->_fave)) {
87             // TRANS: Client exception thrown when referencing a non-existing favorite.
88             throw new ClientException(_('No such favorite.'), 404);
89         }
90
91         return true;
92     }
93
94     /**
95      * Handler method
96      *
97      * @return void
98      */
99     protected function handle()
100     {
101         parent::handle();
102
103         switch ($_SERVER['REQUEST_METHOD']) {
104         case 'GET':
105         case 'HEAD':
106             $this->showFave();
107             break;
108         case 'DELETE':
109             $this->deleteFave();
110             break;
111         default:
112             // TRANS: Client exception thrown using an unsupported HTTP method.
113             throw new ClientException(_('HTTP method not supported.'), 405);
114         }
115         return true;
116     }
117
118     /**
119      * Show a single favorite, in ActivityStreams format
120      *
121      * @return void
122      */
123     function showFave()
124     {
125         $activity = $this->_fave->asActivity();
126
127         header('Content-Type: application/atom+xml; charset=utf-8');
128
129         $this->startXML();
130         $this->raw($activity->asString(true, true, true));
131         $this->endXML();
132
133         return;
134     }
135
136     /**
137      * Delete the favorite
138      *
139      * @return void
140      */
141     function deleteFave()
142     {
143         if (empty($this->auth_user) ||
144             $this->auth_user->id != $this->_profile->id) {
145             // TRANS: Client exception thrown when trying to remove a favorite notice of another user.
146             throw new ClientException(_("Cannot delete someone else's".
147                                         " favorite."), 403);
148         }
149
150         $this->_fave->delete();
151
152         return;
153     }
154
155     /**
156      * Return true if read only.
157      *
158      * MAY override
159      *
160      * @param array $args other arguments
161      *
162      * @return boolean is read only action?
163      */
164     function isReadOnly($args)
165     {
166         if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
167             $_SERVER['REQUEST_METHOD'] == 'HEAD') {
168             return true;
169         } else {
170             return false;
171         }
172     }
173
174     /**
175      * Return last modified, if applicable.
176      *
177      * MAY override
178      *
179      * @return string last modified http header
180      */
181     function lastModified()
182     {
183         return max(strtotime($this->_profile->modified),
184                    strtotime($this->_notice->modified),
185                    strtotime($this->_fave->modified));
186     }
187
188     /**
189      * Return etag, if applicable.
190      *
191      * MAY override
192      *
193      * @return string etag http header
194      */
195     function etag()
196     {
197         $mtime = strtotime($this->_fave->modified);
198
199         return 'W/"' . implode(':', array('AtomPubShowFavorite',
200                                           $this->_profile->id,
201                                           $this->_notice->id,
202                                           $mtime)) . '"';
203     }
204
205     /**
206      * Does this require authentication?
207      *
208      * @return boolean true if delete, else false
209      */
210     function requiresAuth()
211     {
212         if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
213             $_SERVER['REQUEST_METHOD'] == 'HEAD') {
214             return false;
215         } else {
216             return true;
217         }
218     }
219 }