From 23d7922d426d51721227888e6948ac577b61586e Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Thu, 26 Jan 2017 04:02:14 +0100 Subject: [PATCH] Removed web API from main branch --- Caddyfile | 15 ---- dummy_db_connect.php | 3 - index.php | 48 ------------- src/danog/MadelineProto/WebAPI.php | 110 ----------------------------- web_api.sql | 55 --------------- 5 files changed, 231 deletions(-) delete mode 100644 Caddyfile delete mode 100644 dummy_db_connect.php delete mode 100644 index.php delete mode 100644 src/danog/MadelineProto/WebAPI.php delete mode 100644 web_api.sql diff --git a/Caddyfile b/Caddyfile deleted file mode 100644 index 88f491ea..00000000 --- a/Caddyfile +++ /dev/null @@ -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 -} diff --git a/dummy_db_connect.php b/dummy_db_connect.php deleted file mode 100644 index 37b17468..00000000 --- a/dummy_db_connect.php +++ /dev/null @@ -1,3 +0,0 @@ - 'mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=MadelineProto', 'user' => 'user', 'password' => 'password']; diff --git a/index.php b/index.php deleted file mode 100644 index 06ce9b8c..00000000 --- a/index.php +++ /dev/null @@ -1,48 +0,0 @@ - $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()); -} diff --git a/src/danog/MadelineProto/WebAPI.php b/src/danog/MadelineProto/WebAPI.php deleted file mode 100644 index d5183673..00000000 --- a/src/danog/MadelineProto/WebAPI.php +++ /dev/null @@ -1,110 +0,0 @@ -. -*/ - -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]; - } - } -} diff --git a/web_api.sql b/web_api.sql deleted file mode 100644 index 6e9dd9fb..00000000 --- a/web_api.sql +++ /dev/null @@ -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