Example #1
0
 /**
  * Save a new file record.
  *
  * @param array $redir_data lookup data eg from File_redirection::where()
  * @param string $given_url
  * @return File
  */
 public static function saveNew(array $redir_data, $given_url)
 {
     $file = null;
     try {
         // I don't know why we have to keep doing this but we run a last check to avoid
         // uniqueness bugs.
         $file = File::getByUrl($given_url);
         return $file;
     } catch (NoResultException $e) {
         // We don't have the file's URL since before, so let's continue.
     }
     $file = new File();
     $file->url = $given_url;
     if (!empty($redir_data['protected'])) {
         $file->protected = $redir_data['protected'];
     }
     if (!empty($redir_data['title'])) {
         $file->title = $redir_data['title'];
     }
     if (!empty($redir_data['type'])) {
         $file->mimetype = $redir_data['type'];
     }
     if (!empty($redir_data['size'])) {
         $file->size = intval($redir_data['size']);
     }
     if (isset($redir_data['time']) && $redir_data['time'] > 0) {
         $file->date = intval($redir_data['time']);
     }
     $file->saveFile();
     return $file;
 }
Example #2
0
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);
}
Example #3
0
 /**
  * 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;
 }
 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;
 }
 /**
  * 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;
     }
 }