Example #1
0
 /**
  * For initializing members of the class.
  *
  * @param array $args misc. arguments
  *
  * @return boolean true
  */
 function prepare($args)
 {
     parent::prepare($args);
     if (!$this->isPost()) {
         throw new ClientException(_('POST only'), 405);
     }
     $this->checkSessionToken();
     $this->url = $this->trimmed('url');
     if (empty($this->url)) {
         throw new ClientException(_('URL is required.'), 400);
     }
     if (!Validate::uri($this->url, array('allowed_schemes' => array('http', 'https')))) {
         throw new ClientException(_('Invalid URL.'), 400);
     }
     $f = File::staticGet('url', $this->url);
     if (empty($url)) {
         $f = File::processNew($this->url);
     }
     // How about now?
     if (!empty($f)) {
         $this->oembed = File_oembed::staticGet('file_id', $f->id);
         if (!empty($this->oembed)) {
             $this->title = $this->oembed->title;
         }
         $this->thumbnail = File_thumbnail::staticGet('file_id', $f->id);
     }
     return true;
 }
Example #2
0
 function delete()
 {
     $f = File::staticGet('id', $this->file_id);
     if (!empty($f)) {
         $f->blowCache();
     }
     return parent::delete();
 }
Example #3
0
 /**
  * For initializing members of the class.
  *
  * @param array $argarray misc. arguments
  *
  * @return boolean true
  */
 function prepare($argarray)
 {
     parent::prepare($argarray);
     $this->file = File::staticGet('id', $this->trimmed('id'));
     if (empty($this->file)) {
         throw new ClientException(_('Unknown URL'));
     }
     $pageArg = $this->trimmed('page');
     $this->page = empty($pageArg) ? 1 : intval($pageArg);
     $this->notices = $this->file->stream(($this->page - 1) * NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
     return true;
 }
Example #4
0
 /**
  * Load attributes based on database arguments
  *
  * Loads all the DB stuff
  *
  * @param array $args $_REQUEST array
  *
  * @return success flag
  */
 function prepare($args)
 {
     parent::prepare($args);
     if ($id = $this->trimmed('attachment')) {
         $this->attachment = File::staticGet($id);
     }
     if (empty($this->attachment)) {
         $this->clientError(_('No such attachment.'), 404);
         return false;
     }
     return true;
 }
Example #5
0
 /**
  * Load attributes based on database arguments
  *
  * Loads all the DB stuff
  *
  * @param array $args $_REQUEST array
  *
  * @return success flag
  */
 function prepare($args)
 {
     parent::prepare($args);
     if ($id = $this->trimmed('attachment')) {
         $this->attachment = File::staticGet($id);
     }
     if (empty($this->attachment)) {
         // TRANS: Client error displayed trying to get a non-existing attachment.
         $this->clientError(_('No such attachment.'), 404);
         return false;
     }
     return true;
 }
Example #6
0
 /**
  * For initializing members of the class.
  *
  * @param array $argarray misc. arguments
  *
  * @return boolean true
  */
 function prepare($argarray)
 {
     parent::prepare($argarray);
     $this->id = $this->trimmed('id');
     if (empty($this->id)) {
         // TRANS: Client exception thrown when no ID parameter was provided.
         throw new ClientException(_('No id parameter.'));
     }
     $this->file = File::staticGet('id', $this->id);
     if (empty($this->file)) {
         // TRANS: Client exception thrown when an invalid ID parameter was provided for a file.
         // TRANS: %d is the provided ID for which the file is not present (number).
         throw new ClientException(sprintf(_('No such file "%d".'), $this->id), 404);
     }
     return true;
 }
Example #7
0
 function handle($args)
 {
     common_debug("in oembed api action");
     $url = $args['url'];
     if (substr(strtolower($url), 0, strlen(common_root_url())) == strtolower(common_root_url())) {
         $path = substr($url, strlen(common_root_url()));
         $r = Router::get();
         $proxy_args = $r->map($path);
         if (!$proxy_args) {
             // TRANS: Server error displayed in oEmbed action when path not found.
             // TRANS: %s is a path.
             $this->serverError(sprintf(_('"%s" not found.'), $path), 404);
         }
         $oembed = array();
         $oembed['version'] = '1.0';
         $oembed['provider_name'] = common_config('site', 'name');
         $oembed['provider_url'] = common_root_url();
         switch ($proxy_args['action']) {
             case 'shownotice':
                 $oembed['type'] = 'link';
                 $id = $proxy_args['notice'];
                 $notice = Notice::staticGet($id);
                 if (empty($notice)) {
                     // TRANS: Server error displayed in oEmbed action when notice not found.
                     // TRANS: %s is a notice.
                     $this->serverError(sprintf(_("Notice %s not found."), $id), 404);
                 }
                 $profile = $notice->getProfile();
                 if (empty($profile)) {
                     // TRANS: Server error displayed in oEmbed action when notice has not profile.
                     $this->serverError(_('Notice has no profile.'), 500);
                 }
                 $authorname = $profile->getFancyName();
                 // TRANS: oEmbed title. %1$s is the author name, %2$s is the creation date.
                 $oembed['title'] = sprintf(_('%1$s\'s status on %2$s'), $authorname, common_exact_date($notice->created));
                 $oembed['author_name'] = $authorname;
                 $oembed['author_url'] = $profile->profileurl;
                 $oembed['url'] = $notice->url ? $notice->url : $notice->uri;
                 $oembed['html'] = $notice->rendered;
                 break;
             case 'attachment':
                 $id = $proxy_args['attachment'];
                 $attachment = File::staticGet($id);
                 if (empty($attachment)) {
                     // TRANS: Server error displayed in oEmbed action when attachment not found.
                     // TRANS: %d is an attachment ID.
                     $this->serverError(sprintf(_('Attachment %s not found.'), $id), 404);
                 }
                 if (empty($attachment->filename) && ($file_oembed = File_oembed::staticGet('file_id', $attachment->id))) {
                     // Proxy the existing oembed information
                     $oembed['type'] = $file_oembed->type;
                     $oembed['provider'] = $file_oembed->provider;
                     $oembed['provider_url'] = $file_oembed->provider_url;
                     $oembed['width'] = $file_oembed->width;
                     $oembed['height'] = $file_oembed->height;
                     $oembed['html'] = $file_oembed->html;
                     $oembed['title'] = $file_oembed->title;
                     $oembed['author_name'] = $file_oembed->author_name;
                     $oembed['author_url'] = $file_oembed->author_url;
                     $oembed['url'] = $file_oembed->url;
                 } else {
                     if (substr($attachment->mimetype, 0, strlen('image/')) == 'image/') {
                         $oembed['type'] = 'photo';
                         if ($attachment->filename) {
                             $filepath = File::path($attachment->filename);
                             $gis = @getimagesize($filepath);
                             if ($gis) {
                                 $oembed['width'] = $gis[0];
                                 $oembed['height'] = $gis[1];
                             } else {
                                 // TODO Either throw an error or find a fallback?
                             }
                         }
                         $oembed['url'] = $attachment->url;
                         $thumb = $attachment->getThumbnail();
                         if ($thumb) {
                             $oembed['thumbnail_url'] = $thumb->url;
                             $oembed['thumbnail_width'] = $thumb->width;
                             $oembed['thumbnail_height'] = $thumb->height;
                         }
                     } else {
                         $oembed['type'] = 'link';
                         $oembed['url'] = common_local_url('attachment', array('attachment' => $attachment->id));
                     }
                 }
                 if ($attachment->title) {
                     $oembed['title'] = $attachment->title;
                 }
                 break;
             default:
                 // TRANS: Server error displayed in oEmbed request when a path is not supported.
                 // TRANS: %s is a path.
                 $this->serverError(sprintf(_('"%s" not supported for oembed requests.'), $path), 501);
         }
         switch ($args['format']) {
             case 'xml':
                 $this->init_document('xml');
                 $this->elementStart('oembed');
                 $this->element('version', null, $oembed['version']);
                 $this->element('type', null, $oembed['type']);
                 if ($oembed['provider_name']) {
                     $this->element('provider_name', null, $oembed['provider_name']);
                 }
                 if ($oembed['provider_url']) {
                     $this->element('provider_url', null, $oembed['provider_url']);
                 }
                 if ($oembed['title']) {
                     $this->element('title', null, $oembed['title']);
                 }
                 if ($oembed['author_name']) {
                     $this->element('author_name', null, $oembed['author_name']);
                 }
                 if ($oembed['author_url']) {
                     $this->element('author_url', null, $oembed['author_url']);
                 }
                 if ($oembed['url']) {
                     $this->element('url', null, $oembed['url']);
                 }
                 if ($oembed['html']) {
                     $this->element('html', null, $oembed['html']);
                 }
                 if ($oembed['width']) {
                     $this->element('width', null, $oembed['width']);
                 }
                 if ($oembed['height']) {
                     $this->element('height', null, $oembed['height']);
                 }
                 if ($oembed['cache_age']) {
                     $this->element('cache_age', null, $oembed['cache_age']);
                 }
                 if ($oembed['thumbnail_url']) {
                     $this->element('thumbnail_url', null, $oembed['thumbnail_url']);
                 }
                 if ($oembed['thumbnail_width']) {
                     $this->element('thumbnail_width', null, $oembed['thumbnail_width']);
                 }
                 if ($oembed['thumbnail_height']) {
                     $this->element('thumbnail_height', null, $oembed['thumbnail_height']);
                 }
                 $this->elementEnd('oembed');
                 $this->end_document('xml');
                 break;
             case 'json':
             case '':
                 $this->init_document('json');
                 print json_encode($oembed);
                 $this->end_document('json');
                 break;
             default:
                 // TRANS: Error message displaying attachments. %s is a raw MIME type (eg 'image/png')
                 $this->serverError(sprintf(_('Content type %s not supported.'), $apidata['content-type']), 501);
         }
     } else {
         // TRANS: Error message displaying attachments. %s is the site's base URL.
         $this->serverError(sprintf(_('Only %s URLs over plain HTTP please.'), common_root_url()), 404);
     }
 }
Example #8
0
 /**
  * Show configured logo.
  *
  * @return nothing
  */
 function showLogo()
 {
     $this->elementStart('address', array('id' => 'site_contact', 'class' => 'vcard'));
     if (Event::handle('StartAddressData', array($this))) {
         if (common_config('singleuser', 'enabled')) {
             $user = User::singleUser();
             $url = common_local_url('showstream', array('nickname' => $user->nickname));
         } else {
             $url = common_local_url('public');
         }
         $this->elementStart('a', array('class' => 'url home bookmark', 'href' => $url));
         if (StatusNet::isHTTPS()) {
             $logoUrl = common_config('site', 'ssllogo');
             if (empty($logoUrl)) {
                 // if logo is an uploaded file, try to fall back to HTTPS file URL
                 $httpUrl = common_config('site', 'logo');
                 if (!empty($httpUrl)) {
                     $f = File::staticGet('url', $httpUrl);
                     if (!empty($f) && !empty($f->filename)) {
                         // this will handle the HTTPS case
                         $logoUrl = File::url($f->filename);
                     }
                 }
             }
         } else {
             $logoUrl = common_config('site', 'logo');
         }
         if (empty($logoUrl) && file_exists(Theme::file('logo.png'))) {
             // This should handle the HTTPS case internally
             $logoUrl = Theme::path('logo.png');
         }
         if (!empty($logoUrl)) {
             $this->element('img', array('class' => 'logo photo', 'src' => $logoUrl, 'alt' => common_config('site', 'name')));
         }
         $this->text(' ');
         $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
         $this->elementEnd('a');
         Event::handle('EndAddressData', array($this));
     }
     $this->elementEnd('address');
 }
Example #9
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;
 }
