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; }
function update_location($args, $apidata) { parent::handle($args); if ($_SERVER['REQUEST_METHOD'] != 'POST') { $this->clientError(_('This method requires a POST.'), 400, $apidata['content-type']); return; } $location = trim($this->arg('location')); if (!is_null($location) && mb_strlen($location) > 255) { // XXX: But Twitter just truncates and runs with it. -- Zach $this->clientError(_('That\'s too long. Max notice size is 255 chars.'), 406, $apidate['content-type']); return; } $user = $apidata['user']; // Always the auth user $profile = $user->getProfile(); $orig_profile = clone $profile; $profile->location = $location; $result = $profile->update($orig_profile); if (empty($result)) { common_log_db_error($profile, 'UPDATE', __FILE__); $this->serverError(_('Couldn\'t save profile.')); return; } common_broadcast_profile($profile); $type = $apidata['content-type']; $this->init_document($type); $this->show_profile($profile, $type); $this->end_document($type); }
static function saveNew($profile_id, $album_id, $thumb_uri, $uri, $source, $insert_now, $title = null, $photo_description = null) { $photo = new GNUsocialPhoto(); $photo->thumb_uri = $thumb_uri; $photo->uri = $uri; $photo->album_id = $album_id; if (!empty($title)) { $photo->title = $title; } if (!empty($photo_description)) { $photo->photo_description = (string) $photo_description; } if ($insert_now) { $notice = Notice::saveNew($profile_id, $uri, $source); $photo->notice_id = $notice->id; $photo_id = $photo->insert(); if (!$photo_id) { common_log_db_error($photo, 'INSERT', __FILE__); throw new ServerException(_m('Problem Saving Photo.')); } } else { GNUsocialPhotoTemp::$tmp = $photo; Notice::saveNew($profile_id, $uri, $source); } }
static function setTags($tagger, $tagged, $newtags) { $newtags = array_unique($newtags); $oldtags = Profile_tag::getTags($tagger, $tagged); # Delete stuff that's old that not in new $to_delete = array_diff($oldtags, $newtags); # Insert stuff that's in new and not in old $to_insert = array_diff($newtags, $oldtags); $profile_tag = new Profile_tag(); $profile_tag->tagger = $tagger; $profile_tag->tagged = $tagged; $profile_tag->query('BEGIN'); foreach ($to_delete as $deltag) { $profile_tag->tag = $deltag; $result = $profile_tag->delete(); if (!$result) { common_log_db_error($profile_tag, 'DELETE', __FILE__); return false; } } foreach ($to_insert as $instag) { $profile_tag->tag = $instag; $result = $profile_tag->insert(); if (!$result) { common_log_db_error($profile_tag, 'INSERT', __FILE__); return false; } } $profile_tag->query('COMMIT'); return true; }
/** * Handler method * * @param array $args is ignored since it's now passed in in prepare() */ function handle($args) { parent::handle($args); $data = $this->facebook->getSignedRequest(); if (isset($data['user_id'])) { $fbuid = $data['user_id']; $flink = Foreign_link::getByForeignID($fbuid, FACEBOOK_SERVICE); $user = $flink->getUser(); // Remove the link to Facebook $result = $flink->delete(); if (!$result) { common_log_db_error($flink, 'DELETE', __FILE__); common_log(LOG_WARNING, sprintf('Unable to delete Facebook foreign link ' . 'for %s (%d), fbuid %d', $user->nickname, $user->id, $fbuid), __FILE__); return; } common_log(LOG_INFO, sprintf('Facebook callback: %s (%d), fbuid %d has deauthorized ' . 'the Facebook application.', $user->nickname, $user->id, $fbuid), __FILE__); // Warn the user about being locked out of their account // if we can. if (empty($user->password) && !empty($user->email)) { Facebookclient::emailWarn($user); } else { common_log(LOG_WARNING, sprintf('%s (%d), fbuid %d has deauthorized his/her Facebook ' . 'connection but hasn\'t set a password so s/he ' . 'is locked out.', $user->nickname, $user->id, $fbuid), __FILE__); } } else { if (!empty($data)) { common_log(LOG_WARNING, sprintf('Facebook called the deauthorize callback ' . ' but didn\'t provide a user ID.'), __FILE__); } else { // It probably wasn't Facebook that hit this action, // so redirect to the public timeline common_redirect(common_local_url('public'), 303); } } }
function handle($args) { parent::handle($args); $secret = common_config('facebook', 'secret'); $sig = ''; ksort($_POST); foreach ($_POST as $key => $val) { if (substr($key, 0, 7) == 'fb_sig_') { $sig .= substr($key, 7) . '=' . $val; } } $sig .= $secret; $verify = md5($sig); if ($verify == $this->arg('fb_sig')) { $flink = Foreign_link::getByForeignID($this->arg('fb_sig_user'), 2); common_debug("Removing foreign link to Facebook - local user ID: {$flink->user_id}, Facebook ID: {$flink->foreign_id}"); $result = $flink->delete(); if (!$result) { common_log_db_error($flink, 'DELETE', __FILE__); $this->serverError(_('Couldn\'t remove Facebook user.')); return; } } else { # Someone bad tried to remove facebook link? common_log(LOG_ERR, "Someone from {$_SERVER['REMOTE_ADDR']} " . 'unsuccessfully tried to remove a foreign link to Facebook!'); } }
public function insert() { if (parent::insert() === false) { common_log_db_error($this, 'INSERT', __FILE__); throw new ServerException(sprintf(_m('Could not store new object of type %s'), get_called_class())); } self::blowCacheForProfileId($this->user_id); self::blowCacheForNoticeId($this->notice_id); return $this; }
function run() { if (!$this->start()) { return false; } $this->log(LOG_INFO, 'checking for queued confirmations'); do { $confirm = $this->next_confirm(); if ($confirm) { $this->log(LOG_INFO, 'Sending confirmation for ' . $confirm->address); $user = User::staticGet($confirm->user_id); if (!$user) { $this->log(LOG_WARNING, 'Confirmation for unknown user ' . $confirm->user_id); continue; } $success = jabber_confirm_address($confirm->code, $user->nickname, $confirm->address); if (!$success) { $this->log(LOG_ERR, 'Confirmation failed for ' . $confirm->address); # Just let the claim age out; hopefully things work then continue; } else { $this->log(LOG_INFO, 'Confirmation sent for ' . $confirm->address); # Mark confirmation sent; need a dupe so we don't have the WHERE clause $dupe = Confirm_address::staticGet('code', $confirm->code); if (!$dupe) { common_log(LOG_WARNING, 'Could not refetch confirm', __FILE__); continue; } $orig = clone $dupe; $dupe->sent = $dupe->claimed; $result = $dupe->update($orig); if (!$result) { common_log_db_error($dupe, 'UPDATE', __FILE__); # Just let the claim age out; hopefully things work then continue; } $dupe->free(); unset($dupe); } $user->free(); unset($user); $confirm->free(); unset($confirm); $this->idle(0); } else { # $this->clear_old_confirm_claims(); $this->idle(10); } } while (true); if (!$this->finish()) { return false; } return true; }
static function addNew($user, $notice) { $fave = new Fave(); $fave->user_id = $user->id; $fave->notice_id = $notice->id; if (!$fave->insert()) { common_log_db_error($fave, 'INSERT', __FILE__); return false; } return $fave; }
/** * Create a new inbox from existing Notice_inbox stuff */ static function initialize($user_id) { $inbox = Inbox::fromNoticeInbox($user_id); unset($inbox->fake); $result = $inbox->insert(); if (!$result) { common_log_db_error($inbox, 'INSERT', __FILE__); return null; } return $inbox; }
function getImage() { $user = $this->user; $profile = $user->getProfile(); if (!$profile) { common_log_db_error($user, 'SELECT', __FILE__); $this->serverError(_('User without matching profile.')); return null; } $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE); return $avatar ? $avatar->url : null; }
/** * Class handler. * * @param array $args query arguments * * @return void */ function handle($args) { parent::handle($args); if (!common_logged_in()) { // TRANS: Client error displayed when trying to remove a favorite while not logged in. $this->clientError(_('Not logged in.')); return; } $user = common_current_user(); if ($_SERVER['REQUEST_METHOD'] != 'POST') { common_redirect(common_local_url('showfavorites', array('nickname' => $user->nickname))); return; } $id = $this->trimmed('notice'); $notice = Notice::staticGet($id); $token = $this->trimmed('token-' . $notice->id); if (!$token || $token != common_session_token()) { // TRANS: Client error displayed when the session token does not match or is not given. $this->clientError(_('There was a problem with your session token. Try again, please.')); return; } $fave = new Fave(); $fave->user_id = $user->id; $fave->notice_id = $notice->id; if (!$fave->find(true)) { // TRANS: Client error displayed when trying to remove favorite status for a notice that is not a favorite. $this->clientError(_('This notice is not a favorite!')); return; } $result = $fave->delete(); if (!$result) { common_log_db_error($fave, 'DELETE', __FILE__); // TRANS: Server error displayed when removing a favorite from the database fails. $this->serverError(_('Could not delete favorite.')); return; } $user->blowFavesCache(); if ($this->boolean('ajax')) { $this->startHTML('text/xml;charset=utf-8'); $this->elementStart('head'); // TRANS: Title for page on which favorites can be added. $this->element('title', null, _('Add to favorites')); $this->elementEnd('head'); $this->elementStart('body'); $favor = new FavorForm($this, $notice); $favor->show(); $this->elementEnd('body'); $this->elementEnd('html'); } else { common_redirect(common_local_url('showfavorites', array('nickname' => $user->nickname)), 303); } }
static function vincularGrupo($userid, $groupid) { // MAGICALLY put fields into current scope $grGroup = new Gradesgroup(); $grGroup->userid = $userid; $grGroup->groupid = $groupid; $result = $grGroup->insert(); if (!$result) { common_log_db_error($userid, 'INSERT', __FILE__); return false; } return $grGroup; }
function getImage() { $user = $this->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.')); return null; } $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE); return $avatar ? $avatar->url : null; }
static function newResponse($extension_id, $profile_id, $value) { $response = new GNUsocialProfileExtensionResponse(); $response->extension_id = $extension_id; $response->profile_id = $profile_id; $response->value = $value; $response->id = $response->insert(); if (!$response->id) { common_log_db_error($response, 'INSERT', __FILE__); throw new ServerException(_m('Error creating new response.')); } return $response; }
function onStartNoticeDistribute($notice) { common_log(LOG_INFO, "event: StartNoticeDistribute"); if (GNUsocialPhotoTemp::$tmp) { GNUsocialPhotoTemp::$tmp->notice_id = $notice->id; $photo_id = GNUsocialPhotoTemp::$tmp->insert(); if (!$photo_id) { common_log_db_error($photo, 'INSERT', __FILE__); throw new ServerException(_m('Problem saving photo.')); } } return true; }
static function leave($group_id, $profile_id) { $member = Group_member::pkeyGet(array('group_id' => $group_id, 'profile_id' => $profile_id)); if (empty($member)) { throw new Exception(_("Not part of group.")); } $result = $member->delete(); if (!$result) { common_log_db_error($member, 'INSERT', __FILE__); throw new Exception(_("Group leave failed.")); } 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; }
/** * 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; } }
function enqueue($object, $queue) { $notice = $object; $qi = new Queue_item(); $qi->notice_id = $notice->id; $qi->transport = $queue; $qi->created = $notice->created; $result = $qi->insert(); if (!$result) { common_log_db_error($qi, 'INSERT', __FILE__); throw new ServerException('DB error inserting queue item'); } 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; }
function setNickname($nickname) { $this->decache(); $qry = 'UPDATE local_group set nickname = "' . $nickname . '" where group_id = ' . $this->group_id; $result = $this->query($qry); if ($result) { $this->nickname = $nickname; $this->fixupTimestamps(); $this->encache(); } else { common_log_db_error($local, 'UPDATE', __FILE__); throw new ServerException(_('Could not update local group.')); } return $result; }
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; }
/** * Save a favorite record. * @fixme post-author notification should be moved here * * @param Profile $profile the local or remote user who likes * @param Notice $notice the notice that is liked * @return mixed false on failure, or Fave record on success */ static function addNew(Profile $profile, Notice $notice) { $fave = null; if (Event::handle('StartFavorNotice', array($profile, $notice, &$fave))) { $fave = new Fave(); $fave->user_id = $profile->id; $fave->notice_id = $notice->id; if (!$fave->insert()) { common_log_db_error($fave, 'INSERT', __FILE__); return false; } Event::handle('EndFavorNotice', array($profile, $notice)); } return $fave; }
static function leave($group_id, $profile_id) { $member = Group_member::pkeyGet(array('group_id' => $group_id, 'profile_id' => $profile_id)); if (empty($member)) { // TRANS: Exception thrown when trying to leave a group the user is not a member of. throw new Exception(_("Not part of group.")); } $result = $member->delete(); if (!$result) { common_log_db_error($member, 'INSERT', __FILE__); // TRANS: Exception thrown when trying to leave a group fails. throw new Exception(_("Group leave failed.")); } return true; }
/** * 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; }
/** * Class handler. * * @param array $args query arguments * * @return void */ function handle($args) { parent::handle($args); $profile = AnonymousFavePlugin::getAnonProfile(); if (empty($profile) || $_SERVER['REQUEST_METHOD'] != 'POST') { $this->clientError(_m('Could not disfavor notice! Please make sure your browser has cookies enabled.')); return; } $id = $this->trimmed('notice'); $notice = Notice::staticGet($id); $token = $this->trimmed('token-' . $notice->id); if (!$token || $token != common_session_token()) { // TRANS: Client error. $this->clientError(_m('There was a problem with your session token. Try again, please.')); return; } $fave = new Fave(); $fave->user_id = $profile->id; $fave->notice_id = $notice->id; if (!$fave->find(true)) { // TRANS: Client error. $this->clientError(_m('This notice is not a favorite!')); return; } $result = $fave->delete(); if (!$result) { common_log_db_error($fave, 'DELETE', __FILE__); // TRANS: Server error. $this->serverError(_m('Could not delete favorite.')); return; } $profile->blowFavesCache(); if ($this->boolean('ajax')) { $this->startHTML('text/xml;charset=utf-8'); $this->elementStart('head'); // TRANS: Title. $this->element('title', null, _m('Add to favorites')); $this->elementEnd('head'); $this->elementStart('body'); $favor = new AnonFavorForm($this, $notice); $favor->show(); $this->elementEnd('body'); $this->elementEnd('html'); } else { $this->returnToPrevious(); } }
/** * Class handler. * * @param array $args query arguments * * @return void */ function handle($args) { parent::handle($args); if (!common_logged_in()) { $this->clientError(_('Not logged in.')); return; } $user = common_current_user(); if ($_SERVER['REQUEST_METHOD'] != 'POST') { common_redirect(common_local_url('showfavorites', array('nickname' => $user->nickname))); return; } $id = $this->trimmed('notice'); $notice = Notice::staticGet($id); $token = $this->trimmed('token-' . $notice->id); if (!$token || $token != common_session_token()) { $this->clientError(_("There was a problem with your session token. Try again, please.")); return; } $fave = new Fave(); $fave->user_id = $this->id; $fave->notice_id = $notice->id; if (!$fave->find(true)) { $this->clientError(_('This notice is not a favorite!')); return; } $result = $fave->delete(); if (!$result) { common_log_db_error($fave, 'DELETE', __FILE__); $this->serverError(_('Could not delete favorite.')); return; } $user->blowFavesCache(); if ($this->boolean('ajax')) { $this->startHTML('text/xml;charset=utf-8'); $this->elementStart('head'); $this->element('title', null, _('Add to favorites')); $this->elementEnd('head'); $this->elementStart('body'); $favor = new FavorForm($this, $notice); $favor->show(); $this->elementEnd('body'); $this->elementEnd('html'); } else { common_redirect(common_local_url('showfavorites', array('nickname' => $user->nickname))); } }
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; }
function checkCode() { $code = $this->trimmed('code'); $confirm = Confirm_address::getKV('code', $code); if (!$confirm) { // TRANS: Client error displayed when password recovery code is not correct. $this->clientError(_('No such recovery code.')); } if ($confirm->address_type != 'recover') { // TRANS: Client error displayed when no proper password recovery code was submitted. $this->clientError(_('Not a recovery code.')); } $user = User::getKV($confirm->user_id); if (!$user) { // TRANS: Server error displayed trying to recover password without providing a user. $this->serverError(_('Recovery code for unknown user.')); } $touched = strtotime($confirm->modified); $email = $confirm->address; // Burn this code $result = $confirm->delete(); if (!$result) { common_log_db_error($confirm, 'DELETE', __FILE__); // TRANS: Server error displayed removing a password recovery code from the database. $this->serverError(_('Error with confirmation code.')); } // These should be reaped, but for now we just check mod time // Note: it's still deleted; let's avoid a second attempt! if (time() - $touched > MAX_RECOVERY_TIME) { common_log(LOG_WARNING, 'Attempted redemption on recovery code ' . 'that is ' . $touched . ' seconds old. '); // TRANS: Client error displayed trying to recover password with too old a recovery code. $this->clientError(_('This confirmation code is too old. ' . 'Please start again.')); } // If we used an outstanding confirmation to send the email, // it's been confirmed at this point. if (!$user->email) { $orig = clone $user; $user->email = $email; // Throws exception on failure. $user->updateWithKeys($orig); } // Success! $this->setTempUser($user); $this->showPasswordForm(); }