new folder structure for php scripts
renamed api nodes php braces on same line
This commit is contained in:
57
api/src/Database.php
Normal file
57
api/src/Database.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class Database
|
||||
*
|
||||
* Class with all neccessary stuff for the Database connections.
|
||||
*/
|
||||
class Database {
|
||||
private static ?Database $instance = null;
|
||||
private mysqli $conn;
|
||||
|
||||
private string $servername = "192.168.0.30";
|
||||
private string $username = "root";
|
||||
private string $password = "1qayxsw2";
|
||||
private string $dbname = "mediacenter";
|
||||
|
||||
// The db connection is established in the private constructor.
|
||||
private function __construct() {
|
||||
// Create connection
|
||||
$this->conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname);
|
||||
|
||||
if ($this->conn->connect_errno) {
|
||||
echo "connecton failed... nr: " . $this->conn->connect_errno . " -- " . $this->conn->connect_error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* get an instance of this database class
|
||||
* (only possible way to retrieve an object)
|
||||
*
|
||||
* @return Database dbobject
|
||||
*/
|
||||
public static function getInstance() {
|
||||
if (!self::$instance) {
|
||||
self::$instance = new Database();
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* get a connection instance of the database
|
||||
*
|
||||
* @return mysqli mysqli instance
|
||||
*/
|
||||
public function getConnection() {
|
||||
return $this->conn;
|
||||
}
|
||||
|
||||
/**
|
||||
* get name of current active database
|
||||
* @return string name
|
||||
*/
|
||||
public function getDatabaseName() {
|
||||
return $this->dbname;
|
||||
}
|
||||
}
|
45
api/src/SSettings.php
Normal file
45
api/src/SSettings.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class SSettings
|
||||
* class handling all Settings used by php scripts
|
||||
*/
|
||||
class SSettings {
|
||||
private ?Database $database;
|
||||
|
||||
/**
|
||||
* SSettings constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->database = Database::getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* get the videopath saved in db
|
||||
* @return string videopath
|
||||
*/
|
||||
public function getVideoPath() {
|
||||
$query = "SELECT video_path from settings";
|
||||
|
||||
$result = $this->database->getConnection()->query($query);
|
||||
|
||||
$r = mysqli_fetch_assoc($result);
|
||||
return $r['video_path'];
|
||||
}
|
||||
|
||||
/**
|
||||
* check if TMDB is enableds
|
||||
* @return bool isenabled?
|
||||
*/
|
||||
public function isTMDBGrabbingEnabled(): bool {
|
||||
$query = "SELECT TMDB_grabbing from settings";
|
||||
|
||||
$result = $this->database->getConnection()->query($query);
|
||||
if (!$result) {
|
||||
return true; // if undefined in db --> default true
|
||||
} else {
|
||||
$r = mysqli_fetch_assoc($result);
|
||||
return $r['TMDB_grabbing'] == '1';
|
||||
}
|
||||
}
|
||||
}
|
38
api/src/TMDBMovie.php
Normal file
38
api/src/TMDBMovie.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class TMDBMovie
|
||||
* class to handle all interactions with the tmdb api
|
||||
*/
|
||||
class TMDBMovie {
|
||||
public $picturebase = "https://image.tmdb.org/t/p/w500";
|
||||
private $apikey = "9fd90530b11447f5646f8e6fb4733fb4";
|
||||
private $baseurl = "https://api.themoviedb.org/3/";
|
||||
|
||||
/**
|
||||
* search for a specific movie
|
||||
*
|
||||
* @param string $moviename moviename
|
||||
* @return object movie object or null if not found
|
||||
*/
|
||||
public function searchMovie(string $moviename) {
|
||||
$reply = json_decode(file_get_contents($this->baseurl . "search/movie?api_key=" . $this->apikey . "&query=" . urlencode($moviename)));
|
||||
if ($reply->total_results == 0) {
|
||||
// no results found
|
||||
// todo maybe parse first pictures somehow
|
||||
return null;
|
||||
} else {
|
||||
return $reply->results[0];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* query all available genres from tmdb
|
||||
*
|
||||
* @return array of all available genres
|
||||
*/
|
||||
public function getAllGenres() {
|
||||
$reply = json_decode(file_get_contents($this->baseurl . "genre/movie/list?api_key=" . $this->apikey));
|
||||
return $reply->genres;
|
||||
}
|
||||
}
|
50
api/src/handlers/RequestBase.php
Normal file
50
api/src/handlers/RequestBase.php
Normal file
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
require_once 'src/Database.php';
|
||||
|
||||
abstract class RequestBase {
|
||||
protected mysqli $conn;
|
||||
private array $actions = array();
|
||||
|
||||
/**
|
||||
* 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"}');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send response message and exit script
|
||||
* @param $message string the response message
|
||||
*/
|
||||
function commitMessage($message){
|
||||
echo $message;
|
||||
exit(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* add the action handlers in this abstract method
|
||||
*/
|
||||
abstract function initHandlers();
|
||||
}
|
82
api/src/handlers/Settings.php
Normal file
82
api/src/handlers/Settings.php
Normal file
@ -0,0 +1,82 @@
|
||||
<?php
|
||||
require_once 'RequestBase.php';
|
||||
|
||||
/**
|
||||
* Class Settings
|
||||
* Backend for the Settings page
|
||||
*/
|
||||
class Settings extends RequestBase {
|
||||
function initHandlers() {
|
||||
$this->getFromDB();
|
||||
$this->saveToDB();
|
||||
}
|
||||
|
||||
/**
|
||||
* handle settings stuff to load from db
|
||||
*/
|
||||
private function getFromDB(){
|
||||
/**
|
||||
* load currently set settings form db for init of settings page
|
||||
*/
|
||||
$this->addActionHandler("loadGeneralSettings", function () {
|
||||
$query = "SELECT * from settings";
|
||||
|
||||
$result = $this->conn->query($query);
|
||||
|
||||
$r = mysqli_fetch_assoc($result);
|
||||
// booleans need to be set manually
|
||||
$r['passwordEnabled'] = $r['password'] != "-1";
|
||||
$r['TMDB_grabbing'] = ($r['TMDB_grabbing'] != '0');
|
||||
|
||||
echo json_encode($r);
|
||||
});
|
||||
|
||||
/**
|
||||
* load initial data for home page load to check if pwd is set
|
||||
*/
|
||||
$this->addActionHandler("loadInitialData", function () {
|
||||
$query = "SELECT * from settings";
|
||||
|
||||
$result = $this->conn->query($query);
|
||||
|
||||
$r = mysqli_fetch_assoc($result);
|
||||
|
||||
$r['passwordEnabled'] = $r['password'] != "-1";
|
||||
unset($r['password']);
|
||||
$r['DarkMode'] = (bool)($r['DarkMode'] != '0');
|
||||
$this->commitMessage(json_encode($r));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* handle setting stuff to save to db
|
||||
*/
|
||||
private function saveToDB(){
|
||||
/**
|
||||
* save changed settings to db
|
||||
*/
|
||||
$this->addActionHandler("saveGeneralSettings", function () {
|
||||
$mediacentername = $_POST['mediacentername'];
|
||||
$password = $_POST['password'];
|
||||
$videopath = $_POST['videopath'];
|
||||
$tvshowpath = $_POST['tvshowpath'];
|
||||
$tmdbsupport = $_POST['tmdbsupport'];
|
||||
$darkmodeenabled = $_POST['darkmodeenabled'];
|
||||
|
||||
$query = "UPDATE settings SET
|
||||
video_path='$videopath',
|
||||
episode_path='$tvshowpath',
|
||||
password='$password',
|
||||
mediacenter_name='$mediacentername',
|
||||
TMDB_grabbing=$tmdbsupport,
|
||||
DarkMode=$darkmodeenabled
|
||||
WHERE 1";
|
||||
|
||||
if ($this->conn->query($query) === true) {
|
||||
$this->commitMessage('{"success": true}');
|
||||
} else {
|
||||
$this->commitMessage('{"success": true}');
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
66
api/src/handlers/Tags.php
Normal file
66
api/src/handlers/Tags.php
Normal file
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
require_once 'RequestBase.php';
|
||||
|
||||
/**
|
||||
* Class Tags
|
||||
* backend to handle Tag database interactions
|
||||
*/
|
||||
class Tags extends RequestBase {
|
||||
function initHandlers() {
|
||||
$this->addToDB();
|
||||
$this->getFromDB();
|
||||
}
|
||||
|
||||
private function getFromDB(){
|
||||
/**
|
||||
* returns all available tags from database
|
||||
*/
|
||||
$this->addActionHandler("getAllTags", function () {
|
||||
$query = "SELECT tag_name,tag_id from tags";
|
||||
$result = $this->conn->query($query);
|
||||
|
||||
$rows = array();
|
||||
while ($r = mysqli_fetch_assoc($result)) {
|
||||
array_push($rows, $r);
|
||||
}
|
||||
$this->commitMessage(json_encode($rows));
|
||||
});
|
||||
}
|
||||
|
||||
private function addToDB(){
|
||||
/**
|
||||
* creates a new tag
|
||||
* query requirements:
|
||||
* * tagname -- name of the new tag
|
||||
*/
|
||||
$this->addActionHandler("createTag", function () {
|
||||
$query = "INSERT INTO tags (tag_name) VALUES ('" . $_POST['tagname'] . "')";
|
||||
|
||||
if ($this->conn->query($query) === TRUE) {
|
||||
$this->commitMessage('{"result":"success"}');
|
||||
} else {
|
||||
$this->commitMessage('{"result":"' . $this->conn->error . '"}');
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* adds a new tag to an existing video
|
||||
*
|
||||
* query requirements:
|
||||
* * movieid -- the id of the video to add the tag to
|
||||
* * id -- the tag id which tag to add
|
||||
*/
|
||||
$this->addActionHandler("addTag", function () {
|
||||
$movieid = $_POST['movieid'];
|
||||
$tagid = $_POST['id'];
|
||||
|
||||
$query = "INSERT INTO video_tags(tag_id, video_id) VALUES ('$tagid','$movieid')";
|
||||
|
||||
if ($this->conn->query($query) === TRUE) {
|
||||
$this->commitMessage('{"result":"success"}');
|
||||
} else {
|
||||
$this->commitMessage('{"result":"' . $this->conn->error . '"}');
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
206
api/src/handlers/Video.php
Executable file
206
api/src/handlers/Video.php
Executable file
@ -0,0 +1,206 @@
|
||||
<?php
|
||||
require_once 'src/SSettings.php';
|
||||
require_once 'RequestBase.php';
|
||||
|
||||
/**
|
||||
* Class Video
|
||||
* backend for all interactions with videoloads and receiving of video infos
|
||||
*/
|
||||
class Video extends RequestBase {
|
||||
private string $videopath;
|
||||
|
||||
public function __construct() {
|
||||
$settings = new SSettings();
|
||||
// load video path from settings
|
||||
$this->videopath = $settings->getVideoPath();
|
||||
}
|
||||
|
||||
function initHandlers() {
|
||||
$this->getVideos();
|
||||
$this->loadVideos();
|
||||
$this->addToVideo();
|
||||
}
|
||||
|
||||
/**
|
||||
* function handles load of all videos and search for videos
|
||||
*/
|
||||
private function getVideos() {
|
||||
$this->addActionHandler("getMovies", function () {
|
||||
$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") {
|
||||
$query = "SELECT movie_id,movie_name 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
|
||||
WHERE t.tag_name = '$tag'
|
||||
ORDER BY likes DESC, create_date, movie_name";
|
||||
}
|
||||
}
|
||||
$result = $this->conn->query($query);
|
||||
$rows = array();
|
||||
while ($r = mysqli_fetch_assoc($result)) {
|
||||
array_push($rows, $r);
|
||||
}
|
||||
|
||||
$this->commitMessage(json_encode($rows));
|
||||
});
|
||||
|
||||
$this->addActionHandler("getRandomMovies", function () {
|
||||
$return = new stdClass();
|
||||
$query = "SELECT movie_id,movie_name FROM videos ORDER BY RAND() LIMIT " . $_POST['number'];
|
||||
$result = $this->conn->query($query);
|
||||
$return->rows = array();
|
||||
|
||||
// get tags of random videos
|
||||
$ids = [];
|
||||
while ($r = mysqli_fetch_assoc($result)) {
|
||||
array_push($return->rows, $r);
|
||||
array_push($ids, "video_tags.video_id=" . $r['movie_id']);
|
||||
}
|
||||
|
||||
$idstring = implode(" OR ", $ids);
|
||||
|
||||
$return->tags = array();
|
||||
$query = "SELECT t.tag_name FROM video_tags
|
||||
INNER JOIN tags t on video_tags.tag_id = t.tag_id
|
||||
WHERE $idstring
|
||||
GROUP BY t.tag_name";
|
||||
$result = $this->conn->query($query);
|
||||
while ($r = mysqli_fetch_assoc($result)) {
|
||||
array_push($return->tags, $r);
|
||||
}
|
||||
|
||||
$this->commitMessage(json_encode($return));
|
||||
});
|
||||
|
||||
$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";
|
||||
$result = $this->conn->query($query);
|
||||
$rows = array();
|
||||
while ($r = mysqli_fetch_assoc($result)) {
|
||||
array_push($rows, $r);
|
||||
}
|
||||
|
||||
$this->commitMessage(json_encode($rows));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* function to handle stuff for loading specific videos and startdata
|
||||
*/
|
||||
private function loadVideos() {
|
||||
$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 = $this->conn->query($query);
|
||||
$row = $result->fetch_assoc();
|
||||
|
||||
$arr = array();
|
||||
if ($row["poster"] == null) {
|
||||
$arr["thumbnail"] = $row["thumbnail"];
|
||||
} else {
|
||||
$arr["thumbnail"] = $row["poster"];
|
||||
}
|
||||
|
||||
$arr["movie_id"] = $row["movie_id"];
|
||||
$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", $this->videopath . $row["movie_name"] . ".mp4");
|
||||
$arr["likes"] = $row["likes"];
|
||||
$arr["quality"] = $row["quality"];
|
||||
$arr["length"] = $row["length"];
|
||||
|
||||
// load tags of this video
|
||||
$arr['tags'] = array();
|
||||
$query = "SELECT t.tag_name FROM video_tags
|
||||
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 = $this->conn->query($query);
|
||||
while ($r = mysqli_fetch_assoc($result)) {
|
||||
array_push($arr['tags'], $r);
|
||||
}
|
||||
|
||||
$this->commitMessage(json_encode($arr));
|
||||
});
|
||||
|
||||
$this->addActionHandler("readThumbnail", function () {
|
||||
$query = "SELECT thumbnail FROM videos WHERE movie_id='" . $_POST['movieid'] . "'";
|
||||
|
||||
$result = $this->conn->query($query);
|
||||
$row = $result->fetch_assoc();
|
||||
|
||||
$this->commitMessage($row["thumbnail"]);
|
||||
});
|
||||
|
||||
$this->addActionHandler("getStartData", function () {
|
||||
$query = "SELECT COUNT(*) as nr FROM videos";
|
||||
$result = $this->conn->query($query);
|
||||
$r = mysqli_fetch_assoc($result);
|
||||
|
||||
$arr = array();
|
||||
$arr['total'] = $r['nr'];
|
||||
|
||||
$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 = $this->conn->query($query);
|
||||
$r = mysqli_fetch_assoc($result);
|
||||
$arr['tagged'] = $r['nr'];
|
||||
|
||||
$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
|
||||
WHERE t.tag_name='hd'";
|
||||
$result = $this->conn->query($query);
|
||||
$r = mysqli_fetch_assoc($result);
|
||||
$arr['hd'] = $r['nr'];
|
||||
|
||||
$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
|
||||
WHERE t.tag_name='fullhd'";
|
||||
$result = $this->conn->query($query);
|
||||
$r = mysqli_fetch_assoc($result);
|
||||
$arr['fullhd'] = $r['nr'];
|
||||
|
||||
$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
|
||||
WHERE t.tag_name='lowquality'";
|
||||
$result = $this->conn->query($query);
|
||||
$r = mysqli_fetch_assoc($result);
|
||||
$arr['sd'] = $r['nr'];
|
||||
|
||||
$query = "SELECT COUNT(*) as nr FROM tags";
|
||||
$result = $this->conn->query($query);
|
||||
$r = mysqli_fetch_assoc($result);
|
||||
$arr['tags'] = $r['nr'];
|
||||
|
||||
$this->commitMessage(json_encode($arr));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* function to handle api handlers for stuff to add to video or database
|
||||
*/
|
||||
private function addToVideo() {
|
||||
$this->addActionHandler("addLike", function () {
|
||||
$movieid = $_POST['movieid'];
|
||||
|
||||
$query = "update videos set likes = likes + 1 where movie_id = '$movieid'";
|
||||
|
||||
if ($this->conn->query($query) === TRUE) {
|
||||
$this->commitMessage('{"result":"success"}');
|
||||
} else {
|
||||
$this->commitMessage('{"result":"' . $this->conn->error . '"}');
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user