]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/attachmentlist.php
remove extraneous <dl> and <dt> tags
[quix0rs-gnu-social.git] / lib / attachmentlist.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * widget for displaying a list of 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  UI
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @author    Sarven Capadisli <csarven@status.net>
26  * @copyright 2008 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
32     exit(1);
33 }
34
35 /**
36  * widget for displaying a list of notice attachments
37  *
38  * There are a number of actions that display a list of notices, in
39  * reverse chronological order. This widget abstracts out most of the
40  * code for UI for notice lists. It's overridden to hide some
41  * data for e.g. the profile page.
42  *
43  * @category UI
44  * @package  StatusNet
45  * @author   Evan Prodromou <evan@status.net>
46  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47  * @link     http://status.net/
48  * @see      Notice
49  * @see      NoticeListItem
50  * @see      ProfileNoticeList
51  */
52 class AttachmentList extends Widget
53 {
54     /** the current stream of notices being displayed. */
55
56     var $notice = null;
57
58     /**
59      * constructor
60      *
61      * @param Notice $notice stream of notices from DB_DataObject
62      */
63     function __construct($notice, $out=null)
64     {
65         parent::__construct($out);
66         $this->notice = $notice;
67     }
68
69     /**
70      * show the list of notices
71      *
72      * "Uses up" the stream by looping through it. So, probably can't
73      * be called twice on the same list.
74      *
75      * @return int count of notices listed.
76      */
77     function show()
78     {
79         $atts = new File;
80         $att = $atts->getAttachments($this->notice->id);
81         if (empty($att)) return 0;
82         $this->showListStart();
83
84         foreach ($att as $n=>$attachment) {
85             $item = $this->newListItem($attachment);
86             $item->show();
87         }
88
89         $this->showListEnd();
90
91         return count($att);
92     }
93
94     function showListStart()
95     {
96         $this->out->elementStart('ol', array('class' => 'attachments entry-content'));
97     }
98
99     function showListEnd()
100     {
101         $this->out->elementEnd('ol');
102     }
103
104     /**
105      * returns a new list item for the current notice
106      *
107      * Recipe (factory?) method; overridden by sub-classes to give
108      * a different list item class.
109      *
110      * @param Notice $notice the current notice
111      *
112      * @return NoticeListItem a list item for displaying the notice
113      */
114     function newListItem($attachment)
115     {
116         return new AttachmentListItem($attachment, $this->out);
117     }
118 }
119
120 /**
121  * widget for displaying a single notice
122  *
123  * This widget has the core smarts for showing a single notice: what to display,
124  * where, and under which circumstances. Its key method is show(); this is a recipe
125  * that calls all the other show*() methods to build up a single notice. The
126  * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
127  * author info (since that's implicit by the data in the page).
128  *
129  * @category UI
130  * @package  StatusNet
131  * @author   Evan Prodromou <evan@status.net>
132  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
133  * @link     http://status.net/
134  * @see      NoticeList
135  * @see      ProfileNoticeListItem
136  */
137 class AttachmentListItem extends Widget
138 {
139     /** The attachment this item will show. */
140
141     var $attachment = null;
142
143     var $oembed = null;
144
145     /**
146      * constructor
147      *
148      * Also initializes the profile attribute.
149      *
150      * @param Notice $notice The notice we'll display
151      */
152     function __construct($attachment, $out=null)
153     {
154         parent::__construct($out);
155         $this->attachment  = $attachment;
156         $this->oembed = File_oembed::staticGet('file_id', $this->attachment->id);
157     }
158
159     function title() {
160         if (empty($this->attachment->title)) {
161             if (empty($this->oembed->title)) {
162                 $title = $this->attachment->url;
163             } else {
164                 $title = $this->oembed->title;
165             }
166         } else {
167             $title = $this->attachment->title;
168         }
169
170         return $title;
171     }
172
173     function linkTitle() {
174         return $this->title();
175     }
176
177     /**
178      * recipe function for displaying a single notice.
179      *
180      * This uses all the other methods to correctly display a notice. Override
181      * it or one of the others to fine-tune the output.
182      *
183      * @return void
184      */
185     function show()
186     {
187         $this->showStart();
188         $this->showNoticeAttachment();
189         $this->showEnd();
190     }
191
192     function linkAttr() {
193         return array('class' => 'attachment',
194                      'href' => $this->attachment->url,
195                      'id' => 'attachment-' . $this->attachment->id,
196                      'title' => $this->title());
197     }
198
199     function showLink() {
200         $this->out->elementStart('a', $this->linkAttr());
201         $this->out->element('span', null, $this->linkTitle());
202         $this->showRepresentation();
203         $this->out->elementEnd('a');
204     }
205
206     function showNoticeAttachment()
207     {
208         $this->showLink();
209     }
210
211     function showRepresentation() {
212         $thumb = $this->getThumbInfo();
213         if ($thumb) {
214             $this->out->element('img', array('alt' => '', 'src' => $thumb->url, 'width' => $thumb->width, 'height' => $thumb->height));
215         }
216     }
217
218     /**
219      * Pull a thumbnail image reference for the given file, and if necessary
220      * resize it to match currently thumbnail size settings.
221      *
222      * @return File_Thumbnail or false/null
223      */
224     function getThumbInfo()
225     {
226         $thumbnail = File_thumbnail::staticGet('file_id', $this->attachment->id);
227         if ($thumbnail) {
228             $maxWidth = common_config('attachments', 'thumb_width');
229             $maxHeight = common_config('attachments', 'thumb_height');
230             if ($thumbnail->width > $maxWidth) {
231                 $thumb = clone($thumbnail);
232                 $thumb->width = $maxWidth;
233                 $thumb->height = intval($thumbnail->height * $maxWidth / $thumbnail->width);
234                 return $thumb;
235             }
236         }
237         return $thumbnail;
238     }
239
240     /**
241      * start a single notice.
242      *
243      * @return void
244      */
245     function showStart()
246     {
247         // XXX: RDFa
248         // TODO: add notice_type class e.g., notice_video, notice_image
249         $this->out->elementStart('li');
250     }
251
252     /**
253      * finish the notice
254      *
255      * Close the last elements in the notice list item
256      *
257      * @return void
258      */
259     function showEnd()
260     {
261         $this->out->elementEnd('li');
262     }
263 }
264
265 /**
266  * used for one-off attachment action
267  */
268 class Attachment extends AttachmentListItem
269 {
270     function showLink() {
271         $this->out->elementStart('div', array('id' => 'attachment_view',
272                                               'class' => 'hentry'));
273         $this->out->elementStart('div', 'entry-title');
274         $this->out->element('a', $this->linkAttr(), $this->linkTitle());
275         $this->out->elementEnd('div');
276
277         $this->out->elementStart('div', 'entry-content');
278         $this->showRepresentation();
279         $this->out->elementEnd('div');
280
281         if (!empty($this->oembed->author_name) || !empty($this->oembed->provider)) {
282             $this->out->elementStart('div', array('id' => 'oembed_info',
283                                                   'class' => 'entry-content'));
284             if (!empty($this->oembed->author_name)) {
285                 $this->out->elementStart('div', 'fn vcard author');
286                 if (empty($this->oembed->author_url)) {
287                     $this->out->text($this->oembed->author_name);
288                 } else {
289                     $this->out->element('a', array('href' => $this->oembed->author_url,
290                                                    'class' => 'url'), $this->oembed->author_name);
291                 }
292             }
293             if (!empty($this->oembed->provider)) {
294                 $this->out->elementStart('div', 'fn vcard');
295                 if (empty($this->oembed->provider_url)) {
296                     $this->out->text($this->oembed->provider);
297                 } else {
298                     $this->out->element('a', array('href' => $this->oembed->provider_url,
299                                                    'class' => 'url'), $this->oembed->provider);
300                 }
301             }
302             $this->out->elementEnd('div');
303         }
304         $this->out->elementEnd('div');
305     }
306
307     function show() {
308         $this->showNoticeAttachment();
309     }
310
311     function linkAttr() {
312         return array('rel' => 'external', 'href' => $this->attachment->url);
313     }
314
315     function linkTitle() {
316         return $this->attachment->url;
317     }
318
319     function showRepresentation() {
320         if (empty($this->oembed->type)) {
321             if (empty($this->attachment->mimetype)) {
322                 $this->showFallback();
323             } else {
324                 switch ($this->attachment->mimetype) {
325                 case 'image/gif':
326                 case 'image/png':
327                 case 'image/jpg':
328                 case 'image/jpeg':
329                     $this->out->element('img', array('src' => $this->attachment->url, 'alt' => 'alt'));
330                     break;
331
332                 case 'application/ogg':
333                 case 'audio/x-speex':
334                 case 'video/mpeg':
335                 case 'audio/mpeg':
336                 case 'video/mp4':
337                 case 'video/quicktime':
338                     $arr  = array('type' => $this->attachment->mimetype,
339                         'data' => $this->attachment->url,
340                         'width' => 320,
341                         'height' => 240
342                     );
343                     $this->out->elementStart('object', $arr);
344                     $this->out->element('param', array('name' => 'src', 'value' => $this->attachment->url));
345                     $this->out->element('param', array('name' => 'autoStart', 'value' => 1));
346                     $this->out->elementEnd('object');
347                     break;
348
349                 case 'text/html':
350                     if ($this->attachment->filename) {
351                         // Locally-uploaded HTML. Scrub and display inline.
352                         $this->showHtmlFile($this->attachment);
353                         break;
354                     }
355                     // Fall through to default.
356
357                 default:
358                     $this->showFallback();
359                 }
360             }
361         } else {
362             switch ($this->oembed->type) {
363             case 'rich':
364             case 'video':
365             case 'link':
366                 if (!empty($this->oembed->html)) {
367                     require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
368                     $config = array(
369                         'safe'=>1,
370                         'elements'=>'*+object+embed');
371                     $this->out->raw(htmLawed($this->oembed->html,$config));
372                     //$this->out->raw($this->oembed->html);
373                 }
374                 break;
375
376             case 'photo':
377                 $this->out->element('img', array('src' => $this->oembed->url, 'width' => $this->oembed->width, 'height' => $this->oembed->height, 'alt' => 'alt'));
378                 break;
379
380             default:
381                 $this->showFallback();
382             }
383         }
384     }
385
386     protected function showHtmlFile(File $attachment)
387     {
388         $body = $this->scrubHtmlFile($attachment);
389         if ($body) {
390             $this->out->raw($body);
391         }
392     }
393
394     /**
395      * @return mixed false on failure, HTML fragment string on success
396      */
397     protected function scrubHtmlFile(File $attachment)
398     {
399         $path = File::path($attachment->filename);
400         if (!file_exists($path) || !is_readable($path)) {
401             common_log(LOG_ERR, "Missing local HTML attachment $path");
402             return false;
403         }
404         $raw = file_get_contents($path);
405
406         // Normalize...
407         $dom = new DOMDocument();
408         if(!$dom->loadHTML($raw)) {
409             common_log(LOG_ERR, "Bad HTML in local HTML attachment $path");
410             return false;
411         }
412
413         // Remove <script>s or htmlawed will dump their contents into output!
414         // Note: removing child nodes while iterating seems to mess things up,
415         // hence the double loop.
416         $scripts = array();
417         foreach ($dom->getElementsByTagName('script') as $script) {
418             $scripts[] = $script;
419         }
420         foreach ($scripts as $script) {
421             common_log(LOG_DEBUG, $script->textContent);
422             $script->parentNode->removeChild($script);
423         }
424
425         // Trim out everything outside the body...
426         $body = $dom->saveHTML();
427         $body = preg_replace('/^.*<body[^>]*>/is', '', $body);
428         $body = preg_replace('/<\/body[^>]*>.*$/is', '', $body);
429
430         require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
431         $config = array('safe' => 1,
432                         'deny_attribute' => 'id,style,on*',
433                         'comment' => 1); // remove comments
434         $scrubbed = htmLawed($body, $config);
435
436         return $scrubbed;
437     }
438
439     function showFallback()
440     {
441         // still needed: should show a link?
442     }
443 }