예제 #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 _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;
 }
예제 #3
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;
 }
예제 #4
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;
 }
예제 #5
0
 function maybeAddRedir($file_id, $url)
 {
     $file_redir = File_redirection::staticGet('url', $url);
     if (empty($file_redir)) {
         $file_redir = new File_redirection();
         $file_redir->url = $url;
         $file_redir->file_id = $file_id;
         $result = $file_redir->insert();
         if (!$result) {
             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.'));
         }
     }
 }
예제 #6
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;
 }
예제 #7
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);
         }
     }
 }
예제 #8
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;
 }