]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/attachmentlistitem.php
Show more links work with AJAX-retrieved HTML
[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('src' => $thumb->getUrl(), 'alt' => ''));
118                     } catch (UnsupportedMediaException $e) {
119                         // FIXME: Show a good representation of unsupported/unshowable images
120                     }
121                     break;
122
123                 case 'application/ogg':
124                     $arr  = array('type' => $this->attachment->mimetype,
125                         'data' => $this->attachment->url,
126                         'width' => 320,
127                         'height' => 240
128                     );
129                     $this->out->elementStart('object', $arr);
130                     $this->out->element('param', array('name' => 'src', 'value' => $this->attachment->url));
131                     $this->out->element('param', array('name' => 'autoStart', 'value' => 1));
132                     $this->out->elementEnd('object');
133                     break;
134
135                 case 'audio/ogg':
136                 case 'audio/x-speex':
137                 case 'video/mpeg':
138                 case 'audio/mpeg':
139                 case 'video/mp4':
140                 case 'video/ogg':
141                 case 'video/quicktime':
142                 case 'video/webm':
143                     $mediatype = common_get_mime_media($this->attachment->mimetype);
144                     try {
145                         $thumb = $this->attachment->getThumbnail();
146                         $poster = $thumb->getUrl();
147                         unset ($thumb);
148                     } catch (Exception $e) {
149                         $poster = null;
150                     }
151                     $this->out->elementStart($mediatype,
152                                         array('class'=>'attachment_player',
153                                             'poster'=>$poster,
154                                             'controls'=>'controls'));
155                     $this->out->element('source',
156                                         array('src'=>$this->attachment->url,
157                                             'type'=>$this->attachment->mimetype));
158                     $this->out->elementEnd($mediatype);
159                     break;
160
161                 case 'text/html':
162                     if (!empty($this->attachment->filename)
163                             && (StatusNet::isAjax() || common_config('attachments', 'show_html'))) {
164                         // Locally-uploaded HTML. Scrub and display inline.
165                         $this->showHtmlFile($this->attachment);
166                         break;
167                     }
168                     // Fall through to default.
169
170                 default:
171                     Event::handle('ShowUnsupportedAttachmentRepresentation', array($this->out, $this->attachment));
172                 }
173             } else {
174                 Event::handle('ShowUnsupportedAttachmentRepresentation', array($this->out, $this->attachment));
175             }
176         }
177         Event::handle('EndShowAttachmentRepresentation', array($this->out, $this->attachment));
178     }
179
180     protected function showHtmlFile(File $attachment)
181     {
182         $body = $this->scrubHtmlFile($attachment);
183         if ($body) {
184             $this->out->raw($body);
185         }
186     }
187
188     /**
189      * @return mixed false on failure, HTML fragment string on success
190      */
191     protected function scrubHtmlFile(File $attachment)
192     {
193         $path = File::path($attachment->filename);
194         if (!file_exists($path) || !is_readable($path)) {
195             common_log(LOG_ERR, "Missing local HTML attachment $path");
196             return false;
197         }
198         $raw = file_get_contents($path);
199
200         // Normalize...
201         $dom = new DOMDocument();
202         if(!$dom->loadHTML($raw)) {
203             common_log(LOG_ERR, "Bad HTML in local HTML attachment $path");
204             return false;
205         }
206
207         // Remove <script>s or htmlawed will dump their contents into output!
208         // Note: removing child nodes while iterating seems to mess things up,
209         // hence the double loop.
210         $scripts = array();
211         foreach ($dom->getElementsByTagName('script') as $script) {
212             $scripts[] = $script;
213         }
214         foreach ($scripts as $script) {
215             common_log(LOG_DEBUG, $script->textContent);
216             $script->parentNode->removeChild($script);
217         }
218
219         // Trim out everything outside the body...
220         $body = $dom->saveHTML();
221         $body = preg_replace('/^.*<body[^>]*>/is', '', $body);
222         $body = preg_replace('/<\/body[^>]*>.*$/is', '', $body);
223
224         require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
225         $config = array('safe' => 1,
226                         'deny_attribute' => 'id,style,on*',
227                         'comment' => 1); // remove comments
228         $scrubbed = htmLawed($body, $config);
229
230         return $scrubbed;
231     }
232
233     /**
234      * start a single notice.
235      *
236      * @return void
237      */
238     function showStart()
239     {
240         // XXX: RDFa
241         // TODO: add notice_type class e.g., notice_video, notice_image
242         $this->out->elementStart('li');
243     }
244
245     /**
246      * finish the notice
247      *
248      * Close the last elements in the notice list item
249      *
250      * @return void
251      */
252     function showEnd()
253     {
254         $this->out->elementEnd('li');
255     }
256 }