]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/shownotice.php
Merge remote-tracking branch 'mainline/1.0.x' into people_tags_rebase
[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-2009 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 OwnerDesignAction
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     function prepare($args)
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             return false;
101         }
102
103         $this->user = User::staticGet('id', $this->profile->id);
104
105         $this->avatar = $this->profile->getAvatar(AVATAR_PROFILE_SIZE);
106
107         return true;
108     }
109
110     /**
111      * Fetch the notice to show. This may be overridden by child classes to
112      * customize what we fetch without duplicating all of the prepare() method.
113      *
114      * @return Notice
115      */
116     function getNotice()
117     {
118         $id = $this->arg('notice');
119
120         $notice = Notice::staticGet('id', $id);
121
122         if (empty($notice)) {
123             // Did we used to have it, and it got deleted?
124             $deleted = Deleted_notice::staticGet($id);
125             if (!empty($deleted)) {
126                 // TRANS: Client error displayed trying to show a deleted notice.
127                 $this->clientError(_('Notice deleted.'), 410);
128             } else {
129                 // TRANS: Client error displayed trying to show a non-existing notice.
130                 $this->clientError(_('No such notice.'), 404);
131             }
132             return false;
133         }
134         return $notice;
135     }
136
137     /**
138      * Is this action read-only?
139      *
140      * @return boolean true
141      */
142     function isReadOnly($args)
143     {
144         return true;
145     }
146
147     /**
148      * Last-modified date for page
149      *
150      * When was the content of this page last modified? Based on notice,
151      * profile, avatar.
152      *
153      * @return int last-modified date as unix timestamp
154      */
155     function lastModified()
156     {
157         return max(strtotime($this->notice->modified),
158                    strtotime($this->profile->modified),
159                    ($this->avatar) ? strtotime($this->avatar->modified) : 0);
160     }
161
162     /**
163      * An entity tag for this page
164      *
165      * Shows the ETag for the page, based on the notice ID and timestamps
166      * for the notice, profile, and avatar. It's weak, since we change
167      * the date text "one hour ago", etc.
168      *
169      * @return string etag
170      */
171     function etag()
172     {
173         $avtime = ($this->avatar) ?
174           strtotime($this->avatar->modified) : 0;
175
176         return 'W/"' . implode(':', array($this->arg('action'),
177                                           common_user_cache_hash(),
178                                           common_language(),
179                                           $this->notice->id,
180                                           strtotime($this->notice->created),
181                                           strtotime($this->profile->modified),
182                                           $avtime)) . '"';
183     }
184
185     /**
186      * Title of the page
187      *
188      * @return string title of the page
189      */
190     function title()
191     {
192         $base = $this->profile->getFancyName();
193
194         // TRANS: Title of the page that shows a notice.
195         // TRANS: %1$s is a user name, %2$s is the notice creation date/time.
196         return sprintf(_('%1$s\'s status on %2$s'),
197                        $base,
198                        common_exact_date($this->notice->created));
199     }
200
201     /**
202      * Handle input
203      *
204      * Only handles get, so just show the page.
205      *
206      * @param array $args $_REQUEST data (unused)
207      *
208      * @return void
209      */
210     function handle($args)
211     {
212         parent::handle($args);
213
214         if ($this->boolean('ajax')) {
215             $this->showAjax();
216         } else {
217             if ($this->notice->is_local == Notice::REMOTE_OMB) {
218                 if (!empty($this->notice->url)) {
219                     $target = $this->notice->url;
220                 } else if (!empty($this->notice->uri) && preg_match('/^https?:/', $this->notice->uri)) {
221                     // Old OMB posts saved the remote URL only into the URI field.
222                     $target = $this->notice->uri;
223                 } else {
224                     // Shouldn't happen.
225                     $target = false;
226                 }
227                 if ($target && $target != $this->selfUrl()) {
228                     common_redirect($target, 301);
229                     return false;
230                 }
231             }
232             $this->showPage();
233         }
234     }
235
236     /**
237      * Don't show local navigation
238      *
239      * @return void
240      */
241     function showLocalNavBlock()
242     {
243     }
244
245     /**
246      * Fill the content area of the page
247      *
248      * Shows a single notice list item.
249      *
250      * @return void
251      */
252     function showContent()
253     {
254         $this->elementStart('ol', array('class' => 'notices xoxo'));
255         $nli = new SingleNoticeItem($this->notice, $this);
256         $nli->show();
257         $this->elementEnd('ol');
258     }
259
260     function showAjax()
261     {
262         header('Content-Type: text/xml;charset=utf-8');
263         $this->xw->startDocument('1.0', 'UTF-8');
264         $this->elementStart('html');
265         $this->elementStart('head');
266         // TRANS: Title for page that shows a notice.
267         $this->element('title', null, _m('TITLE','Notice'));
268         $this->elementEnd('head');
269         $this->elementStart('body');
270         $nli = new NoticeListItem($this->notice, $this);
271         $nli->show();
272         $this->elementEnd('body');
273         $this->elementEnd('html');
274     }
275
276     /**
277      * Don't show page notice
278      *
279      * @return void
280      */
281     function showPageNoticeBlock()
282     {
283     }
284
285     /**
286      * Don't show aside
287      *
288      * @return void
289      */
290     function showAside() {
291     }
292
293     /**
294      * Extra <head> content
295      *
296      * We show the microid(s) for the author, if any.
297      *
298      * @return void
299      */
300     function extraHead()
301     {
302         $user = User::staticGet($this->profile->id);
303
304         if (!$user) {
305             return;
306         }
307
308         if ($user->emailmicroid && $user->email && $this->notice->uri) {
309             $id = new Microid('mailto:'. $user->email,
310                               $this->notice->uri);
311             $this->element('meta', array('name' => 'microid',
312                                          'content' => $id->toString()));
313         }
314
315         $this->element('link',array('rel'=>'alternate',
316             'type'=>'application/json+oembed',
317             'href'=>common_local_url(
318                 'oembed',
319                 array(),
320                 array('format'=>'json','url'=>$this->notice->uri)),
321             'title'=>'oEmbed'),null);
322         $this->element('link',array('rel'=>'alternate',
323             'type'=>'text/xml+oembed',
324             'href'=>common_local_url(
325                 'oembed',
326                 array(),
327                 array('format'=>'xml','url'=>$this->notice->uri)),
328             'title'=>'oEmbed'),null);
329
330         // Extras to aid in sharing notices to Facebook
331         $avatar = $this->profile->getAvatar(AVATAR_PROFILE_SIZE);
332         $avatarUrl = ($avatar) ?
333                      $avatar->displayUrl() :
334                      Avatar::defaultImage(AVATAR_PROFILE_SIZE);
335         $this->element('meta', array('property' => 'og:image',
336                                      'content' => $avatarUrl));
337         $this->element('meta', array('property' => 'og:description',
338                                      'content' => $this->notice->content));
339     }
340 }
341
342 // @todo FIXME: Class documentation missing.
343 class SingleNoticeItem extends DoFollowListItem
344 {
345     /**
346      * Recipe function for displaying a single notice.
347      *
348      * We overload to show attachments.
349      *
350      * @return void
351      */
352     function show()
353     {
354         $this->showStart();
355         if (Event::handle('StartShowNoticeItem', array($this))) {
356             $this->showNotice();
357             $this->showNoticeAttachments();
358             $this->showNoticeInfo();
359             $this->showNoticeOptions();
360             Event::handle('EndShowNoticeItem', array($this));
361         }
362
363         $this->showEnd();
364     }
365
366     /**
367      * For our zoomed-in special case we'll use a fuller list
368      * for the attachment info.
369      */
370     function showNoticeAttachments() {
371         $al = new AttachmentList($this->notice, $this->out);
372         $al->show();
373     }
374
375     /**
376      * show the avatar of the notice's author
377      *
378      * We use the larger size for single notice page.
379      *
380      * @return void
381      */
382     function showAvatar()
383     {
384         $avatar_size = AVATAR_PROFILE_SIZE;
385
386         $avatar = $this->profile->getAvatar($avatar_size);
387
388         $this->out->element('img', array('src' => ($avatar) ?
389                                          $avatar->displayUrl() :
390                                          Avatar::defaultImage($avatar_size),
391                                          'class' => 'avatar photo',
392                                          'width' => $avatar_size,
393                                          'height' => $avatar_size,
394                                          'alt' =>
395                                          ($this->profile->fullname) ?
396                                          $this->profile->fullname :
397                                          $this->profile->nickname));
398     }
399 }