Updating Media dependent modules to versions compatible with core Media.
[yaffs-website] / web / modules / contrib / media_entity_twitter / src / Plugin / Validation / Constraint / TweetVisibleConstraintValidator.php
1 <?php
2
3 namespace Drupal\media_entity_twitter\Plugin\Validation\Constraint;
4
5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
6 use Drupal\media_entity_twitter\Plugin\media\Source\Twitter;
7 use Drupal\Core\Field\FieldItemInterface;
8 use GuzzleHttp\Client;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10 use Symfony\Component\Validator\Constraint;
11 use Symfony\Component\Validator\ConstraintValidator;
12
13 /**
14  * Validates the TweetVisible constraint.
15  */
16 class TweetVisibleConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
17
18   /**
19    * The HTTP client to fetch the feed data with.
20    *
21    * @var \GuzzleHttp\Client
22    */
23   protected $httpClient;
24
25   /**
26    * Constructs a new TweetVisibleConstraintValidator.
27    *
28    * @param \GuzzleHttp\Client $http_client
29    *   The http client service.
30    */
31   public function __construct(Client $http_client) {
32     $this->httpClient = $http_client;
33   }
34
35   /**
36    * {@inheritdoc}
37    */
38   public static function create(ContainerInterface $container) {
39     return new static($container->get('http_client'));
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public function validate($value, Constraint $constraint) {
46     $data = '';
47     if (is_string($value)) {
48       $data = $value;
49     }
50     elseif ($value instanceof FieldItemList) {
51       $fieldtype = $value->getFieldDefinition()->getType();
52       $field_value = $value->getValue();
53       if ($fieldtype == 'link') {
54         $data = empty($field_value[0]['uri']) ? "" : $field_value[0]['uri'];
55       }
56       else {
57         $data = empty($field_value[0]['value']) ? "" : $field_value[0]['value'];
58       }
59     }
60     elseif ($value instanceof FieldItemInterface) {
61       $class = get_class($value);
62       $property = $class::mainPropertyName();
63       if ($property) {
64         $data = $value->{$property};
65       }
66     }
67     foreach (Twitter::$validationRegexp as $pattern => $key) {
68       if (preg_match($pattern, $data, $item_matches)) {
69         $matches[] = $item_matches;
70       }
71     }
72
73     if (empty($matches[0][0])) {
74       // If there are no matches the URL is not correct, so stop validation.
75       return;
76     }
77
78     // Fetch content from the given url.
79     $response = $this->httpClient->get($matches[0][0], ['allow_redirects' => FALSE]);
80
81     if ($response->getStatusCode() == 302 && ($location = $response->getHeader('location'))) {
82       $effective_url_parts = parse_url($location[0]);
83       if (!empty($effective_url_parts) && isset($effective_url_parts['query']) && $effective_url_parts['query'] == 'protected_redirect=true') {
84         $this->context->addViolation($constraint->message);
85       }
86     }
87   }
88
89 }