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',
201 'href' => $this->attachment->url,
202 'id' => 'attachment-' . $this->attachment->id,
203 'title' => $this->title());
206 function showLink() {
207 $this->out->elementStart('a', $this->linkAttr());
208 $this->out->element('span', null, $this->linkTitle());
209 $this->showRepresentation();
210 $this->out->elementEnd('a');
213 function showNoticeAttachment()
218 function showRepresentation() {
219 $thumb = $this->getThumbInfo();
221 $this->out->element('img', array('alt' => '', 'src' => $thumb->url, 'width' => $thumb->width, 'height' => $thumb->height));
226 * Pull a thumbnail image reference for the given file, and if necessary
227 * resize it to match currently thumbnail size settings.
229 * @return File_Thumbnail or false/null
231 function getThumbInfo()
233 $thumbnail = File_thumbnail::staticGet('file_id', $this->attachment->id);
235 $maxWidth = common_config('attachments', 'thumb_width');
236 $maxHeight = common_config('attachments', 'thumb_height');
237 if ($thumbnail->width > $maxWidth) {
238 $thumb = clone($thumbnail);
239 $thumb->width = $maxWidth;
240 $thumb->height = intval($thumbnail->height * $maxWidth / $thumbnail->width);
248 * start a single notice.
255 // TODO: add notice_type class e.g., notice_video, notice_image
256 $this->out->elementStart('li');
262 * Close the last elements in the notice list item
268 $this->out->elementEnd('li');
273 * used for one-off attachment action
275 class Attachment extends AttachmentListItem
277 function showLink() {
278 $this->out->elementStart('div', array('id' => 'attachment_view',
279 'class' => 'hentry'));
280 $this->out->elementStart('div', 'entry-title');
281 $this->out->element('a', $this->linkAttr(), $this->linkTitle());
282 $this->out->elementEnd('div');
284 $this->out->elementStart('div', 'entry-content');
285 $this->showRepresentation();
286 $this->out->elementEnd('div');
288 if (!empty($this->oembed->author_name) || !empty($this->oembed->provider)) {
289 $this->out->elementStart('div', array('id' => 'oembed_info',
290 'class' => 'entry-content'));
291 if (!empty($this->oembed->author_name)) {
292 $this->out->elementStart('dl', 'vcard author');
293 // TRANS: DT element label in attachment list item.
294 $this->out->element('dt', null, _('Author'));
295 $this->out->elementStart('dd', 'fn');
296 if (empty($this->oembed->author_url)) {
297 $this->out->text($this->oembed->author_name);
299 $this->out->element('a', array('href' => $this->oembed->author_url,
300 'class' => 'url'), $this->oembed->author_name);
302 $this->out->elementEnd('dd');
303 $this->out->elementEnd('dl');
305 if (!empty($this->oembed->provider)) {
306 $this->out->elementStart('dl', 'vcard');
307 // TRANS: DT element label in attachment list item.
308 $this->out->element('dt', null, _('Provider'));
309 $this->out->elementStart('dd', 'fn');
310 if (empty($this->oembed->provider_url)) {
311 $this->out->text($this->oembed->provider);
313 $this->out->element('a', array('href' => $this->oembed->provider_url,
314 'class' => 'url'), $this->oembed->provider);
316 $this->out->elementEnd('dd');
317 $this->out->elementEnd('dl');
319 $this->out->elementEnd('div');
321 $this->out->elementEnd('div');
325 $this->showNoticeAttachment();
328 function linkAttr() {
329 return array('rel' => 'external', 'href' => $this->attachment->url);
332 function linkTitle() {
333 return $this->attachment->url;
336 function showRepresentation() {
337 if (empty($this->oembed->type)) {
338 if (empty($this->attachment->mimetype)) {
339 $this->showFallback();
341 switch ($this->attachment->mimetype) {
346 $this->out->element('img', array('src' => $this->attachment->url, 'alt' => 'alt'));
349 case 'application/ogg':
350 case 'audio/x-speex':
354 case 'video/quicktime':
355 $arr = array('type' => $this->attachment->mimetype,
356 'data' => $this->attachment->url,
360 $this->out->elementStart('object', $arr);
361 $this->out->element('param', array('name' => 'src', 'value' => $this->attachment->url));
362 $this->out->element('param', array('name' => 'autoStart', 'value' => 1));
363 $this->out->elementEnd('object');
367 if ($this->attachment->filename) {
368 // Locally-uploaded HTML. Scrub and display inline.
369 $this->showHtmlFile($this->attachment);
372 // Fall through to default.
375 $this->showFallback();
379 switch ($this->oembed->type) {
383 if (!empty($this->oembed->html)) {
384 require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
387 'elements'=>'*+object+embed');
388 $this->out->raw(htmLawed($this->oembed->html,$config));
389 //$this->out->raw($this->oembed->html);
394 $this->out->element('img', array('src' => $this->oembed->url, 'width' => $this->oembed->width, 'height' => $this->oembed->height, 'alt' => 'alt'));
398 $this->showFallback();
403 protected function showHtmlFile(File $attachment)
405 $body = $this->scrubHtmlFile($attachment);
407 $this->out->raw($body);
412 * @return mixed false on failure, HTML fragment string on success
414 protected function scrubHtmlFile(File $attachment)
416 $path = File::path($attachment->filename);
417 if (!file_exists($path) || !is_readable($path)) {
418 common_log(LOG_ERR, "Missing local HTML attachment $path");
421 $raw = file_get_contents($path);
424 $dom = new DOMDocument();
425 if(!$dom->loadHTML($raw)) {
426 common_log(LOG_ERR, "Bad HTML in local HTML attachment $path");
430 // Remove <script>s or htmlawed will dump their contents into output!
431 // Note: removing child nodes while iterating seems to mess things up,
432 // hence the double loop.
434 foreach ($dom->getElementsByTagName('script') as $script) {
435 $scripts[] = $script;
437 foreach ($scripts as $script) {
438 common_log(LOG_DEBUG, $script->textContent);
439 $script->parentNode->removeChild($script);
442 // Trim out everything outside the body...
443 $body = $dom->saveHTML();
444 $body = preg_replace('/^.*<body[^>]*>/is', '', $body);
445 $body = preg_replace('/<\/body[^>]*>.*$/is', '', $body);
447 require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
448 $config = array('safe' => 1,
449 'deny_attribute' => 'id,style,on*',
450 'comment' => 1); // remove comments
451 $scrubbed = htmLawed($body, $config);
456 function showFallback()
458 // still needed: should show a link?