]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/atompubshowfavorite.php
Add IdentiCurse to notice sources
[quix0rs-gnu-social.git] / 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 require_once INSTALLDIR . '/lib/apiauth.php';
38
39 /**
40  * Show a single favorite in Atom Activity Streams format.
41  *
42  * Can also be used to delete a favorite.
43  *
44  * @category  Action
45  * @package   StatusNet
46  * @author    Evan Prodromou <evan@status.net>
47  * @copyright 2010 StatusNet, Inc.
48  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
49  * @link      http://status.net/
50  */
51
52 class AtompubshowfavoriteAction extends ApiAuthAction
53 {
54     private $_profile = null;
55     private $_notice  = null;
56     private $_fave    = null;
57
58     /**
59      * For initializing members of the class.
60      *
61      * @param array $argarray misc. arguments
62      *
63      * @return boolean true
64      */
65
66     function prepare($argarray)
67     {
68         parent::prepare($argarray);
69
70         $profileId = $this->trimmed('profile');
71         $noticeId  = $this->trimmed('notice');
72
73         $this->_profile = Profile::staticGet('id', $profileId);
74
75         if (empty($this->_profile)) {
76             throw new ClientException(_('No such profile.'), 404);
77         }
78
79         $this->_notice = Notice::staticGet('id', $noticeId);
80
81         if (empty($this->_notice)) {
82             throw new ClientException(_('No such notice.'), 404);
83         }
84
85         $this->_fave = Fave::pkeyGet(array('user_id' => $profileId,
86                                            'notice_id' => $noticeId));
87
88         if (empty($this->_fave)) {
89             throw new ClientException(_('No such favorite.'), 404);
90         }
91
92         return true;
93     }
94
95     /**
96      * Handler method
97      *
98      * @param array $argarray is ignored since it's now passed in in prepare()
99      *
100      * @return void
101      */
102
103     function handle($argarray=null)
104     {
105         parent::handle($argarray);
106
107         switch ($_SERVER['REQUEST_METHOD']) {
108         case GET:
109         case HEAD:
110             $this->showFave();
111             break;
112         case DELETE:
113             $this->deleteFave();
114             break;
115         default:
116             throw new ClientException(_('HTTP method not supported.'),
117                                       405);
118         }
119         return true;
120     }
121
122     /**
123      * Show a single favorite, in ActivityStreams format
124      * 
125      * @return void
126      */
127
128     function showFave()
129     {
130         $activity = $this->_fave->asActivity();
131
132         header('Content-Type: application/atom+xml; charset=utf-8');
133
134         $this->startXML();
135         $this->raw($activity->asString(true, true, true));
136         $this->endXML();
137
138         return;
139     }
140
141     /**
142      * Delete the favorite
143      * 
144      * @return void
145      */
146
147     function deleteFave()
148     {
149         if (empty($this->auth_user) ||
150             $this->auth_user->id != $this->_profile->id) {
151             throw new ClientException(_("Can't delete someone else's".
152                                         " favorite"), 403);
153         }
154
155         $this->_fave->delete();
156
157         return;
158     }
159
160     /**
161      * Return true if read only.
162      *
163      * MAY override
164      *
165      * @param array $args other arguments
166      *
167      * @return boolean is read only action?
168      */
169
170     function isReadOnly($args)
171     {
172         if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
173             $_SERVER['REQUEST_METHOD'] == 'HEAD') {
174             return true;
175         } else {
176             return false;
177         }
178     }
179
180     /**
181      * Return last modified, if applicable.
182      *
183      * MAY override
184      *
185      * @return string last modified http header
186      */
187
188     function lastModified()
189     {
190         return max(strtotime($this->_profile->modified),
191                    strtotime($this->_notice->modified),
192                    strtotime($this->_fave->modified));
193     }
194
195     /**
196      * Return etag, if applicable.
197      *
198      * MAY override
199      *
200      * @return string etag http header
201      */
202
203     function etag()
204     {
205         $mtime = strtotime($this->_fave->modified);
206
207         return 'W/"' . implode(':', array('AtomPubShowFavorite',
208                                           $this->_profile->id,
209                                           $this->_notice->id,
210                                           $mtime)) . '"';
211     }
212
213     /**
214      * Does this require authentication?
215      *
216      * @return boolean true if delete, else false
217      */
218
219     function requiresAuth()
220     {
221         if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
222             $_SERVER['REQUEST_METHOD'] == 'HEAD') {
223             return false;
224         } else {
225             return true;
226         }
227     }
228 }