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.')); } } }
static 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 try { $file = File::getByUrl($long_url); } catch (NoResultException $e) { // Check if the target URL is itself a redirect... $redir = File_redirection::where($long_url); $file = $redir->getFile(); if (empty($file->id)) { $file->saveFile(); } } // Now we definitely have a File object in $file try { $file_redir = File_redirection::getByUrl($short_url); } catch (NoResultException $e) { $file_redir = new File_redirection(); $file_redir->urlhash = File::hashurl($short_url); $file_redir->url = $short_url; $file_redir->file_id = $file->getID(); $file_redir->insert(); } return $short_url; } return null; }
/** * 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; }
/** * Check if this URL is a redirect and return redir info. * If a File record is present for this URL, it is not considered a redirect. * If a File_redirection record is present for this URL, the recorded target is returned. * * If no File or File_redirect record is present, the URL is hit and any * redirects are followed, up to 10 levels or until a protected URL is * reached. * * @param string $in_url * @param boolean $discover true to attempt dereferencing the redirect if we don't know it already * @return mixed one of: * string - target URL, if this is a direct link or a known redirect * array - redirect info if this is an *unknown* redirect: * associative array with the following elements: * code: HTTP status code * redirects: count of redirects followed * url: URL string of final target * type (optional): MIME type from Content-Type header * size (optional): byte size from Content-Length header * time (optional): timestamp from Last-Modified header */ public function where($in_url, $discover = true) { // let's see if we know this... try { $a = File::getByUrl($in_url); // this is a direct link to $a->url return $a->url; } catch (NoResultException $e) { try { $b = File_redirection::getByUrl($in_url); // this is a redirect to $b->file_id $a = File::getKV('id', $b->file_id); return $a->url; } catch (NoResultException $e) { // Oh well, let's keep going } } if ($discover) { $ret = File_redirection::lookupWhere($in_url); return $ret; } else { // No manual dereferencing; leave the unknown URL as is. return $in_url; } }