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 single notice
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).
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/
48 * @see ProfileNoticeListItem
50 class AttachmentListItem extends Widget
52 /** The attachment this item will show. */
54 var $attachment = null;
57 * @param File $attachment the attachment we will display
59 function __construct(File $attachment, $out=null)
61 parent::__construct($out);
62 $this->attachment = $attachment;
66 return $this->attachment->getTitle();
69 function linkTitle() {
70 return $this->title();
74 * recipe function for displaying a single notice.
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.
84 $this->showNoticeAttachment();
89 return array('class' => 'attachment',
90 'href' => $this->attachment->getUrl(),
91 'id' => 'attachment-' . $this->attachment->id,
92 'title' => $this->linkTitle());
96 $this->out->elementStart('a', $this->linkAttr());
97 $this->out->element('span', null, $this->linkTitle());
98 $this->showRepresentation();
99 $this->out->elementEnd('a');
102 function showNoticeAttachment()
107 function showRepresentation() {
108 if (Event::handle('StartShowAttachmentRepresentation', array($this->out, $this->attachment))) {
109 if (!empty($this->attachment->mimetype)) {
110 switch ($this->attachment->mimetype) {
116 // Tell getThumbnail that we can show an animated image if it has one (4th arg, "force_still")
117 $thumb = $this->attachment->getThumbnail(null, null, false, false);
118 $this->out->element('img', array('class'=>'u-photo', 'src' => $thumb->getUrl(), 'alt' => ''));
119 } catch (UseFileAsThumbnailException $e) {
120 $this->out->element('img', array('class'=>'u-photo', 'src' => $e->file->getUrl(), 'alt' => $e->file->title));
121 } catch (UnsupportedMediaException $e) {
122 // FIXME: Show a good representation of unsupported/unshowable images
126 case 'application/ogg':
127 $arr = array('type' => $this->attachment->mimetype,
128 'data' => $this->attachment->url,
132 $this->out->elementStart('object', $arr);
133 $this->out->element('param', array('name' => 'src', 'value' => $this->attachment->url));
134 $this->out->element('param', array('name' => 'autoStart', 'value' => 1));
135 $this->out->elementEnd('object');
139 case 'audio/x-speex':
144 case 'video/quicktime':
146 $mediatype = common_get_mime_media($this->attachment->mimetype);
148 $thumb = $this->attachment->getThumbnail();
149 $poster = $thumb->getUrl();
151 } catch (Exception $e) {
154 $this->out->elementStart($mediatype,
155 array('class'=>"attachment_player u-{$mediatype}",
157 'controls'=>'controls'));
158 $this->out->element('source',
159 array('src'=>$this->attachment->url,
160 'type'=>$this->attachment->mimetype));
161 $this->out->elementEnd($mediatype);
165 if (!empty($this->attachment->filename)
166 && (StatusNet::isAjax() || common_config('attachments', 'show_html'))) {
167 // Locally-uploaded HTML. Scrub and display inline.
168 $this->showHtmlFile($this->attachment);
171 // Fall through to default.
174 Event::handle('ShowUnsupportedAttachmentRepresentation', array($this->out, $this->attachment));
177 Event::handle('ShowUnsupportedAttachmentRepresentation', array($this->out, $this->attachment));
180 Event::handle('EndShowAttachmentRepresentation', array($this->out, $this->attachment));
183 protected function showHtmlFile(File $attachment)
185 $body = $this->scrubHtmlFile($attachment);
187 $this->out->raw($body);
192 * @return mixed false on failure, HTML fragment string on success
194 protected function scrubHtmlFile(File $attachment)
196 $path = File::path($attachment->filename);
197 if (!file_exists($path) || !is_readable($path)) {
198 common_log(LOG_ERR, "Missing local HTML attachment $path");
201 $raw = file_get_contents($path);
204 $dom = new DOMDocument();
205 if(!$dom->loadHTML($raw)) {
206 common_log(LOG_ERR, "Bad HTML in local HTML attachment $path");
210 // Remove <script>s or htmlawed will dump their contents into output!
211 // Note: removing child nodes while iterating seems to mess things up,
212 // hence the double loop.
214 foreach ($dom->getElementsByTagName('script') as $script) {
215 $scripts[] = $script;
217 foreach ($scripts as $script) {
218 common_debug($script->textContent);
219 $script->parentNode->removeChild($script);
222 // Trim out everything outside the body...
223 $body = $dom->saveHTML();
224 $body = preg_replace('/^.*<body[^>]*>/is', '', $body);
225 $body = preg_replace('/<\/body[^>]*>.*$/is', '', $body);
227 require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
228 $config = array('safe' => 1,
229 'deny_attribute' => 'id,style,on*',
230 'comment' => 1); // remove comments
231 $scrubbed = htmLawed($body, $config);
237 * start a single notice.
244 // TODO: add notice_type class e.g., notice_video, notice_image
245 $this->out->elementStart('li');
251 * Close the last elements in the notice list item
257 $this->out->elementEnd('li');