示例#1
0
文件: File.php 项目: Br3nda/StatusNet
 /**
  * @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;
 }
示例#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 (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);
}
示例#3
0
文件: util.php 项目: himmelex/NTW
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);
}
示例#4
0
 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
         $file = File::staticGet('url', $long_url);
         if (empty($file)) {
             // Check if the target URL is itself a redirect...
             $redir_data = File_redirection::where($long_url);
             if (is_array($redir_data)) {
                 // We haven't seen the target URL before.
                 // Save file and embedding data about it!
                 $file = File::saveNew($redir_data, $long_url);
                 $file_id = $file->id;
                 if (!empty($redir_data['oembed']['json'])) {
                     File_oembed::saveNew($redir_data['oembed']['json'], $file_id);
                 }
             } else {
                 if (is_string($redir_data)) {
                     // The file is a known redirect target.
                     $file = File::staticGet('url', $redir_data);
                     if (empty($file)) {
                         // @fixme should we save a new one?
                         // this case was triggering sometimes for redirects
                         // with unresolvable targets; found while fixing
                         // "can't linkify" bugs with shortened links to
                         // SSL sites with cert issues.
                         return null;
                     }
                     $file_id = $file->id;
                 }
             }
         } else {
             $file_id = $file->id;
         }
         $file_redir = File_redirection::staticGet('url', $short_url);
         if (empty($file_redir)) {
             $file_redir = new File_redirection();
             $file_redir->url = $short_url;
             $file_redir->file_id = $file_id;
             $file_redir->insert();
         }
         return $short_url;
     }
     return null;
 }
示例#5
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);
}
示例#6
0
文件: File.php 项目: himmelex/NTW
 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;
 }
示例#7
0
 function _userMakeShort($long_url)
 {
     $short_url = common_shorten_url($long_url);
     if (!empty($short_url) && $short_url != $long_url) {
         $short_url = (string) $short_url;
         // store it
         $file = File::staticGet('url', $long_url);
         if (empty($file)) {
             // Check if the target URL is itself a redirect...
             $redir_data = File_redirection::where($long_url);
             if (is_array($redir_data)) {
                 // We haven't seen the target URL before.
                 // Save file and embedding data about it!
                 $file = File::saveNew($redir_data, $long_url);
                 $file_id = $file->id;
                 if (!empty($redir_data['oembed']['json'])) {
                     File_oembed::saveNew($redir_data['oembed']['json'], $file_id);
                 }
             } else {
                 if (is_string($redir_data)) {
                     // The file is a known redirect target.
                     $file = File::staticGet('url', $redir_data);
                     $file_id = $file->id;
                 }
             }
         } else {
             $file_id = $file->id;
         }
         $file_redir = File_redirection::staticGet('url', $short_url);
         if (empty($file_redir)) {
             $file_redir = new File_redirection();
             $file_redir->url = $short_url;
             $file_redir->file_id = $file_id;
             $file_redir->insert();
         }
         return $short_url;
     }
     return null;
 }
示例#8
0
文件: File.php 项目: Br3nda/laconica
 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;
 }
示例#9
0
 /**
  * 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);
         }
     }
 }
示例#10
0
 /**
  * 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);
         }
     }
 }
示例#11
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;
 }
示例#13
0
 function _userMakeShort($long_url)
 {
     $short_url = common_shorten_url($long_url);
     if (!empty($short_url) && $short_url != $long_url) {
         $short_url = (string) $short_url;
         // store it
         $file = File::staticGet('url', $long_url);
         if (empty($file)) {
             $redir_data = File_redirection::where($long_url);
             $file = File::saveNew($redir_data, $long_url);
             $file_id = $file->id;
             if (!empty($redir_data['oembed']['json'])) {
                 File_oembed::saveNew($redir_data['oembed']['json'], $file_id);
             }
         } else {
             $file_id = $file->id;
         }
         $file_redir = File_redirection::staticGet('url', $short_url);
         if (empty($file_redir)) {
             $file_redir = new File_redirection();
             $file_redir->url = $short_url;
             $file_redir->file_id = $file_id;
             $file_redir->insert();
         }
         return $short_url;
     }
     return null;
 }
示例#14
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
  *
  * @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;
 }