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; } if ($media !== 'image') { return true; } // If there is a local filename, it is either a local file already or has already been downloaded. if (!empty($file->filename)) { return true; } $this->checkWhitelist($file->getUrl()); // First we download the file to memory and test whether it's actually an image file $imgData = HTTPClient::quickGet($file->getUrl()); common_debug(sprintf('Downloading remote file id==%u with URL: %s', $file->id, $file->getUrl())); $info = @getimagesizefromstring($imgData); if ($info === false) { throw new UnsupportedMediaException(_('Remote file format was not identified as an image.'), $file->getUrl()); } elseif (!$info[0] || !$info[1]) { throw new UnsupportedMediaException(_('Image file had impossible geometry (0 width or height)')); } $filehash = hash(File::FILEHASH_ALG, $imgData); try { // Exception will be thrown before $file is set to anything, so old $file value will be kept $file = File::getByHash($filehash); //FIXME: Add some code so we don't have to store duplicate File rows for same hash files. } catch (NoResultException $e) { $filename = $filehash . '.' . common_supported_mime_to_ext($info['mime']); $fullpath = File::path($filename); // Write the file to disk if it doesn't exist yet. Throw Exception on failure. if (!file_exists($fullpath) && file_put_contents($fullpath, $imgData) === false) { throw new ServerException(_('Could not write downloaded file to disk.')); } // Updated our database for the file record $orig = clone $file; $file->filehash = $filehash; $file->filename = $filename; $file->width = $info[0]; // array indexes documented on php.net: $file->height = $info[1]; // https://php.net/manual/en/function.getimagesize.php // Throws exception on failure. $file->updateWithKeys($orig, 'id'); } // Get rid of the file from memory unset($imgData); $imgPath = $file->getPath(); return false; }
/** * Perform or fake an oEmbed lookup for the given resource. * * Some known hosts are whitelisted with API endpoints where we * know they exist but autodiscovery data isn't available. * If autodiscovery links are missing and we don't recognize the * host, we'll pass it to noembed.com's public service which * will either proxy or fake info on a lot of sites. * * A few hosts are blacklisted due to known problems with oohembed, * in which case we'll look up the info another way and return * equivalent data. * * Throws exceptions on failure. * * @param string $url * @param array $params * @return object */ public static function getObject($url, $params = array()) { common_log(LOG_INFO, 'Checking for remote URL metadata for ' . $url); // TODO: Make this class something like UrlMetadata, or use a dataobject? $metadata = new stdClass(); if (Event::handle('GetRemoteUrlMetadata', array($url, &$metadata))) { // If that event didn't return anything, try downloading the body and parse it $body = HTTPClient::quickGet($url); // DOMDocument::loadHTML may throw warnings on unrecognized elements, // and notices on unrecognized namespaces. $old = error_reporting(error_reporting() & ~(E_WARNING | E_NOTICE)); $dom = new DOMDocument(); $ok = $dom->loadHTML($body); unset($body); // storing the DOM in memory is enough... error_reporting($old); if (!$ok) { throw new oEmbedHelper_BadHtmlException(); } Event::handle('GetRemoteUrlMetadataFromDom', array($url, $dom, &$metadata)); } return self::normalize($metadata); }
/** * Download and update given avatar image * * @param string $url * @return Avatar The Avatar we have on disk. (seldom used) * @throws Exception in various failure cases */ public function updateAvatar($url, $force = false) { try { // If avatar URL differs: update. If URLs were identical but we're forced: update. if ($url == $this->avatar && !$force) { // If there's no locally stored avatar, throw an exception and continue fetching below. $avatar = Avatar::getUploaded($this->localProfile()) instanceof Avatar; return $avatar; } } catch (NoAvatarException $e) { // No avatar available, let's fetch it. } if (!common_valid_http_url($url)) { // TRANS: Server exception. %s is a URL. throw new ServerException(sprintf(_m('Invalid avatar URL %s.'), $url)); } $self = $this->localProfile(); // @todo FIXME: This should be better encapsulated // ripped from oauthstore.php (for old OMB client) $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar'); try { $imgData = HTTPClient::quickGet($url); // Make sure it's at least an image file. ImageFile can do the rest. if (false === getimagesizefromstring($imgData)) { throw new UnsupportedMediaException(_('Downloaded group avatar was not an image.')); } file_put_contents($temp_filename, $imgData); unset($imgData); // No need to carry this in memory. if ($this->isGroup()) { $id = $this->group_id; } else { $id = $this->profile_id; } $imagefile = new ImageFile(null, $temp_filename); $filename = Avatar::filename($id, image_type_to_extension($imagefile->type), null, common_timestamp()); rename($temp_filename, Avatar::path($filename)); } catch (Exception $e) { unlink($temp_filename); throw $e; } // @todo FIXME: Hardcoded chmod is lame, but seems to be necessary to // keep from accidentally saving images from command-line (queues) // that can't be read from web server, which causes hard-to-notice // problems later on: // // http://status.net/open-source/issues/2663 chmod(Avatar::path($filename), 0644); $self->setOriginal($filename); $orig = clone $this; $this->avatar = $url; $this->update($orig); return Avatar::getUploaded($self); }
$validate = new Validate(); if (empty($args[0]) || !$validate->uri($args[0])) { print "{$helptext}"; exit(1); } $feedurl = $args[0]; $skip = have_option('skip') ? intval(get_option_value('skip')) : 0; $count = have_option('count') ? intval(get_option_value('count')) : 0; $sub = FeedSub::getKV('uri', $feedurl); if (!$sub) { print "Feed {$feedurl} is not subscribed.\n"; exit(1); } // Fetch the URL try { $xml = HTTPClient::quickGet($feedurl, 'text/html,application/xhtml+xml'); } catch (Exception $e) { echo sprintf("Could not fetch feedurl %s (%d).\n", $e->getMessage(), $e->getCode()); exit(1); } $feed = new DOMDocument(); if (!$feed->loadXML($xml)) { print "Bad XML.\n"; exit(1); } if ($skip || $count) { $entries = $feed->getElementsByTagNameNS(ActivityUtils::ATOM, 'entry'); $remove = array(); for ($i = 0; $i < $skip && $i < $entries->length; $i++) { $item = $entries->item($i); if ($item) {
protected function storeRemoteFileThumbnail(File_thumbnail $thumbnail) { if (!empty($thumbnail->filename) && file_exists($thumbnail->getPath())) { throw new AlreadyFulfilledException(sprintf('A thumbnail seems to already exist for remote file with id==%u', $thumbnail->file_id)); } $url = $thumbnail->getUrl(); $this->checkWhitelist($url); // First we download the file to memory and test whether it's actually an image file // FIXME: To support remote video/whatever files, this needs reworking. common_debug(sprintf('Downloading remote thumbnail for file id==%u with thumbnail URL: %s', $thumbnail->file_id, $url)); $imgData = HTTPClient::quickGet($url); $info = @getimagesizefromstring($imgData); if ($info === false) { throw new UnsupportedMediaException(_('Remote file format was not identified as an image.'), $url); } elseif (!$info[0] || !$info[1]) { throw new UnsupportedMediaException(_('Image file had impossible geometry (0 width or height)')); } // We'll trust sha256 not to have collision issues any time soon :) $filename = hash('sha256', $imgData) . '.' . common_supported_mime_to_ext($info['mime']); $fullpath = File_thumbnail::path($filename); // Write the file to disk. Throw Exception on failure if (!file_exists($fullpath) && file_put_contents($fullpath, $imgData) === false) { throw new ServerException(_('Could not write downloaded file to disk.')); } // Get rid of the file from memory unset($imgData); // Updated our database for the file record $orig = clone $thumbnail; $thumbnail->filename = $filename; $thumbnail->width = $info[0]; // array indexes documented on php.net: $thumbnail->height = $info[1]; // https://php.net/manual/en/function.getimagesize.php // Throws exception on failure. $thumbnail->updateWithKeys($orig, 'file_id'); }