View source
<?php
namespace Drupal\tmgmt_smartling\Context;
use Drupal;
class HtmlAssetInliner {
private $url = '';
private $cookie = '';
private $html = '';
private $dom = '';
protected static $authError = array(
"response" => array(
"code" => "AUTHENTICATION_ERROR",
"data" => array(
"baseUrl" => NULL,
"body" => NULL,
"headers" => NULL,
),
"messages" => array(
"Authentication token is empty or invalid.",
),
),
);
protected static $uriMissingError = array(
"response" => array(
"code" => "VALIDATION_ERROR",
"data" => array(
"baseUrl" => NULL,
"body" => NULL,
"headers" => NULL,
),
"messages" => array(
"fileUri parameter is missing.",
),
),
);
public function __construct() {
libxml_use_internal_errors(TRUE);
$this->dom = new \DOMDocument();
$this->dom->preserveWhiteSpace = FALSE;
$this->dom->strictErrorChecking = FALSE;
}
public function getCompletePage($url, $cookie = '', $keepjs = TRUE, $compress = FALSE, array $settings, $debug = FALSE) {
if (!filter_var($url, FILTER_VALIDATE_URL)) {
throw new \Exception('Invalid URL. Make sure to specify http(s) part.');
}
if (empty($url)) {
if ($debug) {
Drupal::logger('tmgmt_smartling_context_debug')
->info('Url is missing.');
}
return self::$uriMissingError;
}
if (!$cookie) {
if ($debug) {
Drupal::logger('tmgmt_smartling_context_debug')
->info('Auth error.');
}
return self::$authError;
}
$this->url = $url;
$this->cookie = $cookie;
$this->html = $this
->getUrlContents($this->url, 0, 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.215 Safari/534.10', $settings, $debug);
if (strlen($this->html) <= 300) {
if ($debug) {
Drupal::logger('tmgmt_smartling_context_debug')
->info('Response is too small.');
}
return '';
}
return $compress ? $this
->compress($this->html) : $this->html;
}
public function remote_file_exists($url, $proj_settings) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$this
->applySettingsToCurl($proj_settings, $ch);
if (curl_exec($ch) !== FALSE) {
return TRUE;
}
return FALSE;
}
private function compress($string) {
return str_replace(array(
"\r\n",
"\r",
"\n",
"\t",
' ',
' ',
' ',
), ' ', $string);
}
public function getUrlContents($url, $timeout = 0, $userAgent = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.215 Safari/534.10', array $settings, $debug = FALSE) {
$crl = curl_init();
if ($debug) {
curl_setopt($crl, CURLINFO_HEADER_OUT, TRUE);
curl_setopt($crl, CURLOPT_HEADER, 1);
}
$this
->applySettingsToCurl($settings, $crl);
curl_setopt($crl, CURLOPT_URL, $url);
curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($crl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($crl, CURLOPT_COOKIE, $this->cookie);
curl_setopt($crl, CURLOPT_USERAGENT, $userAgent);
curl_setopt($crl, CURLOPT_SSL_VERIFYPEER, FALSE);
$output = curl_exec($crl);
if ($debug) {
$curlInfo = curl_getinfo($crl);
$header_size = $curlInfo['header_size'];
$headers = substr($output, 0, $header_size);
$output = substr($output, $header_size);
Drupal::logger('tmgmt_smartling_context_debug')
->info('Curl request info: @request_info:', [
'@request_info' => print_r($curlInfo, TRUE),
]);
Drupal::logger('tmgmt_smartling_context_debug')
->info('Curl response headers: @response_headers', [
'@response_headers' => $headers,
]);
Drupal::logger('tmgmt_smartling_context_debug')
->info('Curl response body: @response_body', [
'@response_body' => substr($output, 0, 500) . '*****',
]);
}
curl_close($crl);
if (!$output) {
return -1;
}
return $output;
}
private function applySettingsToCurl($proj_settings, $curl) {
if (!empty($proj_settings['context_skip_host_verifying'])) {
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
}
if (!empty($proj_settings['enable_basic_auth'])) {
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, $proj_settings['basic_auth']['login'] . ':' . $proj_settings['basic_auth']['password']);
}
}
}