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