Clean Up
This commit is contained in:
		@@ -1,4 +0,0 @@
 | 
				
			|||||||
Login
 | 
					 | 
				
			||||||
https://dev.steelants.cz/vasek/home/apiFront.php
 | 
					 | 
				
			||||||
```json
 | 
					 | 
				
			||||||
{"username":"username","password":"password"}```
 | 
					 | 
				
			||||||
							
								
								
									
										98
									
								
								apiFront.php
									
									
									
									
									
								
							
							
						
						
									
										98
									
								
								apiFront.php
									
									
									
									
									
								
							@@ -1,98 +0,0 @@
 | 
				
			|||||||
<?php
 | 
					 | 
				
			||||||
/** Includes **/
 | 
					 | 
				
			||||||
include_once('./config.php');
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
//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;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
//Log
 | 
					 | 
				
			||||||
$apiLogManager = new LogManager('./app/logs/apiFront/'. date("Y-m-d").'.log');
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
//DB Conector
 | 
					 | 
				
			||||||
Db::connect (DBHOST, DBUSER, DBPASS, DBNAME);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
//Read API data
 | 
					 | 
				
			||||||
$json = file_get_contents('php://input');
 | 
					 | 
				
			||||||
$obj = json_decode($json, true);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
//Log RAW api request
 | 
					 | 
				
			||||||
