]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/attachmentlist.php
Testing... using photo info for temp thumbnails
[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->out->elementStart('dl', array('id' =>'attachments',
83                                              'class' => 'entry-content'));
84         // TRANS: DT element label in attachment list.
85         $this->out->element('dt', null, _('Attachments'));
86         $this->out->elementStart('dd');
87         $this->out->elementStart('ol', array('class' => 'attachments'));
88
89         foreach ($att as $n=>$attachment) {
90             $item = $this->newListItem($attachment);
91             $item->show();
92         }
93
94         $this->out->elementEnd('dd');
95         $this->out->elementEnd('ol');
96         $this->out->elementEnd('dl');
97
98         return count($att);
99     }
100
101     /**
102      * returns a new list item for the current notice
103      *
104      * Recipe (factory?) method; overridden by sub-classes to give
105      * a different list item class.
106      *
107      * @param Notice $notice the current notice
108      *
109      * @return NoticeListItem a list item for displaying the notice
110      */
111     function newListItem($attachment)
112     {
113         return new AttachmentListItem($attachment, $this->out);
114     }
115 }
116
117 /**
118  * widget for displaying a single notice
119  *
120  * This widget has the core smarts for showing a single notice: what to display,
121  * where, and under which circumstances. Its key method is show(); this is a recipe
122  * that calls all the other show*() methods to build up a single notice. The
123  * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
124  * author info (since that's implicit by the data in the page).
125  *
126  * @category UI
127  * @package  StatusNet
128  * @author   Evan Prodromou <evan@status.net>
129  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
130  * @link     http://status.net/
131  * @see      NoticeList
132  * @see      ProfileNoticeListItem
133  */
134 class AttachmentListItem extends Widget
135 {
136     /** The attachment this item will show. */
137
138     var $attachment = null;
139
140     var $oembed = null;
141
142     /**
143      * constructor
144      *
145      * Also initializes the profile attribute.
146      *
147      * @param Notice $notice The notice we'll display
148      */
149     function __construct($attachment, $out=null)
150     {
151         parent::__construct($out);
152         $this->attachment  = $attachment;
153         $this->oembed = File_oembed::staticGet('file_id', $this->attachment->id);
154     }
155
156     function title() {
157         if (empty($this->attachment->title)) {
158             if (empty($this->oembed->title)) {
159                 $title = $this->attachment->url;
160             } else {
161                 $title = $this->oembed->title;
162             }
163         } else {
164             $title = $this->attachment->title;
165         }
166
167         return $title;
168     }
169
170     function linkTitle() {
171         return $this->title();
172     }
173
174     /**
175      * recipe function for displaying a single notice.
176      *
177      * This uses all the other methods to correctly display a notice. Override
178      * it or one of the others to fine-tune the output.
179      *
180      * @return void
181      */
182     function show()
183     {
184         if ($this->attachment->isEnclosure()) {
185             $this->showStart();
186             $this->showNoticeAttachment();
187             $this->showEnd();
188         }
189     }
190
191     function linkAttr() {
192         return array('class' => 'attachment', 'href' => $this->attachment->url, 'id' => 'attachment-' . $this->attachment->id);
193     }
194
195     function showLink() {
196         $this->out->elementStart('a', $this->linkAttr());
197         $this->out->element('span', null, $this->linkTitle());
198         $this->showRepresentation();
199         $this->out->elementEnd('a');
200     }
201
202     function showNoticeAttachment()
203     {
204         $this->showLink();
205     }
206
207     function showRepresentation() {
208         $thumb = $this->getThumbInfo();
209         if ($thumb) {
210             $thumb = $this->sizeThumb($thumb);
211             $this->out->element('img', array('alt' => '', 'src' => $thumb->url, 'width' => $thumb->width, 'height' => $thumb->height));
212         }
213     }
214
215     /**
216      * Pull a thumbnail image reference for the given file.
217      * In order we check:
218      *  1) file_thumbnail table (thumbnails found via oEmbed)
219      *  2) image URL from direct dereference or oEmbed 'photo' type URL
220      *  3) ???
221      *
222      * @return mixed object with (url, width, height) properties, or false
223      */
224     function getThumbInfo()
225     {
226         $thumbnail = File_thumbnail::staticGet('file_id', $this->attachment->id);
227         if ($thumbnail) {
228             return $thumbnail;
229         }
230         $enc = $this->attachment->getEnclosure();
231         if ($enc) {
232             switch ($enc->mimetype) {
233                 case 'image/gif':
234                 case 'image/png':
235                 case 'image/jpg':
236                 case 'image/jpeg':
237                     $thumb = (object)array();
238                     $thumb->url = $enc->url;
239                     $thumb->width = 100;
240                     $thumb->height = 75; // @fixme
241                     return $thumb;
242             }
243         }
244         return false;
245     }
246
247     function sizeThumb($thumbnail) {
248         $maxWidth = 100;
249         $maxHeight = 75;
250         if ($thumbnail->width > $maxWidth) {
251             $thumb = clone($thumbnail);
252             $thumb->width = $maxWidth;
253             $thumb->height = intval($thumbnail->height * $maxWidth / $thumbnail->width);
254             return $thumb;
255         } else {
256             return $thumbnail;
257         }
258     }
259
260     /**
261      * start a single notice.
262      *
263      * @return void
264      */
265     function showStart()
266     {
267         // XXX: RDFa
268         // TODO: add notice_type class e.g., notice_video, notice_image
269         $this->out->elementStart('li');
270     }
271
272     /**
273      * finish the notice
274      *
275      * Close the last elements in the notice list item
276      *
277      * @return void
278      */
279     function showEnd()
280     {
281         $this->out->elementEnd('li');
282     }
283 }
284
285 /**
286  * used for one-off attachment action
287  */
288 class Attachment extends AttachmentListItem
289 {
290     function showLink() {
291         $this->out->elementStart('div', array('id' => 'attachment_view',
292                                               'class' => 'hentry'));
293         $this->out->elementStart('div', 'entry-title');
294         $this->out->element('a', $this->linkAttr(), $this->linkTitle());
295         $this->out->elementEnd('div');
296
297         $this->out->elementStart('div', 'entry-content');
298         $this->showRepresentation();
299         $this->out->elementEnd('div');
300
301         if (!empty($this->oembed->author_name) || !empty($this->oembed->provider)) {
302             $this->out->elementStart('div', array('id' => 'oembed_info',
303                                                   'class' => 'entry-content'));
304             if (!empty($this->oembed->author_name)) {
305                 $this->out->elementStart('dl', 'vcard author');
306                 // TRANS: DT element label in attachment list item.
307                 $this->out->element('dt', null, _('Author'));
308                 $this->out->elementStart('dd', 'fn');
309                 if (empty($this->oembed->author_url)) {
310                     $this->out->text($this->oembed->author_name);
311                 } else {
312                     $this->out->element('a', array('href' => $this->oembed->author_url,
313                                                    'class' => 'url'), $this->oembed->author_name);
314                 }
315                 $this->out->elementEnd('dd');
316                 $this->out->elementEnd('dl');
317             }
318             if (!empty($this->oembed->provider)) {
319                 $this->out->elementStart('dl', 'vcard');
320                 // TRANS: DT element label in attachment list item.
321                 $this->out->element('dt', null, _('Provider'));
322                 $this->out->elementStart('dd', 'fn');
323                 if (empty($this->oembed->provider_url)) {
324                     $this->out->text($this->oembed->provider);
325                 } else {
326                     $this->out->element('a', array('href' => $this->oembed->provider_url,
327                                                    'class' => 'url'), $this->oembed->provider);
328                 }
329                 $this->out->elementEnd('dd');
330                 $this->out->elementEnd('dl');
331             }
332             $this->out->elementEnd('div');
333         }
334         $this->out->elementEnd('div');
335     }
336
337     function show() {
338         $this->showNoticeAttachment();
339     }
340
341     function linkAttr() {
342         return array('rel' => 'external', 'href' => $this->attachment->url);
343     }
344
345     function linkTitle() {
346         return $this->attachment->url;
347     }
348
349     function showRepresentation() {
350         if (empty($this->oembed->type)) {
351             if (empty($this->attachment->mimetype)) {
352                 $this->showFallback();
353             } else {
354                 switch ($this->attachment->mimetype) {
355                 case 'image/gif':
356                 case 'image/png':
357                 case 'image/jpg':
358                 case 'image/jpeg':
359                     $this->out->element('img', array('src' => $this->attachment->url, 'alt' => 'alt'));
360                     break;
361
362                 case 'application/ogg':
363                 case 'audio/x-speex':
364                 case 'video/mpeg':
365                 case 'audio/mpeg':
366                 case 'video/mp4':
367                 case 'video/quicktime':
368                     $arr  = array('type' => $this->attachment->mimetype,
369                         'data' => $this->attachment->url,
370                         'width' => 320,
371                         'height' => 240
372                     );
373                     $this->out->elementStart('object', $arr);
374                     $this->out->element('param', array('name' => 'src', 'value' => $this->attachment->url));
375                     $this->out->element('param', array('name' => 'autoStart', 'value' => 1));
376                     $this->out->elementEnd('object');
377                     break;
378
379                 case 'text/html':
380                     if ($this->attachment->filename) {
381                         // Locally-uploaded HTML. Scrub and display inline.
382                         $this->showHtmlFile($this->attachment);
383                         break;
384                     }
385                     // Fall through to default.
386
387                 default:
388                     $this->showFallback();
389                 }
390             }
391         } else {
392             switch ($this->oembed->type) {
393             case 'rich':
394             case 'video':
395             case 'link':
396                 if (!empty($this->oembed->html)) {
397                     require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
398                     $config = array(
399                         'safe'=>1,
400                         'elements'=>'*+object+embed');
401                     $this->out->raw(htmLawed($this->oembed->html,$config));
402                     //$this->out->raw($this->oembed->html);
403                 }
404                 break;
405
406             case 'photo':
407                 $this->out->element('img', array('src' => $this->oembed->url, 'width' => $this->oembed->width, 'height' => $this->oembed->height, 'alt' => 'alt'));
408                 break;
409
410             default:
411                 $this->showFallback();
412             }
413         }
414     }
415
416     protected function showHtmlFile(File $attachment)
417     {
418         $body = $this->scrubHtmlFile($attachment);
419         if ($body) {
420             $this->out->raw($body);
421         }
422     }
423
424     /**
425      * @return mixed false on failure, HTML fragment string on success
426      */
427     protected function scrubHtmlFile(File $attachment)
428     {
429         $path = File::path($attachment->filename);
430         if (!file_exists($path) || !is_readable($path)) {
431             common_log(LOG_ERR, "Missing local HTML attachment $path");
432             return false;
433         }
434         $raw = file_get_contents($path);
435
436         // Normalize...
437         $dom = new DOMDocument();
438         if(!$dom->loadHTML($raw)) {
439             common_log(LOG_ERR, "Bad HTML in local HTML attachment $path");
440             return false;
441         }
442
443         // Remove <script>s or htmlawed will dump their contents into output!
444         // Note: removing child nodes while iterating seems to mess things up,
445         // hence the double loop.
446         $scripts = array();
447         foreach ($dom->getElementsByTagName('script') as $script) {
448             $scripts[] = $script;
449         }
450         foreach ($scripts as $script) {
451             common_log(LOG_DEBUG, $script->textContent);
452             $script->parentNode->removeChild($script);
453         }
454
455         // Trim out everything outside the body...
456         $body = $dom->saveHTML();
457         $body = preg_replace('/^.*<body[^>]*>/is', '', $body);
458         $body = preg_replace('/<\/body[^>]*>.*$/is', '', $body);
459
460         require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
461         $config = array('safe' => 1,
462                         'deny_attribute' => 'id,style,on*',
463                         'comment' => 1); // remove comments
464         $scrubbed = htmLawed($body, $config);
465
466         return $scrubbed;
467     }
468 }