]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/atompubshowsubscription.php
Simplified by adding an abstract AtompubAction
[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         if (!$this->_subscriber->isSubscribed($this->_subscribed)) {
74             // TRANS: Client exception thrown when trying to display a subscription for a non-subscribed profile ID.
75             // TRANS: %1$d is the non-existing subscriber ID number, $2$d is the ID of the profile that was not subscribed to.
76             $msg = sprintf(_('Profile %1$d not subscribed to profile %2$d.'),
77                            $subscriberId, $subscribedId);
78             throw new ClientException($msg, 404);
79         }
80
81         return true;
82     }
83
84     protected function handleGet()
85     {
86         $this->showSubscription();
87     }
88
89     protected function handleDelete()
90     {
91         $this->deleteSubscription();
92     }
93
94     /**
95      * Show the subscription in ActivityStreams Atom format.
96      *
97      * @return void
98      */
99     function showSubscription()
100     {
101         $activity = $this->_subscription->asActivity();
102
103         header('Content-Type: application/atom+xml; charset=utf-8');
104
105         $this->startXML();
106         $this->raw($activity->asString(true, true, true));
107         $this->endXML();
108     }
109
110     /**
111      * Delete the subscription
112      *
113      * @return void
114      */
115     function deleteSubscription()
116     {
117         if (!$this->scoped instanceof Profile ||
118                 $this->scoped->id != $this->_subscriber->id) {
119             // TRANS: Client exception thrown when trying to delete a subscription of another user.
120             throw new ClientException(_("Cannot delete someone else's subscription."), 403);
121         }
122
123         Subscription::cancel($this->_subscriber, $this->_subscribed);
124     }
125
126     /**
127      * Is this action read only?
128      *
129      * @param array $args other arguments
130      *
131      * @return boolean true
132      */
133     function isReadOnly($args)
134     {
135         if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
136             return false;
137         }
138
139         return true;
140     }
141
142     /**
143      * Return last modified, if applicable.
144      *
145      * @return string last modified http header
146      */
147     function lastModified()
148     {
149         return max(strtotime($this->_subscriber->modified),
150                    strtotime($this->_subscribed->modified),
151                    strtotime($this->_subscription->modified));
152     }
153
154     /**
155      * Etag for this object
156      *
157      * @return string etag http header
158      */
159     function etag()
160     {
161         $mtime = strtotime($this->_subscription->modified);
162
163         return 'W/"' . implode(':', array('AtomPubShowSubscription',
164                                           $this->_subscriber->id,
165                                           $this->_subscribed->id,
166                                           $mtime)) . '"';
167     }
168
169     /**
170      * Does this require authentication?
171      *
172      * @return boolean true if delete, else false
173      */
174     function requiresAuth()
175     {
176         if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
177             return true;
178         } else {
179             return false;
180         }
181     }
182 }