@@ -592,34 +592,42 @@ With this information the script now can check if an update is needed. It is als
592592
593593 <?PHP
594594
595- header('Content-type: text/plain; charset=utf8', true);
596-
597595 function check_header($name, $value = false) {
598- if(!isset($_SERVER[$name])) {
596+ global $headers;
597+ if (!isset($headers[$name])) {
599598 return false;
600599 }
601- if($value && $_SERVER [$name] != $value) {
600+ if ($value && $headers [$name] != $value) {
602601 return false;
603602 }
604603 return true;
605604 }
606-
607- function sendFile($path) {
605+
606+ function sendFile($path, $version ) {
608607 header($_SERVER["SERVER_PROTOCOL"].' 200 OK', true, 200);
609608 header('Content-Type: application/octet-stream', true);
610609 header('Content-Disposition: attachment; filename='.basename($path));
611610 header('Content-Length: '.filesize($path), true);
612611 header('x-MD5: '.md5_file($path), true);
612+ header('x-version: '.$version, true);
613613 readfile($path);
614614 }
615-
616- if(!check_header('User-Agent', 'ESP8266-http-Update')) {
615+
616+
617+ $headers = getallheaders();
618+
619+ header('Content-type: text/plain; charset=utf8', true);
620+
621+ //if (!check_header('HTTP_USER_AGENT', 'ESP8266-http-Update')) {
622+ if (!check_header('User-Agent', 'ESP8266-http-Update')) {
617623 header($_SERVER["SERVER_PROTOCOL"].' 403 Forbidden', true, 403);
618- echo "only for ESP8266 updater!\n";
624+ echo "Only for ESP8266 updater!\n";
625+ echo "User-Agent: ".$headers['User-Agent']." != ESP8266-http-Update\n";
619626 exit();
620627 }
621-
622- if(
628+
629+ if (
630+ !check_header('x-ESP8266-mode') ||
623631 !check_header('x-ESP8266-STA-MAC') ||
624632 !check_header('x-ESP8266-AP-MAC') ||
625633 !check_header('x-ESP8266-free-space') ||
@@ -629,32 +637,54 @@ With this information the script now can check if an update is needed. It is als
629637 !check_header('x-ESP8266-sdk-version')
630638 ) {
631639 header($_SERVER["SERVER_PROTOCOL"].' 403 Forbidden', true, 403);
632- echo "only for ESP8266 updater! (header)\n";
640+ echo "Only for ESP8266 updater! (header missing )\n";
633641 exit();
634642 }
635-
636- $db = array(
637- "18:FE:AA:AA:AA:AA" => "DOOR-7-g14f53a19",
638- "18:FE:AA:AA:AA:BB" => "TEMP-1.0.0"
639- );
640-
641- if(!isset($db[$_SERVER['x-ESP8266-STA-MAC']])) {
642- header($_SERVER["SERVER_PROTOCOL"].' 500 ESP MAC not configured for updates', true, 500);
643+
644+ $db_string = '{
645+ "18:FE:AA:AA:AA:AA": {"file": "DOOR-7-g14f53a19.bin", "version": 1},
646+ "18:FE:AA:AA:AA:BB": {"file": "TEMP-1.0.0".bin", "version": 1}}';
647+ // $db_string = file_get_contents("arduino-db.json");
648+ $db = json_decode($db_string, true);
649+ $mode = $headers['x-ESP8266-mode'];
650+ $mac = $headers['x-ESP8266-STA-MAC'];
651+
652+ if (!isset($db[$mac])) {
653+ header($_SERVER["SERVER_PROTOCOL"].' 404 ESP MAC not configured for updates', true, 404);
654+ echo "MAC ".$mac." not configured for updates\n";
655+ exit();
643656 }
644-
645- $localBinary = "./bin/".$db[$_SERVER['x-ESP8266-STA-MAC']].".bin";
646-
647- // Check if version has been set and does not match, if not, check if
648- // MD5 hash between local binary and ESP8266 binary do not match if not.
649- // then no update has been found.
650- if((!check_header('x-ESP8266-sdk-version') && $db[$_SERVER['x-ESP8266-STA-MAC']] != $_SERVER['x-ESP8266-version'])
651- || $_SERVER["x-ESP8266-sketch-md5"] != md5_file($localBinary)) {
652- sendFile($localBinary);
657+
658+ $localBinary = $db[$mac]['file'];
659+ $localVersion = $db[$mac]['version'];
660+
661+ if (!is_readable($localBinary)) {
662+ header($_SERVER["SERVER_PROTOCOL"].' 404 File not found', true, 404);
663+ echo "File ".$localBinary." not found\n";
664+ exit();
665+ }
666+
667+ if ($mode == 'sketch') {
668+ // Check if version has been set and does not match, if not, check if
669+ // MD5 hash between local binary and ESP8266 binary do not match if not.
670+ // then no update has been found.
671+ if ((check_header('x-ESP8266-version') && $headers['x-ESP8266-version'] != $localVersion)) {
672+ // || $headers["x-ESP8266-sketch-md5"] != md5_file($localBinary)) {
673+ sendFile($localBinary, $localVersion);
674+ } else {
675+ header($_SERVER["SERVER_PROTOCOL"].' 304 Not Modified', true, 304);
676+ echo "File ".$localBinary." not modified\n";
677+ }
678+ } else if ($mode == 'version') {
679+ header($_SERVER["SERVER_PROTOCOL"].' 200 OK', true, 200);
680+ header('x-MD5: '.md5_file($localBinary), true);
681+ header('x-version: '.$localVersion, true);
653682 } else {
654- header($_SERVER["SERVER_PROTOCOL"].' 304 Not Modified', true, 304);
683+ header($_SERVER["SERVER_PROTOCOL"].' 404 Mode not supported', true, 404);
684+ echo "mode: ".$mode." not supported\n";
685+ exit();
655686 }
656-
657- header($_SERVER["SERVER_PROTOCOL"].' 500 no version for ESP MAC', true, 500);
687+ ?>
658688
659689 Stream Interface
660690----------------
0 commit comments