1
0
Fork 0
mirror of https://github.com/DanielnetoDotCom/YouPHPTube synced 2025-10-05 10:49:36 +02:00

Adding API support for third parties apps

This commit is contained in:
daniel 2019-01-25 12:55:41 -03:00
parent 5b51d42de8
commit 4f34d62d9d
5 changed files with 337 additions and 0 deletions

218
plugin/API/API.php Normal file
View file

@ -0,0 +1,218 @@
<?php
global $global;
require_once $global['systemRootPath'] . 'plugin/Plugin.abstract.php';
class API extends PluginAbstract {
public function getDescription() {
return "Handle APIs for third party Applications";
}
public function getName() {
return "API";
}
public function getUUID() {
return "1apicbec-91db-4357-bb10-ee08b0913778";
}
public function getEmptyDataObject() {
global $global;
$obj = new stdClass();
return $obj;
}
public function getPluginMenu(){
global $global;
$filename = $global['systemRootPath'] . 'plugin/API/pluginMenu.html';
return file_get_contents($filename);
}
public function set($parameters) {
if (empty($parameters['name'])) {
$object = new ApiObject("Parameter name can not be empty");
} else {
if(!empty($parameters['pass'])){
$parameters['password'] = $parameters['pass'];
}
if(!empty($parameters['user']) && !empty($parameters['password'])){
$user = new User("", $parameters['user'], $parameters['password']);
$user->login(false, !empty($parameters['encodedPass']));
}
$name = $parameters['name'];
if (method_exists($this, "set_api_$name")) {
$str = "\$object = \$this->set_api_$name(\$parameters);";
eval($str);
} else {
$object = new ApiObject();
}
}
return $object;
}
public function get($parameters) {
if (empty($parameters['name'])) {
$object = new ApiObject("Parameter name can not be empty");
} else {
if(!empty($parameters['pass'])){
$parameters['password'] = $parameters['pass'];
}
if(!empty($parameters['user']) && !empty($parameters['password'])){
$user = new User("", $parameters['user'], $parameters['password']);
$user->login(false, !empty($parameters['encodedPass']));
}
$name = $parameters['name'];
if (method_exists($this, "get_api_$name")) {
$str = "\$object = \$this->get_api_$name(\$parameters);";
eval($str);
} else {
$object = new ApiObject();
}
}
return $object;
}
private function startResponseObject($parameters){
$obj = new stdClass();
if(empty($parameters['sort']) && !empty($parameters['order'][0]['dir'])){
$index = intval($parameters['order'][0]['column']);
$parameters['sort'][$parameters['columns'][$index]['data']] = $_GET['order'][0]['dir'];
}
$array = array('sort','rowCount','current','searchPhrase');
foreach ($array as $value) {
if(!empty($parameters[$value])){
$obj->$value = $parameters[$value];
$_POST[$value] = $parameters[$value];
}
}
return $obj;
}
/**
* @param type $parameters
* ['sort' database sort column (sample:&sort[created]=DESC)]
* ['rowCount' max numbers of rows]
* ['current' current page]
* ['searchPhrase' to search on the categories]
* ['parentsOnly' will bring only the parents, not children categories]
* @example {webSiteRootURL}plugin/API/{getOrSet}.json.php?name={name}&rowCount=3&current=1
* @return \ApiObject
*/
public function get_api_category($parameters){
global $global;
require_once $global['systemRootPath'].'objects/category.php';
$obj = $this->startResponseObject($parameters);
$rows = Category::getAllCategories();
$totalRows = Category::getTotalCategories();
$obj->totalRows = $totalRows;
$obj->rows = $rows;
return new ApiObject("", false, $obj);
}
/**
* @param type $parameters
* ['sort' database sort column]
* ['rowCount' max numbers of rows]
* ['current' current page]
* ['searchPhrase' to search on the categories]
* ['tags_id' the ID of the tag you want to filter]
* ['catName' the clean_name of the category you want to filter]
* ['channelName' the channelName of the videos you want to filter]
* @example {webSiteRootURL}plugin/API/{getOrSet}.json.php?name={name}&catName=default&rowCount=10
* @return \ApiObject
*/
public function get_api_video($parameters){
global $global;
require_once $global['systemRootPath'].'objects/video.php';
$obj = $this->startResponseObject($parameters);
$rows = Video::getAllVideos();
$totalRows = Video::getTotalVideos();
$obj->totalRows = $totalRows;
$obj->rows = $rows;
return new ApiObject("", false, $obj);
}
/**
* @param type $parameters
* 'videos_id' the video ID what you want to get the likes
* @example {webSiteRootURL}plugin/API/{getOrSet}.json.php?name={name}&videos_id=1
* @return \ApiObject
*/
public function get_api_likes($parameters){
global $global;
require_once $global['systemRootPath'] . 'objects/like.php';
if(empty($parameters['videos_id'])){
return new ApiObject("Videos ID can not be empty");
}
return new ApiObject("", false, Like::getLikes($parameters['videos_id']));
}
/**
* @param type $parameters (all parameters are mandatories)
* 'videos_id' the video ID what you want to send the like
* 'user' username of the user that will like the video
* 'pass' password of the user that will like the video
* @example {webSiteRootURL}plugin/API/{getOrSet}.json.php?name={name}&videos_id=1&user=admin&pass=123
* @return \ApiObject
*/
public function set_api_like($parameters){
return $this->like($parameters, 1);
}
/**
* @param type $parameters (all parameters are mandatories)
* 'videos_id' the video ID what you want to send the like
* 'user' username of the user that will like the video
* 'pass' password of the user that will like the video
* @example {webSiteRootURL}plugin/API/{getOrSet}.json.php?name={name}&videos_id=1&user=admin&pass=123
* @return \ApiObject
*/
public function set_api_dislike($parameters){
return $this->like($parameters, -1);
}
/**
* @param type $parameters (all parameters are mandatories)
* 'videos_id' the video ID what you want to send the like
* 'user' username of the user that will like the video
* 'pass' password of the user that will like the video
* @example {webSiteRootURL}plugin/API/{getOrSet}.json.php?name={name}&videos_id=1&user=admin&pass=123
* @return \ApiObject
*/
public function set_api_removelike($parameters){
return $this->like($parameters, 0);
}
private function like($parameters, $like){
global $global;
require_once $global['systemRootPath'] . 'objects/like.php';
if(empty($parameters['videos_id'])){
return new ApiObject("Videos ID can not be empty");
}
if(!User::isLogged()){
return new ApiObject("User must be logged");
}
new Like($like, $parameters['videos_id']);
return new ApiObject("", false, Like::getLikes($parameters['videos_id']));
}
}
class ApiObject {
public $error;
public $message;
public $response;
function __construct($message = "api not started or not found", $error = true, $response = array()) {
$this->error = $error;
$this->message = $message;
$this->response = $response;
}
}

