new folder structure for php scripts

renamed api nodes
php braces on same line
This commit is contained in:
2020-08-12 17:50:25 +00:00
parent 13336cbf1c
commit 1de36afb69
34 changed files with 457 additions and 238 deletions

View File

@ -1,30 +0,0 @@
<?php
require_once 'RequestBase.php';
class Tags extends RequestBase {
function initHandlers() {
$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);
}
echo json_encode($rows);
});
$this->addActionHandler("createTag", function (){
$query = "INSERT INTO tags (tag_name) VALUES ('" . $_POST['tagname'] . "')";
if ($this->conn->query($query) === TRUE) {
echo('{"result":"success"}');
} else {
echo('{"result":"' . $this->conn->error . '"}');
}
});
}
}
$tags = new Tags();
$tags->handleAction();

View File

@ -1,7 +1,7 @@
<?php
require 'Database.php';
require 'TMDBMovie.php';
require 'SSettings.php';
require_once './src/Database.php';
require_once './src/TMDBMovie.php';
require_once './src/SSettings.php';
// allow UTF8 characters
setlocale(LC_ALL, 'en_US.UTF-8');
@ -226,8 +226,7 @@ writeLog("-42"); // terminating characters to stop webui requesting infos
* @param $video string name including extension
* @return object all infos as object
*/
function _get_video_attributes($video)
{
function _get_video_attributes($video) {
$command = "mediainfo \"../videos/prn/$video\" --Output=JSON";
$output = shell_exec($command);
return json_decode($output);
@ -238,8 +237,7 @@ function _get_video_attributes($video)
*
* @param string $message message to write
*/
function writeLog(string $message)
{
function writeLog(string $message) {
file_put_contents("/tmp/output.log", $message, FILE_APPEND);
flush();
}
@ -249,8 +247,7 @@ function writeLog(string $message)
* @param string $tagname the name of the tag
* @return integer the id of the inserted tag
*/
function tagExists(string $tagname)
{
function tagExists(string $tagname) {
global $conn;
$query = "SELECT * FROM tags WHERE tag_name='$tagname'";

5
api/settings.php Normal file
View File

@ -0,0 +1,5 @@
<?php
require_once './src/handlers/Settings.php';
$sett = new Settings();
$sett->handleAction();

View File

@ -5,8 +5,7 @@
*
* Class with all neccessary stuff for the Database connections.
*/
class Database
{
class Database {
private static ?Database $instance = null;
private mysqli $conn;
@ -16,8 +15,7 @@ class Database
private string $dbname = "mediacenter";
// The db connection is established in the private constructor.
private function __construct()
{
private function __construct() {
// Create connection
$this->conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname);
@ -32,8 +30,7 @@ class Database
*
* @return Database dbobject
*/
public static function getInstance()
{
public static function getInstance() {
if (!self::$instance) {
self::$instance = new Database();
}
@ -46,8 +43,7 @@ class Database
*
* @return mysqli mysqli instance
*/
public function getConnection()
{
public function getConnection() {
return $this->conn;
}
@ -55,7 +51,7 @@ class Database
* get name of current active database
* @return string name
*/
public function getDatabaseName(){
public function getDatabaseName() {
return $this->dbname;
}
}

View File

@ -1,7 +1,10 @@
<?php
class SSettings
{
/**
* Class SSettings
* class handling all Settings used by php scripts
*/
class SSettings {
private ?Database $database;
/**
@ -11,6 +14,10 @@ class SSettings
$this->database = Database::getInstance();
}
/**
* get the videopath saved in db
* @return string videopath
*/
public function getVideoPath() {
$query = "SELECT video_path from settings";
@ -24,16 +31,15 @@ class SSettings
* check if TMDB is enableds
* @return bool isenabled?
*/
public function isTMDBGrabbingEnabled(): bool
{
public function isTMDBGrabbingEnabled(): bool {
$query = "SELECT TMDB_grabbing from settings";
$result = $this->database->getConnection()->query($query);
if(!$result){
if (!$result) {
return true; // if undefined in db --> default true
}else{
} else {
$r = mysqli_fetch_assoc($result);
return $r['TMDB_grabbing'] == '1';
}
}
}
}

View File

@ -1,10 +1,13 @@
<?php
class TMDBMovie
{
/**
* 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/";
public $picturebase = "https://image.tmdb.org/t/p/w500";
/**
* search for a specific movie
@ -12,8 +15,7 @@ class TMDBMovie
* @param string $moviename moviename
* @return object movie object or null if not found
*/
public function searchMovie(string $moviename)
{
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
@ -29,8 +31,7 @@ class TMDBMovie
*
* @return array of all available genres
*/
public function getAllGenres()
{
public function getAllGenres() {
$reply = json_decode(file_get_contents($this->baseurl . "genre/movie/list?api_key=" . $this->apikey));
return $reply->genres;
}

View File

@ -1,14 +1,9 @@
<?php
require_once 'Database.php';
require_once 'src/Database.php';
abstract class RequestBase {
private array $actions = array();
protected mysqli $conn;
/**
* add the action handlers in this abstract method
*/
abstract function initHandlers();
private array $actions = array();
/**
* adds a new action handler to the current api file
@ -38,4 +33,18 @@ abstract class RequestBase {
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();
}

View File

@ -1,8 +1,23 @@
<?php
require 'RequestBase.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";
@ -16,6 +31,30 @@ class Settings extends RequestBase {
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'];
@ -34,26 +73,10 @@ class Settings extends RequestBase {
WHERE 1";
if ($this->conn->query($query) === true) {
echo '{"success": true}';
$this->commitMessage('{"success": true}');
} else {
echo '{"success": true}';
$this->commitMessage('{"success": true}');
}
});
$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');
echo json_encode($r);
});
}
}
$sett = new Settings();
$sett->handleAction();

66
api/src/handlers/Tags.php Normal file
View 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 . '"}');
}
});
}
}

View File

@ -1,8 +1,11 @@
<?php
require_once 'Database.php';
require_once 'SSettings.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;
@ -13,6 +16,15 @@ class Video extends RequestBase {
}
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'])) {
@ -31,7 +43,7 @@ class Video extends RequestBase {
array_push($rows, $r);
}
echo(json_encode($rows));
$this->commitMessage(json_encode($rows));
});
$this->addActionHandler("getRandomMovies", function () {
@ -59,7 +71,7 @@ class Video extends RequestBase {
array_push($return->tags, $r);
}
echo(json_encode($return));
$this->commitMessage(json_encode($return));
});
$this->addActionHandler("getSearchKeyWord", function () {
@ -74,9 +86,14 @@ class Video extends RequestBase {
array_push($rows, $r);
}
echo(json_encode($rows));
$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'] . "'";
@ -110,23 +127,7 @@ class Video extends RequestBase {
array_push($arr['tags'], $r);
}
echo(json_encode($arr));
});
$this->addActionHandler("getDbSize", function () {
$dbname = Database::getInstance()->getDatabaseName();
$query = "SELECT table_schema AS \"Database\",
ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS \"Size\"
FROM information_schema.TABLES
WHERE TABLE_SCHEMA='$dbname'
GROUP BY table_schema;";
$result = $this->conn->query($query);
if ($result->num_rows == 1) {
$row = $result->fetch_assoc();
echo '{"data":"' . $row["Size"] . 'MB"}';
}
$this->commitMessage(json_encode($arr));
});
$this->addActionHandler("readThumbnail", function () {
@ -135,38 +136,7 @@ class Video extends RequestBase {
$result = $this->conn->query($query);
$row = $result->fetch_assoc();
echo($row["thumbnail"]);
});
$this->addActionHandler("getTags", function () {
// todo add this to loadVideo maybe
$movieid = $_POST['movieid'];
$query = "SELECT tag_name FROM video_tags
INNER JOIN tags t on video_tags.tag_id = t.tag_id
WHERE video_id='$movieid'";
$result = $this->conn->query($query);
$rows = array();
$rows['tags'] = array();
while ($r = mysqli_fetch_assoc($result)) {
array_push($rows['tags'], $r['tag_name']);
}
echo(json_encode($rows));
});
$this->addActionHandler("addLike", function () {
$movieid = $_POST['movieid'];
$query = "update videos set likes = likes + 1 where movie_id = '$movieid'";
if ($this->conn->query($query) === TRUE) {
echo('{"result":"success"}');
} else {
echo('{"result":"' . $this->conn->error . '"}');
}
$this->commitMessage($row["thumbnail"]);
});
$this->addActionHandler("getStartData", function () {
@ -213,34 +183,24 @@ class Video extends RequestBase {
$r = mysqli_fetch_assoc($result);
$arr['tags'] = $r['nr'];
echo(json_encode($arr));
$this->commitMessage(json_encode($arr));
});
}
$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);
}
echo(json_encode($rows));
});
$this->addActionHandler("addTag", function () {
/**
* function to handle api handlers for stuff to add to video or database
*/
private function addToVideo() {
$this->addActionHandler("addLike", function () {
$movieid = $_POST['movieid'];
$tagid = $_POST['id'];
$query = "INSERT INTO video_tags(tag_id, video_id) VALUES ('$tagid','$movieid')";
$query = "update videos set likes = likes + 1 where movie_id = '$movieid'";
if ($this->conn->query($query) === TRUE) {
echo('{"result":"success"}');
$this->commitMessage('{"result":"success"}');
} else {
echo('{"result":"' . $this->conn->error . '"}');
$this->commitMessage('{"result":"' . $this->conn->error . '"}');
}
});
}
}
$video = new Video();
$video->handleAction();

5
api/tags.php Normal file
View File

@ -0,0 +1,5 @@
<?php
include_once './src/handlers/Tags.php';
$tags = new Tags();
$tags->handleAction();

5
api/video.php Normal file
View File

@ -0,0 +1,5 @@
<?php
include_once './src/handlers/Video.php';
$video = new Video();
$video->handleAction();