3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
22 require_once(INSTALLDIR.'/plugins/OpenID/User_openid.php');
24 require_once('Auth/OpenID.php');
25 require_once('Auth/OpenID/Consumer.php');
26 require_once('Auth/OpenID/Server.php');
27 require_once('Auth/OpenID/SReg.php');
28 require_once('Auth/OpenID/MySQLStore.php');
30 # About one year cookie expiry
32 define('OPENID_COOKIE_EXPIRY', round(365.25 * 24 * 60 * 60));
33 define('OPENID_COOKIE_KEY', 'lastusedopenid');
39 # Can't be called statically
41 $conn = $user->getDatabaseConnection();
42 $store = new Auth_OpenID_MySQLStore($conn);
47 function oid_consumer()
50 $consumer = new Auth_OpenID_Consumer($store);
57 $server = new Auth_OpenID_Server($store, common_local_url('openidserver'));
61 function oid_clear_last()
66 function oid_set_last($openid_url)
68 common_set_cookie(OPENID_COOKIE_KEY,
70 time() + OPENID_COOKIE_EXPIRY);
73 function oid_get_last()
75 if (empty($_COOKIE[OPENID_COOKIE_KEY])) {
78 $openid_url = $_COOKIE[OPENID_COOKIE_KEY];
79 if ($openid_url && strlen($openid_url) > 0) {
86 function oid_link_user($id, $canonical, $display)
89 $oid = new User_openid();
91 $oid->canonical = $canonical;
92 $oid->display = $display;
93 $oid->created = DB_DataObject_Cast::dateTime();
95 if (!$oid->insert()) {
96 $err = PEAR::getStaticProperty('DB_DataObject','lastError');
97 common_debug('DB error ' . $err->code . ': ' . $err->message, __FILE__);
104 function oid_get_user($openid_url)
107 $oid = User_openid::staticGet('canonical', $openid_url);
109 $user = User::staticGet('id', $oid->user_id);
114 function oid_check_immediate($openid_url, $backto=null)
117 $action = $_REQUEST['action'];
118 $args = common_copy_args($_GET);
119 unset($args['action']);
120 $backto = common_local_url($action, $args);
122 common_debug('going back to "' . $backto . '"', __FILE__);
124 common_ensure_session();
126 $_SESSION['openid_immediate_backto'] = $backto;
127 common_debug('passed-in variable is "' . $backto . '"', __FILE__);
128 common_debug('session variable is "' . $_SESSION['openid_immediate_backto'] . '"', __FILE__);
130 oid_authenticate($openid_url,
135 function oid_authenticate($openid_url, $returnto, $immediate=false)
138 $consumer = oid_consumer();
141 common_server_error(_m('Cannot instantiate OpenID consumer object.'));
145 common_ensure_session();
147 $auth_request = $consumer->begin($openid_url);
149 // Handle failure status return values.
150 if (!$auth_request) {
151 return _m('Not a valid OpenID.');
152 } else if (Auth_OpenID::isFailure($auth_request)) {
153 return sprintf(_m('OpenID failure: %s'), $auth_request->message);
156 $sreg_request = Auth_OpenID_SRegRequest::build(// Required
168 $auth_request->addExtension($sreg_request);
171 $trust_root = common_root_url(true);
172 $process_url = common_local_url($returnto);
174 if ($auth_request->shouldSendRedirect()) {
175 $redirect_url = $auth_request->redirectURL($trust_root,
178 if (!$redirect_url) {
179 } else if (Auth_OpenID::isFailure($redirect_url)) {
180 return sprintf(_m('Could not redirect to server: %s'), $redirect_url->message);
182 common_redirect($redirect_url, 303);
185 // Generate form markup and render it.
186 $form_id = 'openid_message';
187 $form_html = $auth_request->formMarkup($trust_root, $process_url,
188 $immediate, array('id' => $form_id));
190 # XXX: This is cheap, but things choke if we don't escape ampersands
191 # in the HTML attributes
193 $form_html = preg_replace('/&/', '&', $form_html);
195 // Display an error if the form markup couldn't be generated;
196 // otherwise, render the HTML.
197 if (Auth_OpenID::isFailure($form_html)) {
198 common_server_error(sprintf(_m('Could not create OpenID form: %s'), $form_html->message));
200 $action = new AutosubmitAction(); // see below
201 $action->form_html = $form_html;
202 $action->form_id = $form_id;
203 $action->prepare(array('action' => 'autosubmit'));
204 $action->handle(array('action' => 'autosubmit'));
209 # Half-assed attempt at a module-private function
211 function _oid_print_instructions()
213 common_element('div', 'instructions',
214 _m('This form should automatically submit itself. '.
215 'If not, click the submit button to go to your '.
216 'OpenID provider.'));
219 # update a user from sreg parameters
221 function oid_update_user(&$user, &$sreg)
224 $profile = $user->getProfile();
226 $orig_profile = clone($profile);
228 if ($sreg['fullname'] && strlen($sreg['fullname']) <= 255) {
229 $profile->fullname = $sreg['fullname'];
232 if ($sreg['country']) {
233 if ($sreg['postcode']) {
234 # XXX: use postcode to get city and region
235 # XXX: also, store postcode somewhere -- it's valuable!
236 $profile->location = $sreg['postcode'] . ', ' . $sreg['country'];
238 $profile->location = $sreg['country'];
242 # XXX save language if it's passed
243 # XXX save timezone if it's passed
245 if (!$profile->update($orig_profile)) {
246 common_server_error(_m('Error saving the profile.'));
250 $orig_user = clone($user);
252 if ($sreg['email'] && Validate::email($sreg['email'], common_config('email', 'check_domain'))) {
253 $user->email = $sreg['email'];
256 if (!$user->update($orig_user)) {
257 common_server_error(_m('Error saving the user.'));
264 class AutosubmitAction extends Action
266 var $form_html = null;
269 function handle($args)
271 parent::handle($args);
277 return _m('OpenID Auto-Submit');
280 function showContent()
282 $this->raw($this->form_html);
285 function showScripts()
287 parent::showScripts();
288 $this->element('script', null,
289 '$(document).ready(function() { ' .
290 ' $(\'#'. $this->form_id .'\').submit(); '.