]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/attachmentlistitem.php
We don't have the thumbnail title in attachment list
[quix0rs-gnu-social.git] / lib / attachmentlistitem.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('GNUSOCIAL')) { exit(1); }
32
33 /**
34  * widget for displaying a single notice
35  *
36  * This widget has the core smarts for showing a single notice: what to display,
37  * where, and under which circumstances. Its key method is show(); this is a recipe
38  * that calls all the other show*() methods to build up a single notice. The
39  * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
40  * author info (since that's implicit by the data in the page).
41  *
42  * @category UI
43  * @package  StatusNet
44  * @author   Evan Prodromou <evan@status.net>
45  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
46  * @link     http://status.net/
47  * @see      NoticeList
48  * @see      ProfileNoticeListItem
49  */
50 class AttachmentListItem extends Widget
51 {
52     /** The attachment this item will show. */
53
54     var $attachment = null;
55
56     /**
57      * @param File $attachment the attachment we will display
58      */
59     function __construct(File $attachment, $out=null)
60     {
61         parent::__construct($out);
62         $this->attachment  = $attachment;
63     }
64
65     function title() {
66         return $this->attachment->title ?: $this->attachment->filename;
67     }
68
69     function linkTitle() {
70         return $this->title();
71     }
72
73     /**
74      * recipe function for displaying a single notice.
75      *
76      * This uses all the other methods to correctly display a notice. Override
77      * it or one of the others to fine-tune the output.
78      *
79      * @return void
80      */
81     function show()
82     {
83         $this->showStart();
84         $this->showNoticeAttachment();
85         $this->showEnd();
86     }
87
88     function linkAttr() {
89         return array('class' => 'attachment',
90                      'href' => $this->attachment->getUrl(),
91                      'id' => 'attachment-' . $this->attachment->id,
92                      'title' => $this->linkTitle());
93     }
94
95     function showLink() {
96         $this->out->elementStart('a', $this->linkAttr());
97         $this->out->element('span', null, $this->linkTitle());
98         $this->showRepresentation();
99         $this->out->elementEnd('a');
100     }
101
102     function showNoticeAttachment()
103     {
104         $this->showLink();
105     }
106
107     function showRepresentation() {
108         if (Event::handle('StartShowAttachmentRepresentation', array($this->out, $this->attachment))) {
109             if (!empty($this->attachment->mimetype)) {
110                 switch ($this->attachment->mimetype) {
111                 case 'image/gif':
112                 case 'image/png':
113                 case 'image/jpg':
114                 case 'image/jpeg':
115                     try {
116                         $thumb = $this->attachment->getThumbnail();
117                         $this->out->element('img', array('class'=>'u-photo', 'src' => $thumb->getUrl(), 'alt' => ''));
118                     } catch (UseFileAsThumbnailException $e) {
119                         $this->out->element('img', array('class'=>'u-photo', 'src' => $e->file->getUrl(), 'alt' => $e->file->title));
120                     } catch (UnsupportedMediaException $e) {
121                         // FIXME: Show a good representation of unsupported/unshowable images
122                     }
123                     break;
124
125                 case 'application/ogg':
126                     $arr  = array('type' => $this->attachment->mimetype,
127                         'data' => $this->attachment->url,
128                         'width' => 320,
129                         'height' => 240
130                     );
131                     $this->out->elementStart('object', $arr);
132                     $this->out->element('param', array('name' => 'src', 'value' => $this->attachment->url));
133                     $this->out->element('param', array('name' => 'autoStart', 'value' => 1));
134                     $this->out->elementEnd('object');
135                     break;
136
137                 case 'audio/ogg':
138                 case 'audio/x-speex':
139                 case 'video/mpeg':
140                 case 'audio/mpeg':
141                 case 'video/mp4':
142                 case 'video/ogg':
143                 case 'video/quicktime':
144                 case 'video/webm':
145                     $mediatype = common_get_mime_media($this->attachment->mimetype);
146                     try {
147                         $thumb = $this->attachment->getThumbnail();
148                         $poster = $thumb->getUrl();
149                         unset ($thumb);
150                     } catch (Exception $e) {
151                         $poster = null;
152                     }
153                     $this->out->elementStart($mediatype,
154                                         array('class'=>"attachment_player u-{$mediatype}",
155                                             'poster'=>$poster,
156                                             'controls'=>'controls'));
157                     $this->out->element('source',
158                                         array('src'=>$this->attachment->url,
159                                             'type'=>$this->attachment->mimetype));
160                     $this->out->elementEnd($mediatype);
161                     break;
162
163                 case 'text/html':
164                     if (!empty($this->attachment->filename)
165                             && (StatusNet::isAjax() || common_config('attachments', 'show_html'))) {
166                         // Locally-uploaded HTML. Scrub and display inline.
167                         $this->showHtmlFile($this->attachment);
168                         break;
169                     }
170                     // Fall through to default.
171
172                 default:
173                     Event::handle('ShowUnsupportedAttachmentRepresentation', array($this->out, $this->attachment));
174                 }
175             } else {
176                 Event::handle('ShowUnsupportedAttachmentRepresentation', array($this->out, $this->attachment));
177             }
178         }
179         Event::handle('EndShowAttachmentRepresentation', array($this->out, $this->attachment));
180     }
181
182     protected function showHtmlFile(File $attachment)
183     {
184         $body = $this->scrubHtmlFile($attachment);
185         if ($body) {
186             $this->out->raw($body);
187         }
188     }
189
190     /**
191      * @return mixed false on failure, HTML fragment string on success
192      */
193     protected function scrubHtmlFile(File $attachment)
194     {
195         $path = File::path($attachment->filename);
196         if (!file_exists($path) || !is_readable($path)) {
197             common_log(LOG_ERR, "Missing local HTML attachment $path");
198             return false;
199         }
200         $raw = file_get_contents($path);
201
202         // Normalize...
203         $dom = new DOMDocument();
204         if(!$dom->loadHTML($raw)) {
205             common_log(LOG_ERR, "Bad HTML in local HTML attachment $path");
206             return false;
207         }
208
209         // Remove <script>s or htmlawed will dump their contents into output!
210         // Note: removing child nodes while iterating seems to mess things up,
211         // hence the double loop.
212         $scripts = array();
213         foreach ($dom->getElementsByTagName('script') as $script) {
214             $scripts[] = $script;
215         }
216         foreach ($scripts as $script) {
217             common_log(LOG_DEBUG, $script->textContent);
218             $script->parentNode->removeChild($script);
219         }
220
221         // Trim out everything outside the body...
222         $body = $dom->saveHTML();
223         $body = preg_replace('/^.*<body[^>]*>/is', '', $body);
224         $body = preg_replace('/<\/body[^>]*>.*$/is', '', $body);
225
226         require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
227         $config = array('safe' => 1,
228                         'deny_attribute' => 'id,style,on*',
229                         'comment' => 1); // remove comments
230         $scrubbed = htmLawed($body, $config);
231
232         return $scrubbed;
233     }
234
235     /**
236      * start a single notice.
237      *
238      * @return void
239      */
240     function showStart()
241     {
242         // XXX: RDFa
243         // TODO: add notice_type class e.g., notice_video, notice_image
244         $this->out->elementStart('li');
245     }
246
247     /**
248      * finish the notice
249      *
250      * Close the last elements in the notice list item
251      *
252      * @return void
253      */
254     function showEnd()
255     {
256         $this->out->elementEnd('li');
257     }
258 }