123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- <?php
- if (!defined('GNUSOCIAL')) { exit(1); }
- class AttachmentListItem extends Widget
- {
-
- var $attachment = null;
-
- function __construct(File $attachment, $out=null)
- {
- parent::__construct($out);
- $this->attachment = $attachment;
- }
- function title() {
- return $this->attachment->getTitle() ?: MediaFile::getDisplayName($this->attachment);
- }
- function linkTitle() {
- return $this->title();
- }
-
- function show()
- {
- $this->showStart();
- try {
- $this->showNoticeAttachment();
- } catch (Exception $e) {
- $this->element('div', ['class'=>'error'], $e->getMessage());
- common_debug($e->getMessage());
- }
- $this->showEnd();
- }
- function linkAttr() {
- return array(
- 'class' => 'u-url',
- 'href' => $this->attachment->getAttachmentUrl(),
- 'title' => $this->linkTitle());
- }
- function showNoticeAttachment()
- {
- $this->showRepresentation();
- }
- function showRepresentation() {
- $enclosure = $this->attachment->getEnclosure();
- if (Event::handle('StartShowAttachmentRepresentation', array($this->out, $this->attachment))) {
- $this->out->elementStart('label');
- $this->out->element('a', $this->linkAttr(), $this->title());
- $this->out->elementEnd('label');
- $this->out->element('br');
- if (!empty($enclosure->mimetype)) {
-
- $thumb = null;
- try {
-
- $thumb = $this->attachment->getThumbnail(null, null, false, false);
- } catch (UseFileAsThumbnailException $e) {
- $thumb = null;
- } catch (UnsupportedMediaException $e) {
-
- $thumb = null;
- }
-
- $mediatype = common_get_mime_media($enclosure->mimetype);
-
-
- if ($this->attachment->mimetype === 'application/ogg') {
- $mediatype = 'video';
- }
-
- if (!in_array($mediatype, ['image','audio','video']) && $thumb instanceof File_thumbnail) {
- $mediatype = 'image';
- }
- switch ($mediatype) {
-
- case 'image':
- if ($thumb instanceof File_thumbnail) {
- $this->out->element('img', $thumb->getHtmlAttrs(['class'=>'u-photo', 'alt' => '']));
- } else {
- try {
-
- $this->out->element('img', ['class'=>'u-photo',
- 'src'=>$this->attachment->getUrl(true),
- 'alt' => $this->attachment->getTitle()]);
- } catch (FileNotStoredLocallyException $e) {
- $url = $e->file->getUrl(false);
- $this->out->element('a', ['href'=>$url, 'rel'=>'external'], $url);
- }
- }
- unset($thumb);
- break;
-
- case 'audio':
- case 'video':
- if ($thumb instanceof File_thumbnail) {
- $poster = $thumb->getUrl();
- unset($thumb);
- } else {
- $poster = null;
- }
- $this->out->elementStart($mediatype,
- array('class'=>"attachment_player u-{$mediatype}",
- 'poster'=>$poster,
- 'controls'=>'controls'));
- $this->out->element('source',
- array('src'=>$this->attachment->getUrl(),
- 'type'=>$this->attachment->mimetype));
- $this->out->elementEnd($mediatype);
- break;
- default:
- unset($thumb);
- switch (common_bare_mime($this->attachment->mimetype)) {
- case 'text/plain':
- $this->element('div', ['class'=>'e-content plaintext'],
- file_get_contents($this->attachment->getPath()));
- break;
- case 'text/html':
- if (!empty($this->attachment->filename)
- && (GNUsocial::isAjax() || common_config('attachments', 'show_html'))) {
-
- $this->showHtmlFile($this->attachment);
- break;
- }
-
- default:
- Event::handle('ShowUnsupportedAttachmentRepresentation', array($this->out, $this->attachment));
- }
- }
- } else {
- Event::handle('ShowUnsupportedAttachmentRepresentation', array($this->out, $this->attachment));
- }
- }
- Event::handle('EndShowAttachmentRepresentation', array($this->out, $this->attachment));
- }
- protected function showHtmlFile(File $attachment)
- {
- $body = $this->scrubHtmlFile($attachment);
- if ($body) {
- $this->out->raw($body);
- }
- }
-
- protected function scrubHtmlFile(File $attachment)
- {
- $path = $attachment->getPath();
- $raw = file_get_contents($path);
-
- $dom = new DOMDocument();
- if(!$dom->loadHTML($raw)) {
- common_log(LOG_ERR, "Bad HTML in local HTML attachment $path");
- return false;
- }
-
-
-
- $scripts = array();
- foreach ($dom->getElementsByTagName('script') as $script) {
- $scripts[] = $script;
- }
- foreach ($scripts as $script) {
- common_log(LOG_DEBUG, $script->textContent);
- $script->parentNode->removeChild($script);
- }
-
- $body = $dom->saveHTML();
- $body = preg_replace('/^.*<body[^>]*>/is', '', $body);
- $body = preg_replace('/<\/body[^>]*>.*$/is', '', $body);
- require_once INSTALLDIR.'/extlib/HTMLPurifier/HTMLPurifier.auto.php';
- $purifier = new HTMLPurifier();
- return $purifier->purify($body);
- }
-
- function showStart()
- {
-
-
- $this->out->elementStart('li');
- }
-
- function showEnd()
- {
- $this->out->elementEnd('li');
- }
- }
|