/** * Process an incoming post activity from this remote feed. * @param Activity $activity * @param string $method 'push' or 'salmon' * @return mixed saved Notice or false * @todo FIXME: Break up this function, it's getting nasty long */ public function processPost($activity, $method) { $notice = null; $oprofile = $this->checkAuthorship($activity); if (empty($oprofile)) { return null; } // It's not always an ActivityObject::NOTE, but... let's just say it is. $note = $activity->objects[0]; // The id URI will be used as a unique identifier for for the notice, // protecting against duplicate saves. It isn't required to be a URL; // tag: URIs for instance are found in Google Buzz feeds. $sourceUri = $note->id; $dupe = Notice::staticGet('uri', $sourceUri); if ($dupe) { common_log(LOG_INFO, "OStatus: ignoring duplicate post: {$sourceUri}"); return $dupe; } // We'll also want to save a web link to the original notice, if provided. $sourceUrl = null; if ($note->link) { $sourceUrl = $note->link; } else { if ($activity->link) { $sourceUrl = $activity->link; } else { if (preg_match('!^https?://!', $note->id)) { $sourceUrl = $note->id; } } } // Use summary as fallback for content if (!empty($note->content)) { $sourceContent = $note->content; } else { if (!empty($note->summary)) { $sourceContent = $note->summary; } else { if (!empty($note->title)) { $sourceContent = $note->title; } else { // @todo FIXME: Fetch from $sourceUrl? // TRANS: Client exception. %s is a source URI. throw new ClientException(sprintf(_m('No content for notice %s.'), $sourceUri)); } } } // Get (safe!) HTML and text versions of the content $rendered = $this->purify($sourceContent); $content = html_entity_decode(strip_tags($rendered), ENT_QUOTES, 'UTF-8'); $shortened = common_shorten_links($content); // If it's too long, try using the summary, and make the // HTML an attachment. $attachment = null; if (Notice::contentTooLong($shortened)) { $attachment = $this->saveHTMLFile($note->title, $rendered); $summary = html_entity_decode(strip_tags($note->summary), ENT_QUOTES, 'UTF-8'); if (empty($summary)) { $summary = $content; } $shortSummary = common_shorten_links($summary); if (Notice::contentTooLong($shortSummary)) { $url = common_shorten_url($sourceUrl); $shortSummary = substr($shortSummary, 0, Notice::maxContent() - (mb_strlen($url) + 2)); $content = $shortSummary . ' ' . $url; // We mark up the attachment link specially for the HTML output // so we can fold-out the full version inline. // @todo FIXME i18n: This tooltip will be saved with the site's default language // TRANS: Shown when a notice is longer than supported and/or when attachments are present. At runtime // TRANS: this will usually be replaced with localised text from StatusNet core messages. $showMoreText = _m('Show more'); $attachUrl = common_local_url('attachment', array('attachment' => $attachment->id)); $rendered = common_render_text($shortSummary) . '<a href="' . htmlspecialchars($attachUrl) . '"' . ' class="attachment more"' . ' title="' . htmlspecialchars($showMoreText) . '">' . '…' . '</a>'; } } $options = array('is_local' => Notice::REMOTE, 'url' => $sourceUrl, 'uri' => $sourceUri, 'rendered' => $rendered, 'replies' => array(), 'groups' => array(), 'peopletags' => array(), 'tags' => array(), 'urls' => array()); // Check for optional attributes... if (!empty($activity->time)) { $options['created'] = common_sql_date($activity->time); } if ($activity->context) { // Any individual or group attn: targets? $replies = $activity->context->attention; $options['groups'] = $this->filterReplies($oprofile, $replies); $options['replies'] = $replies; // Maintain direct reply associations // @todo FIXME: What about conversation ID? if (!empty($activity->context->replyToID)) { $orig = Notice::staticGet('uri', $activity->context->replyToID); if (!empty($orig)) { $options['reply_to'] = $orig->id; } } $location = $activity->context->location; if ($location) { $options['lat'] = $location->lat; $options['lon'] = $location->lon; if ($location->location_id) { $options['location_ns'] = $location->location_ns; $options['location_id'] = $location->location_id; } } } if ($this->isPeopletag()) { $options['peopletags'][] = $this->localPeopletag(); } // Atom categories <-> hashtags foreach ($activity->categories as $cat) { if ($cat->term) { $term = common_canonical_tag($cat->term); if ($term) { $options['tags'][] = $term; } } } // Atom enclosures -> attachment URLs foreach ($activity->enclosures as $href) { // @todo FIXME: Save these locally or....? $options['urls'][] = $href; } try { $saved = Notice::saveNew($oprofile->profile_id, $content, 'ostatus', $options); if ($saved) { Ostatus_source::saveNew($saved, $this, $method); if (!empty($attachment)) { File_to_post::processNew($attachment->id, $saved->id); } } } catch (Exception $e) { common_log(LOG_ERR, "OStatus save of remote message {$sourceUri} failed: " . $e->getMessage()); throw $e; } common_log(LOG_INFO, "OStatus saved remote message {$sourceUri} as notice id {$saved->id}"); return $saved; }
/** * @fixme refactor this mess, it's gotten pretty scary. * @param bool $followRedirects */ function processNew($given_url, $notice_id = null, $followRedirects = true) { if (empty($given_url)) { return -1; } // error, no url to process $given_url = File_redirection::_canonUrl($given_url); if (empty($given_url)) { return -1; } // error, no url to process $file = File::staticGet('url', $given_url); if (empty($file)) { $file_redir = File_redirection::staticGet('url', $given_url); if (empty($file_redir)) { // @fixme for new URLs this also looks up non-redirect data // such as target content type, size, etc, which we need // for File::saveNew(); so we call it even if not following // new redirects. $redir_data = File_redirection::where($given_url); if (is_array($redir_data)) { $redir_url = $redir_data['url']; } elseif (is_string($redir_data)) { $redir_url = $redir_data; $redir_data = array(); } else { throw new ServerException("Can't process url '{$given_url}'"); } // TODO: max field length if ($redir_url === $given_url || strlen($redir_url) > 255 || !$followRedirects) { $x = File::saveNew($redir_data, $given_url); $file_id = $x->id; } else { // This seems kind of messed up... for now skipping this part // if we're already under a redirect, so we don't go into // horrible infinite loops if we've been given an unstable // redirect (where the final destination of the first request // doesn't match what we get when we ask for it again). // // Seen in the wild with clojure.org, which redirects through // wikispaces for auth and appends session data in the URL params. $x = File::processNew($redir_url, $notice_id, false); $file_id = $x->id; File_redirection::saveNew($redir_data, $file_id, $given_url); } } else { $file_id = $file_redir->file_id; } } else { $file_id = $file->id; $x = $file; } if (empty($x)) { $x = File::staticGet($file_id); if (empty($x)) { throw new ServerException("Robin thinks something is impossible."); } } if (!empty($notice_id)) { File_to_post::processNew($file_id, $notice_id); } return $x; }
/** * Process an incoming post activity from this remote feed. * @param Activity $activity * @param string $method 'push' or 'salmon' * @return mixed saved Notice or false * @fixme break up this function, it's getting nasty long */ public function processPost($activity, $method) { if ($this->isGroup()) { // A group feed will contain posts from multiple authors. // @fixme validate these profiles in some way! $oprofile = self::ensureActorProfile($activity); if ($oprofile->isGroup()) { // Groups can't post notices in StatusNet. common_log(LOG_WARNING, "OStatus: skipping post with group listed as author: {$oprofile->uri} in feed from {$this->uri}"); return false; } } else { $actor = $activity->actor; if (empty($actor)) { // OK here! assume the default } else { if ($actor->id == $this->uri || $actor->link == $this->uri) { $this->updateFromActivityObject($actor); } else { throw new Exception("Got an actor '{$actor->title}' ({$actor->id}) on single-user feed for {$this->uri}"); } } $oprofile = $this; } // It's not always an ActivityObject::NOTE, but... let's just say it is. $note = $activity->objects[0]; // The id URI will be used as a unique identifier for for the notice, // protecting against duplicate saves. It isn't required to be a URL; // tag: URIs for instance are found in Google Buzz feeds. $sourceUri = $note->id; $dupe = Notice::staticGet('uri', $sourceUri); if ($dupe) { common_log(LOG_INFO, "OStatus: ignoring duplicate post: {$sourceUri}"); return false; } // We'll also want to save a web link to the original notice, if provided. $sourceUrl = null; if ($note->link) { $sourceUrl = $note->link; } else { if ($activity->link) { $sourceUrl = $activity->link; } else { if (preg_match('!^https?://!', $note->id)) { $sourceUrl = $note->id; } } } // Use summary as fallback for content if (!empty($note->content)) { $sourceContent = $note->content; } else { if (!empty($note->summary)) { $sourceContent = $note->summary; } else { if (!empty($note->title)) { $sourceContent = $note->title; } else { // @fixme fetch from $sourceUrl? throw new ClientException("No content for notice {$sourceUri}"); } } } // Get (safe!) HTML and text versions of the content $rendered = $this->purify($sourceContent); $content = html_entity_decode(strip_tags($rendered)); $shortened = common_shorten_links($content); // If it's too long, try using the summary, and make the // HTML an attachment. $attachment = null; if (Notice::contentTooLong($shortened)) { $attachment = $this->saveHTMLFile($note->title, $rendered); $summary = html_entity_decode(strip_tags($note->summary)); if (empty($summary)) { $summary = $content; } $shortSummary = common_shorten_links($summary); if (Notice::contentTooLong($shortSummary)) { $url = common_shorten_url($sourceUrl); $shortSummary = substr($shortSummary, 0, Notice::maxContent() - (mb_strlen($url) + 2)); $content = $shortSummary . ' ' . $url; // We mark up the attachment link specially for the HTML output // so we can fold-out the full version inline. $attachUrl = common_local_url('attachment', array('attachment' => $attachment->id)); $rendered = common_render_text($shortSummary) . '<a href="' . htmlspecialchars($attachUrl) . '"' . ' class="attachment more"' . ' title="' . htmlspecialchars(_m('Show more')) . '">' . '…' . '</a>'; } } $options = array('is_local' => Notice::REMOTE_OMB, 'url' => $sourceUrl, 'uri' => $sourceUri, 'rendered' => $rendered, 'replies' => array(), 'groups' => array(), 'tags' => array(), 'urls' => array()); // Check for optional attributes... if (!empty($activity->time)) { $options['created'] = common_sql_date($activity->time); } if ($activity->context) { // Any individual or group attn: targets? $replies = $activity->context->attention; $options['groups'] = $this->filterReplies($oprofile, $replies); $options['replies'] = $replies; // Maintain direct reply associations // @fixme what about conversation ID? if (!empty($activity->context->replyToID)) { $orig = Notice::staticGet('uri', $activity->context->replyToID); if (!empty($orig)) { $options['reply_to'] = $orig->id; } } $location = $activity->context->location; if ($location) { $options['lat'] = $location->lat; $options['lon'] = $location->lon; if ($location->location_id) { $options['location_ns'] = $location->location_ns; $options['location_id'] = $location->location_id; } } } // Atom categories <-> hashtags foreach ($activity->categories as $cat) { if ($cat->term) { $term = common_canonical_tag($cat->term); if ($term) { $options['tags'][] = $term; } } } // Atom enclosures -> attachment URLs foreach ($activity->enclosures as $href) { // @fixme save these locally or....? $options['urls'][] = $href; } try { $saved = Notice::saveNew($oprofile->profile_id, $content, 'ostatus', $options); if ($saved) { Ostatus_source::saveNew($saved, $this, $method); if (!empty($attachment)) { File_to_post::processNew($attachment->id, $saved->id); } } } catch (Exception $e) { common_log(LOG_ERR, "OStatus save of remote message {$sourceUri} failed: " . $e->getMessage()); throw $e; } common_log(LOG_INFO, "OStatus saved remote message {$sourceUri} as notice id {$saved->id}"); return $saved; }
function processNew($given_url, $notice_id = null) { if (empty($given_url)) { return -1; } // error, no url to process $given_url = File_redirection::_canonUrl($given_url); if (empty($given_url)) { return -1; } // error, no url to process $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); if (is_array($redir_data)) { $redir_url = $redir_data['url']; } elseif (is_string($redir_data)) { $redir_url = $redir_data; $redir_data = array(); } else { throw new ServerException("Can't process url '{$given_url}'"); } // TODO: max field length if ($redir_url === $given_url || strlen($redir_url) > 255) { $x = File::saveNew($redir_data, $given_url); $file_id = $x->id; } else { $x = File::processNew($redir_url, $notice_id); $file_id = $x->id; File_redirection::saveNew($redir_data, $file_id, $given_url); } } else { $file_id = $file_redir->file_id; } } else { $file_id = $file->id; $x = $file; } if (empty($x)) { $x = File::staticGet($file_id); if (empty($x)) { throw new ServerException("Robin thinks something is impossible."); } } if (!empty($notice_id)) { File_to_post::processNew($file_id, $notice_id); } return $x; }
function attachToNotice($notice) { File_to_post::processNew($this->fileRecord->id, $notice->id); $this->maybeAddRedir($this->fileRecord->id, common_local_url('file', array('notice' => $notice->id))); }
function processNew($given_url, $notice_id) { if (empty($given_url)) { return -1; } // error, no url to process $given_url = File_redirection::_canonUrl($given_url); if (empty($given_url)) { return -1; } // error, no url to process $file = File::staticGet('url', $given_url); if (empty($file)) { $file_redir = File_redirection::staticGet('url', $given_url); if (empty($file_redir)) { common_debug("processNew() '{$given_url}' not a known redirect.\n"); $redir_data = File_redirection::where($given_url); $redir_url = $redir_data['url']; if ($redir_url === $given_url) { $x = File::saveNew($redir_data, $given_url); $file_id = $x->id; } else { $x = File::processNew($redir_url, $notice_id); $file_id = $x->id; File_redirection::saveNew($redir_data, $file_id, $given_url); } } else { $file_id = $file_redir->file_id; } } else { $file_id = $file->id; $x = $file; } if (empty($x)) { $x = File::staticGet($file_id); if (empty($x)) { die('Impossible!'); } } File_to_post::processNew($file_id, $notice_id); return $x; }
public function attachToNotice(Notice $notice) { File_to_post::processNew($this->fileRecord, $notice); }
/** * Go look at a URL and possibly save data about it if it's new: * - follow redirect chains and store them in file_redirection * - if a thumbnail is available, save it in file_thumbnail * - save file record with basic info * - optionally save a file_to_post record * - return the File object with the full reference * * @fixme refactor this mess, it's gotten pretty scary. * @param string $given_url the URL we're looking at * @param Notice $notice (optional) * @param bool $followRedirects defaults to true * * @return mixed File on success, -1 on some errors * * @throws ServerException on failure */ public static function processNew($given_url, Notice $notice = null, $followRedirects = true) { if (empty($given_url)) { throw new ServerException('No given URL to process'); } $given_url = File_redirection::_canonUrl($given_url); if (empty($given_url)) { throw new ServerException('No canonical URL from given URL to process'); } $file = null; try { $file = File::getByUrl($given_url); } catch (NoResultException $e) { // First check if we have a lookup trace for this URL already try { $file_redir = File_redirection::getByUrl($given_url); $file = File::getKV('id', $file_redir->file_id); if (!$file instanceof File) { // File did not exist, let's clean up the File_redirection entry $file_redir->delete(); } } catch (NoResultException $e) { // We just wanted to doublecheck whether a File_thumbnail we might've had // actually referenced an existing File object. } } // If we still don't have a File object, let's create one now! if (!$file instanceof File) { // @fixme for new URLs this also looks up non-redirect data // such as target content type, size, etc, which we need // for File::saveNew(); so we call it even if not following // new redirects. $redir_data = File_redirection::where($given_url); if (is_array($redir_data)) { $redir_url = $redir_data['url']; } elseif (is_string($redir_data)) { $redir_url = $redir_data; $redir_data = array(); } else { // TRANS: Server exception thrown when a URL cannot be processed. throw new ServerException(sprintf(_("Cannot process URL '%s'"), $given_url)); } if ($redir_url === $given_url || !$followRedirects) { // Save the File object based on our lookup trace $file = File::saveNew($redir_data, $given_url); } else { // This seems kind of messed up... for now skipping this part // if we're already under a redirect, so we don't go into // horrible infinite loops if we've been given an unstable // redirect (where the final destination of the first request // doesn't match what we get when we ask for it again). // // Seen in the wild with clojure.org, which redirects through // wikispaces for auth and appends session data in the URL params. $file = self::processNew($redir_url, $notice, false); File_redirection::saveNew($redir_data, $file->id, $given_url); } if (!$file instanceof File) { // This should only happen if File::saveNew somehow did not return a File object, // though we have an assert for that in case the event there might've gone wrong. // If anything else goes wrong, there should've been an exception thrown. throw new ServerException('URL processing failed without new File object'); } } if ($notice instanceof Notice) { File_to_post::processNew($file, $notice); } return $file; }
/** * Go look at a URL and possibly save data about it if it's new: * - follow redirect chains and store them in file_redirection * - if a thumbnail is available, save it in file_thumbnail * - save file record with basic info * - optionally save a file_to_post record * - return the File object with the full reference * * @param string $given_url the URL we're looking at * @param Notice $notice (optional) * @param bool $followRedirects defaults to true * * @return mixed File on success, -1 on some errors * * @throws ServerException on failure */ public static function processNew($given_url, Notice $notice = null, $followRedirects = true) { if (empty($given_url)) { throw new ServerException('No given URL to process'); } $given_url = File_redirection::_canonUrl($given_url); if (empty($given_url)) { throw new ServerException('No canonical URL from given URL to process'); } $redir = File_redirection::where($given_url); $file = $redir->getFile(); // If we still don't have a File object, let's create one now! if (empty($file->id)) { if ($redir->url === $given_url || !$followRedirects) { // Save the File object based on our lookup trace $file->saveFile(); } else { $file->saveFile(); $redir->file_id = $file->id; $redir->insert(); } } if (!$file instanceof File || empty($file->id)) { // This should not happen throw new ServerException('URL processing failed without new File object'); } if ($notice instanceof Notice) { File_to_post::processNew($file, $notice); } return $file; }