/**
  * 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 (!common_valid_http_url($this->url)) {
         throw new ClientException(_('Invalid URL.'), 400);
     }
     try {
         // processNew will first try to fetch a locally stored File entry
         $f = File::processNew($this->url);
     } catch (ServerException $e) {
         $f = null;
     }
     // How about now?
     if ($f instanceof File) {
         // FIXME: Use some File metadata Event instead
         $this->oembed = File_oembed::getKV('file_id', $f->id);
         if ($this->oembed instanceof File_oembed) {
             $this->title = $this->oembed->title;
         }
         $this->thumbnail = File_thumbnail::getKV('file_id', $f->id);
     }
     return true;
 }
Exemple #2
0
 /**
  * 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;
 }
Exemple #3
0
 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;
 }
Exemple #4
0
 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;
 }
Exemple #5
0
 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 _userMakeShort($long_url, User $user = null, $force = false)
 {
     $short_url = common_shorten_url($long_url, $user, $force);
     if (!empty($short_url) && $short_url != $long_url) {
         $short_url = (string) $short_url;
         // store it
         $file = File::staticGet('url', $long_url);
         if (empty($file)) {
             // Check if the target URL is itself a redirect...
             $redir_data = File_redirection::where($long_url);
             if (is_array($redir_data)) {
                 // We haven't seen the target URL before.
                 // Save file and embedding data about it!
                 $file = File::saveNew($redir_data, $long_url);
                 $file_id = $file->id;
                 if (!empty($redir_data['oembed']['json'])) {
                     File_oembed::saveNew($redir_data['oembed']['json'], $file_id);
                 }
             } else {
                 if (is_string($redir_data)) {
                     // The file is a known redirect target.
                     $file = File::staticGet('url', $redir_data);
                     if (empty($file)) {
                         // @fixme should we save a new one?
                         // this case was triggering sometimes for redirects
                         // with unresolvable targets; found while fixing
                         // "can't linkify" bugs with shortened links to
                         // SSL sites with cert issues.
                         return null;
                     }
                     $file_id = $file->id;
                 }
             }
         } else {
             $file_id = $file->id;
         }
         $file_redir = File_redirection::staticGet('url', $short_url);
         if (empty($file_redir)) {
             $file_redir = new File_redirection();
             $file_redir->url = $short_url;
             $file_redir->file_id = $file_id;
             $file_redir->insert();
         }
         return $short_url;
     }
     return null;
 }
Exemple #8
0
 function _userMakeShort($long_url)
 {
     $short_url = common_shorten_url($long_url);
     if (!empty($short_url) && $short_url != $long_url) {
         $short_url = (string) $short_url;
         // store it
         $file = File::staticGet('url', $long_url);
         if (empty($file)) {
             // Check if the target URL is itself a redirect...
             $redir_data = File_redirection::where($long_url);
             if (is_array($redir_data)) {
                 // We haven't seen the target URL before.
                 // Save file and embedding data about it!
                 $file = File::saveNew($redir_data, $long_url);
                 $file_id = $file->id;
                 if (!empty($redir_data['oembed']['json'])) {
                     File_oembed::saveNew($redir_data['oembed']['json'], $file_id);
                 }
             } else {
                 if (is_string($redir_data)) {
                     // The file is a known redirect target.
                     $file = File::staticGet('url', $redir_data);
                     $file_id = $file->id;
                 }
             }
         } else {
             $file_id = $file->id;
         }
         $file_redir = File_redirection::staticGet('url', $short_url);
         if (empty($file_redir)) {
             $file_redir = new File_redirection();
             $file_redir->url = $short_url;
             $file_redir->file_id = $file_id;
             $file_redir->insert();
         }
         return $short_url;
     }
     return null;
 }
 /**
  * Save embedding info for a new file.
  *
  * @param object $data Services_oEmbed_Object_*
  * @param int $file_id
  */
 public static function saveNew($data, $file_id)
 {
     $file_oembed = new File_oembed();
     $file_oembed->file_id = $file_id;
     if (!isset($data->version)) {
         common_debug('DEBUGGING oEmbed: data->version undefined in variable $data: ' . var_export($data, true));
     }
     $file_oembed->version = $data->version;
     $file_oembed->type = $data->type;
     if (!empty($data->provider_name)) {
         $file_oembed->provider = $data->provider_name;
     }
     if (!empty($data->provider)) {
         $file_oembed->provider = $data->provider;
     }
     if (!empty($data->provider_url)) {
         $file_oembed->provider_url = $data->provider_url;
     }
     if (!empty($data->width)) {
         $file_oembed->width = intval($data->width);
     }
     if (!empty($data->height)) {
         $file_oembed->height = intval($data->height);
     }
     if (!empty($data->html)) {
         $file_oembed->html = $data->html;
     }
     if (!empty($data->title)) {
         $file_oembed->title = $data->title;
     }
     if (!empty($data->author_name)) {
         $file_oembed->author_name = $data->author_name;
     }
     if (!empty($data->author_url)) {
         $file_oembed->author_url = $data->author_url;
     }
     if (!empty($data->url)) {
         $file_oembed->url = $data->url;
         $given_url = File_redirection::_canonUrl($file_oembed->url);
         if (!empty($given_url)) {
             $file = File::getKV('url', $given_url);
             if ($file instanceof File) {
                 $file_oembed->mimetype = $file->mimetype;
             } else {
                 $redir = File_redirection::where($given_url);
                 if (empty($redir->file_id)) {
                     $f = $redir->getFile();
                     $file_oembed->mimetype = $f->mimetype;
                 } else {
                     $file_id = $redir->file_id;
                 }
             }
         }
     }
     $result = $file_oembed->insert();
     if ($result === false) {
         throw new ServerException('Failed to insert File_oembed data into database!');
     }
     if (!empty($data->thumbnail_url) || $data->type == 'photo') {
         $ft = File_thumbnail::getKV('file_id', $file_id);
         if ($ft instanceof File_thumbnail) {
             common_log(LOG_WARNING, "Strangely, a File_thumbnail object exists for new file {$file_id}", __FILE__);
         } else {
             File_thumbnail::saveNew($data, $file_id);
         }
     }
 }
 /**
  * Add stuff to notices in API responses
  *
  * @param Action $action action being executed
  *
  * @return boolean hook return
  */
 function onNoticeSimpleStatusArray($notice, &$twitter_status, $scoped)
 {
     // groups
     $notice_groups = $notice->getGroups();
     $group_addressees = false;
     foreach ($notice_groups as $g) {
         $group_addressees[] = array('nickname' => $g->nickname, 'url' => $g->mainpage);
     }
     $twitter_status['statusnet_in_groups'] = $group_addressees;
     // for older verions of gnu social: include the repeat-id, which we need when unrepeating later
     if (array_key_exists('repeated', $twitter_status) && $twitter_status['repeated'] === true) {
         $repeated = Notice::pkeyGet(array('profile_id' => $scoped->id, 'repeat_of' => $notice->id, 'verb' => 'http://activitystrea.ms/schema/1.0/share'));
         $twitter_status['repeated_id'] = $repeated->id;
     }
     // more metadata about attachments
     // get all attachments first, and put all the extra meta data in an array
     $attachments = $notice->attachments();
     $attachment_url_to_id = array();
     if (!empty($attachments)) {
         foreach ($attachments as $attachment) {
             if (is_object($attachment)) {
                 try {
                     $enclosure_o = $attachment->getEnclosure();
                     // Oembed
                     if (array_key_exists('Oembed', StatusNet::getActivePlugins())) {
                         $oembed = File_oembed::getKV('file_id', $attachment->id);
                         if ($oembed instanceof File_oembed) {
                             $oembed_html = str_replace('<!--//-->', '', $oembed->html);
                             // trash left of wordpress' javascript after htmLawed removed the tags
                             if ($oembed->provider == 'Twitter' && strstr($oembed_html, '>— ' . $oembed->author_name)) {
                                 $oembed_html = substr($oembed_html, 0, strpos($oembed_html, '>— ' . $oembed->author_name) + 1);
                                 // remove user data from twitter oembed html (we have it in )
                                 $twitter_username = substr($oembed->html, strpos($oembed->html, '>— ' . $oembed->author_name) + strlen('>— ' . $oembed->author_name));
                                 $twitter_username = substr($twitter_username, strpos($twitter_username, '(@') + 1);
                                 $twitter_username = substr($twitter_username, 0, strpos($twitter_username, ')'));
                                 $oembed->title = $twitter_username;
                             }
                             $oembed_html = str_replace('…', '...', $oembed_html);
                             // ellipsis is sometimes stored as html in db, for some reason
                             $oembed_html = mb_substr(trim(strip_tags(html_entity_decode($oembed_html, ENT_QUOTES))), 0, 250);
                             // sometimes we have html charachters that we want to decode and then strip
                             $oembed_title = trim(strip_tags(html_entity_decode($oembed->title, ENT_QUOTES)));
                             $oembed_provider = trim(strip_tags(html_entity_decode($oembed->provider, ENT_QUOTES)));
                             $oembed_author_name = trim(strip_tags(html_entity_decode($oembed->author_name, ENT_QUOTES)));
                             $attachment_url_to_id[$enclosure_o->url]['oembed'] = array('type' => $oembed->type, 'provider' => $oembed_provider, 'provider_url' => $oembed->provider_url, 'oembedHTML' => $oembed_html, 'title' => $oembed_title, 'author_name' => $oembed_author_name, 'author_url' => $oembed->author_url);
                         } else {
                             $attachment_url_to_id[$enclosure_o->url]['oembed'] = false;
                         }
                     }
                     // add id to all attachments
                     $attachment_url_to_id[$enclosure_o->url]['id'] = $attachment->id;
                     // add an attachment version to all attachments
                     // this means we can force all cached attachments to update, if we change this
                     $attachment_url_to_id[$enclosure_o->url]['version'] = '1.2';
                     // add data about thumbnails
                     $thumb = $attachment->getThumbnail();
                     $large_thumb = $attachment->getThumbnail(1000, 3000, false);
                     if (method_exists('File_thumbnail', 'url')) {
                         $thumb_url = File_thumbnail::url($thumb->filename);
                         $large_thumb_url = File_thumbnail::url($large_thumb->filename);
                     } else {
                         $thumb_url = $thumb->getUrl();
                         $large_thumb_url = $large_thumb->getUrl();
                     }
                     $attachment_url_to_id[$enclosure_o->url]['thumb_url'] = $thumb_url;
                     $attachment_url_to_id[$enclosure_o->url]['large_thumb_url'] = $large_thumb_url;
                     $attachment_url_to_id[$enclosure_o->url]['width'] = $attachment->width;
                     $attachment_url_to_id[$enclosure_o->url]['height'] = $attachment->height;
                     // animated gif?
                     if ($attachment->mimetype == 'image/gif') {
                         $image = ImageFile::fromFileObject($attachment);
                         if ($image->animated == 1) {
                             $attachment_url_to_id[$enclosure_o->url]['animated'] = true;
                         } else {
                             $attachment_url_to_id[$enclosure_o->url]['animated'] = false;
                         }
                     }
                     // this applies to older versions of gnu social, i think
                 } catch (Exception $e) {
                     $thumb = File_thumbnail::getKV('file_id', $attachment->id);
                     if ($thumb instanceof File_thumbnail) {
                         $thumb_url = $thumb->getUrl();
                         $attachment_url_to_id[$enclosure_o->url]['id'] = $attachment->id;
                         $attachment_url_to_id[$enclosure_o->url]['thumb_url'] = $thumb_url;
                         $attachment_url_to_id[$enclosure_o->url]['large_thumb_url'] = $thumb_url;
                         $attachment_url_to_id[$enclosure_o->url]['width'] = $attachment->width;
                         $attachment_url_to_id[$enclosure_o->url]['height'] = $attachment->height;
                         // animated gif?
                         if ($attachment->mimetype == 'image/gif') {
                             $image = ImageFile::fromFileObject($attachment);
                             if ($image->animated == 1) {
                                 $attachment_url_to_id[$enclosure_o->url]['animated'] = true;
                             } else {
                                 $attachment_url_to_id[$enclosure_o->url]['animated'] = false;
                             }
                         }
                     }
                 }
             }
         }
     }
     // add the extra meta data to $twitter_status
     if (!empty($twitter_status['attachments'])) {
         foreach ($twitter_status['attachments'] as &$attachment) {
             if (!empty($attachment_url_to_id[$attachment['url']])) {
                 $attachment = array_merge($attachment, $attachment_url_to_id[$attachment['url']]);
             }
         }
     }
     // quoted notices
     if (!empty($twitter_status['attachments'])) {
         foreach ($twitter_status['attachments'] as &$attachment) {
             $quoted_notice = false;
             // if this attachment has an url this might be a notice url
             if (isset($attachment['url'])) {
                 $noticeurl = common_path('notice/', StatusNet::isHTTPS());
                 $instanceurl = common_path('', StatusNet::isHTTPS());
                 // remove protocol for the comparison below
                 $noticeurl_wo_protocol = preg_replace('(^https?://)', '', $noticeurl);
                 $instanceurl_wo_protocol = preg_replace('(^https?://)', '', $instanceurl);
                 $attachment_url_wo_protocol = preg_replace('(^https?://)', '', $attachment['url']);
                 // local notice urls
                 if (strpos($attachment_url_wo_protocol, $noticeurl_wo_protocol) === 0) {
                     $possible_notice_id = str_replace($noticeurl_wo_protocol, '', $attachment_url_wo_protocol);
                     if (ctype_digit($possible_notice_id)) {
                         $quoted_notice = Notice::getKV('id', $possible_notice_id);
                     }
                 } elseif (strpos($attachment_url_wo_protocol, $instanceurl_wo_protocol) !== 0 && stristr($attachment_url_wo_protocol, '/notice/')) {
                     $quoted_notice = Notice::getKV('url', $attachment['url']);
                     // try with http<->https if no match. applies to quitter.se notices mostly
                     if (!$quoted_notice instanceof Notice) {
                         if (strpos($attachment['url'], 'https://') === 0) {
                             $quoted_notice = Notice::getKV('url', str_replace('https://', 'http://', $attachment['url']));
                         } else {
                             $quoted_notice = Notice::getKV('url', str_replace('http://', 'https://', $attachment['url']));
                         }
                     }
                 }
                 // include the quoted notice in the attachment
                 if ($quoted_notice instanceof Notice) {
                     $quoted_notice_author = Profile::getKV('id', $quoted_notice->profile_id);
                     if ($quoted_notice_author instanceof Profile) {
                         $attachment['quoted_notice']['id'] = $quoted_notice->id;
                         $attachment['quoted_notice']['content'] = $quoted_notice->content;
                         $attachment['quoted_notice']['nickname'] = $quoted_notice_author->nickname;
                         $attachment['quoted_notice']['fullname'] = $quoted_notice_author->fullname;
                         $attachment['quoted_notice']['is_local'] = $quoted_notice_author->isLocal();
                         $quoted_notice_attachments = $quoted_notice->attachments();
                         foreach ($quoted_notice_attachments as $q_attach) {
                             if (is_object($q_attach)) {
                                 try {
                                     $qthumb = $q_attach->getThumbnail();
                                     if (method_exists('File_thumbnail', 'url')) {
                                         $thumb_url = File_thumbnail::url($qthumb->filename);
                                     } else {
                                         $thumb_url = $qthumb->getUrl();
                                     }
                                     $attachment['quoted_notice']['attachments'][] = array('thumb_url' => $thumb_url, 'attachment_id' => $q_attach->id);
                                 } catch (Exception $e) {
                                     common_debug('Qvitter: could not get thumbnail for attachment id=' . $q_attach->id . ' in quoted notice id=' . $quoted_notice->id);
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     try {
         $twitter_status['external_url'] = $notice->getUrl(true);
     } catch (InvalidUrlException $e) {
         common_debug('Qvitter: No URL available for external notice: id=' . $notice->id);
     }
     // reply-to profile url
     try {
         $reply = $notice->getParent();
         $twitter_status['in_reply_to_profileurl'] = $reply->getProfile()->getUrl();
     } catch (ServerException $e) {
         $twitter_status['in_reply_to_profileurl'] = null;
     }
     // fave number
     $faves = Fave::byNotice($notice);
     $favenum = count($faves);
     $twitter_status['fave_num'] = $favenum;
     // repeat number
     $repeats = $notice->repeatStream();
     $repeatnum = 0;
     while ($repeats->fetch()) {
         if ($repeats->verb == ActivityVerb::SHARE) {
             // i.e. not deleted repeats
             $repeatnum++;
         }
     }
     $twitter_status['repeat_num'] = $repeatnum;
     // is this a post? (previously is_activity)
     if (method_exists('ActivityUtils', 'compareVerbs')) {
         $twitter_status['is_post_verb'] = ActivityUtils::compareVerbs($notice->verb, array(ActivityVerb::POST));
     } else {
         $twitter_status['is_post_verb'] = $notice->verb == ActivityVerb::POST ? true : false;
     }
     // some more metadata about notice
     if ($notice->is_local == '1') {
         $twitter_status['is_local'] = true;
     } else {
         $twitter_status['is_local'] = false;
         if ($twitter_status['is_post_verb'] === true) {
             try {
                 $twitter_status['external_url'] = $notice->getUrl(true);
             } catch (InvalidUrlException $e) {
                 common_debug('Qvitter: No URL available for external notice: id=' . $notice->id);
             }
         }
     }
     if (ActivityUtils::compareTypes($notice->verb, array('qvitter-delete-notice', 'delete'))) {
         $twitter_status['qvitter_delete_notice'] = true;
     }
     return true;
 }
Exemple #11
0
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);
}
Exemple #12
0
 public function onCreateFileImageThumbnailSource(File $file, &$imgPath, $media = null)
 {
     // If we are on a private node, we won't do any remote calls (just as a precaution until
     // we can configure this from config.php for the private nodes)
     if (common_config('site', 'private')) {
         return true;
     }
     // All our remote Oembed images lack a local filename property in the File object
     if ($file->filename !== null) {
         return true;
     }
     try {
         // If we have proper oEmbed data, there should be an entry in the File_oembed
         // and File_thumbnail tables respectively. If not, we're not going to do anything.
         $file_oembed = File_oembed::byFile($file);
         $thumbnail = File_thumbnail::byFile($file);
     } catch (Exception $e) {
         // Not Oembed data, or at least nothing we either can or want to use.
         return true;
     }
     try {
         $this->storeRemoteFileThumbnail($thumbnail);
     } catch (AlreadyFulfilledException $e) {
         // aw yiss!
     }
     $imgPath = $thumbnail->getPath();
     return false;
 }
Exemple #13
0
 function saveNew($redir_data, $given_url)
 {
     $x = new File();
     $x->url = $given_url;
     if (!empty($redir_data['protected'])) {
         $x->protected = $redir_data['protected'];
     }
     if (!empty($redir_data['title'])) {
         $x->title = $redir_data['title'];
     }
     if (!empty($redir_data['type'])) {
         $x->mimetype = $redir_data['type'];
     }
     if (!empty($redir_data['size'])) {
         $x->size = intval($redir_data['size']);
     }
     if (isset($redir_data['time']) && $redir_data['time'] > 0) {
         $x->date = intval($redir_data['time']);
     }
     $file_id = $x->insert();
     if (isset($redir_data['type']) && 'text/html' === substr($redir_data['type'], 0, 9) && ($oembed_data = File_oembed::_getOembed($given_url)) && isset($oembed_data['json'])) {
         File_oembed::saveNew($oembed_data['json'], $file_id);
     }
     return $x;
 }
Exemple #14
0
 /**
  * Save embedding info for a new file.
  *
  * @param object $data Services_oEmbed_Object_*
  * @param int $file_id
  */
 function saveNew($data, $file_id)
 {
     $file_oembed = new File_oembed();
     $file_oembed->file_id = $file_id;
     $file_oembed->version = $data->version;
     $file_oembed->type = $data->type;
     if (!empty($data->provider_name)) {
         $file_oembed->provider = $data->provider_name;
     }
     if (!empty($data->provider)) {
         $file_oembed->provider = $data->provider;
     }
     if (!empty($data->provide_url)) {
         $file_oembed->provider_url = $data->provider_url;
     }
     if (!empty($data->width)) {
         $file_oembed->width = intval($data->width);
     }
     if (!empty($data->height)) {
         $file_oembed->height = intval($data->height);
     }
     if (!empty($data->html)) {
         $file_oembed->html = $data->html;
     }
     if (!empty($data->title)) {
         $file_oembed->title = $data->title;
     }
     if (!empty($data->author_name)) {
         $file_oembed->author_name = $data->author_name;
     }
     if (!empty($data->author_url)) {
         $file_oembed->author_url = $data->author_url;
     }
     if (!empty($data->url)) {
         $file_oembed->url = $data->url;
         $given_url = File_redirection::_canonUrl($file_oembed->url);
         if (!empty($given_url)) {
             $file = File::staticGet('url', $given_url);
             if (empty($file)) {
                 $file_redir = File_redirection::staticGet('url', $given_url);
                 if (empty($file_redir)) {
                     $redir_data = File_redirection::where($given_url);
                     $file_oembed->mimetype = $redir_data['type'];
                 } else {
                     $file_id = $file_redir->file_id;
                 }
             } else {
                 $file_oembed->mimetype = $file->mimetype;
             }
         }
     }
     $file_oembed->insert();
     if (!empty($data->thumbnail_url) || $data->type == 'photo') {
         $ft = File_thumbnail::staticGet('file_id', $file_id);
         if (!empty($ft)) {
             common_log(LOG_WARNING, "Strangely, a File_thumbnail object exists for new file {$file_id}", __FILE__);
         } else {
             File_thumbnail::saveNew($data, $file_id);
         }
     }
 }
Exemple #15
0
 function saveNew($data, $file_id)
 {
     $file_oembed = new File_oembed();
     $file_oembed->file_id = $file_id;
     $file_oembed->version = $data['version'];
     $file_oembed->type = $data['type'];
     if (!empty($data['provider_name'])) {
         $file_oembed->provider = $data['provider_name'];
     }
     if (!isset($file_oembed->provider) && !empty($data['provide'])) {
         $file_oembed->provider = $data['provider'];
     }
     if (!empty($data['provide_url'])) {
         $file_oembed->provider_url = $data['provider_url'];
     }
     if (!empty($data['width'])) {
         $file_oembed->width = intval($data['width']);
     }
     if (!empty($data['height'])) {
         $file_oembed->height = intval($data['height']);
     }
     if (!empty($data['html'])) {
         $file_oembed->html = $data['html'];
     }
     if (!empty($data['title'])) {
         $file_oembed->title = $data['title'];
     }
     if (!empty($data['author_name'])) {
         $file_oembed->author_name = $data['author_name'];
     }
     if (!empty($data['author_url'])) {
         $file_oembed->author_url = $data['author_url'];
     }
     if (!empty($data['url'])) {
         $file_oembed->url = $data['url'];
     }
     $file_oembed->insert();
     if (!empty($data['thumbnail_url'])) {
         File_thumbnail::saveNew($data, $file_id);
     }
 }
Exemple #16
0
 function _userMakeShort($long_url)
 {
     $short_url = common_shorten_url($long_url);
     if (!empty($short_url) && $short_url != $long_url) {
         $short_url = (string) $short_url;
         // store it
         $file = File::staticGet('url', $long_url);
         if (empty($file)) {
             $redir_data = File_redirection::where($long_url);
             $file = File::saveNew($redir_data, $long_url);
             $file_id = $file->id;
             if (!empty($redir_data['oembed']['json'])) {
                 File_oembed::saveNew($redir_data['oembed']['json'], $file_id);
             }
         } else {
             $file_id = $file->id;
         }
         $file_redir = File_redirection::staticGet('url', $short_url);
         if (empty($file_redir)) {
             $file_redir = new File_redirection();
             $file_redir->url = $short_url;
             $file_redir->file_id = $file_id;
             $file_redir->insert();
         }
         return $short_url;
     }
     return null;
 }