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 { throw new ServerException("Can't linkify url '{$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()) { $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); }
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 && Validate::email($url)) { //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, common_config('attachments', 'process_links')); if (isset($longurl_data->redir_url)) { $longurl = $longurl_data->redir_url; } else { // e.g. local files $longurl = $longurl_data->url; } } $attrs = array('href' => $longurl, 'title' => $longurl); $is_attachment = false; $attachment_id = null; $has_thumb = false; // Check to see whether this is a known "attachment" URL. try { $f = File::getByUrl($longurl); } catch (NoResultException $e) { if (common_config('attachments', 'process_links')) { // XXX: this writes to the database. :< try { $f = File::processNew($longurl); } catch (ServerException $e) { $f = null; } } } if ($f instanceof File) { try { $enclosure = $f->getEnclosure(); $is_attachment = true; $attachment_id = $f->id; $thumb = File_thumbnail::getKV('file_id', $f->id); $has_thumb = $thumb instanceof File_thumbnail; } catch (ServerException $e) { // There was not enough metadata available } } // Add clippy if ($is_attachment) { $attrs['class'] = 'attachment'; if ($has_thumb) { $attrs['class'] = 'attachment thumbnail'; } $attrs['id'] = "attachment-{$attachment_id}"; } // Whether to nofollow $nf = common_config('nofollow', 'external'); if ($nf == 'never') { $attrs['rel'] = 'external'; } else { $attrs['rel'] = 'nofollow external'; } return XMLStringer::estring('a', $attrs, $url); }
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 && Validate::email($url)) { //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, common_config('attachments', 'process_links')); 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); $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)) { if (common_config('attachments', 'process_links')) { // XXX: this writes to the database. :< $f = File::processNew($longurl); } } if (!empty($f)) { if ($f->getEnclosure()) { $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}"; } // Whether to nofollow $nf = common_config('nofollow', 'external'); if ($nf == 'never') { $attrs['rel'] = 'external'; } else { $attrs['rel'] = 'nofollow external'; } return XMLStringer::estring('a', $attrs, $url); }
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 saveNew($data, $file_id, $url) { $file_redir = new File_redirection(); $file_redir->url = $url; $file_redir->file_id = $file_id; $file_redir->redirections = intval($data['redirects']); $file_redir->httpcode = intval($data['code']); $file_redir->insert(); }
/** * Save a new notice bookmark * * @param Profile $profile To save the bookmark for * @param string $title Title of the bookmark * @param string $url URL of the bookmark * @param array $rawtags array of tags * @param string $description Description of the bookmark * @param array $options Options for the Notice::saveNew() * * @return Notice saved notice */ static function addNew(Profile $actor, $title, $url, array $rawtags, $description, array $options = array()) { $act = new Activity(); $act->verb = ActivityVerb::POST; $act->time = time(); $act->actor = $actor->asActivityObject(); $actobj = new ActivityObject(); $actobj->type = ActivityObject::BOOKMARK; $actobj->title = $title; $actobj->summary = $description; $actobj->extra[] = array('link', array('rel' => 'related', 'href' => $url), null); $act->objects[] = $actobj; $act->enclosures[] = $url; $tags = array(); $replies = array(); // filter "for:nickname" tags foreach ($rawtags as $tag) { if (strtolower(mb_substr($tag, 0, 4)) == 'for:') { // skip if done by caller if (!array_key_exists('replies', $options)) { $nickname = mb_substr($tag, 4); $other = common_relative_profile($actor, $nickname); if (!empty($other)) { $replies[] = $other->getUri(); } } } else { $tags[] = common_canonical_tag($tag); } } $hashtags = array(); $taglinks = array(); foreach ($tags as $tag) { $hashtags[] = '#' . $tag; $attrs = array('href' => Notice_tag::url($tag), 'rel' => $tag, 'class' => 'tag'); $taglinks[] = XMLStringer::estring('a', $attrs, $tag); } // Use user's preferences for short URLs, if possible // FIXME: Should be possible to with the Profile object... try { $user = $actor->getUser(); $shortUrl = File_redirection::makeShort($url, empty($user) ? null : $user); } catch (Exception $e) { // Don't let this stop us. $shortUrl = $url; } // TRANS: Rendered bookmark content. // TRANS: %1$s is a URL, %2$s the bookmark title, %3$s is the bookmark description, // TRANS: %4$s is space separated list of hash tags. $actobj->content = sprintf(_m('<span class="xfolkentry">' . '<a class="taggedlink" href="%1$s">%2$s</a> ' . '<span class="description">%3$s</span> ' . '<span class="meta">%4$s</span>' . '</span>'), htmlspecialchars($url), htmlspecialchars($title), htmlspecialchars($description), implode(' ', $taglinks)); foreach ($tags as $term) { $catEl = new AtomCategory(); $catEl->term = $term; $activity->categories[] = $catEl; } $options = array_merge(array('urls' => array($url), 'rendered' => $rendered, 'tags' => $tags, 'replies' => $replies, 'object_type' => ActivityObject::BOOKMARK), $options); return Notice::saveActivity($act, $actor, $options); }
/** * 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); } } }
static function saveNew($profile, $title, $content, $options = null) { if (is_null($options)) { $options = array(); } $be = new Blog_entry(); $be->id = (string) new UUID(); $be->profile_id = $profile->id; $be->title = $title; // Note: not HTML-protected $be->content = self::purify($content); if (array_key_exists('summary', $options)) { $be->summary = self::purify($options['summary']); } else { // Already purified $be->summary = self::summarize($be->content); } // Don't save an identical summary if ($be->summary == $be->content) { $be->summary = null; } $url = common_local_url('showblogentry', array('id' => $be->id)); if (!array_key_exists('uri', $options)) { $options['uri'] = $url; } $be->uri = $options['uri']; if (!array_key_exists('url', $options)) { $options['url'] = $url; } $be->url = $options['url']; if (!array_key_exists('created', $options)) { $be->created = common_sql_now(); } $be->created = $options['created']; $be->modified = common_sql_now(); $be->insert(); // Use user's preferences for short URLs, if possible try { $user = $profile->getUser(); $shortUrl = File_redirection::makeShort($url, empty($user) ? null : $user); } catch (Exception $e) { // Don't let this stop us. $shortUrl = $url; } // XXX: this might be too long. if (!empty($be->summary)) { $options['rendered'] = $be->summary . ' ' . XMLStringer::estring('a', array('href' => $url, 'class' => 'blog-entry'), _('More...')); $text = html_entity_decode(strip_tags($be->summary), ENT_QUOTES, 'UTF-8'); } else { $options['rendered'] = $be->content; $text = html_entity_decode(strip_tags($be->content), ENT_QUOTES, 'UTF-8'); } if (Notice::contentTooLong($text)) { $text = substr($text, 0, Notice::maxContent() - mb_strlen($shortUrl) - 2) . '… ' . $shortUrl; } // Override this no matter what. $options['object_type'] = self::TYPE; $source = array_key_exists('source', $options) ? $options['source'] : 'web'; $saved = Notice::saveNew($profile->id, $text, $source, $options); return $saved; }
// and only deleting starting with this fetch. while ($dupfile->fetch()) { print "."; $dupfile->delete(); } print "]\n"; } else { print "\nWarning! URL suddenly disappeared from database: {$file->url}\n"; } } $file = new File_redirection(); $file->query('SELECT file_id, url, COUNT(*) AS c FROM file_redirection GROUP BY url HAVING c > 1'); print "\nFound {$file->N} URLs with duplicate entries in file_redirection table"; while ($file->fetch()) { // We've got a URL that is duplicated in the file_redirection table $dupfile = new File_redirection(); $dupfile->url = $file->url; if ($dupfile->find(true)) { print "\nDeleting duplicate entries in file table for URL: {$file->url} ["; // Leave one of the URLs in the database by using ->find(true) // and only deleting starting with this fetch. while ($dupfile->fetch()) { print "."; $dupfile->delete(); } print "]\n"; } else { print "\nWarning! URL suddenly disappeared from database: {$file->url}\n"; } } print "\nDONE.\n";
/** * 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); } } }
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; }
function maybeAddRedir($file_id, $url) { try { $file_redir = File_redirection::getByUrl($url); } catch (NoResultException $e) { $file_redir = new File_redirection(); $file_redir->urlhash = File::hashurl($url); $file_redir->url = $url; $file_redir->file_id = $file_id; $result = $file_redir->insert(); if ($result === false) { common_log_db_error($file_redir, "INSERT", __FILE__); // TRANS: Client exception thrown when a database error was thrown during a file upload operation. throw new ClientException(_('There was a database error while saving your file. Please try again.')); } } }
/** * 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; }
--dry-run look but don't touch END_OF_USERROLE_HELP; require_once INSTALLDIR . '/scripts/commandline.inc'; $dry = have_option('dry-run'); $f = new File(); $f->title = 'h'; $f->mimetype = 'h'; $f->size = 0; $f->protected = 0; $f->find(); echo "Found {$f->N} bad items:\n"; while ($f->fetch()) { echo "{$f->id} {$f->url}"; $data = File_redirection::lookupWhere($f->url); if ($dry) { if (is_array($data)) { echo " (unchanged)\n"; } else { echo " (unchanged, but embedding lookup failed)\n"; } } else { // NULL out the mime/title/size/protected fields $sql = sprintf("UPDATE file " . "SET mimetype=null,title=null,size=null,protected=null " . "WHERE id=%d", $f->id); $f->query($sql); $f->decache(); if (is_array($data)) { Event::handle('EndFileSaveNew', array($f, $data, $f->url)); echo " (ok)\n"; } else {
/** * @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; }
function maybeAddRedir($file_id, $url) { $file_redir = File_redirection::staticGet('url', $url); if (empty($file_redir)) { $file_redir = new File_redirection(); $file_redir->url = $url; $file_redir->file_id = $file_id; $result = $file_redir->insert(); if (!$result) { common_log_db_error($file_redir, "INSERT", __FILE__); // TRANS: Client exception thrown when a database error was thrown during a file upload operation. throw new ClientException(_('There was a database error while saving your file. Please try again.')); } } }
/** * Save a new notice bookmark * * @param Profile $profile To save the bookmark for * @param string $title Title of the bookmark * @param string $url URL of the bookmark * @param mixed $rawtags array of tags or string * @param string $description Description of the bookmark * @param array $options Options for the Notice::saveNew() * * @return Notice saved notice */ static function saveNew($profile, $title, $url, $rawtags, $description, $options = null) { if (!common_valid_http_url($url)) { throw new ClientException(_m('Only web bookmarks can be posted (HTTP or HTTPS).')); } $nb = self::getByURL($profile, $url); if (!empty($nb)) { // TRANS: Client exception thrown when trying to save a new bookmark that already exists. throw new ClientException(_m('Bookmark already exists.')); } if (empty($options)) { $options = array(); } if (array_key_exists('uri', $options)) { $other = Bookmark::getKV('uri', $options['uri']); if (!empty($other)) { // TRANS: Client exception thrown when trying to save a new bookmark that already exists. throw new ClientException(_m('Bookmark already exists.')); } } if (is_string($rawtags)) { if (empty($rawtags)) { $rawtags = array(); } else { $rawtags = preg_split('/[\\s,]+/', $rawtags); } } $nb = new Bookmark(); $nb->id = UUID::gen(); $nb->profile_id = $profile->id; $nb->url = $url; $nb->title = $title; $nb->description = $description; if (array_key_exists('created', $options)) { $nb->created = $options['created']; } else { $nb->created = common_sql_now(); } if (array_key_exists('uri', $options)) { $nb->uri = $options['uri']; } else { // FIXME: hacks to work around router bugs in // queue daemons $r = Router::get(); $path = $r->build('showbookmark', array('id' => $nb->id)); if (empty($path)) { $nb->uri = common_path('bookmark/' . $nb->id, false, false); } else { $nb->uri = common_local_url('showbookmark', array('id' => $nb->id), null, null, false); } } $nb->insert(); $tags = array(); $replies = array(); // filter "for:nickname" tags foreach ($rawtags as $tag) { if (strtolower(mb_substr($tag, 0, 4)) == 'for:') { // skip if done by caller if (!array_key_exists('replies', $options)) { $nickname = mb_substr($tag, 4); $other = common_relative_profile($profile, $nickname); if (!empty($other)) { $replies[] = $other->getUri(); } } } else { $tags[] = common_canonical_tag($tag); } } $hashtags = array(); $taglinks = array(); foreach ($tags as $tag) { $hashtags[] = '#' . $tag; $attrs = array('href' => Notice_tag::url($tag), 'rel' => $tag, 'class' => 'tag'); $taglinks[] = XMLStringer::estring('a', $attrs, $tag); } // Use user's preferences for short URLs, if possible try { $user = User::getKV('id', $profile->id); $shortUrl = File_redirection::makeShort($url, empty($user) ? null : $user); } catch (Exception $e) { // Don't let this stop us. $shortUrl = $url; } // TRANS: Bookmark content. // TRANS: %1$s is a title, %2$s is a short URL, %3$s is the bookmark description, // TRANS: %4$s is space separated list of hash tags. $content = sprintf(_m('"%1$s" %2$s %3$s %4$s'), $title, $shortUrl, $description, implode(' ', $hashtags)); // TRANS: Rendered bookmark content. // TRANS: %1$s is a URL, %2$s the bookmark title, %3$s is the bookmark description, // TRANS: %4$s is space separated list of hash tags. $rendered = sprintf(_m('<span class="xfolkentry">' . '<a class="taggedlink" href="%1$s">%2$s</a> ' . '<span class="description">%3$s</span> ' . '<span class="meta">%4$s</span>' . '</span>'), htmlspecialchars($url), htmlspecialchars($title), htmlspecialchars($description), implode(' ', $taglinks)); $options = array_merge(array('urls' => array($url), 'rendered' => $rendered, 'tags' => $tags, 'replies' => $replies, 'object_type' => ActivityObject::BOOKMARK), $options); if (!array_key_exists('uri', $options)) { $options['uri'] = $nb->uri; } try { $saved = Notice::saveNew($profile->id, $content, array_key_exists('source', $options) ? $options['source'] : 'web', $options); } catch (Exception $e) { $nb->delete(); throw $e; } if (empty($saved)) { $nb->delete(); } return $saved; }
/** * 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; }