Beispiel #1
0
function add_twitter_user($twitter_id, $screen_name)
{
    // Clear out any bad old foreign_users with the new user's legit URL
    // This can happen when users move around or fakester accounts get
    // repoed, and things like that.
    $luser = Foreign_user::getForeignUser($twitter_id, TWITTER_SERVICE);
    if (!empty($luser)) {
        $result = $luser->delete();
        if ($result != false) {
            common_log(LOG_INFO, "Twitter bridge - removed old Twitter user: {$screen_name} ({$twitter_id}).");
        }
    }
    $fuser = new Foreign_user();
    $fuser->nickname = $screen_name;
    $fuser->uri = 'http://twitter.com/' . $screen_name;
    $fuser->id = $twitter_id;
    $fuser->service = TWITTER_SERVICE;
    $fuser->created = common_sql_now();
    $result = $fuser->insert();
    if (empty($result)) {
        common_log(LOG_WARNING, "Twitter bridge - failed to add new Twitter user: {$twitter_id} - {$screen_name}.");
        common_log_db_error($fuser, 'INSERT', __FILE__);
    } else {
        common_log(LOG_INFO, "Twitter bridge - Added new Twitter user: {$screen_name} ({$twitter_id}).");
    }
    return $result;
}
Beispiel #2
0
 static function top($transport)
 {
     $qi = new Queue_item();
     $qi->transport = $transport;
     $qi->orderBy('created');
     $qi->whereAdd('claimed is null');
     $qi->limit(1);
     $cnt = $qi->find(true);
     if ($cnt) {
         # XXX: potential race condition
         # can we force it to only update if claimed is still null
         # (or old)?
         common_log(LOG_INFO, 'claiming queue item = ' . $qi->notice_id . ' for transport ' . $transport);
         $orig = clone $qi;
         $qi->claimed = common_sql_now();
         $result = $qi->update($orig);
         if ($result) {
             common_log(LOG_INFO, 'claim succeeded.');
             return $qi;
         } else {
             common_log(LOG_INFO, 'claim failed.');
         }
     }
     $qi = null;
     return null;
 }
 static function add($peopletag, $profile)
 {
     if ($peopletag->private) {
         return false;
     }
     if (Event::handle('StartSubscribePeopletag', array($peopletag, $profile))) {
         $args = array('profile_tag_id' => $peopletag->id, 'profile_id' => $profile->id);
         $existing = Profile_tag_subscription::pkeyGet($args);
         if (!empty($existing)) {
             return $existing;
         }
         $sub = new Profile_tag_subscription();
         $sub->profile_tag_id = $peopletag->id;
         $sub->profile_id = $profile->id;
         $sub->created = common_sql_now();
         $result = $sub->insert();
         if (!$result) {
             common_log_db_error($sub, 'INSERT', __FILE__);
             // TRANS: Exception thrown when inserting a list subscription in the database fails.
             throw new Exception(_('Adding list subscription failed.'));
         }
         $ptag = Profile_list::getKV('id', $peopletag->id);
         $ptag->subscriberCount(true);
         Event::handle('EndSubscribePeopletag', array($peopletag, $profile));
         return $ptag;
     }
 }
 /**
  * @param mixed $transports name of a single queue or array of queues to pull from
  *                          If not specified, checks all queues in the system.
  */
 static function top($transports = null)
 {
     $qi = new Queue_item();
     if ($transports) {
         if (is_array($transports)) {
             // @fixme use safer escaping
             $list = implode("','", array_map(array($qi, 'escape'), $transports));
             $qi->whereAdd("transport in ('{$list}')");
         } else {
             $qi->transport = $transports;
         }
     }
     $qi->orderBy('created');
     $qi->whereAdd('claimed is null');
     $qi->limit(1);
     $cnt = $qi->find(true);
     if ($cnt) {
         // XXX: potential race condition
         // can we force it to only update if claimed is still null
         // (or old)?
         common_log(LOG_INFO, 'claiming queue item id = ' . $qi->id . ' for transport ' . $qi->transport);
         $orig = clone $qi;
         $qi->claimed = common_sql_now();
         $result = $qi->update($orig);
         if ($result) {
             common_log(LOG_INFO, 'claim succeeded.');
             return $qi;
         } else {
             common_log(LOG_INFO, 'claim failed.');
         }
     }
     $qi = null;
     return null;
 }
