-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfire.php
139 lines (131 loc) · 4.51 KB
/
fire.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
<?php
use Google\Cloud\Firestore\FirestoreClient;
use Google\Cloud\Firestore\FieldValue;
require_once "vendor/autoload.php";
putenv("GOOGLE_APPLICATION_CREDENTIALS=" . __DIR__ . '/fire-key.json');
$projectId = "mal-user2id";
function setup_client_create(string $projectId = null)
{
// Create the Cloud Firestore client
if (empty($projectId)) {
// The `projectId` parameter is optional and represents which project the
// client will act on behalf of. If not supplied, the client falls back to
// the default project inferred from the environment.
$db = new FirestoreClient();
printf('Created Cloud Firestore client with default project ID.' . PHP_EOL);
} else {
$db = new FirestoreClient([
'credentials' => json_decode(file_get_contents('fire-key.json'), true),
'projectId' => $projectId,
]);
//printf('Created Cloud Firestore client with project ID: %s' . PHP_EOL, $projectId);
}
}
setup_client_create($projectId);
function push($id, $username)
{
global $projectId;
$db = new FirestoreClient([
'credentials' => json_decode(file_get_contents('fire-key.json'), true),
'projectId' => $projectId,
]);
$docRef = $db->collection('users')->document($id);
$snapshot = $docRef->snapshot();
$lastdate = time();
$doc = [
"username" => array($username),
"first_date" => array(time()),
"last_date" => array(time())
];
if ($snapshot->exists()) {
$doc = $snapshot->data();
if (end($doc["username"]) != $username) {
$firstdate = time();
array_push($doc["username"], $username);
array_push($doc["first_date"], $firstdate);
array_push($doc["last_date"], $lastdate);
}
}
$sub = $db->collection('users')->document($id);
if (!$snapshot->exists()) {
$sub->set(
[
"username" => $username,
"last_date" => $lastdate,
"first_date" => $lastdate,
]
);
}
$subdata = $sub->snapshot()->data();
if (!$snapshot->exists() || ($snapshot->exists() && end($subdata["username"]) != $username)) {
$sub->update([
['path' => 'username', 'value' => FieldValue::arrayUnion([$username])]
]);
$sub->update([
['path' => 'first_date', 'value' => FieldValue::arrayUnion([$lastdate])]
]);
$sub->update([
['path' => 'last_date', 'value' => FieldValue::arrayUnion([$lastdate])]
]);
} else {
$sub->update([
['path' => 'last_date', 'value' => FieldValue::arrayRemove([end($doc["last_date"])])]
]);
$sub->update([
['path' => 'last_date', 'value' => FieldValue::arrayUnion([$lastdate])]
]);
}
$username = strtolower($username);
$snapshot = $db->collection('username_records')->document($username)->snapshot();
// $snapshot = $docRef;
if ($snapshot->exists()) {
$doc = $snapshot->data();
$sub = $db->collection('username_records')->document($username);
if(!in_array($id, $doc["id"])) {
$sub->update([
['path' => 'id', 'value' => FieldValue::arrayUnion([$id])]
]);
}
}
else{
$sub = $db->collection('username_records')->document($username);
$sub->set([
"id" => [$id],
]);
}
}
function pull($id)
{
global $projectId;
$db = new FirestoreClient([
'projectId' => $projectId,
'credentials' => json_decode(file_get_contents('fire-key.json'), true),
]);
$doc = $db->collection('users')->document($id)->snapshot()->data();
return $doc;
}
function print_table($doc)
{
echo "<div></div>";
echo "<div class=\"table\">
<p>Username</p>
<p>First Seen</p>
<p>Last Seen</p>";
for ($i = 0; $i < count($doc["username"]); $i++) {
echo "<p>" . $doc["username"][$i] . "</p> " .
"<p>" . date("Y-m-d", $doc["first_date"][$i]) . "</p> " .
"<p>" . date("Y-m-d", $doc["last_date"][$i]) . "</p> ";
}
echo "</div><br>
<p><i>All dates are expressed in ISO 8601 <b>(YYYY-MM-DD)</b> format.</i></p>";
}
function dig_records($username){
global $projectId;
$db = new FirestoreClient([
'projectId' => $projectId,
'credentials' => json_decode(file_get_contents('fire-key.json'), true),
]);
$username = strtolower($username);
$doc = $db->collection('username_records')->document($username)->snapshot()->data();
return $doc["id"];
}