Merge branch 'phppostclass' into 'master'

new class based syntax for handling api requests in php

See merge request lukas/openmediacenter!9
This commit is contained in:
Lukas Heiligenbrunner 2020-07-30 21:14:38 +00:00
commit 748f0410de
4 changed files with 136 additions and 98 deletions

41
api/RequestBase.php Normal file
View File

@ -0,0 +1,41 @@
<?php
require_once 'Database.php';
abstract class RequestBase {
private array $actions = array();
protected mysqli $conn;
/**
* add the action handlers in this abstract method
*/
abstract function initHandlers();
/**
* adds a new action handler to the current api file
*
* @param $action string name of the action variable
* @param $callback Closure callback function to be called
*/
function addActionHandler($action, $callback) {
$this->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->initHandlers();
$action = $_POST['action'];
// call the right handler
$this->actions[$action]();
} else {
echo('{data:"error"}');
}
}
}

View File

@ -1,29 +1,22 @@
<?php <?php
require 'Database.php'; require 'RequestBase.php';
require 'SSettings.php';
$conn = Database::getInstance()->getConnection(); class Settings extends RequestBase {
$settings = new SSettings(); function initHandlers() {
$this->addActionHandler("loadGeneralSettings", function () {
if (isset($_POST['action'])) {
$action = $_POST['action'];
switch ($action) {
case "loadGeneralSettings":
$query = "SELECT * from settings"; $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); $r = mysqli_fetch_assoc($result);
// booleans need to be set manually // booleans need to be set manually
$r['passwordEnabled'] = $r['password'] != "-1"; $r['passwordEnabled'] = $r['password'] != "-1";
$r['TMDB_grabbing'] = ($r['TMDB_grabbing'] != '0'); $r['TMDB_grabbing'] = ($r['TMDB_grabbing'] != '0');
echo json_encode($r); echo json_encode($r);
break; });
case "saveGeneralSettings":
$this->addActionHandler("saveGeneralSettings", function () {
$mediacentername = $_POST['mediacentername']; $mediacentername = $_POST['mediacentername'];
$password = $_POST['password']; $password = $_POST['password'];
$videopath = $_POST['videopath']; $videopath = $_POST['videopath'];
@ -38,19 +31,17 @@ if (isset($_POST['action'])) {
TMDB_grabbing=$tmdbsupport TMDB_grabbing=$tmdbsupport
WHERE 1"; WHERE 1";
if ($conn->query($query) === true) { if ($this->conn->query($query) === true) {
echo '{"success": true}'; echo '{"success": true}';
} else { } else {
echo '{"success": true}'; echo '{"success": true}';
} }
break; });
case "loadInitialData":
$this->addActionHandler("loadInitialData", function () {
$query = "SELECT * from settings"; $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); $r = mysqli_fetch_assoc($result);
if ($r['password'] != "-1") { if ($r['password'] != "-1") {
@ -60,6 +51,9 @@ if (isset($_POST['action'])) {
} }
unset($r['password']); unset($r['password']);
echo json_encode($r); echo json_encode($r);
break; });
} }
} }
$sett = new Settings();
$sett->handleAction();

View File

@ -1,31 +1,30 @@
<?php <?php
require 'Database.php'; require_once 'RequestBase.php';
$conn = Database::getInstance()->getConnection(); class Tags extends RequestBase {
function initHandlers() {
if (isset($_POST['action'])) { $this->addActionHandler("getAllTags", function () {
$action = $_POST['action'];
switch ($action) {
case "getAllTags":
$query = "SELECT tag_name,tag_id from tags"; $query = "SELECT tag_name,tag_id from tags";
$result = $conn->query($query); $result = $this->conn->query($query);
$rows = array(); $rows = array();
while ($r = mysqli_fetch_assoc($result)) { while ($r = mysqli_fetch_assoc($result)) {
array_push($rows, $r); array_push($rows, $r);
} }
echo json_encode($rows); echo json_encode($rows);
});
break; $this->addActionHandler("createTag", function (){
case "createTag":
$query = "INSERT INTO tags (tag_name) VALUES ('" . $_POST['tagname'] . "')"; $query = "INSERT INTO tags (tag_name) VALUES ('" . $_POST['tagname'] . "')";
if ($conn->query($query) === TRUE) { if ($this->conn->query($query) === TRUE) {
echo('{"result":"success"}'); echo('{"result":"success"}');
} else { } else {
echo('{"result":"' . $conn->error . '"}'); echo('{"result":"' . $this->conn->error . '"}');
} }
break; });
} }
} }
$tags = new Tags();
$tags->handleAction();

