Exemple #1
0
 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;
 }
 /**
  * Handle the request
  *
  * @param array $args $_REQUEST data (unused)
  *
  * @return void
  */
 protected function handle()
 {
     parent::handle();
     $noticeurl = common_path('notice/', StatusNet::isHTTPS());
     $instanceurl = common_path('', StatusNet::isHTTPS());
     // remove protocol for the comparison below
     $noticeurl_wo_protocol = preg_replace('(^https?://)', '', $noticeurl);
     $instanceurl_wo_protocol = preg_replace('(^https?://)', '', $instanceurl);
     $url_wo_protocol = preg_replace('(^https?://)', '', $this->url);
     // find local notice
     if (strpos($url_wo_protocol, $noticeurl_wo_protocol) === 0) {
         $possible_notice_id = str_replace($noticeurl_wo_protocol, '', $url_wo_protocol);
         if (ctype_digit($possible_notice_id)) {
             $notice = Notice::getKV('id', $possible_notice_id);
         } else {
             $this->clientError("Notice not found.", 404);
         }
     }
     if (!$notice instanceof Notice) {
         // TRANS: Client error displayed in oEmbed action when notice not found.
         // TRANS: %s is a notice.
         $this->clientError(sprintf(_("Notice %s not found."), $this->id), 404);
     }
     $profile = $notice->getProfile();
     if (!$profile instanceof Profile) {
         // TRANS: Server error displayed in oEmbed action when notice has not profile.
         $this->serverError(_('Notice has no profile.'), 500);
     }
     $authorname = $profile->getFancyName();
     $oembed = array();
     $oembed['version'] = '1.0';
     $oembed['provider_name'] = common_config('site', 'name');
     $oembed['provider_url'] = common_root_url();
     $oembed['type'] = 'link';
     // TRANS: oEmbed title. %1$s is the author name, %2$s is the creation date.
     $oembed['title'] = ApiAction::dateTwitter($notice->created) . ' (Qvitter)';
     $oembed['author_name'] = $authorname;
     $oembed['author_url'] = $profile->profileurl;
     $oembed['url'] = $notice->getUrl();
     $oembed['html'] = $notice->getRendered();
     // maybe add thumbnail
     $attachments = $notice->attachments();
     if (!empty($attachments)) {
         foreach ($attachments as $attachment) {
             if (is_object($attachment)) {
                 try {
                     $thumb = $attachment->getThumbnail();
                 } catch (ServerException $e) {
                     //
                 }
                 if (!empty($thumb) && method_exists('File_thumbnail', 'url')) {
                     try {
                         $thumb_url = File_thumbnail::url($thumb->filename);
                         $oembed['thumbnail_url'] = $thumb_url;
                         break;
                         // only first one
                     } catch (ClientException $e) {
                         //
                     }
                 }
             }
         }
     }
     if ($this->format == 'json') {
         $this->initDocument('json');
         print json_encode($oembed);
         $this->endDocument('json');
     } elseif ($this->format == 'xml') {
         $this->initDocument('xml');
         $this->elementStart('oembed');
         foreach (array('version', 'type', 'provider_name', 'provider_url', 'title', 'author_name', 'author_url', 'url', 'html') as $key) {
             if (isset($oembed[$key]) && $oembed[$key] != '') {
                 $this->element($key, null, $oembed[$key]);
             }
         }
         $this->elementEnd('oembed');
         $this->endDocument('xml');
     } else {
         $this->serverError(sprintf(_('Format %s not supported.'), $this->format), 501);
     }
 }