| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <?php
- namespace ThurData\Servers\KerioEmail\Core\Api\AbstractApi;
- use \ThurData\Servers\KerioEmail\Core\Api\AbstractApi\Curl\Response;
- use \ThurData\Servers\KerioEmail\Core\DependencyInjection;
- use \ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\Exception;
- use \ThurData\Servers\KerioEmail\Core\HandlerError\ErrorCodes\ErrorCodesLib;
- /**
- * Description of Curl
- *
- * @autor ThurData <info@thurdata.ch>
- */
- abstract class Curl
- {
- private $curl;
- private $options = [
- CURLOPT_TIMEOUT => 30,
- CURLOPT_HEADER => false,
- CURLOPT_RETURNTRANSFER => true,
- CURLINFO_HEADER_OUT => true
- ];
- protected $curlParser;
- public function setCurlParser($curlParser)
- {
- $this->curlParser = $curlParser;
- return $this;
- }
- public function setOptions($options, $value)
- {
- $this->options[$options] = $value;
- return $this;
- }
- protected function open()
- {
- $this->curl = curl_init();
- return $this;
- }
- protected function close()
- {
- curl_close($this->curl);
- return $this;
- }
- protected function unsetOptions($options)
- {
- if (is_array($options))
- {
- foreach ($options as $option)
- {
- if (isset($this->options[$option]))
- {
- unset($this->options[$option]);
- }
- }
- }
- else
- {
- unset($this->options[$options]);
- }
- return $this;
- }
- /**
- * @return Response
- * @throws \ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\Exception
- */
- protected function send()
- {
- $this->includeOptions();
- if (($head = $this->execute()) === false)
- {
- throw new Exception(ErrorCodesLib::CORE_CURL_000001, ['lastCurlError' => $this->getLastErrorWithCurl()]);
- }
- if ($errno = $this->getLastErrorNumber())
- {
- throw new Exception(ErrorCodesLib::CORE_CURL_000002, ['curlError' => $this->getLastError($errno)]);
- }
- list($header, $body) = $this->curlParser->rebuild($head, $this->getHeaderSize());
- return DependencyInjection::create(Response::class)
- ->setRequest($this->getHeaderOut())
- ->setHeader($header)
- ->setCode($this->getHttpCode())
- ->setBody($body);
- }
- private function execute()
- {
- return curl_exec($this->curl);
- }
- private function getLastErrorNumber()
- {
- return curl_errno($this->curl);
- }
- /**
- * (PHP 5 >= 5.5.0, PHP 7)<br/>
- * Return string describing the given error code
- * @link http://php.net/manual/en/function.curl-strerror.php
- * @param int $errornum <p>
- * One of the cURL error codes constants.
- * </p>
- * @return string error description or <b>NULL</b> for invalid error code.
- */
- private function getLastError($errmo)
- {
- return curl_strerror($errmo);
- }
- /**
- * (PHP 4 >= 4.0.3, PHP 5, PHP 7)<br/>
- * Return a string containing the last error for the current session
- * @link http://php.net/manual/en/function.curl-error.php
- * @param resource $ch
- * @return string the error message or '' (the empty string) if no
- * error occurred.
- */
- private function getLastErrorWithCurl()
- {
- return curl_error($this->curl);
- }
- private function getHeaderSize()
- {
- return curl_getinfo($this->curl, CURLINFO_HEADER_SIZE);
- }
- private function getHeaderOut()
- {
- return curl_getinfo($this->curl, CURLINFO_HEADER_OUT);
- }
- private function getHttpCode()
- {
- return curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
- }
- private function includeOptions()
- {
- curl_setopt_array($this->curl, $this->options);
- return $this;
- }
- }
|