From bf4da864b9e196f55bac146a54b262d717bb6fc3 Mon Sep 17 00:00:00 2001 From: rsgrinko Date: Wed, 12 Jan 2022 11:26:34 +0300 Subject: [PATCH 01/12] add api method getUserInfo --- inc/lib/CAPI.class.php | 11 ++- inc/lib/CBackup.class.php | 157 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 inc/lib/CBackup.class.php diff --git a/inc/lib/CAPI.class.php b/inc/lib/CAPI.class.php index e0ad469..b8fcfce 100644 --- a/inc/lib/CAPI.class.php +++ b/inc/lib/CAPI.class.php @@ -1,7 +1,16 @@ 'ok', 'message' => 'test was completed :)']; } + + public static function getUserInfo() { + global $USER; + unset($USER['pass']); + return $USER; + } } \ No newline at end of file diff --git a/inc/lib/CBackup.class.php b/inc/lib/CBackup.class.php new file mode 100644 index 0000000..43ebdf4 --- /dev/null +++ b/inc/lib/CBackup.class.php @@ -0,0 +1,157 @@ +backupFolder = $backupFolder; + } + + /** + * @param string $backupName + */ + public function setBackupName(string $backupName): void + { + $this->backupName = $backupName; + } + + /** + * @param string $dir + */ + public function setDir(string $dir): void + { + $this->dir = $dir; + } + + /** + * @param int $delayDelete + */ + public function setDelayDelete(int $delayDelete): void + { + $this->delayDelete = $delayDelete; + } + + /** + * @param string $dbHost + */ + public function setDbHost(string $dbHost): void + { + $this->dbHost = $dbHost; + } + + /** + * @param string $dbHser + */ + public function setDbHser(string $dbHser): void + { + $this->dbHser = $dbHser; + } + + /** + * @param string $dbPassword + */ + public function setDbPassword(string $dbPassword): void + { + $this->dbPassword = $dbPassword; + } + + /** + * @param string $dbName + */ + public function setDbName(string $dbName): void + { + $this->dbName = $dbName; + } + + +public function __construct() { + +} + + +public function backupFiles($backup_folder, $backup_name, $dir) { + $fullFileName = $backup_folder . '/' . $backup_name . '.tar.gz'; + shell_exec("tar -cvf " . $fullFileName . " " . $dir . "/* "); + return $fullFileName; +} + +public function backupDB($backup_folder, $backup_name) { + $fullFileName = $backup_folder . '/' . $backup_name . '.sql'; + $command = 'mysqldump -h' . $db_host . ' -u' . $db_user . ' -p' . $db_password . ' ' . $db_name . ' > ' . $fullFileName; + shell_exec($command); + return $fullFileName; +} + +public function deleteOldArchives($backup_folder, $delay_delete) { + $this_time = time(); + $files = glob($backup_folder . "/*.tar.gz*"); + $deleted = array(); + foreach ($files as $file) { + if ($this_time - filemtime($file) > $delay_delete) { + array_push($deleted, $file); + unlink($file); + } + } + return $deleted; +} + + +public function createBackup() { + +} + +} +$mail_to = 'my_email@example.com'; +$mail_subject = 'Site backup'; +$mail_message = ''; +$mail_headers = 'MIME-Version: 1.0' . "\r\n"; +$mail_headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; +$mail_headers .= 'To: me ' . "\r\n"; +$mail_headers .= 'From: my_site ' . "\r\n"; + + + + + + + + + + +$start = microtime(true); // запускаем таймер + +$deleteOld = deleteOldArchives($backup_folder, $delay_delete); // удаляем старые архивы +$doBackupFiles = backupFiles($backup_folder, $backup_name, $dir); // делаем бэкап файлов +$doBackupDB = backupDB($backup_folder, $backup_name); // и базы данных + +// добавляем в письмо отчеты +if ($doBackupFiles) { + $mail_message .= 'site backuped successfully
'; + $mail_message .= 'Files: ' . $doBackupFiles . '
'; +} + +if ($doBackupDB) { + $mail_message .= 'DB: ' . $doBackupDB . '
'; +} + +if ($deleteOld) { + foreach ($deleteOld as $val) { + $mail_message .= 'File deleted: ' . $val . '
'; + } +} + +$time = microtime(true) - $start; // считаем время, потраченое на выполнение скрипта +$mail_message .= 'script time: ' . $time . '
'; + +mail($mail_to, $mail_subject, $mail_message, $mail_headers); // и отправляем письмо \ No newline at end of file From 4de0b199ff193d11cb0c45ffc15584bb464bbdc4 Mon Sep 17 00:00:00 2001 From: rsgrinko Date: Wed, 12 Jan 2022 11:27:45 +0300 Subject: [PATCH 02/12] fix array key --- inc/lib/CAPI.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/lib/CAPI.class.php b/inc/lib/CAPI.class.php index b8fcfce..3eb7e2c 100644 --- a/inc/lib/CAPI.class.php +++ b/inc/lib/CAPI.class.php @@ -10,7 +10,7 @@ class CAPI { public static function getUserInfo() { global $USER; - unset($USER['pass']); + unset($USER['password']); return $USER; } } \ No newline at end of file From e795fe3daff2dc806988a2088511d840cd20e1ab Mon Sep 17 00:00:00 2001 From: rsgrinko Date: Wed, 12 Jan 2022 11:31:36 +0300 Subject: [PATCH 03/12] fix method --- inc/lib/CAPI.class.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/inc/lib/CAPI.class.php b/inc/lib/CAPI.class.php index 3eb7e2c..071d451 100644 --- a/inc/lib/CAPI.class.php +++ b/inc/lib/CAPI.class.php @@ -11,6 +11,8 @@ class CAPI { public static function getUserInfo() { global $USER; unset($USER['password']); - return $USER; + $result = $USER; + $result['devices'] = getUserDevices($USER['id']); + return $result; } } \ No newline at end of file From 7b2c0c2f70335aefdbeadfe87e683bfd361271cf Mon Sep 17 00:00:00 2001 From: rsgrinko Date: Wed, 12 Jan 2022 12:36:31 +0300 Subject: [PATCH 04/12] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=BF=D0=B0=D1=80=D0=B0=D0=BC=D0=B5=D1=82?= =?UTF-8?q?=D1=80=D1=8B=20=D1=83=D1=81=D1=82=D1=80=D0=BE=D0=B9=D1=81=D1=82?= =?UTF-8?q?=D0=B2=20=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D1=82?= =?UTF-8?q?=D0=B5=D0=BB=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/lib/CAPI.class.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/inc/lib/CAPI.class.php b/inc/lib/CAPI.class.php index 071d451..9099af4 100644 --- a/inc/lib/CAPI.class.php +++ b/inc/lib/CAPI.class.php @@ -13,6 +13,9 @@ class CAPI { unset($USER['password']); $result = $USER; $result['devices'] = getUserDevices($USER['id']); + foreach($result['devices'] as $key => $value) { + $result['devices'][$key]['config'] = CIoT::getBoardConfig($value['id']); + } return $result; } } \ No newline at end of file From 670e0826c19c99d6773c8759cf14828024ed2354 Mon Sep 17 00:00:00 2001 From: rsgrinko Date: Thu, 13 Jan 2022 11:47:56 +0300 Subject: [PATCH 05/12] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=B4=D0=BE=D0=B5=D0=B2=D0=BE=D0=B9=20API=20?= =?UTF-8?q?=D0=BE=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=87=D0=B8=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/api.php | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 api/api.php diff --git a/api/api.php b/api/api.php new file mode 100644 index 0000000..c8f5f40 --- /dev/null +++ b/api/api.php @@ -0,0 +1,41 @@ + 'fail', 'message' => 'Token not set']); + die(); + } + + if(!isset($_REQUEST['method']) or empty($_REQUEST['method'])) { + echo CJson::create(['status' => 'fail', 'message' => 'Method not set']); + die(); + } + + $token = prepareString($_REQUEST['token']); + + if(!CUser::isTokenExists($token)) { + echo CJson::create(['status' => 'fail', 'message' => 'Token not found']); + die(); + } + + $method = prepareString($_REQUEST['method']); + + $USER = CUser::getUserByToken($token); + + // имеем пользователя, который что то хочет. действуем... + // TODO: дописать реализацию API + // pre($USER); + require_once __DIR__ . '/../inc/lib/CAPI.class.php'; + + try { + $result = CAPI::$method(); + } catch (Throwable $e) { + echo CJson::create(['status' => 'fail', 'message' => 'Метод '.$method.' не найден']); + die(); + } + + echo CJson::create($result); + \ No newline at end of file From cf0b1c40e6233d0dab77833a16422726910eb48d Mon Sep 17 00:00:00 2001 From: rsgrinko Date: Thu, 13 Jan 2022 11:59:41 +0300 Subject: [PATCH 06/12] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=20=D0=BF=D0=BE?= =?UTF-8?q?=D0=BB=D1=83=D1=87=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BF=D0=BE=D0=BA?= =?UTF-8?q?=D0=B0=D0=B7=D0=B0=D0=BD=D0=B8=D0=B9=20=D0=B4=D0=B0=D1=82=D1=87?= =?UTF-8?q?=D0=B8=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/lib/CAPI.class.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/inc/lib/CAPI.class.php b/inc/lib/CAPI.class.php index 9099af4..37def3b 100644 --- a/inc/lib/CAPI.class.php +++ b/inc/lib/CAPI.class.php @@ -16,6 +16,22 @@ class CAPI { foreach($result['devices'] as $key => $value) { $result['devices'][$key]['config'] = CIoT::getBoardConfig($value['id']); } + $result['status'] = 'ok'; return $result; } + + public static function getSensorValue() { + global $USER; + $sensor = $_REQUEST['sensor']; + $deviceId = $_REQUEST['deviceId']; + + $arUserDevices = getUserDevices($USER['id']); + if (!in_array($deviceId, $arUserDevices)) { + return ['status' => 'fail', 'message' => 'Выбранное устройство не принадлежит текущему пользователю']; + } + + $sensorValue = CIoT::getSensorData($deviceId, $sensor); + return ['status' => 'ok', 'deviceId' => $deviceId, 'sensor' => $sensor, 'value' => $sensorValue]; + + } } \ No newline at end of file From 51c4a985f360895d72ce5874e68ef6f7288a7284 Mon Sep 17 00:00:00 2001 From: rsgrinko Date: Thu, 13 Jan 2022 12:22:30 +0300 Subject: [PATCH 07/12] fix --- inc/lib/CAPI.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/lib/CAPI.class.php b/inc/lib/CAPI.class.php index 37def3b..c3cce82 100644 --- a/inc/lib/CAPI.class.php +++ b/inc/lib/CAPI.class.php @@ -25,7 +25,7 @@ class CAPI { $sensor = $_REQUEST['sensor']; $deviceId = $_REQUEST['deviceId']; - $arUserDevices = getUserDevices($USER['id']); + $arUserDevices = CIoT::getUserDevices($USER['id']); if (!in_array($deviceId, $arUserDevices)) { return ['status' => 'fail', 'message' => 'Выбранное устройство не принадлежит текущему пользователю']; } From 49c5bd8276f7298978ea9f0f6f18eb0d79bdee9d Mon Sep 17 00:00:00 2001 From: rsgrinko Date: Thu, 13 Jan 2022 12:34:41 +0300 Subject: [PATCH 08/12] =?UTF-8?q?=D1=84=D0=B8=D0=BA=D1=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/lib/CAPI.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/lib/CAPI.class.php b/inc/lib/CAPI.class.php index c3cce82..37def3b 100644 --- a/inc/lib/CAPI.class.php +++ b/inc/lib/CAPI.class.php @@ -25,7 +25,7 @@ class CAPI { $sensor = $_REQUEST['sensor']; $deviceId = $_REQUEST['deviceId']; - $arUserDevices = CIoT::getUserDevices($USER['id']); + $arUserDevices = getUserDevices($USER['id']); if (!in_array($deviceId, $arUserDevices)) { return ['status' => 'fail', 'message' => 'Выбранное устройство не принадлежит текущему пользователю']; } From fe3bd6ff4e42d9fe7a6b7b08bc3dd0f0da2d2b44 Mon Sep 17 00:00:00 2001 From: rsgrinko Date: Thu, 13 Jan 2022 12:36:36 +0300 Subject: [PATCH 09/12] fix --- inc/lib/CAPI.class.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/inc/lib/CAPI.class.php b/inc/lib/CAPI.class.php index 37def3b..15912e6 100644 --- a/inc/lib/CAPI.class.php +++ b/inc/lib/CAPI.class.php @@ -26,6 +26,14 @@ class CAPI { $deviceId = $_REQUEST['deviceId']; $arUserDevices = getUserDevices($USER['id']); + + $canAccessToDevice = false; + foreach($arUserDevices as $device) { + if($device['id'] == $deviceId) { + $canAccessToDevice = true; + break; + } + } if (!in_array($deviceId, $arUserDevices)) { return ['status' => 'fail', 'message' => 'Выбранное устройство не принадлежит текущему пользователю']; } From 97c65ffa7f4a3e8dcdc2c4952ee3ceeb02842820 Mon Sep 17 00:00:00 2001 From: rsgrinko Date: Thu, 13 Jan 2022 12:37:02 +0300 Subject: [PATCH 10/12] fix --- inc/lib/CAPI.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/lib/CAPI.class.php b/inc/lib/CAPI.class.php index 15912e6..e0b2bba 100644 --- a/inc/lib/CAPI.class.php +++ b/inc/lib/CAPI.class.php @@ -34,7 +34,7 @@ class CAPI { break; } } - if (!in_array($deviceId, $arUserDevices)) { + if (!$canAccessToDevice) { return ['status' => 'fail', 'message' => 'Выбранное устройство не принадлежит текущему пользователю']; } From 66ce3cb448d56bcadaa890c6680943fd18f52c2e Mon Sep 17 00:00:00 2001 From: rsgrinko Date: Thu, 13 Jan 2022 12:37:53 +0300 Subject: [PATCH 11/12] =?UTF-8?q?fix=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4?= =?UTF-8?q?=D0=B0=20=D0=BF=D0=BE=D0=BB=D1=83=D1=87=D0=B5=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D1=85=20=D0=B4=D0=B0=D1=82?= =?UTF-8?q?=D1=87=D0=B8=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/lib/CAPI.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/lib/CAPI.class.php b/inc/lib/CAPI.class.php index e0b2bba..6ddc4a8 100644 --- a/inc/lib/CAPI.class.php +++ b/inc/lib/CAPI.class.php @@ -38,7 +38,7 @@ class CAPI { return ['status' => 'fail', 'message' => 'Выбранное устройство не принадлежит текущему пользователю']; } - $sensorValue = CIoT::getSensorData($deviceId, $sensor); + $sensorValue = CIoT::getSensorData($deviceId, $sensor)['value']; return ['status' => 'ok', 'deviceId' => $deviceId, 'sensor' => $sensor, 'value' => $sensorValue]; } From 2113134abb01b43871384eb008f83b81a8304e6c Mon Sep 17 00:00:00 2001 From: rsgrinko Date: Thu, 13 Jan 2022 12:41:31 +0300 Subject: [PATCH 12/12] =?UTF-8?q?fix=20=D1=83=D1=81=D0=BB=D0=BE=D0=B2?= =?UTF-8?q?=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/lib/CAPI.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/lib/CAPI.class.php b/inc/lib/CAPI.class.php index 6ddc4a8..abc5cf6 100644 --- a/inc/lib/CAPI.class.php +++ b/inc/lib/CAPI.class.php @@ -34,8 +34,8 @@ class CAPI { break; } } - if (!$canAccessToDevice) { - return ['status' => 'fail', 'message' => 'Выбранное устройство не принадлежит текущему пользователю']; + if (!$canAccessToDevice and $USER['access_level'] !== 'admin') { + return ['status' => 'fail', 'message' => 'Недостаточно прав для доступа к показаниям данного устройства']; } $sensorValue = CIoT::getSensorData($deviceId, $sensor)['value'];