]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/atompubshowsubscription.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / actions / atompubshowsubscription.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * Single subscription
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('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
32
33 /**
34  * Show a single subscription
35  *
36  * @category  AtomPub
37  * @package   StatusNet
38  * @author    Evan Prodromou <evan@status.net>
39  * @copyright 2010 StatusNet, Inc.
40  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
41  * @link      http://status.net/
42  */
43 class AtompubshowsubscriptionAction extends AtompubAction
44 {
45     private $_subscriber   = null;
46     private $_subscribed   = null;
47     private $_subscription = null;
48
49     protected function atompubPrepare()
50     {
51         $subscriberId = $this->trimmed('subscriber');
52
53         $this->_subscriber = Profile::getKV('id', $subscriberId);
54
55         if (!$this->_subscriber instanceof Profile) {
56             // TRANS: Client exception thrown when trying to display a subscription for a non-existing profile ID.
57             // TRANS: %d is the non-existing profile ID number.
58             throw new ClientException(sprintf(_('No such profile id: %d.'),
59                                               $subscriberId), 404);
60         }
61
62         $subscribedId = $this->trimmed('subscribed');
63
64         $this->_subscribed = Profile::getKV('id', $subscribedId);
65
66         if (!$this->_subscribed instanceof Profile) {
67             // TRANS: Client exception thrown when trying to display a subscription for a non-existing profile ID.
68             // TRANS: %d is the non-existing profile ID number.
69             throw new ClientException(sprintf(_('No such profile id: %d.'),
70                                               $subscribedId), 404);
71         }
72
73         $this->_subscription = Subscription::pkeyGet(array('subscriber' => $subscriberId,
74                                                            'subscribed' => $subscribedId));
75         if (!$this->_subscription instanceof Subscription) {
76             // TRANS: Client exception thrown when trying to display a subscription for a non-subscribed profile ID.
77             // TRANS: %1$d is the non-existing subscriber ID number, $2$d is the ID of the profile that was not subscribed to.
78             $msg = sprintf(_('Profile %1$d not subscribed to profile %2$d.'),
79                            $subscriberId, $subscribedId);
80             throw new ClientException($msg, 404);
81         }
82
83         return true;
84     }
85
86     protected function handleGet()
87     {
88         $this->showSubscription();
89     }
90
91     protected function handleDelete()
92     {
93         $this->deleteSubscription();
94     }
95
96     /**
97      * Show the subscription in ActivityStreams Atom format.
98      *
99      * @return void
100      */
101     function showSubscription()
102     {
103         $activity = $this->_subscription->asActivity();
104
105         header('Content-Type: application/atom+xml; charset=utf-8');
106
107         $this->startXML();
108         $this->raw($activity->asString(true, true, true));
109         $this->endXML();
110     }
111
112     /**
113      * Delete the subscription
114      *
115      * @return void
116      */
117     function deleteSubscription()
118     {
119         if (!$this->scoped instanceof Profile ||
120                 $this->scoped->id != $this->_subscriber->id) {
121             // TRANS: Client exception thrown when trying to delete a subscription of another user.
122             throw new ClientException(_("Cannot delete someone else's subscription."), 403);
123         }
124
125         Subscription::cancel($this->_subscriber, $this->_subscribed);
126     }
127
128     /**
129      * Is this action read only?
130      *
131      * @param array $args other arguments
132      *
133      * @return boolean true
134      */
135     function isReadOnly($args)
136     {
137         if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
138             return false;
139         }
140
141         return true;
142     }
143
144     /**
145      * Return last modified, if applicable.
146      *
147      * @return string last modified http header
148      */
149     function lastModified()
150     {
151         return max(strtotime($this->_subscriber->modified),
152                    strtotime($this->_subscribed->modified),
153                    strtotime($this->_subscription->modified));
154     }
155
156     /**
157      * Etag for this object
158      *
159      * @return string etag http header
160      */
161     function etag()
162     {
163         $mtime = strtotime($this->_subscription->modified);
164
165         return 'W/"' . implode(':', array('AtomPubShowSubscription',
166                                           $this->_subscriber->id,
167                                           $this->_subscribed->id,
168                                           $mtime)) . '"';
169     }
170
171     /**
172      * Does this require authentication?
173      *
174      * @return boolean true if delete, else false
175      */
176     function requiresAuth()
177     {
178         if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
179             return true;
180         } else {
181             return false;
182         }
183     }
184 }