PHP_SMART_HOME_V3/update.php

92 lines
2.9 KiB
PHP
Raw Permalink 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;
}
2020-03-12 20:34:12 +00:00
//DB Conector
Db::connect (DBHOST, DBUSER, DBPASS, DBNAME);
2020-03-09 18:45:49 +00:00
$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
2020-03-11 11:23:49 +00:00
//Filtrování IP adress
if (DEBUGMOD != 1) {
if (!in_array($_SERVER['REMOTE_ADDR'], HOMEIP)) {
echo json_encode(array(
'state' => 'unsuccess',
'errorMSG' => "Using API from your IP insnt alowed!",
));
header($_SERVER["SERVER_PROTOCOL"]." 401 Unauthorized");
$logManager->write("[Updater] acces denied from " . $_SERVER['REMOTE_ADDR'], LogRecordType::WARNING);
exit();
}
}
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-13 13:45:12 +00:00
$macAddress = $_SERVER['HTTP_X_ESP8266_STA_MAC'];
$localBinary = "./app/updater/" . str_replace(':', '', $macAddress) . ".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-25 14:58:47 +00:00
$logManager->write("[Updater] version PHP: \n" . 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);
2020-03-16 14:03:43 +00:00
//get device data
$device = DeviceManager::getDeviceByMac($macAddress);
$deviceName = $device['name'];
$deviceId = $device['device_id'];
//logfile write
2020-03-25 14:58:47 +00:00
$logManager->write("[Device] device_ID " . $deviceId . " was just updated to new version", LogRecordType::WARNING);
$logManager->write("[Device] version hash: \n" . md5_file($localBinary), LogRecordType::INFO);
2020-03-11 11:22:44 +00:00
//notification
2020-03-12 20:34:12 +00:00
$notificationMng = new NotificationManager;
$notificationData = [
'title' => 'Info',
2020-03-13 13:45:12 +00:00
'body' => $deviceName.' was just updated to new version',
2020-03-12 20:34:12 +00:00
'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);
$answer = $notificationMng->sendSimpleNotification(SERVERKEY, $subscriber['token'], $notificationData);
2020-03-11 11:22:44 +00:00
}
}
2020-03-09 18:45:49 +00:00
} else {
2020-03-12 20:34:12 +00:00
header($_SERVER["SERVER_PROTOCOL"].' 304 Not Modified', true, 304);
2020-03-09 18:45:49 +00:00
}
2020-03-12 20:34:12 +00:00
} else {
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
}
die();