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('GNUSOCIAL') && !defined('STATUSNET')) {
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 attachments
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 items listed.
79 $att = $this->notice->attachments();
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 attachment
106 * @param File $attachment the current attachment
108 * @return AttachmentListItem a list item for displaying the attachment
110 function newListItem(File $attachment)
112 return new AttachmentListItem($attachment, $this->out);
117 * widget for displaying a single notice
119 * This widget has the core smarts for showing a single notice: what to display,
120 * where, and under which circumstances. Its key method is show(); this is a recipe
121 * that calls all the other show*() methods to build up a single notice. The
122 * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
123 * author info (since that's implicit by the data in the page).
127 * @author Evan Prodromou <evan@status.net>
128 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
129 * @link http://status.net/
131 * @see ProfileNoticeListItem
133 class AttachmentListItem extends Widget
135 /** The attachment this item will show. */
137 var $attachment = null;
142 * @param File $attachment the attachment we will display
144 function __construct(File $attachment, $out=null)
146 parent::__construct($out);
147 $this->attachment = $attachment;
148 $this->oembed = File_oembed::getKV('file_id', $this->attachment->id);
152 if (empty($this->attachment->title)) {
153 if (empty($this->oembed->title)) {
154 $title = $this->attachment->url;
156 $title = $this->oembed->title;
159 $title = $this->attachment->title;
165 function linkTitle() {
166 return $this->title();
170 * recipe function for displaying a single notice.
172 * This uses all the other methods to correctly display a notice. Override
173 * it or one of the others to fine-tune the output.
180 $this->showNoticeAttachment();
184 function linkAttr() {
185 return array('class' => 'attachment',
186 'href' => $this->attachment->url,
187 'id' => 'attachment-' . $this->attachment->id,
188 'title' => $this->title());
191 function showLink() {
192 $this->out->elementStart('a', $this->linkAttr());
193 $this->out->element('span', null, $this->linkTitle());
194 $this->showRepresentation();
195 $this->out->elementEnd('a');
198 function showNoticeAttachment()
203 function showRepresentation() {
204 $thumb = $this->getThumbInfo();
206 $this->out->element('img', array('alt' => '', 'src' => $thumb->url, 'width' => $thumb->width, 'height' => $thumb->height));
211 * Pull a thumbnail image reference for the given file, and if necessary
212 * resize it to match currently thumbnail size settings.
214 * @return File_Thumbnail or false/null
216 function getThumbInfo()
218 $thumbnail = File_thumbnail::getKV('file_id', $this->attachment->id);
220 $maxWidth = common_config('attachments', 'thumb_width');
221 $maxHeight = common_config('attachments', 'thumb_height');
222 if ($thumbnail->width > $maxWidth) {
223 $thumb = clone($thumbnail);
224 $thumb->width = $maxWidth;
225 $thumb->height = intval($thumbnail->height * $maxWidth / $thumbnail->width);
233 * start a single notice.
240 // TODO: add notice_type class e.g., notice_video, notice_image
241 $this->out->elementStart('li');
247 * Close the last elements in the notice list item
253 $this->out->elementEnd('li');
258 * used for one-off attachment action
260 class Attachment extends AttachmentListItem
262 function showLink() {
263 $this->out->elementStart('div', array('id' => 'attachment_view',
264 'class' => 'hentry'));
265 $this->out->elementStart('div', 'entry-title');
266 $this->out->element('a', $this->linkAttr(), $this->linkTitle());
267 $this->out->elementEnd('div');
269 $this->out->elementStart('div', 'entry-content');
270 $this->showRepresentation();
271 $this->out->elementEnd('div');
273 if (!empty($this->oembed->author_name) || !empty($this->oembed->provider)) {
274 $this->out->elementStart('div', array('id' => 'oembed_info',
275 'class' => 'entry-content'));
276 if (!empty($this->oembed->author_name)) {
277 $this->out->elementStart('div', 'fn vcard author');
278 if (empty($this->oembed->author_url)) {
279 $this->out->text($this->oembed->author_name);
281 $this->out->element('a', array('href' => $this->oembed->author_url,
282 'class' => 'url'), $this->oembed->author_name);
285 if (!empty($this->oembed->provider)) {
286 $this->out->elementStart('div', 'fn vcard');
287 if (empty($this->oembed->provider_url)) {
288 $this->out->text($this->oembed->provider);
290 $this->out->element('a', array('href' => $this->oembed->provider_url,
291 'class' => 'url'), $this->oembed->provider);
294 $this->out->elementEnd('div');
296 $this->out->elementEnd('div');
300 $this->showNoticeAttachment();
303 function linkAttr() {
304 return array('rel' => 'external', 'href' => $this->attachment->url);
307 function linkTitle() {
308 return $this->attachment->url;
311 function showRepresentation() {
312 if (empty($this->oembed->type)) {
313 if (empty($this->attachment->mimetype)) {
314 $this->showFallback();
316 switch ($this->attachment->mimetype) {
321 $this->out->element('img', array('src' => $this->attachment->url, 'alt' => 'alt'));
324 case 'application/ogg':
325 case 'audio/x-speex':
329 case 'video/quicktime':
330 $arr = array('type' => $this->attachment->mimetype,
331 'data' => $this->attachment->url,
335 $this->out->elementStart('object', $arr);
336 $this->out->element('param', array('name' => 'src', 'value' => $this->attachment->url));
337 $this->out->element('param', array('name' => 'autoStart', 'value' => 1));
338 $this->out->elementEnd('object');
342 if ($this->attachment->filename) {
343 // Locally-uploaded HTML. Scrub and display inline.
344 $this->showHtmlFile($this->attachment);
347 // Fall through to default.
350 $this->showFallback();
354 switch ($this->oembed->type) {
358 if (!empty($this->oembed->html)) {
359 require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
362 'elements'=>'*+object+embed');
363 $this->out->raw(htmLawed($this->oembed->html,$config));
364 //$this->out->raw($this->oembed->html);
369 $this->out->element('img', array('src' => $this->oembed->url, 'width' => $this->oembed->width, 'height' => $this->oembed->height, 'alt' => 'alt'));
373 $this->showFallback();
378 protected function showHtmlFile(File $attachment)
380 $body = $this->scrubHtmlFile($attachment);
382 $this->out->raw($body);
387 * @return mixed false on failure, HTML fragment string on success
389 protected function scrubHtmlFile(File $attachment)
391 $path = File::path($attachment->filename);
392 if (!file_exists($path) || !is_readable($path)) {
393 common_log(LOG_ERR, "Missing local HTML attachment $path");
396 $raw = file_get_contents($path);
399 $dom = new DOMDocument();
400 if(!$dom->loadHTML($raw)) {
401 common_log(LOG_ERR, "Bad HTML in local HTML attachment $path");
405 // Remove <script>s or htmlawed will dump their contents into output!
406 // Note: removing child nodes while iterating seems to mess things up,
407 // hence the double loop.
409 foreach ($dom->getElementsByTagName('script') as $script) {
410 $scripts[] = $script;
412 foreach ($scripts as $script) {
413 common_log(LOG_DEBUG, $script->textContent);
414 $script->parentNode->removeChild($script);
417 // Trim out everything outside the body...
418 $body = $dom->saveHTML();
419 $body = preg_replace('/^.*<body[^>]*>/is', '', $body);
420 $body = preg_replace('/<\/body[^>]*>.*$/is', '', $body);
422 require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
423 $config = array('safe' => 1,
424 'deny_attribute' => 'id,style,on*',
425 'comment' => 1); // remove comments
426 $scrubbed = htmLawed($body, $config);
431 function showFallback()
433 // still needed: should show a link?