Issues (8)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Plugin/ImageEffect/CropEffect.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Drupal\crop\Plugin\ImageEffect;
4
5
use Drupal\Core\Config\Entity\ConfigEntityStorageInterface;
6
use Drupal\Core\Form\FormStateInterface;
7
use Drupal\Core\Image\ImageFactory;
8
use Drupal\Core\Image\ImageInterface;
9
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
10
use Drupal\crop\CropInterface;
11
use Drupal\crop\CropStorageInterface;
12
use Drupal\crop\Entity\Crop;
13
use Drupal\image\ConfigurableImageEffectBase;
14
use Psr\Log\LoggerInterface;
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16
17
/**
18
 * Crops an image resource.
19
 *
20
 * @ImageEffect(
21
 *   id = "crop_crop",
22
 *   label = @Translation("Manual crop"),
23
 *   description = @Translation("Applies manually provided crop to the image.")
24
 * )
25
 */
26
class CropEffect extends ConfigurableImageEffectBase implements ContainerFactoryPluginInterface {
27
28
  /**
29
   * Crop entity storage.
30
   *
31
   * @var \Drupal\crop\CropStorageInterface
32
   */
33
  protected $storage;
34
35
  /**
36
   * Crop type entity storage.
37
   *
38
   * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
39
   */
40
  protected $typeStorage;
41
42
  /**
43
   * Crop entity.
44
   *
45
   * @var \Drupal\crop\CropInterface
46
   */
47
  protected $crop;
48
49
  /**
50
   * The image factory service.
51
   *
52
   * @var \Drupal\Core\Image\ImageFactory
53
   */
54
  protected $imageFactory;
55
56
  /**
57
   * {@inheritdoc}
58
   */
59
  public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerInterface $logger, CropStorageInterface $crop_storage, ConfigEntityStorageInterface $crop_type_storage, ImageFactory $image_factory) {
60
    parent::__construct($configuration, $plugin_id, $plugin_definition, $logger);
61
    $this->storage = $crop_storage;
62
    $this->typeStorage = $crop_type_storage;
63
    $this->imageFactory = $image_factory;
64
  }
65
66
  /**
67
   * {@inheritdoc}
68
   */
69
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
70
    return new static(
71
      $configuration,
72
      $plugin_id,
73
      $plugin_definition,
74
      $container->get('logger.factory')->get('image'),
75
      $container->get('entity_type.manager')->getStorage('crop'),
76
      $container->get('entity_type.manager')->getStorage('crop_type'),
77
      $container->get('image.factory')
78
    );
79
  }
80
81
  /**
82
   * {@inheritdoc}
83
   */
84
  public function applyEffect(ImageInterface $image) {
85
    if (empty($this->configuration['crop_type']) || !$this->typeStorage->load($this->configuration['crop_type'])) {
86
      $this->logger->error('Manual image crop failed due to misconfigured crop type on %path.', ['%path' => $image->getSource()]);
87
      return FALSE;
88
    }
89
90
    if ($crop = $this->getCrop($image)) {
91
      $anchor = $crop->anchor();
92
      $size = $crop->size();
93
94
      if (!$image->crop($anchor['x'], $anchor['y'], $size['width'], $size['height'])) {
95
        $this->logger->error('Manual image crop failed using the %toolkit toolkit on %path (%mimetype, %width x %height)', [
96
          '%toolkit' => $image->getToolkitId(),
97
          '%path' => $image->getSource(),
98
          '%mimetype' => $image->getMimeType(),
99
          '%width' => $image->getWidth(),
100
          '%height' => $image->getHeight(),
101
        ]
102
        );
103
        return FALSE;
104
      }
105
    }
106
107
    return TRUE;
108
  }
109
110
  /**
111
   * {@inheritdoc}
112
   */
113
  public function getSummary() {
114
    $summary = [
115
      '#theme' => 'crop_crop_summary',
116
      '#data' => $this->configuration,
117
    ];
118
    $summary += parent::getSummary();
119
120
    return $summary;
121
  }
122
123
  /**
124
   * {@inheritdoc}
125
   */
126
  public function defaultConfiguration() {
127
    return parent::defaultConfiguration() + [
128
      'crop_type' => NULL,
129
    ];
130
  }
131
132
  /**
133
   * {@inheritdoc}
134
   */
135
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
136
    $options = [];
137
    foreach ($this->typeStorage->loadMultiple() as $type) {
138
      $options[$type->id()] = $type->label();
139
    }
140
141
    $form['crop_type'] = [
142
      '#type' => 'select',
143
      '#title' => t('Crop type'),
144
      '#default_value' => $this->configuration['crop_type'],
145
      '#options' => $options,
146
      '#description' => t('Crop type to be used for the image style.'),
147
    ];
148
149
    return $form;
150
  }
151
152
  /**
153
   * {@inheritdoc}
154
   */
155
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
156
    parent::submitConfigurationForm($form, $form_state);
157
    $this->configuration['crop_type'] = $form_state->getValue('crop_type');
158
  }
159
160
  /**
161
   * Gets crop entity for the image.
162
   *
163
   * @param ImageInterface $image
164
   *   Image object.
165
   *
166
   * @return \Drupal\Core\Entity\EntityInterface|\Drupal\crop\CropInterface|false
167
   *   Crop entity or FALSE if crop doesn't exist.
168
   */
169
  protected function getCrop(ImageInterface $image) {
170
    if (!isset($this->crop)) {
171
      $this->crop = FALSE;
0 ignored issues
show
Documentation Bug introduced by
It seems like FALSE of type false is incompatible with the declared type object<Drupal\crop\CropInterface> of property $crop.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
172
      if ($crop = Crop::findCrop($image->getSource(), $this->configuration['crop_type'])) {
173
        $this->crop = $crop;
174
      }
175
    }
176
177
    return $this->crop;
178
  }
179
180
  /**
181
   * {@inheritdoc}
182
   */
183
  public function transformDimensions(array &$dimensions, $uri) {
184
    $crop = Crop::findCrop($uri, $this->configuration['crop_type']);
185
    if (!$crop instanceof CropInterface) {
186
      return;
187
    }
188
    $size = $crop->size();
189
190
    // The new image will have the exact dimensions defined for the crop effect.
191
    $dimensions['width'] = $size['width'];
192
    $dimensions['height'] = $size['height'];
193
  }
194
195
}
196