[OpenID] Add sync confirmation in both OpenID settings and login connection

This commit is contained in:
brunoccast 2019-06-04 16:55:49 +01:00
parent 54d7a7cccd
commit 69a1d77480
3 changed files with 182 additions and 157 deletions

View File

@ -46,7 +46,7 @@ require_once INSTALLDIR.'/plugins/OpenID/openid.php';
*/ */
class FinishaddopenidAction extends Action class FinishaddopenidAction extends Action
{ {
var $msg = null; public $msg = null;
/** /**
* Handle the redirect back from OpenID confirmation * Handle the redirect back from OpenID confirmation
@ -58,7 +58,7 @@ class FinishaddopenidAction extends Action
* *
* @return void * @return void
*/ */
function handle() public function handle()
{ {
parent::handle(); parent::handle();
if (!common_logged_in()) { if (!common_logged_in()) {
@ -76,7 +76,7 @@ class FinishaddopenidAction extends Action
* *
* @return void * @return void
*/ */
function tryLogin() public function tryLogin()
{ {
$consumer = oid_consumer(); $consumer = oid_consumer();
@ -86,13 +86,14 @@ class FinishaddopenidAction extends Action
// TRANS: Status message in case the response from the OpenID provider is that the logon attempt was cancelled. // TRANS: Status message in case the response from the OpenID provider is that the logon attempt was cancelled.
$this->message(_m('OpenID authentication cancelled.')); $this->message(_m('OpenID authentication cancelled.'));
return; return;
} else if ($response->status == Auth_OpenID_FAILURE) { } elseif ($response->status == Auth_OpenID_FAILURE) {
// TRANS: OpenID authentication failed; display the error message. // TRANS: OpenID authentication failed; display the error message.
// TRANS: %s is the error message. // TRANS: %s is the error message.
$this->message(sprintf(_m('OpenID authentication failed: %s.'), $this->message(sprintf(
$response->message)); _m('OpenID authentication failed: %s.'),
} else if ($response->status == Auth_OpenID_SUCCESS) { $response->message
));
} elseif ($response->status == Auth_OpenID_SUCCESS) {
$display = $response->getDisplayIdentifier(); $display = $response->getDisplayIdentifier();
$canonical = ($response->endpoint && $response->endpoint->canonicalID) ? $canonical = ($response->endpoint && $response->endpoint->canonicalID) ?
$response->endpoint->canonicalID : $display; $response->endpoint->canonicalID : $display;
@ -136,17 +137,20 @@ class FinishaddopenidAction extends Action
$this->message(_m('Error connecting user.')); $this->message(_m('Error connecting user.'));
return; return;
} }
if (Event::handle('StartOpenIDUpdateUser', array($cur, $canonical, &$sreg))) {
if ($sreg) { if (isset($_SESSION['openid_sync']) && $_SESSION['openid_sync']) {
if (Event::handle('StartOpenIDUpdateUser', [$cur, $canonical, &$sreg])) {
if (!oid_update_user($cur, $sreg)) { if (!oid_update_user($cur, $sreg)) {
// TRANS: Message in case the user or the user profile cannot be saved in StatusNet. // TRANS: Message in case the user or the user profile cannot be saved in StatusNet.
$this->message(_m('Error updating profile.')); $this->message(_m('Error updating profile.'));
return; return;
} }
} }
Event::handle('EndOpenIDUpdateUser', [$cur, $canonical, $sreg]);
} }
Event::handle('EndOpenIDUpdateUser', array($cur, $canonical, $sreg));
unset($_SESSION['openid_sync']);
// success! // success!
$cur->query('COMMIT'); $cur->query('COMMIT');
@ -166,7 +170,7 @@ class FinishaddopenidAction extends Action
* *
* @return void * @return void
*/ */
function message($msg) public function message($msg)
{ {
$this->message = $msg; $this->message = $msg;
$this->showPage(); $this->showPage();
@ -177,7 +181,7 @@ class FinishaddopenidAction extends Action
* *
* @return string title * @return string title
*/ */
function title() public function title()
{ {
// TRANS: Title after getting the status of the OpenID authorisation request. // TRANS: Title after getting the status of the OpenID authorisation request.
return _m('OpenID Login'); return _m('OpenID Login');
@ -188,7 +192,7 @@ class FinishaddopenidAction extends Action
* *
* @return void * @return void
*/ */
function showPageNotice() public function showPageNotice()
{ {
if ($this->message) { if ($this->message) {
$this->element('p', 'error', $this->message); $this->element('p', 'error', $this->message);

View File

@ -25,17 +25,17 @@ require_once INSTALLDIR.'/plugins/OpenID/openid.php';
class FinishopenidloginAction extends Action class FinishopenidloginAction extends Action
{ {
var $error = null; public $error = null;
var $username = null; public $username = null;
var $message = null; public $message = null;
function handle() public function handle()
{ {
parent::handle(); parent::handle();
if (common_is_real_login()) { if (common_is_real_login()) {
// TRANS: Client error message trying to log on with OpenID while already logged on. // TRANS: Client error message trying to log on with OpenID while already logged on.
$this->clientError(_m('Already logged in.')); $this->clientError(_m('Already logged in.'));
} else if ($_SERVER['REQUEST_METHOD'] == 'POST') { } elseif ($_SERVER['REQUEST_METHOD'] == 'POST') {
$token = $this->trimmed('token'); $token = $this->trimmed('token');
if (!$token || $token != common_session_token()) { if (!$token || $token != common_session_token()) {
// TRANS: Message given when there is a problem with the user's session token. // TRANS: Message given when there is a problem with the user's session token.
@ -45,27 +45,31 @@ class FinishopenidloginAction extends Action
if ($this->arg('create')) { if ($this->arg('create')) {
if (!$this->boolean('license')) { if (!$this->boolean('license')) {
// TRANS: Message given if user does not agree with the site's license. // TRANS: Message given if user does not agree with the site's license.
$this->showForm(_m('You cannot register if you do not agree to the license.'), $this->showForm(
$this->trimmed('newname')); _m('You cannot register if you do not agree to the license.'),
$this->trimmed('newname')
);
return; return;
} }
$this->createNewUser(); $this->createNewUser();
} else if ($this->arg('connect')) { } elseif ($this->arg('connect')) {
$this->connectUser(); $this->connectUser();
} else { } else {
// TRANS: Messag given on an unknown error. // TRANS: Messag given on an unknown error.
$this->showForm(_m('An unknown error has occured.'), $this->showForm(
$this->trimmed('newname')); _m('An unknown error has occured.'),
$this->trimmed('newname')
);
} }
} else { } else {
$this->tryLogin(); $this->tryLogin();
} }
} }
function showPageNotice() public function showPageNotice()
{ {
if ($this->error) { if ($this->error) {
$this->element('div', array('class' => 'error'), $this->error); $this->element('div', ['class' => 'error'], $this->error);
} else { } else {
$this->element('div', 'instructions', $this->element('div', 'instructions',
// TRANS: Instructions given after a first successful logon using OpenID. // TRANS: Instructions given after a first successful logon using OpenID.
@ -74,13 +78,13 @@ class FinishopenidloginAction extends Action
} }
} }
function title() public function title()
{ {
// TRANS: Title // TRANS: Title
return _m('TITLE','OpenID Account Setup'); return _m('TITLE', 'OpenID Account Setup');
} }
function showForm($error=null, $username=null) public function showForm($error=null, $username=null)
{ {
$this->error = $error; $this->error = $error;
$this->username = $username; $this->username = $username;
@ -93,10 +97,10 @@ class FinishopenidloginAction extends Action
* Should probably be replaced with an extensible mini version of * Should probably be replaced with an extensible mini version of
* the core registration form. * the core registration form.
*/ */
function showContent() public function showContent()
{ {
if (!empty($this->message_text)) { if (!empty($this->message_text)) {
$this->element('div', array('class' => 'error'), $this->message_text); $this->element('div', ['class' => 'error'], $this->message_text);
return; return;
} }
@ -107,12 +111,12 @@ class FinishopenidloginAction extends Action
// info. The profile will be pre-populated with whatever name, // info. The profile will be pre-populated with whatever name,
// email, and location we can get from the OpenID provider, so // email, and location we can get from the OpenID provider, so
// all we ask for is the license confirmation. // all we ask for is the license confirmation.
$this->elementStart('form', array('method' => 'post', $this->elementStart('form', ['method' => 'post',
'id' => 'account_create', 'id' => 'account_create',
'class' => 'form_settings', 'class' => 'form_settings',
'action' => common_local_url('finishopenidlogin'))); 'action' => common_local_url('finishopenidlogin')]);
$this->hidden('token', common_session_token()); $this->hidden('token', common_session_token());
$this->elementStart('fieldset', array('id' => 'form_openid_createaccount')); $this->elementStart('fieldset', ['id' => 'form_openid_createaccount']);
$this->element('legend', null, $this->element('legend', null,
// TRANS: Fieldset legend. // TRANS: Fieldset legend.
_m('Create new account')); _m('Create new account'));
@ -122,39 +126,41 @@ class FinishopenidloginAction extends Action
$this->elementStart('ul', 'form_data'); $this->elementStart('ul', 'form_data');
// Hook point for captcha etc // Hook point for captcha etc
Event::handle('StartRegistrationFormData', array($this)); Event::handle('StartRegistrationFormData', [$this]);
$this->elementStart('li'); $this->elementStart('li');
// TRANS: Field label. // TRANS: Field label.
$this->input('newname', _m('New nickname'), $this->input('newname',
_m('New nickname'),
($this->username) ? $this->username : '', ($this->username) ? $this->username : '',
// TRANS: Field title. // TRANS: Field title.
_m('1-64 lowercase letters or numbers, no punctuation or spaces.')); _m('1-64 lowercase letters or numbers, no punctuation or spaces.'));
$this->elementEnd('li'); $this->elementEnd('li');
$this->elementStart('li'); $this->elementStart('li');
// TRANS: Field label. // TRANS: Field label.
$this->input('email', _m('Email'), $this->getEmail(), $this->input('email', _m('Email'),
$this->getEmail(),
// TRANS: Field title. // TRANS: Field title.
_m('Used only for updates, announcements, '. _m('Used only for updates, announcements, '.
'and password recovery.')); 'and password recovery.'));
$this->elementEnd('li'); $this->elementEnd('li');
// Hook point for captcha etc // Hook point for captcha etc
Event::handle('EndRegistrationFormData', array($this)); Event::handle('EndRegistrationFormData', [$this]);
$this->elementStart('li'); $this->elementStart('li');
$this->element('input', array('type' => 'checkbox', $this->element('input', ['type' => 'checkbox',
'id' => 'license', 'id' => 'license',
'class' => 'checkbox', 'class' => 'checkbox',
'name' => 'license', 'name' => 'license',
'value' => 'true')); 'value' => 'true']);
$this->elementStart('label', array('for' => 'license', $this->elementStart('label', ['for' => 'license',
'class' => 'checkbox')); 'class' => 'checkbox']);
// TRANS: OpenID plugin link text. // TRANS: OpenID plugin link text.
// TRANS: %s is a link to a license with the license name as link text. // TRANS: %s is a link to a license with the license name as link text.
$message = _m('My text and files are available under %s ' . $message = _m('My text and files are available under %s ' .
'except this private data: password, ' . 'except this private data: password, ' .
'email address, IM address, and phone number.'); 'email address, IM address, and phone number.');
$link = '<a href="' . $link = '<a href="' .
htmlspecialchars(common_config('license', 'url')) . htmlspecialchars(common_config('license', 'url')) .
'">' . '">' .
@ -171,12 +177,12 @@ class FinishopenidloginAction extends Action
// The second option is to attach this OpenID to an existing account // The second option is to attach this OpenID to an existing account
// on the local system, which they need to provide a password for. // on the local system, which they need to provide a password for.
$this->elementStart('form', array('method' => 'post', $this->elementStart('form', ['method' => 'post',
'id' => 'account_connect', 'id' => 'account_connect',
'class' => 'form_settings', 'class' => 'form_settings',
'action' => common_local_url('finishopenidlogin'))); 'action' => common_local_url('finishopenidlogin')]);
$this->hidden('token', common_session_token()); $this->hidden('token', common_session_token());
$this->elementStart('fieldset', array('id' => 'form_openid_createaccount')); $this->elementStart('fieldset', ['id' => 'form_openid_createaccount']);
$this->element('legend', null, $this->element('legend', null,
// TRANS: Used as form legend for form in which to connect an OpenID to an existing user on the site. // TRANS: Used as form legend for form in which to connect an OpenID to an existing user on the site.
_m('Connect existing account')); _m('Connect existing account'));
@ -192,6 +198,11 @@ class FinishopenidloginAction extends Action
// TRANS: Field label in form in which to connect an OpenID to an existing user on the site. // TRANS: Field label in form in which to connect an OpenID to an existing user on the site.
$this->password('password', _m('Password')); $this->password('password', _m('Password'));
$this->elementEnd('li'); $this->elementEnd('li');
$this->elementStart('li');
// TRANS: Field label in form in which to connect an OpenID to an existing user on the site.
$this->checkbox('openid-sync', _m('Sync Account'), false,
_m('Syncronize GNU social profile with this OpenID identity.'));
$this->elementEnd('li');
$this->elementEnd('ul'); $this->elementEnd('ul');
// TRANS: Button text in form in which to connect an OpenID to an existing user on the site. // TRANS: Button text in form in which to connect an OpenID to an existing user on the site.
$this->submit('connect', _m('BUTTON', 'Connect')); $this->submit('connect', _m('BUTTON', 'Connect'));
@ -205,7 +216,7 @@ class FinishopenidloginAction extends Action
* *
* @return string * @return string
*/ */
function getEmail() public function getEmail()
{ {
$email = $this->trimmed('email'); $email = $this->trimmed('email');
if (!empty($email)) { if (!empty($email)) {
@ -232,7 +243,7 @@ class FinishopenidloginAction extends Action
return ''; return '';
} }
function tryLogin() public function tryLogin()
{ {
$consumer = oid_consumer(); $consumer = oid_consumer();
@ -242,10 +253,10 @@ class FinishopenidloginAction extends Action
// TRANS: Status message in case the response from the OpenID provider is that the logon attempt was cancelled. // TRANS: Status message in case the response from the OpenID provider is that the logon attempt was cancelled.
$this->message(_m('OpenID authentication cancelled.')); $this->message(_m('OpenID authentication cancelled.'));
return; return;
} else if ($response->status == Auth_OpenID_FAILURE) { } elseif ($response->status == Auth_OpenID_FAILURE) {
// TRANS: OpenID authentication failed; display the error message. %s is the error message. // TRANS: OpenID authentication failed; display the error message. %s is the error message.
$this->message(sprintf(_m('OpenID authentication failed: %s.'), $response->message)); $this->message(sprintf(_m('OpenID authentication failed: %s.'), $response->message));
} else if ($response->status == Auth_OpenID_SUCCESS) { } elseif ($response->status == Auth_OpenID_SUCCESS) {
// This means the authentication succeeded; extract the // This means the authentication succeeded; extract the
// identity URL and Simple Registration data (if it was // identity URL and Simple Registration data (if it was
// returned). // returned).
@ -290,13 +301,13 @@ class FinishopenidloginAction extends Action
} }
} }
function message($msg) public function message($msg)
{ {
$this->message_text = $msg; $this->message_text = $msg;
$this->showPage(); $this->showPage();
} }
function saveValues($display, $canonical, $sreg) public function saveValues($display, $canonical, $sreg)
{ {
common_ensure_session(); common_ensure_session();
$_SESSION['openid_display'] = $display; $_SESSION['openid_display'] = $display;
@ -304,18 +315,18 @@ class FinishopenidloginAction extends Action
$_SESSION['openid_sreg'] = $sreg; $_SESSION['openid_sreg'] = $sreg;
} }
function getSavedValues() public function getSavedValues()
{ {
return array($_SESSION['openid_display'], return [$_SESSION['openid_display'],
$_SESSION['openid_canonical'], $_SESSION['openid_canonical'],
$_SESSION['openid_sreg']); $_SESSION['openid_sreg']];
} }
function createNewUser() public function createNewUser()
{ {
// FIXME: save invite code before redirect, and check here // FIXME: save invite code before redirect, and check here
if (!Event::handle('StartRegistrationTry', array($this))) { if (!Event::handle('StartRegistrationTry', [$this])) {
return; return;
} }
@ -364,7 +375,7 @@ class FinishopenidloginAction extends Action
$this->serverError(_m('Creating new account for OpenID that already has a user.')); $this->serverError(_m('Creating new account for OpenID that already has a user.'));
} }
Event::handle('StartOpenIDCreateNewUser', array($canonical, &$sreg)); Event::handle('StartOpenIDCreateNewUser', [$canonical, &$sreg]);
$location = ''; $location = '';
if (!empty($sreg['country'])) { if (!empty($sreg['country'])) {
@ -388,10 +399,10 @@ class FinishopenidloginAction extends Action
// XXX: add language // XXX: add language
// XXX: add timezone // XXX: add timezone
$args = array('nickname' => $nickname, $args = ['nickname' => $nickname,
'email' => $email, 'email' => $email,
'fullname' => $fullname, 'fullname' => $fullname,
'location' => $location); 'location' => $location];
if (!empty($invite)) { if (!empty($invite)) {
$args['code'] = $invite->code; $args['code'] = $invite->code;
@ -401,7 +412,7 @@ class FinishopenidloginAction extends Action
$result = oid_link_user($user->id, $canonical, $display); $result = oid_link_user($user->id, $canonical, $display);
Event::handle('EndOpenIDCreateNewUser', array($user, $canonical, $sreg)); Event::handle('EndOpenIDCreateNewUser', [$user, $canonical, $sreg]);
oid_set_last($display); oid_set_last($display);
common_set_user($user); common_set_user($user);
@ -411,16 +422,17 @@ class FinishopenidloginAction extends Action
} }
unset($_SESSION['openid_rememberme']); unset($_SESSION['openid_rememberme']);
Event::handle('EndRegistrationTry', array($this)); Event::handle('EndRegistrationTry', [$this]);
common_redirect(common_local_url('showstream', array('nickname' => $user->nickname)), 303); common_redirect(common_local_url('showstream', ['nickname' => $user->nickname]), 303);
} }
function connectUser() public function connectUser()
{ {
$nickname = $this->trimmed('nickname'); $nickname = $this->trimmed('nickname');
$password = $this->trimmed('password'); $password = $this->trimmed('password');
$sync = $this->boolean('openid-sync');
if (!common_check_user($nickname, $password)) { if (!common_check_user($nickname, $password)) {
// TRANS: OpenID plugin message. // TRANS: OpenID plugin message.
$this->showForm(_m('Invalid username or password.')); $this->showForm(_m('Invalid username or password.'));
@ -445,10 +457,12 @@ class FinishopenidloginAction extends Action
$this->serverError(_m('Error connecting user to OpenID.')); $this->serverError(_m('Error connecting user to OpenID.'));
} }
if (Event::handle('StartOpenIDUpdateUser', array($user, $canonical, &$sreg))) { if ($sync) {
oid_update_user($user, $sreg); if (Event::handle('StartOpenIDUpdateUser', [$user, $canonical, &$sreg])) {
oid_update_user($user, $sreg);
}
Event::handle('EndOpenIDUpdateUser', [$user, $canonical, $sreg]);
} }
Event::handle('EndOpenIDUpdateUser', array($user, $canonical, $sreg));
oid_set_last($display); oid_set_last($display);
common_set_user($user); common_set_user($user);
@ -460,22 +474,20 @@ class FinishopenidloginAction extends Action
$this->goHome($user->nickname); $this->goHome($user->nickname);
} }
function goHome($nickname) public function goHome($nickname)
{ {
$url = common_get_returnto(); $url = common_get_returnto();
if ($url) { if ($url) {
// We don't have to return to it again // We don't have to return to it again
common_set_returnto(null); common_set_returnto(null);
$url = common_inject_session($url); $url = common_inject_session($url);
} else { } else {
$url = common_local_url('all', $url = common_local_url('all', ['nickname' => $nickname]);
array('nickname' =>
$nickname));
} }
common_redirect($url, 303); common_redirect($url, 303);
} }
function bestNewNickname($display, $sreg) public function bestNewNickname($display, $sreg)
{ {
// Try the passed-in nickname // Try the passed-in nickname
@ -508,7 +520,7 @@ class FinishopenidloginAction extends Action
return null; return null;
} }
function openidToNickname($openid) public function openidToNickname($openid)
{ {
if (Auth_Yadis_identifierScheme($openid) == 'XRI') { if (Auth_Yadis_identifierScheme($openid) == 'XRI') {
return $this->xriToNickname($openid); return $this->xriToNickname($openid);
@ -521,12 +533,12 @@ class FinishopenidloginAction extends Action
// 1. Plain hostname, like http://evanp.myopenid.com/ // 1. Plain hostname, like http://evanp.myopenid.com/
// 2. One element in path, like http://profile.typekey.com/EvanProdromou/ // 2. One element in path, like http://profile.typekey.com/EvanProdromou/
// or http://getopenid.com/evanprodromou // or http://getopenid.com/evanprodromou
function urlToNickname($openid) public function urlToNickname($openid)
{ {
return common_url_to_nickname($openid); return common_url_to_nickname($openid);
} }
function xriToNickname($xri) public function xriToNickname($xri)
{ {
$base = $this->xriBase($xri); $base = $this->xriBase($xri);
@ -540,7 +552,7 @@ class FinishopenidloginAction extends Action
} }
} }
function xriBase($xri) public function xriBase($xri)
{ {
if (substr($xri, 0, 6) == 'xri://') { if (substr($xri, 0, 6) == 'xri://') {
return substr($xri, 6); return substr($xri, 6);

View File

@ -27,7 +27,9 @@
* @link http://status.net/ * @link http://status.net/
*/ */
if (!defined('GNUSOCIAL')) { exit(1); } if (!defined('GNUSOCIAL')) {
exit(1);
}
require_once INSTALLDIR.'/plugins/OpenID/openid.php'; require_once INSTALLDIR.'/plugins/OpenID/openid.php';
@ -49,10 +51,10 @@ class OpenidsettingsAction extends SettingsAction
* *
* @return string Page title * @return string Page title
*/ */
function title() public function title()
{ {
// TRANS: Title of OpenID settings page for a user. // TRANS: Title of OpenID settings page for a user.
return _m('TITLE','OpenID settings'); return _m('TITLE', 'OpenID settings');
} }
/** /**
@ -60,16 +62,16 @@ class OpenidsettingsAction extends SettingsAction
* *
* @return string Instructions for use * @return string Instructions for use
*/ */
function getInstructions() public function getInstructions()
{ {
// TRANS: Form instructions for OpenID settings. // TRANS: Form instructions for OpenID settings.
// TRANS: This message contains Markdown links in the form [description](link). // TRANS: This message contains Markdown links in the form [description](link).
return _m('[OpenID](%%doc.openid%%) lets you log into many sites ' . return _m('[OpenID](%%doc.openid%%) lets you log into many sites ' .
'with the same user account. '. 'with the same user account. '.
'Manage your associated OpenIDs from here.'); 'Manage your associated OpenIDs from here.');
} }
function showScripts() public function showScripts()
{ {
parent::showScripts(); parent::showScripts();
$this->autofocus('openid_url'); $this->autofocus('openid_url');
@ -82,30 +84,36 @@ class OpenidsettingsAction extends SettingsAction
* *
* @return void * @return void
*/ */
function showContent() public function showContent()
{ {
if (!common_config('openid', 'trusted_provider')) { if (!common_config('openid', 'trusted_provider')) {
$this->elementStart('form', array('method' => 'post', $this->elementStart('form', ['method' => 'post',
'id' => 'form_settings_openid_add', 'id' => 'form_settings_openid_add',
'class' => 'form_settings', 'class' => 'form_settings',
'action' => 'action' =>
common_local_url('openidsettings'))); common_local_url('openidsettings')]);
$this->elementStart('fieldset', array('id' => 'settings_openid_add')); $this->elementStart('fieldset', ['id' => 'settings_openid_add']);
// TRANS: Fieldset legend. // TRANS: Fieldset legend.
$this->element('legend', null, _m('LEGEND','Add OpenID')); $this->element('legend', null, _m('LEGEND', 'Add OpenID'));
$this->hidden('token', common_session_token()); $this->hidden('token', common_session_token());
$this->elementStart('ul', 'form_data'); $this->elementStart('ul', 'form_data');
$this->elementStart('li'); $this->elementStart('li');
// TRANS: Field label. // TRANS: Field label.
$this->input('openid_url', _m('OpenID URL'), null, $this->input('openid_url', _m('OpenID URL'), null,
// TRANS: Form guide. // TRANS: Form guide.
_m('An OpenID URL which identifies you.'), null, true, _m('An OpenID URL which identifies you.'),
array('placeholder'=>'https://example.com/you')); null, true,
['placeholder'=>'https://example.com/you']);
$this->elementEnd('li');
$this->elementStart('li');
// TRANS: Field label.
$this->checkbox('openid-sync', _m('Sync Account'), false,
_m('Syncronize GNU social profile with this OpenID identity.'));
$this->elementEnd('li'); $this->elementEnd('li');
$this->elementEnd('ul'); $this->elementEnd('ul');
// TRANS: Button text for adding an OpenID URL. // TRANS: Button text for adding an OpenID URL.
$this->submit('settings_openid_add_action-submit', _m('BUTTON','Add'), 'submit', 'add'); $this->submit('settings_openid_add_action-submit', _m('BUTTON', 'Add'), 'submit', 'add');
$this->elementEnd('fieldset'); $this->elementEnd('fieldset');
$this->elementEnd('form'); $this->elementEnd('form');
} }
@ -117,46 +125,40 @@ class OpenidsettingsAction extends SettingsAction
if ($cnt > 0) { if ($cnt > 0) {
// TRANS: Header on OpenID settings page. // TRANS: Header on OpenID settings page.
$this->element('h2', null, _m('HEADER','Remove OpenID')); $this->element('h2', null, _m('HEADER', 'Remove OpenID'));
if ($cnt == 1 && !$this->scoped->hasPassword()) { if ($cnt == 1 && !$this->scoped->hasPassword()) {
$this->element('p', 'form_guide', $this->element('p', 'form_guide',
// TRANS: Form guide. // TRANS: Form guide.
_m('Removing your only OpenID '. _m('Removing your only OpenID '.
'would make it impossible to log in! ' . 'would make it impossible to log in! ' .
'If you need to remove it, '. 'If you need to remove it, '.
'add another OpenID first.')); 'add another OpenID first.'));
if ($oid->fetch()) { if ($oid->fetch()) {
$this->elementStart('p'); $this->elementStart('p');
$this->element('a', array('href' => $oid->canonical), $this->element('a', ['href' => $oid->canonical], $oid->display);
$oid->display);
$this->elementEnd('p'); $this->elementEnd('p');
} }
} else { } else {
$this->element('p', 'form_guide', $this->element('p', 'form_guide',
// TRANS: Form guide. // TRANS: Form guide.
_m('You can remove an OpenID from your account '. _m('You can remove an OpenID from your account '.
'by clicking the button marked "Remove".')); 'by clicking the button marked "Remove".'));
$idx = 0; $idx = 0;
while ($oid->fetch()) { while ($oid->fetch()) {
$this->elementStart('form', $this->elementStart('form', ['method' => 'POST',
array('method' => 'POST', 'id' => 'form_settings_openid_delete' . $idx,
'id' => 'form_settings_openid_delete' . $idx, 'class' => 'form_settings',
'class' => 'form_settings', 'action' =>
'action' => common_local_url('openidsettings')]);
common_local_url('openidsettings')));
$this->elementStart('fieldset'); $this->elementStart('fieldset');
$this->hidden('token', common_session_token()); $this->hidden('token', common_session_token());
$this->element('a', array('href' => $oid->canonical), $this->element('a', ['href' => $oid->canonical], $oid->display);
$oid->display);
$this->hidden("openid_url{$idx}", $oid->canonical, 'openid_url'); $this->hidden("openid_url{$idx}", $oid->canonical, 'openid_url');
// TRANS: Button text to remove an OpenID. // TRANS: Button text to remove an OpenID.
$this->submit("remove{$idx}", _m('BUTTON','Remove'), 'submit remove', 'remove'); $this->submit("remove{$idx}", _m('BUTTON', 'Remove'), 'submit remove', 'remove');
$this->elementEnd('fieldset'); $this->elementEnd('fieldset');
$this->elementEnd('form'); $this->elementEnd('form');
$idx++; $idx++;
@ -164,49 +166,51 @@ class OpenidsettingsAction extends SettingsAction
} }
} }
$this->elementStart('form', array('method' => 'post', $this->elementStart('form', ['method' => 'post',
'id' => 'form_settings_openid_trustroots', 'id' => 'form_settings_openid_trustroots',
'class' => 'form_settings', 'class' => 'form_settings',
'action' => 'action' =>
common_local_url('openidsettings'))); common_local_url('openidsettings')]);
$this->elementStart('fieldset', array('id' => 'settings_openid_trustroots')); $this->elementStart('fieldset', ['id' => 'settings_openid_trustroots']);
// TRANS: Fieldset legend. // TRANS: Fieldset legend.
$this->element('legend', null, _m('OpenID Trusted Sites')); $this->element('legend', null, _m('OpenID Trusted Sites'));
$this->hidden('token', common_session_token()); $this->hidden('token', common_session_token());
$this->element('p', 'form_guide', $this->element('p', 'form_guide',
// TRANS: Form guide. // TRANS: Form guide.
_m('The following sites are allowed to access your ' . _m('The following sites are allowed to access your ' .
'identity and log you in. You can remove a site from ' . 'identity and log you in. You can remove a site from ' .
'this list to deny it access to your OpenID.')); 'this list to deny it access to your OpenID.'));
$this->elementStart('ul', 'form_data'); $this->elementStart('ul', 'form_data');
$user_openid_trustroot = new User_openid_trustroot(); $user_openid_trustroot = new User_openid_trustroot();
$user_openid_trustroot->user_id = $this->scoped->getID(); $user_openid_trustroot->user_id = $this->scoped->getID();
if($user_openid_trustroot->find()) { if ($user_openid_trustroot->find()) {
while($user_openid_trustroot->fetch()) { while ($user_openid_trustroot->fetch()) {
$this->elementStart('li'); $this->elementStart('li');
$this->element('input', array('name' => 'openid_trustroot[]', $this->element('input', ['name' => 'openid_trustroot[]',
'type' => 'checkbox', 'type' => 'checkbox',
'class' => 'checkbox', 'class' => 'checkbox',
'value' => $user_openid_trustroot->trustroot, 'value' => $user_openid_trustroot->trustroot,
'id' => 'openid_trustroot_' . crc32($user_openid_trustroot->trustroot))); 'id' => 'openid_trustroot_' . crc32($user_openid_trustroot->trustroot)]);
$this->element('label', array('class'=>'checkbox', 'for' => 'openid_trustroot_' . crc32($user_openid_trustroot->trustroot)), $this->element('label',
['class'=>'checkbox',
'for' => 'openid_trustroot_' . crc32($user_openid_trustroot->trustroot)],
$user_openid_trustroot->trustroot); $user_openid_trustroot->trustroot);
$this->elementEnd('li'); $this->elementEnd('li');
} }
} }
$this->elementEnd('ul'); $this->elementEnd('ul');
// TRANS: Button text to remove an OpenID trustroot. // TRANS: Button text to remove an OpenID trustroot.
$this->submit('settings_openid_trustroots_action-submit', _m('BUTTON','Remove'), 'submit', 'remove_trustroots'); $this->submit('settings_openid_trustroots_action-submit', _m('BUTTON', 'Remove'), 'submit', 'remove_trustroots');
$this->elementEnd('fieldset'); $this->elementEnd('fieldset');
$prefs = User_openid_prefs::getKV('user_id', $this->scoped->getID()); $prefs = User_openid_prefs::getKV('user_id', $this->scoped->getID());
$this->elementStart('fieldset'); $this->elementStart('fieldset');
$this->element('legend', null, _m('LEGEND','Preferences')); $this->element('legend', null, _m('LEGEND', 'Preferences'));
$this->elementStart('ul', 'form_data'); $this->elementStart('ul', 'form_data');
$this->checkbox('hide_profile_link', "Hide OpenID links from my profile", !empty($prefs) && $prefs->hide_profile_link); $this->checkbox('hide_profile_link', "Hide OpenID links from my profile", !empty($prefs) && $prefs->hide_profile_link);
// TRANS: Button text to save OpenID prefs // TRANS: Button text to save OpenID prefs
$this->submit('settings_openid_prefs_save', _m('BUTTON','Save'), 'submit', 'save_prefs'); $this->submit('settings_openid_prefs_save', _m('BUTTON', 'Save'), 'submit', 'save_prefs');
$this->elementEnd('ul'); $this->elementEnd('ul');
$this->elementEnd('fieldset'); $this->elementEnd('fieldset');
@ -227,17 +231,21 @@ class OpenidsettingsAction extends SettingsAction
// TRANS: Form validation error if no OpenID providers can be added. // TRANS: Form validation error if no OpenID providers can be added.
throw new ServerException(_m('Cannot add new providers.')); throw new ServerException(_m('Cannot add new providers.'));
} else { } else {
common_ensure_session();
$_SESSION['openid_sync'] = $this->boolean('openid-sync');
$result = oid_authenticate($this->trimmed('openid_url'), 'finishaddopenid'); $result = oid_authenticate($this->trimmed('openid_url'), 'finishaddopenid');
if (is_string($result)) { // error message if (is_string($result)) { // error message
unset($_SESSION['openid-sync']);
throw new ServerException($result); throw new ServerException($result);
} }
return _('Added new provider.'); return _('Added new provider.');
} }
} else if ($this->arg('remove')) { } elseif ($this->arg('remove')) {
return $this->removeOpenid(); return $this->removeOpenid();
} else if($this->arg('remove_trustroots')) { } elseif ($this->arg('remove_trustroots')) {
return $this->removeTrustroots(); return $this->removeTrustroots();
} else if($this->arg('save_prefs')) { } elseif ($this->arg('save_prefs')) {
return $this->savePrefs(); return $this->savePrefs();
} }
@ -253,13 +261,14 @@ class OpenidsettingsAction extends SettingsAction
* *
* @return void * @return void
*/ */
function removeTrustroots() public function removeTrustroots()
{ {
$trustroots = $this->arg('openid_trustroot', array()); $trustroots = $this->arg('openid_trustroot', []);
foreach($trustroots as $trustroot) { foreach ($trustroots as $trustroot) {
$user_openid_trustroot = User_openid_trustroot::pkeyGet( $user_openid_trustroot = User_openid_trustroot::pkeyGet(
array('user_id'=>$this->scoped->getID(), 'trustroot'=>$trustroot)); ['user_id'=>$this->scoped->getID(), 'trustroot'=>$trustroot]
if($user_openid_trustroot) { );
if ($user_openid_trustroot) {
$user_openid_trustroot->delete(); $user_openid_trustroot->delete();
} else { } else {
// TRANS: Form validation error when trying to remove a non-existing trustroot. // TRANS: Form validation error when trying to remove a non-existing trustroot.
@ -279,7 +288,7 @@ class OpenidsettingsAction extends SettingsAction
* *
* @return void * @return void
*/ */
function removeOpenid() public function removeOpenid()
{ {
$oid = User_openid::getKV('canonical', $this->trimmed('openid_url')); $oid = User_openid::getKV('canonical', $this->trimmed('openid_url'));
@ -304,7 +313,7 @@ class OpenidsettingsAction extends SettingsAction
* *
* @return void * @return void
*/ */
function savePrefs() public function savePrefs()
{ {
$orig = null; $orig = null;
$prefs = User_openid_prefs::getKV('user_id', $this->scoped->getID()); $prefs = User_openid_prefs::getKV('user_id', $this->scoped->getID());