[23808] | 1 | <?php |
---|
| 2 | define('PHPWG_ROOT_PATH', '../../'); |
---|
| 3 | include_once(PHPWG_ROOT_PATH.'include/common.inc.php'); |
---|
| 4 | |
---|
| 5 | global $hybridauth_conf; |
---|
| 6 | require_once(OAUTH_PATH . 'include/hybridauth/Hybrid/Auth.php'); |
---|
| 7 | |
---|
| 8 | $provider = @$_GET['provider']; |
---|
| 9 | |
---|
| 10 | try { |
---|
| 11 | // inputs |
---|
[26605] | 12 | if ($provider == 'OpenID' and !isset($_GET['openid_identifier'])) |
---|
[23808] | 13 | { |
---|
| 14 | throw new Exception('Invalid OpenID!', 1003); |
---|
| 15 | } |
---|
| 16 | |
---|
[26604] | 17 | // OpenID is always enabled |
---|
| 18 | $hybridauth_conf['providers']['OpenID']['enabled'] = true; |
---|
| 19 | |
---|
| 20 | if (!array_key_exists($provider, $hybridauth_conf['providers']) |
---|
| 21 | or !$hybridauth_conf['providers'][$provider]['enabled'] |
---|
| 22 | ) |
---|
| 23 | { |
---|
[23808] | 24 | throw new Exception('Invalid provider!', 1002); |
---|
| 25 | } |
---|
| 26 | |
---|
[26605] | 27 | if ($provider == 'Persona') |
---|
[23808] | 28 | { |
---|
[26605] | 29 | $response = persona_verify($_POST['assertion']); |
---|
[23808] | 30 | |
---|
[26605] | 31 | if ($response === false || $response['status'] != 'okay') |
---|
| 32 | { |
---|
| 33 | header('HTTP/1.1 503 Service Unavailable'); |
---|
| 34 | exit; |
---|
| 35 | } |
---|
| 36 | else |
---|
| 37 | { |
---|
| 38 | $oauth_id = array($provider, $response['email']); |
---|
| 39 | } |
---|
| 40 | } |
---|
| 41 | else |
---|
| 42 | { |
---|
| 43 | $hybridauth = new Hybrid_Auth($hybridauth_conf); |
---|
[23808] | 44 | |
---|
[26605] | 45 | // connected |
---|
| 46 | if ($hybridauth->isConnectedWith($provider)) |
---|
| 47 | { |
---|
| 48 | $adapter = $hybridauth->getAdapter($provider); |
---|
| 49 | $remote_user = $adapter->getUserProfile(); |
---|
| 50 | |
---|
| 51 | $oauth_id = array($provider, $remote_user->identifier); |
---|
| 52 | } |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | if (!empty($oauth_id)) |
---|
| 56 | { |
---|
[23808] | 57 | // check is already registered |
---|
| 58 | $query = ' |
---|
[26605] | 59 | SELECT id FROM ' . USERS_TABLE . ' |
---|
| 60 | WHERE oauth_id = "' . implode('---', $oauth_id) . '" |
---|
[23808] | 61 | ;'; |
---|
| 62 | $result = pwg_query($query); |
---|
| 63 | // registered : log_user and redirect |
---|
| 64 | if (pwg_db_num_rows($result)) |
---|
| 65 | { |
---|
| 66 | list($user_id) = pwg_db_fetch_row($result); |
---|
| 67 | log_user($user_id, false); |
---|
| 68 | |
---|
[26605] | 69 | $redirect_to = 'default'; |
---|
[23808] | 70 | } |
---|
| 71 | // not registered : redirect to register page |
---|
| 72 | else |
---|
| 73 | { |
---|
| 74 | if ($conf['allow_user_registration']) |
---|
| 75 | { |
---|
[26605] | 76 | pwg_set_session_var('oauth_new_user', $oauth_id); |
---|
| 77 | $redirect_to = 'register'; |
---|
[23808] | 78 | } |
---|
| 79 | else |
---|
| 80 | { |
---|
| 81 | $_SESSION['page_errors'][] = l10n('Sorry, new registrations are blocked on this gallery.'); |
---|
[26605] | 82 | if (isset($adapter)) $adapter->logout(); |
---|
| 83 | $redirect_to = 'identification'; |
---|
[23808] | 84 | } |
---|
| 85 | } |
---|
[26605] | 86 | |
---|
| 87 | if ($provider == 'Persona') |
---|
| 88 | { |
---|
| 89 | echo json_encode(compact('redirect_to')); |
---|
| 90 | header('HTTP/1.1 200 OK'); |
---|
| 91 | exit; |
---|
| 92 | } |
---|
| 93 | else |
---|
| 94 | { |
---|
| 95 | $template->assign('REDIRECT_TO', $redirect_to); |
---|
| 96 | } |
---|
[23808] | 97 | } |
---|
| 98 | // init connect |
---|
| 99 | else if (isset($_GET['init_auth'])) |
---|
| 100 | { |
---|
| 101 | $params = array(); |
---|
| 102 | if ($provider == 'OpenID') |
---|
| 103 | { |
---|
| 104 | $params['openid_identifier'] = $_GET['openid_identifier']; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | // try to authenticate |
---|
| 108 | $adapter = $hybridauth->authenticate($provider, $params); |
---|
| 109 | } |
---|
| 110 | // display loader |
---|
| 111 | else |
---|
| 112 | { |
---|
| 113 | $template->assign('LOADING', '&openid_identifier='.@$_GET['openid_identifier'].'&init_auth=1'); |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | /* |
---|
| 117 | library errors : |
---|
| 118 | 0 : Unspecified error |
---|
| 119 | 1 : Hybriauth configuration error |
---|
| 120 | 2 : Provider not properly configured |
---|
| 121 | 3 : Unknown or disabled provider |
---|
| 122 | 4 : Missing provider application credentials |
---|
| 123 | 5 : Authentication aborded |
---|
| 124 | 6 : User profile request failed |
---|
[26605] | 125 | 404 : User not found |
---|
[23808] | 126 | other errors : |
---|
[26605] | 127 | 503 : Persona error |
---|
[23808] | 128 | 1002 : Invalid provider |
---|
| 129 | 1003 : Missing openid_identifier |
---|
| 130 | */ |
---|
[26605] | 131 | catch (Exception $e) |
---|
| 132 | { |
---|
| 133 | switch ($e->getCode()) |
---|
| 134 | { |
---|
[23808] | 135 | case 5: |
---|
| 136 | $template->assign('ERROR', l10n('Authentication canceled')); break; |
---|
| 137 | case 404: |
---|
| 138 | $template->assign('ERROR', l10n('User not found')); break; |
---|
| 139 | default: |
---|
[26556] | 140 | $template->assign('ERROR', l10n('An error occured, please contact the gallery owner. <i>Error code : %s</i>', '<span title="'.$e->getMessage().'">'.$e->getCode().'</span>')); |
---|
[23808] | 141 | } |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | |
---|
| 145 | $template->assign(array( |
---|
| 146 | 'GALLERY_TITLE' => $conf['gallery_title'], |
---|
| 147 | 'CONTENT_ENCODING' => get_pwg_charset(), |
---|
| 148 | 'U_HOME' => get_gallery_home_url(), |
---|
| 149 | |
---|
| 150 | 'OAUTH_PATH' => OAUTH_PATH, |
---|
| 151 | 'PROVIDER' => $provider, |
---|
| 152 | 'SELF_URL' => OAUTH_PATH . 'auth.php?provider='.$provider, |
---|
| 153 | )); |
---|
| 154 | |
---|
| 155 | $template->set_filename('index', realpath(OAUTH_PATH . 'template/auth.tpl')); |
---|
| 156 | $template->pparse('index'); |
---|