Author: Jan Tichý
Author: Jakub Tománek
Author: Jaroslav Hanslík
Example #1
0
 /**
  * Performs the test.
  *
  * @return \Jyxo\Beholder\Result
  */
 public function run() : \Jyxo\Beholder\Result
 {
     // The \GuzzleHttp library is required
     if (!class_exists(\GuzzleHttp\Client::class)) {
         return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::NOT_APPLICABLE, 'Guzzle library missing');
     }
     try {
         $httpClient = new \GuzzleHttp\Client();
         $httpRequest = new \GuzzleHttp\Psr7\Request('GET', $this->url, ['User-Agent' => 'JyxoBeholder']);
         $httpResponse = $httpClient->send($httpRequest, [\GuzzleHttp\RequestOptions::CONNECT_TIMEOUT => 5, \GuzzleHttp\RequestOptions::TIMEOUT => 10]);
         if (200 !== $httpResponse->getStatusCode()) {
             throw new \Exception(sprintf('Http error: %s', $httpResponse->getReasonPhrase()));
         }
         if (isset($this->tests['body'])) {
             $body = (string) $httpResponse->getBody();
             if (strpos($body, $this->tests['body']) === false) {
                 $body = trim(strip_tags($body));
                 throw new \Exception(sprintf('Invalid body: %s', \Jyxo\StringUtil::cut($body, 128)));
             }
         }
         return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::SUCCESS, $this->url);
     } catch (\Exception $e) {
         return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, $e->getMessage());
     }
 }
Example #2
0
 /**
  * Performs the test.
  *
  * @return \Jyxo\Beholder\Result
  */
 public function run()
 {
     // The http extension is required
     if (!extension_loaded('http')) {
         return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::NOT_APPLICABLE, 'Extension http missing');
     }
     $http = new \HttpRequest($this->url, \HttpRequest::METH_GET, array('connecttimeout' => 5, 'timeout' => 10, 'useragent' => 'JyxoBeholder'));
     try {
         $http->send();
         if (200 !== $http->getResponseCode()) {
             throw new \Exception(sprintf('Http error: %s', $http->getResponseCode()));
         }
         if (isset($this->tests['body'])) {
             $body = $http->getResponseBody();
             if (!preg_match($this->tests['body'], $body)) {
                 $body = trim(strip_tags($body));
                 throw new \Exception(sprintf('Invalid body: %s', \Jyxo\StringUtil::cut($body, 16)));
             }
         }
         // OK
         return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::SUCCESS);
     } catch (\HttpException $e) {
         $inner = $e;
         while (null !== $inner->innerException) {
             $inner = $inner->innerException;
         }
         return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, $inner->getMessage());
     } catch (\Exception $e) {
         return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, $e->getMessage());
     }
 }
Example #3
0
File: Sender.php Project: jyxo/php
 /**
  * Adds all attachments to the email.
  *
  * @return string
  */
 private function attachAll() : string
 {
     $mime = [];
     foreach ($this->email->attachments as $attachment) {
         $encoding = !empty($attachment->encoding) ? $attachment->encoding : Encoding::BASE64;
         $name = $this->changeCharset($this->clearHeaderValue($attachment->name));
         $mime[] = '--' . $this->boundary[1] . self::LINE_END;
         $mime[] = 'Content-Type: ' . $this->clearHeaderValue($attachment->mimeType) . ';' . self::LINE_END;
         $mime[] = ' name="' . $this->encodeHeader($name) . '"' . self::LINE_END;
         $mime[] = 'Content-Transfer-Encoding: ' . $encoding . self::LINE_END;
         if ($attachment->isInline()) {
             $mime[] = 'Content-ID: <' . $this->clearHeaderValue($attachment->cid) . '>' . self::LINE_END;
         }
         $mime[] = 'Content-Disposition: ' . $attachment->disposition . ';' . self::LINE_END;
         $mime[] = ' filename="' . $this->encodeHeader($name) . '"' . self::LINE_END . self::LINE_END;
         // Just fix line endings in case of encoded attachments, encode otherwise
         $mime[] = !empty($attachment->encoding) ? \Jyxo\StringUtil::fixLineEnding($attachment->content, self::LINE_END) : $this->encodeString($attachment->content, $encoding);
         $mime[] = self::LINE_END . self::LINE_END;
     }
     $mime[] = '--' . $this->boundary[1] . '--' . self::LINE_END;
     return implode('', $mime);
 }
Example #4
0
 /**
  * Encodes a string using the quoted-printable encoding.
  *
  * @param string $string Input string
  * @param integer $lineLength Line length
  * @param string $lineEnd Line ending
  * @return string
  */
 private static function encodeQuotedPrintable(string $string, int $lineLength, string $lineEnd) : string
 {
     $encoded = \Jyxo\StringUtil::fixLineEnding(trim($string), $lineEnd);
     // Replaces all high ASCII characters, control codes and '='
     $encoded = preg_replace_callback('~([\\000-\\010\\013\\014\\016-\\037\\075\\177-\\377])~', function ($matches) {
         return '=' . sprintf('%02X', ord($matches[1]));
     }, $encoded);
     // Replaces tabs and spaces if on line ends
     $encoded = preg_replace_callback('~([\\011\\040])' . $lineEnd . '~', function ($matches) use($lineEnd) {
         return '=' . sprintf('%02X', ord($matches[1])) . $lineEnd;
     }, $encoded);
     $output = '';
     $lines = explode($lineEnd, $encoded);
     // Release from memory
     unset($encoded);
     foreach ($lines as $line) {
         // Line length is less than maximum
         if (strlen($line) <= $lineLength) {
             $output .= $line . $lineEnd;
             continue;
         }
         do {
             $partLength = strlen($line);
             if ($partLength > $lineLength) {
                 $partLength = $lineLength;
             }
             // Cannot break a line in the middle of a character
             $pos = strrpos(substr($line, 0, $partLength), '=');
             if (false !== $pos && $pos >= $partLength - 2) {
                 $partLength = $pos;
             }
             // If the last char is a break, move one character backwards
             if ($partLength > 0 && ' ' == $line[$partLength - 1]) {
                 $partLength--;
             }
             // Saves string parts, trims the string and continues
             $output .= substr($line, 0, $partLength);
             $line = substr($line, $partLength);
             // We are in the middle of a line
             if (!empty($line)) {
                 $output .= '=';
             }
             $output .= $lineEnd;
         } while (!empty($line));
     }
     return $output;
 }