forked from aneisch/Sonoff-Wemo-Home-Assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.php
126 lines (108 loc) · 4.22 KB
/
update.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
<?php
// CONFIG
$logfile = "log.txt";
$latest = array("SWITCH" => 1.1, "TH16" => 1.1);
// Functions
function check_header($name, $value = false) {
if(!isset($_SERVER[$name])) {
return false;
}
if($value && $_SERVER[$name] != $value) {
return false;
}
return true;
}
function sendFile($path) {
header($_SERVER["SERVER_PROTOCOL"].' 200 OK', true, 200);
header('Content-Type: application/octet-stream', true);
header('Content-Disposition: attachment; filename='.basename($path));
header('Content-Length: '.filesize($path), true);
header('x-MD5: '.md5_file($path), true);
readfile($path);
}
// MAIN
// prepare log
$log = date("d.m.Y H:m:s") . " " . $_SERVER['REMOTE_ADDR'] . " ";
// debug log
//file_put_contents($logfile, print_r($_SERVER, TRUE), FILE_APPEND);
// set output to plain text, not html
header('Content-type: text/plain; charset=utf8', true);
// check if the user agent matches esp update library
if(!check_header('HTTP_USER_AGENT', 'ESP8266-http-Update')) {
header($_SERVER["SERVER_PROTOCOL"].' 403 Forbidden', true, 403);
echo "This service is only available for ESP8266 updater!\n\n";
echo "For more information visit: https://github.com/Rup0rt/Sonoff-Smart-Switch-Amazon-Alexa\n";
$log .= "Wrong user agent: " . $_SERVER['HTTP_USER_AGENT'] . "\n";
file_put_contents($logfile, $log, FILE_APPEND);
exit();
}
// check if all header data is available
if(
!check_header('HTTP_X_ESP8266_STA_MAC') ||
!check_header('HTTP_X_ESP8266_AP_MAC') ||
!check_header('HTTP_X_ESP8266_FREE_SPACE') ||
!check_header('HTTP_X_ESP8266_SKETCH_SIZE') ||
!check_header('HTTP_X_ESP8266_CHIP_SIZE') ||
!check_header('HTTP_X_ESP8266_SDK_VERSION') ||
!check_header('HTTP_X_ESP8266_VERSION')
) {
header($_SERVER["SERVER_PROTOCOL"].' 403 Forbidden', true, 403);
echo "Missing headers...\n\n";
echo "This service is only available for ESP8266 updater!\n\n";
echo "For more information visit: https://github.com/Rup0rt/Sonoff-Smart-Switch-Amazon-Alexa\n";
$log .= "Missing headers!\n";
file_put_contents($logfile, $log, FILE_APPEND);
exit();
}
// get data
$mac = $_SERVER['HTTP_X_ESP8266_STA_MAC'];
$ap = $_SERVER['HTTP_X_ESP8266_AP_MAC'];
$freespace = $_SERVER['HTTP_X_ESP8266_FREE_SPACE'];
$sketchsize = $_SERVER['HTTP_X_ESP8266_SKETCH_SIZE'];
$chipsize = $_SERVER['HTTP_X_ESP8266_CHIP_SIZE'];
$sdk = $_SERVER['HTTP_X_ESP8266_SDK_VERSION'];
$versionstr = $_SERVER['HTTP_X_ESP8266_VERSION'];
// split version string
list($firmname, $firmver) = explode("-", $versionstr);
// add log information
$log .= "Request by $mac running firmware $firmname version $firmver --> ";
// check firmware and version
if (array_key_exists($firmname, $latest)) {
// firmware name exists
// check version
$ver = $latest[$firmname];
$firmverf = floatval($firmver);
if ($firmverf == $ver) {
// already up to date
header($_SERVER["SERVER_PROTOCOL"].' 304 Not Modified', true, 304);
$log .= "Already up to date!\n";
file_put_contents($logfile, $log, FILE_APPEND);
exit();
} elseif ($firmverf < $ver) {
// update necessary
$log .= "Updating to latest version: $ver!\n";
file_put_contents($logfile, $log, FILE_APPEND);
$filename = "$firmname-$ver.bin";
sendFile($filename);
exit();
} else {
// version is newer than server available, huh?
header($_SERVER["SERVER_PROTOCOL"] . " 500 You version is newer than the available!.", true, 500);
echo "Firmware is newer than the available one: $ver\n\n";
echo "This service is only available for ESP8266 updater!\n\n";
echo "For more information visit: https://github.com/Rup0rt/Sonoff-Smart-Switch-Amazon-Alexa\n";
$log .= "Firmware is newer than the available one: $ver\n";
file_put_contents($logfile, $log, FILE_APPEND);
exit();
}
} else {
// report no update available
header($_SERVER["SERVER_PROTOCOL"] . " 500 No version for this firmware.", true, 500);
echo "Unknown firmware...\n\n";
echo "This service is only available for ESP8266 updater!\n\n";
echo "For more information visit: https://github.com/Rup0rt/Sonoff-Smart-Switch-Amazon-Alexa\n";
$log .= "Unknown firmware: $firmname\n";
file_put_contents($logfile, $log, FILE_APPEND);
exit();
}
?>