/** * For initializing members of the class. * * @param array $args misc. arguments * * @return boolean true */ function prepare($args) { parent::prepare($args); if (!$this->isPost()) { throw new ClientException(_('POST only'), 405); } $this->checkSessionToken(); $this->url = $this->trimmed('url'); if (empty($this->url)) { throw new ClientException(_('URL is required.'), 400); } if (!Validate::uri($this->url, array('allowed_schemes' => array('http', 'https')))) { throw new ClientException(_('Invalid URL.'), 400); } $f = File::staticGet('url', $this->url); if (empty($url)) { $f = File::processNew($this->url); } // How about now? if (!empty($f)) { $this->oembed = File_oembed::staticGet('file_id', $f->id); if (!empty($this->oembed)) { $this->title = $this->oembed->title; } $this->thumbnail = File_thumbnail::staticGet('file_id', $f->id); } return true; }
function getEnclosure() { $enclosure = (object) array(); $enclosure->title = $this->title; $enclosure->url = $this->url; $enclosure->title = $this->title; $enclosure->date = $this->date; $enclosure->modified = $this->modified; $enclosure->size = $this->size; $enclosure->mimetype = $this->mimetype; if (!isset($this->filename)) { $notEnclosureMimeTypes = array(null, 'text/html', 'application/xhtml+xml'); $mimetype = $this->mimetype; if ($mimetype != null) { $mimetype = strtolower($this->mimetype); } $semicolon = strpos($mimetype, ';'); if ($semicolon) { $mimetype = substr($mimetype, 0, $semicolon); } if (in_array($mimetype, $notEnclosureMimeTypes)) { $oembed = File_oembed::staticGet('file_id', $this->id); if ($oembed) { $mimetype = strtolower($oembed->mimetype); $semicolon = strpos($mimetype, ';'); if ($semicolon) { $mimetype = substr($mimetype, 0, $semicolon); } if (in_array($mimetype, $notEnclosureMimeTypes)) { return false; } else { if ($oembed->mimetype) { $enclosure->mimetype = $oembed->mimetype; } if ($oembed->url) { $enclosure->url = $oembed->url; } if ($oembed->title) { $enclosure->title = $oembed->title; } if ($oembed->modified) { $enclosure->modified = $oembed->modified; } unset($oembed->size); } } else { return false; } } } return $enclosure; }
function getEnclosure() { $enclosure = (object) array(); $enclosure->title = $this->title; $enclosure->url = $this->url; $enclosure->title = $this->title; $enclosure->date = $this->date; $enclosure->modified = $this->modified; $enclosure->size = $this->size; $enclosure->mimetype = $this->mimetype; if (!isset($this->filename)) { $notEnclosureMimeTypes = array(null, 'text/html', 'application/xhtml+xml'); $mimetype = $this->mimetype; if ($mimetype != null) { $mimetype = strtolower($this->mimetype); } $semicolon = strpos($mimetype, ';'); if ($semicolon) { $mimetype = substr($mimetype, 0, $semicolon); } if (in_array($mimetype, $notEnclosureMimeTypes)) { // Never treat generic HTML links as an enclosure type! // But if we have oEmbed info, we'll consider it golden. $oembed = File_oembed::staticGet('file_id', $this->id); if ($oembed && in_array($oembed->type, array('photo', 'video'))) { $mimetype = strtolower($oembed->mimetype); $semicolon = strpos($mimetype, ';'); if ($semicolon) { $mimetype = substr($mimetype, 0, $semicolon); } // @fixme uncertain if this is right. // we want to expose things like YouTube videos as // viewable attachments, but don't expose them as // downloadable enclosures.....? //if (in_array($mimetype, $notEnclosureMimeTypes)) { // return false; //} else { if ($oembed->mimetype) { $enclosure->mimetype = $oembed->mimetype; } if ($oembed->url) { $enclosure->url = $oembed->url; } if ($oembed->title) { $enclosure->title = $oembed->title; } if ($oembed->modified) { $enclosure->modified = $oembed->modified; } unset($oembed->size); //} } else { return false; } } } return $enclosure; }
function handle($args) { common_debug("in oembed api action"); $url = $args['url']; if (substr(strtolower($url), 0, strlen(common_root_url())) == strtolower(common_root_url())) { $path = substr($url, strlen(common_root_url())); $r = Router::get(); $proxy_args = $r->map($path); if (!$proxy_args) { // TRANS: Server error displayed in oEmbed action when path not found. // TRANS: %s is a path. $this->serverError(sprintf(_('"%s" not found.'), $path), 404); } $oembed = array(); $oembed['version'] = '1.0'; $oembed['provider_name'] = common_config('site', 'name'); $oembed['provider_url'] = common_root_url(); switch ($proxy_args['action']) { case 'shownotice': $oembed['type'] = 'link'; $id = $proxy_args['notice']; $notice = Notice::staticGet($id); if (empty($notice)) { // TRANS: Server error displayed in oEmbed action when notice not found. // TRANS: %s is a notice. $this->serverError(sprintf(_("Notice %s not found."), $id), 404); } $profile = $notice->getProfile(); if (empty($profile)) { // TRANS: Server error displayed in oEmbed action when notice has not profile. $this->serverError(_('Notice has no profile.'), 500); } $authorname = $profile->getFancyName(); // TRANS: oEmbed title. %1$s is the author name, %2$s is the creation date. $oembed['title'] = sprintf(_('%1$s\'s status on %2$s'), $authorname, common_exact_date($notice->created)); $oembed['author_name'] = $authorname; $oembed['author_url'] = $profile->profileurl; $oembed['url'] = $notice->url ? $notice->url : $notice->uri; $oembed['html'] = $notice->rendered; break; case 'attachment': $id = $proxy_args['attachment']; $attachment = File::staticGet($id); if (empty($attachment)) { // TRANS: Server error displayed in oEmbed action when attachment not found. // TRANS: %d is an attachment ID. $this->serverError(sprintf(_('Attachment %s not found.'), $id), 404); } if (empty($attachment->filename) && ($file_oembed = File_oembed::staticGet('file_id', $attachment->id))) { // Proxy the existing oembed information $oembed['type'] = $file_oembed->type; $oembed['provider'] = $file_oembed->provider; $oembed['provider_url'] = $file_oembed->provider_url; $oembed['width'] = $file_oembed->width; $oembed['height'] = $file_oembed->height; $oembed['html'] = $file_oembed->html; $oembed['title'] = $file_oembed->title; $oembed['author_name'] = $file_oembed->author_name; $oembed['author_url'] = $file_oembed->author_url; $oembed['url'] = $file_oembed->url; } else { if (substr($attachment->mimetype, 0, strlen('image/')) == 'image/') { $oembed['type'] = 'photo'; if ($attachment->filename) { $filepath = File::path($attachment->filename); $gis = @getimagesize($filepath); if ($gis) { $oembed['width'] = $gis[0]; $oembed['height'] = $gis[1]; } else { // TODO Either throw an error or find a fallback? } } $oembed['url'] = $attachment->url; $thumb = $attachment->getThumbnail(); if ($thumb) { $oembed['thumbnail_url'] = $thumb->url; $oembed['thumbnail_width'] = $thumb->width; $oembed['thumbnail_height'] = $thumb->height; } } else { $oembed['type'] = 'link'; $oembed['url'] = common_local_url('attachment', array('attachment' => $attachment->id)); } } if ($attachment->title) { $oembed['title'] = $attachment->title; } break; default: // TRANS: Server error displayed in oEmbed request when a path is not supported. // TRANS: %s is a path. $this->serverError(sprintf(_('"%s" not supported for oembed requests.'), $path), 501); } switch ($args['format']) { case 'xml': $this->init_document('xml'); $this->elementStart('oembed'); $this->element('version', null, $oembed['version']); $this->element('type', null, $oembed['type']); if ($oembed['provider_name']) { $this->element('provider_name', null, $oembed['provider_name']); } if ($oembed['provider_url']) { $this->element('provider_url', null, $oembed['provider_url']); } if ($oembed['title']) { $this->element('title', null, $oembed['title']); } if ($oembed['author_name']) { $this->element('author_name', null, $oembed['author_name']); } if ($oembed['author_url']) { $this->element('author_url', null, $oembed['author_url']); } if ($oembed['url']) { $this->element('url', null, $oembed['url']); } if ($oembed['html']) { $this->element('html', null, $oembed['html']); } if ($oembed['width']) { $this->element('width', null, $oembed['width']); } if ($oembed['height']) { $this->element('height', null, $oembed['height']); } if ($oembed['cache_age']) { $this->element('cache_age', null, $oembed['cache_age']); } if ($oembed['thumbnail_url']) { $this->element('thumbnail_url', null, $oembed['thumbnail_url']); } if ($oembed['thumbnail_width']) { $this->element('thumbnail_width', null, $oembed['thumbnail_width']); } if ($oembed['thumbnail_height']) { $this->element('thumbnail_height', null, $oembed['thumbnail_height']); } $this->elementEnd('oembed'); $this->end_document('xml'); break; case 'json': case '': $this->init_document('json'); print json_encode($oembed); $this->end_document('json'); break; default: // TRANS: Error message displaying attachments. %s is a raw MIME type (eg 'image/png') $this->serverError(sprintf(_('Content type %s not supported.'), $apidata['content-type']), 501); } } else { // TRANS: Error message displaying attachments. %s is the site's base URL. $this->serverError(sprintf(_('Only %s URLs over plain HTTP please.'), common_root_url()), 404); } }
/** * constructor * * Also initializes the profile attribute. * * @param Notice $notice The notice we'll display */ function __construct($attachment, $out = null) { parent::__construct($out); $this->attachment = $attachment; $this->oembed = File_oembed::staticGet('file_id', $this->attachment->id); }
function common_linkify($url) { // It comes in special'd, so we unspecial it before passing to the stringifying // functions $url = htmlspecialchars_decode($url); if (strpos($url, '@') !== false && strpos($url, ':') === false) { //url is an email address without the mailto: protocol $canon = "mailto:{$url}"; $longurl = "mailto:{$url}"; } else { $canon = File_redirection::_canonUrl($url); $longurl_data = File_redirection::where($canon); if (is_array($longurl_data)) { $longurl = $longurl_data['url']; } elseif (is_string($longurl_data)) { $longurl = $longurl_data; } else { // Unable to reach the server to verify contents, etc // Just pass the link on through for now. common_log(LOG_ERR, "Can't linkify url '{$url}'"); $longurl = $url; } } $attrs = array('href' => $canon, 'title' => $longurl, 'rel' => 'external'); $is_attachment = false; $attachment_id = null; $has_thumb = false; // Check to see whether this is a known "attachment" URL. $f = File::staticGet('url', $longurl); if (empty($f)) { // XXX: this writes to the database. :< $f = File::processNew($longurl); } if (!empty($f)) { if ($f->getEnclosure() || File_oembed::staticGet('file_id', $f->id)) { $is_attachment = true; $attachment_id = $f->id; $thumb = File_thumbnail::staticGet('file_id', $f->id); if (!empty($thumb)) { $has_thumb = true; } } } // Add clippy if ($is_attachment) { $attrs['class'] = 'attachment'; if ($has_thumb) { $attrs['class'] = 'attachment thumbnail'; } $attrs['id'] = "attachment-{$attachment_id}"; } return XMLStringer::estring('a', $attrs, $url); }