-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcharging.php
More file actions
117 lines (99 loc) · 2.76 KB
/
Copy pathcharging.php
File metadata and controls
117 lines (99 loc) · 2.76 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
<?php
define('SECONDS_PER_DAY', 86400);
define('SECONDS_PER_HOUR', 3600);
define('SECONDS_PER_MINUTE', 60);
function getNiceDuration($durationInSeconds) {
$timePeriods = [
'days' => SECONDS_PER_DAY,
'hours' => SECONDS_PER_HOUR,
'minutes' => SECONDS_PER_MINUTE,
'seconds' => 1,
];
$duration = '';
foreach ($timePeriods as $unit => $value) {
if ($durationInSeconds >= $value) {
$num = floor($durationInSeconds / $value);
$durationInSeconds -= $num * $value;
$duration .= "$num $unit" . ($num > 1 ? 'n' : '') . ' ';
}
}
return trim($duration);
}
$type = $_GET['type'];
$percent = filter_input(INPUT_GET, 'percent', FILTER_VALIDATE_INT, [
'options' => [
'default' => 0,
'min_range' => 0,
'max_range' => 100
]
]);
if ($percent === false || $percent === null) {
die('Invalid percentage value.');
}
$webhookurl = "YOUR WEBHOOK";
$timestamp = date("c", strtotime("now"));
if ($type == "plug") {
$title = "⚡️ iPhone is being charged";
$color = hexdec("0ddd14");
$name = "Current battery level:";
$name2 = "Last charged:";
$value = getNiceDuration(time() - file_get_contents('cache.txt'));
} elseif ($type == "unplug") {
$title = "🔋 iPhone is no longer charging!";
$color = hexdec("ddb40d");
$name = "Current battery level:";
$name2 = "Charged for:";
$value = getNiceDuration(time() - file_get_contents('cache.txt'));
}
$json_data = json_encode([
"username" => "iPhone - Charging status",
"avatar_url" => "YOUR WEBHOOK AVATAR URL",
"tts" => false,
"embeds" => [
[
"title" => $title,
"type" => "rich",
"timestamp" => $timestamp,
"color" => $color,
"footer" => [
"text" => "GitHub.com/Niclassslua",
"icon_url" => "https://avatars.githubusercontent.com/u/78554432?v=4"
],
"fields" => [
[
"name" => $name,
"value" => $percent . "%",
"inline" => true
],
[
"name" => $name2,
"value" => $value,
"inline" => true
]
]
]
]
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
$ch = curl_init($webhookurl);
curl_setopt_array($ch, [
CURLOPT_HTTPHEADER => ['Content-type: application/json'],
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $json_data,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => true,
]);
$response = curl_exec($ch);
if ($response === false) {
$error = curl_error($ch);
curl_close($ch);
logError($error); //TODO: Implement a logError Function
die("An error occurred: $error");
}
curl_close($ch);
if (file_put_contents('cache.txt', time()) === false) {
logError("Error writing to the cache file.");
die("An error occurred while updating the cache file.");
}
?>