View File

@ -1,20 +1,20 @@
<?php <?php
require 'Database.php'; require_once 'Database.php';
require 'SSettings.php'; require_once 'SSettings.php';
require_once 'RequestBase.php';
// establish initial db connection class Video extends RequestBase {
$conn = Database::getInstance()->getConnection(); private string $videopath;
$settings = new SSettings();
// load video path from settings public function __construct() {
$videopath = $settings->getVideoPath(); $settings = new SSettings();
// load video path from settings
$this->videopath = $settings->getVideoPath();
}
function initHandlers() {
if (isset($_POST['action'])) { $this->addActionHandler("getMovies", function () {
$action = $_POST['action']; $query = "SELECT movie_id,movie_name FROM videos ORDER BY create_date DESC, movie_name";
switch ($action) {
case "getMovies":
$query = "SELECT movie_id,movie_name FROM videos ORDER BY create_date DESC, movie_name ASC";
if (isset($_POST['tag'])) { if (isset($_POST['tag'])) {
$tag = $_POST['tag']; $tag = $_POST['tag'];
if ($_POST['tag'] != "all") { if ($_POST['tag'] != "all") {
@ -22,21 +22,22 @@ if (isset($_POST['action'])) {
INNER JOIN video_tags vt on videos.movie_id = vt.video_id INNER JOIN video_tags vt on videos.movie_id = vt.video_id
INNER JOIN tags t on vt.tag_id = t.tag_id INNER JOIN tags t on vt.tag_id = t.tag_id
WHERE t.tag_name = '$tag' WHERE t.tag_name = '$tag'
ORDER BY likes DESC, create_date ASC, movie_name ASC"; ORDER BY likes DESC, create_date, movie_name";
} }
} }
$result = $conn->query($query); $result = $this->conn->query($query);
$rows = array(); $rows = array();
while ($r = mysqli_fetch_assoc($result)) { while ($r = mysqli_fetch_assoc($result)) {
array_push($rows, $r); array_push($rows, $r);
} }
echo(json_encode($rows)); echo(json_encode($rows));
break; });
case "getRandomMovies":
$this->addActionHandler("getRandomMovies", function () {
$return = new stdClass(); $return = new stdClass();
$query = "SELECT movie_id,movie_name FROM videos ORDER BY RAND() LIMIT " . $_POST['number']; $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(); $return->rows = array();
// get tags of random videos // 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 INNER JOIN tags t on video_tags.tag_id = t.tag_id
WHERE $idstring WHERE $idstring
GROUP BY t.tag_name"; GROUP BY t.tag_name";
$result = $conn->query($query); $result = $this->conn->query($query);
while ($r = mysqli_fetch_assoc($result)) { while ($r = mysqli_fetch_assoc($result)) {
array_push($return->tags, $r); array_push($return->tags, $r);
} }
echo(json_encode($return)); echo(json_encode($return));
break; });
case "getSearchKeyWord":
$this->addActionHandler("getSearchKeyWord", function () {
$search = $_POST['keyword']; $search = $_POST['keyword'];
$query = "SELECT movie_id,movie_name FROM videos $query = "SELECT movie_id,movie_name FROM videos
WHERE movie_name LIKE '%$search%' 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 = $conn->query($query); $result = $this->conn->query($query);
$rows = array(); $rows = array();
while ($r = mysqli_fetch_assoc($result)) { while ($r = mysqli_fetch_assoc($result)) {
array_push($rows, $r); array_push($rows, $r);
} }
echo(json_encode($rows)); echo(json_encode($rows));
});
break; $this->addActionHandler("loadVideo", function () {
case "loadVideo":
$query = "SELECT movie_name,movie_id,movie_url,thumbnail,poster,likes,quality,length FROM videos WHERE movie_id='" . $_POST['movieid'] . "'"; $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(); $row = $result->fetch_assoc();
$arr = array(); $arr = array();
@ -92,7 +94,7 @@ if (isset($_POST['action'])) {
$arr["movie_name"] = $row["movie_name"]; $arr["movie_name"] = $row["movie_name"];
// todo drop video url from db -- maybe one with and one without extension // todo drop video url from db -- maybe one with and one without extension
// extension hardcoded here!!! // 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["likes"] = $row["likes"];
$arr["quality"] = $row["quality"]; $arr["quality"] = $row["quality"];
$arr["length"] = $row["length"]; $arr["length"] = $row["length"];
@ -103,15 +105,15 @@ if (isset($_POST['action'])) {
INNER JOIN tags t on video_tags.tag_id = t.tag_id INNER JOIN tags t on video_tags.tag_id = t.tag_id
WHERE video_tags.video_id=" . $_POST['movieid'] . " WHERE video_tags.video_id=" . $_POST['movieid'] . "
GROUP BY t.tag_name"; GROUP BY t.tag_name";
$result = $conn->query($query); $result = $this->conn->query($query);
while ($r = mysqli_fetch_assoc($result)) { while ($r = mysqli_fetch_assoc($result)) {
array_push($arr['tags'], $r); array_push($arr['tags'], $r);
} }
echo(json_encode($arr)); echo(json_encode($arr));
});
break; $this->addActionHandler("getDbSize", function () {
case "getDbSize":
$dbname = Database::getInstance()->getDatabaseName(); $dbname = Database::getInstance()->getDatabaseName();
$query = "SELECT table_schema AS \"Database\", $query = "SELECT table_schema AS \"Database\",
@ -119,24 +121,24 @@ if (isset($_POST['action'])) {
FROM information_schema.TABLES FROM information_schema.TABLES
WHERE TABLE_SCHEMA='$dbname' WHERE TABLE_SCHEMA='$dbname'
GROUP BY table_schema;"; GROUP BY table_schema;";
$result = $conn->query($query); $result = $this->conn->query($query);
if ($result->num_rows == 1) { if ($result->num_rows == 1) {
$row = $result->fetch_assoc(); $row = $result->fetch_assoc();
echo '{"data":"' . $row["Size"] . 'MB"}'; echo '{"data":"' . $row["Size"] . 'MB"}';
} }
});
break; $this->addActionHandler("readThumbnail", function () {
case "readThumbnail":
$query = "SELECT thumbnail FROM videos WHERE movie_id='" . $_POST['movieid'] . "'"; $query = "SELECT thumbnail FROM videos WHERE movie_id='" . $_POST['movieid'] . "'";
$result = $conn->query($query); $result = $this->conn->query($query);
$row = $result->fetch_assoc(); $row = $result->fetch_assoc();
echo($row["thumbnail"]); echo($row["thumbnail"]);
});
break; $this->addActionHandler("getTags", function () {
case "getTags":
// todo add this to loadVideo maybe // todo add this to loadVideo maybe
$movieid = $_POST['movieid']; $movieid = $_POST['movieid'];
@ -144,7 +146,7 @@ if (isset($_POST['action'])) {
INNER JOIN tags t on video_tags.tag_id = t.tag_id INNER JOIN tags t on video_tags.tag_id = t.tag_id
WHERE video_id='$movieid'"; WHERE video_id='$movieid'";
$result = $conn->query($query); $result = $this->conn->query($query);
$rows = array(); $rows = array();
$rows['tags'] = array(); $rows['tags'] = array();
@ -153,21 +155,23 @@ if (isset($_POST['action'])) {
} }
echo(json_encode($rows)); echo(json_encode($rows));
break; });
case "addLike":
$this->addActionHandler("addLike", function () {
$movieid = $_POST['movieid']; $movieid = $_POST['movieid'];
$query = "update videos set likes = likes + 1 where movie_id = '$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"}'); echo('{"result":"success"}');
} else { } else {
echo('{"result":"' . $conn->error . '"}'); echo('{"result":"' . $this->conn->error . '"}');
} }
break; });
case "getStartData":
$this->addActionHandler("getStartData", function () {
$query = "SELECT COUNT(*) as nr FROM videos"; $query = "SELECT COUNT(*) as nr FROM videos";
$result = $conn->query($query); $result = $this->conn->query($query);
$r = mysqli_fetch_assoc($result); $r = mysqli_fetch_assoc($result);
$arr = array(); $arr = array();
@ -176,7 +180,7 @@ if (isset($_POST['action'])) {
$query = "SELECT COUNT(*) as nr FROM videos $query = "SELECT COUNT(*) as nr FROM videos
INNER JOIN video_tags vt on videos.movie_id = vt.video_id INNER JOIN video_tags vt on videos.movie_id = vt.video_id
INNER JOIN tags t on vt.tag_id = t.tag_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); $r = mysqli_fetch_assoc($result);
$arr['tagged'] = $r['nr']; $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 video_tags vt on videos.movie_id = vt.video_id
INNER JOIN tags t on vt.tag_id = t.tag_id INNER JOIN tags t on vt.tag_id = t.tag_id
WHERE t.tag_name='hd'"; WHERE t.tag_name='hd'";
$result = $conn->query($query); $result = $this->conn->query($query);
$r = mysqli_fetch_assoc($result); $r = mysqli_fetch_assoc($result);
$arr['hd'] = $r['nr']; $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 video_tags vt on videos.movie_id = vt.video_id
INNER JOIN tags t on vt.tag_id = t.tag_id INNER JOIN tags t on vt.tag_id = t.tag_id
WHERE t.tag_name='fullhd'"; WHERE t.tag_name='fullhd'";
$result = $conn->query($query); $result = $this->conn->query($query);
$r = mysqli_fetch_assoc($result); $r = mysqli_fetch_assoc($result);
$arr['fullhd'] = $r['nr']; $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 video_tags vt on videos.movie_id = vt.video_id
INNER JOIN tags t on vt.tag_id = t.tag_id INNER JOIN tags t on vt.tag_id = t.tag_id
WHERE t.tag_name='lowquality'"; WHERE t.tag_name='lowquality'";
$result = $conn->query($query); $result = $this->conn->query($query);
$r = mysqli_fetch_assoc($result); $r = mysqli_fetch_assoc($result);
$arr['sd'] = $r['nr']; $arr['sd'] = $r['nr'];
$query = "SELECT COUNT(*) as nr FROM tags"; $query = "SELECT COUNT(*) as nr FROM tags";
$result = $conn->query($query); $result = $this->conn->query($query);
$r = mysqli_fetch_assoc($result); $r = mysqli_fetch_assoc($result);
$arr['tags'] = $r['nr']; $arr['tags'] = $r['nr'];
echo(json_encode($arr)); echo(json_encode($arr));
break; });
case "getAllTags": $this->addActionHandler("getAllTags", function () {
$query = "SELECT tag_name,tag_id from tags"; $query = "SELECT tag_name,tag_id from tags";
$result = $conn->query($query); $result = $this->conn->query($query);
$rows = array(); $rows = array();
while ($r = mysqli_fetch_assoc($result)) { while ($r = mysqli_fetch_assoc($result)) {
array_push($rows, $r); array_push($rows, $r);
} }
echo(json_encode($rows)); echo(json_encode($rows));
break; });
case "addTag":
$this->addActionHandler("addTag", function () {
$movieid = $_POST['movieid']; $movieid = $_POST['movieid'];
$tagid = $_POST['id']; $tagid = $_POST['id'];
$query = "INSERT INTO video_tags(tag_id, video_id) VALUES ('$tagid','$movieid')"; $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"}'); echo('{"result":"success"}');
} else { } else {
echo('{"result":"' . $conn->error . '"}'); echo('{"result":"' . $this->conn->error . '"}');
} }
break; });
} }
} else {
echo('{data:"error"}');
} }
return;
$video = new Video();
$video->handleAction();