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.
79 $att = File::getAttachments($this->notice->id);
80 if (empty($att)) return 0;
81 $this->showListStart();
83 foreach ($att as $n=>$attachment) {
84 $item = $this->newListItem($attachment);
93 function showListStart()
95 $this->out->elementStart('dl', array('id' =>'attachments',
96 'class' => 'entry-content'));
97 // TRANS: DT element label in attachment list.
98 $this->out->element('dt', null, _('Attachments'));
99 $this->out->elementStart('dd');
100 $this->out->elementStart('ol', array('class' => 'attachments'));
103 function showListEnd()
105 $this->out->elementEnd('dd');
106 $this->out->elementEnd('ol');
107 $this->out->elementEnd('dl');
111 * returns a new list item for the current notice
113 * Recipe (factory?) method; overridden by sub-classes to give
114 * a different list item class.
116 * @param Notice $notice the current notice
118 * @return NoticeListItem a list item for displaying the notice
120 function newListItem($attachment)
122 return new AttachmentListItem($attachment, $this->out);
127 * widget for displaying a single notice
129 * This widget has the core smarts for showing a single notice: what to display,
130 * where, and under which circumstances. Its key method is show(); this is a recipe
131 * that calls all the other show*() methods to build up a single notice. The
132 * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
133 * author info (since that's implicit by the data in the page).
137 * @author Evan Prodromou <evan@status.net>
138 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
139 * @link http://status.net/
141 * @see ProfileNoticeListItem
143 class AttachmentListItem extends Widget
145 /** The attachment this item will show. */
147 var $attachment = null;
154 * Also initializes the profile attribute.
156 * @param Notice $notice The notice we'll display
158 function __construct($attachment, $out=null)
160 parent::__construct($out);
161 $this->attachment = $attachment;
162 $this->oembed = File_oembed::staticGet('file_id', $this->attachment->id);
166 if (empty($this->attachment->title)) {
167 if (empty($this->oembed->title)) {
168 $title = $this->attachment->url;
170 $title = $this->oembed->title;
173 $title = $this->attachment->title;
179 function linkTitle() {
180 return $this->title();
184 * recipe function for displaying a single notice.
186 * This uses all the other methods to correctly display a notice. Override
187 * it or one of the others to fine-tune the output.
194 $this->showNoticeAttachment();
198 function linkAttr() {
199 return array('class' => 'attachment',
200 'href' => $this->attachment->url,
201 'id' => 'attachment-' . $this->attachment->id,
202 'title' => $this->title());
205 function showLink() {
206 $this->out->elementStart('a', $this->linkAttr());
207 $this->out->element('span', null, $this->linkTitle());
208 $this->showRepresentation();
209 $this->out->elementEnd('a');
212 function showNoticeAttachment()
217 function showRepresentation() {
218 $thumb = $this->getThumbInfo();
220 $this->out->element('img', array('alt' => '', 'src' => $thumb->url, 'width' => $thumb->width, 'height' => $thumb->height));
225 * Pull a thumbnail image reference for the given file, and if necessary
226 * resize it to match currently thumbnail size settings.
228 * @return File_Thumbnail or false/null
230 function getThumbInfo()
232 $thumbnail = File_thumbnail::staticGet('file_id', $this->attachment->id);
234 $maxWidth = common_config('attachments', 'thumb_width');
235 $maxHeight = common_config('attachments', 'thumb_height');
236 if ($thumbnail->width > $maxWidth) {
237 $thumb = clone($thumbnail);
238 $thumb->width = $maxWidth;
239 $thumb->height = intval($thumbnail->height * $maxWidth / $thumbnail->width);
247 * start a single notice.
254 // TODO: add notice_type class e.g., notice_video, notice_image
255 $this->out->elementStart('li');
261 * Close the last elements in the notice list item
267 $this->out->elementEnd('li');
272 * used for one-off attachment action
274 class Attachment extends AttachmentListItem
276 function showLink() {
277 $this->out->elementStart('div', array('id' => 'attachment_view',
278 'class' => 'hentry'));
279 $this->out->elementStart('div', 'entry-title');
280 $this->out->element('a', $this->linkAttr(), $this->linkTitle());
281 $this->out->elementEnd('div');
283 $this->out->elementStart('div', 'entry-content');
284 $this->showRepresentation();
285 $this->out->elementEnd('div');
287 if (!empty($this->oembed->author_name) || !empty($this->oembed->provider)) {
288 $this->out->elementStart('div', array('id' => 'oembed_info',
289 'class' => 'entry-content'));
290 if (!empty($this->oembed->author_name)) {
291 $this->out->elementStart('dl', 'vcard author');
292 // TRANS: DT element label in attachment list item.
293 $this->out->element('dt', null, _('Author'));
294 $this->out->elementStart('dd', 'fn');
295 if (empty($this->oembed->author_url)) {
296 $this->out->text($this->oembed->author_name);
298 $this->out->element('a', array('href' => $this->oembed->author_url,
299 'class' => 'url'), $this->oembed->author_name);
301 $this->out->elementEnd('dd');
302 $this->out->elementEnd('dl');
304 if (!empty($this->oembed->provider)) {
305 $this->out->elementStart('dl', 'vcard');
306 // TRANS: DT element label in attachment list item.
307 $this->out->element('dt', null, _('Provider'));
308 $this->out->elementStart('dd', 'fn');
309 if (empty($this->oembed->provider_url)) {
310 $this->out->text($this->oembed->provider);
312 $this->out->element('a', array('href' => $this->oembed->provider_url,
313 'class' => 'url'), $this->oembed->provider);
315 $this->out->elementEnd('dd');
316 $this->out->elementEnd('dl');
318 $this->out->elementEnd('div');
320 $this->out->elementEnd('div');
324 $this->showNoticeAttachment();
327 function linkAttr() {
328 return array('rel' => 'external', 'href' => $this->attachment->url);
331 function linkTitle() {
332 return $this->attachment->url;
335 function showRepresentation() {
336 if (empty($this->oembed->type)) {
337 if (empty($this->attachment->mimetype)) {
338 $this->showFallback();
340 switch ($this->attachment->mimetype) {
345 $this->out->element('img', array('src' => $this->attachment->url, 'alt' => 'alt'));
348 case 'application/ogg':
349 case 'audio/x-speex':
353 case 'video/quicktime':
354 $arr = array('type' => $this->attachment->mimetype,
355 'data' => $this->attachment->url,
359 $this->out->elementStart('object', $arr);
360 $this->out->element('param', array('name' => 'src', 'value' => $this->attachment->url));
361 $this->out->element('param', array('name' => 'autoStart', 'value' => 1));
362 $this->out->elementEnd('object');
366 if ($this->attachment->filename) {
367 // Locally-uploaded HTML. Scrub and display inline.
368 $this->showHtmlFile($this->attachment);
371 // Fall through to default.
374 $this->showFallback();
378 switch ($this->oembed->type) {
382 if (!empty($this->oembed->html)) {
383 require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
386 'elements'=>'*+object+embed');
387 $this->out->raw(htmLawed($this->oembed->html,$config));
388 //$this->out->raw($this->oembed->html);
393 $this->out->element('img', array('src' => $this->oembed->url, 'width' => $this->oembed->width, 'height' => $this->oembed->height, 'alt' => 'alt'));
397 $this->showFallback();
402 protected function showHtmlFile(File $attachment)
404 $body = $this->scrubHtmlFile($attachment);
406 $this->out->raw($body);
411 * @return mixed false on failure, HTML fragment string on success
413 protected function scrubHtmlFile(File $attachment)
415 $path = File::path($attachment->filename);
416 if (!file_exists($path) || !is_readable($path)) {
417 common_log(LOG_ERR, "Missing local HTML attachment $path");
420 $raw = file_get_contents($path);
423 $dom = new DOMDocument();
424 if(!$dom->loadHTML($raw)) {
425 common_log(LOG_ERR, "Bad HTML in local HTML attachment $path");
429 // Remove <script>s or htmlawed will dump their contents into output!
430 // Note: removing child nodes while iterating seems to mess things up,
431 // hence the double loop.
433 foreach ($dom->getElementsByTagName('script') as $script) {
434 $scripts[] = $script;
436 foreach ($scripts as $script) {
437 common_log(LOG_DEBUG, $script->textContent);
438 $script->parentNode->removeChild($script);
441 // Trim out everything outside the body...
442 $body = $dom->saveHTML();
443 $body = preg_replace('/^.*<body[^>]*>/is', '', $body);
444 $body = preg_replace('/<\/body[^>]*>.*$/is', '', $body);
446 require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
447 $config = array('safe' => 1,
448 'deny_attribute' => 'id,style,on*',
449 'comment' => 1); // remove comments
450 $scrubbed = htmLawed($body, $config);
455 function showFallback()
457 // still needed: should show a link?