Beispiel #5
0
 static function saveNew($from, $to, $content, $source)
 {
     $sender = Profile::staticGet('id', $from);
     if (!$sender->hasRight(Right::NEWMESSAGE)) {
         // TRANS: Client exception thrown when a user tries to send a direct message while being banned from sending them.
         throw new ClientException(_('You are banned from sending direct messages.'));
     }
     $msg = new Message();
     $msg->from_profile = $from;
     $msg->to_profile = $to;
     $msg->content = common_shorten_links($content);
     $msg->rendered = common_render_text($content);
     $msg->created = common_sql_now();
     $msg->source = $source;
     $result = $msg->insert();
     if (!$result) {
         common_log_db_error($msg, 'INSERT', __FILE__);
         // TRANS: Message given when a message could not be stored on the server.
         return _('Could not insert message.');
     }
     $orig = clone $msg;
     $msg->uri = common_local_url('showmessage', array('message' => $msg->id));
     $result = $msg->update($orig);
     if (!$result) {
         common_log_db_error($msg, 'UPDATE', __FILE__);
         // TRANS: Message given when a message could not be updated on the server.
         return _('Could not update message with new URI.');
     }
     return $msg;
 }
 public static function saveNew(Profile $subscriber, Profile $subscribed)
 {
     $rq = new Subscription_queue();
     $rq->subscriber = $subscriber->id;
     $rq->subscribed = $subscribed->id;
     $rq->created = common_sql_now();
     $rq->insert();
     return $rq;
 }
 public static function saveNew(Profile $profile, User_group $group)
 {
     $rq = new Group_join_queue();
     $rq->profile_id = $profile->id;
     $rq->group_id = $group->id;
     $rq->created = common_sql_now();
     $rq->insert();
     return $rq;
 }
 public static function fromUrl($url, $depth = 0)
 {
     common_debug('MentionURL: trying to find a profile for ' . $url);
     $url = preg_replace('#https?://#', 'https://', $url);
     try {
         $profile = Profile::fromUri($url);
     } catch (UnknownUriException $ex) {
     }
     if (!$profile instanceof Profile) {
         $profile = self::findProfileByProfileURL($url);
     }
     $url = str_replace('https://', 'http://', $url);
     if (!$profile instanceof Profile) {
         try {
             $profile = Profile::fromUri($url);
         } catch (UnknownUriException $ex) {
         }
     }
     if (!$profile instanceof Profile) {
         $profile = self::findProfileByProfileURL($url);
     }
     if (!$profile instanceof Profile) {
         $hcard = mention_url_representative_hcard($url);
         if (!$hcard) {
             return null;
         }
         $mention_profile = new Mention_url_profile();
         $mention_profile->query('BEGIN');
         $profile = new Profile();
         $profile->profileurl = $hcard['url'][0];
         $profile->fullname = $hcard['name'][0];
         preg_match('/\\/([^\\/]+)\\/*$/', $profile->profileurl, $matches);
         if (!$hcard['nickname']) {
             $hcard['nickname'] = array($matches[1]);
         }
         $profile->nickname = $hcard['nickname'][0];
         $profile->created = common_sql_now();
         $mention_profile->profile_id = $profile->insert();
         if (!$mention_profile->profile_id) {
             $mention_profile->query('ROLLBACK');
             return null;
         }
         $mention_profile->profileurl = $profile->profileurl;
         if (!$mention_profile->insert()) {
             $mention_profile->query('ROLLBACK');
             if ($depth > 0) {
                 return null;
             } else {
                 return self::fromUrl($url, $depth + 1);
             }
         } else {
             $mention_profile->query('COMMIT');
         }
     }
     return $profile;
 }
