/**
  * Try to log in using OpenID
  *
  * Check the OpenID for validity; potentially store it.
  *
  * @return void
  */
 function tryLogin()
 {
     $consumer = oid_consumer();
     $response = $consumer->complete(common_local_url('finishaddopenid'));
     if ($response->status == Auth_OpenID_CANCEL) {
         $this->message(_m('OpenID authentication cancelled.'));
         return;
     } else {
         if ($response->status == Auth_OpenID_FAILURE) {
             // Authentication failed; display the error message.
             $this->message(sprintf(_m('OpenID authentication failed: %s'), $response->message));
         } else {
             if ($response->status == Auth_OpenID_SUCCESS) {
                 $display = $response->getDisplayIdentifier();
                 $canonical = $response->endpoint && $response->endpoint->canonicalID ? $response->endpoint->canonicalID : $display;
                 $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($response);
                 if ($sreg_resp) {
                     $sreg = $sreg_resp->contents();
                 }
                 $cur = common_current_user();
                 $other = oid_get_user($canonical);
                 if ($other) {
                     if ($other->id == $cur->id) {
                         $this->message(_m('You already have this OpenID!'));
                     } else {
                         $this->message(_m('Someone else already has this OpenID.'));
                     }
                     return;
                 }
                 // start a transaction
                 $cur->query('BEGIN');
                 $result = oid_link_user($cur->id, $canonical, $display);
                 if (!$result) {
                     $this->message(_m('Error connecting user.'));
                     return;
                 }
                 if ($sreg) {
                     if (!oid_update_user($cur, $sreg)) {
                         $this->message(_m('Error updating profile'));
                         return;
                     }
                 }
                 // success!
                 $cur->query('COMMIT');
                 oid_set_last($display);
                 common_redirect(common_local_url('openidsettings'), 303);
             }
         }
     }
 }
 function createNewUser()
 {
     # FIXME: save invite code before redirect, and check here
     if (common_config('site', 'closed')) {
         // TRANS: OpenID plugin message. No new user registration is allowed on the site.
         $this->clientError(_m('Registration not allowed.'));
         return;
     }
     $invite = null;
     if (common_config('site', 'inviteonly')) {
         $code = $_SESSION['invitecode'];
         if (empty($code)) {
             // TRANS: OpenID plugin message. No new user registration is allowed on the site without an invitation code, and none was provided.
             $this->clientError(_m('Registration not allowed.'));
             return;
         }
         $invite = Invitation::staticGet($code);
         if (empty($invite)) {
             // TRANS: OpenID plugin message. No new user registration is allowed on the site without an invitation code, and the one provided was not valid.
             $this->clientError(_m('Not a valid invitation code.'));
             return;
         }
     }
     $nickname = $this->trimmed('newname');
     if (!Validate::string($nickname, array('min_length' => 1, 'max_length' => 64, 'format' => NICKNAME_FMT))) {
         // TRANS: OpenID plugin message. The entered new user name did not conform to the requirements.
         $this->showForm(_m('Nickname must have only lowercase letters and numbers and no spaces.'));
         return;
     }
     if (!User::allowed_nickname($nickname)) {
         // TRANS: OpenID plugin message. The entered new user name is blacklisted.
         $this->showForm(_m('Nickname not allowed.'));
         return;
     }
     if (User::staticGet('nickname', $nickname)) {
         // TRANS: OpenID plugin message. The entered new user name is already used.
         $this->showForm(_m('Nickname already in use. Try another one.'));
         return;
     }
     list($display, $canonical, $sreg) = $this->getSavedValues();
     if (!$display || !$canonical) {
         // TRANS: OpenID plugin server error. A stored OpenID cannot be retrieved.
         $this->serverError(_m('Stored OpenID not found.'));
         return;
     }
     # Possible race condition... let's be paranoid
     $other = oid_get_user($canonical);
     if ($other) {
         // TRANS: OpenID plugin server error.
         $this->serverError(_m('Creating new account for OpenID that already has a user.'));
         return;
     }
     Event::handle('StartOpenIDCreateNewUser', array($canonical, &$sreg));
     $location = '';
     if (!empty($sreg['country'])) {
         if ($sreg['postcode']) {
             # XXX: use postcode to get city and region
             # XXX: also, store postcode somewhere -- it's valuable!
             $location = $sreg['postcode'] . ', ' . $sreg['country'];
         } else {
             $location = $sreg['country'];
         }
     }
     if (!empty($sreg['fullname']) && mb_strlen($sreg['fullname']) <= 255) {
         $fullname = $sreg['fullname'];
     } else {
         $fullname = '';
     }
     if (!empty($sreg['email']) && Validate::email($sreg['email'], common_config('email', 'check_domain'))) {
         $email = $sreg['email'];
     } else {
         $email = '';
     }
     # XXX: add language
     # XXX: add timezone
     $args = array('nickname' => $nickname, 'email' => $email, 'fullname' => $fullname, 'location' => $location);
     if (!empty($invite)) {
         $args['code'] = $invite->code;
     }
     $user = User::register($args);
     $result = oid_link_user($user->id, $canonical, $display);
     Event::handle('EndOpenIDCreateNewUser', array($user, $canonical, $sreg));
     oid_set_last($display);
     common_set_user($user);
     common_real_login(true);
     if (isset($_SESSION['openid_rememberme']) && $_SESSION['openid_rememberme']) {
         common_rememberme($user);
     }
     unset($_SESSION['openid_rememberme']);
     common_redirect(common_local_url('showstream', array('nickname' => $user->nickname)), 303);
 }
 function createNewUser()
 {
     # FIXME: save invite code before redirect, and check here
     if (common_config('site', 'closed') || common_config('site', 'inviteonly')) {
         $this->clientError(_('Registration not allowed.'));
         return;
     }
     $nickname = $this->trimmed('newname');
     if (!Validate::string($nickname, array('min_length' => 1, 'max_length' => 64, 'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
         $this->showForm(_('Nickname must have only lowercase letters and numbers and no spaces.'));
         return;
     }
     if (!User::allowed_nickname($nickname)) {
         $this->showForm(_('Nickname not allowed.'));
         return;
     }
     if (User::staticGet('nickname', $nickname)) {
         $this->showForm(_('Nickname already in use. Try another one.'));
         return;
     }
     list($display, $canonical, $sreg) = $this->getSavedValues();
     if (!$display || !$canonical) {
         $this->serverError(_('Stored OpenID not found.'));
         return;
     }
     # Possible race condition... let's be paranoid
     $other = oid_get_user($canonical);
     if ($other) {
         $this->serverError(_('Creating new account for OpenID that already has a user.'));
         return;
     }
     $location = '';
     if (!empty($sreg['country'])) {
         if ($sreg['postcode']) {
             # XXX: use postcode to get city and region
             # XXX: also, store postcode somewhere -- it's valuable!
             $location = $sreg['postcode'] . ', ' . $sreg['country'];
         } else {
             $location = $sreg['country'];
         }
     }
     if (!empty($sreg['fullname']) && mb_strlen($sreg['fullname']) <= 255) {
         $fullname = $sreg['fullname'];
     } else {
         $fullname = '';
     }
     if (!empty($sreg['email']) && Validate::email($sreg['email'], true)) {
         $email = $sreg['email'];
     } else {
         $email = '';
     }
     # XXX: add language
     # XXX: add timezone
     $user = User::register(array('nickname' => $nickname, 'email' => $email, 'fullname' => $fullname, 'location' => $location));
     $result = oid_link_user($user->id, $canonical, $display);
     oid_set_last($display);
     common_set_user($user);
     common_real_login(true);
     if (isset($_SESSION['openid_rememberme']) && $_SESSION['openid_rememberme']) {
         common_rememberme($user);
     }
     unset($_SESSION['openid_rememberme']);
     common_redirect(common_local_url('showstream', array('nickname' => $user->nickname)));
 }
 /**
  * Try to log in using OpenID
  *
  * Check the OpenID for validity; potentially store it.
  *
  * @return void
  */
 function tryLogin()
 {
     $consumer = oid_consumer();
     $response = $consumer->complete(common_local_url('finishaddopenid'));
     if ($response->status == Auth_OpenID_CANCEL) {
         // TRANS: Status message in case the response from the OpenID provider is that the logon attempt was cancelled.
         $this->message(_m('OpenID authentication cancelled.'));
         return;
     } else {
         if ($response->status == Auth_OpenID_FAILURE) {
             // TRANS: OpenID authentication failed; display the error message.
             // TRANS: %s is the error message.
             $this->message(sprintf(_m('OpenID authentication failed: %s.'), $response->message));
         } else {
             if ($response->status == Auth_OpenID_SUCCESS) {
                 $display = $response->getDisplayIdentifier();
                 $canonical = $response->endpoint && $response->endpoint->canonicalID ? $response->endpoint->canonicalID : $display;
                 $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($response);
                 if ($sreg_resp) {
                     $sreg = $sreg_resp->contents();
                 }
                 // Launchpad teams extension
                 if (!oid_check_teams($response)) {
                     // TRANS: OpenID authentication error.
                     $this->message(_m('OpenID authentication aborted: You are not allowed to login to this site.'));
                     return;
                 }
                 $cur = common_current_user();
                 $other = oid_get_user($canonical);
                 if ($other) {
                     if ($other->id == $cur->id) {
                         // TRANS: Message in case a user tries to add an OpenID that is already connected to them.
                         $this->message(_m('You already have this OpenID!'));
                     } else {
                         // TRANS: Message in case a user tries to add an OpenID that is already used by another user.
                         $this->message(_m('Someone else already has this OpenID.'));
                     }
                     return;
                 }
                 // start a transaction
                 $cur->query('BEGIN');
                 $result = oid_link_user($cur->id, $canonical, $display);
                 if (!$result) {
                     // TRANS: Message in case the OpenID object cannot be connected to the user.
                     $this->message(_m('Error connecting user.'));
                     return;
                 }
                 if (Event::handle('StartOpenIDUpdateUser', array($cur, $canonical, &$sreg))) {
                     if ($sreg) {
                         if (!oid_update_user($cur, $sreg)) {
                             // TRANS: Message in case the user or the user profile cannot be saved in StatusNet.
                             $this->message(_m('Error updating profile.'));
                             return;
                         }
                     }
                 }
                 Event::handle('EndOpenIDUpdateUser', array($cur, $canonical, $sreg));
                 // success!
                 $cur->query('COMMIT');
                 oid_set_last($display);
                 common_redirect(common_local_url('openidsettings'), 303);
             }
         }
     }
 }
 function createNewUser()
 {
     // FIXME: save invite code before redirect, and check here
     if (!Event::handle('StartRegistrationTry', array($this))) {
         return;
     }
     if (common_config('site', 'closed')) {
         // TRANS: OpenID plugin message. No new user registration is allowed on the site.
         $this->clientError(_m('Registration not allowed.'));
     }
     $invite = null;
     if (common_config('site', 'inviteonly')) {
         $code = $_SESSION['invitecode'];
         if (empty($code)) {
             // TRANS: OpenID plugin message. No new user registration is allowed on the site without an invitation code, and none was provided.
             $this->clientError(_m('Registration not allowed.'));
         }
         $invite = Invitation::getKV($code);
         if (empty($invite)) {
             // TRANS: OpenID plugin message. No new user registration is allowed on the site without an invitation code, and the one provided was not valid.
             $this->clientError(_m('Not a valid invitation code.'));
         }
     }
     try {
         $nickname = Nickname::normalize($this->trimmed('newname'), true);
     } catch (NicknameException $e) {
         $this->showForm($e->getMessage());
         return;
     }
     list($display, $canonical, $sreg) = $this->getSavedValues();
     if (!$display || !$canonical) {
         // TRANS: OpenID plugin server error. A stored OpenID cannot be retrieved.
         $this->serverError(_m('Stored OpenID not found.'));
     }
     // Possible race condition... let's be paranoid
     $other = oid_get_user($canonical);
     if ($other) {
         // TRANS: OpenID plugin server error.
         $this->serverError(_m('Creating new account for OpenID that already has a user.'));
     }
     Event::handle('StartOpenIDCreateNewUser', array($canonical, &$sreg));
     $location = '';
     if (!empty($sreg['country'])) {
         if ($sreg['postcode']) {
             // XXX: use postcode to get city and region
             // XXX: also, store postcode somewhere -- it's valuable!
             $location = $sreg['postcode'] . ', ' . $sreg['country'];
         } else {
             $location = $sreg['country'];
         }
     }
     if (!empty($sreg['fullname']) && mb_strlen($sreg['fullname']) <= 255) {
         $fullname = $sreg['fullname'];
     } else {
         $fullname = '';
     }
     $email = $this->getEmail();
     // XXX: add language
     // XXX: add timezone
     $args = array('nickname' => $nickname, 'email' => $email, 'fullname' => $fullname, 'location' => $location);
     if (!empty($invite)) {
         $args['code'] = $invite->code;
     }
     $user = User::register($args);
     $result = oid_link_user($user->id, $canonical, $display);
     Event::handle('EndOpenIDCreateNewUser', array($user, $canonical, $sreg));
     oid_set_last($display);
     common_set_user($user);
     common_real_login(true);
     if (isset($_SESSION['openid_rememberme']) && $_SESSION['openid_rememberme']) {
         common_rememberme($user);
     }
     unset($_SESSION['openid_rememberme']);
     Event::handle('EndRegistrationTry', array($this));
     common_redirect(common_local_url('showstream', array('nickname' => $user->nickname)), 303);
 }