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 | if ( $provider == 'OpenID' and !isset($_GET['openid_identifier']) ) |
---|
12 | { |
---|
13 | throw new Exception('Invalid OpenID!'); |
---|
14 | } |
---|
15 | |
---|
16 | // inputs |
---|
17 | if ( |
---|
18 | !array_key_exists($provider, $hybridauth_conf['providers']) |
---|
19 | or !$hybridauth_conf['providers'][$provider]['enabled'] |
---|
20 | ) { |
---|
21 | throw new Exception('Hacking attempt!'); |
---|
22 | } |
---|
23 | |
---|
24 | |
---|
25 | |
---|
26 | $hybridauth = new Hybrid_Auth($hybridauth_conf); |
---|
27 | |
---|
28 | // connected |
---|
29 | if ($hybridauth->isConnectedWith($provider)) |
---|
30 | { |
---|
31 | $template->assign('AUTH_DONE', true); |
---|
32 | |
---|
33 | $adapter = $hybridauth->getAdapter($provider); |
---|
34 | $remote_user = $adapter->getUserProfile(); |
---|
35 | |
---|
36 | $oauth_id = $provider.'---'.$remote_user->identifier; |
---|
37 | |
---|
38 | // check is already registered |
---|
39 | $query = ' |
---|
40 | SELECT id FROM '.USERS_TABLE.' |
---|
41 | WHERE oauth_id = "'.$oauth_id.'" |
---|
42 | ;'; |
---|
43 | $result = pwg_query($query); |
---|
44 | // registered : log_user and redirect |
---|
45 | if (pwg_db_num_rows($result)) |
---|
46 | { |
---|
47 | list($user_id) = pwg_db_fetch_row($result); |
---|
48 | log_user($user_id, false); |
---|
49 | |
---|
50 | $template->assign('REDIRECT_TO', 'default'); |
---|
51 | } |
---|
52 | // not registered : redirect to register page |
---|
53 | else |
---|
54 | { |
---|
55 | if ($conf['allow_user_registration']) |
---|
56 | { |
---|
57 | pwg_set_session_var('oauth_new_user', array($provider,$remote_user->identifier)); |
---|
58 | $template->assign('REDIRECT_TO', 'register'); |
---|
59 | } |
---|
60 | else |
---|
61 | { |
---|
62 | $_SESSION['page_errors'][] = l10n('Sorry, new registrations are blocked on this gallery.'); |
---|
63 | $adapter->logout(); |
---|
64 | $template->assign('REDIRECT_TO', 'identification'); |
---|
65 | } |
---|
66 | } |
---|
67 | } |
---|
68 | // init connect |
---|
69 | else if (isset($_GET['init_auth'])) |
---|
70 | { |
---|
71 | $params = array('hauth_return_to', get_absolute_root_url().OAUTH_PATH.'auth.php?provider='.$provider.'&auth_done=1'); |
---|
72 | if ($provider == 'OpenID') |
---|
73 | { |
---|
74 | $params['openid_identifier'] = $_GET['openid_identifier']; |
---|
75 | } |
---|
76 | |
---|
77 | // try to authenticate |
---|
78 | $adapter = $hybridauth->authenticate($provider, $params); |
---|
79 | } |
---|
80 | // display loader |
---|
81 | else |
---|
82 | { |
---|
83 | $template->assign('LOADING', '&openid_identifier='.@$_GET['openid_identifier'].'&init_auth=1'); |
---|
84 | } |
---|
85 | } |
---|
86 | catch( Exception $e ){ |
---|
87 | $template->assign('ERROR', $e->getMessage()); |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | $template->assign(array( |
---|
92 | 'GALLERY_TITLE' => $conf['gallery_title'], |
---|
93 | 'CONTENT_ENCODING' => get_pwg_charset(), |
---|
94 | 'U_HOME' => get_gallery_home_url(), |
---|
95 | |
---|
96 | 'OAUTH_PATH' => OAUTH_PATH, |
---|
97 | 'PROVIDER' => $provider, |
---|
98 | 'SELF_URL' => OAUTH_PATH.'auth.php?provider='.$provider, |
---|
99 | )); |
---|
100 | |
---|
101 | $template->set_filename('index', realpath(OAUTH_PATH.'template/auth.tpl')); |
---|
102 | $template->pparse('index'); |
---|
103 | ?> |
---|