rudestan /
Teebot
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * Request class that communicates with Telegram's web servers. Prepares an HTML request and instantiates |
||
| 5 | * the Response object. |
||
| 6 | * |
||
| 7 | * @package Teebot (Telegram bot framework) |
||
| 8 | * |
||
| 9 | * @author Stanislav Drozdov <[email protected]> |
||
| 10 | */ |
||
| 11 | |||
| 12 | namespace Teebot; |
||
| 13 | |||
| 14 | use Teebot\Entity\AbstractEntity; |
||
| 15 | use Teebot\Method\AbstractMethod; |
||
| 16 | use Teebot\Exception\Critical; |
||
| 17 | use Teebot\Exception\Output; |
||
| 18 | |||
| 19 | class Request |
||
| 20 | { |
||
| 21 | const METHOD_GET = 'GET'; |
||
| 22 | |||
| 23 | const CONTENT_TYPE_MULTIPART = 'Content-Type:multipart/form-data'; |
||
| 24 | |||
| 25 | protected $ch; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var Config $config Instance of configuration class |
||
| 29 | */ |
||
| 30 | protected $config; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Constructs the Request object. |
||
| 34 | * |
||
| 35 | * @param Config $config |
||
| 36 | */ |
||
| 37 | public function __construct(Config $config) |
||
| 38 | { |
||
| 39 | $this->config = $config; |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Destructs current object and closes CURL session. |
||
| 44 | */ |
||
| 45 | public function __destruct() |
||
| 46 | { |
||
| 47 | if ($this->ch) { |
||
| 48 | curl_close($this->ch); |
||
| 49 | } |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Executes the Request to Telegram's servers and returns Response object. |
||
| 54 | * |
||
| 55 | * @param AbstractMethod $method Teebot method's instance to get arguments from |
||
| 56 | * @param null|AbstractEntity $parent Parent entity that initiated the Request |
||
| 57 | * |
||
| 58 | * @return null|Response |
||
| 59 | */ |
||
| 60 | public function exec(AbstractMethod $method, $parent = null) |
||
| 61 | { |
||
| 62 | $entityClass = $method->getReturnEntity(); |
||
| 63 | $result = $this->sendRequest($method); |
||
| 64 | |||
| 65 | return $this->createResponseFromData($result, $entityClass, $parent); |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Prepares parameters that are required for sending and performs sending to Telegram's |
||
| 70 | * servers via CURL and returns the result from CURL. |
||
| 71 | * |
||
| 72 | * @param AbstractMethod $methodInstance |
||
| 73 | * |
||
| 74 | * @return mixed |
||
| 75 | */ |
||
| 76 | protected function sendRequest(AbstractMethod $methodInstance) |
||
| 77 | { |
||
| 78 | if (!$this->ch) { |
||
| 79 | $this->ch = curl_init(); |
||
| 80 | } |
||
| 81 | |||
| 82 | $name = $methodInstance->getName(); |
||
| 83 | $curlOptions = [ |
||
| 84 | CURLOPT_SSL_VERIFYHOST => 0, |
||
| 85 | CURLOPT_HEADER => 0, |
||
| 86 | CURLOPT_SSL_VERIFYHOST => 0, |
||
| 87 | CURLOPT_RETURNTRANSFER => 1, |
||
| 88 | CURLOPT_TIMEOUT => Config::REQUEST_TIMEOUT |
||
| 89 | ]; |
||
| 90 | |||
| 91 | // Default method is always POST |
||
| 92 | if ($this->config->getMethod() !== self::METHOD_GET) { |
||
| 93 | $curlOptions[CURLOPT_POST] = 1; |
||
| 94 | |||
| 95 | if ($methodInstance->hasAttachedData()) { |
||
| 96 | $curlOptions[CURLOPT_HTTPHEADER] = [static::CONTENT_TYPE_MULTIPART]; |
||
| 97 | $curlOptions[CURLOPT_SAFE_UPLOAD] = 1; |
||
| 98 | } |
||
| 99 | |||
| 100 | $curlOptions[CURLOPT_POSTFIELDS] = $methodInstance->getPropertiesArray(); |
||
| 101 | |||
| 102 | $curlOptions[CURLOPT_URL] = $this->buildUrl($name); |
||
| 103 | } else { |
||
| 104 | $curlOptions[CURLOPT_URL] = $this->buildUrl($name, $methodInstance->getPropertiesAsString()); |
||
| 105 | } |
||
| 106 | |||
| 107 | curl_setopt_array($this->ch, $curlOptions); |
||
| 108 | |||
| 109 | return curl_exec($this->ch); |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Creates the Response object from received data. |
||
| 114 | * |
||
| 115 | * @param string $receivedData Received data from Telegram's servers |
||
| 116 | * @param string $entityClass Entity class name that should be passed to Response constructor |
||
| 117 | * @param null|AbstractEntity $parent Parent entity |
||
| 118 | * |
||
| 119 | * @return null|Response |
||
| 120 | */ |
||
| 121 | public function createResponseFromData($receivedData, $entityClass, $parent = null) |
||
| 122 | { |
||
| 123 | $response = null; |
||
| 124 | |||
| 125 | try { |
||
| 126 | $response = new Response($receivedData, $entityClass, $parent); |
||
| 127 | } catch (Critical $e) { |
||
| 128 | Output::log($e); |
||
| 129 | } |
||
| 130 | |||
| 131 | return $response; |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Returns url for request to Telegram's bot API |
||
| 136 | * |
||
| 137 | * @param string $methodName The name of Telegram's method to query |
||
| 138 | * @param null|string $args String with arguments to pass to the method via GET |
||
| 139 | * |
||
| 140 | * @return string |
||
| 141 | */ |
||
| 142 | protected function buildUrl($methodName, $args = null) |
||
| 143 | { |
||
| 144 | $url = sprintf( |
||
| 145 | '%s/%s%s/%s', |
||
| 146 | $this->config->getUrl(), |
||
| 147 | Config::BOT_PREFIX, |
||
| 148 | $this->config->getToken(), |
||
| 149 | $methodName |
||
| 150 | ); |
||
| 151 | |||
| 152 | if ($args && strlen($args)) { |
||
|
0 ignored issues
–
show
|
|||
| 153 | $url .= '?' . $args; |
||
| 154 | } |
||
| 155 | |||
| 156 | return $url; |
||
| 157 | } |
||
| 158 | } |
||
| 159 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: