Issues (899)

core/controller/file.php (10 issues)

1
<?php
2
/**
3
 * phpBB Gallery - Core Extension
4
 *
5
 * @package   phpbbgallery/core
6
 * @author    nickvergessen
7
 * @author    satanasov
8
 * @author    Leinad4Mind
9
 * @copyright 2014 nickvergessen, 2014- satanasov, 2018- Leinad4Mind
10
 * @license   GPL-2.0-only
11
 */
12
13
namespace phpbbgallery\core\controller;
14
15
class file
16
{
17
	/* @var \phpbb\config\config */
0 ignored issues
show
The type phpbb\config\config 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...
18
	protected $config;
19
20
	/* @var \phpbb\db\driver\driver */
0 ignored issues
show
The type phpbb\db\driver\driver 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...
21
	protected $db;
22
23
	/* @var \phpbb\user */
0 ignored issues
show
The type phpbb\user 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...
24
	protected $user;
25
26
	/* @var \phpbbgallery\core\auth\auth */
27
	protected $auth;
28
29
	/* @var \phpbbgallery\core\user */
30
	protected $gallery_user;
31
32
	/* @var string */
33
	protected $path_source;
34
35
	/* @var string */
36
	protected $path_medium;
37
38
	/* @var string */
39
	protected $path_mini;
40
41
	/* @var string */
42
	protected $path_watermark;
43
44
	/* @var \phpbbgallery\core\file\file */
45
	protected $tool;
46
47
	/* @var \phpbb\request\request */
0 ignored issues
show
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...
48
	protected $request;
49
50
	/* @var string */
51
	protected $table_albums;
52
53
	/* @var string */
54
	protected $table_images;
55
56
	/* @var string */
57
	protected $path;
58
59
	/* @var array */
60
	protected $data;
61
62
	/* @var string */
63
	protected $error;
64
65
	/* @var string */
66
	protected $image_src;
67
68
	/* @var boolean */
69
	protected $use_watermark = false;
70
71
	/**
72
	 * Constructor
73
	 *
74
	 * @param \phpbb\config\config $config Config object
75
	 * @param \phpbb\db\driver\driver|\phpbb\db\driver\driver_interface $db Database object
76
	 * @param \phpbb\user $user User object
77
	 * @param \phpbbgallery\core\auth\auth $gallery_auth Gallery auth object
78
	 * @param \phpbbgallery\core\user $gallery_user Gallery user object
79
	 * @param \phpbbgallery\core\file\file $tool
80
	 * @param \phpbb\request\request $request
81
	 * @param $source_path
82
	 * @param $medium_path
83
	 * @param $mini_path
84
	 * @param $watermark_file
85
	 * @param $albums_table
86
	 * @param $images_table
87
	 * @internal param \phpbbgallery\core\album\display $display Albums display object
88
	 */
89
	public function __construct(\phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\user $user, \phpbbgallery\core\auth\auth $gallery_auth,
0 ignored issues
show
The type phpbb\db\driver\driver_interface 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...
90
	\phpbbgallery\core\user $gallery_user, \phpbbgallery\core\file\file $tool, \phpbb\request\request $request,
91
	$source_path, $medium_path, $mini_path, $watermark_file, $albums_table, $images_table)
92
	{
93
		$this->config = $config;
94
		$this->db = $db;
95
		$this->user = $user;
96
		$this->auth = $gallery_auth;
97
		$this->gallery_user = $gallery_user;
98
		$this->tool = $tool;
99
		$this->request = $request;
100
		$this->path_source = $source_path;
101
		$this->path_medium = $medium_path;
102
		$this->path_mini = $mini_path;
103
		$this->path_watermark = $watermark_file;
104
		$this->table_albums = $albums_table;
105
		$this->table_images = $images_table;
106
	}
107
108
	/**
109
	* Image File Controller
110
	*	Route: gallery/image/{image_id}/source
111
	*
112
	* @param	int		$image_id
113
	* @return \Symfony\Component\HttpFoundation\Response A Symfony Response object
0 ignored issues
show
The type Symfony\Component\HttpFoundation\Response 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...
114
	*/
115
	public function source($image_id)
116
	{
117
		$this->auth->load_user_permissions($this->user->data['user_id']);
118
		$this->path = $this->path_source;
119
		$this->load_data($image_id);
120
		$this->check_auth();
121
122
		if (!file_exists($this->path_source . $this->data['image_filename']))
123
		{
124
			$sql = 'UPDATE ' . $this->table_images . '
125
				SET image_filemissing = 1
126
				WHERE image_id = ' . (int) $image_id;
127
			$this->db->sql_query($sql);
128
129
			// trigger_error('IMAGE_NOT_EXIST');
130
			$this->error = 'image_not_exist.jpg';
131
			$this->data['image_filename'] = 'image_not_exist.jpg';
132
			$this->data['image_name'] = 'Image is missing!';
133
			$this->data['image_user_id'] = 1;
134
			$this->data['image_status'] = 2;
135
			$this->data['album_id'] = 0;
136
			$this->data['album_user_id'] = 1;
137
			$this->data['image_filemissing'] = 0;
138
			$this->data['album_watermark'] = 0;
139
		}
140
141
		$this->generate_image_src();
142
		// @todo Enable watermark
143
144
		$this->use_watermark = $this->config['phpbb_gallery_watermark_enabled'] && $this->data['album_watermark'] && !$this->auth->acl_check('i_watermark', $this->data['album_id'], $this->data['album_user_id']);
145
146
		$this->tool->set_image_options($this->config['phpbb_gallery_max_filesize'], $this->config['phpbb_gallery_max_height'], $this->config['phpbb_gallery_max_width']);
147
		$this->tool->set_image_data($this->image_src, $this->data['image_name']);
148
		if ($this->error || !$this->user->data['is_registered'])
149
		{
150
			$this->tool->disable_browser_cache();
151
		}
152
153
		if (!$this->user->data['is_bot'] && !$this->error)
154
		{
155
			$sql = 'UPDATE ' . $this->table_images . '
156
				SET image_view_count = image_view_count + 1
157
				WHERE image_id = ' . (int) $image_id;
158
			$this->db->sql_query($sql);
159
		}
160
161
		return $this->display();
162
	}
163
164
	/**
165
	* Image File Controller
166
	*	Route: gallery/image/{image_id}/medium
167
	*
168
	* @param	int		$image_id
169
	* @return \Symfony\Component\HttpFoundation\Response A Symfony Response object
170
	*/
171
	public function medium($image_id)
172
	{
173
174
		$this->path = $this->path_medium;
175
		$this->load_data($image_id);
176
		$this->check_auth();
177
178
		$this->generate_image_src();
179
180
		if (!file_exists($this->image_src))
181
		{
182
			$this->resize($image_id, $this->config['phpbb_gallery_medium_width'], $this->config['phpbb_gallery_medium_height'], 'filesize_medium');
183
			$this->generate_image_src();
184
		}
185
		$this->auth->load_user_permissions($this->user->data['user_id']);
186
		$this->use_watermark = $this->config['phpbb_gallery_watermark_enabled'] && $this->data['album_watermark'] && !$this->auth->acl_check('i_watermark', $this->data['album_id'], $this->data['album_user_id']);
187
		$this->tool->set_image_options($this->config['phpbb_gallery_max_filesize'], $this->config['phpbb_gallery_max_height'], $this->config['phpbb_gallery_max_width']);
188
		$this->tool->set_image_data($this->image_src, $this->data['image_name']);
189
		if ($this->error || !$this->user->data['is_registered'])
190
		{
191
			$this->tool->disable_browser_cache();
192
		}
193
194
		$this->resize($image_id, $this->config['phpbb_gallery_medium_width'], $this->config['phpbb_gallery_medium_height'], 'filesize_medium');
195
196
		return $this->display();
197
	}
198
199
	/**
200
	* Image File Controller
201
	*	Route: gallery/image/{image_id}/mini
202
	*
203
	* @param	int		$image_id
204
	* @return \Symfony\Component\HttpFoundation\Response A Symfony Response object
205
	*/
206
	public function mini($image_id)
207
	{
208
		$this->path = $this->path_mini;
209
		$this->load_data($image_id);
210
		$this->check_auth();
211
		$this->generate_image_src();
212
213
		if (!file_exists($this->image_src))
214
		{
215
			$this->resize($image_id, $this->config['phpbb_gallery_thumbnail_width'], $this->config['phpbb_gallery_thumbnail_height'], 'filesize_cache');
216
			$this->generate_image_src();
217
		}
218
		$this->tool->set_image_options($this->config['phpbb_gallery_max_filesize'], $this->config['phpbb_gallery_max_height'], $this->config['phpbb_gallery_max_width']);
219
		$this->tool->set_image_data($this->image_src, $this->data['image_name']);
220
		if ($this->error || !$this->user->data['is_registered'])
221
		{
222
			$this->tool->disable_browser_cache();
223
		}
224
225
		$this->resize($image_id, $this->config['phpbb_gallery_thumbnail_width'], $this->config['phpbb_gallery_thumbnail_height'], 'filesize_cache');
226
227
		return $this->display();
228
	}
229
230
	public function load_data($image_id)
231
	{
232
		if ($image_id == 0)
233
		{
234
			$this->error = 'image_not_exist.jpg';
235
			$this->data['image_filename'] = 'image_not_exist.jpg';
236
			$this->data['image_name'] = 'Image is missing!';
237
			$this->data['image_user_id'] = 1;
238
			$this->data['image_status'] = 2;
239
			$this->data['album_id'] = 0;
240
			$this->data['album_user_id'] = 1;
241
			$this->data['image_filemissing'] = 0;
242
			$this->data['album_watermark'] = 0;
243
		}
244
		else
245
		{
246
			$sql = 'SELECT *
247
				FROM ' . $this->table_images . ' i
248
				LEFT JOIN ' . $this->table_albums . ' a
249
					ON (i.image_album_id = a.album_id)
250
				WHERE i.image_id = ' . (int) $image_id;
251
			$result = $this->db->sql_query($sql);
252
			$this->data = $this->db->sql_fetchrow($result);
253
			$this->db->sql_freeresult($result);
254
255
			if (!$this->data || !$this->data['album_id'])
256
			{
257
				// Image or album does not exist
258
				// trigger_error('INVALID_IMAGE');
259
				$this->error = 'not_authorised.jpg';
260
				$this->data['image_filename'] = 'not_authorised.jpg';
261
				$this->data['image_name'] = 'You are not authorized!';
262
				$this->data['image_user_id'] = 1;
263
				$this->data['image_status'] = 2;
264
				$this->data['album_id'] = 0;
265
				$this->data['album_user_id'] = 1;
266
				$this->data['image_filemissing'] = 0;
267
				$this->data['album_watermark'] = 0;
268
269
			}
270
		}
271
	}
272
273
	public function check_auth()
274
	{
275
		$this->auth->load_user_permissions($this->user->data['user_id']);
276
		$zebra_array = $this->auth->get_user_zebra($this->user->data['user_id']);
277
		// Check permissions
278
		if (($this->data['image_user_id'] != $this->user->data['user_id']) && ($this->data['image_status'] == (int) \phpbbgallery\core\block::STATUS_ORPHAN))
279
		{
280
			// The image is currently being uploaded
281
			// trigger_error('NOT_AUTHORISED');
282
			$this->error = 'not_authorised.jpg';
283
			$this->data['image_filename'] = 'not_authorised.jpg';
284
			$this->data['image_name'] = 'You are not authorized!';
285
			$this->data['image_user_id'] = 1;
286
			$this->data['image_status'] = 2;
287
			$this->data['album_id'] = 0;
288
			$this->data['album_user_id'] = 1;
289
			$this->data['image_filemissing'] = 0;
290
			$this->data['album_watermark'] = 0;
291
		}
292
		if (!$this->auth->acl_check('i_view', $this->data['album_id'], $this->data['album_user_id']) || (
293
				!$this->auth->acl_check('m_status', $this->data['album_id'], $this->data['album_user_id'])
294
				&& $this->data['image_status'] == (int) \phpbbgallery\core\block::STATUS_UNAPPROVED
295
				&& $this->data['image_user_id'] != $this->user->data['user_id']
296
			))
297
		{
298
			// Missing permissions
299
			// trigger_error('NOT_AUTHORISED');
300
			$this->error = 'not_authorised.jpg';
301
			$this->data['image_filename'] = 'not_authorised.jpg';
302
			$this->data['image_name'] = 'You are not authorized!';
303
			$this->data['image_user_id'] = 1;
304
			$this->data['image_status'] = 2;
305
			$this->data['album_id'] = 0;
306
			$this->data['album_user_id'] = 1;
307
			$this->data['image_filemissing'] = 0;
308
			$this->data['album_watermark'] = 0;
309
		}
310
		if (($this->auth->get_zebra_state($zebra_array, (int) $this->data['album_user_id'], $this->data['album_id']) < (int) $this->data['album_auth_access'] && !$this->error))
311
		{
312
			// Zebra parameters not met
313
			// trigger_error('NOT_AUTHORISED');
314
			$this->error = 'not_authorised.jpg';
315
			$this->data['image_filename'] = 'not_authorised.jpg';
316
			$this->data['image_name'] = 'You are not authorized!';
317
			$this->data['image_user_id'] = 1;
318
			$this->data['image_status'] = 2;
319
			$this->data['album_id'] = 0;
320
			$this->data['album_user_id'] = 1;
321
			$this->data['image_filemissing'] = 0;
322
			$this->data['album_watermark'] = 0;
323
		}
324
	}
325
326
	public function generate_image_src()
327
	{
328
		$this->image_src = $this->path  . $this->data['image_filename'];
329
330
		if ($this->data['image_filemissing'] || !file_exists($this->path_source . $this->data['image_filename']))
331
		{
332
			$sql = 'UPDATE ' . $this->table_images . '
333
				SET image_filemissing = 1
334
				WHERE image_id = ' . (int) $this->data['image_id'];
335
			$this->db->sql_query($sql);
336
337
			// trigger_error('IMAGE_NOT_EXIST');
338
			$this->error = 'image_not_exist.jpg';
339
			$this->data['image_filename'] = 'image_not_exist.jpg';
340
			$this->data['image_name'] = 'Image is missing!';
341
			$this->data['image_user_id'] = 1;
342
			$this->data['image_status'] = 2;
343
			$this->data['album_id'] = 0;
344
			$this->data['album_user_id'] = 1;
345
			$this->data['image_filemissing'] = 0;
346
			$this->data['album_watermark'] = 0;
347
		}
348
349
		$this->check_hot_link();
350
351
		// There was a reason to not display the image, so we send an error-image
352
		if ($this->error)
353
		{
354
			$this->data['image_filename'] = $this->user->data['user_lang'] . '_' . $this->error;
355
			if (!file_exists($this->path . $this->data['image_filename']))
356
			{
357
				$this->data['image_filename'] = $this->error;
358
			}
359
			$this->image_src = $this->path . $this->data['image_filename'];
360
			$this->use_watermark = false;
361
		}
362
	}
363
364
	/**
365
	* Image File Controller
366
	*	Route: gallery/image/{image_id}/x
367
	*
368
	* @return \Symfony\Component\HttpFoundation\BinaryFileResponseResponse A Symfony Response object
0 ignored issues
show
The type Symfony\Component\HttpFo...aryFileResponseResponse 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...
369
	*/
370
	public function display()
371
	{
372
		$this->tool->set_last_modified($this->gallery_user->get_data('user_permissions_changed'));
373
		$this->tool->set_last_modified($this->config['phpbb_gallery_watermark_changed']);
374
375
		// Watermark
376
		if ($this->use_watermark)
377
		{
378
			//$this->tool->set_last_modified(@filemtime($this->path_watermark));
379
			//$this->tool->watermark_image($this->path_watermark, $this->config['phpbb_gallery_watermark_position'], $this->config['phpbb_gallery_watermark_height'], $this->config['phpbb_gallery_watermark_width']);
380
			$this->tool->set_last_modified(@filemtime($this->config['phpbb_gallery_watermark_source']));
381
			$this->tool->watermark_image($this->config['phpbb_gallery_watermark_source'], $this->config['phpbb_gallery_watermark_position'], $this->config['phpbb_gallery_watermark_height'], $this->config['phpbb_gallery_watermark_width']);
382
		}
383
384
		// Let's check image is loaded
385
		if (!$this->tool->image_content_type)
386
		{
387
			$this->tool->image_content_type = $this->tool->mimetype_by_filename($this->tool->image_source);
388
			if (!$this->tool->image_content_type)
389
			{
390
				trigger_error('NO_MIMETYPE_MATCHED');
391
			}
392
		}
393
394
		if (!$this->tool->image_type)
395
		{
396
			$this->tool->image_type = $this->tool->extension_by_filename($this->tool->image_source);
397
			if (!$this->tool->image_type)
398
			{
399
				trigger_error('NO_EXTENSION_MATCHED');
400
			}
401
		}
402
403
		$response = new \Symfony\Component\HttpFoundation\BinaryFileResponse($this->tool->image_source);
0 ignored issues
show
The type Symfony\Component\HttpFo...tion\BinaryFileResponse 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...
404
405
		$response->headers->set('Pragma', 'public');
406
		$response->headers->set('Content-Type', $this->tool->image_content_type);
407
		if ($this->tool->is_ie_greater7($this->user->browser))
408
		{
409
			$response->headers->set('X-Content-Type-Options', 'nosniff');
410
		}
411
		if (empty($this->user->browser) || (!$this->tool->is_ie_greater7($this->user->browser) && (strpos(strtolower($this->user->browser), 'msie') !== false)))
412
		{
413
			$response->headers->set('Content-Disposition', 'attachment; ' . $this->tool->header_filename(htmlspecialchars_decode($this->tool->image_name) . '.' . $this->tool->image_type));
414
			if (empty($this->user->browser) || (strpos(strtolower($this->user->browser), 'msie 6.0') !== false))
415
			{
416
				$response->headers->set('expires', '-1');
417
			}
418
		}
419
		else
420
		{
421
			$response->headers->set('Content-Disposition', 'inline; ' . $this->tool->header_filename(htmlspecialchars_decode($this->tool->image_name) . '.' . $this->tool->image_type));
422
			if ($this->tool->is_ie_greater7($this->user->browser))
423
			{
424
				$response->headers->set('X-Download-Options', 'noopen');
425
			}
426
		}
427
428
		return $response;
429
	}
430
431
	protected function resize($image_id, $resize_width, $resize_height, $store_filesize = '', $put_details = false)
432
	{
433
		if (!file_exists($this->image_src))
434
		{
435
			$this->tool->set_image_data($this->path_source . $this->data['image_filename']);
436
			$this->tool->read_image(true);
437
438
			$image_size['file'] = $this->tool->image_size['file'];
0 ignored issues
show
Comprehensibility Best Practice introduced by
$image_size was never initialized. Although not strictly required by PHP, it is generally a good practice to add $image_size = array(); before regardless.
Loading history...
439
			$image_size['width'] = $this->tool->image_size['width'];
440
			$image_size['height'] = $this->tool->image_size['height'];
441
442
			$this->tool->set_image_data($this->image_src);
443
444
			if (($image_size['width'] > $resize_width) || ($image_size['height'] > $resize_height))
445
			{
446
				$this->tool->create_thumbnail($resize_width, $resize_height, $put_details, \phpbbgallery\core\file\file::THUMBNAIL_INFO_HEIGHT, $image_size);
447
			}
448
449
//			if ($phpbb_ext_gallery->config->get($mode . '_cache'))
450
//			{
451
			$this->tool->write_image($this->image_src, $this->config['phpbb_gallery_jpg_quality'], false);
452
453
			if ($store_filesize)
454
			{
455
				$this->data[$store_filesize] = @filesize($this->image_src);
456
				$sql = 'UPDATE ' . $this->table_images . '
457
					SET ' . $this->db->sql_build_array('UPDATE', array(
458
						$store_filesize => $this->data[$store_filesize],
459
					)) . '
460
					WHERE ' . $this->db->sql_in_set('image_id', $image_id);
461
				$this->db->sql_query($sql);
462
			}
463
464
//			}
465
		}
