3 * StatusNet, the distributed open-source microblogging tool
5 * widget for displaying a list of notice attachments
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.
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.
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/>.
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/
31 if (!defined('GNUSOCIAL')) { exit(1); }
34 * widget for displaying a list of notice attachments
36 * There are a number of actions that display a list of notices, in
37 * reverse chronological order. This widget abstracts out most of the
38 * code for UI for notice lists. It's overridden to hide some
39 * data for e.g. the profile page.
43 * @author Evan Prodromou <evan@status.net>
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45 * @link http://status.net/
48 * @see ProfileNoticeList
50 class AttachmentList extends Widget
52 /** the current stream of notices being displayed. */
59 * @param Notice $notice stream of notices from DB_DataObject
61 function __construct($notice, $out=null)
63 parent::__construct($out);
64 $this->notice = $notice;
68 * show the list of attachments
70 * "Uses up" the stream by looping through it. So, probably can't
71 * be called twice on the same list.
73 * @return int count of items listed.
77 $att = $this->notice->attachments();
78 if (empty($att)) return 0;
79 $this->showListStart();
81 foreach ($att as $n=>$attachment) {
82 $item = $this->newListItem($attachment);
91 function showListStart()
93 $this->out->elementStart('ol', array('class' => 'attachments entry-content'));
96 function showListEnd()
98 $this->out->elementEnd('ol');
102 * returns a new list item for the current attachment
104 * @param File $attachment the current attachment
106 * @return AttachmentListItem a list item for displaying the attachment
108 function newListItem(File $attachment)
110 return new AttachmentListItem($attachment, $this->out);
115 * widget for displaying a single notice
117 * This widget has the core smarts for showing a single notice: what to display,
118 * where, and under which circumstances. Its key method is show(); this is a recipe
119 * that calls all the other show*() methods to build up a single notice. The
120 * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
121 * author info (since that's implicit by the data in the page).
125 * @author Evan Prodromou <evan@status.net>
126 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
127 * @link http://status.net/
129 * @see ProfileNoticeListItem
131 class AttachmentListItem extends Widget
133 /** The attachment this item will show. */
135 var $attachment = null;
140 * @param File $attachment the attachment we will display
142 function __construct(File $attachment, $out=null)
144 parent::__construct($out);
145 $this->attachment = $attachment;
146 $this->oembed = File_oembed::getKV('file_id', $this->attachment->id);
150 if (empty($this->attachment->title)) {
151 if (empty($this->oembed->title)) {
152 $title = $this->attachment->filename;
154 $title = $this->oembed->title;
157 $title = $this->attachment->title;
163 function linkTitle() {
164 return $this->title();
168 * recipe function for displaying a single notice.
170 * This uses all the other methods to correctly display a notice. Override
171 * it or one of the others to fine-tune the output.
178 $this->showNoticeAttachment();
182 function linkAttr() {
183 return array('class' => 'attachment',
184 'href' => $this->attachment->url,
185 'id' => 'attachment-' . $this->attachment->id,
186 'title' => $this->linkTitle());
189 function showLink() {
190 $this->out->elementStart('a', $this->linkAttr());
191 $this->out->element('span', null, $this->linkTitle());
192 $this->showRepresentation();
193 $this->out->elementEnd('a');
196 function showNoticeAttachment()
201 function showRepresentation() {
203 $thumb = $this->attachment->getThumbnail();
204 $this->out->element('img', array('alt' => '', 'src' => $thumb->getUrl(), 'width' => $thumb->width, 'height' => $thumb->height));
205 } catch (UnsupportedMediaException $e) {
206 // Image representation unavailable
211 * start a single notice.
218 // TODO: add notice_type class e.g., notice_video, notice_image
219 $this->out->elementStart('li');
225 * Close the last elements in the notice list item
231 $this->out->elementEnd('li');
236 * used for one-off attachment action
238 class Attachment extends AttachmentListItem
240 function showLink() {
241 $this->out->elementStart('div', array('id' => 'attachment_view',
242 'class' => 'hentry'));
243 $this->out->elementStart('div', 'entry-title');
244 $this->out->element('a', $this->linkAttr(), $this->linkTitle());
245 $this->out->elementEnd('div');
247 $this->out->elementStart('div', 'entry-content');
248 $this->showRepresentation();
249 $this->out->elementEnd('div');
251 if (!empty($this->oembed->author_name) || !empty($this->oembed->provider)) {
252 $this->out->elementStart('div', array('id' => 'oembed_info',
253 'class' => 'entry-content'));
254 if (!empty($this->oembed->author_name)) {
255 $this->out->elementStart('div', 'fn vcard author');
256 if (empty($this->oembed->author_url)) {
257 $this->out->text($this->oembed->author_name);
259 $this->out->element('a', array('href' => $this->oembed->author_url,
260 'class' => 'url'), $this->oembed->author_name);
263 if (!empty($this->oembed->provider)) {
264 $this->out->elementStart('div', 'fn vcard');
265 if (empty($this->oembed->provider_url)) {
266 $this->out->text($this->oembed->provider);
268 $this->out->element('a', array('href' => $this->oembed->provider_url,
269 'class' => 'url'), $this->oembed->provider);
272 $this->out->elementEnd('div');
274 $this->out->elementEnd('div');
278 $this->showNoticeAttachment();
281 function linkAttr() {
282 return array('rel' => 'external', 'href' => $this->attachment->url);
285 function linkTitle() {
286 return $this->attachment->url;
289 function showRepresentation() {
290 if (Event::handle('StartShowAttachmentRepresentation', array($this->out, $this->attachment))) {
291 if (!empty($this->oembed->type)) {
292 switch ($this->oembed->type) {
296 if (!empty($this->oembed->html)) {
297 require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
300 'elements'=>'*+object+embed');
301 $this->out->raw(htmLawed($this->oembed->html,$config));
306 $this->out->element('img', array('src' => $this->oembed->url, 'width' => $this->oembed->width, 'height' => $this->oembed->height, 'alt' => 'alt'));
310 Event::handle('ShowUnsupportedAttachmentRepresentation', array($this->out, $this->attachment));
312 } elseif (!empty($this->attachment->mimetype)) {
313 switch ($this->attachment->mimetype) {
318 $this->out->element('img', array('src' => $this->attachment->url, 'alt' => 'alt'));
321 case 'application/ogg':
322 $arr = array('type' => $this->attachment->mimetype,
323 'data' => $this->attachment->url,
327 $this->out->elementStart('object', $arr);
328 $this->out->element('param', array('name' => 'src', 'value' => $this->attachment->url));
329 $this->out->element('param', array('name' => 'autoStart', 'value' => 1));
330 $this->out->elementEnd('object');
334 case 'audio/x-speex':
339 case 'video/quicktime':
341 $mediatype = common_get_mime_media($this->attachment->mimetype);
343 $thumb = $this->attachment->getThumbnail();
344 $poster = $thumb->getUrl();
346 } catch (Exception $e) {
349 $this->out->elementStart($mediatype,
350 array('class'=>'attachment_player',
352 'controls'=>'controls'));
353 $this->out->element('source',
354 array('src'=>$this->attachment->url,
355 'type'=>$this->attachment->mimetype));
356 $this->out->elementEnd($mediatype);
360 if ($this->attachment->filename) {
361 // Locally-uploaded HTML. Scrub and display inline.
362 $this->showHtmlFile($this->attachment);
365 // Fall through to default.
368 Event::handle('ShowUnsupportedAttachmentRepresentation', array($this->out, $this->attachment));
371 Event::handle('ShowUnsupportedAttachmentRepresentation', array($this->out, $this->attachment));
374 Event::handle('EndShowAttachmentRepresentation', array($this->out, $this->attachment));
377 protected function showHtmlFile(File $attachment)
379 $body = $this->scrubHtmlFile($attachment);
381 $this->out->raw($body);
386 * @return mixed false on failure, HTML fragment string on success
388 protected function scrubHtmlFile(File $attachment)
390 $path = File::path($attachment->filename);
391 if (!file_exists($path) || !is_readable($path)) {
392 common_log(LOG_ERR, "Missing local HTML attachment $path");
395 $raw = file_get_contents($path);
398 $dom = new DOMDocument();
399 if(!$dom->loadHTML($raw)) {
400 common_log(LOG_ERR, "Bad HTML in local HTML attachment $path");
404 // Remove <script>s or htmlawed will dump their contents into output!
405 // Note: removing child nodes while iterating seems to mess things up,
406 // hence the double loop.
408 foreach ($dom->getElementsByTagName('script') as $script) {
409 $scripts[] = $script;
411 foreach ($scripts as $script) {
412 common_log(LOG_DEBUG, $script->textContent);
413 $script->parentNode->removeChild($script);
416 // Trim out everything outside the body...
417 $body = $dom->saveHTML();
418 $body = preg_replace('/^.*<body[^>]*>/is', '', $body);
419 $body = preg_replace('/<\/body[^>]*>.*$/is', '', $body);
421 require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
422 $config = array('safe' => 1,
423 'deny_attribute' => 'id,style,on*',
424 'comment' => 1); // remove comments
425 $scrubbed = htmLawed($body, $config);