]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/atompubshowfavorite.php
Translator comments added
[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 class AtompubshowfavoriteAction extends ApiAuthAction
52 {
53     private $_profile = null;
54     private $_notice  = null;
55     private $_fave    = null;
56
57     /**
58      * For initializing members of the class.
59      *
60      * @param array $argarray misc. arguments
61      *
62      * @return boolean true
63      */
64     function prepare($argarray)
65     {
66         parent::prepare($argarray);
67
68         $profileId = $this->trimmed('profile');
69         $noticeId  = $this->trimmed('notice');
70
71         $this->_profile = Profile::staticGet('id', $profileId);
72
73         if (empty($this->_profile)) {
74             // TRANS: Client exception.
75             throw new ClientException(_('No such profile.'), 404);
76         }
77
78         $this->_notice = Notice::staticGet('id', $noticeId);
79
80         if (empty($this->_notice)) {
81             // TRANS: Client exception thrown when referencing a non-existing 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             // TRANS: Client exception thrown when referencing a non-existing favorite.
90             throw new ClientException(_('No such favorite.'), 404);
91         }
92
93         return true;
94     }
95
96     /**
97      * Handler method
98      *
99      * @param array $argarray is ignored since it's now passed in in prepare()
100      *
101      * @return void
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             // TRANS: Client exception thrown using an unsupported HTTP method.
117             throw new ClientException(_('HTTP method not supported.'),
118                                       405);
119         }
120         return true;
121     }
122
123     /**
124      * Show a single favorite, in ActivityStreams format
125      *
126      * @return void
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     function deleteFave()
147     {
148         if (empty($this->auth_user) ||
149             $this->auth_user->id != $this->_profile->id) {
150             // TRANS: Client exception thrown when trying to remove a favorite notice of another user.
151             throw new ClientException(_("Cannot 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     function isReadOnly($args)
170     {
171         if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
172             $_SERVER['REQUEST_METHOD'] == 'HEAD') {
173             return true;
174         } else {
175             return false;
176         }
177     }
178
179     /**
180      * Return last modified, if applicable.
181      *
182      * MAY override
183      *
184      * @return string last modified http header
185      */
186     function lastModified()
187     {
188         return max(strtotime($this->_profile->modified),
189                    strtotime($this->_notice->modified),
190                    strtotime($this->_fave->modified));
191     }
192
193     /**
194      * Return etag, if applicable.
195      *
196      * MAY override
197      *
198      * @return string etag http header
199      */
200     function etag()
201     {
202         $mtime = strtotime($this->_fave->modified);
203
204         return 'W/"' . implode(':', array('AtomPubShowFavorite',
205                                           $this->_profile->id,
206                                           $this->_notice->id,
207                                           $mtime)) . '"';
208     }
209
210     /**
211      * Does this require authentication?
212      *
213      * @return boolean true if delete, else false
214      */
215     function requiresAuth()
216     {
217         if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
218             $_SERVER['REQUEST_METHOD'] == 'HEAD') {
219             return false;
220         } else {
221             return true;
222         }
223     }
224 }