466
	}
467
468
	protected function check_hot_link()
469
	{
470
		if (!$this->config['phpbb_gallery_allow_hotlinking'])
471
		{
472
			$haystack = array();
0 ignored issues
show
The assignment to $haystack is dead and can be removed.
Loading history...
473
			$haystack = explode(',', $this->config['phpbb_gallery_hotlinking_domains']);
474
			//add one extra array - current phpbb domain
475
			$haystack[] = $this->config['server_name'];
476
			$referrer = $this->request->server('HTTP_REFERER', '');
477
			$not_hl = false;
478
			foreach ($haystack as $var)
479
			{
480
				if (!empty($var))
481
				{
482
					if (strpos($referrer, $var) > 0 || empty($referrer))
483
					{
484
						$not_hl = true;
485
					}
486
				}
487
			}
488
			if (!$not_hl)
489
			{
490
				$this->error = 'no_hotlinking.jpg';
491
				$this->data['image_filename'] = 'no_hotlinking.jpg';
492
				$this->data['image_name'] = 'Hot linking not allowed';
493
				$this->data['image_user_id'] = 1;
494
				$this->data['image_status'] = 2;
495
				$this->data['album_id'] = 0;
496
				$this->data['album_user_id'] = 1;
497
				$this->data['image_filemissing'] = 0;
498
				$this->data['album_watermark'] = 0;
499
			}
500
		}
501
	}
502
}
503