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