]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/shownotice.php
Notice URIs are not necessarily URLs.
[quix0rs-gnu-social.git] / actions / shownotice.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Show a single notice
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Personal
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @copyright 2008-2011 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR.'/lib/personalgroupnav.php';
35 require_once INSTALLDIR.'/lib/noticelist.php';
36 require_once INSTALLDIR.'/lib/feedlist.php';
37
38 /**
39  * Show a single notice
40  *
41  * @category Personal
42  * @package  StatusNet
43  * @author   Evan Prodromou <evan@status.net>
44  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45  * @link     http://status.net/
46  */
47 class ShownoticeAction extends Action
48 {
49     /**
50      * Notice object to show
51      */
52     var $notice = null;
53
54     /**
55      * Profile of the notice object
56      */
57     var $profile = null;
58
59     /**
60      * Avatar of the profile of the notice object
61      */
62     var $avatar = null;
63
64     /**
65      * Load attributes based on database arguments
66      *
67      * Loads all the DB stuff
68      *
69      * @param array $args $_REQUEST array
70      *
71      * @return success flag
72      */
73     protected function prepare(array $args=array())
74     {
75         parent::prepare($args);
76         if ($this->boolean('ajax')) {
77             StatusNet::setApi(true);
78         }
79
80         $this->notice = $this->getNotice();
81
82         $cur = common_current_user();
83
84         if (!empty($cur)) {
85             $curProfile = $cur->getProfile();
86         } else {
87             $curProfile = null;
88         }
89
90         if (!$this->notice->inScope($curProfile)) {
91             // TRANS: Client exception thrown when trying a view a notice the user has no access to.
92             throw new ClientException(_('Not available.'), 403);
93         }
94
95         $this->profile = $this->notice->getProfile();
96
97         if (empty($this->profile)) {
98             // TRANS: Server error displayed trying to show a notice without a connected profile.
99             $this->serverError(_('Notice has no profile.'), 500);
100         }
101
102         $this->user = User::getKV('id', $this->profile->id);
103
104         try {
105             $this->avatar = $this->profile->getAvatar(AVATAR_PROFILE_SIZE);
106         } catch (Exception $e) {
107             $this->avatar = null;
108         }
109
110         return true;
111     }
112
113     /**
114      * Fetch the notice to show. This may be overridden by child classes to
115      * customize what we fetch without duplicating all of the prepare() method.
116      *
117      * @return Notice
118      */
119     protected function getNotice()
120     {
121         $id = $this->arg('notice');
122
123         $notice = Notice::getKV('id', $id);
124
125         if (!$notice instanceof Notice) {
126             // Did we used to have it, and it got deleted?
127             $deleted = Deleted_notice::getKV($id);
128             if ($deleted instanceof Deleted_notice) {
129                 // TRANS: Client error displayed trying to show a deleted notice.
130                 $this->clientError(_('Notice deleted.'), 410);
131             } else {
132                 // TRANS: Client error displayed trying to show a non-existing notice.
133                 $this->clientError(_('No such notice.'), 404);
134             }
135             return false;
136         }
137         return $notice;
138     }
139
140     /**
141      * Is this action read-only?
142      *
143      * @return boolean true
144      */
145     function isReadOnly($args)
146     {
147         return true;
148     }
149
150     /**
151      * Last-modified date for page
152      *
153      * When was the content of this page last modified? Based on notice,
154      * profile, avatar.
155      *
156      * @return int last-modified date as unix timestamp
157      */
158     function lastModified()
159     {
160         return max(strtotime($this->notice->modified),
161                    strtotime($this->profile->modified),
162                    ($this->avatar) ? strtotime($this->avatar->modified) : 0);
163     }
164
165     /**
166      * An entity tag for this page
167      *
168      * Shows the ETag for the page, based on the notice ID and timestamps
169      * for the notice, profile, and avatar. It's weak, since we change
170      * the date text "one hour ago", etc.
171      *
172      * @return string etag
173      */
174     function etag()
175     {
176         $avtime = ($this->avatar) ?
177           strtotime($this->avatar->modified) : 0;
178
179         return 'W/"' . implode(':', array($this->arg('action'),
180                                           common_user_cache_hash(),
181                                           common_language(),
182                                           $this->notice->id,
183                                           strtotime($this->notice->created),
184                                           strtotime($this->profile->modified),
185                                           $avtime)) . '"';
186     }
187
188     /**
189      * Title of the page
190      *
191      * @return string title of the page
192      */
193     function title()
194     {
195         $base = $this->profile->getFancyName();
196
197         // TRANS: Title of the page that shows a notice.
198         // TRANS: %1$s is a user name, %2$s is the notice creation date/time.
199         return sprintf(_('%1$s\'s status on %2$s'),
200                        $base,
201                        common_exact_date($this->notice->created));
202     }
203
204     /**
205      * Handle input
206      *
207      * Only handles get, so just show the page.
208      *
209      * @param array $args $_REQUEST data (unused)
210      *
211      * @return void
212      */
213     protected function handle()
214     {
215         parent::handle();
216
217         if ($this->boolean('ajax')) {
218             $this->showAjax();
219         } else {
220             if ($this->notice->is_local == Notice::REMOTE) {
221                 try {
222                     $target = $this->notice->getUrl();
223                     if ($target != $this->selfUrl()) {
224                         common_redirect($target, 301);
225                     }
226                 } catch (InvalidUrlException $e) {
227                     common_debug('ShownoticeAction could not redirect to remote notice with id='.$this->notice->id . '. Falling back to showPage().');
228                 }
229             }
230             $this->showPage();
231         }
232     }
233
234     /**
235      * Fill the content area of the page
236      *
237      * Shows a single notice list item.
238      *
239      * @return void
240      */
241     function showContent()
242     {
243         $this->elementStart('ol', array('class' => 'notices xoxo'));
244         $nli = new SingleNoticeItem($this->notice, $this);
245         $nli->show();
246         $this->elementEnd('ol');
247     }
248
249     function showAjax()
250     {
251         $this->startHTML('text/xml;charset=utf-8');
252         $this->elementStart('head');
253         // TRANS: Title for page that shows a notice.
254         $this->element('title', null, _m('TITLE','Notice'));
255         $this->elementEnd('head');
256         $this->elementStart('body');
257         $nli = new NoticeListItem($this->notice, $this);
258         $nli->show();
259         $this->elementEnd('body');
260         $this->endHTML();
261     }
262
263     /**
264      * Don't show page notice
265      *
266      * @return void
267      */
268     function showPageNoticeBlock()
269     {
270     }
271
272     /**
273      * Don't show aside
274      *
275      * @return void
276      */
277     function showAside() {
278     }
279
280     /**
281      * Extra <head> content
282      *
283      * We show the microid(s) for the author, if any.
284      *
285      * @return void
286      */
287     function extraHead()
288     {
289         $user = User::getKV($this->profile->id);
290
291         if (!$user) {
292             return;
293         }
294
295         if ($user->emailmicroid && $user->email && $this->notice->uri) {
296             $id = new Microid('mailto:'. $user->email,
297                               $this->notice->uri);
298             $this->element('meta', array('name' => 'microid',
299                                          'content' => $id->toString()));
300         }
301
302         $this->element('link',array('rel'=>'alternate',
303             'type'=>'application/json+oembed',
304             'href'=>common_local_url(
305                 'oembed',
306                 array(),
307                 array('format'=>'json','url'=>$this->notice->getUrl())),
308             'title'=>'oEmbed'),null);
309         $this->element('link',array('rel'=>'alternate',
310             'type'=>'text/xml+oembed',
311             'href'=>common_local_url(
312                 'oembed',
313                 array(),
314                 array('format'=>'xml','url'=>$this->notice->getUrl())),
315             'title'=>'oEmbed'),null);
316
317         // Extras to aid in sharing notices to Facebook
318         $avatarUrl = $this->profile->avatarUrl(AVATAR_PROFILE_SIZE);
319         $this->element('meta', array('property' => 'og:image',
320                                      'content' => $avatarUrl));
321         $this->element('meta', array('property' => 'og:description',
322                                      'content' => $this->notice->content));
323     }
324 }
325
326 // @todo FIXME: Class documentation missing.
327 class SingleNoticeItem extends DoFollowListItem
328 {
329     function avatarSize()
330     {
331         return AVATAR_STREAM_SIZE;
332     }
333 }