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('ol', array('class' => 'attachments entry-content'));
98 function showListEnd()
100 $this->out->elementEnd('ol');
104 * returns a new list item for the current notice
106 * Recipe (factory?) method; overridden by sub-classes to give
107 * a different list item class.
109 * @param Notice $notice the current notice
111 * @return NoticeListItem a list item for displaying the notice
113 function newListItem($attachment)
115 return new AttachmentListItem($attachment, $this->out);
120 * widget for displaying a single notice
122 * This widget has the core smarts for showing a single notice: what to display,
123 * where, and under which circumstances. Its key method is show(); this is a recipe
124 * that calls all the other show*() methods to build up a single notice. The
125 * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
126 * author info (since that's implicit by the data in the page).
130 * @author Evan Prodromou <evan@status.net>
131 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
132 * @link http://status.net/
134 * @see ProfileNoticeListItem
136 class AttachmentListItem extends Widget
138 /** The attachment this item will show. */
140 var $attachment = null;
147 * Also initializes the profile attribute.
149 * @param Notice $notice The notice we'll display
151 function __construct($attachment, $out=null)
153 parent::__construct($out);
154 $this->attachment = $attachment;
155 $this->oembed = File_oembed::staticGet('file_id', $this->attachment->id);
159 if (empty($this->attachment->title)) {
160 if (empty($this->oembed->title)) {
161 $title = $this->attachment->url;
163 $title = $this->oembed->title;
166 $title = $this->attachment->title;
172 function linkTitle() {
173 return $this->title();
177 * recipe function for displaying a single notice.
179 * This uses all the other methods to correctly display a notice. Override
180 * it or one of the others to fine-tune the output.
187 $this->showNoticeAttachment();
191 function linkAttr() {
192 return array('class' => 'attachment',
193 'href' => $this->attachment->url,
194 'id' => 'attachment-' . $this->attachment->id,
195 'title' => $this->title());
198 function showLink() {
199 $this->out->elementStart('a', $this->linkAttr());
200 $this->out->element('span', null, $this->linkTitle());
201 $this->showRepresentation();
202 $this->out->elementEnd('a');
205 function showNoticeAttachment()
210 function showRepresentation() {
211 $thumb = $this->getThumbInfo();
213 $this->out->element('img', array('alt' => '', 'src' => $thumb->url, 'width' => $thumb->width, 'height' => $thumb->height));
218 * Pull a thumbnail image reference for the given file, and if necessary
219 * resize it to match currently thumbnail size settings.
221 * @return File_Thumbnail or false/null
223 function getThumbInfo()
225 $thumbnail = File_thumbnail::staticGet('file_id', $this->attachment->id);
227 $maxWidth = common_config('attachments', 'thumb_width');
228 $maxHeight = common_config('attachments', 'thumb_height');
229 if ($thumbnail->width > $maxWidth) {
230 $thumb = clone($thumbnail);
231 $thumb->width = $maxWidth;
232 $thumb->height = intval($thumbnail->height * $maxWidth / $thumbnail->width);
240 * start a single notice.
247 // TODO: add notice_type class e.g., notice_video, notice_image
248 $this->out->elementStart('li');
254 * Close the last elements in the notice list item
260 $this->out->elementEnd('li');
265 * used for one-off attachment action
267 class Attachment extends AttachmentListItem
269 function showLink() {
270 $this->out->elementStart('div', array('id' => 'attachment_view',
271 'class' => 'hentry'));
272 $this->out->elementStart('div', 'entry-title');
273 $this->out->element('a', $this->linkAttr(), $this->linkTitle());
274 $this->out->elementEnd('div');
276 $this->out->elementStart('div', 'entry-content');
277 $this->showRepresentation();
278 $this->out->elementEnd('div');
280 if (!empty($this->oembed->author_name) || !empty($this->oembed->provider)) {
281 $this->out->elementStart('div', array('id' => 'oembed_info',
282 'class' => 'entry-content'));
283 if (!empty($this->oembed->author_name)) {
284 $this->out->elementStart('div', 'fn vcard author');
285 if (empty($this->oembed->author_url)) {
286 $this->out->text($this->oembed->author_name);
288 $this->out->element('a', array('href' => $this->oembed->author_url,
289 'class' => 'url'), $this->oembed->author_name);
292 if (!empty($this->oembed->provider)) {
293 $this->out->elementStart('div', 'fn vcard');
294 if (empty($this->oembed->provider_url)) {
295 $this->out->text($this->oembed->provider);
297 $this->out->element('a', array('href' => $this->oembed->provider_url,
298 'class' => 'url'), $this->oembed->provider);
301 $this->out->elementEnd('div');
303 $this->out->elementEnd('div');
307 $this->showNoticeAttachment();
310 function linkAttr() {
311 return array('rel' => 'external', 'href' => $this->attachment->url);
314 function linkTitle() {
315 return $this->attachment->url;
318 function showRepresentation() {
319 if (empty($this->oembed->type)) {
320 if (empty($this->attachment->mimetype)) {
321 $this->showFallback();
323 switch ($this->attachment->mimetype) {
328 $this->out->element('img', array('src' => $this->attachment->url, 'alt' => 'alt'));
331 case 'application/ogg':
332 case 'audio/x-speex':
336 case 'video/quicktime':
337 $arr = array('type' => $this->attachment->mimetype,
338 'data' => $this->attachment->url,
342 $this->out->elementStart('object', $arr);
343 $this->out->element('param', array('name' => 'src', 'value' => $this->attachment->url));
344 $this->out->element('param', array('name' => 'autoStart', 'value' => 1));
345 $this->out->elementEnd('object');
349 if ($this->attachment->filename) {
350 // Locally-uploaded HTML. Scrub and display inline.
351 $this->showHtmlFile($this->attachment);
354 // Fall through to default.
357 $this->showFallback();
361 switch ($this->oembed->type) {
365 if (!empty($this->oembed->html)) {
366 require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
369 'elements'=>'*+object+embed');
370 $this->out->raw(htmLawed($this->oembed->html,$config));
371 //$this->out->raw($this->oembed->html);
376 $this->out->element('img', array('src' => $this->oembed->url, 'width' => $this->oembed->width, 'height' => $this->oembed->height, 'alt' => 'alt'));
380 $this->showFallback();
385 protected function showHtmlFile(File $attachment)
387 $body = $this->scrubHtmlFile($attachment);
389 $this->out->raw($body);
394 * @return mixed false on failure, HTML fragment string on success
396 protected function scrubHtmlFile(File $attachment)
398 $path = File::path($attachment->filename);
399 if (!file_exists($path) || !is_readable($path)) {
400 common_log(LOG_ERR, "Missing local HTML attachment $path");
403 $raw = file_get_contents($path);
406 $dom = new DOMDocument();
407 if(!$dom->loadHTML($raw)) {
408 common_log(LOG_ERR, "Bad HTML in local HTML attachment $path");
412 // Remove <script>s or htmlawed will dump their contents into output!
413 // Note: removing child nodes while iterating seems to mess things up,
414 // hence the double loop.
416 foreach ($dom->getElementsByTagName('script') as $script) {
417 $scripts[] = $script;
419 foreach ($scripts as $script) {
420 common_log(LOG_DEBUG, $script->textContent);
421 $script->parentNode->removeChild($script);
424 // Trim out everything outside the body...
425 $body = $dom->saveHTML();
426 $body = preg_replace('/^.*<body[^>]*>/is', '', $body);
427 $body = preg_replace('/<\/body[^>]*>.*$/is', '', $body);
429 require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
430 $config = array('safe' => 1,
431 'deny_attribute' => 'id,style,on*',
432 'comment' => 1); // remove comments
433 $scrubbed = htmLawed($body, $config);
438 function showFallback()
440 // still needed: should show a link?