25
plugin/API/get.json.php Normal file
View file

@ -0,0 +1,25 @@
<?php
header('Access-Control-Allow-Origin: *');
$configFile = '../../videos/configuration.php';
if (!file_exists($configFile)) {
list($scriptPath) = get_included_files();
$path = pathinfo($scriptPath);
$configFile = $path['dirname'] . "/" . $configFile;
}
require_once $configFile;
require_once $global['systemRootPath'].'plugin/API/API.php';
header('Content-Type: application/json');
$plugin = YouPHPTubePlugin::loadPluginIfEnabled("API");
$objData = YouPHPTubePlugin::getObjectDataIfEnabled("API");
if(empty($plugin)){
$obj = new ApiObject("API Plugin disabled");
die(json_encode($obj));
}
$parameters = array_merge($_GET, $_POST);
$obj = $plugin->get($parameters);
die(json_encode($obj));

68
plugin/API/info.php Normal file
View file

@ -0,0 +1,68 @@
<?php
global $global, $config;
if (!isset($global['systemRootPath'])) {
require_once '../../videos/configuration.php';
}
if (!User::isAdmin()) {
header("Location: {$global['webSiteRootURL']}?error=" . __("You can not do this"));
exit;
}
require_once $global['systemRootPath'] . 'plugin/API/API.php';
$plugin = YouPHPTubePlugin::loadPluginIfEnabled("API");
if (empty($plugin)) {
header("Location: {$global['webSiteRootURL']}?error=" . __("You can not do this"));
exit;
}
$reflector = new ReflectionClass('API');
?>
<!DOCTYPE html>
<html lang="<?php echo $_SESSION['language']; ?>">
<head>
<title><?php echo $config->getWebSiteTitle(); ?> :: API</title>
<?php
include $global['systemRootPath'] . 'view/include/head.php';
?>
</head>
<body class="<?php echo $global['bodyClass']; ?>">
<?php
include $global['systemRootPath'] . 'view/include/navbar.php';
$class_methods = get_class_methods('API');
?>
<div class="container">
<ul class="list-group">
<?php
foreach ($class_methods as $method_name) {
if (!preg_match("/(get|set)_api_(.*)/", $method_name, $matches)) {
continue;
}
?>
<li class="list-group-item">
<details>
<summary><?php echo $matches[2] ?></summary>
<br>
<pre><?php
$comment = $reflector->getMethod($method_name)->getDocComment();
$comment = str_replace(array('{webSiteRootURL}','{getOrSet}','{name}'), array($global['webSiteRootURL'],$matches[1],$matches[2]), $comment);
preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $comment, $match2);
//var_dump($match2[0]);
$link = "<a target='_blank' href='{$match2[0][0]}'>".htmlentities($match2[0][0])."</a>";
$comment = str_replace(array($match2[0][0]), array($link), $comment);
echo ($comment);
//{webSiteRootURL}plugin/API/{getOrSet}.json.php?name={name}
?>
</pre>
</details>
</li>
<?php
}
?>
</ul>
</div>
<?php
include $global['systemRootPath'] . 'view/include/footer.php';
?>
</body>
</html>

View file

@ -0,0 +1 @@
<a href="plugin/API/info.php" class="btn btn-primary btn-sm"><i class="fas fa-info-circle"></i> Info</a>

25
plugin/API/set.json.php Normal file
View file

@ -0,0 +1,25 @@
<?php
header('Access-Control-Allow-Origin: *');
$configFile = '../../videos/configuration.php';
if (!file_exists($configFile)) {
list($scriptPath) = get_included_files();
$path = pathinfo($scriptPath);
$configFile = $path['dirname'] . "/" . $configFile;
}
require_once $configFile;
require_once $global['systemRootPath'].'plugin/API/API.php';
header('Content-Type: application/json');
$plugin = YouPHPTubePlugin::loadPluginIfEnabled("API");
$objData = YouPHPTubePlugin::getObjectDataIfEnabled("API");
if(empty($plugin)){
$obj = new ApiObject("API Plugin disabled");
die(json_encode($obj));
}
$parameters = array_merge($_GET, $_POST);
$obj = $plugin->set($parameters);
die(json_encode($obj));