Removed web API from main branch
This commit is contained in:
parent
e6255d088c
commit
23d7922d42
15
Caddyfile
15
Caddyfile
@ -1,15 +0,0 @@
|
||||
http://localhost {
|
||||
markdown /docs
|
||||
rewrite {
|
||||
to {path} /index.php
|
||||
}
|
||||
|
||||
fastcgi / unix:/run/php/php7.0-fpm.sock {
|
||||
index index.php
|
||||
}
|
||||
|
||||
cors
|
||||
gzip
|
||||
|
||||
errors web_API.log
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
<?php
|
||||
|
||||
$db_settings = ['connection' => 'mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=MadelineProto', 'user' => 'user', 'password' => 'password'];
|
48
index.php
48
index.php
@ -1,48 +0,0 @@
|
||||
<?php
|
||||
|
||||
|
||||
ini_set('log_errors', 1);
|
||||
ini_set('error_log', '/tmp/php-Madeline-errors.log');
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
require 'db_connect.php';
|
||||
$settings = [
|
||||
'db' => $db_settings,
|
||||
'workers' => [
|
||||
'serialization_interval' => 120,
|
||||
'worker_sleep' => 1,
|
||||
],
|
||||
'token' => ['min_length' => 30, 'max_length' => 40],
|
||||
'other' => [
|
||||
'homedir' => '/tmp/',
|
||||
'uri' => $_SERVER['REQUEST_URI'],
|
||||
'params' => $_REQUEST,
|
||||
'endpoint' => 'http'.($_SERVER['SERVER_PORT'] == 443 ? 's' : '').'://'.$_SERVER['HTTP_HOST'].':'.$_SERVER['SERVER_PORT'].'/',
|
||||
'response_wait' => 60,
|
||||
],
|
||||
];
|
||||
|
||||
try {
|
||||
$web_API = new \danog\MadelineProto\WebAPI($settings);
|
||||
echo json_encode($web_API->run());
|
||||
} catch (\danog\MadelineProto\ResponseException $e) {
|
||||
echo json_encode(['ok' => false, 'error_code' => 400, 'error_description' => $e->getMessage().' on line '.$e->getLine().' of '.basename($e->getFile())]);
|
||||
error_log('Exception thrown: '.$e->getMessage());
|
||||
error_log($e->getTraceAsString());
|
||||
} catch (\danog\MadelineProto\Exception $e) {
|
||||
echo json_encode(['ok' => false, 'error_code' => 400, 'error_description' => $e->getMessage().' on line '.$e->getLine().' of '.basename($e->getFile())]);
|
||||
error_log('Exception thrown: '.$e->getMessage());
|
||||
error_log($e->getTraceAsString());
|
||||
} catch (\danog\MadelineProto\RPCErrorException $e) {
|
||||
echo json_encode(['ok' => false, 'error_code' => $e->getCode(), 'error_description' => $e->getMessage().' on line '.$e->getLine().' of '.basename($e->getFile())]);
|
||||
error_log('Exception thrown: '.$e->getMessage());
|
||||
error_log($e->getTraceAsString());
|
||||
} catch (\danog\MadelineProto\TL\Exception $e) {
|
||||
echo json_encode(['ok' => false, 'error_code' => 400, 'error_description' => $e->getMessage().' on line '.$e->getLine().' of '.basename($e->getFile())]);
|
||||
error_log('Exception thrown: '.$e->getMessage());
|
||||
error_log($e->getTraceAsString());
|
||||
} catch (\PDOException $e) {
|
||||
echo json_encode(['ok' => false, 'error_code' => 400, 'error_description' => $e->getMessage().' on line '.$e->getLine().' of '.basename($e->getFile())]);
|
||||
error_log('Exception thrown: '.$e->getMessage());
|
||||
error_log($e->getTraceAsString());
|
||||
}
|
@ -1,110 +0,0 @@
|
||||
<?php
|
||||
/*
|
||||
Copyright 2016-2017 Daniil Gentili
|
||||
(https://daniil.it)
|
||||
This file is part of MadelineProto.
|
||||
MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
||||
MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
See the GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU General Public License along with MadelineProto.
|
||||
If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace danog\MadelineProto;
|
||||
|
||||
class WebAPI
|
||||
{
|
||||
use \danog\MadelineProto\Worker\Worker;
|
||||
use \danog\MadelineProto\Worker\WorkerTools;
|
||||
use \danog\MadelineProto\Worker\Tools;
|
||||
|
||||
public $settings = [];
|
||||
|
||||
public function __construct($settings)
|
||||
{
|
||||
$this->settings = $settings;
|
||||
set_error_handler(['\danog\MadelineProto\Exception', 'ExceptionErrorHandler']);
|
||||
$uri = preg_replace(["/\?.*$/", "/^\//"], '', $settings['other']['uri'], 1);
|
||||
$this->token = preg_replace('/\/.*/', '', $uri);
|
||||
$this->method = strtolower(preg_replace('/.*\//', '', $uri, 1));
|
||||
$this->sessions_dir = $this->settings['other']['homedir'].'/sessions/';
|
||||
if (!file_exists($this->sessions_dir)) {
|
||||
mkdir($this->sessions_dir);
|
||||
}
|
||||
foreach ($this->settings['other']['params'] as &$param) {
|
||||
$new_param = json_decode($param, true);
|
||||
if (is_array($new_param)) {
|
||||
$param = $new_param;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
switch ($this->token) {
|
||||
case 'new_token':
|
||||
$token = '';
|
||||
while ($token === '' || file_exists($this->sessions_dir.$token)) {
|
||||
for ($len = 0; $len < rand($this->settings['token']['min_length'], $this->settings['token']['min_length']); $len++) {
|
||||
$token .= $this->base_64[rand(0, 63)];
|
||||
}
|
||||
}
|
||||
touch($this->sessions_dir.$token);
|
||||
|
||||
return ['ok' => true, 'result' => ['token' => $token, 'worker_status' => $this->start_worker_async($token)]];
|
||||
case 'check_all_workers':
|
||||
return $this->check_all_workers();
|
||||
case '':
|
||||
return ['ok' => false, 'error_code' => 404, 'error_description' => 'Invalid token provided'];
|
||||
default:
|
||||
if (!$this->check_token($this->token)) {
|
||||
return ['ok' => false, 'error_code' => 404, 'error_description' => 'Invalid token provided'];
|
||||
}
|
||||
if (!file_exists($this->sessions_dir.$this->token)) {
|
||||
return ['ok' => false, 'error_code' => 404, 'error_description' => 'Invalid token provided'];
|
||||
}
|
||||
}
|
||||
switch ($this->method) {
|
||||
case 'start_worker_sync':
|
||||
return $this->start_worker_sync($this->token);
|
||||
case 'start_worker':
|
||||
case 'start_worker_async':
|
||||
return $this->start_worker_async($this->token);
|
||||
case 'check_worker':
|
||||
return $this->check_worker($this->token);
|
||||
default:
|
||||
if (!$this->check_worker($this->token)['result']['active']) {
|
||||
throw new Exception('Worker not active');
|
||||
}
|
||||
$this->db_connect();
|
||||
$insert = $this->pdo->prepare('INSERT INTO worker_jobs (worker, method, params) VALUES (?, ?, ?);');
|
||||
$insert->execute([$this->token, $this->method, json_encode($this->settings['other']['params'])]);
|
||||
$id = $this->pdo->lastInsertId();
|
||||
$request_count = 0;
|
||||
while ($request_count++ < $this->settings['other']['response_wait']) {
|
||||
usleep(250000);
|
||||
$select = $this->pdo->prepare('SELECT response, request_id FROM worker_jobs WHERE request_id=? AND processed = ?');
|
||||
$select->execute([$id, (int) true]);
|
||||
$select = $select->fetchAll();
|
||||
if (count($select) > 0) {
|
||||
if (count($select) > 1) {
|
||||
return ['ok' => false, 'error_code' => 400, 'error_description' => 'Got multiple responses, request id '.$id];
|
||||
}
|
||||
if ($select[0]['request_id'] != $id) {
|
||||
return ['ok' => false, 'error_code' => 400, 'error_description' => 'Request id mismatch: got '.$select[0]['request_id'].', actual request id '.$id];
|
||||
}
|
||||
|
||||
$res = json_decode($select[0]['response'], true);
|
||||
if ($res === null) {
|
||||
return ['ok' => false, 'error_code' => 400, 'error_description' => 'Result is null, request id '.$id];
|
||||
}
|
||||
$this->pdo->prepare('DELETE FROM worker_jobs WHERE request_id = ? AND processed = ?')->execute([$id, (int) true]);
|
||||
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
|
||||
return ['ok' => false, 'error_code' => 400, 'error_description' => 'Timeout while fetching result, request id '.$id];
|
||||
}
|
||||
}
|
||||
}
|
55
web_api.sql
55
web_api.sql
@ -1,55 +0,0 @@
|
||||
-- MySQL dump 10.13 Distrib 5.7.16, for Linux (x86_64)
|
||||
--
|
||||
-- Host: localhost Database: MadelineProto
|
||||
-- ------------------------------------------------------
|
||||
-- Server version 5.7.16-0ubuntu0.16.04.1
|
||||
|
||||
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
|
||||
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
|
||||
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
|
||||
/*!40101 SET NAMES utf8 */;
|
||||
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
|
||||
/*!40103 SET TIME_ZONE='+00:00' */;
|
||||
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
|
||||
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
|
||||
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
|
||||
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
|
||||
|
||||
--
|
||||
-- Table structure for table `worker_jobs`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `worker_jobs`;
|
||||
/*!40101 SET @saved_cs_client = @@character_set_client */;
|
||||
/*!40101 SET character_set_client = utf8 */;
|
||||
CREATE TABLE `worker_jobs` (
|
||||
`worker` varchar(255) NOT NULL,
|
||||
`method` text NOT NULL,
|
||||
`params` longtext NOT NULL,
|
||||
`response` longtext,
|
||||
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
`request_id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`processed` tinyint(1) DEFAULT '0',
|
||||
PRIMARY KEY (`request_id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=112 DEFAULT CHARSET=latin1;
|
||||
/*!40101 SET character_set_client = @saved_cs_client */;
|
||||
|
||||
--
|
||||
-- Dumping data for table `worker_jobs`
|
||||
--
|
||||
|
||||
LOCK TABLES `worker_jobs` WRITE;
|
||||
/*!40000 ALTER TABLE `worker_jobs` DISABLE KEYS */;
|
||||
/*!40000 ALTER TABLE `worker_jobs` ENABLE KEYS */;
|
||||
UNLOCK TABLES;
|
||||
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
|
||||
|
||||
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
|
||||
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
|
||||
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
|
||||
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
|
||||
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
|
||||
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
|
||||
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
|
||||
|
||||
-- Dump completed on 2017-01-01 17:05:27
|
Loading…
x
Reference in New Issue
Block a user