-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcore_success.php
188 lines (160 loc) · 6.15 KB
/
core_success.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
use Aura\Session\Session;
use Brave\CoreConnector\Bootstrap;
use Brave\CoreConnector\Helper;
use Brave\NeucoreApi\Api\ApplicationGroupsApi;
use Eve\Sso\AuthenticationProvider;
use Psr\Container\ContainerExceptionInterface;
require 'vendor/autoload.php';
const ROOT_DIR = __DIR__;
$bootstrap = new Bootstrap();
try {
$settings = $bootstrap->getContainer()->get('settings');
} catch (ContainerExceptionInterface $e) {
error_log($e->getMessage());
die('Error 500.');
}
/** @var Session $session */
try {
$session = $bootstrap->getContainer()->get(Session::class);
} catch (ContainerExceptionInterface $e) {
error_log($e->getMessage());
die('Error 500.');
}
$sessionState = $session->getSegment('Bravecollective_Neucore')->get('sso_state');
$helper = new Helper($settings['ESI_DOMAIN']);
if (!isset($_GET['code']) || !isset($_GET['state']) || empty($sessionState)) {
echo 'Invalid SSO state, <a href="/start?do=login">please try again</a>.';
exit;
}
$code = $_GET['code'];
$state = $_GET['state'];
/** @var AuthenticationProvider $authenticationProvider */
try {
$authenticationProvider = $bootstrap->getContainer()->get(AuthenticationProvider::class);
} catch (ContainerExceptionInterface $e) {
error_log($e->getMessage());
die('Error 500.');
}
try {
$eveAuthentication = $authenticationProvider->validateAuthenticationV2($state, $sessionState, $code);
} catch(UnexpectedValueException $uve) {
echo $uve->getMessage(), '<br>',
'<a href="/start?do=login">Please try again</a>.';
exit;
}
$session->getSegment('Bravecollective_Neucore')->set('eveAuth', $eveAuthentication);
/** @var ApplicationGroupsApi $groupApi */
try {
$groupApi = $bootstrap->getContainer()->get(ApplicationGroupsApi::class);
} catch (ContainerExceptionInterface $e) {
error_log($e->getMessage());
die('Error 500.');
}
// -----------------------------------------------
$token = (string)$eveAuthentication->getToken();
$charid = $eveAuthentication->getCharacterId();
$charname = $eveAuthentication->getCharacterName();
$username = preg_replace("/[^A-Za-z0-9]/", '_', strtolower($charname));
//$corpid = $result->corporation->id;
//$corpname = $result->corporation->name;
//$allianceid = $result->alliance->id;
//$alliancename = $result->alliance->name;
$corpid = 0;
$corpname = '';
$allianceid = 0;
$alliancename = '';
$tags = $helper->getCoreGroups($groupApi, $eveAuthentication);
if (count($tags) === 0) {
echo '<strong>No groups found for this character or corporation.</strong><br><br>',
'Please register at <a href="'.$settings['CORE_URL'].'">BRAVE Core</a>. ',
'If the member group is listed on the right, try again here.<br><br>' .
'<a href="/start?do=login">Back to Login</a>';
exit;
}
// -----------------------------------------------
/** @var $db PDO */
try {
$db = $bootstrap->getContainer()->get(PDO::class);
} catch (ContainerExceptionInterface $e) {
error_log($e->getMessage());
die('Error 500.');
}
// -----------------------------------------------
$groups = array('user');
$groups = $helper->addGroup($db, $groups, 'charid_' . $charid);
//$groups = addGroup($db, $groups, 'corpid_' . $corpid);
//$groups = addGroup($db, $groups, 'allianceid_' . $allianceid);
foreach ($tags as $tkey => $tvalue) {
$groups = $helper->addGroup($db, $groups, 'tag_' . $tvalue);
}
$groups = array_unique($groups);
// -----------------------------------------------
$banned = $helper->addBan($db, false, 'charid_' . $charid);
//$banned = addBan($db, $banned, 'corpid_' . $corpid);
//$banned = addBan($db, $banned, 'allianceid_' . $allianceid);
foreach ($tags as $tkey => $tvalue) {
$banned = $helper->addBan($db, $banned, 'tag_' . $tvalue);
}
if ($banned) {
$groups = array('user');
}
// -----------------------------------------------
$stm = $db->prepare('SELECT charid FROM user WHERE charid = :charid;');
$stm->bindValue(':charid', $charid);
if (!$stm->execute()) {
error_log('user query failed');
echo 'An error occurred, <a href="/start?do=login">please try again</a>.';
exit;
}
if ($stm->fetch()) {
$stm = $db->prepare(
'UPDATE user SET
username = :username, groups = :groups, charname = :charname, corpid = :corpid,
corpname = :corpname, allianceid = :allianceid, alliancename = :alliancename,
authtoken = :authtoken, authlast = :now
WHERE charid = :charid;'
);
} else {
$stm = $db->prepare(
'INSERT INTO user
(username, groups, charid, charname, corpid, corpname, allianceid, alliancename,
authtoken, authcreated, authlast)
VALUES (:username, :groups, :charid, :charname, :corpid, :corpname, :allianceid, :alliancename,
:authtoken, :now, :now);'
);
}
$stm->bindValue(':username', $username);
$stm->bindValue(':groups', implode(',', $groups));
$stm->bindValue(':charid', $charid, PDO::PARAM_INT);
$stm->bindValue(':charname', $charname);
$stm->bindValue(':corpid', $corpid, PDO::PARAM_INT);
$stm->bindValue(':corpname', $corpname);
$stm->bindValue(':allianceid', $allianceid, PDO::PARAM_INT);
$stm->bindValue(':alliancename', $alliancename);
$stm->bindValue(':authtoken', $token);
$stm->bindValue(':now', time(), PDO::PARAM_INT);
if (!$stm->execute()) {
error_log('user insert or update failed');
echo 'An error occurred, <a href="/start?do=login">please try again</a>.';
exit;
}
$stm = $db->prepare('DELETE from session where sessionid = :sessionid;');
$stm->bindValue(':sessionid', $session->getId());
if (!$stm->execute()) {
error_log('session cleanup failed');
echo 'An error occurred, <a href="/start?do=login">please try again</a>.';
exit;
}
$stm = $db->prepare('INSERT INTO session (sessionid, charid, created) VALUES (:sessionid, :charid, :created)');
$stm->bindValue(':sessionid', $session->getId());
$stm->bindValue(':charid', $charid);
$stm->bindValue(':created', time());
if (!$stm->execute()) {
error_log('session insert failed');
echo 'An error occurred, <a href="/start?do=login">please try again</a>.';
exit;
}
// -----------------------------------------------
$cb = $session->getSegment('Bravecollective_Neucore')->get('cb');
header("Location: " . '/' . $cb);