PHP_SMART_HOME_V3/update.php

71 lines
2.1 KiB
PHP
Raw Normal View History

2020-03-04 14:44:55 +00:00
<?PHP
2020-03-09 18:45:49 +00:00
/** Includes **/
include_once('./config.php');
2020-03-04 14:44:55 +00:00
2020-03-09 18:45:49 +00:00
//Autoloader
$files = scandir('./app/class/');
$files = array_diff($files, array(
'.',
'..',
'app',
'ChartJS.php',
'ChartJS_Line.php',
'ChartManager.php',
'DashboardManager.php',
'Partial.php',
'Form.php',
'Route.php',
'Template.php',
'Ajax.php',
));
foreach($files as $file) {
include './app/class/'. $file;
}
$logManager = new LogManager();
2020-03-04 14:44:55 +00:00
2020-03-11 11:22:44 +00:00
header('Content-type: text/plain; charset=utf8', true);
2020-03-04 14:44:55 +00:00
function sendFile($path)
{
2020-03-11 11:22:44 +00:00
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);
2020-03-04 14:44:55 +00:00
}
2020-03-09 18:45:49 +00:00
$localBinary = "./app/updater/" . str_replace(':', '', $_SERVER['HTTP_X_ESP8266_STA_MAC']) . ".bin";
2020-03-11 11:22:44 +00:00
$logManager->write("[Updater] url: " . $localBinary, LogRecordType::INFO);
$logManager->write("[Updater] version: " . $_SERVER['HTTP_X_ESP8266_SKETCH_MD5'], LogRecordType::INFO);
2020-03-09 18:45:49 +00:00
if (file_exists($localBinary)) {
2020-03-11 11:22:44 +00:00
$logManager->write("[Updater] version PHP: " . md5_file($localBinary), LogRecordType::INFO);
2020-03-09 18:45:49 +00:00
if ($_SERVER['HTTP_X_ESP8266_SKETCH_MD5'] != md5_file($localBinary)) {
2020-03-11 11:22:44 +00:00
sendFile($localBinary);
//notification
$notificationMng = new NotificationManager;
$notificationData = [
'title' => 'Info',
'body' => 'Someone device was just updated to new version',
'icon' => BASEDIR . '/app/templates/images/icon-192x192.png',
];
if ($notificationData != []) {
$subscribers = $notificationMng::getSubscription();
foreach ($subscribers as $key => $subscriber) {
$logManager->write("[NOTIFICATION] SENDING TO" . $subscriber['id'] . " ", LogRecordType::INFO);
$notificationMng::sendSimpleNotification(SERVERKEY, $subscriber['token'], $notificationData);
}
}
} else {
header($_SERVER["SERVER_PROTOCOL"].' 304 Not Modified', true, 304);
}
2020-03-09 18:45:49 +00:00
} else {
2020-03-11 11:22:44 +00:00
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
2020-03-09 18:45:49 +00:00
}
2020-03-11 11:22:44 +00:00
header($_SERVER["SERVER_PROTOCOL"].' 500 no version for ESP MAC', true, 500);
die();