if (API_DEBUGMOD == 1) {
 | 
					 | 
				
			||||||
	$apiLogManager->write("[API] request body\n" . json_encode($obj, JSON_PRETTY_PRINT), LogRecordType::INFO);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
$apiManager = new ApiManager();
 | 
					 | 
				
			||||||
echo $apiManager->generateToken($obj['username'],$obj['password']);
 | 
					 | 
				
			||||||
die();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/*
 | 
					 | 
				
			||||||
if (
 | 
					 | 
				
			||||||
	isset($obj['username']) &&
 | 
					 | 
				
			||||||
	$obj['username'] != '' &&
 | 
					 | 
				
			||||||
	isset($obj['password']) &&
 | 
					 | 
				
			||||||
	$obj['password'] != ''
 | 
					 | 
				
			||||||
){
 | 
					 | 
				
			||||||
	$ota = false;
 | 
					 | 
				
			||||||
	$userName = $_POST['username'];
 | 
					 | 
				
			||||||
	$userPassword = $_POST['password'];
 | 
					 | 
				
			||||||
	$rememberMe = (isset ($_POST['remember']) ? $_POST['remember'] : "");
 | 
					 | 
				
			||||||
	$ota = $userManager->haveOtaEnabled($userName);
 | 
					 | 
				
			||||||
	if ($ota == "") {
 | 
					 | 
				
			||||||
		$landingPage = $userManager->login($userName, $userPassword, $rememberMe);
 | 
					 | 
				
			||||||
		header('Location: ' . BASEDIR . $landingPage);
 | 
					 | 
				
			||||||
		die();
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	$_SESSION['USERNAME'] = $userName;
 | 
					 | 
				
			||||||
	$_SESSION['PASSWORD'] = $userPassword;
 | 
					 | 
				
			||||||
	$_SESSION['REMEMBER'] = $rememberMe;
 | 
					 | 
				
			||||||
	$_SESSION['OTA'] = $ota;
 | 
					 | 
				
			||||||
} else if (
 | 
					 | 
				
			||||||
	isset($_POST['otaCode']) &&
 | 
					 | 
				
			||||||
	$_POST['otaCode'] != ''
 | 
					 | 
				
			||||||
) {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	$otaCode = $_POST['otaCode'];
 | 
					 | 
				
			||||||
	$otaSecret = $_POST['otaSecret'];
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	$ga = new PHPGangsta_GoogleAuthenticator();
 | 
					 | 
				
			||||||
	$ota = $_SESSION['OTA'];
 | 
					 | 
				
			||||||
	$userName = $_SESSION['USERNAME'];
 | 
					 | 
				
			||||||
	$userPassword = $_SESSION['PASSWORD'];
 | 
					 | 
				
			||||||
	$rememberMe = $_SESSION['REMEMBER'];
 | 
					 | 
				
			||||||
	unset($_SESSION['OTA']);
 | 
					 | 
				
			||||||
	$checkResult = $ga->verifyCode($otaSecret, $otaCode, 2);    // 2 = 2*30sec clock tolerance
 | 
					 | 
				
			||||||
	if ($checkResult) {
 | 
					 | 
				
			||||||
		$landingPage = $userManager->login($userName, $userPassword, $rememberMe);
 | 
					 | 
				
			||||||
		header('Location: ' . BASEDIR . $landingPage);
 | 
					 | 
				
			||||||
		echo 'OK';
 | 
					 | 
				
			||||||
	} else {
 | 
					 | 
				
			||||||
		echo 'FAILED';
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	//TODO: upravi a ověřit jeslti ja zabezpečené
 | 
					 | 
				
			||||||
	//TODO:
 | 
					 | 
				
			||||||
	die();
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
*/
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/*unset($logManager);
 | 
					 | 
				
			||||||
Db::disconect();
 | 
					 | 
				
			||||||
die();*/
 | 
					 | 
				
			||||||
							
								
								
									
										180
									
								
								old.home
									
									
									
									
									
								
							
							
						
						
									
										180
									
								
								old.home
									
									
									
									
									
								
							@@ -1,180 +0,0 @@
 | 
				
			|||||||
<!-- <?php
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// class Home extends Template
 | 
					 | 
				
			||||||
// {
 | 
					 | 
				
			||||||
// 	function __construct()
 | 
					 | 
				
			||||||
// 	{
 | 
					 | 
				
			||||||
// 		global $userManager;
 | 
					 | 
				
			||||||
// 		global $lang;
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 		if (!$userManager->isLogin()){
 | 
					 | 
				
			||||||
// 			header('Location: ./login');
 | 
					 | 
				
			||||||
// 		}
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 		$template = new Template('home');
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 		//users instantialize
 | 
					 | 
				
			||||||
// 		$users = UserManager::getUsers();
 | 
					 | 
				
			||||||
// 		$template->prepare('users', $users);
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 		//Users at home Info
 | 
					 | 
				
			||||||
// 		$usersAtHome = '';
 | 
					 | 
				
			||||||
// 		$i = 0;
 | 
					 | 
				
			||||||
// 		foreach ($users as $user) {
 | 
					 | 
				
			||||||
// 			$i++;
 | 
					 | 
				
			||||||
// 			if ($user['at_home'] == 'true') {
 | 
					 | 
				
			||||||
// 				$usersAtHome .= $user['username'];
 | 
					 | 
				
			||||||
// 				if ($usersAtHome != "" && isset($users[$i + 1])){
 | 
					 | 
				
			||||||
// 					$usersAtHome .= ', ';
 | 
					 | 
				
			||||||
// 				}
 | 
					 | 
				
			||||||
// 			}
 | 
					 | 
				
			||||||
// 		}
 | 
					 | 
				
			||||||
// 		$template->prepare('usersAtHome', $usersAtHome);
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 		$roomsItems = [];
 | 
					 | 
				
			||||||
// 		$roomsData = RoomManager::getAllRooms();
 | 
					 | 
				
			||||||
// 		foreach ($roomsData as $roomKey => $roomsData) {
 | 
					 | 
				
			||||||
// 			$devices = [];
 | 
					 | 
				
			||||||
// 			$devicesData = DeviceManager::getAllDevicesInRoom($roomsData['room_id']);
 | 
					 | 
				
			||||||
// 			foreach ($devicesData as $deviceKey => $deviceData) {
 | 
					 | 
				
			||||||
// 				$subDevices = [];
 | 
					 | 
				
			||||||
// 				$subDevicesData = SubDeviceManager::getAllSubDevices($deviceData['device_id']);
 | 
					 | 
				
			||||||
// 				foreach ($subDevicesData as $subDeviceKey => $subDeviceData) {
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 					$events = RecordManager::getLastRecord($subDeviceData['subdevice_id'], 5);
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 					$connectionError = false;
 | 
					 | 
				
			||||||
// 					$parsedValue = "";
 | 
					 | 
				
			||||||
// 					$niceTime = "";
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 					if (sizeof($events) > 1) {
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 						//TODO: skontrolovat zdali se jedná o poslední (opravdu nejaktuálnější) záznam
 | 
					 | 
				
			||||||
// 						$lastRecord = $events[0];
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 						$parsedValue = round($lastRecord['value']);
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 						/*Value Parsing*/
 | 
					 | 
				
			||||||
// 						if ($subDeviceData['type'] == "on/off") {
 | 
					 | 
				
			||||||
// 							$parsedValue = ($parsedValue == 1 ? 'ON' : 'OFF');
 | 
					 | 
				
			||||||
// 						}
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 						if ($subDeviceData['type'] == "door") {
 | 
					 | 
				
			||||||
// 							$replacementTrue = 'Closed';
 | 
					 | 
				
			||||||
// 							$replacementFalse = 'Opened';
 | 
					 | 
				
			||||||
// 							foreach ($events as $key => $value) {
 | 
					 | 
				
			||||||
// 								$events[$key]['value'] = ($value['value'] == 1 ? $replacementTrue : $replacementFalse);
 | 
					 | 
				
			||||||
// 							}
 | 
					 | 
				
			||||||
// 							$parsedValue = ($parsedValue == 1 ? $replacementTrue : $replacementFalse);
 | 
					 | 
				
			||||||
// 						}
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 						if ($subDeviceData['type'] == "light") {
 | 
					 | 
				
			||||||
// 							$replacementTrue = 'Light';
 | 
					 | 
				
			||||||
// 							$replacementFalse = 'Dark';
 | 
					 | 
				
			||||||
// 							foreach ($events as $key => $value) {
 | 
					 | 
				
			||||||
// 								if ($parsedValue != 1){
 | 
					 | 
				
			||||||
// 									//Analog Reading
 | 
					 | 
				
			||||||
// 									$events[$key]['value'] = ($value['value'] <= 810 ? $replacementTrue : $replacementFalse);
 | 
					 | 
				
			||||||
// 								} else {
 | 
					 | 
				
			||||||
// 									//Digital Reading
 | 
					 | 
				
			||||||
// 									$events[$key]['value'] = ($value['value'] == 0 ? $replacementTrue : $replacementFalse);
 | 
					 | 
				
			||||||
// 								}
 | 
					 | 
				
			||||||
// 							}
 | 
					 | 
				
			||||||
// 							if ($parsedValue != 1){
 | 
					 | 
				
			||||||
// 								//Analog Reading
 | 
					 | 
				
			||||||
// 								$parsedValue = ($parsedValue <= 810 ? $replacementTrue : $replacementFalse);
 | 
					 | 
				
			||||||
// 							} else {
 | 
					 | 
				
			||||||
// 								//Digital Reading
 | 
					 | 
				
			||||||
// 								$parsedValue = ($parsedValue == 0 ? $replacementTrue : $replacementFalse);
 | 
					 | 
				
			||||||
// 							}
 | 
					 | 
				
			||||||
// 						}
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 						$date2 = new DateTime($lastRecord['time']);
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 						$niceTime = $this->ago($date2);
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 						$startDate = date_create($lastRecord['time']);
 | 
					 | 
				
			||||||
// 						$interval = $startDate->diff(new DateTime());
 | 
					 | 
				
			||||||
// 						$hours   = $interval->format('%h');
 | 
					 | 
				
			||||||
// 						$minutes = $interval->format('%i');
 | 
					 | 
				
			||||||
// 						$lastSeen = ($hours * 60 + $minutes);
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 						if ($lastSeen > $deviceData['sleep_time'] && $subDeviceData['type'] != "on/off") {
 | 
					 | 
				
			||||||
// 							$connectionError = true;
 | 
					 | 
				
			||||||
// 						}
 | 
					 | 
				
			||||||
// 					} else {
 | 
					 | 
				
			||||||
// 						$connectionError = true;
 | 
					 | 
				
			||||||
// 					}
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 					$subDevices[$subDeviceData['subdevice_id']] = [
 | 
					 | 
				
			||||||
// 						'events'=> $events,
 | 
					 | 
				
			||||||
// 						'type' => $subDeviceData['type'],
 | 
					 | 
				
			||||||
// 						'unit' => $subDeviceData['unit'],
 | 
					 | 
				
			||||||
// 						'comError' => $connectionError,
 | 
					 | 
				
			||||||
// 						'lastRecort' =>  [
 | 
					 | 
				
			||||||
// 							'value' => (empty($parsedValue) ? 0 : $parsedValue),
 | 
					 | 
				
			||||||
// 							'time' => (empty($lastRecord['time']) ? "00:00" : $lastRecord['time']),
 | 
					 | 
				
			||||||
// 							'niceTime' => (empty($niceTime) ? "00:00" : $niceTime),
 | 
					 | 
				
			||||||
// 						],
 | 
					 | 
				
			||||||
// 					];
 | 
					 | 
				
			||||||
// 				}
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 				$permissionArray = json_decode($deviceData['permission']);
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 				$userIsDeviceAdmin = false;
 | 
					 | 
				
			||||||
// 				if($permissionArray[1] == 3) {
 | 
					 | 
				
			||||||
// 					$userIsDeviceAdmin = true;
 | 
					 | 
				
			||||||
// 				} else if ($permissionArray[0] == 3) {
 | 
					 | 
				
			||||||
// 					if ( $deviceData['owner'] == $userManager->getUserData('user_id')) {
 | 
					 | 
				
			||||||
// 						$userIsDeviceAdmin = true;
 | 
					 | 
				
			||||||
// 					}
 | 
					 | 
				
			||||||
// 				}
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 				$devices[$deviceData['device_id']] = [
 | 
					 | 
				
			||||||
// 					'name' => $deviceData['name'],
 | 
					 | 
				
			||||||
// 					'icon' => $deviceData['icon'],
 | 
					 | 
				
			||||||
// 					'room' => $deviceData['room_id'],
 | 
					 | 
				
			||||||
// 					'token' => $deviceData['token'],
 | 
					 | 
				
			||||||
// 					'sleepTime' => $deviceData['sleep_time'],
 | 
					 | 
				
			||||||
// 					'approved' => $deviceData['approved'],
 | 
					 | 
				
			||||||
// 					'permission' => $permissionArray,
 | 
					 | 
				
			||||||
// 					'owner' => $deviceData['owner'],
 | 
					 | 
				
			||||||
// 					'userIsAdmin' => $userIsDeviceAdmin,
 | 
					 | 
				
			||||||
// 					'subDevices' => $subDevices,
 | 
					 | 
				
			||||||
// 				];
 | 
					 | 
				
			||||||
// 			}
 | 
					 | 
				
			||||||
// 			$roomsItems[$roomsData['room_id']] = [
 | 
					 | 
				
			||||||
// 				'name' => $roomsData['name'],
 | 
					 | 
				
			||||||
// 				'deviceCount' => $roomsData['device_count'],
 | 
					 | 
				
			||||||
// 				'devices' => $devices,
 | 
					 | 
				
			||||||
// 			];
 | 
					 | 
				
			||||||
// 		}
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 		$rooms = RoomManager::getAllRooms();
 | 
					 | 
				
			||||||
// 		$template->prepare('rooms', $rooms);
 | 
					 | 
				
			||||||
// 		$template->prepare('title', 'Home');
 | 
					 | 
				
			||||||
// 		$template->prepare('lang', $lang);
 | 
					 | 
				
			||||||
// 		$template->prepare('data', $roomsItems);
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 		$template->render();
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 	}
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 	function ago( $datetime )
 | 
					 | 
				
			||||||
// 	{
 | 
					 | 
				
			||||||
// 		$interval = date_create('now')->diff( $datetime );
 | 
					 | 
				
			||||||
// 		$suffix = ( $interval->invert ? ' ago' : '' );
 | 
					 | 
				
			||||||
// 		if ( $v = $interval->y >= 1 ) return $this->pluralize( $interval->m, 'month' ) . $suffix;
 | 
					 | 
				
			||||||
// 		if ( $v = $interval->d >= 1 ) return $this->pluralize( $interval->d, 'day' ) . $suffix;
 | 
					 | 
				
			||||||
// 		if ( $v = $interval->h >= 1 ) return $this->pluralize( $interval->h, 'hour' ) . $suffix;
 | 
					 | 
				
			||||||
// 		if ( $v = $interval->i >= 1 ) return $this->pluralize( $interval->i, 'minute' ) . $suffix;
 | 
					 | 
				
			||||||
// 		return $this->pluralize( $interval->s, 'second' ) . $suffix;
 | 
					 | 
				
			||||||
// 	}
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 	function pluralize( $count, $text )
 | 
					 | 
				
			||||||
// 	{
 | 
					 | 
				
			||||||
// 		return $count . ( ( $count == 1 ) ? ( " $text" ) : ( " ${text}s" ) );
 | 
					 | 
				
			||||||
// 	}
 | 
					 | 
				
			||||||
// }
 | 
					 | 
				
			||||||
		Reference in New Issue
	
	Block a user