]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/shownotice.php
Merge remote-tracking branch 'upstream/master' into social-master
[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('GNUSOCIAL')) { exit(1); }
31
32 require_once INSTALLDIR.'/lib/noticelist.php';
33
34 /**
35  * Show a single notice
36  *
37  * @category Personal
38  * @package  StatusNet
39  * @author   Evan Prodromou <evan@status.net>
40  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41  * @link     http://status.net/
42  */
43 class ShownoticeAction extends ManagedAction
44 {
45     /**
46      * Notice object to show
47      */
48     var $notice = null;
49
50     /**
51      * Profile of the notice object
52      */
53     var $profile = null;
54
55     /**
56      * Avatar of the profile of the notice object
57      */
58     var $avatar = null;
59
60     /**
61      * Load attributes based on database arguments
62      *
63      * Loads all the DB stuff
64      *
65      * @param array $args $_REQUEST array
66      *
67      * @return success flag
68      */
69     protected function prepare(array $args=array())
70     {
71         parent::prepare($args);
72         if ($this->boolean('ajax')) {
73             GNUsocial::setApi(true);
74         }
75
76         $this->notice = $this->getNotice();
77
78         if (!$this->notice->inScope($this->scoped)) {
79             // TRANS: Client exception thrown when trying a view a notice the user has no access to.
80             throw new ClientException(_('Access restricted.'), 403);
81         }
82
83         $this->profile = $this->notice->getProfile();
84
85         if (!$this->profile instanceof Profile) {
86             // TRANS: Server error displayed trying to show a notice without a connected profile.
87             $this->serverError(_('Notice has no profile.'), 500);
88         }
89
90         try {
91             $this->user = $this->profile->getUser();
92         } catch (NoSuchUserException $e) {
93             // FIXME: deprecate $this->user stuff in extended classes
94             $this->user = null;
95         }
96
97         try {
98             $this->avatar = $this->profile->getAvatar(AVATAR_PROFILE_SIZE);
99         } catch (Exception $e) {
100             $this->avatar = null;
101         }
102
103         return true;
104     }
105
106     /**
107      * Fetch the notice to show. This may be overridden by child classes to
108      * customize what we fetch without duplicating all of the prepare() method.
109      *
110      * @return Notice
111      */
112     protected function getNotice()
113     {
114         $id = $this->arg('notice');
115
116         $notice = Notice::getKV('id', $id);
117         if ($notice instanceof Notice) {
118             // Alright, got it!
119             return $notice;
120         }
121
122         // Did we use to have it, and it got deleted?
123         $deleted = Deleted_notice::getKV('id', $id);
124         if ($deleted instanceof Deleted_notice) {
125             // TRANS: Client error displayed trying to show a deleted notice.
126             $this->clientError(_('Notice deleted.'), 410);
127         }
128         // TRANS: Client error displayed trying to show a non-existing notice.
129         $this->clientError(_('No such notice.'), 404);
130     }
131
132     /**
133      * Is this action read-only?
134      *
135      * @return boolean true
136      */
137     function isReadOnly(array $args=array())
138     {
139         return true;
140     }
141
142     /**
143      * Last-modified date for page
144      *
145      * When was the content of this page last modified? Based on notice,
146      * profile, avatar.
147      *
148      * @return int last-modified date as unix timestamp
149      */
150     function lastModified()
151     {
152         return max(strtotime($this->notice->modified),
153                    strtotime($this->profile->modified),
154                    ($this->avatar) ? strtotime($this->avatar->modified) : 0);
155     }
156
157     /**
158      * An entity tag for this page
159      *
160      * Shows the ETag for the page, based on the notice ID and timestamps
161      * for the notice, profile, and avatar. It's weak, since we change
162      * the date text "one hour ago", etc.
163      *
164      * @return string etag
165      */
166     function etag()
167     {
168         $avtime = ($this->avatar) ?
169           strtotime($this->avatar->modified) : 0;
170
171         return 'W/"' . implode(':', array($this->arg('action'),
172                                           common_user_cache_hash(),
173                                           common_language(),
174                                           $this->notice->id,
175                                           strtotime($this->notice->created),
176                                           strtotime($this->profile->modified),
177                                           $avtime)) . '"';
178     }
179
180     /**
181      * Title of the page
182      *
183      * @return string title of the page
184      */
185     function title()
186     {
187         return $this->notice->getTitle();
188     }
189
190     /**
191      * Fill the content area of the page
192      *
193      * Shows a single notice list item.
194      *
195      * @return void
196      */
197     function showContent()
198     {
199         $this->elementStart('ol', array('class' => 'notices xoxo'));
200         $nli = new NoticeListItem($this->notice, $this);
201         $nli->show();
202         $this->elementEnd('ol');
203     }
204
205     /**
206      * Don't show page notice
207      *
208      * @return void
209      */
210     function showPageNoticeBlock()
211     {
212     }
213
214     /**
215      * Don't show aside
216      *
217      * @return void
218      */
219     function showAside() {
220     }
221
222     /**
223      * Extra <head> content
224      *
225      * Facebook OpenGraph metadata.
226      *
227      * @return void
228      */
229     function extraHead()
230     {
231         // Extras to aid in sharing notices to Facebook
232         $avatarUrl = $this->profile->avatarUrl(AVATAR_PROFILE_SIZE);
233         $this->element('meta', array('property' => 'og:image',
234                                      'content' => $avatarUrl));
235         $this->element('meta', array('property' => 'og:description',
236                                      'content' => $this->notice->content));
237     }
238 }