/** * Class handler. * * @param array $args query arguments * * @return boolean false if nickname or user isn't found */ protected function handle() { parent::handle(); $nickname = $this->trimmed('nickname'); if (!$nickname) { // TRANS: Client error displayed trying to get an avatar without providing a nickname. $this->clientError(_('No nickname.')); } $size = $this->trimmed('size') ?: 'original'; $user = User::getKV('nickname', $nickname); if (!$user) { // TRANS: Client error displayed trying to get an avatar for a non-existing user. $this->clientError(_('No such user.')); } $profile = $user->getProfile(); if (!$profile) { // TRANS: Error message displayed when referring to a user without a profile. $this->clientError(_('User has no profile.')); } if ($size === 'original') { try { $avatar = Avatar::getUploaded($profile); $url = $avatar->displayUrl(); } catch (NoAvatarException $e) { $url = Avatar::defaultImage(AVATAR_PROFILE_SIZE); } } else { $url = $profile->avatarUrl($size); } common_redirect($url, 302); }
/** * Download and update given avatar image * * @param string $url * @return Avatar The Avatar we have on disk. (seldom used) * @throws Exception in various failure cases */ public function updateAvatar($url, $force = false) { try { // If avatar URL differs: update. If URLs were identical but we're forced: update. if ($url == $this->avatar && !$force) { // If there's no locally stored avatar, throw an exception and continue fetching below. $avatar = Avatar::getUploaded($this->localProfile()) instanceof Avatar; return $avatar; } } catch (NoAvatarException $e) { // No avatar available, let's fetch it. } if (!common_valid_http_url($url)) { // TRANS: Server exception. %s is a URL. throw new ServerException(sprintf(_m('Invalid avatar URL %s.'), $url)); } $self = $this->localProfile(); // @todo FIXME: This should be better encapsulated // ripped from oauthstore.php (for old OMB client) $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar'); try { $imgData = HTTPClient::quickGet($url); // Make sure it's at least an image file. ImageFile can do the rest. if (false === getimagesizefromstring($imgData)) { throw new UnsupportedMediaException(_('Downloaded group avatar was not an image.')); } file_put_contents($temp_filename, $imgData); unset($imgData); // No need to carry this in memory. if ($this->isGroup()) { $id = $this->group_id; } else { $id = $this->profile_id; } $imagefile = new ImageFile(null, $temp_filename); $filename = Avatar::filename($id, image_type_to_extension($imagefile->type), null, common_timestamp()); rename($temp_filename, Avatar::path($filename)); } catch (Exception $e) { unlink($temp_filename); throw $e; } // @todo FIXME: Hardcoded chmod is lame, but seems to be necessary to // keep from accidentally saving images from command-line (queues) // that can't be read from web server, which causes hard-to-notice // problems later on: // // http://status.net/open-source/issues/2663 chmod(Avatar::path($filename), 0644); $self->setOriginal($filename); $orig = clone $this; $this->avatar = $url; $this->update($orig); return Avatar::getUploaded($self); }
function qvitterTwitterUserArray($profile) { $twitter_user = array(); try { $user = $profile->getUser(); } catch (NoSuchUserException $e) { $user = null; } $twitter_user['id'] = intval($profile->id); $twitter_user['name'] = $profile->getBestName(); $twitter_user['screen_name'] = $profile->nickname; $twitter_user['location'] = $profile->location ? $profile->location : null; $twitter_user['description'] = $profile->bio ? $profile->bio : null; // TODO: avatar url template (example.com/user/avatar?size={x}x{y}) $twitter_user['profile_image_url'] = Avatar::urlByProfile($profile, AVATAR_STREAM_SIZE); $twitter_user['profile_image_url_https'] = $twitter_user['profile_image_url']; // START introduced by qvitter API, not necessary for StatusNet API $twitter_user['profile_image_url_profile_size'] = Avatar::urlByProfile($profile, AVATAR_PROFILE_SIZE); try { $avatar = Avatar::getUploaded($profile); $origurl = $avatar->displayUrl(); } catch (Exception $e) { $origurl = $twitter_user['profile_image_url_profile_size']; } $twitter_user['profile_image_url_original'] = $origurl; $twitter_user['groups_count'] = $profile->getGroupCount(); foreach (array('linkcolor', 'backgroundcolor') as $key) { $twitter_user[$key] = Profile_prefs::getConfigData($profile, 'theme', $key); } // END introduced by qvitter API, not necessary for StatusNet API $twitter_user['url'] = $profile->homepage ? $profile->homepage : null; $twitter_user['protected'] = !empty($user) && $user->private_stream ? true : false; $twitter_user['followers_count'] = $profile->subscriberCount(); // Note: some profiles don't have an associated user $twitter_user['friends_count'] = $profile->subscriptionCount(); $twitter_user['created_at'] = ApiAction::dateTwitter($profile->created); $timezone = 'UTC'; if (!empty($user) && $user->timezone) { $timezone = $user->timezone; } $t = new DateTime(); $t->setTimezone(new DateTimeZone($timezone)); $twitter_user['utc_offset'] = $t->format('Z'); $twitter_user['time_zone'] = $timezone; $twitter_user['statuses_count'] = $profile->noticeCount(); // Is the requesting user following this user? $twitter_user['following'] = false; $twitter_user['statusnet_blocking'] = false; $logged_in_profile = null; if (common_logged_in()) { $logged_in_profile = Profile::current(); $twitter_user['following'] = $logged_in_profile->isSubscribed($profile); $twitter_user['statusnet_blocking'] = $logged_in_profile->hasBlocked($profile); } // StatusNet-specific $twitter_user['statusnet_profile_url'] = $profile->profileurl; Event::handle('TwitterUserArray', array($profile, &$twitter_user, $logged_in_profile, array())); return $twitter_user; }
function twitterUserArray($profile, $get_notice = false) { $twitter_user = array(); try { $user = $profile->getUser(); } catch (NoSuchUserException $e) { $user = null; } $twitter_user['id'] = intval($profile->id); $twitter_user['name'] = $profile->getBestName(); $twitter_user['screen_name'] = $profile->nickname; $twitter_user['location'] = $profile->location ? $profile->location : null; $twitter_user['description'] = $profile->bio ? $profile->bio : null; // TODO: avatar url template (example.com/user/avatar?size={x}x{y}) $twitter_user['profile_image_url'] = Avatar::urlByProfile($profile, AVATAR_STREAM_SIZE); $twitter_user['profile_image_url_https'] = $twitter_user['profile_image_url']; // START introduced by qvitter API, not necessary for StatusNet API $twitter_user['profile_image_url_profile_size'] = Avatar::urlByProfile($profile, AVATAR_PROFILE_SIZE); try { $avatar = Avatar::getUploaded($profile); $origurl = $avatar->displayUrl(); } catch (Exception $e) { $origurl = $twitter_user['profile_image_url_profile_size']; } $twitter_user['profile_image_url_original'] = $origurl; $twitter_user['groups_count'] = $profile->getGroupCount(); foreach (array('linkcolor', 'backgroundcolor') as $key) { $twitter_user[$key] = Profile_prefs::getConfigData($profile, 'theme', $key); } // END introduced by qvitter API, not necessary for StatusNet API $twitter_user['url'] = $profile->homepage ? $profile->homepage : null; $twitter_user['protected'] = !empty($user) && $user->private_stream ? true : false; $twitter_user['followers_count'] = $profile->subscriberCount(); // Note: some profiles don't have an associated user $twitter_user['friends_count'] = $profile->subscriptionCount(); $twitter_user['created_at'] = $this->dateTwitter($profile->created); $timezone = 'UTC'; if (!empty($user) && $user->timezone) { $timezone = $user->timezone; } $t = new DateTime(); $t->setTimezone(new DateTimeZone($timezone)); $twitter_user['utc_offset'] = $t->format('Z'); $twitter_user['time_zone'] = $timezone; $twitter_user['statuses_count'] = $profile->noticeCount(); // Is the requesting user following this user? // These values might actually also mean "unknown". Ambiguity issues? $twitter_user['following'] = false; $twitter_user['statusnet_blocking'] = false; $twitter_user['notifications'] = false; if ($this->scoped instanceof Profile) { try { $sub = Subscription::getSubscription($this->scoped, $profile); // Notifications on? $twitter_user['following'] = true; $twitter_user['statusnet_blocking'] = $this->scoped->hasBlocked($profile); $twitter_user['notifications'] = $sub->jabber || $sub->sms; } catch (NoResultException $e) { // well, the values are already false... } } if ($get_notice) { $notice = $profile->getCurrentNotice(); if ($notice instanceof Notice) { // don't get user! $twitter_user['status'] = $this->twitterStatusArray($notice, false); } } // StatusNet-specific $twitter_user['statusnet_profile_url'] = $profile->profileurl; // The event call to handle NoticeSimpleStatusArray lets plugins add data to the output array Event::handle('TwitterUserArray', array($profile, &$twitter_user, $this->scoped, array())); return $twitter_user; }
public function asActivityObject() { $object = new ActivityObject(); if (Event::handle('StartActivityObjectFromProfile', array($this, &$object))) { $object->type = $this->getObjectType(); $object->id = $this->getUri(); $object->title = $this->getBestName(); $object->link = $this->getUrl(); $object->summary = $this->getDescription(); try { $avatar = Avatar::getUploaded($this); $object->avatarLinks[] = AvatarLink::fromAvatar($avatar); } catch (NoAvatarException $e) { // Could not find an original avatar to link } $sizes = array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE); foreach ($sizes as $size) { $alink = null; try { $avatar = Avatar::byProfile($this, $size); $alink = AvatarLink::fromAvatar($avatar); } catch (NoAvatarException $e) { $alink = new AvatarLink(); $alink->type = 'image/png'; $alink->height = $size; $alink->width = $size; $alink->url = Avatar::defaultImage($size); } $object->avatarLinks[] = $alink; } if (isset($this->lat) && isset($this->lon)) { $object->geopoint = (double) $this->lat . ' ' . (double) $this->lon; } $object->poco = PoCo::fromProfile($this); if ($this->isLocal()) { $object->extra[] = array('followers', array('url' => common_local_url('subscribers', array('nickname' => $this->getNickname())))); } Event::handle('EndActivityObjectFromProfile', array($this, &$object)); } return $object; }
protected function updateAvatar($twuser, Profile $profile) { $path_parts = pathinfo($twuser->profile_image_url); $ext = isset($path_parts['extension']) ? '.' . $path_parts['extension'] : ''; // some lack extension $img_root = basename($path_parts['basename'], '_normal' . $ext); // cut off extension $filename = "Twitter_{$twuser->id}_{$img_root}_{$this->avatarsizename}{$ext}"; try { $avatar = Avatar::getUploaded($profile); if ($avatar->filename === $filename) { return null; } common_debug(__METHOD__ . " - Updating profile avatar (profile_id={$profile->id}) " . "from {$avatar->filename} to {$filename}"); // else we continue with creating a new avatar } catch (NoAvatarException $e) { // Avatar was not found. We can catch NoAvatarException or FileNotFoundException // but generally we just want to continue creating a new avatar. common_debug(__METHOD__ . " - No avatar found for (profile_id={$profile->id})"); } $url = "{$path_parts['dirname']}/{$img_root}_{$this->avatarsizename}{$ext}"; $mediatype = $this->getMediatype(mb_substr($ext, 1)); try { $this->newAvatar($profile, $url, $filename, $mediatype); } catch (Exception $e) { if (file_exists(Avatar::path($filename))) { unlink(Avatar::path($filename)); } return false; } return true; }
public function showPage() { header('Content-Type: application/rdf+xml'); $this->startXML(); $this->elementStart('rdf:RDF', array('xmlns:rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'xmlns:rdfs' => 'http://www.w3.org/2000/01/rdf-schema#', 'xmlns:geo' => 'http://www.w3.org/2003/01/geo/wgs84_pos#', 'xmlns:bio' => 'http://purl.org/vocab/bio/0.1/', 'xmlns:sioc' => 'http://rdfs.org/sioc/ns#', 'xmlns' => 'http://xmlns.com/foaf/0.1/')); // This is the document about the user $this->showPpd('', $this->user->getUri()); // Would be nice to tell if they were a Person or not (e.g. a #person usertag?) $this->elementStart('Agent', array('rdf:about' => $this->user->getUri())); if ($this->user->email) { $this->element('mbox_sha1sum', null, sha1('mailto:' . $this->user->email)); } if ($this->profile->fullname) { $this->element('name', null, $this->profile->fullname); } if ($this->profile->homepage) { $this->element('homepage', array('rdf:resource' => $this->profile->homepage)); } if ($this->profile->profileurl) { $this->element('weblog', array('rdf:resource' => $this->profile->profileurl)); } if ($this->profile->bio) { $this->element('bio:olb', null, $this->profile->bio); } $location = $this->profile->getLocation(); if ($location) { $attr = array(); if ($location->getRdfURL()) { $attr['rdf:about'] = $location->getRdfURL(); } $location_name = $location->getName(); $this->elementStart('based_near'); $this->elementStart('geo:SpatialThing', $attr); if ($location_name) { $this->element('name', null, $location_name); } if ($location->lat) { $this->element('geo:lat', null, $location->lat); } if ($location->lon) { $this->element('geo:long', null, $location->lon); } if ($location->getURL()) { $this->element('page', array('rdf:resource' => $location->getURL())); } $this->elementEnd('geo:SpatialThing'); $this->elementEnd('based_near'); } try { $avatar = Avatar::getUploaded($this->profile); $this->elementStart('img'); $this->elementStart('Image', array('rdf:about' => $avatar->displayUrl())); foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) { try { $scaled = $this->profile->getAvatar($size); $this->elementStart('thumbnail'); $this->element('Image', array('rdf:about' => $scaled->displayUrl())); $this->elementEnd('thumbnail'); } catch (Exception $e) { // This avatar did not exist } } $this->elementEnd('Image'); $this->elementEnd('img'); } catch (NoAvatarException $e) { // No avatar for this user! } $person = $this->showMicrobloggingAccount($this->profile, common_root_url(), $this->user->getUri(), true, false); // Get people who subscribe to user $sub = new Subscription(); $sub->subscribed = $this->profile->id; $sub->whereAdd('subscriber != subscribed'); if ($sub->find()) { while ($sub->fetch()) { $profile = Profile::getKV('id', $sub->subscriber); if (!$profile instanceof Profile) { common_debug('Got a bad subscription: ' . print_r($sub, true)); continue; } $other_uri = $profile->getUri(); if (array_key_exists($other_uri, $person)) { $person[$other_uri][0] = BOTH; } else { $person[$other_uri] = array(LISTENER, $profile->id, $profile->nickname, $profile->isLocal() ? 'local' : 'remote'); } unset($profile); } } unset($sub); foreach ($person as $uri => $p) { list($type, $id, $nickname, $local) = $p; if ($type == BOTH) { $this->element('knows', array('rdf:resource' => $uri)); } } $this->elementEnd('Agent'); foreach ($person as $uri => $p) { $foaf_url = null; list($type, $id, $nickname, $local) = $p; if ($local == 'local') { $foaf_url = common_local_url('foaf', array('nickname' => $nickname)); } $profile = Profile::getKV($id); $this->elementStart('Agent', array('rdf:about' => $uri)); if ($type == BOTH) { $this->element('knows', array('rdf:resource' => $this->user->getUri())); } $this->showMicrobloggingAccount($profile, $local == 'local' ? common_root_url() : null, $uri, false, $type == LISTENER || $type == BOTH); if ($foaf_url) { $this->element('rdfs:seeAlso', array('rdf:resource' => $foaf_url)); } $this->elementEnd('Agent'); if ($foaf_url) { $this->showPpd($foaf_url, $uri); } $profile->free(); $profile = null; unset($profile); } $this->elementEnd('rdf:RDF'); $this->endXML(); }
function showUploadForm() { $user = common_current_user(); $profile = $user->getProfile(); if (!$profile) { common_log_db_error($user, 'SELECT', __FILE__); // TRANS: Error message displayed when referring to a user without a profile. $this->serverError(_('User has no profile.')); } $this->elementStart('form', array('enctype' => 'multipart/form-data', 'method' => 'post', 'id' => 'form_settings_avatar', 'class' => 'form_settings', 'action' => common_local_url('avatarsettings'))); $this->elementStart('fieldset'); // TRANS: Avatar upload page form legend. $this->element('legend', null, _('Avatar settings')); $this->hidden('token', common_session_token()); if (Event::handle('StartAvatarFormData', array($this))) { $this->elementStart('ul', 'form_data'); try { $original = Avatar::getUploaded($profile); $this->elementStart('li', array('id' => 'avatar_original', 'class' => 'avatar_view')); // TRANS: Header on avatar upload page for thumbnail of originally uploaded avatar (h2). $this->element('h2', null, _("Original")); $this->elementStart('div', array('id' => 'avatar_original_view')); $this->element('img', array('src' => $original->displayUrl(), 'width' => $original->width, 'height' => $original->height, 'alt' => $user->nickname)); $this->elementEnd('div'); $this->elementEnd('li'); } catch (NoAvatarException $e) { // No original avatar found! } try { $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE); $this->elementStart('li', array('id' => 'avatar_preview', 'class' => 'avatar_view')); // TRANS: Header on avatar upload page for thumbnail of to be used rendition of uploaded avatar (h2). $this->element('h2', null, _("Preview")); $this->elementStart('div', array('id' => 'avatar_preview_view')); $this->element('img', array('src' => $avatar->displayUrl(), 'width' => AVATAR_PROFILE_SIZE, 'height' => AVATAR_PROFILE_SIZE, 'alt' => $user->nickname)); $this->elementEnd('div'); if (!empty($avatar->filename)) { // TRANS: Button on avatar upload page to delete current avatar. $this->submit('delete', _m('BUTTON', 'Delete')); } $this->elementEnd('li'); } catch (NoAvatarException $e) { // No previously uploaded avatar to preview. } $this->elementStart('li', array('id' => 'settings_attach')); $this->element('input', array('name' => 'MAX_FILE_SIZE', 'type' => 'hidden', 'id' => 'MAX_FILE_SIZE', 'value' => ImageFile::maxFileSizeInt())); $this->element('input', array('name' => 'avatarfile', 'type' => 'file', 'id' => 'avatarfile')); $this->elementEnd('li'); $this->elementEnd('ul'); $this->elementStart('ul', 'form_actions'); $this->elementStart('li'); // TRANS: Button on avatar upload page to upload an avatar. $this->submit('upload', _m('BUTTON', 'Upload')); $this->elementEnd('li'); $this->elementEnd('ul'); } Event::handle('EndAvatarFormData', array($this)); $this->elementEnd('fieldset'); $this->elementEnd('form'); }
static function newSize(Profile $target, $width) { $width = intval($width); if ($width < 1 || $width > common_config('avatar', 'maxsize')) { // TRANS: An error message when avatar size is unreasonable throw new Exception(_m('Avatar size too large')); } // So far we only have square avatars and I don't have time to // rewrite support for non-square ones right now ;) $height = $width; $original = Avatar::getUploaded($target); $imagefile = new ImageFile(null, Avatar::path($original->filename)); $filename = Avatar::filename($target->getID(), image_type_to_extension($imagefile->preferredType()), $width, common_timestamp()); $imagefile->resizeTo(Avatar::path($filename), array('width' => $width, 'height' => $height)); $scaled = clone $original; $scaled->original = false; $scaled->width = $width; $scaled->height = $height; $scaled->filename = $filename; $scaled->created = common_sql_now(); if (!$scaled->insert()) { // TRANS: An error message when unable to insert avatar data into the db throw new Exception(_m('Could not insert new avatar data to database')); } // Return the new avatar object return $scaled; }