public function __construct($file_id) { $this->file = File::getKV('id', $file_id); if (!$this->file instanceof File) { throw new ServerException('No File ID supplied to exception'); } parent::__construct('Thumbnail not generated', $this->file->getPath()); }
/** * Load attributes based on database arguments * * Loads all the DB stuff * * @param array $args $_REQUEST array * * @return success flag */ protected function prepare(array $args = array()) { parent::prepare($args); if ($id = $this->trimmed('attachment')) { $this->attachment = File::getKV($id); } if (!$this->attachment instanceof File) { // TRANS: Client error displayed trying to get a non-existing attachment. $this->clientError(_('No such attachment.'), 404); } return true; }
/** * For initializing members of the class. * * @param array $argarray misc. arguments * * @return boolean true */ function prepare($argarray) { parent::prepare($argarray); $this->file = File::getKV('id', $this->trimmed('id')); if (empty($this->file)) { // TRANS: Client exception thrown when an unknown URL is provided. throw new ClientException(_m('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; }
/** * 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::getKV('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; }
/** * 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); } } }
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::getKV('url', $longurl); if (!$f instanceof File) { 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); }
*/ define('INSTALLDIR', realpath(dirname(__FILE__) . '/..')); $shortoptions = 'i::yv'; $longoptions = array('id=', 'yes', 'verbose'); $helptext = <<<END_OF_HELP nukefile.php [options] deletes a file and related notices from the database -i --id ID of the file -v --verbose Be verbose (print the contents of the notices deleted). END_OF_HELP; require_once INSTALLDIR . '/scripts/commandline.inc'; if (have_option('i', 'id')) { $id = get_option_value('i', 'id'); $file = File::getKV('id', $id); if (!$file instanceof File) { print "Can't find file with ID {$id}\n"; exit(1); } } else { print "You must provide a file ID.\n"; exit(1); } $verbose = have_option('v', 'verbose'); if (!have_option('y', 'yes')) { try { $filename = $file->getFilename(); } catch (Exception $e) { $filename = '(remote file or no filename)'; }
public function updateUrl($url) { $file = File::getKV('urlhash', self::hashurl($url)); if ($file instanceof File) { throw new ServerException('URL already exists in DB'); } $sql = 'UPDATE %1$s SET urlhash=%2$s, url=%3$s WHERE urlhash=%4$s;'; $result = $this->query(sprintf($sql, $this->tableName(), $this->_quote((string) self::hashurl($url)), $this->_quote((string) $url), $this->_quote((string) $this->urlhash))); if ($result === false) { common_log_db_error($this, 'UPDATE', __FILE__); throw new ServerException("Could not UPDATE {$this->tableName()}.url"); } return $result; }
public function getFile() { if (empty($this->file) && $this->file_id) { $this->file = File::getKV('id', $this->file_id); } return $this->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 $file = File::getKV('url', $long_url); if ($file instanceof File) { $file_id = $file->getID(); } else { // 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->getID(); } else { if (is_string($redir_data)) { // The file is a known redirect target. $file = File::getKV('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->getID(); } } } $file_redir = File_redirection::getKV('url', $short_url); if (!$file_redir instanceof File_redirection) { $file_redir = new File_redirection(); $file_redir->urlhash = File::hashurl($short_url); $file_redir->url = $short_url; $file_redir->file_id = $file_id; $file_redir->insert(); } return $short_url; } return null; }
protected function handle() { parent::handle(); $url = $this->trimmed('url'); if (substr(strtolower($url), 0, strlen(common_root_url())) !== strtolower(common_root_url())) { // TRANS: Error message displaying attachments. %s is the site's base URL. $this->clientError(sprintf(_('oEmbed data will only be provided for %s URLs.'), common_root_url()), 400); } $path = substr($url, strlen(common_root_url())); $r = Router::get(); $proxy_args = $r->map($path); if (!$proxy_args) { // TRANS: Client error displayed in oEmbed action when path not found. // TRANS: %s is a path. $this->clientError(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::getKV($id); if (empty($notice)) { // TRANS: Client error displayed in oEmbed action when notice not found. // TRANS: %s is a notice. $this->clientError(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->getUrl(); $oembed['html'] = $notice->rendered; break; case 'attachment': $id = $proxy_args['attachment']; $attachment = File::getKV($id); if (empty($attachment)) { // TRANS: Client error displayed in oEmbed action when attachment not found. // TRANS: %d is an attachment ID. $this->clientError(sprintf(_('Attachment %s not found.'), $id), 404); } if (empty($attachment->filename) && ($file_oembed = File_oembed::getKV('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->getUrl(); } elseif (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->getUrl(); try { $thumb = $attachment->getThumbnail(); $oembed['thumbnail_url'] = $thumb->getUrl(); $oembed['thumbnail_width'] = $thumb->width; $oembed['thumbnail_height'] = $thumb->height; unset($thumb); } catch (UnsupportedMediaException $e) { // No thumbnail data available } } 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 ($this->trimmed('format')) { case 'xml': $this->init_document('xml'); $this->elementStart('oembed'); foreach (array('version', 'type', 'provider_name', 'provider_url', 'title', 'author_name', 'author_url', 'url', 'html', 'width', 'height', 'cache_age', 'thumbnail_url', 'thumbnail_width', 'thumbnail_height') as $key) { if (isset($oembed[$key]) && $oembed[$key] != '') { $this->element($key, null, $oembed[$key]); } } $this->elementEnd('oembed'); $this->end_document('xml'); break; case 'json': case null: $this->init_document('json'); $this->raw(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); } }
/** * Show configured logo. * * @return nothing */ function showLogo() { $this->elementStart('address', array('id' => 'site_contact', 'class' => 'h-card')); if (Event::handle('StartAddressData', array($this))) { if (common_config('singleuser', 'enabled')) { $user = User::singleUser(); $url = common_local_url('showstream', array('nickname' => $user->nickname)); } else { if (common_logged_in()) { $cur = common_current_user(); $url = common_local_url('all', array('nickname' => $cur->nickname)); } else { $url = common_local_url('public'); } } $this->elementStart('a', array('class' => 'home bookmark', 'href' => $url)); if (GNUsocial::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::getKV('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 u-photo p-name', 'src' => $logoUrl, 'alt' => common_config('site', 'name'))); } $this->elementEnd('a'); Event::handle('EndAddressData', array($this)); } $this->elementEnd('address'); }