Beispiel #9
0
 /**
  * Start a search subscription!
  *
  * @param profile $profile subscriber
  * @param string $search subscribee
  * @return SearchSub
  */
 static function start(Profile $profile, $search)
 {
     $ts = new SearchSub();
     $ts->search = $search;
     $ts->profile_id = $profile->id;
     $ts->created = common_sql_now();
     $ts->insert();
     self::blow('searchsub:by_profile:%d', $profile->id);
     return $ts;
 }
 /**
  * Factory method for creating a new conversation.
  *
  * Use this for locally initiated conversations. Remote notices should
  * preferrably supply their own conversation URIs in the OStatus feed.
  *
  * @return Conversation the new conversation DO
  */
 static function create($uri = null, $created = null)
 {
     // Be aware that the Notice does not have an id yet since it's not inserted!
     $conv = new Conversation();
     $conv->created = $created ?: common_sql_now();
     $conv->uri = $uri ?: sprintf('%s%s=%s:%s=%s', TagURI::mint(), 'objectType', 'thread', 'nonce', common_random_hexstr(8));
     // This insert throws exceptions on failure
     $conv->insert();
     return $conv;
 }
Beispiel #11
0
 /**
  * Start a tag subscription!
  *
  * @param profile $profile subscriber
  * @param string $tag subscribee
  * @return TagSub
  */
 static function start(Profile $profile, $tag)
 {
     $ts = new TagSub();
     $ts->tag = $tag;
     $ts->profile_id = $profile->id;
     $ts->created = common_sql_now();
     $ts->insert();
     self::blow('tagsub:by_profile:%d', $profile->id);
     return $ts;
 }
Beispiel #12
0
 static function generateNew()
 {
     $cons = new Consumer();
     $rand = common_good_rand(16);
     $cons->seed = $rand;
     $cons->consumer_key = md5(time() + $rand);
     $cons->consumer_secret = md5(md5(time() + time() + $rand));
     $cons->created = common_sql_now();
     return $cons;
 }
 public static function saveNew(Profile $subscriber, Profile $subscribed)
 {
     if (self::exists($subscriber, $subscribed)) {
         throw new AlreadyFulfilledException(_('This subscription request is already in progress.'));
     }
     $rq = new Subscription_queue();
     $rq->subscriber = $subscriber->id;
     $rq->subscribed = $subscribed->id;
     $rq->created = common_sql_now();
     $rq->insert();
     return $rq;
 }
 /**
  * Register a user with a username on a given provider
  * @param User User object
  * @param string username on the given provider
  * @param provider_name string name of the provider
  * @return mixed User_username instance if the registration succeeded, false if it did not
  */
 static function register($user, $username, $provider_name)
 {
     $user_username = new User_username();
     $user_username->user_id = $user->id;
     $user_username->provider_name = $provider_name;
     $user_username->username = $username;
     $user_username->created = common_sql_now();
     if ($user_username->insert()) {
         return $user_username;
     } else {
         return false;
     }
 }
 function send($gm, $profile)
 {
     $gmp = new Group_message_profile();
     $gmp->group_message_id = $gm->id;
     $gmp->to_profile = $profile->id;
     $gmp->created = common_sql_now();
     $gmp->insert();
     // If it's not for the author, send email notification
     if ($gm->from_profile != $profile->id) {
         $gmp->notify();
     }
     return $gmp;
 }
Beispiel #16
0
 public static function saveNew(Notice $notice, Profile $profile, $reason = null)
 {
     $att = new Attention();
     $att->notice_id = $notice->getID();
     $att->profile_id = $profile->getID();
     $att->reason = $reason;
     $att->created = common_sql_now();
     $result = $att->insert();
     if ($result === false) {
         throw new Exception('Could not saveNew in Attention');
     }
     return $att;
 }
