diff --git a/.htaccess b/.htaccess index b0a8024..289a0f5 100644 --- a/.htaccess +++ b/.htaccess @@ -1,9 +1,9 @@ RewriteEngine On # require https -RewriteCond %{HTTPS} off -RewriteCond %{REQUEST_URI} !^/api/update -RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] +#RewriteCond %{HTTPS} off +#RewriteCond %{REQUEST_URI} !^/api/update +#RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] #token to HTTP_AUTHORIZATION RewriteCond %{HTTP:Authorization} ^(.*) diff --git a/app/Bootstrap.php b/app/Bootstrap.php index 9faf5db..b45c175 100644 --- a/app/Bootstrap.php +++ b/app/Bootstrap.php @@ -1,4 +1,15 @@ write("[API] request body\n" . json_encode($obj, JSON_PRETTY_PRI $apiLogManager->write("[API] POST body\n" . json_encode($_POST, JSON_PRETTY_PRINT), LogRecordTypes::INFO); $apiLogManager->write("[API] GET body\n" . json_encode($_GET, JSON_PRETTY_PRINT), LogRecordTypes::INFO); -//Debug -error_reporting(E_ALL); -ini_set( 'display_errors','1'); -//setup -ini_set ('session.cookie_httponly', '1'); -ini_set('session.cookie_domain', $_SERVER['HTTP_HOST']); -ini_set('session.cookie_path', str_replace("login", "", str_replace('https://' . $_SERVER['HTTP_HOST'], "", $_SERVER['REQUEST_URI']))); -ini_set('session.cookie_secure', '1'); -mb_internal_encoding ("UTF-8"); Debugger::flag('dbconnect'); //D B Conector diff --git a/app/api/EndpointsApi.php b/app/api/EndpointsApi.php index 834a584..05a4558 100644 --- a/app/api/EndpointsApi.php +++ b/app/api/EndpointsApi.php @@ -76,7 +76,7 @@ class EndpointsApi extends ApiController{ //Log Data Save if (isset($obj['logs'])){ - foreach ($deviceLogs as $log) { + foreach ($obj['logs'] as $log) { $logManager = new LogManager('../logs/devices/'. date("Y-m-d").'.log'); $logManager->setLevel(LOGLEVEL); $logManager->write("[Device Log Msg] Device_ID " . $deviceId . "->" . $log, LogRecordTypes::ERROR); diff --git a/app/api/UpdatesApi.php b/app/api/UpdatesApi.php index 2656f44..0b72bf6 100644 --- a/app/api/UpdatesApi.php +++ b/app/api/UpdatesApi.php @@ -17,6 +17,7 @@ class UpdatesApi { return true; } return false; + } public function default(){ @@ -31,18 +32,18 @@ class UpdatesApi { header('Content-type: text/plain; charset=utf8', true); - //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'], LogRecordTypes::INFO); - exit(); - } - } + // //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'], LogRecordTypes::INFO); + // exit(); + // } + // } $macAddress = $_SERVER['HTTP_X_ESP8266_STA_MAC']; $localBinary = "../updater/" . str_replace(':', '', $macAddress) . ".bin"; diff --git a/app/api/WidgetApi.php b/app/api/WidgetApi.php index 2734d9f..65e5950 100644 --- a/app/api/WidgetApi.php +++ b/app/api/WidgetApi.php @@ -12,7 +12,7 @@ class WidgetApi extends ApiController{ $subDeviceData = SubDeviceManager::getSubDevice($subDeviceId); if ($subDeviceData['type'] == 'on/off'){ $lastValue = RecordManager::getLastRecord($subDeviceData['subdevice_id'])['value']; - RecordManager::create($subDeviceData['device_id'], 'on/off', !$lastValue); + RecordManager::create($subDeviceData['device_id'], 'on/off', (int) !$lastValue); $response = !$lastValue; } else { throw new Exception("Bad Request", 403); diff --git a/app/controllers/deviceController.php b/app/controllers/deviceController.php index 43ce0d0..0958618 100644 --- a/app/controllers/deviceController.php +++ b/app/controllers/deviceController.php @@ -1,7 +1,7 @@ getDeviceById ($_POST['deviceId'])['mac']; $fileName = (!empty ($deviceMac) ? str_replace (":", "", $deviceMac) . ".bin" : ""); @@ -10,15 +10,22 @@ if (!empty ($_POST)){ unlink("../updater/" . $fileName); } if ($fileName != "") { - echo 'coping file'.$fileName .copy ($file['tmp_name'], "../updater/" . $fileName);; - - } else { - + copy ($file['tmp_name'], "../updater/" . $fileName); } } if (isset ($_POST['deviceCommand']) && !empty ($_POST['deviceId'])) { - $deviceManager = new DeviceManager (); $deviceManager->edit ($_POST['deviceId'], array ('command' => $_POST['deviceCommand'])); + } else if (!empty ($_POST['deviceCommand'])) { + $devices = $deviceManager->getAllDevices(); + foreach ($devices as $key => $device) { + $deviceManager->edit ($device['device_id'], array ('command' => $_POST['deviceCommand'])); + } + } + if (!empty ($_POST['deviceRoomId']) && !empty ($_POST['deviceId'])) { + $deviceManager->edit ($_POST['deviceId'], array ('room_id' => $_POST['deviceRoomId'])); + } + if (!empty ($_POST['deviceName']) && !empty ($_POST['deviceId'])) { + $deviceManager->edit ($_POST['deviceId'], array ('name' => $_POST['deviceName'])); } header('Location: ./device'); die(); diff --git a/app/models/managers/DeviceManager.php b/app/models/managers/DeviceManager.php index 53255f8..415896c 100644 --- a/app/models/managers/DeviceManager.php +++ b/app/models/managers/DeviceManager.php @@ -3,7 +3,8 @@ class DeviceManager{ public static $devices; static function getAllDevices () { - return Db::loadAll ("SELECT * FROM devices WHERE approved != ?", Array(2)); + return Db::loadAll ("SELECT devices.* FROM devices + WHERE approved != ?", Array(2)); } static function getAllDevicesInRoom ($roomId = "") { @@ -26,6 +27,12 @@ class DeviceManager{ return Db::loadOne("SELECT * FROM devices WHERE device_id = ?", array($deviceId)); } + static function getAllDevicesSorted ($sort, $sortType = "ASC") { + return Db::loadAll ("SELECT devices.* FROM devices + LEFT JOIN rooms ON (devices.room_id = rooms.room_id) + WHERE devices.approved != ? ORDER BY $sort $sortType", Array(2)); + } + public static function create ($name, $token) { $defaultRoom = RoomManager::getDefaultRoomId(); $device = array ( diff --git a/app/models/managers/LogManager.php b/app/models/managers/LogManager.php index 082c646..edae6e8 100644 --- a/app/models/managers/LogManager.php +++ b/app/models/managers/LogManager.php @@ -35,7 +35,7 @@ class LogManager } public function __destruct(){ - if (isset($this->logFile)) { + if (isset($this->logFile) && $this->logFile != "Unable to open file!") { fclose($this->logFile); } } diff --git a/app/views/Device.php b/app/views/Device.php index fd586e5..d2dc4a1 100644 --- a/app/views/Device.php +++ b/app/views/Device.php @@ -6,6 +6,9 @@ class Device extends Template function __construct () { $userManager = new UserManager (); $deviceManager = new DeviceManager (); + $subDeviceManager = new SubDeviceManager (); + $recordManager = new RecordManager (); + $roomManager = new RoomManager (); $langMng = new LanguageManager ('en'); if (!$userManager->isLogin ()) { @@ -15,9 +18,73 @@ class Device extends Template $template = new Template ('device'); $template->prepare ('title', $langMng->get ("m_devices")); - $devices = $deviceManager->getAllDevices (); + if (!empty ($_GET['sort']) && !empty ($_SESSION['sort']) && $_SESSION['sort'] != $_GET['sort']) { + unset($_SESSION['sort']); + header('Location: device?sort=' . $_GET["sort"] . "&sortType=ASC"); + die(); + } + + if (isset ($_GET['sortType'])) { + switch ($_GET['sortType']) { + case "DESC": + $sortType = ""; + $sortIcon = ""; + break; + case "ASC": + $sortType = "DESC"; + $sortIcon = ""; + break; + case "": + unset($_GET["sort"]); + unset($_GET["sortType"]); + header('Location: device'); + die(); + break; + } + } else { + $sortType = "ASC"; + } + + if (!empty ($_GET['sort']) && !empty ($_GET['sortType'])) { + $template->prepare ('sortIcon', array ($_GET['sort'] => $sortIcon)); + $actualSort = "devices.device_id"; + switch ($_GET['sort']) { + case "name": + $actualSort = "devices.name"; + break; + case "room": + $actualSort = "rooms.name"; + break; + case "ip": + $actualSort = "devices.ip_address"; + break; + case "mac": + $actualSort = "devices.mac"; + break; + case "token": + $actualSort = "devices.token"; + break; + } + $devices = $deviceManager->getAllDevicesSorted ($actualSort, $_GET['sortType']); + } else { + $devices = $deviceManager->getAllDevices (); + } + + if (!empty ($_GET['sort'])) { + $_SESSION['sort'] = $_GET['sort']; + } foreach ($devices as $key => $device) { + $subdevice = $subDeviceManager->getSubDeviceByMasterAndType ($device['device_id'], "wifi"); + if (!empty ($subdevice['subdevice_id'])) { + $record = $recordManager->getLastRecord($subdevice['subdevice_id']); + if (!empty ($record)) { + $devices[$key]['signal'] = $record['value'] . " " . $subdevice['unit']; + } + } + if (empty ($devices[$key]['signal'])) { + $devices[$key]['signal'] = ""; + } $localBinary = "../updater/" . str_replace (':', '', $device['mac']) . ".bin"; if (file_exists ($localBinary)) { $hash = md5_file ($localBinary); @@ -29,11 +96,40 @@ class Device extends Template } else { $devices[$key]['firmware_hash'] = "false"; } + if (empty ($device['mac'])) { + $devices[$key]['firmware_hash'] = ""; + } } + if (!empty ($_GET['sort']) && !empty ($_GET['sortType']) && $_GET['sort'] == "firmware") { + if ($_GET['sortType'] == "DESC") { + usort($devices, function($a, $b) { + return $a['firmware_hash'] <=> $b['firmware_hash']; + }); + } else if ($_GET['sortType'] == "ASC") { + usort($devices, function($a, $b) { + return $b['firmware_hash'] <=> $a['firmware_hash']; + }); + } + } else if (!empty ($_GET['sort']) && !empty ($_GET['sortType']) && $_GET['sort'] == "signal") { + if ($_GET['sortType'] == "DESC") { + usort($devices, function($a, $b) { + return $a['signal'] <=> $b['signal']; + }); + } else if ($_GET['sortType'] == "ASC") { + usort($devices, function($a, $b) { + return $b['signal'] <=> $a['signal']; + }); + } + } + + $rooms = $roomManager->getAllRooms(); + $template->prepare ('baseDir', BASEDIR); $template->prepare ('debugMod', DEBUGMOD); $template->prepare ('logToLiveTime', LOGTIMOUT); + $template->prepare ('rooms', $rooms); + $template->prepare ('sortType', $sortType); $template->prepare ('devices', $devices); $template->prepare ('langMng', $langMng); diff --git a/app/views/Logout.php b/app/views/Logout.php index e0cf627..5debc4f 100644 --- a/app/views/Logout.php +++ b/app/views/Logout.php @@ -3,7 +3,7 @@ class Logout extends Template { function __construct() { - global $userManager; + $userManager = new UserManager (); $userManager->logout(); header('Location: ' . BASEURL . 'login'); die(); diff --git a/app/views/Oauth.php b/app/views/Oauth.php index 3da5d77..bee818e 100644 --- a/app/views/Oauth.php +++ b/app/views/Oauth.php @@ -3,7 +3,7 @@ class Oauth extends Template { function __construct() { - global $userManager; + $userManager = new UserManager (); $langMng = new LanguageManager('en'); $template = new Template('oauth'); diff --git a/app/views/templates/device.phtml b/app/views/templates/device.phtml index 367f930..dbd5aa5 100644 --- a/app/views/templates/device.phtml +++ b/app/views/templates/device.phtml @@ -1,5 +1,6 @@ + <?php echo $TITLE ?> +
@@ -22,50 +24,94 @@
- - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + +
#NameRoomFirmwareIP Address
(Mac)
Action
&#xRoom - - " style="float: center; "> - - -
- "> - -
- -
-

- -
- "> - - -
-
- -
#NameRoomSignalFirmware + IP Address
+ (Mac)
+ Token +
Action +
+ +
+
+
&#x +
+ "> + "> +
+
+
+ "> + +
+
+ + + + + + +
+ "> + +
+ +
+

+
+
+ +
+ "> + + +
+
+ +
@@ -75,6 +121,12 @@ $partial->render(); //TODO js do main.js ?> + - + \ No newline at end of file