Example #10
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;
 }
Example #11
0
 function attachments()
 {
     // XXX: cache this
     $att = array();
     $f2p = new File_to_post();
     $f2p->post_id = $this->id;
     if ($f2p->find()) {
         while ($f2p->fetch()) {
             $f = File::staticGet($f2p->file_id);
             if ($f) {
                 $att[] = clone $f;
             }
         }
     }
     return $att;
 }
Example #12
0
 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;
 }
Example #13
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);
         }
     }
 }
Example #14
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);
}
Example #15
0
 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;
 }
Example #16
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) {
        //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);
}
Example #17
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;
 }
Example #18
0
 /**
  * @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;
 }
Example #19
0
 function attachments()
 {
     $keypart = sprintf('notice:file_ids:%d', $this->id);
     $idstr = self::cacheGet($keypart);
     if ($idstr !== false) {
         $ids = explode(',', $idstr);
     } else {
         $ids = array();
         $f2p = new File_to_post();
         $f2p->post_id = $this->id;
         if ($f2p->find()) {
             while ($f2p->fetch()) {
                 $ids[] = $f2p->file_id;
             }
         }
         self::cacheSet($keypart, implode(',', $ids));
     }
     $att = array();
     foreach ($ids as $id) {
         $f = File::staticGet('id', $id);
         if (!empty($f)) {
             $att[] = clone $f;
         }
     }
     return $att;
 }