View source
<?php
namespace Drupal\advagg_validator\Form;
use DOMDocument;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\RendererInterface;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class CssW3Form extends BaseValidatorForm {
protected $httpClient;
protected $renderer;
public function __construct(ConfigFactoryInterface $config_factory, RequestStack $request_stack, Client $http_client, RendererInterface $renderer) {
parent::__construct($config_factory, $request_stack);
$this->requestStack = $request_stack;
$this->httpClient = $http_client;
$this->renderer = $renderer;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('request_stack'), $container
->get('http_client'), $container
->get('renderer'));
}
public function getFormId() {
return 'advagg_validator_cssw3';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::generateForm('css', FALSE);
$form['notice'] = [
'#markup' => '<div>' . $this
->t('Notice: The form below will submit files to the <a href="http://jigsaw.w3.org/css-validator/">http://jigsaw.w3.org/css-validator/</a> service if used.') . '</div>',
'#weight' => -1,
];
$form = parent::buildForm($form, $form_state);
unset($form['actions']);
return $form;
}
public function submitCheckAll(array &$form, FormStateInterface $form_state) {
$dir = $form_state
->getTriggeringElement()['#name'];
$files = [];
foreach ($form_state
->getValues() as $key => $value) {
if (strpos($key, 'hidden') === FALSE || strpos($value, $dir) === FALSE || $dir === '.' && substr_count($value, '/') > 0) {
continue;
}
$files[] = $value;
}
$info = $this
->testFiles($files);
$info = $this
->hideGoodFiles($info);
$output = [
'#theme' => 'item_list',
'#items' => $info,
];
$this
->messenger()
->addMessage($this->renderer
->render($output));
}
public function ajaxCheck(array &$form, FormStateInterface $form_state) {
$dir = $form_state
->getTriggeringElement()['#name'];
return $this
->getElement($form, explode('/', $dir))['wrapper'];
}
public function submitCheckDirectory(array &$form, FormStateInterface $form_state) {
$dir = $form_state
->getTriggeringElement()['#name'];
$files = [];
$slash_count = substr_count('/' . $dir, '/');
foreach ($form_state
->getValues() as $key => $value) {
if (strpos($key, 'hidden') === FALSE || strpos($value, $dir) === FALSE || substr_count($value, '/') > $slash_count || $dir === '.' && substr_count($value, '/') > 0) {
continue;
}
$files[] = $value;
}
$info = $this
->testFiles($files);
$info = $this
->hideGoodFiles($info);
$output = [
'#theme' => 'item_list',
'#items' => $info,
];
$this
->messenger()
->addMessage($this->renderer
->render($output));
}
protected function testFiles(array $files, array $options = []) {
$output = [];
foreach ($files as $filename) {
if (!file_exists($filename)) {
continue;
}
$lines = file($filename);
$output[$filename]['jigsaw.w3.org'] = $this
->testW3C($filename, $options);
if (!empty($output[$filename]['jigsaw.w3.org']['errors'])) {
foreach ($output[$filename]['jigsaw.w3.org']['errors'] as &$value) {
if (isset($value['line'])) {
$value['linedata'] = $lines[$value['line'] - 1];
if (strlen($value['linedata']) > 512) {
unset($value['linedata']);
}
}
}
unset($value);
}
if (!empty($output[$filename]['jigsaw.w3.org']['warnings'])) {
foreach ($output[$filename]['jigsaw.w3.org']['warnings'] as &$value) {
if (isset($value['line'])) {
$value['linedata'] = $lines[$value['line'] - 1];
if (strlen($value['linedata']) > 512) {
unset($value['linedata']);
}
}
}
unset($value);
}
}
return $output;
}
private function testW3C($filename, array &$validator_options = []) {
$validator_options['text'] = file_get_contents($filename);
if (strlen($validator_options['text']) > 50000) {
unset($validator_options['text']);
$validator_options['uri'] = $this->requestStack
->getCurrentRequest()
->getBaseUrl() . $filename;
}
$validator_options += [
'output' => 'soap12',
'warning' => '1',
'profile' => 'css3',
'usermedium' => 'all',
'lang' => 'en',
];
$request_url = 'http://jigsaw.w3.org/css-validator/validator';
$query = http_build_query($validator_options, '', '&');
$url = $request_url . '?' . $query;
try {
$data = $this->httpClient
->get($url)
->getBody();
} catch (RequestException $e) {
watchdog_exception('AdvAgg Validator', $e);
} catch (\Exception $e) {
watchdog_exception('AdvAgg Validator', $e);
}
if (!empty($data)) {
$return = $this
->parseSoapResponse($data);
$return['filename'] = $filename;
if (isset($validator_options['text'])) {
unset($validator_options['text']);
}
elseif (isset($validator_options['uri'])) {
unset($validator_options['uri']);
}
$return['options'] = $validator_options;
return $return;
}
return [
'error' => $this
->t('W3C Server did not return a 200 or request data was empty.'),
];
}
private function parseSoapResponse($xml) {
$doc = new DOMDocument();
$response = [];
if (!@$doc
->loadXML($xml)) {
return $response;
}
$cdata = [
'uri',
'checkedby',
'csslevel',
'date',
];
foreach ($cdata as $var) {
$element = $doc
->getElementsByTagName($var);
if ($element->length) {
$response[$var] = $element
->item(0)->nodeValue;
}
}
$element = $doc
->getElementsByTagName('validity');
if ($element->length && $element
->item(0)->nodeValue === 'true') {
$response['validity'] = TRUE;
}
else {
$response['validity'] = FALSE;
$errors = $doc
->getElementsByTagName('error');
foreach ($errors as $error) {
$response['errors'][] = $this
->domExtractor($error);
}
}
$warnings = $doc
->getElementsByTagName('warning');
foreach ($warnings as $warning) {
$response['warnings'][] = $this
->domExtractor($warning);
}
return $response;
}
}