Beispiel #17
0
 static function join($group_id, $profile_id)
 {
     $member = new Group_member();
     $member->group_id = $group_id;
     $member->profile_id = $profile_id;
     $member->created = common_sql_now();
     $result = $member->insert();
     if (!$result) {
         common_log_db_error($member, 'INSERT', __FILE__);
         throw new Exception(_("Group join failed."));
     }
     return true;
 }
 /**
  * Record a record of sending the reminder
  *
  * @param string $type      type of reminder
  * @param Object $object    an object with a 'code' property
  *                          (Confirm_address or Invitation)
  * @param int    $days      Number of days after the code was created
  * @return int   $result    row ID of the new reminder record
  */
 static function recordReminder($type, $object, $days)
 {
     $reminder = new Email_reminder();
     $reminder->type = $type;
     $reminder->code = $object->code;
     $reminder->days = $days;
     $reminder->sent = $reminder->created = common_sql_now();
     $result = $reminder->insert();
     if (empty($result)) {
         common_log_db_error($reminder, 'INSERT', __FILE__);
         throw new ServerException(_m('Database error inserting reminder record.'));
     }
     return $result;
 }
 /**
  * Save a remote notice source record; this helps indicate how trusted we are.
  * @param string $method
  */
 public static function saveNew(Notice $notice, Ostatus_profile $oprofile, $method)
 {
     $osource = new Ostatus_source();
     $osource->notice_id = $notice->id;
     $osource->profile_uri = $oprofile->uri;
     $osource->method = $method;
     $osource->created = common_sql_now();
     if ($osource->insert()) {
         return true;
     } else {
         common_log_db_error($osource, 'INSERT', __FILE__);
         return false;
     }
 }
Beispiel #20
0
function oid_link_user($id, $canonical, $display)
{
    global $_PEAR;
    $oid = new User_openid();
    $oid->user_id = $id;
    $oid->canonical = $canonical;
    $oid->display = $display;
    $oid->created = common_sql_now();
    if (!$oid->insert()) {
        $err =& $_PEAR->getStaticProperty('DB_DataObject', 'lastError');
        return false;
    }
    return true;
}
 /**
  * Saves an object reference into the queue item table.
  * @return boolean true on success
  * @throws ServerException on failure
  */
 public function enqueue($object, $queue)
 {
     $qi = new Queue_item();
     $qi->frame = $this->encode($object);
     $qi->transport = $queue;
     $qi->created = common_sql_now();
     $result = $qi->insert();
     if (!$result) {
         common_log_db_error($qi, 'INSERT', __FILE__);
         throw new ServerException('DB error inserting queue item');
     }
     $this->stats('enqueued', $queue);
     return true;
 }
Beispiel #22
0
 static function addPortfolio($fields)
 {
     extract($fields);
     $portfolio = new Portfolio();
     $portfolio->name = $name;
     $portfolio->owner = $owner;
     $portfolio->created = common_sql_now();
     $portfolio->modified = common_sql_now();
     $result = $portfolio->insert();
     if (!$result) {
         common_log_db_error($portfolio, 'INSERT', __FILE__);
         return false;
     }
     return $portfolio;
 }
Beispiel #23
0
 static function write($id, $session_data)
 {
     self::logdeb("Writing session '{$id}'");
     $session = Session::staticGet('id', $id);
     if (empty($session)) {
         $session = new Session();
         $session->id = $id;
         $session->session_data = $session_data;
         $session->created = common_sql_now();
         return $session->insert();
     } else {
         $session->session_data = $session_data;
         return $session->update();
     }
 }
Beispiel #24
0
 /**
  * Method to add a user to a group.
  * In most cases, you should call Profile->joinGroup() instead.
  *
  * @param integer $group_id   Group to add to
  * @param integer $profile_id Profile being added
  * 
  * @return Group_member new membership object
  */
 static function join($group_id, $profile_id)
 {
     $member = new Group_member();
     $member->group_id = $group_id;
     $member->profile_id = $profile_id;
     $member->created = common_sql_now();
     $member->uri = self::newURI($profile_id, $group_id, $member->created);
     $result = $member->insert();
     if (!$result) {
         common_log_db_error($member, 'INSERT', __FILE__);
         // TRANS: Exception thrown when joining a group fails.
         throw new Exception(_("Group join failed."));
     }
     return $member;
 }
