/** * 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; }
/** * 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; }
/** * @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 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); }
/** * Shorten a URL with the current user's configured shortening service, * or ur1.ca if configured, or not at all if no shortening is set up. * * @param string $long_url original URL * @param User $user to specify a particular user's options * @param boolean $force Force shortening (used when notice is too long) * @return string may return the original URL if shortening failed * * @fixme provide a way to specify a particular shortener */ function common_shorten_url($long_url, User $user = null, $force = false) { $long_url = trim($long_url); $user = common_current_user(); $maxUrlLength = User_urlshortener_prefs::maxUrlLength($user); // $force forces shortening even if it's not strictly needed // I doubt URL shortening is ever 'strictly' needed. - ESP if (mb_strlen($long_url) < $maxUrlLength && !$force) { return $long_url; } $shortenerName = User_urlshortener_prefs::urlShorteningService($user); if (Event::handle('StartShortenUrl', array($long_url, $shortenerName, &$shortenedUrl))) { if ($shortenerName == 'internal') { $f = File::processNew($long_url); if (empty($f)) { return $long_url; } else { $shortenedUrl = common_local_url('redirecturl', array('id' => $f->id)); return $shortenedUrl; } } else { return $long_url; } } else { //URL was shortened, so return the result return trim($shortenedUrl); } }
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); }
/** * Record URL links from the notice. Needed to get thumbnail records * for referenced photo and video posts, etc. * * @param Notice $notice * @param object $status */ function saveStatusAttachments($notice, $status) { if (common_config('attachments', 'process_links')) { if (!empty($status->entities) && !empty($status->entities->urls)) { foreach ($status->entities->urls as $url) { File::processNew($url->url, $notice->id); } } } }
/** * @private callback */ function saveUrl($url, Notice $notice) { try { File::processNew($url, $notice); } catch (ServerException $e) { // Could not save URL. Log it? } }
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; }
/** * @private callback */ function saveUrl($data) { list($url, $notice_id) = $data; File::processNew($url, $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; }
/** * @private callback */ function saveUrl($url, $notice_id) { File::processNew($url, $notice_id); }
/** * Record URL links from the notice. Needed to get thumbnail records * for referenced photo and video posts, etc. * * @param Notice $notice * @param object $status */ function saveStatusAttachments(Notice $notice, $status) { if (common_config('attachments', 'process_links')) { if (!empty($status->entities) && !empty($status->entities->urls)) { foreach ($status->entities->urls as $url) { try { File::processNew($url->url, $notice); } catch (ServerException $e) { // Could not process attached URL } } } } }