Compare commits

...

3 Commits

Author SHA1 Message Date
katamaz 794e2447c0 perf: specify sql select 2026-09-01 21:01:29 +03:00
katamaz ff437d082c remove db=null 2026-09-01 20:51:57 +03:00
katamaz 22e3776892 cleanup config 2026-09-01 20:26:39 +03:00
7 changed files with 33 additions and 60 deletions
+1 -1
View File
@@ -16,6 +16,6 @@
- change `base` in `vite.config.ts` - change `base` in `vite.config.ts`
- change `API_BASE_URL` and `CLIENT_FRONTEND_URL` in `src/App.jsx` - change `API_BASE_URL` and `CLIENT_FRONTEND_URL` in `src/App.jsx`
- **backend !!** - **backend !!**
- change `ADMIN_PASS` and `API_PATH` in `admin_api.php` - change `ADMIN_PASS` in `admin_api.php`
- change `$password` in `adm/phpliteadmin.config.php` - change `$password` in `adm/phpliteadmin.config.php`
- **block http requests to `data.db`!!!** - **block http requests to `data.db`!!!**
+2 -1
View File
@@ -1,8 +1,9 @@
<?php <?php
include 'k-api.php'; include 'k-api.php';
// CHANGE THIS
const ADMIN_PASS = "superdengipassword4"; const ADMIN_PASS = "superdengipassword4";
const API_PATH = "./"; const API_PATH = "./";
function db() function db()
+24 -47
View File
@@ -112,8 +112,8 @@ CreateApi(function ($api) {
$classId = substr(strtoupper($req->data['classId']), 0, 10); $classId = substr(strtoupper($req->data['classId']), 0, 10);
// according docs (UPPERCASE, MIN 3 MAX 10) // according docs (UPPERCASE, MIN 3 MAX 10)
$db = db(); $db = db();
$results = FastDBReq($db, "SELECT * FROM classes WHERE classid = ?", [$classId]); $results = FastDBReq($db, "SELECT classid FROM classes WHERE classid = ?", [$classId]);
$db = null;
if (count($results) != 0) { if (count($results) != 0) {
return $results[0]; return $results[0];
} else { } else {
@@ -130,7 +130,7 @@ CreateApi(function ($api) {
// according docs (UPPERCASE, MIN 3 MAX 10) // according docs (UPPERCASE, MIN 3 MAX 10)
$db = db(); $db = db();
$results = FastDBReq($db, "SELECT userclassname FROM classes WHERE classid = ?", [$classId]); $results = FastDBReq($db, "SELECT userclassname FROM classes WHERE classid = ?", [$classId]);
$db = null;
if (count($results) != 0) { if (count($results) != 0) {
return $results[0]; return $results[0];
@@ -147,7 +147,7 @@ CreateApi(function ($api) {
$adminId = $req->data['adminId']; $adminId = $req->data['adminId'];
$db = db(); $db = db();
$results = FastDBReq($db, "SELECT adminName FROM admins where adminid = ?", [$adminId]); $results = FastDBReq($db, "SELECT adminName FROM admins where adminid = ?", [$adminId]);
$db = null;
if (count($results) != 0) { if (count($results) != 0) {
return $results[0]; return $results[0];
@@ -164,7 +164,7 @@ CreateApi(function ($api) {
$adminId = $req->data['adminId']; $adminId = $req->data['adminId'];
$db = db(); $db = db();
$results = FastDBReq($db, "SELECT adminPrems FROM admins where adminid = ?", [$adminId]); $results = FastDBReq($db, "SELECT adminPrems FROM admins where adminid = ?", [$adminId]);
$db = null;
if (count($results) != 0) { if (count($results) != 0) {
return $results[0]; return $results[0];
@@ -180,8 +180,8 @@ CreateApi(function ($api) {
} }
$adminId = $req->data['adminId']; $adminId = $req->data['adminId'];
$db = db(); $db = db();
$results = FastDBReq($db, "SELECT * FROM admins WHERE adminid = ?", [$adminId]); $results = FastDBReq($db, "SELECT adminId FROM admins WHERE adminid = ?", [$adminId]);
$db = null;
if (count($results) != 0) { if (count($results) != 0) {
return $results[0]; return $results[0];
@@ -203,7 +203,7 @@ CreateApi(function ($api) {
if (!preg_match('/\d{1,2}-\d{1,2}-(\d{2}|\d{4})$/', $day)) return "wrong date format"; if (!preg_match('/\d{1,2}-\d{1,2}-(\d{2}|\d{4})$/', $day)) return "wrong date format";
$db = db(); $db = db();
$results = FastDBReq($db, "SELECT * FROM changes WHERE classId = ? AND forDay = ?", [$classId, $day]); $results = FastDBReq($db, "SELECT content FROM changes WHERE classId = ? AND forDay = ?", [$classId, $day]);
if (count($results) > 0) { if (count($results) > 0) {
@@ -218,37 +218,33 @@ CreateApi(function ($api) {
} }
} }
if ($has_full_rings) { if ($has_full_rings) {
$db = null;
// with rings // with rings
return ['data' => $content, 'type' => 2]; return ['data' => $content, 'type' => 2];
} else { } else {
// without rings // without rings
$results = FastDBReq($db, "SELECT * FROM RINGS WHERE classId = ?", [$classId]); $results = FastDBReq($db, "SELECT content, fromDate FROM RINGS WHERE classId = ?", [$classId]);
$rings = json_decode(findClosestDateSort($results, $day)['content'], true); $rings = json_decode(findClosestDateSort($results, $day)['content'], true);
$out = $content; $out = $content;
if ($rings !== null) { if ($rings !== null) {
$out = joinArraysByKey($content, $rings, true); $out = joinArraysByKey($content, $rings, true);
} }
$db = null;
return ['data' => $out, 'type' => 1]; return ['data' => $out, 'type' => 1];
} }
} else { } else {
// return shedules + calls // return shedules + calls
$dayOfWeek = getDayOfWeek($day); $dayOfWeek = getDayOfWeek($day);
$results = FastDBReq($db, "SELECT * FROM schedules WHERE classId = ? AND dayOfWeek = ?", [$classId, $dayOfWeek]); $results = FastDBReq($db, "SELECT content, fromDate FROM schedules WHERE classId = ? AND dayOfWeek = ?", [$classId, $dayOfWeek]);
if (count($results) > 0) { if (count($results) > 0) {
$out = []; $out = [];
$schedule = json_decode(findClosestDateSort($results, $day)['content'], true); $schedule = json_decode(findClosestDateSort($results, $day)['content'], true);
$results = FastDBReq($db, "SELECT * FROM RINGS WHERE classId = ?", [$classId]); $results = FastDBReq($db, "SELECT content, fromDate FROM RINGS WHERE classId = ?", [$classId]);
$rings = json_decode(findClosestDateSort($results, $day)['content'], true); $rings = json_decode(findClosestDateSort($results, $day)['content'], true);
$out = $schedule; $out = $schedule;
if ($rings !== null) { if ($rings !== null) {
$out = joinArraysByKey($schedule, $rings); $out = joinArraysByKey($schedule, $rings);
} }
$db = null;
return ['data' => $out, 'type' => 0]; return ['data' => $out, 'type' => 0];
} else { } else {
$db = null;
return ['data' => []]; return ['data' => []];
} }
} }
@@ -260,9 +256,8 @@ CreateApi(function ($api) {
} }
$adminId = $req->data['adminId']; $adminId = $req->data['adminId'];
$db = db(); $db = db();
$results = FastDBReq($db, "SELECT * FROM admins WHERE adminId=?", [$adminId]); $results = FastDBReq($db, "SELECT adminClass FROM admins WHERE adminId=?", [$adminId]);
if (count($results) == 0) { if (count($results) == 0) {
$db = null;
$req->res->code(400); $req->res->code(400);
return "админ не найден"; return "админ не найден";
# end # end
@@ -277,23 +272,22 @@ CreateApi(function ($api) {
if ($tab != 2) { if ($tab != 2) {
if ($tab == 0) { if ($tab == 0) {
$results = FastDBReq($db, "SELECT * FROM changes WHERE classId = ? AND forDay = ?", [$classId, $day]); $results = FastDBReq($db, "SELECT content FROM changes WHERE classId = ? AND forDay = ?", [$classId, $day]);
if (count($results) > 0) { if (count($results) > 0) {
$changes = json_decode($results[0]['content'], true); $changes = json_decode($results[0]['content'], true);
} }
} }
$dayOfWeek = getDayOfWeek($day); $dayOfWeek = getDayOfWeek($day);
$results = FastDBReq($db, "SELECT * FROM schedules WHERE classId = ? AND dayOfWeek = ?", [$classId, $dayOfWeek]); $results = FastDBReq($db, "SELECT content, fromDate FROM schedules WHERE classId = ? AND dayOfWeek = ?", [$classId, $dayOfWeek]);
if (count($results) > 0) { if (count($results) > 0) {
$schedule = json_decode(findClosestDateSort($results, $day)['content'], true); $schedule = json_decode(findClosestDateSort($results, $day)['content'], true);
} }
} else { } else {
$results = FastDBReq($db, "SELECT * FROM RINGS WHERE classId = ?", [$classId]); $results = FastDBReq($db, "SELECT content, fromDate FROM RINGS WHERE classId = ?", [$classId]);
if (count($results) > 0) { if (count($results) > 0) {
$rings = json_decode(findClosestDateSort($results, $day)['content'], true); $rings = json_decode(findClosestDateSort($results, $day)['content'], true);
} }
} }
$db = null;
switch ($tab) { switch ($tab) {
case 0: case 0:
@@ -321,9 +315,8 @@ CreateApi(function ($api) {
} }
$adminId = $req->data['adminId']; $adminId = $req->data['adminId'];
$db = db(); $db = db();
$results = FastDBReq($db, "SELECT * FROM admins WHERE adminId=?", [$adminId]); $results = FastDBReq($db, "SELECT adminClass FROM admins WHERE adminId=?", [$adminId]);
if (count($results) == 0) { if (count($results) == 0) {
$db = null;
$req->res->code(400); $req->res->code(400);
return "админ не найден"; return "админ не найден";
# end # end
@@ -334,7 +327,6 @@ CreateApi(function ($api) {
$newDataRaw = $req->data['newData']; $newDataRaw = $req->data['newData'];
$newData = json_decode($newDataRaw, true); $newData = json_decode($newDataRaw, true);
// im too lazy to close db
switch ($tab) { switch ($tab) {
case 0: // changes case 0: // changes
@@ -342,7 +334,7 @@ CreateApi(function ($api) {
// if no chnages skip, else publish // if no chnages skip, else publish
$dayOfWeek = getDayOfWeek($day); $dayOfWeek = getDayOfWeek($day);
$results = FastDBReq($db, "SELECT * FROM schedules WHERE classId = ? AND dayOfWeek = ?", [$classId, $dayOfWeek]); $results = FastDBReq($db, "SELECT fromDate, content FROM schedules WHERE classId = ? AND dayOfWeek = ?", [$classId, $dayOfWeek]);
$publish = true; $publish = true;
if (count($results) > 0) { if (count($results) > 0) {
$schedule = json_decode(findClosestDateSort($results, $day)['content'], true); $schedule = json_decode(findClosestDateSort($results, $day)['content'], true);
@@ -352,7 +344,7 @@ CreateApi(function ($api) {
} }
if ($publish) { if ($publish) {
// removing changes for today if they are exists // removing changes for today if they are exists
$results = FastDBReq($db, "SELECT * FROM changes WHERE classId = ? AND forDay = ?", [$classId, $day]); $results = FastDBReq($db, "SELECT content FROM changes WHERE classId = ? AND forDay = ?", [$classId, $day]);
if (count($results) > 0) { if (count($results) > 0) {
$changes = json_decode($results[0]['content'], true); $changes = json_decode($results[0]['content'], true);
if ($newData === $changes) { if ($newData === $changes) {
@@ -395,9 +387,8 @@ CreateApi(function ($api) {
} }
$adminId = $req->data['adminId']; $adminId = $req->data['adminId'];
$db = db(); $db = db();
$results = FastDBReq($db, "SELECT * FROM admins WHERE adminId=?", [$adminId]); $results = FastDBReq($db, "SELECT adminClass FROM admins WHERE adminId=?", [$adminId]);
if (count($results) == 0) { if (count($results) == 0) {
$db = null;
$req->res->code(400); $req->res->code(400);
return "админ не найден"; return "админ не найден";
# end # end
@@ -417,10 +408,8 @@ CreateApi(function ($api) {
FastDBReq($db, 'DELETE FROM rings WHERE classId=? AND fromDate=?', [$classId, $day]); FastDBReq($db, 'DELETE FROM rings WHERE classId=? AND fromDate=?', [$classId, $day]);
break; break;
default: default:
$db = null;
return "you saved your live because you didn't know what tabs are here"; return "you saved your live because you didn't know what tabs are here";
} }
$db = null;
return true; return true;
}); });
$api->on('/update_admin', "POST", function ($req) { $api->on('/update_admin', "POST", function ($req) {
@@ -436,14 +425,12 @@ CreateApi(function ($api) {
} }
$adminId = $req->data['adminId']; $adminId = $req->data['adminId'];
$db = db(); $db = db();
$results = FastDBReq($db, "SELECT * FROM admins WHERE adminId=?", [$adminId]); $results = FastDBReq($db, "SELECT adminPrems FROM admins WHERE adminId=?", [$adminId]);
if (count($results) == 0) { if (count($results) == 0) {
$db = null;
$req->res->code(400); $req->res->code(400);
return "админ не найден"; return "админ не найден";
} }
if (!$withSelf && !in_array($results[0]['adminPrems'], [-1, 2])) { if (!$withSelf && !in_array($results[0]['adminPrems'], [-1, 2])) {
$db = null;
$req->res->code(400); $req->res->code(400);
return 'нету прав'; return 'нету прав';
} }
@@ -462,7 +449,6 @@ CreateApi(function ($api) {
try { try {
FastDBReq($db, 'UPDATE admins SET adminName = ? WHERE adminId = ?', [$value, $adminId]); FastDBReq($db, 'UPDATE admins SET adminName = ? WHERE adminId = ?', [$value, $adminId]);
} catch (PDOException) { } catch (PDOException) {
$db = null;
return "egor"; return "egor";
} }
break; break;
@@ -483,16 +469,13 @@ CreateApi(function ($api) {
try { try {
FastDBReq($db, 'UPDATE admins SET adminName = ? WHERE adminId = ?', [$value, $person]); FastDBReq($db, 'UPDATE admins SET adminName = ? WHERE adminId = ?', [$value, $person]);
} catch (PDOException) { } catch (PDOException) {
$db = null;
return "egor"; return "egor";
} }
break; break;
default: default:
$db = null;
return "?"; return "?";
} }
} }
$db = null;
return true; return true;
}); });
@@ -503,29 +486,26 @@ CreateApi(function ($api) {
} }
$adminId = $req->data['adminId']; $adminId = $req->data['adminId'];
$db = db(); $db = db();
$results = FastDBReq($db, "SELECT * FROM admins WHERE adminId=?", [$adminId]); $results = FastDBReq($db, "SELECT adminPrems, adminClass FROM admins WHERE adminId=?", [$adminId]);
if (count($results) == 0) { if (count($results) == 0) {
$db = null;
$req->res->code(400); $req->res->code(400);
return "админ не найден"; return "админ не найден";
} }
if (!in_array($results[0]['adminPrems'], [-1, 2])) { if (!in_array($results[0]['adminPrems'], [-1, 2])) {
$db = null;
$req->res->code(400); $req->res->code(400);
return "недостаточно прав"; return "недостаточно прав";
} }
$classId = $results[0]['adminClass']; $classId = $results[0]['adminClass'];
$results = FastDBReq($db, "SELECT * FROM classes WHERE classId=?", [$classId]); $results = FastDBReq($db, "SELECT classid FROM classes WHERE classid=?", [$classId]);
if (count($results) == 0) { if (count($results) == 0) {
$db = null;
$req->res->code(400); $req->res->code(400);
return "класса не существует"; return "класса не существует";
} }
$classData = $results[0]; $classData = $results[0];
$adminsData = FastDBReq($db, "SELECT * FROM admins WHERE adminClass=?", [$classId]); $adminsData = FastDBReq($db, "SELECT * FROM admins WHERE adminClass=?", [$classId]);
$db = null;
return ["status" => 1, "class" => $classData, "admins" => $adminsData]; return ["status" => 1, "class" => $classData, "admins" => $adminsData];
}); });
@@ -537,14 +517,12 @@ CreateApi(function ($api) {
} }
$adminId = $req->data['adminId']; $adminId = $req->data['adminId'];
$db = db(); $db = db();
$results = FastDBReq($db, "SELECT * FROM admins WHERE adminId=?", [$adminId]); $results = FastDBReq($db, "SELECT adminPrems, adminClass FROM admins WHERE adminId=?", [$adminId]);
if (count($results) == 0) { if (count($results) == 0) {
$db = null;
$req->res->code(400); $req->res->code(400);
return "админ не найден"; return "админ не найден";
} }
if (!in_array($results[0]['adminPrems'], [-1, 2])) { if (!in_array($results[0]['adminPrems'], [-1, 2])) {
$db = null;
$req->res->code(400); $req->res->code(400);
return "недостаточно прав"; return "недостаточно прав";
} }
@@ -610,7 +588,6 @@ CreateApi(function ($api) {
FastDBReq($db, "DELETE FROM admins WHERE adminId=?", [$person]); FastDBReq($db, "DELETE FROM admins WHERE adminId=?", [$person]);
return true; return true;
default: default:
$db = null;
return "?"; return "?";
} }
}); });
+1 -4
View File
@@ -1,11 +1,8 @@
// CHANGE TO YOUR API DESTINATION // CHANGE TO YOUR API DESTINATION
export const API_BASE_URL = "http://localhost:8321/admin_api.php"; export const API_BASE_URL = "http://localhost:8321/admin_api.php";
// export const API_BASE_URL = "http://192.168.3.10:8321/admin_api.php";
// export const API_BASE_URL = "http://x90620bx.beget.tech/dx4back/admin_api.php"
// TYPE: url/dx4/ // TYPE: url/dx4/
export const CLIENT_FRONTEND_URL = "http://localhost:5173/dx4/"; export const CLIENT_FRONTEND_URL = "http://localhost:5173/";
// export const CLIENT_FRONTEND_URL = "http://x90620bx.beget.tech/dx4/";
import { atom, useAtom } from "jotai"; import { atom, useAtom } from "jotai";
import { atomWithStorage } from "jotai/utils"; import { atomWithStorage } from "jotai/utils";
+1 -1
View File
@@ -11,5 +11,5 @@ export default defineConfig({
}), }),
tailwindcss(), tailwindcss(),
], ],
base: "/dx4dashboard", base: "/",
}); });
-2
View File
@@ -1,7 +1,5 @@
// CHANGE TO YOUR API DESTINATION // CHANGE TO YOUR API DESTINATION
export const API_BASE_URL = "http://localhost:8321/api.php"; export const API_BASE_URL = "http://localhost:8321/api.php";
// export const API_BASE_URL = "http://192.168.3.10:8321/api.php";
// export const API_BASE_URL = "http://x90620bx.beget.tech/dx4back/api.php"
import { atom, useAtom } from "jotai"; import { atom, useAtom } from "jotai";
import { atomWithStorage } from "jotai/utils"; import { atomWithStorage } from "jotai/utils";
+4 -4
View File
@@ -5,7 +5,7 @@ import { VitePWA } from "vite-plugin-pwa";
// https://vite.dev/config/ // https://vite.dev/config/
export default defineConfig({ export default defineConfig({
base: "/dx4/", base: "/",
plugins: [ plugins: [
react({ react({
babel: { babel: {
@@ -19,11 +19,11 @@ export default defineConfig({
name: "dx 4", name: "dx 4",
short_name: "dx4", short_name: "dx4",
description: "dnevnik-x app", description: "dnevnik-x app",
theme_color: "#eee", theme_color: "#000",
background_color: "#000", background_color: "#000",
display: "standalone", display: "standalone",
scope: "/dx4/", scope: "/",
start_url: "/dx4/", start_url: "/",
icons: [ icons: [
{ {
src: "icons/windows11/SmallTile.scale-100.png", src: "icons/windows11/SmallTile.scale-100.png",