-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_tokens.php
More file actions
78 lines (60 loc) · 1.67 KB
/
update_tokens.php
File metadata and controls
78 lines (60 loc) · 1.67 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
<?php
include_once ('classes/Database.php');
include_once ('user/local.php');
# database options
$db = Database::getDB();
$mysqli = $db->getCon();
# line endings
if (php_sapi_name() == 'cli') {
$line_end = "\n";
} else {
$line_end = '<br>' . "\n";
}
# add token field
$sql = 'ALTER TABLE `users`
CHANGE `registred` `registered` DATETIME NOT NULL,
ADD `token` VARCHAR(128) NOT NULL;';
$result = $mysqli->multi_query($sql);
if (!$result) {
echo 'could not add field to table users' . $line_end;
} else {
echo 'added field to table users' . $line_end;
}
# adding tokens
$fields = array('id');
$res = $db->select('users', $fields);
if (count($res)) {
foreach ($res as $user) {
do {
$token = hash('sha256', microtime() + rand(0, 1000));
$conds = array('token = ?', 's', array($token));
$unique = $db->select('users', $fields, $conds);
} while (count($unique) > 0);
$sql = 'UPDATE
users
SET
token = ?
WHERE
id = ?';
$stmt = $mysqli->prepare($sql);
if (!$stmt) {
return $mysqli->error;
}
$stmt->bind_param('si', $token, $user['id']);
if (!$stmt->execute()) {
return $stmt->error;
}
$stmt->close();
}
}
# make tokens unique
$sql = 'ALTER TABLE `users`
ADD UNIQUE `token` (`token`(128));';
$result = $mysqli->multi_query($sql);
if (!$result) {
echo '<pre>'; print_r($mysqli); echo '</pre>';
echo 'could not make tokens unique' . $line_end;
} else {
echo 'made tokens unique' . $line_end;
}
?>