PHP_SMART_HOME_V3/app/controllers/homeController.php

88 lines
2.2 KiB
PHP
Raw Normal View History

2019-08-23 11:39:42 +00:00
<?php
if (isset($_POST) && !empty($_POST)){
if (isset($_POST['saveDevice']) && $_POST['saveDevice'] != "") {
$deviceId = $_POST['deviceId'];
$deviceName = $_POST['deviceName'];
$deviceIcon = $_POST['deviceIcon'];
2019-09-09 19:01:45 +00:00
$sleepTime = 0;
2020-03-11 13:45:42 +00:00
if (isset($_FILES['deviceFirmware']) && isset($_FILES['deviceFirmware']['tmp_name']) && $_FILES['deviceFirmware']['tmp_name'] != "") {
$file = $_FILES['deviceFirmware'];
2020-03-12 20:34:12 +00:00
$deviceMac = DeviceManager::getDeviceById($deviceId)['mac'];
$fileName = (isset ($deviceMac) && $deviceMac != "" ? str_replace(":", "", $deviceMac) . ".bin" : "");
if ($fileName != "" && file_exists("./app/updater/" . $fileName)) {
2020-03-11 13:45:42 +00:00
unlink("./app/updater/" . $fileName);
}
2020-03-11 14:30:11 +00:00
if ($fileName != "") {
copy($file['tmp_name'], "./app/updater/" . $fileName);
} else {
}
2020-03-11 13:45:42 +00:00
}
2019-09-09 19:01:45 +00:00
if (isset($_POST['sleepTime'])) {
$sleepTime = $_POST['sleepTime'];
}
2019-08-23 11:39:42 +00:00
//TODO: if device isnt on off
$permissionsInJson = json_encode([
(int) $_POST['permissionOwner'],
(int) $_POST['permissionOther'],
]);
$deviceOwnerUserId = $_POST['deviceOwnerUserId'];
2019-08-24 11:07:07 +00:00
$deviceOwnerRoomId = $_POST['deviceOwnerId'];
2019-08-23 11:39:42 +00:00
try {
$args = array(
'owner' => $deviceOwnerUserId,
'name' => $deviceName,
'icon' => $deviceIcon,
'permission' => $permissionsInJson,
2019-08-24 11:07:07 +00:00
'sleep_time' => $sleepTime,
'room_id' => $deviceOwnerRoomId,
2019-08-23 11:39:42 +00:00
);
DeviceManager::edit($deviceId, $args);
} catch (\Exception $e) {
echo $e->message();
}
//Debug
if (DEBUGMOD == 1) {
echo '<pre>';
echo $permissionsInJson;
echo $deviceId;
var_dump(json_decode ($permissionsInJson));
echo '</pre>';
2019-10-11 14:32:05 +00:00
echo '<a href="' . BASEDIR .'">CONTINUE</a>';
2019-08-23 11:39:42 +00:00
die();
}
} else if (isset($_POST['approveDevice'])) {
$deviceId = $_POST['deviceId'];
$args = array(
'approved' => 1,
);
DeviceManager::edit($deviceId, $args);
} else if (isset($_POST['disableDevice'])) {
$deviceId = $_POST['deviceId'];
$args = array(
'approved' => 2,
);
DeviceManager::edit($deviceId, $args);
}
//Debug
if (DEBUGMOD == 1) {
echo '<pre>';
var_dump($POST);
echo '</pre>';
2019-10-11 14:32:05 +00:00
echo '<a href="' . BASEDIR . '">CONTINUE</a>';
2019-08-23 11:39:42 +00:00
die();
}
2019-10-11 14:32:05 +00:00
header('Location: ' . BASEDIR );
2019-08-23 11:39:42 +00:00
die();
}
?>