Beispiel #25
0
 /**
  * Factory method for creating a new conversation.
  *
  * Use this for locally initiated conversations. Remote notices should
  * preferrably supply their own conversation URIs in the OStatus feed.
  *
  * @return Conversation the new conversation DO
  */
 static function create(Notice $notice, $uri = null)
 {
     if (empty($notice->id)) {
         throw new ServerException(_('Tried to create conversation for not yet inserted notice'));
     }
     $conv = new Conversation();
     $conv->created = common_sql_now();
     $conv->id = $notice->id;
     $conv->uri = $uri ?: sprintf('%s%s=%d:%s=%s:%s=%x', TagURI::mint(), 'noticeId', $notice->id, 'objectType', 'thread', 'crc32', crc32($notice->content));
     $result = $conv->insert();
     if ($result === false) {
         common_log_db_error($conv, 'INSERT', __FILE__);
         throw new ServerException(_('Failed to create conversation for notice'));
     }
     return $conv;
 }
 function makeBackupFile($user)
 {
     // XXX: this is pretty lose-y;  try another way
     $tmpdir = sys_get_temp_dir() . '/offline-backup/' . $user->nickname . '/' . common_date_iso8601(common_sql_now());
     common_log(LOG_INFO, 'Writing backup data to ' . $tmpdir . ' for ' . $user->nickname);
     mkdir($tmpdir, 0700, true);
     $this->dumpNotices($user, $tmpdir);
     $this->dumpFaves($user, $tmpdir);
     $this->dumpSubscriptions($user, $tmpdir);
     $this->dumpSubscribers($user, $tmpdir);
     $this->dumpGroups($user, $tmpdir);
     $fileName = File::filename($user->getProfile(), "backup", "application/atom+xml");
     $fullPath = File::path($fileName);
     $this->makeActivityFeed($user, $tmpdir, $fullPath);
     $this->delTree($tmpdir);
     return $fileName;
 }
 function makeNew($user)
 {
     $login_token = Login_token::staticGet('user_id', $user->id);
     if (!empty($login_token)) {
         $login_token->delete();
     }
     $login_token = new Login_token();
     $login_token->user_id = $user->id;
     $login_token->token = common_good_rand(16);
     $login_token->created = common_sql_now();
     $result = $login_token->insert();
     if (!$result) {
         common_log_db_error($login_token, 'INSERT', __FILE__);
         throw new Exception(sprintf(_('Could not create login token for %s'), $user->nickname));
     }
     return $login_token;
 }
Beispiel #28
0
 /**
  * Factory method for creating a new conversation
  *
  * @return Conversation the new conversation DO
  */
 static function create()
 {
     $conv = new Conversation();
     $conv->created = common_sql_now();
     $id = $conv->insert();
     if (empty($id)) {
         common_log_db_error($conv, 'INSERT', __FILE__);
         return null;
     }
     $orig = clone $conv;
     $orig->uri = common_local_url('conversation', array('id' => $id), null, null, false);
     $result = $orig->update($conv);
     if (empty($result)) {
         common_log_db_error($conv, 'UPDATE', __FILE__);
         return null;
     }
     return $conv;
 }
Beispiel #29
0
 public static function saveNew(Notice $notice, Profile $target, $reason = null)
 {
     try {
         $att = Attention::getByKeys(['notice_id' => $notice->getID(), 'profile_id' => $target->getID()]);
         throw new AlreadyFulfilledException('Attention already exists with reason: ' . var_export($att->reason, true));
     } catch (NoResultException $e) {
         $att = new Attention();
         $att->notice_id = $notice->getID();
         $att->profile_id = $target->getID();
         $att->reason = $reason;
         $att->created = common_sql_now();
         $result = $att->insert();
         if ($result === false) {
             throw new Exception('Failed Attention::saveNew for notice id==' . $notice->getID() . ' target id==' . $target->getID() . ', reason=="' . $reason . '"');
         }
     }
     return $att;
 }
Beispiel #30
0
function add_twitter_user($twitter_id, $screen_name)
{
    // Otherwise, create a new Twitter user
    $fuser = DB_DataObject::factory('foreign_user');
    $fuser->nickname = $screen_name;
    $fuser->uri = 'http://twitter.com/' . $screen_name;
    $fuser->id = $twitter_id;
    $fuser->service = TWITTER_SERVICE;
    // Twitter
    $fuser->created = common_sql_now();
    $result = $fuser->insert();
    if (!$result) {
        common_debug("Twitter bridge - failed to add new Twitter user: {$twitter_id} - {$screen_name}.");
        common_log_db_error($fuser, 'INSERT', __FILE__);
        return false;
    }
    common_debug("Twitter bridge - Added new Twitter user: {$screen_name} ({$twitter_id}).");
    return true;
}