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