From fd9a54209ddfbe549407eb0181f7cfa920f1be00 Mon Sep 17 00:00:00 2001 From: Lukas Heiligenbrunner Date: Wed, 29 Jul 2020 23:42:36 +0200 Subject: [PATCH 1/3] new class based syntax for handling api requests in php --- api/RequestBase.php | 24 ++++++++++++++++++++++++ api/Settings.php | 36 ++++++++++++++++++------------------ 2 files changed, 42 insertions(+), 18 deletions(-) create mode 100644 api/RequestBase.php diff --git a/api/RequestBase.php b/api/RequestBase.php new file mode 100644 index 0000000..e95c62b --- /dev/null +++ b/api/RequestBase.php @@ -0,0 +1,24 @@ +actions[$action] = $callback; + } + + function handleAction() { + $this->conn = Database::getInstance()->getConnection(); + + if (isset($_POST['action'])) { + $this->initIdentifiers(); + + $action = $_POST['action']; + call_user_func($this->actions[$action]); + } + } +} diff --git a/api/Settings.php b/api/Settings.php index 273446e..3fbe1e9 100644 --- a/api/Settings.php +++ b/api/Settings.php @@ -1,29 +1,25 @@ getConnection(); -$settings = new SSettings(); - -if (isset($_POST['action'])) { - $action = $_POST['action']; - switch ($action) { - case "loadGeneralSettings": +class Settings extends RequestBase { + function initIdentifiers() { + $this->addIdentifier("loadGeneralSettings", function () { $query = "SELECT * from settings"; - $result = $conn->query($query); + $result = $this->conn->query($query); if ($result->num_rows > 1) { // todo throw error } $r = mysqli_fetch_assoc($result); // booleans need to be set manually - $r['passwordEnabled'] = $r['password'] != "-1"; + $r['passwordEnabled'] = $r['password'] != "-1"; $r['TMDB_grabbing'] = ($r['TMDB_grabbing'] != '0'); echo json_encode($r); - break; - case "saveGeneralSettings": + }); + + $this->addIdentifier("saveGeneralSettings", function () { $mediacentername = $_POST['mediacentername']; $password = $_POST['password']; $videopath = $_POST['videopath']; @@ -38,16 +34,17 @@ if (isset($_POST['action'])) { TMDB_grabbing=$tmdbsupport WHERE 1"; - if ($conn->query($query) === true) { + if ($this->conn->query($query) === true) { echo '{"success": true}'; } else { echo '{"success": true}'; } - break; - case "loadInitialData": + }); + + $this->addIdentifier("loadInitialData", function () { $query = "SELECT * from settings"; - $result = $conn->query($query); + $result = $this->conn->query($query); if ($result->num_rows > 1) { // todo throw error } @@ -60,6 +57,9 @@ if (isset($_POST['action'])) { } unset($r['password']); echo json_encode($r); - break; + }); } } + +$sett = new Settings(); +$sett->handleAction(); From a2385e8e4c5f2b8146b0b76ff32f8da49e6c219e Mon Sep 17 00:00:00 2001 From: Lukas Heiligenbrunner Date: Fri, 31 Jul 2020 01:03:51 +0200 Subject: [PATCH 2/3] classify videoload and Tag requests --- api/RequestBase.php | 27 ++++++++-- api/Settings.php | 8 +-- api/Tags.php | 27 +++++----- api/videoload.php | 118 +++++++++++++++++++++++--------------------- 4 files changed, 100 insertions(+), 80 deletions(-) diff --git a/api/RequestBase.php b/api/RequestBase.php index e95c62b..3eea2ec 100644 --- a/api/RequestBase.php +++ b/api/RequestBase.php @@ -1,24 +1,41 @@ actions[$action] = $callback; } + /** + * runs the correct handler + * should be called once within the api request + */ function handleAction() { $this->conn = Database::getInstance()->getConnection(); if (isset($_POST['action'])) { - $this->initIdentifiers(); + $this->initHandlers(); $action = $_POST['action']; - call_user_func($this->actions[$action]); + + // call the right handler + $this->actions[$action](); + } else { + echo('{data:"error"}'); } } } diff --git a/api/Settings.php b/api/Settings.php index 3fbe1e9..c0afc23 100644 --- a/api/Settings.php +++ b/api/Settings.php @@ -2,8 +2,8 @@ require 'RequestBase.php'; class Settings extends RequestBase { - function initIdentifiers() { - $this->addIdentifier("loadGeneralSettings", function () { + function initHandlers() { + $this->addActionHandler("loadGeneralSettings", function () { $query = "SELECT * from settings"; $result = $this->conn->query($query); @@ -19,7 +19,7 @@ class Settings extends RequestBase { echo json_encode($r); }); - $this->addIdentifier("saveGeneralSettings", function () { + $this->addActionHandler("saveGeneralSettings", function () { $mediacentername = $_POST['mediacentername']; $password = $_POST['password']; $videopath = $_POST['videopath']; @@ -41,7 +41,7 @@ class Settings extends RequestBase { } }); - $this->addIdentifier("loadInitialData", function () { + $this->addActionHandler("loadInitialData", function () { $query = "SELECT * from settings"; $result = $this->conn->query($query); diff --git a/api/Tags.php b/api/Tags.php index fcc63b7..2953419 100644 --- a/api/Tags.php +++ b/api/Tags.php @@ -1,31 +1,30 @@ getConnection(); - -if (isset($_POST['action'])) { - $action = $_POST['action']; - switch ($action) { - case "getAllTags": +class Tags extends RequestBase { + function initHandlers() { + $this->addActionHandler("getAllTags", function () { $query = "SELECT tag_name,tag_id from tags"; - $result = $conn->query($query); + $result = $this->conn->query($query); $rows = array(); while ($r = mysqli_fetch_assoc($result)) { array_push($rows, $r); } echo json_encode($rows); + }); - break; - - case "createTag": + $this->addActionHandler("createTag", function (){ $query = "INSERT INTO tags (tag_name) VALUES ('" . $_POST['tagname'] . "')"; - if ($conn->query($query) === TRUE) { + if ($this->conn->query($query) === TRUE) { echo('{"result":"success"}'); } else { - echo('{"result":"' . $conn->error . '"}'); + echo('{"result":"' . $this->conn->error . '"}'); } - break; + }); } } + +$tags = new Tags(); +$tags->handleAction(); diff --git a/api/videoload.php b/api/videoload.php index c2070a9..0119650 100755 --- a/api/videoload.php +++ b/api/videoload.php @@ -1,19 +1,19 @@ getConnection(); -$settings = new SSettings(); +class Video extends RequestBase { + private string $videopath; -// load video path from settings -$videopath = $settings->getVideoPath(); + public function __construct() { + $settings = new SSettings(); + // load video path from settings + $this->videopath = $settings->getVideoPath(); + } - -if (isset($_POST['action'])) { - $action = $_POST['action']; - switch ($action) { - case "getMovies": + function initHandlers() { + $this->addActionHandler("getMovies", function () { $query = "SELECT movie_id,movie_name FROM videos ORDER BY create_date DESC, movie_name ASC"; if (isset($_POST['tag'])) { $tag = $_POST['tag']; @@ -25,18 +25,19 @@ if (isset($_POST['action'])) { ORDER BY likes DESC, create_date ASC, movie_name ASC"; } } - $result = $conn->query($query); + $result = $this->conn->query($query); $rows = array(); while ($r = mysqli_fetch_assoc($result)) { array_push($rows, $r); } echo(json_encode($rows)); - break; - case "getRandomMovies": + }); + + $this->addActionHandler("getRandomMovies", function () { $return = new stdClass(); $query = "SELECT movie_id,movie_name FROM videos ORDER BY RAND() LIMIT " . $_POST['number']; - $result = $conn->query($query); + $result = $this->conn->query($query); $return->rows = array(); // get tags of random videos @@ -53,32 +54,33 @@ if (isset($_POST['action'])) { INNER JOIN tags t on video_tags.tag_id = t.tag_id WHERE $idstring GROUP BY t.tag_name"; - $result = $conn->query($query); + $result = $this->conn->query($query); while ($r = mysqli_fetch_assoc($result)) { array_push($return->tags, $r); } echo(json_encode($return)); - break; - case "getSearchKeyWord": + }); + + $this->addActionHandler("getSearchKeyWord", function () { $search = $_POST['keyword']; $query = "SELECT movie_id,movie_name FROM videos WHERE movie_name LIKE '%$search%' ORDER BY likes DESC, create_date DESC, movie_name ASC"; - $result = $conn->query($query); + $result = $this->conn->query($query); $rows = array(); while ($r = mysqli_fetch_assoc($result)) { array_push($rows, $r); } echo(json_encode($rows)); + }); - break; - case "loadVideo": + $this->addActionHandler("loadVideo", function () { $query = "SELECT movie_name,movie_id,movie_url,thumbnail,poster,likes,quality,length FROM videos WHERE movie_id='" . $_POST['movieid'] . "'"; - $result = $conn->query($query); + $result = $this->conn->query($query); $row = $result->fetch_assoc(); $arr = array(); @@ -92,7 +94,7 @@ if (isset($_POST['action'])) { $arr["movie_name"] = $row["movie_name"]; // todo drop video url from db -- maybe one with and one without extension // extension hardcoded here!!! - $arr["movie_url"] = str_replace("?", "%3F", $videopath . $row["movie_name"] . ".mp4"); + $arr["movie_url"] = str_replace("?", "%3F", $this->videopath . $row["movie_name"] . ".mp4"); $arr["likes"] = $row["likes"]; $arr["quality"] = $row["quality"]; $arr["length"] = $row["length"]; @@ -103,15 +105,15 @@ if (isset($_POST['action'])) { INNER JOIN tags t on video_tags.tag_id = t.tag_id WHERE video_tags.video_id=" . $_POST['movieid'] . " GROUP BY t.tag_name"; - $result = $conn->query($query); + $result = $this->conn->query($query); while ($r = mysqli_fetch_assoc($result)) { array_push($arr['tags'], $r); } echo(json_encode($arr)); + }); - break; - case "getDbSize": + $this->addActionHandler("getDbSize", function () { $dbname = Database::getInstance()->getDatabaseName(); $query = "SELECT table_schema AS \"Database\", @@ -119,24 +121,24 @@ if (isset($_POST['action'])) { FROM information_schema.TABLES WHERE TABLE_SCHEMA='$dbname' GROUP BY table_schema;"; - $result = $conn->query($query); + $result = $this->conn->query($query); if ($result->num_rows == 1) { $row = $result->fetch_assoc(); echo '{"data":"' . $row["Size"] . 'MB"}'; } + }); - break; - case "readThumbnail": + $this->addActionHandler("readThumbnail", function () { $query = "SELECT thumbnail FROM videos WHERE movie_id='" . $_POST['movieid'] . "'"; - $result = $conn->query($query); + $result = $this->conn->query($query); $row = $result->fetch_assoc(); echo($row["thumbnail"]); + }); - break; - case "getTags": + $this->addActionHandler("getTags", function () { // todo add this to loadVideo maybe $movieid = $_POST['movieid']; @@ -144,7 +146,7 @@ if (isset($_POST['action'])) { INNER JOIN tags t on video_tags.tag_id = t.tag_id WHERE video_id='$movieid'"; - $result = $conn->query($query); + $result = $this->conn->query($query); $rows = array(); $rows['tags'] = array(); @@ -153,21 +155,23 @@ if (isset($_POST['action'])) { } echo(json_encode($rows)); - break; - case "addLike": + }); + + $this->addActionHandler("addLike", function () { $movieid = $_POST['movieid']; $query = "update videos set likes = likes + 1 where movie_id = '$movieid'"; - if ($conn->query($query) === TRUE) { + if ($this->conn->query($query) === TRUE) { echo('{"result":"success"}'); } else { - echo('{"result":"' . $conn->error . '"}'); + echo('{"result":"' . $this->conn->error . '"}'); } - break; - case "getStartData": + }); + + $this->addActionHandler("getStartData", function () { $query = "SELECT COUNT(*) as nr FROM videos"; - $result = $conn->query($query); + $result = $this->conn->query($query); $r = mysqli_fetch_assoc($result); $arr = array(); @@ -176,7 +180,7 @@ if (isset($_POST['action'])) { $query = "SELECT COUNT(*) as nr FROM videos INNER JOIN video_tags vt on videos.movie_id = vt.video_id INNER JOIN tags t on vt.tag_id = t.tag_id"; - $result = $conn->query($query); + $result = $this->conn->query($query); $r = mysqli_fetch_assoc($result); $arr['tagged'] = $r['nr']; @@ -184,7 +188,7 @@ if (isset($_POST['action'])) { INNER JOIN video_tags vt on videos.movie_id = vt.video_id INNER JOIN tags t on vt.tag_id = t.tag_id WHERE t.tag_name='hd'"; - $result = $conn->query($query); + $result = $this->conn->query($query); $r = mysqli_fetch_assoc($result); $arr['hd'] = $r['nr']; @@ -192,7 +196,7 @@ if (isset($_POST['action'])) { INNER JOIN video_tags vt on videos.movie_id = vt.video_id INNER JOIN tags t on vt.tag_id = t.tag_id WHERE t.tag_name='fullhd'"; - $result = $conn->query($query); + $result = $this->conn->query($query); $r = mysqli_fetch_assoc($result); $arr['fullhd'] = $r['nr']; @@ -200,43 +204,43 @@ if (isset($_POST['action'])) { INNER JOIN video_tags vt on videos.movie_id = vt.video_id INNER JOIN tags t on vt.tag_id = t.tag_id WHERE t.tag_name='lowquality'"; - $result = $conn->query($query); + $result = $this->conn->query($query); $r = mysqli_fetch_assoc($result); $arr['sd'] = $r['nr']; $query = "SELECT COUNT(*) as nr FROM tags"; - $result = $conn->query($query); + $result = $this->conn->query($query); $r = mysqli_fetch_assoc($result); $arr['tags'] = $r['nr']; echo(json_encode($arr)); - break; + }); - case "getAllTags": + $this->addActionHandler("getAllTags", function () { $query = "SELECT tag_name,tag_id from tags"; - $result = $conn->query($query); + $result = $this->conn->query($query); $rows = array(); while ($r = mysqli_fetch_assoc($result)) { array_push($rows, $r); } echo(json_encode($rows)); - break; - case "addTag": + }); + + $this->addActionHandler("addTag", function () { $movieid = $_POST['movieid']; $tagid = $_POST['id']; $query = "INSERT INTO video_tags(tag_id, video_id) VALUES ('$tagid','$movieid')"; - if ($conn->query($query) === TRUE) { + if ($this->conn->query($query) === TRUE) { echo('{"result":"success"}'); } else { - echo('{"result":"' . $conn->error . '"}'); + echo('{"result":"' . $this->conn->error . '"}'); } - break; + }); } -} else { - echo('{data:"error"}'); } -return; +$video = new Video(); +$video->handleAction(); From 08df6d64dd365f02cc767ecae512f0457db5f979 Mon Sep 17 00:00:00 2001 From: Lukas Heiligenbrunner Date: Fri, 31 Jul 2020 01:05:58 +0200 Subject: [PATCH 3/3] delete unnecessary ordering in sql statements --- api/Settings.php | 6 ------ api/videoload.php | 6 +++--- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/api/Settings.php b/api/Settings.php index c0afc23..f45410c 100644 --- a/api/Settings.php +++ b/api/Settings.php @@ -7,9 +7,6 @@ class Settings extends RequestBase { $query = "SELECT * from settings"; $result = $this->conn->query($query); - if ($result->num_rows > 1) { - // todo throw error - } $r = mysqli_fetch_assoc($result); // booleans need to be set manually @@ -45,9 +42,6 @@ class Settings extends RequestBase { $query = "SELECT * from settings"; $result = $this->conn->query($query); - if ($result->num_rows > 1) { - // todo throw error - } $r = mysqli_fetch_assoc($result); if ($r['password'] != "-1") { diff --git a/api/videoload.php b/api/videoload.php index 0119650..b2c2e0f 100755 --- a/api/videoload.php +++ b/api/videoload.php @@ -14,7 +14,7 @@ class Video extends RequestBase { function initHandlers() { $this->addActionHandler("getMovies", function () { - $query = "SELECT movie_id,movie_name FROM videos ORDER BY create_date DESC, movie_name ASC"; + $query = "SELECT movie_id,movie_name FROM videos ORDER BY create_date DESC, movie_name"; if (isset($_POST['tag'])) { $tag = $_POST['tag']; if ($_POST['tag'] != "all") { @@ -22,7 +22,7 @@ class Video extends RequestBase { INNER JOIN video_tags vt on videos.movie_id = vt.video_id INNER JOIN tags t on vt.tag_id = t.tag_id WHERE t.tag_name = '$tag' - ORDER BY likes DESC, create_date ASC, movie_name ASC"; + ORDER BY likes DESC, create_date, movie_name"; } } $result = $this->conn->query($query); @@ -67,7 +67,7 @@ class Video extends RequestBase { $query = "SELECT movie_id,movie_name FROM videos WHERE movie_name LIKE '%$search%' - ORDER BY likes DESC, create_date DESC, movie_name ASC"; + ORDER BY likes DESC, create_date DESC, movie_name"; $result = $this->conn->query($query); $rows = array(); while ($r = mysqli_fetch_assoc($result)) {