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
52 class AttachmentList extends Widget
54 /** the current stream of notices being displayed. */
61 * @param Notice $notice stream of notices from DB_DataObject
63 function __construct($notice, $out=null)
65 parent::__construct($out);
66 $this->notice = $notice;
70 * show the list of notices
72 * "Uses up" the stream by looping through it. So, probably can't
73 * be called twice on the same list.
75 * @return int count of notices listed.
80 $att = $atts->getAttachments($this->notice->id);
81 if (empty($att)) return 0;
82 $this->showListStart();
84 foreach ($att as $n=>$attachment) {
85 $item = $this->newListItem($attachment);
94 function showListStart()
96 $this->out->elementStart('dl', array('id' =>'attachments',
97 'class' => 'entry-content'));
98 // TRANS: DT element label in attachment list.
99 $this->out->element('dt', null, _('Attachments'));
100 $this->out->elementStart('dd');
101 $this->out->elementStart('ol', array('class' => 'attachments'));
104 function showListEnd()
106 $this->out->elementEnd('dd');
107 $this->out->elementEnd('ol');
108 $this->out->elementEnd('dl');
112 * returns a new list item for the current notice
114 * Recipe (factory?) method; overridden by sub-classes to give
115 * a different list item class.
117 * @param Notice $notice the current notice
119 * @return NoticeListItem a list item for displaying the notice
121 function newListItem($attachment)
123 return new AttachmentListItem($attachment, $this->out);
128 * widget for displaying a single notice
130 * This widget has the core smarts for showing a single notice: what to display,
131 * where, and under which circumstances. Its key method is show(); this is a recipe
132 * that calls all the other show*() methods to build up a single notice. The
133 * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
134 * author info (since that's implicit by the data in the page).
138 * @author Evan Prodromou <evan@status.net>
139 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
140 * @link http://status.net/
142 * @see ProfileNoticeListItem
144 class AttachmentListItem extends Widget
146 /** The attachment this item will show. */
148 var $attachment = null;
155 * Also initializes the profile attribute.
157 * @param Notice $notice The notice we'll display
159 function __construct($attachment, $out=null)
161 parent::__construct($out);
162 $this->attachment = $attachment;
163 $this->oembed = File_oembed::staticGet('file_id', $this->attachment->id);
167 if (empty($this->attachment->title)) {
168 if (empty($this->oembed->title)) {
169 $title = $this->attachment->url;
171 $title = $this->oembed->title;
174 $title = $this->attachment->title;
180 function linkTitle() {
181 return $this->title();
185 * recipe function for displaying a single notice.
187 * This uses all the other methods to correctly display a notice. Override
188 * it or one of the others to fine-tune the output.
195 $this->showNoticeAttachment();
199 function linkAttr() {
200 return array('class' => 'attachment', 'href' => $this->attachment->url, 'id' => 'attachment-' . $this->attachment->id);
203 function showLink() {
204 $this->out->elementStart('a', $this->linkAttr());
205 $this->out->element('span', null, $this->linkTitle());
206 $this->showRepresentation();
207 $this->out->elementEnd('a');
210 function showNoticeAttachment()
215 function showRepresentation() {
216 $thumb = $this->getThumbInfo();
218 $thumb = $this->sizeThumb($thumb);
219 $this->out->element('img', array('alt' => '', 'src' => $thumb->url, 'width' => $thumb->width, 'height' => $thumb->height));
224 * Pull a thumbnail image reference for the given file.
226 * 1) file_thumbnail table (thumbnails found via oEmbed)
227 * 2) image URL from direct dereference or oEmbed 'photo' type URL
230 * @return mixed object with (url, width, height) properties, or false
232 function getThumbInfo()
234 $thumbnail = File_thumbnail::staticGet('file_id', $this->attachment->id);
238 $enc = $this->attachment->getEnclosure();
240 switch ($enc->mimetype) {
245 $thumb = (object)array();
246 $thumb->url = $enc->url;
248 $thumb->height = 75; // @fixme
255 function sizeThumb($thumbnail) {
258 if ($thumbnail->width > $maxWidth) {
259 $thumb = clone($thumbnail);
260 $thumb->width = $maxWidth;
261 $thumb->height = intval($thumbnail->height * $maxWidth / $thumbnail->width);
269 * start a single notice.
276 // TODO: add notice_type class e.g., notice_video, notice_image
277 $this->out->elementStart('li');
283 * Close the last elements in the notice list item
289 $this->out->elementEnd('li');
294 * used for one-off attachment action
296 class Attachment extends AttachmentListItem
298 function showLink() {
299 $this->out->elementStart('div', array('id' => 'attachment_view',
300 'class' => 'hentry'));
301 $this->out->elementStart('div', 'entry-title');
302 $this->out->element('a', $this->linkAttr(), $this->linkTitle());
303 $this->out->elementEnd('div');
305 $this->out->elementStart('div', 'entry-content');
306 $this->showRepresentation();
307 $this->out->elementEnd('div');
309 if (!empty($this->oembed->author_name) || !empty($this->oembed->provider)) {
310 $this->out->elementStart('div', array('id' => 'oembed_info',
311 'class' => 'entry-content'));
312 if (!empty($this->oembed->author_name)) {
313 $this->out->elementStart('dl', 'vcard author');
314 // TRANS: DT element label in attachment list item.
315 $this->out->element('dt', null, _('Author'));
316 $this->out->elementStart('dd', 'fn');
317 if (empty($this->oembed->author_url)) {
318 $this->out->text($this->oembed->author_name);
320 $this->out->element('a', array('href' => $this->oembed->author_url,
321 'class' => 'url'), $this->oembed->author_name);
323 $this->out->elementEnd('dd');
324 $this->out->elementEnd('dl');
326 if (!empty($this->oembed->provider)) {
327 $this->out->elementStart('dl', 'vcard');
328 // TRANS: DT element label in attachment list item.
329 $this->out->element('dt', null, _('Provider'));
330 $this->out->elementStart('dd', 'fn');
331 if (empty($this->oembed->provider_url)) {
332 $this->out->text($this->oembed->provider);
334 $this->out->element('a', array('href' => $this->oembed->provider_url,
335 'class' => 'url'), $this->oembed->provider);
337 $this->out->elementEnd('dd');
338 $this->out->elementEnd('dl');
340 $this->out->elementEnd('div');
342 $this->out->elementEnd('div');
346 $this->showNoticeAttachment();
349 function linkAttr() {
350 return array('rel' => 'external', 'href' => $this->attachment->url);
353 function linkTitle() {
354 return $this->attachment->url;
357 function showRepresentation() {
358 if (empty($this->oembed->type)) {
359 if (empty($this->attachment->mimetype)) {
360 $this->showFallback();
362 switch ($this->attachment->mimetype) {
367 $this->out->element('img', array('src' => $this->attachment->url, 'alt' => 'alt'));
370 case 'application/ogg':
371 case 'audio/x-speex':
375 case 'video/quicktime':
376 $arr = array('type' => $this->attachment->mimetype,
377 'data' => $this->attachment->url,
381 $this->out->elementStart('object', $arr);
382 $this->out->element('param', array('name' => 'src', 'value' => $this->attachment->url));
383 $this->out->element('param', array('name' => 'autoStart', 'value' => 1));
384 $this->out->elementEnd('object');
388 if ($this->attachment->filename) {
389 // Locally-uploaded HTML. Scrub and display inline.
390 $this->showHtmlFile($this->attachment);
393 // Fall through to default.
396 $this->showFallback();
400 switch ($this->oembed->type) {
404 if (!empty($this->oembed->html)) {
405 require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
408 'elements'=>'*+object+embed');
409 $this->out->raw(htmLawed($this->oembed->html,$config));
410 //$this->out->raw($this->oembed->html);
415 $this->out->element('img', array('src' => $this->oembed->url, 'width' => $this->oembed->width, 'height' => $this->oembed->height, 'alt' => 'alt'));
419 $this->showFallback();
424 protected function showHtmlFile(File $attachment)
426 $body = $this->scrubHtmlFile($attachment);
428 $this->out->raw($body);
433 * @return mixed false on failure, HTML fragment string on success
435 protected function scrubHtmlFile(File $attachment)
437 $path = File::path($attachment->filename);
438 if (!file_exists($path) || !is_readable($path)) {
439 common_log(LOG_ERR, "Missing local HTML attachment $path");
442 $raw = file_get_contents($path);
445 $dom = new DOMDocument();
446 if(!$dom->loadHTML($raw)) {
447 common_log(LOG_ERR, "Bad HTML in local HTML attachment $path");
451 // Remove <script>s or htmlawed will dump their contents into output!
452 // Note: removing child nodes while iterating seems to mess things up,
453 // hence the double loop.
455 foreach ($dom->getElementsByTagName('script') as $script) {
456 $scripts[] = $script;
458 foreach ($scripts as $script) {
459 common_log(LOG_DEBUG, $script->textContent);
460 $script->parentNode->removeChild($script);
463 // Trim out everything outside the body...
464 $body = $dom->saveHTML();
465 $body = preg_replace('/^.*<body[^>]*>/is', '', $body);
466 $body = preg_replace('/<\/body[^>]*>.*$/is', '', $body);
468 require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
469 $config = array('safe' => 1,
470 'deny_attribute' => 'id,style,on*',
471 'comment' => 1); // remove comments
472 $scrubbed = htmLawed($body, $config);