-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredir.php
More file actions
273 lines (265 loc) · 9.11 KB
/
Copy pathredir.php
File metadata and controls
273 lines (265 loc) · 9.11 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
//
// Copyright (C) 2015-16 - Bugzilla @ Twinoid - This program is free software
// distributed under the GNU Affero General Public License, verion 3 or later.
// No warranty. See the files LICENSE and/or README.md for details.
//
include_once('include/session.php');
// Contrary to the other files, this one does not include header.php
// here because it only redirects to another page according to the
// result of the Twinoid authentication.
// Initialize $_SESSION['token'] and $_SESSION['token_refresh'].
function do_twinoid_auth($code) {
global $error_msg;
if (! isset($code)) {
$error_msg = 'bug!';
return false;
}
$json = do_post_json('https://twinoid.com/oauth/token',
array(
'client_id' => APP_TWINOID_ID,
'client_secret' => APP_SECRET_KEY,
'redirect_uri' => APP_REDIRECT_URI,
'code' => $code,
'grant_type' => 'authorization_code'
)
);
if (is_string($json)) {
$error_msg = localized_msg([
"en" => "Error connecting to the Twinoid server:<br /><em>$json</em>",
"fr" => "Erreur de connexion au serveur Twinoid :<br /><em>$json</em>"
]);
return false;
}
if (isset($json->access_token)) {
$_SESSION['token'] = $json->access_token;
if (isset($json->expires_in)) {
$_SESSION['token_refresh'] = time() + $json->expires_in - 60;
} else {
$_SESSION['token_refresh'] = time() + 300;
}
unset($error_msg);
return true;
} else if (isset($json->error)) {
$error_msg = localized_msg([
"en" => "Authentication error:<br /><em>" . $json->error . "</em>.",
"fr" => "Échec lors de l'identification :<br /><em>" . $json->error
. "</em>."
]);
return false;
} else {
$error_msg = localized_msg([
"en" => "Cannot parse the response from the Twinoid server.",
"fr" => "Échec du décodage de la réponse du serveur Twinoid."
]);
return false;
}
}
// Initialize $_SESSION['uid'], $_SESSION['name'] and $_SESSION['avatar'].
function get_twinoid_user_info() {
global $error_msg;
if (! isset($_SESSION['token'])) {
return false;
}
$json = do_post_json('http://twinoid.com/graph/me',
array(
'access_token' => $_SESSION['token'],
'fields' => 'id,name,picture,locale,sites.fields(npoints)'
)
);
if (is_string($json)) {
$error_msg = localized_msg([
"en" => "Error connecting to the Twinoid server:<br /><em>$json</em>",
"fr" => "Erreur de connexion au serveur Twinoid :<br /><em>$json</em>"
]);
return false;
}
if (isset($json->error)) {
$error_msg = localized_msg([
"en" => "Error fetching Twinoid data:<br /><em>"
. $json->error . "</em>.",
"fr" => "Erreur de récupération des informations Twinoid :<br /><em>"
. $json->error . "</em>."
]);
return false;
}
if (! is_numeric($json->id)) {
$error_msg = localized_msg([
"en" => "Invalid Twinoid data: id=" . $json->id,
"fr" => "Informations Twinoid incorrectes : id=" . $json->id
]);
}
$_SESSION['uid'] = intval($json->id);
$_SESSION['name'] = $json->name;
if (! isset($_SESSION['locale'])) {
$_SESSION['locale'] = $json->locale;
}
//$_SESSION['sites'] = $json->sites;
$_SESSION['h_score'] = 0;
$_SESSION['t_score'] = 0;
$_SESSION['t_nulls'] = 0;
if (isset($json->sites)) {
foreach ($json->sites as $site) {
if (isset($site->npoints)) {
$_SESSION['t_score'] += $site->npoints;
if ($site->npoints > $_SESSION['h_score']) {
$_SESSION['h_score'] = $site->npoints;
}
} else {
$_SESSION['t_nulls']++;
}
}
}
if (isset($json->picture) && isset($json->picture->url)) {
$_SESSION['avatar'] = $json->picture->url;
}
return true;
}
// Checks if this user can be automatically upgraded to another role
function check_role_upgrade($role) {
if (($role == ROLE_NEWBIE) && ($_SESSION['t_score'] > 100)) {
$role = ROLE_NORMAL;
}
if (($role == ROLE_NORMAL) && ($_SESSION['t_score'] > 1000)) {
$role = ROLE_TRUSTED;
}
return $role;
}
// Initializes $_SESSION['role'] and updates the database.
//
// The following elements must already be set in the $_SESSION[]
// before calling this function: uid, avatar and name.
// Other values will be read from the database: locale, fr_duid,
// en_duid, es_duid and de_duid.
function get_db_user_info() {
global $error_msg;
if (! isset($_SESSION['uid']) || ! is_int($_SESSION['uid'])) {
return false;
}
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if ($mysqli->connect_error) {
$error_msg = localized_msg([
"en" => "Error connecting to the database:<br /><em>"
. $mysqli->error . "</em>",
"fr" => "Erreur de connexion à la base de données: :<br /><em>"
. $mysqli->error . "</em>"
]);
return false;
}
if (! $mysqli->set_charset('utf8')) {
$error_msg = localized_msg([
"en" => "Error switching to UTF-8:<br /><em>"
. $mysqli->error . "</em>",
"fr" => "Erreur pour passer en UTF-8 :<br /><em>"
. $mysqli->error . "</em>"
]);
return false;
}
$sql_uid = db_quote_int($_SESSION['uid']);
$result = $mysqli->query("SELECT * FROM users WHERE uid=$sql_uid");
if ($row = $result->fetch_assoc()) {
// The user is already known.
$_SESSION['locale'] = $row['locale'];
$_SESSION['role'] = check_role_upgrade($row['role']);
if ($row['fr_duid']) {
$_SESSION['fr_duid'] = intval($row['fr_duid']);
}
if ($row['en_duid']) {
$_SESSION['en_duid'] = intval($row['en_duid']);
}
if ($row['es_duid']) {
$_SESSION['es_duid'] = intval($row['es_duid']);
}
if ($row['de_duid']) {
$_SESSION['de_duid'] = intval($row['de_duid']);
}
// Update the database (at least atime, others if necessary).
if (($_SESSION['name'] == $row['name'])
|| ($_SESSION['avatar'] == $row['avatar'])
|| ($_SESSION['role'] == $row['role'])) {
$mysqli->query("UPDATE users SET atime=NOW() WHERE uid=$sql_uid");
} else {
$sql_avatar = db_quote_str($mysqli, $_SESSION['avatar']);
$sql_name = db_quote_str($mysqli, $_SESSION['name'], "'???'");
$sql_role = db_quote_int($_SESSION['role']);
$mysqli->query("UPDATE users SET name=$sql_name, avatar=$sql_avatar, role=$sql_role, mtime=NOW(), atime=NOW() WHERE uid=$sql_uid");
}
} else {
// This is a new user. Create a new entry for this user.
$sql_avatar = db_quote_str($mysqli, $_SESSION['avatar']);
$sql_name = db_quote_str($mysqli, $_SESSION['name'], "'???'");
$_SESSION['role'] = check_role_upgrade(ROLE_NEWBIE);
$sql_role = db_quote_int($_SESSION['role']);
$sql_locale = db_quote_str($mysqli, $_SESSION['locale'], "'en'");
$mysqli->query("INSERT INTO users (uid, name, avatar, locale, role, ctime, mtime, atime) VALUES ($sql_uid, $sql_name, $sql_avatar, $sql_locale, $sql_role, NOW(), NOW(), NOW())");
}
$result->free();
$mysqli->close();
return true;
}
// Documentation : http://twinoid.com/developers/doc
if (isset($_GET['state'])) {
// Decode the redirection link that has been encoded by
// twin_auth_href() (in session.php)
$redir_link = str_replace(array("^-", "^=", "^+"),
array(";", "&", "^"),
$_GET['state']);
} else {
$redir_link = "";
}
if (isset($_SERVER["HTTP_HOST"])) {
$redir_link = $_SERVER["HTTP_HOST"] . "/" . $redir_link;
} else {
$redir_link = $_SERVER["SERVER_NAME"] . "/" . $redir_link;
}
if (strpos($redir_link, "://") === false) {
$redir_link = "http://" . $redir_link;
}
if (isset($_GET['code'])) {
$_SESSION = array();
if (do_twinoid_auth($_GET['code'])) {
if (get_twinoid_user_info()) {
if (get_db_user_info()) {
header("Location: " . $redir_link);
exit();
//$info_msg = "Location: " . $redir_link;
}
}
}
} elseif (isset($_GET['error'])) {
$_SESSION = array();
$error_msg = localized_msg([
"en" => "Error during Twinoid redirection:<br /><em>"
. $_GET['error'] . "</em>",
"fr" => "Erreur de redirection Twinoid :<br /><em>"
. $_GET['error'] . "</em>"
]);
} else {
$error_msg = localized_msg([
"en" => "Incorrect call. Missing parameters.",
"fr" => "Appel incorrect. Paramètres manquants."
]);
}
// If we reach this point, something went wrong...
$page_title = localized_msg(["en" => "Error", "fr" => "Erreur"]);
include_once('include/header.php');
//echo '<h2>Test</h2>';
//echo "<p>Debug:</p><pre>$XXX</pre>";
//echo "<p>Get :</p><pre>";
//print_r ($_GET);
//echo "</pre>\n";
//echo "<p>Post :</p><pre>";
//print_r ($_POST);
//echo "</pre>\n";
//echo "<p>Session :</p><pre>";
//print_r ($_SESSION);
//echo "</pre>\n";
//echo "<p>Server :</p><pre>";
//print_r ($_SERVER);
//echo "</pre>\n";
echo localized_msg([
"en" => "<p class=\"error_box\">The connection to the Twinoid server has failed. You are not authenticated and you will not be able to use some features of this site.</p>",
"fr" => "<p class=\"error_box\">La connexion au serveur Twinoid a échoué. Vous n'êtes pas identifié et ne pourrez pas profiter de certaines fonctions de ce site.</p>"
]);
include_once('include/footer.php');
?>