2009-08-01 17:20:44 +09:00
< ? php
2020-08-14 05:59:40 +09:00
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
2009-08-01 17:20:44 +09:00
/**
* Class for doing OAuth authentication against Twitter
*
2010-01-26 16:50:01 +09:00
* @ category Plugin
2020-08-14 05:59:40 +09:00
* @ package GNUsocial
2009-12-09 06:16:03 +09:00
* @ author Zach Copley < zach @ status . net >
2010-01-26 16:50:01 +09:00
* @ author Julien C < chaumond @ gmail . com >
* @ copyright 2009 - 2010 StatusNet , Inc .
2020-08-14 05:59:40 +09:00
* @ license https :// www . gnu . org / licenses / agpl . html GNU AGPL v3 or later
2009-08-01 17:20:44 +09:00
*/
2020-08-14 05:59:40 +09:00
defined ( 'GNUSOCIAL' ) || die ();
2009-08-01 17:20:44 +09:00
2014-08-06 16:29:47 +09:00
require_once dirname ( __DIR__ ) . '/twitter.php' ;
2019-08-23 21:36:02 +09:00
require_once INSTALLDIR . '/lib/util/oauthclient.php' ;
2009-08-26 09:59:06 +09:00
2009-08-09 03:13:19 +09:00
/**
* Class for doing OAuth authentication against Twitter
*
2009-09-09 08:02:57 +09:00
* Peforms the OAuth " dance " between StatusNet and Twitter -- requests a token ,
2009-08-09 03:13:19 +09:00
* authorizes it , and exchanges it for an access token . It also creates a link
2009-09-09 08:02:57 +09:00
* ( Foreign_link ) between the StatusNet user and Twitter user and stores the
2009-08-09 03:13:19 +09:00
* access token and secret in the link .
*
2020-08-14 05:59:40 +09:00
* @ category Plugin
* @ package GNUsocial
* @ author Zach Copley < zach @ status . net >
* @ author Julien C < chaumond @ gmail . com >
* @ license https :// www . gnu . org / licenses / agpl . html GNU AGPL v3 or later
2009-08-09 03:13:19 +09:00
*/
2015-07-18 01:44:09 +09:00
class TwitterauthorizationAction extends FormAction
2009-08-01 17:20:44 +09:00
{
2020-08-14 05:59:40 +09:00
public $twuid = null ;
public $tw_fields = null ;
public $access_token = null ;
public $verifier = null ;
2010-01-26 10:25:33 +09:00
2015-07-18 01:44:09 +09:00
protected $needLogin = false ; // authorization page can also be used to create a new user
2009-08-01 17:20:44 +09:00
2015-07-18 01:44:09 +09:00
protected function doPreparation ()
{
2009-08-01 17:20:44 +09:00
$this -> oauth_token = $this -> arg ( 'oauth_token' );
2010-02-16 15:12:08 +09:00
$this -> verifier = $this -> arg ( 'oauth_verifier' );
2009-08-01 17:20:44 +09:00
2015-07-18 01:44:09 +09:00
if ( $this -> scoped instanceof Profile ) {
2015-07-18 08:18:08 +09:00
try {
$flink = Foreign_link :: getByUserID ( $this -> scoped -> getID (), TWITTER_SERVICE );
$fuser = $flink -> getForeignUser ();
// If there's already a foreign link record and a foreign user
// (no exceptions were thrown when fetching either of them...)
// it means the accounts are already linked, and this is unecessary.
// So go back.
common_redirect ( common_local_url ( 'twittersettings' ));
} catch ( NoResultException $e ) {
2020-08-14 05:59:40 +09:00
// but if we don't have a foreign user linked, let's continue authorization procedure.
2009-12-09 06:16:03 +09:00
}
2009-08-01 17:20:44 +09:00
}
2015-07-18 01:44:09 +09:00
}
2010-01-26 10:25:33 +09:00
2015-07-18 01:44:09 +09:00
protected function doPost ()
{
// User was not logged in to StatusNet before
2010-01-26 10:25:33 +09:00
2015-07-18 01:44:09 +09:00
$this -> twuid = $this -> trimmed ( 'twuid' );
2009-12-09 06:16:03 +09:00
2015-07-18 01:44:09 +09:00
$this -> tw_fields = array ( 'screen_name' => $this -> trimmed ( 'tw_fields_screen_name' ),
'fullname' => $this -> trimmed ( 'tw_fields_fullname' ));
2010-01-26 10:25:33 +09:00
2015-07-18 01:44:09 +09:00
$this -> access_token = new OAuthToken ( $this -> trimmed ( 'access_token_key' ), $this -> trimmed ( 'access_token_secret' ));
2010-01-26 10:25:33 +09:00
2015-07-18 01:44:09 +09:00
if ( $this -> arg ( 'create' )) {
2015-07-18 08:18:08 +09:00
common_debug ( 'TwitterBridgeDebug - POST with create' );
2015-07-18 01:44:09 +09:00
if ( ! $this -> boolean ( 'license' )) {
// TRANS: Form validation error displayed when the checkbox to agree to the license has not been checked.
throw new ClientException ( _m ( 'You cannot register if you do not agree to the license.' ));
2009-12-09 06:16:03 +09:00
}
2020-08-14 05:59:40 +09:00
$this -> createNewUser ();
2015-07-18 08:18:08 +09:00
} elseif ( $this -> arg ( 'connect' )) {
common_debug ( 'TwitterBridgeDebug - POST with connect' );
2020-08-14 05:59:40 +09:00
$this -> connectNewUser ();
} else {
common_debug ( 'TwitterBridgeDebug - ' . print_r ( $this -> args , true ));
// TRANS: Form validation error displayed when an unhandled error occurs.
throw new ClientException ( _m ( 'No known action for POST.' ));
2009-08-09 03:13:19 +09:00
}
}
2009-08-01 17:20:44 +09:00
2009-08-09 03:13:19 +09:00
/**
* Asks Twitter for a request token , and then redirects to Twitter
* to authorize it .
*/
2015-07-18 01:44:09 +09:00
protected function authorizeRequestToken ()
2009-08-09 03:13:19 +09:00
{
try {
// Get a new request token and authorize it
$client = new TwitterOAuthClient ();
2015-07-18 01:44:09 +09:00
$req_tok = $client -> getTwitterRequestToken ();
2009-08-01 17:20:44 +09:00
2009-08-09 03:13:19 +09:00
// Sock the request token away in the session temporarily
$_SESSION [ 'twitter_request_token' ] = $req_tok -> key ;
2009-08-10 15:05:43 +09:00
$_SESSION [ 'twitter_request_token_secret' ] = $req_tok -> secret ;
2009-08-04 07:46:01 +09:00
2015-07-18 08:18:08 +09:00
$auth_link = $client -> getTwitterAuthorizeLink ( $req_tok , $this -> boolean ( 'signin' ));
2009-10-29 04:29:20 +09:00
} catch ( OAuthClientException $e ) {
2010-02-18 04:24:38 +09:00
$msg = sprintf (
'OAuth client error - code: %1s, msg: %2s' ,
$e -> getCode (),
$e -> getMessage ()
);
common_log ( LOG_INFO , 'Twitter bridge - ' . $msg );
2015-07-18 01:44:09 +09:00
// TRANS: Server error displayed when linking to a Twitter account fails.
throw new ServerException ( _m ( 'Could not link your Twitter account.' ));
2009-08-09 03:13:19 +09:00
}
2009-08-01 17:20:44 +09:00
2009-08-09 03:13:19 +09:00
common_redirect ( $auth_link );
}
2009-08-01 17:20:44 +09:00
2009-08-09 03:13:19 +09:00
/**
* Called when Twitter returns an authorized request token . Exchanges
* it for an access token and stores it .
*
2020-08-14 05:59:40 +09:00
* @ return void
2009-08-09 03:13:19 +09:00
*/
2020-08-14 05:59:40 +09:00
private function saveAccessToken () : void
2009-08-09 03:13:19 +09:00
{
// Check to make sure Twitter returned the same request
// token we sent them
2009-08-01 17:20:44 +09:00
2009-08-09 03:13:19 +09:00
if ( $_SESSION [ 'twitter_request_token' ] != $this -> oauth_token ) {
2015-07-18 01:44:09 +09:00
// TRANS: Server error displayed when linking to a Twitter account fails because of an incorrect oauth_token.
throw new ServerException ( _m ( 'Could not link your Twitter account: oauth_token mismatch.' ));
2009-08-09 03:13:19 +09:00
}
2009-08-01 17:20:44 +09:00
2010-01-26 10:25:33 +09:00
$twitter_user = null ;
2009-08-09 03:13:19 +09:00
try {
2015-07-18 04:03:37 +09:00
$client = new TwitterOAuthClient ( $_SESSION [ 'twitter_request_token' ], $_SESSION [ 'twitter_request_token_secret' ]);
2009-08-01 17:20:44 +09:00
2009-08-09 03:13:19 +09:00
// Exchange the request token for an access token
2015-07-18 01:44:09 +09:00
$atok = $client -> getTwitterAccessToken ( $this -> verifier );
2009-08-01 17:20:44 +09:00
2009-08-09 03:13:19 +09:00
// Test the access token and get the user's Twitter info
$client = new TwitterOAuthClient ( $atok -> key , $atok -> secret );
2009-08-10 15:05:43 +09:00
$twitter_user = $client -> verifyCredentials ();
2009-08-09 03:13:19 +09:00
} catch ( OAuthClientException $e ) {
2010-02-18 04:24:38 +09:00
$msg = sprintf (
'OAuth client error - code: %1$s, msg: %2$s' ,
$e -> getCode (),
$e -> getMessage ()
);
common_log ( LOG_INFO , 'Twitter bridge - ' . $msg );
2015-07-18 04:03:37 +09:00
// TRANS: Server error displayed when linking to a Twitter account fails.
throw new ServerException ( _m ( 'Could not link your Twitter account.' ));
2009-08-09 03:13:19 +09:00
}
2009-08-01 17:20:44 +09:00
2015-07-18 04:03:37 +09:00
if ( $this -> scoped instanceof Profile ) {
2009-12-09 06:16:03 +09:00
// Save the access token and Twitter user info
2010-01-26 10:25:33 +09:00
2015-07-18 01:44:09 +09:00
$this -> saveForeignLink ( $this -> scoped -> getID (), $twitter_user -> id , $atok );
2010-02-06 14:39:29 +09:00
save_twitter_user ( $twitter_user -> id , $twitter_user -> screen_name );
2010-01-26 10:25:33 +09:00
} else {
2009-12-09 06:16:03 +09:00
$this -> twuid = $twitter_user -> id ;
2010-01-26 16:29:40 +09:00
$this -> tw_fields = array ( " screen_name " => $twitter_user -> screen_name ,
2011-01-21 08:47:51 +09:00
" fullname " => $twitter_user -> name );
2009-12-09 06:16:03 +09:00
$this -> access_token = $atok ;
2020-08-14 05:59:40 +09:00
$this -> tryLogin ();
return ;
2009-12-09 06:16:03 +09:00
}
2009-08-01 17:20:44 +09:00
2009-08-09 03:13:19 +09:00
// Clean up the the mess we made in the session
2009-08-01 17:20:44 +09:00
2009-08-09 03:13:19 +09:00
unset ( $_SESSION [ 'twitter_request_token' ]);
unset ( $_SESSION [ 'twitter_request_token_secret' ]);
2010-01-26 10:25:33 +09:00
2009-12-09 06:16:03 +09:00
if ( common_logged_in ()) {
common_redirect ( common_local_url ( 'twittersettings' ));
}
2009-08-09 03:13:19 +09:00
}
2009-08-01 17:20:44 +09:00
2009-08-09 03:13:19 +09:00
/**
* Saves a Foreign_link between Twitter user and local user ,
* which includes the access token and secret .
*
2010-01-26 16:29:40 +09:00
* @ param int $user_id StatusNet user ID
* @ param int $twuid Twitter user ID
* @ param OAuthToken $token the access token to save
2009-08-09 03:13:19 +09:00
*
2020-08-14 05:59:40 +09:00
* @ return void
2009-08-09 03:13:19 +09:00
*/
2020-08-14 05:59:40 +09:00
private function saveForeignLink (
int $user_id ,
int $twuid ,
OAuthToken $access_token
) : void {
2010-01-26 10:25:33 +09:00
$flink = new Foreign_link ();
2010-02-16 06:10:45 +09:00
$flink -> user_id = $user_id ;
$flink -> service = TWITTER_SERVICE ;
2010-03-05 11:27:01 +09:00
// delete stale flink, if any
$result = $flink -> find ( true );
if ( ! empty ( $result )) {
2010-03-05 12:14:40 +09:00
$flink -> safeDelete ();
2010-03-05 11:27:01 +09:00
}
2010-02-16 06:10:45 +09:00
2010-01-26 10:25:33 +09:00
$flink -> user_id = $user_id ;
$flink -> foreign_id = $twuid ;
$flink -> service = TWITTER_SERVICE ;
2010-01-26 16:29:40 +09:00
$creds = TwitterOAuthClient :: packToken ( $access_token );
2009-12-09 06:16:03 +09:00
2010-01-26 10:25:33 +09:00
$flink -> credentials = $creds ;
$flink -> created = common_sql_now ();
2009-12-09 06:16:03 +09:00
2010-01-26 10:25:33 +09:00
// Defaults: noticesync on, everything else off
2016-03-19 19:23:26 +09:00
$flink -> set_flags ( true , false , false , false , false );
2010-01-26 10:25:33 +09:00
$flink_id = $flink -> insert ();
2015-07-18 01:44:09 +09:00
// We want to make sure we got a numerical >0 value, not just failed the insert (which would be === false)
2010-01-26 10:25:33 +09:00
if ( empty ( $flink_id )) {
common_log_db_error ( $flink , 'INSERT' , __FILE__ );
2011-06-19 00:20:36 +09:00
// TRANS: Server error displayed when linking to a Twitter account fails.
2015-07-18 01:44:09 +09:00
throw new ServerException ( _m ( 'Could not link your Twitter account.' ));
2010-01-26 10:25:33 +09:00
}
}
2009-12-09 06:16:03 +09:00
2020-08-14 05:59:40 +09:00
public function getInstructions ()
2009-12-09 06:16:03 +09:00
{
2015-07-18 01:44:09 +09:00
// TRANS: Page instruction. %s is the StatusNet sitename.
return sprintf ( _m ( 'This is the first time you have logged into %s so we must connect your Twitter account to a local account. You can either create a new account, or connect with your existing account, if you have one.' ), common_config ( 'site' , 'name' ));
2009-12-09 06:16:03 +09:00
}
2020-08-14 05:59:40 +09:00
public function title ()
2009-12-09 06:16:03 +09:00
{
2011-06-19 00:20:36 +09:00
// TRANS: Page title.
2010-09-19 00:45:18 +09:00
return _m ( 'Twitter Account Setup' );
2009-12-09 06:16:03 +09:00
}
2015-07-18 04:03:37 +09:00
public function showPage ()
{
// $this->oauth_token is only populated once Twitter authorizes our
// request token. If it's empty we're at the beginning of the auth
// process
2015-07-18 08:18:08 +09:00
if ( empty ( $this -> error )) {
if ( empty ( $this -> oauth_token )) {
// authorizeRequestToken either throws an exception or redirects
$this -> authorizeRequestToken ();
} else {
$this -> saveAccessToken ();
}
2015-07-18 04:03:37 +09:00
}
parent :: showPage ();
}
2010-04-10 02:11:11 +09:00
/**
* @ fixme much of this duplicates core code , which is very fragile .
* Should probably be replaced with an extensible mini version of
* the core registration form .
*/
2020-08-14 05:59:40 +09:00
public function showContent ()
2009-12-09 06:16:03 +09:00
{
$this -> elementStart ( 'form' , array ( 'method' => 'post' ,
'id' => 'form_settings_twitter_connect' ,
'class' => 'form_settings' ,
'action' => common_local_url ( 'twitterauthorization' )));
$this -> elementStart ( 'fieldset' , array ( 'id' => 'settings_twitter_connect_options' ));
2011-06-19 00:20:36 +09:00
// TRANS: Fieldset legend.
2010-09-19 00:45:18 +09:00
$this -> element ( 'legend' , null , _m ( 'Connection options' ));
2011-09-18 07:43:21 +09:00
2009-12-09 06:16:03 +09:00
$this -> hidden ( 'access_token_key' , $this -> access_token -> key );
$this -> hidden ( 'access_token_secret' , $this -> access_token -> secret );
$this -> hidden ( 'twuid' , $this -> twuid );
2010-01-26 16:29:40 +09:00
$this -> hidden ( 'tw_fields_screen_name' , $this -> tw_fields [ 'screen_name' ]);
2011-01-21 08:47:51 +09:00
$this -> hidden ( 'tw_fields_name' , $this -> tw_fields [ 'fullname' ]);
2011-09-18 07:50:13 +09:00
$this -> hidden ( 'token' , common_session_token ());
2009-12-09 06:16:03 +09:00
2015-07-18 08:18:08 +09:00
// Only allow new account creation if site is not flagged invite-only
if ( ! common_config ( 'site' , 'inviteonly' )) {
2011-09-18 06:47:35 +09:00
$this -> elementStart ( 'fieldset' );
2020-08-14 05:59:40 +09:00
$this -> element (
'legend' ,
null ,
// TRANS: Fieldset legend.
_m ( 'Create new account' )
);
$this -> element (
'p' ,
null ,
// TRANS: Sub form introduction text.
_m ( 'Create a new user with this nickname.' )
);
2011-09-18 06:47:35 +09:00
$this -> elementStart ( 'ul' , 'form_data' );
// Hook point for captcha etc
Event :: handle ( 'StartRegistrationFormData' , array ( $this ));
$this -> elementStart ( 'li' );
// TRANS: Field label.
2020-08-14 05:59:40 +09:00
$this -> input (
'newname' ,
_m ( 'New nickname' ),
$this -> username ? : '' ,
// TRANS: Field title for nickname field.
_m ( '1-64 lowercase letters or numbers, no punctuation or spaces.' )
);
2011-09-18 06:47:35 +09:00
$this -> elementEnd ( 'li' );
$this -> elementStart ( 'li' );
// TRANS: Field label.
2020-08-14 05:59:40 +09:00
$this -> input (
'email' ,
_m ( 'LABEL' , 'Email' ),
$this -> getEmail (),
// TRANS: Field title for e-mail address field.
_m ( 'Used only for updates, announcements, and password recovery' )
);
2011-09-18 06:47:35 +09:00
$this -> elementEnd ( 'li' );
// Hook point for captcha etc
Event :: handle ( 'EndRegistrationFormData' , array ( $this ));
$this -> elementEnd ( 'ul' );
// TRANS: Button text for creating a new StatusNet account in the Twitter connect page.
2020-08-14 05:59:40 +09:00
$this -> submit ( 'create' , _m ( 'BUTTON' , 'Create' ));
2011-09-18 06:47:35 +09:00
$this -> elementEnd ( 'fieldset' );
}
2009-12-09 06:16:03 +09:00
$this -> elementStart ( 'fieldset' );
2020-08-14 05:59:40 +09:00
$this -> element (
'legend' ,
null ,
// TRANS: Fieldset legend.
_m ( 'Connect existing account' )
);
$this -> element (
'p' ,
null ,
// TRANS: Sub form introduction text.
_m ( 'If you already have an account, login with your username and password to connect it to your Twitter account.' )
);
2009-12-09 06:16:03 +09:00
$this -> elementStart ( 'ul' , 'form_data' );
$this -> elementStart ( 'li' );
2011-06-19 00:20:36 +09:00
// TRANS: Field label.
2010-09-19 00:45:18 +09:00
$this -> input ( 'nickname' , _m ( 'Existing nickname' ));
2009-12-09 06:16:03 +09:00
$this -> elementEnd ( 'li' );
$this -> elementStart ( 'li' );
2011-06-19 00:20:36 +09:00
// TRANS: Field label.
2010-09-19 00:45:18 +09:00
$this -> password ( 'password' , _m ( 'Password' ));
2009-12-09 06:16:03 +09:00
$this -> elementEnd ( 'li' );
$this -> elementEnd ( 'ul' );
$this -> elementEnd ( 'fieldset' );
2011-09-18 07:43:21 +09:00
$this -> elementStart ( 'fieldset' );
2020-08-14 05:59:40 +09:00
$this -> element (
'legend' ,
null ,
// TRANS: Fieldset legend.
_m ( 'License' )
);
2011-09-18 07:43:21 +09:00
$this -> elementStart ( 'ul' , 'form_data' );
$this -> elementStart ( 'li' );
$this -> element ( 'input' , array ( 'type' => 'checkbox' ,
'id' => 'license' ,
'class' => 'checkbox' ,
'name' => 'license' ,
'value' => 'true' ));
$this -> elementStart ( 'label' , array ( 'class' => 'checkbox' , 'for' => 'license' ));
// TRANS: Text for license agreement checkbox.
// TRANS: %s is the license as configured for the StatusNet site.
$message = _m ( 'My text and files are available under %s ' .
'except this private data: password, ' .
'email address, IM address, and phone number.' );
$link = '<a href="' .
htmlspecialchars ( common_config ( 'license' , 'url' )) .
'">' .
htmlspecialchars ( common_config ( 'license' , 'title' )) .
'</a>' ;
$this -> raw ( sprintf ( htmlspecialchars ( $message ), $link ));
$this -> elementEnd ( 'label' );
$this -> elementEnd ( 'li' );
$this -> elementEnd ( 'ul' );
$this -> elementEnd ( 'fieldset' );
2011-09-18 07:45:55 +09:00
// TRANS: Button text for connecting an existing StatusNet account in the Twitter connect page..
2020-08-14 05:59:40 +09:00
$this -> submit ( 'connect' , _m ( 'BUTTON' , 'Connect' ));
2009-12-09 06:16:03 +09:00
$this -> elementEnd ( 'fieldset' );
$this -> elementEnd ( 'form' );
}
2011-01-21 10:02:34 +09:00
/**
* Get specified e - mail from the form , or the invite code .
*
* @ return string
*/
2020-08-14 05:59:40 +09:00
private function getEmail () : string
2011-01-21 10:02:34 +09:00
{
$email = $this -> trimmed ( 'email' );
if ( ! empty ( $email )) {
return $email ;
}
// Terrible hack for invites...
if ( common_config ( 'site' , 'inviteonly' )) {
$code = $_SESSION [ 'invitecode' ];
if ( $code ) {
2013-08-18 20:04:58 +09:00
$invite = Invitation :: getKV ( $code );
2011-01-21 10:02:34 +09:00
if ( $invite && $invite -> address_type == 'email' ) {
return $invite -> address ;
}
}
}
return '' ;
}
2015-07-18 08:18:08 +09:00
protected function createNewUser ()
2009-12-09 06:16:03 +09:00
{
2015-07-18 08:18:08 +09:00
common_debug ( 'TwitterBridgeDebug - createNewUser' );
2011-01-06 05:26:20 +09:00
if ( ! Event :: handle ( 'StartRegistrationTry' , array ( $this ))) {
2015-07-18 08:18:08 +09:00
common_debug ( 'TwitterBridgeDebug - StartRegistrationTry failed' );
2015-07-18 01:44:09 +09:00
// TRANS: Client error displayed when trying to create a new user but a plugin aborted the process.
throw new ClientException ( _m ( 'Registration of new user was aborted, maybe you failed a captcha?' ));
2011-01-06 05:26:20 +09:00
}
2009-12-09 06:16:03 +09:00
if ( common_config ( 'site' , 'closed' )) {
2015-07-18 08:18:08 +09:00
common_debug ( 'TwitterBridgeDebug - site is closed for registrations' );
2011-06-19 00:20:36 +09:00
// TRANS: Client error displayed when trying to create a new user while creating new users is not allowed.
2015-07-18 01:44:09 +09:00
throw new ClientException ( _m ( 'Registration not allowed.' ));
2009-12-09 06:16:03 +09:00
}
$invite = null ;
if ( common_config ( 'site' , 'inviteonly' )) {
2015-07-18 08:18:08 +09:00
common_debug ( 'TwitterBridgeDebug - site is inviteonly' );
2009-12-09 06:16:03 +09:00
$code = $_SESSION [ 'invitecode' ];
if ( empty ( $code )) {
2011-06-19 00:20:36 +09:00
// TRANS: Client error displayed when trying to create a new user while creating new users is not allowed.
2015-07-18 01:44:09 +09:00
throw new ClientException ( _m ( 'Registration not allowed.' ));
2009-12-09 06:16:03 +09:00
}
2015-07-18 01:44:09 +09:00
$invite = Invitation :: getKV ( 'code' , $code );
2009-12-09 06:16:03 +09:00
2015-07-18 01:44:09 +09:00
if ( ! $invite instanceof Invite ) {
2015-07-18 08:18:08 +09:00
common_debug ( 'TwitterBridgeDebug - and we failed the invite code test' );
2011-06-19 00:20:36 +09:00
// TRANS: Client error displayed when trying to create a new user with an invalid invitation code.
2015-07-18 01:44:09 +09:00
throw new ClientException ( _m ( 'Not a valid invitation code.' ));
2009-12-09 06:16:03 +09:00
}
}
2015-07-18 08:18:08 +09:00
common_debug ( 'TwitterBridgeDebug - trying our nickname: ' . $this -> trimmed ( 'newname' ));
2015-07-18 01:44:09 +09:00
// Nickname::normalize throws exception if the nickname is taken
$nickname = Nickname :: normalize ( $this -> trimmed ( 'newname' ), true );
2009-12-09 06:16:03 +09:00
2011-01-21 08:47:51 +09:00
$fullname = trim ( $this -> tw_fields [ 'fullname' ]);
2009-12-09 06:16:03 +09:00
$args = array ( 'nickname' => $nickname , 'fullname' => $fullname );
if ( ! empty ( $invite )) {
$args [ 'code' ] = $invite -> code ;
}
2011-01-21 10:02:34 +09:00
$email = $this -> getEmail ();
if ( ! empty ( $email )) {
$args [ 'email' ] = $email ;
}
2020-08-14 05:59:40 +09:00
common_debug (
'TwitterBridgeDebug - registering user with args:'
. var_export ( $args , true )
);
2015-07-18 01:44:09 +09:00
$user = User :: register ( $args );
2010-03-05 11:27:01 +09:00
2020-08-14 05:59:40 +09:00
common_debug (
'TwitterBridgeDebug - registered the user and saving twitter user'
);
save_twitter_user ( $this -> twuid , $this -> tw_fields [ 'screen_name' ]);
2015-07-18 08:18:08 +09:00
2020-08-14 05:59:40 +09:00
common_debug (
'TwitterBridgeDebug - saving foreign link after creating new '
. 'local user ' . $user -> id
);
2010-01-26 16:29:40 +09:00
2020-08-14 05:59:40 +09:00
$this -> saveForeignLink (
$user -> id ,
$this -> twuid ,
$this -> access_token
);
2009-12-09 06:16:03 +09:00
common_set_user ( $user );
common_real_login ( true );
2010-01-26 10:25:33 +09:00
common_debug ( 'TwitterBridge Plugin - ' .
2010-01-26 16:29:40 +09:00
" Registered new user $user->id from Twitter user $this->twuid " );
2009-12-09 06:16:03 +09:00
2011-01-06 05:26:20 +09:00
Event :: handle ( 'EndRegistrationTry' , array ( $this ));
2014-03-10 08:19:35 +09:00
common_redirect ( common_local_url ( 'showstream' , array ( 'nickname' => $user -> nickname )), 303 );
2009-12-09 06:16:03 +09:00
}
2020-08-14 05:59:40 +09:00
private function connectNewUser () : void
2009-12-09 06:16:03 +09:00
{
$nickname = $this -> trimmed ( 'nickname' );
$password = $this -> trimmed ( 'password' );
if ( ! common_check_user ( $nickname , $password )) {
2011-06-19 00:20:36 +09:00
// TRANS: Form validation error displayed when connecting an existing user to a Twitter user fails because
// TRANS: the provided username and/or password are incorrect.
2015-07-18 01:44:09 +09:00
throw new ClientException ( _m ( 'Invalid username or password.' ));
2009-12-09 06:16:03 +09:00
}
2013-08-18 20:04:58 +09:00
$user = User :: getKV ( 'nickname' , $nickname );
2009-12-09 06:16:03 +09:00
2015-07-18 01:44:09 +09:00
if ( $user instanceof User ) {
2010-01-26 10:25:33 +09:00
common_debug ( 'TwitterBridge Plugin - ' .
2009-12-09 06:16:03 +09:00
" Legit user to connect to Twitter: $nickname " );
}
2015-07-18 01:44:09 +09:00
// throws exception on failure
2020-08-14 05:59:40 +09:00
$this -> saveForeignLink (
$user -> id ,
$this -> twuid ,
$this -> access_token
);
2010-01-26 16:29:40 +09:00
save_twitter_user ( $this -> twuid , $this -> tw_fields [ 'screen_name' ]);
2009-12-09 06:16:03 +09:00
2010-01-26 10:25:33 +09:00
common_debug ( 'TwitterBridge Plugin - ' .
2010-01-26 16:29:40 +09:00
" Connected Twitter user $this->twuid to local user $user->id " );
2009-12-09 06:16:03 +09:00
common_set_user ( $user );
common_real_login ( true );
$this -> goHome ( $user -> nickname );
}
2020-08-14 05:59:40 +09:00
private function connectUser () : void
2009-12-09 06:16:03 +09:00
{
$user = common_current_user ();
$result = $this -> flinkUser ( $user -> id , $this -> twuid );
if ( empty ( $result )) {
2011-06-19 00:20:36 +09:00
// TRANS: Server error displayed connecting a user to a Twitter user has failed.
2010-09-19 00:45:18 +09:00
$this -> serverError ( _m ( 'Error connecting user to Twitter.' ));
2009-12-09 06:16:03 +09:00
}
2010-01-26 10:25:33 +09:00
common_debug ( 'TwitterBridge Plugin - ' .
2010-01-26 16:29:40 +09:00
" Connected Twitter user $this->twuid to local user $user->id " );
2009-12-09 06:16:03 +09:00
// Return to Twitter connection settings tab
common_redirect ( common_local_url ( 'twittersettings' ), 303 );
}
2010-01-26 10:25:33 +09:00
2015-07-18 08:18:08 +09:00
protected function tryLogin ()
2009-12-09 06:16:03 +09:00
{
2010-01-26 10:25:33 +09:00
common_debug ( 'TwitterBridge Plugin - ' .
" Trying login for Twitter user $this->twuid . " );
2009-12-09 06:16:03 +09:00
2015-07-18 08:18:08 +09:00
try {
$flink = Foreign_link :: getByForeignID ( $this -> twuid , TWITTER_SERVICE );
2009-12-09 06:16:03 +09:00
$user = $flink -> getUser ();
2015-07-18 18:39:34 +09:00
common_debug ( 'TwitterBridge Plugin - ' .
" Logged in Twitter user $flink->foreign_id as user $user->id ( $user->nickname ) " );
2009-12-09 06:16:03 +09:00
2015-07-18 18:39:34 +09:00
common_set_user ( $user );
common_real_login ( true );
$this -> goHome ( $user -> nickname );
2015-07-18 08:18:08 +09:00
} catch ( NoResultException $e ) {
// Either no Foreign_link was found or not the user connected to it.
// Let's just continue to allow creating or logging in as a new user.
2009-12-09 06:16:03 +09:00
}
2015-07-18 08:18:08 +09:00
common_debug ( " TwitterBridge Plugin - No flink found for twuid: { $this -> twuid } - new user " );
2015-07-18 04:03:37 +09:00
2015-07-18 18:39:34 +09:00
// FIXME: what do we want to do here? I forgot
2015-07-18 04:03:37 +09:00
return ;
throw new ServerException ( _m ( 'No foreign link found for Twitter user' ));
2009-12-09 06:16:03 +09:00
}
2020-08-14 05:59:40 +09:00
private function goHome ( string $nickname ) : void
2009-12-09 06:16:03 +09:00
{
$url = common_get_returnto ();
if ( $url ) {
// We don't have to return to it again
common_set_returnto ( null );
} else {
2020-08-14 05:59:40 +09:00
$url = common_local_url (
'all' ,
[ 'nickname' => $nickname ]
);
2009-12-09 06:16:03 +09:00
}
common_redirect ( $url , 303 );
}
2020-08-14 05:59:40 +09:00
private function bestNewNickname () : ? string
2009-12-09 06:16:03 +09:00
{
2013-10-16 21:58:22 +09:00
try {
return Nickname :: normalize ( $this -> tw_fields [ 'fullname' ], true );
} catch ( NicknameException $e ) {
2013-10-17 08:16:03 +09:00
return null ;
2009-12-09 06:16:03 +09:00
}
}
2009-08-01 17:20:44 +09:00
}