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