123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768 |
- <?php
- if (!defined('STATUSNET')) {
- exit(1);
- }
- class OpenIDPlugin extends Plugin
- {
- const PLUGIN_VERSION = '2.1.1';
-
-
- public $openidOnly = null;
- function initialize()
- {
- parent::initialize();
- if ($this->openidOnly !== null) {
- global $config;
- $config['site']['openidonly'] = (bool)$this->openidOnly;
- }
- }
-
- public function onStartInitializeRouter(URLMapper $m)
- {
- $m->connect('main/openid', array('action' => 'openidlogin'));
- $m->connect('main/openidtrust', array('action' => 'openidtrust'));
- $m->connect('settings/openid', array('action' => 'openidsettings'));
- $m->connect('index.php?action=finishopenidlogin',
- array('action' => 'finishopenidlogin'));
- $m->connect('index.php?action=finishaddopenid',
- array('action' => 'finishaddopenid'));
- $m->connect('index.php?action=finishsynchopenid',
- array('action' => 'finishsynchopenid'));
- $m->connect('main/openidserver', array('action' => 'openidserver'));
- $m->connect('panel/openid', array('action' => 'openidadminpanel'));
- return true;
- }
-
- function onStartConnectPath(&$path, &$defaults, &$rules, &$result)
- {
- if (common_config('site', 'openidonly')) {
-
-
-
-
-
-
-
- static $block = array('main/recoverpassword',
- 'settings/password');
- if (in_array($path, $block)) {
- return false;
- }
- }
- return true;
- }
-
- function onArgsInitialize($args)
- {
- if (common_config('site', 'openidonly')) {
- if (array_key_exists('action', $args)) {
- $action = trim($args['action']);
- if (in_array($action, array('login', 'register'))) {
- common_redirect(common_local_url('openidlogin'));
- } else if ($action == 'passwordsettings') {
- common_redirect(common_local_url('openidsettings'));
- } else if ($action == 'recoverpassword') {
-
- throw new ClientException(_m('Unavailable action.'));
- }
- }
- }
- return true;
- }
-
- function onEndPublicXRDS(Action $action, &$xrdsOutputter)
- {
- $xrdsOutputter->elementStart('XRD', array('xmlns' => 'xri://$xrd*($v*2.0)',
- 'xmlns:simple' => 'http://xrds-simple.net/core/1.0',
- 'version' => '2.0'));
- $xrdsOutputter->element('Type', null, 'xri://$xrds*simple');
-
- foreach (array('finishopenidlogin', 'finishaddopenid') as $finish) {
- $xrdsOutputter->showXrdsService(Auth_OpenID_RP_RETURN_TO_URL_TYPE,
- common_local_url($finish));
- }
-
- $xrdsOutputter->showXrdsService('http://specs.openid.net/auth/2.0/server',
- common_local_url('openidserver'),
- null,
- null,
- 'http://specs.openid.net/auth/2.0/identifier_select');
- $xrdsOutputter->elementEnd('XRD');
- }
-
- function onStartPrimaryNav($action)
- {
- if (common_config('site', 'openidonly') && !common_logged_in()) {
-
- $tooltip = _m('TOOLTIP', 'Login to the site.');
- $action->menuItem(common_local_url('openidlogin'),
-
- _m('MENU', 'Login'),
- $tooltip,
- false,
- 'nav_login');
-
- $tooltip = _m('TOOLTIP', 'Help me!');
- $action->menuItem(common_local_url('doc', array('title' => 'help')),
-
- _m('MENU', 'Help'),
- $tooltip,
- false,
- 'nav_help');
- if (!common_config('site', 'private')) {
-
- $tooltip = _m('TOOLTIP', 'Search for people or text.');
- $action->menuItem(common_local_url('peoplesearch'),
-
- _m('MENU', 'Search'), $tooltip, false, 'nav_search');
- }
- Event::handle('EndPrimaryNav', array($action));
- return false;
- }
- return true;
- }
-
- function onStartLoginGroupNav($action)
- {
- if (common_config('site', 'openidonly')) {
- $this->showOpenIDLoginTab($action);
-
-
-
- return false;
- }
- return true;
- }
-
- function onEndLoginGroupNav($action)
- {
- $this->showOpenIDLoginTab($action);
- return true;
- }
-
- function showOpenIDLoginTab($action)
- {
- $action_name = $action->trimmed('action');
- $action->menuItem(common_local_url('openidlogin'),
-
- _m('MENU', 'OpenID'),
-
- _m('Login or register with OpenID.'),
- $action_name === 'openidlogin');
- }
-
- function onStartAccountSettingsPasswordMenuItem($menu, &$unused) {
- if (common_config('site', 'openidonly')) {
- return false;
- }
- return true;
- }
-
- function onEndAccountSettingsNav($action)
- {
- $action_name = $action->trimmed('action');
- $action->menuItem(common_local_url('openidsettings'),
-
- _m('MENU', 'OpenID'),
-
- _m('Add or remove OpenIDs.'),
- $action_name === 'openidsettings');
- return true;
- }
-
- function onAutoload($cls)
- {
- switch ($cls)
- {
- case 'Auth_OpenID_TeamsExtension':
- case 'Auth_OpenID_TeamsRequest':
- case 'Auth_OpenID_TeamsResponse':
- require_once dirname(__FILE__) . '/extlib/teams-extension.php';
- return false;
- }
- return parent::onAutoload($cls);
- }
-
- function onLoginAction($action, &$login)
- {
- switch ($action)
- {
- case 'openidlogin':
- case 'finishopenidlogin':
- case 'openidserver':
- $login = true;
- return false;
- default:
- return true;
- }
- }
-
- function onEndShowHeadElements(Action $action)
- {
- if ($action instanceof ShowstreamAction) {
- $action->element('link', array('rel' => 'openid2.provider',
- 'href' => common_local_url('openidserver')));
- $action->element('link', array('rel' => 'openid2.local_id',
- 'href' => $action->getTarget()->getUrl()));
- $action->element('link', array('rel' => 'openid.server',
- 'href' => common_local_url('openidserver')));
- $action->element('link', array('rel' => 'openid.delegate',
- 'href' => $action->getTarget()->getUrl()));
- }
- if ($action instanceof SitestreamAction) {
- $action->element('meta', array('http-equiv' => 'X-XRDS-Location',
- 'content' => common_local_url('publicxrds')));
- }
- return true;
- }
-
- function onRedirectToLogin($action, $user)
- {
- if (common_config('site', 'openidonly') || (!empty($user) && User_openid::hasOpenID($user->id))) {
- common_redirect(common_local_url('openidlogin'), 303);
- }
- return true;
- }
-
- function onEndShowPageNotice($action)
- {
- $name = $action->trimmed('action');
- switch ($name)
- {
- case 'register':
- if (common_logged_in()) {
-
-
- $instr = _m('(Have an [OpenID](http://openid.net/)? ' .
- '[Add an OpenID to your account](%%action.openidsettings%%)!');
- } else {
-
-
- $instr = _m('(Have an [OpenID](http://openid.net/)? ' .
- 'Try our [OpenID registration]'.
- '(%%action.openidlogin%%)!)');
- }
- break;
- case 'login':
-
-
- $instr = _m('(Have an [OpenID](http://openid.net/)? ' .
- 'Try our [OpenID login]'.
- '(%%action.openidlogin%%)!)');
- break;
- default:
- return true;
- }
- $output = common_markup_to_html($instr);
- $action->raw($output);
- return true;
- }
-
- function onStartLoadDoc(&$title, &$output)
- {
- if ($title == 'openid') {
- $filename = INSTALLDIR.'/plugins/OpenID/doc-src/openid';
- $c = file_get_contents($filename);
- $output = common_markup_to_html($c);
- return false;
- }
- return true;
- }
-
- function onEndDocsMenu(&$items) {
- $items[] = array('doc',
- array('title' => 'openid'),
- _m('MENU', 'OpenID'),
- _('Logging in with OpenID'),
- 'nav_doc_openid');
- return true;
- }
-
- function onCheckSchema()
- {
- $schema = Schema::get();
- $schema->ensureTable('user_openid', User_openid::schemaDef());
- $schema->ensureTable('user_openid_trustroot', User_openid_trustroot::schemaDef());
- $schema->ensureTable('user_openid_prefs', User_openid_prefs::schemaDef());
-
- $schema->ensureTable('oid_associations',
- array(
- 'fields' => array(
- 'server_url' => array('type' => 'blob', 'not null' => true),
- 'handle' => array('type' => 'varchar', 'length' => 191, 'not null' => true, 'default' => ''),
- 'secret' => array('type' => 'blob'),
- 'issued' => array('type' => 'int'),
- 'lifetime' => array('type' => 'int'),
- 'assoc_type' => array('type' => 'varchar', 'length' => 64),
- ),
- 'primary key' => array(array('server_url', 191), 'handle'),
- ));
- $schema->ensureTable('oid_nonces',
- array(
- 'fields' => array(
- 'server_url' => array('type' => 'varchar', 'length' => 2047),
- 'timestamp' => array('type' => 'int'),
- 'salt' => array('type' => 'char', 'length' => 40),
- ),
- 'unique keys' => array(
- 'oid_nonces_server_url_timestamp_salt_key' => array(array('server_url', 191), 'timestamp', 'salt'),
- ),
- ));
- return true;
- }
-
- function onUserDeleteRelated($user, &$tables)
- {
- $tables[] = 'User_openid';
- $tables[] = 'User_openid_trustroot';
- return true;
- }
-
- function onEndAdminPanelNav($nav)
- {
- if (AdminPanelAction::canAdmin('openid')) {
- $action_name = $nav->action->trimmed('action');
- $nav->out->menuItem(
- common_local_url('openidadminpanel'),
-
- _m('MENU','OpenID'),
-
- _m('OpenID configuration.'),
- $action_name == 'openidadminpanel',
- 'nav_openid_admin_panel'
- );
- }
- return true;
- }
-
- function onEndAccountManagementControlDocument(&$amcd)
- {
- $amcd['auth-methods']['openid'] = array(
- 'connect' => array(
- 'method' => 'POST',
- 'path' => common_local_url('openidlogin'),
- 'params' => array(
- 'identity' => 'openid_url'
- )
- )
- );
- }
-
- function onPluginVersion(array &$versions)
- {
- $versions[] = array('name' => 'OpenID',
- 'version' => self::PLUGIN_VERSION,
- 'author' => 'Evan Prodromou, Craig Andrews',
- 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/OpenID',
- 'rawdescription' =>
-
- _m('Use <a href="http://openid.net/">OpenID</a> to login to the site.'));
- return true;
- }
- function onStartOAuthLoginForm($action, &$button)
- {
- if (common_config('site', 'openidonly')) {
-
- $this->showOAuthLoginForm($action);
-
- $button = _m('BUTTON', 'Continue');
- return false;
- } else {
-
-
- return true;
- }
- }
-
- protected function showOAuthLoginForm($action)
- {
- $action->elementStart('fieldset');
-
- $action->element('legend', null, _m('LEGEND','OpenID login'));
- $action->elementStart('ul', 'form_data');
- $action->elementStart('li');
- $provider = common_config('openid', 'trusted_provider');
- $appendUsername = common_config('openid', 'append_username');
- if ($provider) {
-
- $action->element('label', array(), _m('OpenID provider'));
- $action->element('span', array(), $provider);
- if ($appendUsername) {
- $action->element('input', array('id' => 'openid_username',
- 'name' => 'openid_username',
- 'style' => 'float: none'));
- }
- $action->element('p', 'form_guide',
-
- ($appendUsername ? _m('Enter your username.') . ' ' : '') .
-
- _m('You will be sent to the provider\'s site for authentication.'));
- $action->hidden('openid_url', $provider);
- } else {
-
- $action->input('openid_url', _m('OpenID URL'),
- '',
-
- _m('Your OpenID URL.'));
- }
- $action->elementEnd('li');
- $action->elementEnd('ul');
- $action->elementEnd('fieldset');
- }
-
- function onStartOAuthLoginCheck($action, &$user)
- {
- $provider = common_config('openid', 'trusted_provider');
- if ($provider) {
- $openid_url = $provider;
- if (common_config('openid', 'append_username')) {
- $openid_url .= $action->trimmed('openid_username');
- }
- } else {
- $openid_url = $action->trimmed('openid_url');
- }
- if ($openid_url) {
- require_once dirname(__FILE__) . '/openid.php';
- oid_assert_allowed($openid_url);
- $returnto = common_local_url(
- 'ApiOAuthAuthorize',
- array(),
- array(
- 'oauth_token' => $action->arg('oauth_token'),
- 'mode' => $action->arg('mode')
- )
- );
- common_set_returnto($returnto);
-
- $result = oid_authenticate($openid_url,
- 'finishopenidlogin');
- if (is_string($result)) {
- throw new ServerException($result);
- } else {
- exit(0);
- }
- }
- return true;
- }
-
- function onEndWebFingerProfileLinks(XML_XRD $xrd, Profile $target)
- {
- $xrd->links[] = new XML_XRD_Element_Link(
- 'http://specs.openid.net/auth/2.0/provider',
- $target->profileurl);
- return true;
- }
-
- function onOtherAccountProfiles($profile, &$links)
- {
- $prefs = User_openid_prefs::getKV('user_id', $profile->id);
- if (empty($prefs) || !$prefs->hide_profile_link) {
- $oid = new User_openid();
- $oid->user_id = $profile->id;
- if ($oid->find()) {
- while ($oid->fetch()) {
- $links[] = array('href' => $oid->display,
- 'text' => _('OpenID'),
- 'image' => $this->path("icons/openid-16x16.gif"));
- }
- }
- }
- return true;
- }
- }
|