|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Script retranslates queries to telegram bot API |
| 4 | + */ |
| 5 | +class TelegramApiProxy { |
| 6 | + private $url; |
| 7 | + private $ch; |
| 8 | + private $log = false; |
| 9 | + |
| 10 | + public function __construct(){ |
| 11 | + $this->getUrl(); |
| 12 | + $this->initCurl(); |
| 13 | + } |
| 14 | + |
| 15 | + public function setLog(bool $log){ |
| 16 | + $this->log = $log; |
| 17 | + } |
| 18 | + |
| 19 | + private function log(string $m){ |
| 20 | + if(!$this->log) return; |
| 21 | + file_put_contents('proxy.log', $m . PHP_EOL, FILE_APPEND); |
| 22 | + } |
| 23 | + |
| 24 | + public function start(){ |
| 25 | + $this->log('[' . date('Y-m-d H:i:s') . '] Query init. URL: ' . $this->url); |
| 26 | + $this->sendRequest(); |
| 27 | + |
| 28 | + $response = curl_exec($this->ch); |
| 29 | + $debug = curl_getinfo($this->ch); |
| 30 | + $this->log('Response headers: code=' . $debug['http_code'] . '; content_type=' . $debug['content_type'] . '; size_upload=' . $debug['size_upload'] . '; size_download=' . $debug['size_upload'] . ';'); |
| 31 | + $this->log('Response body: ' . $response); |
| 32 | + $this->log(' '); |
| 33 | + |
| 34 | + $this->sendResponse($response, $debug['content_type'], $debug['http_code']); |
| 35 | + } |
| 36 | + |
| 37 | + private function sendResponse(string $response, string $type, int $code){ |
| 38 | + header('Content-type: ' . $type, $code); |
| 39 | + die($response); |
| 40 | + } |
| 41 | + |
| 42 | + public function getUrl(){ |
| 43 | + $dir = dirname($_SERVER['SCRIPT_NAME']); // detect path to dir |
| 44 | + if(strpos($_SERVER['REQUEST_URI'], $dir) === 0){ |
| 45 | + $uri = substr($_SERVER['REQUEST_URI'], strlen($dir)); |
| 46 | + } else { |
| 47 | + $uri = $_SERVER['REQUEST_URI']; |
| 48 | + } |
| 49 | + |
| 50 | + if(substr($uri, 0, 1) != '/'){ |
| 51 | + $uri = '/' . $uri; |
| 52 | + } |
| 53 | + |
| 54 | + return $this->url = "https://api.telegram.org" . $uri; |
| 55 | + } |
| 56 | + |
| 57 | + private function initCurl(){ |
| 58 | + $this->ch = curl_init($this->url); |
| 59 | + curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true); |
| 60 | + return $this->ch; |
| 61 | + } |
| 62 | + |
| 63 | + private function sendRequest(){ |
| 64 | + $method = $_SERVER['REQUEST_METHOD'] ?? 'GET'; |
| 65 | + $this->log('HTTP Request: ' . $method); |
| 66 | + |
| 67 | + if($method == 'POST'){ |
| 68 | + curl_setopt($this->ch, CURLOPT_POST, true); |
| 69 | + |
| 70 | + if(sizeof($_FILES) > 0){ |
| 71 | + $post = []; |
| 72 | + foreach ($_FILES as $name => $file) { |
| 73 | + $post[$name] = new CURLFile($file['tmp_name'], $file['type'], $file['name']); |
| 74 | + } |
| 75 | + |
| 76 | + foreach ($_POST as $name => $value) { |
| 77 | + $post[$name] = $value; |
| 78 | + } |
| 79 | + |
| 80 | + } else { |
| 81 | + $post = file_get_contents('php://input'); |
| 82 | + $ct = $_SERVER['HTTP_CONTENT_TYPE'] ?? $_SERVER['CONTENT_TYPE']; |
| 83 | + curl_setopt($this->ch, CURLOPT_HTTPHEADER, ['Content-type: ' . $ct]); |
| 84 | + } |
| 85 | + |
| 86 | + curl_setopt($this->ch, CURLOPT_POSTFIELDS, $post); |
| 87 | + $this->log('Send data: ' . var_export($post, true)); |
| 88 | + |
| 89 | + } else { |
| 90 | + curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, $method); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +$proxy = new TelegramApiProxy; |
| 96 | +$proxy->setLog(true); // use logs only for debug |
| 97 | +$proxy->start(); |
| 98 | +?> |
0 commit comments