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('STATUSNET') && !defined('LACONICA')) {
36 * widget for displaying a list of notice attachments
38 * There are a number of actions that display a list of notices, in
39 * reverse chronological order. This widget abstracts out most of the
40 * code for UI for notice lists. It's overridden to hide some
41 * data for e.g. the profile page.
45 * @author Evan Prodromou <evan@status.net>
46 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47 * @link http://status.net/
50 * @see ProfileNoticeList
53 class AttachmentList extends Widget
55 /** the current stream of notices being displayed. */
62 * @param Notice $notice stream of notices from DB_DataObject
65 function __construct($notice, $out=null)
67 parent::__construct($out);
68 $this->notice = $notice;
72 * show the list of notices
74 * "Uses up" the stream by looping through it. So, probably can't
75 * be called twice on the same list.
77 * @return int count of notices listed.
83 $att = $atts->getAttachments($this->notice->id);
84 if (empty($att)) return 0;
85 $this->out->elementStart('dl', array('id' =>'attachments',
86 'class' => 'entry-content'));
87 // TRANS: DT element label in attachment list.
88 $this->out->element('dt', null, _('Attachments'));
89 $this->out->elementStart('dd');
90 $this->out->elementStart('ol', array('class' => 'attachments'));
92 foreach ($att as $n=>$attachment) {
93 $item = $this->newListItem($attachment);
97 $this->out->elementEnd('dd');
98 $this->out->elementEnd('ol');
99 $this->out->elementEnd('dl');
105 * returns a new list item for the current notice
107 * Recipe (factory?) method; overridden by sub-classes to give
108 * a different list item class.
110 * @param Notice $notice the current notice
112 * @return NoticeListItem a list item for displaying the notice
115 function newListItem($attachment)
117 return new AttachmentListItem($attachment, $this->out);
122 * widget for displaying a single notice
124 * This widget has the core smarts for showing a single notice: what to display,
125 * where, and under which circumstances. Its key method is show(); this is a recipe
126 * that calls all the other show*() methods to build up a single notice. The
127 * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
128 * author info (since that's implicit by the data in the page).
132 * @author Evan Prodromou <evan@status.net>
133 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
134 * @link http://status.net/
136 * @see ProfileNoticeListItem
139 class AttachmentListItem extends Widget
141 /** The attachment this item will show. */
143 var $attachment = null;
150 * Also initializes the profile attribute.
152 * @param Notice $notice The notice we'll display
155 function __construct($attachment, $out=null)
157 parent::__construct($out);
158 $this->attachment = $attachment;
159 $this->oembed = File_oembed::staticGet('file_id', $this->attachment->id);
163 if (empty($this->attachment->title)) {
164 if (empty($this->oembed->title)) {
165 $title = $this->attachment->url;
167 $title = $this->oembed->title;
170 $title = $this->attachment->title;
176 function linkTitle() {
177 return $this->title();
181 * recipe function for displaying a single notice.
183 * This uses all the other methods to correctly display a notice. Override
184 * it or one of the others to fine-tune the output.
192 $this->showNoticeAttachment();
196 function linkAttr() {
197 return array('class' => 'attachment', 'href' => $this->attachment->url, 'id' => 'attachment-' . $this->attachment->id);
200 function showLink() {
201 $this->out->elementStart('a', $this->linkAttr());
202 $this->out->element('span', null, $this->linkTitle());
203 $this->showRepresentation();
204 $this->out->elementEnd('a');
207 function showNoticeAttachment()
212 function showRepresentation() {
213 $thumbnail = File_thumbnail::staticGet('file_id', $this->attachment->id);
214 if (!empty($thumbnail)) {
215 $this->out->element('img', array('alt' => '', 'src' => $thumbnail->url, 'width' => $thumbnail->width, 'height' => $thumbnail->height));
220 * start a single notice.
228 // TODO: add notice_type class e.g., notice_video, notice_image
229 $this->out->elementStart('li');
235 * Close the last elements in the notice list item
242 $this->out->elementEnd('li');
246 class Attachment extends AttachmentListItem
248 function showLink() {
249 $this->out->elementStart('div', array('id' => 'attachment_view',
250 'class' => 'hentry'));
251 $this->out->elementStart('div', 'entry-title');
252 $this->out->element('a', $this->linkAttr(), $this->linkTitle());
253 $this->out->elementEnd('div');
255 $this->out->elementStart('div', 'entry-content');
256 $this->showRepresentation();
257 $this->out->elementEnd('div');
259 if (!empty($this->oembed->author_name) || !empty($this->oembed->provider)) {
260 $this->out->elementStart('div', array('id' => 'oembed_info',
261 'class' => 'entry-content'));
262 if (!empty($this->oembed->author_name)) {
263 $this->out->elementStart('dl', 'vcard author');
264 // TRANS: DT element label in attachment list item.
265 $this->out->element('dt', null, _('Author'));
266 $this->out->elementStart('dd', 'fn');
267 if (empty($this->oembed->author_url)) {
268 $this->out->text($this->oembed->author_name);
270 $this->out->element('a', array('href' => $this->oembed->author_url,
271 'class' => 'url'), $this->oembed->author_name);
273 $this->out->elementEnd('dd');
274 $this->out->elementEnd('dl');
276 if (!empty($this->oembed->provider)) {
277 $this->out->elementStart('dl', 'vcard');
278 // TRANS: DT element label in attachment list item.
279 $this->out->element('dt', null, _('Provider'));
280 $this->out->elementStart('dd', 'fn');
281 if (empty($this->oembed->provider_url)) {
282 $this->out->text($this->oembed->provider);
284 $this->out->element('a', array('href' => $this->oembed->provider_url,
285 'class' => 'url'), $this->oembed->provider);
287 $this->out->elementEnd('dd');
288 $this->out->elementEnd('dl');
290 $this->out->elementEnd('div');
292 $this->out->elementEnd('div');
296 $this->showNoticeAttachment();
299 function linkAttr() {
300 return array('rel' => 'external', 'href' => $this->attachment->url);
303 function linkTitle() {
304 return $this->attachment->url;
307 function showRepresentation() {
308 if (empty($this->oembed->type)) {
309 if (empty($this->attachment->mimetype)) {
310 $this->showFallback();
312 switch ($this->attachment->mimetype) {
317 $this->out->element('img', array('src' => $this->attachment->url, 'alt' => 'alt'));
320 case 'application/ogg':
321 case 'audio/x-speex':
325 case 'video/quicktime':
326 $arr = array('type' => $this->attachment->mimetype,
327 'data' => $this->attachment->url,
331 $this->out->elementStart('object', $arr);
332 $this->out->element('param', array('name' => 'src', 'value' => $this->attachment->url));
333 $this->out->element('param', array('name' => 'autoStart', 'value' => 1));
334 $this->out->elementEnd('object');
338 if ($this->attachment->filename) {
339 // Locally-uploaded HTML. Scrub and display inline.
340 $this->showHtmlFile($this->attachment);
343 // Fall through to default.
346 $this->showFallback();
350 switch ($this->oembed->type) {
354 if (!empty($this->oembed->html)) {
355 require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
358 'elements'=>'*+object+embed');
359 $this->out->raw(htmLawed($this->oembed->html,$config));
360 //$this->out->raw($this->oembed->html);
365 $this->out->element('img', array('src' => $this->oembed->url, 'width' => $this->oembed->width, 'height' => $this->oembed->height, 'alt' => 'alt'));
369 $this->showFallback();
374 protected function showHtmlFile(File $attachment)
376 $body = $this->scrubHtmlFile($attachment);
378 $this->out->raw($body);
383 * @return mixed false on failure, HTML fragment string on success
385 protected function scrubHtmlFile(File $attachment)
387 $path = File::path($attachment->filename);
388 if (!file_exists($path) || !is_readable($path)) {
389 common_log(LOG_ERR, "Missing local HTML attachment $path");
392 $raw = file_get_contents($path);
395 $dom = new DOMDocument();
396 if(!$dom->loadHTML($raw)) {
397 common_log(LOG_ERR, "Bad HTML in local HTML attachment $path");
401 // Remove <script>s or htmlawed will dump their contents into output!
402 // Note: removing child nodes while iterating seems to mess things up,
403 // hence the double loop.
405 foreach ($dom->getElementsByTagName('script') as $script) {
406 $scripts[] = $script;
408 foreach ($scripts as $script) {
409 common_log(LOG_DEBUG, $script->textContent);
410 $script->parentNode->removeChild($script);
413 // Trim out everything outside the body...
414 $body = $dom->saveHTML();
415 $body = preg_replace('/^.*<body[^>]*>/is', '', $body);
416 $body = preg_replace('/<\/body[^>]*>.*$/is', '', $body);
418 require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
419 $config = array('safe' => 1,
420 'deny_attribute' => 'id,style,on*',
421 'comment' => 1); // remove comments
422 $scrubbed = htmLawed($body, $config);
427 function showFallback()
429 // If we don't know how to display an attachment inline, we probably
430 // shouldn't have gotten to this point.
432 // But, here we are... displaying details on a file or remote URL
433 // either on the main view or in an ajax-loaded lightbox. As a lesser
434 // of several evils, we'll try redirecting to the actual target via
437 common_log(LOG_ERR, "Empty or unknown type for file id {$this->attachment->id}; falling back to client-side redirect.");
438 $this->out->raw('<script>window.location = ' . json_encode($this->attachment->url) . ';</script>');