Example #1
0
/**
 * return the configured mail backend
 *
 * Uses the $config array to make a mail backend. Cached so it is safe to call
 * more than once.
 *
 * @return Mail backend
 */
function mail_backend()
{
    static $backend = null;
    if (!$backend) {
        $backend = Mail::factory(common_config('mail', 'backend'), common_config('mail', 'params') ? common_config('mail', 'params') : array());
        if (PEAR::isError($backend)) {
            common_server_error($backend->getMessage(), 500);
        }
    }
    return $backend;
}
Example #2
0
/**
 * Update a user from sreg parameters
 * @param User $user
 * @param array $sreg fields from OpenID sreg response
 * @access private
 */
function oid_update_user($user, $sreg)
{
    $profile = $user->getProfile();
    $orig_profile = clone $profile;
    if (!empty($sreg['fullname']) && strlen($sreg['fullname']) <= 255) {
        $profile->fullname = $sreg['fullname'];
    }
    if (!empty($sreg['country'])) {
        if ($sreg['postcode']) {
            # XXX: use postcode to get city and region
            # XXX: also, store postcode somewhere -- it's valuable!
            $profile->location = $sreg['postcode'] . ', ' . $sreg['country'];
        } else {
            $profile->location = $sreg['country'];
        }
    }
    # XXX save language if it's passed
    # XXX save timezone if it's passed
    if (!$profile->update($orig_profile)) {
        // TRANS: OpenID plugin server error.
        common_server_error(_m('Error saving the profile.'));
        return false;
    }
    $orig_user = clone $user;
    if (!empty($sreg['email']) && Validate::email($sreg['email'], common_config('email', 'check_domain'))) {
        $user->email = $sreg['email'];
    }
    if (!$user->update($orig_user)) {
        // TRANS: OpenID plugin server error.
        common_server_error(_m('Error saving the user.'));
        return false;
    }
    return true;
}
Example #3
0
 function show($args, $apidata)
 {
     parent::handle($args);
     if (!in_array($apidata['content-type'], array('xml', 'json'))) {
         $this->clientError(_('API method not found!'), $code = 404);
         return;
     }
     $user = null;
     $email = $this->arg('email');
     $user_id = $this->arg('user_id');
     if ($email) {
         $user = User::staticGet('email', $email);
     } elseif ($user_id) {
         $user = $this->get_user($user_id);
     } elseif (isset($apidata['api_arg'])) {
         $user = $this->get_user($apidata['api_arg']);
     } elseif (isset($apidata['user'])) {
         $user = $apidata['user'];
     }
     if (!$user) {
         // XXX: Twitter returns a random(?) user instead of throwing and err! -- Zach
         $this->client_error(_('Not found.'), 404, $apidata['content-type']);
         return;
     }
     $profile = $user->getProfile();
     if (!$profile) {
         common_server_error(_('User has no profile.'));
         return;
     }
     $twitter_user = $this->twitter_user_array($profile, true);
     // Add in extended user fields offered up by this method
     $twitter_user['created_at'] = $this->date_twitter($profile->created);
     $subbed = DB_DataObject::factory('subscription');
     $subbed->subscriber = $profile->id;
     $subbed_count = (int) $subbed->count() - 1;
     $notices = DB_DataObject::factory('notice');
     $notices->profile_id = $profile->id;
     $notice_count = (int) $notices->count();
     $twitter_user['friends_count'] = is_int($subbed_count) ? $subbed_count : 0;
     $twitter_user['statuses_count'] = is_int($notice_count) ? $notice_count : 0;
     // Other fields Twitter sends...
     $twitter_user['profile_background_color'] = '';
     $twitter_user['profile_background_image_url'] = '';
     $twitter_user['profile_text_color'] = '';
     $twitter_user['profile_link_color'] = '';
     $twitter_user['profile_sidebar_fill_color'] = '';
     $twitter_user['profile_sidebar_border_color'] = '';
     $twitter_user['profile_background_tile'] = 'false';
     $faves = DB_DataObject::factory('fave');
     $faves->user_id = $user->id;
     $faves_count = (int) $faves->count();
     $twitter_user['favourites_count'] = $faves_count;
     $timezone = 'UTC';
     if ($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;
     if (isset($apidata['user'])) {
         if ($apidata['user']->isSubscribed($profile)) {
             $twitter_user['following'] = 'true';
         } else {
             $twitter_user['following'] = 'false';
         }
         // Notifications on?
         $sub = Subscription::pkeyGet(array('subscriber' => $apidata['user']->id, 'subscribed' => $profile->id));
         if ($sub) {
             if ($sub->jabber || $sub->sms) {
                 $twitter_user['notifications'] = 'true';
             } else {
                 $twitter_user['notifications'] = 'false';
             }
         }
     }
     if ($apidata['content-type'] == 'xml') {
         $this->init_document('xml');
         $this->show_twitter_xml_user($twitter_user);
         $this->end_document('xml');
     } elseif ($apidata['content-type'] == 'json') {
         $this->init_document('json');
         $this->show_json_objects($twitter_user);
         $this->end_document('json');
     } else {
         // This is in case 'show' was called via /account/verify_credentials
         // without a format (xml or json).
         header('Content-Type: text/html; charset=utf-8');
         print 'Authorized';
     }
 }
Example #4
0
 function saveReplies()
 {
     // Alternative reply format
     $tname = false;
     if (preg_match('/^T ([A-Z0-9]{1,64}) /', $this->content, $match)) {
         $tname = $match[1];
     }
     // extract all @messages
     $cnt = preg_match_all('/(?:^|\\s)@([a-z0-9]{1,64})/', $this->content, $match);
     $names = array();
     if ($cnt || $tname) {
         // XXX: is there another way to make an array copy?
         $names = $tname ? array_unique(array_merge(array(strtolower($tname)), $match[1])) : array_unique($match[1]);
     }
     $sender = Profile::staticGet($this->profile_id);
     $replied = array();
     // store replied only for first @ (what user/notice what the reply directed,
     // we assume first @ is it)
     for ($i = 0; $i < count($names); $i++) {
         $nickname = $names[$i];
         $recipient = common_relative_profile($sender, $nickname, $this->created);
         if (!$recipient) {
             continue;
         }
         if ($i == 0 && $recipient->id != $sender->id && !$this->reply_to) {
             // Don't save reply to self
             $reply_for = $recipient;
             $recipient_notice = $reply_for->getCurrentNotice();
             if ($recipient_notice) {
                 $orig = clone $this;
                 $this->reply_to = $recipient_notice->id;
                 $this->update($orig);
             }
         }
         // Don't save replies from blocked profile to local user
         $recipient_user = User::staticGet('id', $recipient->id);
         if ($recipient_user && $recipient_user->hasBlocked($sender)) {
             continue;
         }
         $reply = new Reply();
         $reply->notice_id = $this->id;
         $reply->profile_id = $recipient->id;
         $id = $reply->insert();
         if (!$id) {
             $last_error =& PEAR::getStaticProperty('DB_DataObject', 'lastError');
             common_log(LOG_ERR, 'DB error inserting reply: ' . $last_error->message);
             common_server_error(sprintf(_('DB error inserting reply: %s'), $last_error->message));
             return;
         } else {
             $replied[$recipient->id] = 1;
         }
     }
     // Hash format replies, too
     $cnt = preg_match_all('/(?:^|\\s)@#([a-z0-9]{1,64})/', $this->content, $match);
     if ($cnt) {
         foreach ($match[1] as $tag) {
             $tagged = Profile_tag::getTagged($sender->id, $tag);
             foreach ($tagged as $t) {
                 if (!$replied[$t->id]) {
                     // Don't save replies from blocked profile to local user
                     $t_user = User::staticGet('id', $t->id);
                     if ($t_user && $t_user->hasBlocked($sender)) {
                         continue;
                     }
                     $reply = new Reply();
                     $reply->notice_id = $this->id;
                     $reply->profile_id = $t->id;
                     $id = $reply->insert();
                     if (!$id) {
                         common_log_db_error($reply, 'INSERT', __FILE__);
                         return;
                     } else {
                         $replied[$recipient->id] = 1;
                     }
                 }
             }
         }
     }
     foreach (array_keys($replied) as $recipient) {
         $user = User::staticGet('id', $recipient);
         if ($user) {
             mail_notify_attn($user, $this);
         }
     }
 }