Passed
Pull Request — master (#301)
by
unknown
04:44 queued 02:14
created

file   F

Complexity

Total Complexity 92

Size/Duplication

Total Lines 540
Duplicated Lines 0 %

Test Coverage

Coverage 2.41%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 92
eloc 256
c 1
b 0
f 0
dl 0
loc 540
ccs 6
cts 249
cp 0.0241
rs 2

18 Methods

Rating   Name   Duplication   Size   Complexity  
A set_image_options() 0 5 1
A set_image_data() 0 20 5
A __construct() 0 6 1
A mimetype_by_filename() 0 20 6
A extension_by_filename() 0 20 6
D watermark_image() 0 78 17
A delete() 0 13 4
A create_thumbnail() 0 17 4
B resize_image() 0 44 8
A delete_cache() 0 12 4
A header_filename() 0 14 4
A disable_browser_cache() 0 3 1
A set_last_modified() 0 3 1
B rotate_image() 0 40 11
B read_image() 0 46 7
A delete_wm() 0 14 4
A is_ie_greater7() 0 3 1
B write_image() 0 26 7

How to fix   Complexity   

Complex Class

Complex classes like file often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use file, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
*
5
* @package phpBB Gallery Core
6
* @copyright (c) 2014 nickvergessen
7
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
8
*
9
*/
10
11
namespace phpbbgallery\core\file;
12
13
/**
14
 * A little class for all the actions that the gallery does on images.
15
*
16
* resize, rotate, watermark, crete thumbnail, write to hdd, send to browser
17
 *
18
 * @property \phpbbgallery\core\url url
19
 * @property \phpbb\request\request request
20
 */
21
class file
22
{
23
	const THUMBNAIL_INFO_HEIGHT = 16;
24
	const GDLIB1 = 1;
25
	const GDLIB2 = 2;
26
27
	public $chmod = 0644;
28
29
	public $errors = array();
30
	private $browser_cache = true;
31
	private $last_modified = 0;
32
33
	public $gd_version = 0;
34
35
	/** @var \phpbbgallery\core\config */
36
	public $gallery_config;
37
38
	public $image;
39
	public $image_content_type;
40
	public $image_name = '';
41
	public $image_quality = 100;
42
	public $image_size = array();
43
	public $image_source = '';
44
	public $image_type;
45
46
	public $max_file_size = 0;
47
	public $max_height = 0;
48
	public $max_width = 0;
49
50
	public $resized = false;
51
	public $rotated = false;
52
53
	public $thumb_height = 0;
54
	public $thumb_width = 0;
55
56
	public $watermark;
57
	public $watermark_size = array();
58
	public $watermark_source = '';
59
	public $watermarked = false;
60
61
	/**
62
	 * Constructor - init some basic stuff
63
	 *
64
	 * @param \phpbb\request\request $request
65
	 * @param \phpbbgallery\core\url $url
66
	 * @param \phpbbgallery\core\config $gallery_config
67
	 * @param int $gd_version
68
	 */
69 96
	public function __construct(\phpbb\request\request $request, \phpbbgallery\core\url $url, \phpbbgallery\core\config $gallery_config, $gd_version)
0 ignored issues
show
Bug introduced by
The type phpbb\request\request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
70
	{
71 96
		$this->request = $request;
72 96
		$this->url = $url;
73 96
		$this->gallery_config = $gallery_config;
74 96
		$this->gd_version = $gd_version;
75 96
	}
76
77
	public function set_image_options($max_file_size, $max_height, $max_width)
78
	{
79
		$this->max_file_size = $max_file_size;
80
		$this->max_height = $max_height;
81
		$this->max_width = $max_width;
82
	}
83
84
	public function set_image_data($source = '', $name = '', $size = 0, $force_empty_image = false)
85
	{
86
		if ($source)
87
		{
88
			$this->image_source = $source;
89
		}
90
		if ($name)
91
		{
92
			$this->image_name = $name;
93
		}
94
		if ($size)
95
		{
96
			$this->image_size['file'] = $size;
97
		}
98
		if ($force_empty_image)
99
		{
100
			$this->image = null;
101
			$this->watermarked = false;
102
			$this->rotated = false;
103
			$this->resized = false;
104
		}
105
	}
106
107
	/**
108
	 * Get image mimetype by filename
109
	 *
110
	 * Only use this, if the image is secure. As we created all these images, they should be...
111
	 * @param $filename
112
	 * @return string
113
	 */
114
	static public function mimetype_by_filename($filename)
115
	{
116
		switch (utf8_substr(strtolower($filename), -4))
0 ignored issues
show
Bug introduced by
The function utf8_substr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

116
		switch (/** @scrutinizer ignore-call */ utf8_substr(strtolower($filename), -4))
Loading history...
117
		{
118
			case '.png':
119
				return 'image/png';
120
			break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
121
			case '.gif':
122
				return 'image/gif';
123
			break;
124
			case 'jpeg':
125
			case '.jpg':
126
				return 'image/jpeg';
127
			break;
128
			case '.webp':
129
				return 'image/webp';
130
			break;
131
		}
132
133
		return '';
134
	}
135
136
	static public function extension_by_filename($filename)
137
	{
138
		switch (utf8_substr(strtolower($filename), -4))
0 ignored issues
show
Bug introduced by
The function utf8_substr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

138
		switch (/** @scrutinizer ignore-call */ utf8_substr(strtolower($filename), -4))
Loading history...
139
		{
140
			case '.png':
141
				return 'png';
142
			break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
143
			case '.gif':
144
				return 'gif';
145
			break;
146
			case 'jpeg':
147
			case '.jpg':
148
				return 'jpg';
149
			break;
150
			case '.webp':
151
				return 'webp';
152
			break;
153
		}
154
155
		return '';
156
	}
157
158
	/**
159
	 * Read image
160
	 * @param bool $force_filesize
161
	 * @return bool
162
	 */
163
	public function read_image($force_filesize = false)
164
	{
165
		if (!file_exists($this->image_source))
166
		{
167
			return false;
168
		}
169
170
		switch (utf8_substr(strtolower($this->image_source), -4))
0 ignored issues
show
Bug introduced by
The function utf8_substr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

170
		switch (/** @scrutinizer ignore-call */ utf8_substr(strtolower($this->image_source), -4))
Loading history...
171
		{
172
			case '.png':
173
				$this->image = imagecreatefrompng($this->image_source);
174
				imagealphablending($this->image, true); // Set alpha blending on ...
175
				imagesavealpha($this->image, true); // ... and save alphablending!
176
				$this->image_type = 'png';
177
			break;
178
			case '.webp':
179
				$this->image = imagecreatefromwebp($this->image_source);
180
				$this->image_type = 'webp';
181
			break;
182
			case '.gif':
183
				$this->image = imagecreatefromgif($this->image_source);
184
				$this->image_type = 'gif';
185
			break;
186
			default:
187
				$this->image = imagecreatefromjpeg($this->image_source);
188
				$this->image_type = 'jpeg';
189
			break;
190
		}
191
192
		$file_size = 0;
193
		if (isset($this->image_size['file']))
194
		{
195
			$file_size = $this->image_size['file'];
196
		}
197
		else if ($force_filesize)
198
		{
199
			$file_size = @filesize($this->image_source);
200
		}
201
202
		$image_size = getimagesize($this->image_source);
203
204
		$this->image_size['file'] = $file_size;
205
		$this->image_size['width'] = $image_size[0];
206
		$this->image_size['height'] = $image_size[1];
207
208
		$this->image_content_type = $image_size['mime'];
209
	}
210
211
	/**
212
	 * Write image to disk
213
	 * @param $destination
214
	 * @param int $quality
215
	 * @param bool $destroy_image
216
	 */
217
	public function write_image($destination, $quality = -1, $destroy_image = false)
218
	{
219
		if ($quality == -1)
220
		{
221
			$quality = $this->gallery_config->get('jpg_quality');
222
		}
223
		switch ($this->image_type)
224
		{
225
			case 'jpeg':
226
				imagejpeg($this->image, $destination, $quality);
227
			break;
228
			case 'png':
229
				imagepng($this->image, $destination);
230
			break;
231
			case 'webp':
232
				imagewebp($this->image, $destination);
233
			break;
234
			case 'gif':
235
				imagegif($this->image, $destination);
236
			break;
237
		}
238
		@chmod($destination, $this->chmod);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for chmod(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

238
		/** @scrutinizer ignore-unhandled */ @chmod($destination, $this->chmod);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
239
240
		if ($destroy_image)
241
		{
242
			imagedestroy($this->image);
243
		}
244
	}
245
246
	/**
247
	 * Get a browser friendly UTF-8 encoded filename
248
	 *
249
	 * @param $file
250
	 * @return string
251
	 */
252
	public function header_filename($file)
253
	{
254
		$raw = $this->request->server('HTTP_USER_AGENT');
255
		$user_agent = htmlspecialchars($raw);
256
257
		// There be dragons here.
258
		// Not many follows the RFC...
259
		if (strpos($user_agent, 'MSIE') !== false || strpos($user_agent, 'Safari') !== false || strpos($user_agent, 'Konqueror') !== false)
260
		{
261
			return "filename=" . rawurlencode($file);
262
		}
263
264
		// follow the RFC for extended filename for the rest
265
		return "filename*=UTF-8''" . rawurlencode($file);
266
	}
267
268
	/**
269
	* We need to disable the "last-modified" caching for guests and in cases of image-errors,
270
	* so that they can view them, if they logged in or the error was fixed.
271
	*/
272
	public function disable_browser_cache()
273
	{
274
		$this->browser_cache = false;
275
	}
276
277
	/**
278
	 * Collect the last timestamp where something changed.
279
	 * This must contain:
280
	 *    - Last change of the file
281
	 *    - Last change of user's permissions
282
	 *    - Last change of user's groups
283
	 *    - Last change of watermark config
284
	 *    - Last change of watermark file
285
	 * @param $timestamp
286
	 */
287
	public function set_last_modified($timestamp)
288
	{
289
		$this->last_modified = max($timestamp, $this->last_modified);
290
	}
291
292
	static public function is_ie_greater7($browser)
293
	{
294
		return (bool) preg_match('/msie (\d{2,3}|[89]+).[0-9.]*;/', strtolower($browser));
295
	}
296
297
	public function create_thumbnail($max_width, $max_height, $print_details = false, $additional_height = 0, $image_size = array())
298
	{
299
		$this->resize_image($max_width, $max_height, (($print_details) ? $additional_height : 0));
300
301
		// Create image details credits to Dr.Death
302
		if ($print_details && sizeof($image_size))
303
		{
304
			$dimension_font = 1;
305
			$dimension_string = $image_size['width'] . "x" . $image_size['height'] . "(" . intval($image_size['file'] / 1024) . "KiB)";
306
			$dimension_colour = imagecolorallocate($this->image, 255, 255, 255);
307
			$dimension_height = imagefontheight($dimension_font);
308
			$dimension_width = imagefontwidth($dimension_font) * strlen($dimension_string);
309
			$dimension_x = ($this->image_size['width'] - $dimension_width) / 2;
310
			$dimension_y = $this->image_size['height'] + (($additional_height - $dimension_height) / 2);
311
			$black_background = imagecolorallocate($this->image, 0, 0, 0);
312
			imagefilledrectangle($this->image, 0, $this->thumb_height, $this->thumb_width, $this->thumb_height + $additional_height, $black_background);
313
			imagestring($this->image, 1, $dimension_x, $dimension_y, $dimension_string, $dimension_colour);
314
		}
315
	}
316
317
	public function resize_image($max_width, $max_height, $additional_height = 0)
318
	{
319
		if (!$this->image)
320
		{
321
			$this->read_image();
322
		}
323
324
		if (($this->image_size['height'] <= $max_height) && ($this->image_size['width'] <= $max_width))
325
		{
326
			// image is small enough, nothing to do here.
327
			return;
328
		}
329
330
		if (($this->image_size['height'] / $max_height) > ($this->image_size['width'] / $max_width))
331
		{
332
			$this->thumb_height	= $max_height;
333
			$this->thumb_width	= round($max_width * (($this->image_size['width'] / $max_width) / ($this->image_size['height'] / $max_height)));
334
		}
335
		else
336
		{
337
			$this->thumb_height	= round($max_height * (($this->image_size['height'] / $max_height) / ($this->image_size['width'] / $max_width)));
338
			$this->thumb_width	= $max_width;
339
		}
340
341
		$image_copy = (($this->gd_version == self::GDLIB1) ? @imagecreate($this->thumb_width, $this->thumb_height + $additional_height) : @imagecreatetruecolor($this->thumb_width, $this->thumb_height + $additional_height));
342
		if ($this->image_type != 'jpeg')
343
		{
344
			imagealphablending($image_copy, false);
0 ignored issues
show
Bug introduced by
It seems like $image_copy can also be of type false; however, parameter $image of imagealphablending() does only seem to accept GdImage|resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

344
			imagealphablending(/** @scrutinizer ignore-type */ $image_copy, false);
Loading history...
345
			imagesavealpha($image_copy, true);
0 ignored issues
show
Bug introduced by
It seems like $image_copy can also be of type false; however, parameter $image of imagesavealpha() does only seem to accept GdImage|resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

345
			imagesavealpha(/** @scrutinizer ignore-type */ $image_copy, true);
Loading history...
346
			$transparent = imagecolorallocatealpha($image_copy, 255, 255, 255, 127);
0 ignored issues
show
Bug introduced by
It seems like $image_copy can also be of type false; however, parameter $image of imagecolorallocatealpha() does only seem to accept GdImage|resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

346
			$transparent = imagecolorallocatealpha(/** @scrutinizer ignore-type */ $image_copy, 255, 255, 255, 127);
Loading history...
347
			imagefilledrectangle($image_copy, 0, 0, $this->thumb_width, $this->thumb_height + $additional_height, $transparent);
0 ignored issues
show
Bug introduced by
It seems like $image_copy can also be of type false; however, parameter $image of imagefilledrectangle() does only seem to accept GdImage|resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

347
			imagefilledrectangle(/** @scrutinizer ignore-type */ $image_copy, 0, 0, $this->thumb_width, $this->thumb_height + $additional_height, $transparent);
Loading history...
348
		}
349
350
		$resize_function = ($this->gd_version == self::GDLIB1) ? 'imagecopyresized' : 'imagecopyresampled';
351
		$resize_function($image_copy, $this->image, 0, 0, 0, 0, $this->thumb_width, $this->thumb_height, $this->image_size['width'], $this->image_size['height']);
352
353
		imagealphablending($image_copy, true);
354
		imagesavealpha($image_copy, true);
355
		$this->image = $image_copy;
356
357
		$this->image_size['height'] = $this->thumb_height;
358
		$this->image_size['width'] = $this->thumb_width;
359
360
		$this->resized = true;
361
	}
362
363
	/**
364
	 * Rotate the image
365
	 * Usage optimized for 0º, 90º, 180º and 270º because of the height and width
366
	 *
367
	 * @param $angle
368
	 * @param $ignore_dimensions
369
	 */
370
	public function rotate_image($angle, $ignore_dimensions)
371
	{
372
		if (!function_exists('imagerotate'))
373
		{
374
			$this->errors[] = array('ROTATE_IMAGE_FUNCTION', $angle);
375
			return;
376
		}
377
		if (($angle <= 0) || (($angle % 90) != 0))
378
		{
379
			$this->errors[] = array('ROTATE_IMAGE_ANGLE', $angle);
380
			return;
381
		}
382
383
		if (!$this->image)
384
		{
385
			$this->read_image();
386
		}
387
		if ((($angle / 90) % 2) == 1)
388
		{
389
			// Left or Right, we need to switch the height and width
390
			if (!$ignore_dimensions && (($this->image_size['height'] > $this->max_width) || ($this->image_size['width'] > $this->max_height)))
391
			{
392
				// image would be to wide/high
393
				if ($this->image_size['height'] > $this->max_width)
394
				{
395
					$this->errors[] = array('ROTATE_IMAGE_WIDTH');
396
				}
397
				if ($this->image_size['width'] > $this->max_height)
398
				{
399
					$this->errors[] = array('ROTATE_IMAGE_HEIGHT');
400
				}
401
				return;
402
			}
403
			$new_width = $this->image_size['height'];
404
			$this->image_size['height'] = $this->image_size['width'];
405
			$this->image_size['width'] = $new_width;
406
		}
407
		$this->image = imagerotate($this->image, $angle, 0);
408
409
		$this->rotated = true;
410
	}
411
412
	/**
413
	 * Watermark the image:
414
	 *
415
	 * @param $watermark_source
416
	 * @param int $watermark_position summary of the parameters for vertical and horizontal adjustment
417
	 * @param int $min_height
418
	 * @param int $min_width
419
	 */
420
	public function watermark_image($watermark_source, $watermark_position = 20, $min_height = 0, $min_width = 0)
421
	{
422
		$this->watermark_source = $watermark_source;
423
		if (!$this->watermark_source || !file_exists($this->watermark_source))
424
		{
425
			$this->errors[] = array('WATERMARK_IMAGE_SOURCE');
426
			return;
427
		}
428
429
		if (!$this->image)
430
		{
431
			$this->read_image();
432
		}
433
434
		if (($min_height && ($this->image_size['height'] < $min_height)) || ($min_width && ($this->image_size['width'] < $min_width)))
435
		{
436
			return;
437
			//$this->errors[] = array('WATERMARK_IMAGE_DIMENSION');
438
		}
439
		$get_dot = strrpos($this->image_source, '.');
440
		$get_wm_name = substr_replace($this->image_source, '_wm', $get_dot, 0);
441
		if (file_exists($get_wm_name))
0 ignored issues
show
Bug introduced by
It seems like $get_wm_name can also be of type array; however, parameter $filename of file_exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

441
		if (file_exists(/** @scrutinizer ignore-type */ $get_wm_name))
Loading history...
442
		{
443
			$this->image_source = $get_wm_name;
444
			$this->read_image();
445
		}
446
		else
447
		{
448
			$this->watermark_size = getimagesize($this->watermark_source);
449
			switch ($this->watermark_size['mime'])
450
			{
451
				case 'image/png':
452
					$imagecreate = 'imagecreatefrompng';
453
					break;
454
				case 'image/webp':
455
					$imagecreate = 'imagecreatefromwebp';
456
					break;
457
				case 'image/gif':
458
					$imagecreate = 'imagecreatefromgif';
459
					break;
460
				default:
461
					$imagecreate = 'imagecreatefromjpeg';
462
					break;
463
			}
464
465
			// Get the watermark as resource.
466
			if (($this->watermark = $imagecreate($this->watermark_source)) === false)
467
			{
468
				$this->errors[] = array('WATERMARK_IMAGE_IMAGECREATE');
469
			}
470
471
			$phpbb_gallery_constants = new \phpbbgallery\core\constants();
472
			// Where do we display the watermark? up-left, down-right, ...?
473
			$dst_x = (($this->image_size['width'] * 0.5) - ($this->watermark_size[0] * 0.5));
474
			$dst_y = ($this->image_size['height'] - $this->watermark_size[1] - 5);
475
			if ($watermark_position & $phpbb_gallery_constants::WATERMARK_LEFT)
476
			{
477
				$dst_x = 5;
478
			}
479
			else if ($watermark_position & $phpbb_gallery_constants::WATERMARK_RIGHT)
480
			{
481
				$dst_x = ($this->image_size['width'] - $this->watermark_size[0] - 5);
482
			}
483
			if ($watermark_position & $phpbb_gallery_constants::WATERMARK_TOP)
484
			{
485
				$dst_y = 5;
486
			}
487
			else if ($watermark_position & $phpbb_gallery_constants::WATERMARK_MIDDLE)
488
			{
489
				$dst_y = (($this->image_size['height'] * 0.5) - ($this->watermark_size[1] * 0.5));
490
			}
491
			imagecopy($this->image, $this->watermark, $dst_x, $dst_y, 0, 0, $this->watermark_size[0], $this->watermark_size[1]);
0 ignored issues
show
Bug introduced by
It seems like $dst_x can also be of type double; however, parameter $dst_x of imagecopy() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

491
			imagecopy($this->image, $this->watermark, /** @scrutinizer ignore-type */ $dst_x, $dst_y, 0, 0, $this->watermark_size[0], $this->watermark_size[1]);
Loading history...
Bug introduced by
It seems like $dst_y can also be of type double; however, parameter $dst_y of imagecopy() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

491
			imagecopy($this->image, $this->watermark, $dst_x, /** @scrutinizer ignore-type */ $dst_y, 0, 0, $this->watermark_size[0], $this->watermark_size[1]);
Loading history...
492
			imagedestroy($this->watermark);
493
			$this->write_image($get_wm_name);
494
			$this->image_source = $get_wm_name;
495
			$this->read_image();
496
		}
497
		$this->watermarked = true;
498
	}
499
500
	/**
501
	* Delete file from disc.
502
	*
503
	* @param	mixed		$files		String with filename or an array of filenames
504
	*									Array-Format: $image_id => $filename
505
	* @param	array		$locations	Array of valid url::path()s where the image should be deleted from
506
	*/
507
	public function delete($files, $locations = array('thumbnail', 'medium', 'upload'))
508
	{
509
		if (!is_array($files))
510
		{
511
			$files = array(1 => $files);
512
		}
513
		// Let's delete watermarked
514
		$this->delete_wm($files);
515
		foreach ($files as $image_id => $file)
516
		{
517
			foreach ($locations as $location)
518
			{
519
				@unlink($this->url->path($location) . $file);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

519
				/** @scrutinizer ignore-unhandled */ @unlink($this->url->path($location) . $file);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
Bug introduced by
Are you sure $this->url->path($location) of type false|mixed|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

519
				@unlink(/** @scrutinizer ignore-type */ $this->url->path($location) . $file);
Loading history...
520
			}
521
		}
522
	}
523
524
	/**
525
	 * @param $files
526
	 * @param array $locations
527
	 */
528
	public function delete_cache($files, $locations = array('thumbnail', 'medium'))
529
	{
530
		if (!is_array($files))
531
		{
532
			$files = array(1 => $files);
533
		}
534
		$this->delete_wm($files);
535
		foreach ($files as $image_id => $file)
536
		{
537
			foreach ($locations as $location)
538
			{
539
				@unlink($this->url->path($location) . $file);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

539
				/** @scrutinizer ignore-unhandled */ @unlink($this->url->path($location) . $file);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
Bug introduced by
Are you sure $this->url->path($location) of type false|mixed|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

539
				@unlink(/** @scrutinizer ignore-type */ $this->url->path($location) . $file);
Loading history...
540
			}
541
		}
542
	}
543
544
	/**
545
	 * @param $files
546
	 */
547
	public function delete_wm($files)
548
	{
549
		$locations = array('upload', 'medium');
550
		if (!is_array($files))
551
		{
552
			$files = array(1 => $files);
553
		}
554
		foreach ($files as $image_id => $file)
555
		{
556
			$get_dot = strrpos($file, '.');
557
			$get_wm_name = substr_replace($file, '_wm', $get_dot, 0);
558
			foreach ($locations as $location)
559
			{
560
				@unlink($this->url->path($location) . $get_wm_name);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

560
				/** @scrutinizer ignore-unhandled */ @unlink($this->url->path($location) . $get_wm_name);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
Bug introduced by
Are you sure $this->url->path($location) of type false|mixed|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

560
				@unlink(/** @scrutinizer ignore-type */ $this->url->path($location) . $get_wm_name);
Loading history...
561
			}
562
		}
563
	}
564
}
565