]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/shownotice.php
Merge branch 'limitdist2' into 1.0.x
[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', $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         $cur = common_current_user();
98
99         if (!empty($cur)) {
100             $curProfile = $cur->getProfile();
101         } else {
102             $curProfile = null;
103         }
104
105         if (!$this->notice->inScope($curProfile)) {
106             throw new ClientException(_('Not available.'), 403);
107         }
108
109         $this->profile = $this->notice->getProfile();
110
111         if (empty($this->profile)) {
112             // TRANS: Server error displayed trying to show a notice without a connected profile.
113             $this->serverError(_('Notice has no profile.'), 500);
114             return false;
115         }
116
117         $this->user = User::staticGet('id', $this->profile->id);
118
119         $this->avatar = $this->profile->getAvatar(AVATAR_PROFILE_SIZE);
120
121         return true;
122     }
123
124     /**
125      * Is this action read-only?
126      *
127      * @return boolean true
128      */
129     function isReadOnly($args)
130     {
131         return true;
132     }
133
134     /**
135      * Last-modified date for page
136      *
137      * When was the content of this page last modified? Based on notice,
138      * profile, avatar.
139      *
140      * @return int last-modified date as unix timestamp
141      */
142     function lastModified()
143     {
144         return max(strtotime($this->notice->modified),
145                    strtotime($this->profile->modified),
146                    ($this->avatar) ? strtotime($this->avatar->modified) : 0);
147     }
148
149     /**
150      * An entity tag for this page
151      *
152      * Shows the ETag for the page, based on the notice ID and timestamps
153      * for the notice, profile, and avatar. It's weak, since we change
154      * the date text "one hour ago", etc.
155      *
156      * @return string etag
157      */
158     function etag()
159     {
160         $avtime = ($this->avatar) ?
161           strtotime($this->avatar->modified) : 0;
162
163         return 'W/"' . implode(':', array($this->arg('action'),
164                                           common_user_cache_hash(),
165                                           common_language(),
166                                           $this->notice->id,
167                                           strtotime($this->notice->created),
168                                           strtotime($this->profile->modified),
169                                           $avtime)) . '"';
170     }
171
172     /**
173      * Title of the page
174      *
175      * @return string title of the page
176      */
177     function title()
178     {
179         $base = $this->profile->getFancyName();
180
181         // TRANS: Title of the page that shows a notice.
182         // TRANS: %1$s is a user name, %2$s is the notice creation date/time.
183         return sprintf(_('%1$s\'s status on %2$s'),
184                        $base,
185                        common_exact_date($this->notice->created));
186     }
187
188     /**
189      * Handle input
190      *
191      * Only handles get, so just show the page.
192      *
193      * @param array $args $_REQUEST data (unused)
194      *
195      * @return void
196      */
197     function handle($args)
198     {
199         parent::handle($args);
200
201         if ($this->boolean('ajax')) {
202             $this->showAjax();
203         } else {
204             if ($this->notice->is_local == Notice::REMOTE_OMB) {
205                 if (!empty($this->notice->url)) {
206                     $target = $this->notice->url;
207                 } else if (!empty($this->notice->uri) && preg_match('/^https?:/', $this->notice->uri)) {
208                     // Old OMB posts saved the remote URL only into the URI field.
209                     $target = $this->notice->uri;
210                 } else {
211                     // Shouldn't happen.
212                     $target = false;
213                 }
214                 if ($target && $target != $this->selfUrl()) {
215                     common_redirect($target, 301);
216                     return false;
217                 }
218             }
219             $this->showPage();
220         }
221     }
222
223     /**
224      * Don't show local navigation
225      *
226      * @return void
227      */
228     function showLocalNavBlock()
229     {
230     }
231
232     /**
233      * Fill the content area of the page
234      *
235      * Shows a single notice list item.
236      *
237      * @return void
238      */
239     function showContent()
240     {
241         $this->elementStart('ol', array('class' => 'notices xoxo'));
242         $nli = new SingleNoticeItem($this->notice, $this);
243         $nli->show();
244         $this->elementEnd('ol');
245     }
246
247     function showAjax()
248     {
249         header('Content-Type: text/xml;charset=utf-8');
250         $this->xw->startDocument('1.0', 'UTF-8');
251         $this->elementStart('html');
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->elementEnd('html');
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::staticGet($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->uri)),
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->uri)),
315             'title'=>'oEmbed'),null);
316
317         // Extras to aid in sharing notices to Facebook
318         $avatar = $this->profile->getAvatar(AVATAR_PROFILE_SIZE);
319         $avatarUrl = ($avatar) ?
320                      $avatar->displayUrl() :
321                      Avatar::defaultImage(AVATAR_PROFILE_SIZE);
322         $this->element('meta', array('property' => 'og:image',
323                                      'content' => $avatarUrl));
324         $this->element('meta', array('property' => 'og:description',
325                                      'content' => $this->notice->content));
326     }
327 }
328
329 // @todo FIXME: Class documentation missing.
330 class SingleNoticeItem extends DoFollowListItem
331 {
332     /**
333      * Recipe function for displaying a single notice.
334      *
335      * We overload to show attachments.
336      *
337      * @return void
338      */
339     function show()
340     {
341         $this->showStart();
342         if (Event::handle('StartShowNoticeItem', array($this))) {
343             $this->showNotice();
344             $this->showNoticeAttachments();
345             $this->showNoticeInfo();
346             $this->showNoticeOptions();
347             Event::handle('EndShowNoticeItem', array($this));
348         }
349
350         $this->showEnd();
351     }
352
353     /**
354      * For our zoomed-in special case we'll use a fuller list
355      * for the attachment info.
356      */
357     function showNoticeAttachments() {
358         $al = new AttachmentList($this->notice, $this->out);
359         $al->show();
360     }
361
362     /**
363      * show the avatar of the notice's author
364      *
365      * We use the larger size for single notice page.
366      *
367      * @return void
368      */
369     function showAvatar()
370     {
371         $avatar_size = AVATAR_PROFILE_SIZE;
372
373         $avatar = $this->profile->getAvatar($avatar_size);
374
375         $this->out->element('img', array('src' => ($avatar) ?
376                                          $avatar->displayUrl() :
377                                          Avatar::defaultImage($avatar_size),
378                                          'class' => 'avatar photo',
379                                          'width' => $avatar_size,
380                                          'height' => $avatar_size,
381                                          'alt' =>
382                                          ($this->profile->fullname) ?
383                                          $this->profile->fullname :
384                                          $this->profile->nickname));
385     }
386 }