]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/attachments.php
Merge branch '0.8.x' of git@gitorious.org:+laconica-developers/laconica/dev into...
[quix0rs-gnu-social.git] / actions / attachments.php
1 <?php
2 /**
3  * Laconica, the distributed open-source microblogging tool
4  *
5  * Show notice attachments
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   Laconica
24  * @author    Evan Prodromou <evan@controlyourself.ca>
25  * @copyright 2008-2009 Control Yourself, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://laconi.ca/
28  */
29
30 if (!defined('LACONICA')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR.'/lib/attachmentlist.php';
35
36 /**
37  * Show notice attachments
38  *
39  * @category Personal
40  * @package  Laconica
41  * @author   Evan Prodromou <evan@controlyourself.ca>
42  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43  * @link     http://laconi.ca/
44  */
45
46 class AttachmentsAction extends Action
47 {
48     /**
49      * Notice object to show
50      */
51
52     var $notice = null;
53
54     /**
55      * Profile of the notice object
56      */
57
58     var $profile = null;
59
60     /**
61      * Avatar of the profile of the notice object
62      */
63
64     var $avatar = null;
65
66     /**
67      * Is this action read-only?
68      *
69      * @return boolean true
70      */
71
72     function isReadOnly($args)
73     {
74         return true;
75     }
76
77     /**
78      * Last-modified date for page
79      *
80      * When was the content of this page last modified? Based on notice,
81      * profile, avatar.
82      *
83      * @return int last-modified date as unix timestamp
84      */
85
86     function lastModified()
87     {
88         return max(strtotime($this->notice->created),
89                    strtotime($this->profile->modified),
90                    ($this->avatar) ? strtotime($this->avatar->modified) : 0);
91     }
92
93     /**
94      * An entity tag for this page
95      *
96      * Shows the ETag for the page, based on the notice ID and timestamps
97      * for the notice, profile, and avatar. It's weak, since we change
98      * the date text "one hour ago", etc.
99      *
100      * @return string etag
101      */
102
103     function etag()
104     {
105         $avtime = ($this->avatar) ?
106           strtotime($this->avatar->modified) : 0;
107
108         return 'W/"' . implode(':', array($this->arg('action'),
109                                           common_language(),
110                                           $this->notice->id,
111                                           strtotime($this->notice->created),
112                                           strtotime($this->profile->modified),
113                                           $avtime)) . '"';
114     }
115
116     /**
117      * Title of the page
118      *
119      * @return string title of the page
120      */
121
122     function title()
123     {
124         return sprintf(_('%1$s\'s status on %2$s'),
125                        $this->profile->nickname,
126                        common_exact_date($this->notice->created));
127     }
128
129
130     /**
131      * Load attributes based on database arguments
132      *
133      * Loads all the DB stuff
134      *
135      * @param array $args $_REQUEST array
136      *
137      * @return success flag
138      */
139
140     function prepare($args)
141     {
142         parent::prepare($args);
143
144         $id = $this->arg('notice');
145
146         $this->notice = Notice::staticGet($id);
147
148         if (!$this->notice) {
149             $this->clientError(_('No such notice.'), 404);
150             return false;
151         }
152
153
154 /*
155 // STOP if there are no attachments
156 // maybe even redirect if there's a single one
157 // RYM FIXME TODO
158             $this->clientError(_('No such attachment.'), 404);
159             return false;
160
161 */
162
163
164
165
166         $this->profile = $this->notice->getProfile();
167
168         if (!$this->profile) {
169             $this->serverError(_('Notice has no profile'), 500);
170             return false;
171         }
172
173         $this->avatar = $this->profile->getAvatar(AVATAR_PROFILE_SIZE);
174         return true;
175     }
176
177
178
179     /**
180      * Handle input
181      *
182      * Only handles get, so just show the page.
183      *
184      * @param array $args $_REQUEST data (unused)
185      *
186      * @return void
187      */
188
189     function handle($args)
190     {
191         parent::handle($args);
192
193         if ($this->notice->is_local == 0) {
194             if (!empty($this->notice->url)) {
195                 common_redirect($this->notice->url, 301);
196             } else if (!empty($this->notice->uri) && preg_match('/^https?:/', $this->notice->uri)) {
197                 common_redirect($this->notice->uri, 301);
198             }
199         } else {
200             $f2p = new File_to_post;
201             $f2p->post_id = $this->notice->id;
202             $file = new File;
203             $file->joinAdd($f2p);
204             $file->selectAdd();
205             $file->selectAdd('file.id as id');
206             $count = $file->find(true);
207             if (!$count) return;
208             if (1 === $count) {
209                 common_redirect(common_local_url('attachment', array('attachment' => $file->id)), 301);
210             } else {
211                 $this->showPage();
212             }
213         }
214     }
215
216     /**
217      * Don't show local navigation
218      *
219      * @return void
220      */
221
222     function showLocalNavBlock()
223     {
224     }
225
226     /**
227      * Fill the content area of the page
228      *
229      * Shows a single notice list item.
230      *
231      * @return void
232      */
233
234     function showContent()
235     {
236         $al = new AttachmentList($this->notice, $this);
237         $cnt = $al->show();
238     }
239
240     /**
241      * Don't show page notice
242      *
243      * @return void
244      */
245
246     function showPageNoticeBlock()
247     {
248     }
249
250     /**
251      * Don't show aside
252      *
253      * @return void
254      */
255
256     function showAside() {
257     }
258
259     /**
260      * Extra <head> content
261      *
262      * We show the microid(s) for the author, if any.
263      *
264      * @return void
265      */
266
267     function extraHead()
268     {
269         $user = User::staticGet($this->profile->id);
270
271         if (!$user) {
272             return;
273         }
274
275         if ($user->emailmicroid && $user->email && $this->notice->uri) {
276             $id = new Microid('mailto:'. $user->email,
277                               $this->notice->uri);
278             $this->element('meta', array('name' => 'microid',
279                                          'content' => $id->toString()));
280         }
281
282         if ($user->jabbermicroid && $user->jabber && $this->notice->uri) {
283             $id = new Microid('xmpp:', $user->jabber,
284                               $this->notice->uri);
285             $this->element('meta', array('name' => 'microid',
286                                          'content' => $id->toString()));
287         }
288     }
289 }
290