Completed
Push — master ( 843c8c...eb00b7 )
by Stanislav
19s queued 15s
created

album::get_public_albums()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.9666
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
/**
4
 *
5
 * @package       PhpBB Gallery
6
 * @copyright (c) 2017 Lucifer
7
 * @license       http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
8
 *
9
 */
10
11
namespace phpbbgallery\core\album;
12
13
class album
14
{
15
	/** @var \phpbb\db\driver\driver_interface */
0 ignored issues
show
Bug introduced by
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...
16
	protected $db;
17
18
	/** @var \phpbb\user */
0 ignored issues
show
Bug introduced by
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...
19
	protected $user;
20
21
	/** @var \phpbb\language\language */
0 ignored issues
show
Bug introduced by
The type phpbb\language\language 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...
22
	protected $language;
23
24
	/** @var \phpbb\profilefields\manager */
0 ignored issues
show
Bug introduced by
The type phpbb\profilefields\manager 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...
25
	protected $user_cpf;
26
27
	/** @var \phpbbgallery\core\auth\auth */
28
	protected $gallery_auth;
29
30
	/** @var \phpbbgallery\core\cache */
31
	protected $gallery_cache;
32
33
	/** @var \phpbbgallery\core\block */
34
	protected $block;
35
36
	/** @var \phpbbgallery\core\config */
37
	protected $gallery_config;
38
39
	/** @var */
40
	protected $images_table;
41
42
	/** @var */
43
	protected $watch_table;
44
45
	/** @var */
46
	protected $contests_table;
47
48
	/** @var */
49
	protected $albums_table;
50
51
	/**
52
	 * album constructor.
53
	 *
54
	 * @param \phpbb\db\driver\driver_interface $db
55
	 * @param \phpbb\user                       $user
56
	 * @param language                          $language
0 ignored issues
show
Bug introduced by
The type phpbbgallery\core\album\language 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...
57
	 * @param \phpbb\profilefields\manager      $user_cpf
58
	 * @param \phpbbgallery\core\auth\auth      $gallery_auth
59
	 * @param \phpbbgallery\core\cache          $gallery_cache
60
	 * @param \phpbbgallery\core\block          $block
61
	 * @param \phpbbgallery\core\config         $gallery_config
62
	 * @param                                   $albums_table
63
	 * @param                                   $images_table
64
	 * @param                                   $watch_table
65
	 * @param                                   $contest_table
66
	 */
67 99
	public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\user $user,
68
		\phpbb\language\language $language, \phpbb\profilefields\manager $user_cpf,
69
		\phpbbgallery\core\auth\auth $gallery_auth, \phpbbgallery\core\cache $gallery_cache, \phpbbgallery\core\block $block,
70
		\phpbbgallery\core\config $gallery_config,
71
		$albums_table, $images_table, $watch_table, $contest_table)
72
	{
73 99
		$this->db = $db;
74 99
		$this->user = $user;
75 99
		$this->language = $language;
76 99
		$this->user_cpf = $user_cpf;
77 99
		$this->gallery_auth = $gallery_auth;
78 99
		$this->gallery_cache = $gallery_cache;
79 99
		$this->block = $block;
80 99
		$this->gallery_config = $gallery_config;
81 99
		$this->albums_table = $albums_table;
82 99
		$this->images_table = $images_table;
83 99
		$this->watch_table = $watch_table;
84 99
		$this->contests_table = $contest_table;
85 99
	}
86
87
	/**
88
	 * Get album information
89
	 *
90
	 * @param      $album_id
91
	 * @param bool $extended_info
92
	 * @return mixed
93
	 */
94 6
	public function get_info($album_id, $extended_info = true)
95
	{
96
		$sql_array = array(
97 6
			'SELECT' => 'a.*',
98 6
			'FROM'   => array($this->albums_table => 'a'),
99
100 6
			'WHERE' => 'a.album_id = ' . (int) $album_id,
101
		);
102
103 6
		if ($extended_info)
104
		{
105 6
			$sql_array['SELECT'] .= ', c.*, w.watch_id';
106 6
			$sql_array['LEFT_JOIN'] = array(
107
				array(
108 6
					'FROM' => array($this->watch_table => 'w'),
109 6
					'ON'   => 'a.album_id = w.album_id AND w.user_id = ' . (int) $this->user->data['user_id'],
110
				),
111
				array(
112 6
					'FROM' => array($this->contests_table => 'c'),
113 6
					'ON'   => 'a.album_id = c.contest_album_id',
114
				),
115
			);
116
		}
117 6
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
118
119 6
		$result = $this->db->sql_query($sql);
120 6
		$row = $this->db->sql_fetchrow($result);
121 6
		$this->db->sql_freeresult($result);
122
123 6
		if (!$row)
124
		{
125
			throw new \phpbb\exception\http_exception(404, 'ALBUM_NOT_EXIST');
0 ignored issues
show
Bug introduced by
The type phpbb\exception\http_exception 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...
126
		}
127
128 6
		if ($extended_info && !isset($row['contest_id']))
129
		{
130 6
			$row['contest_id'] = 0;
131 6
			$row['contest_rates_start'] = 0;
132 6
			$row['contest_end'] = 0;
133 6
			$row['contest_marked'] = 0;
134 6
			$row['contest_first'] = 0;
135 6
			$row['contest_second'] = 0;
136 6
			$row['contest_third'] = 0;
137
		}
138
139 6
		return $row;
140
	}
141
142
	/**
143
	 * Check whether the album_user is the user who wants to do something
144
	 *
145
	 * @param      $album_id
146
	 * @param bool $user_id
147
	 * @return bool
148
	 */
149 3
	public function check_user($album_id, $user_id = false)
150
	{
151 3
		if ($user_id === false)
152
		{
153 2
			$user_id = (int) $this->user->data['user_id'];
154
		}
155
156
		$sql = 'SELECT album_id
157 3
			FROM ' . $this->albums_table . '
158 3
			WHERE album_id = ' . (int) $album_id . '
159 3
				AND album_user_id = ' . (int) $user_id;
160 3
		$result = $this->db->sql_query($sql);
161 3
		$row = $this->db->sql_fetchrow($result);
162 3
		$this->db->sql_freeresult($result);
163
164 3
		if ($row === false)
165
		{
166
			// return false;
167 2
			throw new \phpbb\exception\http_exception(403, 'NO_ALBUM_STEALING');
168
		}
169
170 1
		return true;
171
	}
172
173
	/**
174
	 * Generate gallery-albumbox
175
	 *
176
	 * @param bool        $ignore_personals     list personal albums
177
	 * @param string      $select_name          request_var() for the select-box
178
	 * @param bool|int    $select_id            selected album
179
	 * @param bool|string $requested_permission Exp: for moving a image you need i_upload permissions or a_moderate
180
	 * @param bool        $ignore_id
181
	 * @param int         $album_user_id        for the select-boxes of the ucp so you only can attach to your own
182
	 *                                          albums
183
	 * @param int         $requested_album_type only albums of the album_type are allowed
184
	 * @return string $gallery_albumbox        if ($select_name) {full select-box} else {list with options}
185
	 *                                          else {list with options}
186
	 *
187
	 * comparable to make_forum_select (includes/functions_admin.php)
188
	 * @internal param $ (string || array)    $ignore_id                disabled albums, Exp: on moving: the album
189
	 *           where the image is now
190
	 */
191
	public function get_albumbox($ignore_personals, $select_name, $select_id = false, $requested_permission = false, $ignore_id = false, $album_user_id = \phpbbgallery\core\block::PUBLIC_ALBUM, $requested_album_type = -1)
192
	{
193
		// Instead of the query we use the cache
194
		$album_data = $this->gallery_cache->get('albums');
195
196
		$right = $last_a_u_id = 0;
197
		$access_own = $access_personal = $requested_own = $requested_personal = false;
198
		$c_access_own = $c_access_personal = false;
199
		$padding_store = array('0' => '');
200
		$padding = $album_list = '';
201
		$check_album_type = ($requested_album_type >= 0) ? true : false;
202
		$this->gallery_auth->load_user_permissions($this->user->data['user_id']);
203
204
		// Sometimes it could happen that albums will be displayed here not be displayed within the index page
205
		// This is the result of albums not displayed at index and a parent of a album with no permissions.
206
		// If this happens, the padding could be "broken", see includes/functions_admin.php > make_forum_select
207
208
		foreach ($album_data as $row)
209
		{
210
			$list = false;
211
			if ($row['album_user_id'] != $last_a_u_id)
212
			{
213
				if (!$last_a_u_id && $this->gallery_auth->acl_check('a_list', $this->gallery_auth->get_personal_album()) && !$ignore_personals)
214
				{
215
					$album_list .= '<option disabled="disabled" class="disabled-option">' . $this->language->lang('PERSONAL_ALBUMS') . '</option>';
216
				}
217
				$padding = '';
218
				$padding_store[$row['parent_id']] = '';
219
			}
220
			if ($row['left_id'] < $right)
221
			{
222
				$padding .= '&nbsp; &nbsp;';
223
				$padding_store[$row['parent_id']] = $padding;
224
			}
225
			else if ($row['left_id'] > $right + 1)
226
			{
227
				$padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : '';
228
			}
229
230
			$right = $row['right_id'];
231
			$last_a_u_id = $row['album_user_id'];
232
			$disabled = false;
233
234
			if (
235
				// Is in the ignore_id
236
				((is_array($ignore_id) && in_array($row['album_id'], $ignore_id)) || $row['album_id'] == $ignore_id)
237
				||
238
				// Need upload permissions (for moving)
239
				(($requested_permission == 'm_move') && (($row['album_type'] == (int) \phpbbgallery\core\block::TYPE_CAT) || (!$this->gallery_auth->acl_check('i_upload', $row['album_id'], $row['album_user_id']) && !$this->gallery_auth->acl_check('m_move', $row['album_id'], $row['album_user_id']))))
240
				||
241
				// album_type does not fit
242
				($check_album_type && ($row['album_type'] != $requested_album_type))
243
			)
244
			{
245
				$disabled = true;
246
			}
247
248
			if (($select_id == $this->gallery_auth->get_setting_permissions()) && !$row['album_user_id'])
249
			{
250
				$list = true;
251
			}
252
			else if (!$row['album_user_id'])
253
			{
254
				if ($this->gallery_auth->acl_check('a_list', $row['album_id'], $row['album_user_id']) || defined('IN_ADMIN'))
255
				{
256
					$list = true;
257
				}
258
			}
259
			else if (!$ignore_personals)
260
			{
261
				if ($row['album_user_id'] == $this->user->data['user_id'])
262
				{
263
					if (!$c_access_own)
264
					{
265
						$c_access_own = true;
266
						$access_own = $this->gallery_auth->acl_check('a_list', $this->gallery_auth->get_own_album());
267
						if ($requested_permission)
268
						{
269
							$requested_own = !$this->gallery_auth->acl_check($requested_permission, $this->gallery_auth->get_own_album());
0 ignored issues
show
Bug introduced by
It seems like $requested_permission can also be of type true; however, parameter $acl of phpbbgallery\core\auth\auth::acl_check() 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

269
							$requested_own = !$this->gallery_auth->acl_check(/** @scrutinizer ignore-type */ $requested_permission, $this->gallery_auth->get_own_album());
Loading history...
270
						}
271
						else
272
						{
273
							$requested_own = false; // We need the negated version of true here
274
						}
275
					}
276
					$list = (!$list) ? $access_own : $list;
277
					$disabled = (!$disabled) ? $requested_own : $disabled;
278
				}
279
				else if ($row['album_user_id'])
280
				{
281
					if (!$c_access_personal)
282
					{
283
						$c_access_personal = true;
284
						$access_personal = $this->gallery_auth->acl_check('a_list', $this->gallery_auth->get_personal_album());
285
						if ($requested_permission)
286
						{
287
							$requested_personal = !$this->gallery_auth->acl_check($requested_permission, $this->gallery_auth->get_personal_album());
288
						}
289
						else
290
						{
291
							$requested_personal = false; // We need the negated version of true here
292
						}
293
					}
294
					$list = (!$list) ? $access_personal : $list;
295
					$disabled = (!$disabled) ? $requested_personal : $disabled;
296
				}
297
			}
298
			if (($album_user_id != (int) \phpbbgallery\core\block::PUBLIC_ALBUM) && ($album_user_id != $row['album_user_id']))
299
			{
300
				$list = false;
301
			}
302
			else if (($album_user_id != (int) \phpbbgallery\core\block::PUBLIC_ALBUM) && ($row['parent_id'] == 0))
303
			{
304
				$disabled = true;
305
			}
306
307
			if ($list)
308
			{
309
				$selected = (is_array($select_id)) ? ((in_array($row['album_id'], $select_id)) ? ' selected="selected"' : '') : (($row['album_id'] == $select_id) ? ' selected="selected"' : '');
310
				$album_list .= '<option value="' . $row['album_id'] . '"' . (($disabled) ? ' disabled="disabled" class="disabled-option"' : $selected) . '>' . $padding . $row['album_name'] . ' (ID: ' . $row['album_id'] . ')</option>';
311
			}
312
		}
313
		unset($padding_store);
314
315
		if ($select_name)
316
		{
317
			$gallery_albumbox = "<select name='$select_name' id='$select_name'>";
318
			$gallery_albumbox .= $album_list;
319
			$gallery_albumbox .= '</select>';
320
		}
321
		else
322
		{
323
			$gallery_albumbox = $album_list;
324
		}
325
326
		return $gallery_albumbox;
327
	}
328
329
	/**
330
	 * Update album information
331
	 * Resets the following columns with the correct value:
332
	 * - album_images, _real
333
	 * - album_last_image_id, _time, _name
334
	 * - album_last_username, _user_colour, _user_id
335
	 *
336
	 * @param $album_id
337
	 * @return mixed
338
	 */
339
	public function update_info($album_id)
340
	{
341
		$images_real = $images = $album_user_id = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $album_user_id is dead and can be removed.
Loading history...
Unused Code introduced by
The assignment to $images is dead and can be removed.
Loading history...
Unused Code introduced by
The assignment to $images_real is dead and can be removed.
Loading history...
342
343
		// Get the album_user_id, so we can keep the user_colour
344
		$sql = 'SELECT album_user_id
345
			FROM ' . $this->albums_table . '
346
			WHERE album_id = ' . (int) $album_id;
347
		$result = $this->db->sql_query($sql);
348
		$album_user_id = $this->db->sql_fetchfield('album_user_id');
349
		$this->db->sql_freeresult($result);
350
351
		// Number of not unapproved images
352
		$sql = 'SELECT COUNT(image_id) images
353
			FROM ' . $this->images_table . ' 
354
			WHERE image_status <> ' . (int) $this->block->get_image_status_unapproved() . '
355
				AND image_status <> ' . (int) $this->block->get_image_status_orphan() . '
356
				AND image_album_id = ' . (int) $album_id;
357
		$result = $this->db->sql_query($sql);
358
		$images = $this->db->sql_fetchfield('images');
359
		$this->db->sql_freeresult($result);
360
361
		// Number of total images
362
		$sql = 'SELECT COUNT(image_id) images_real
363
			FROM ' . $this->images_table . '
364
			WHERE image_status <> ' . (int) $this->block->get_image_status_orphan() . '
365
				AND image_album_id = ' . (int) $album_id;
366
		$result = $this->db->sql_query($sql);
367
		$images_real = $this->db->sql_fetchfield('images_real');
368
		$this->db->sql_freeresult($result);
369
370
		// Data of the last not unapproved image
371
		$sql = 'SELECT image_id, image_time, image_name, image_username, image_user_colour, image_user_id
372
			FROM ' . $this->images_table . '
373
			WHERE image_status <> ' . (int) $this->block->get_image_status_unapproved() . '
374
				AND image_status <> ' . (int) $this->block->get_image_status_orphan() . '
375
				AND image_album_id = ' . (int) $album_id . '
376
			ORDER BY image_time DESC';
377
		$result = $this->db->sql_query($sql);
378
		if ($row = $this->db->sql_fetchrow($result))
379
		{
380
			$sql_ary = array(
381
				'album_images_real'      => $images_real,
382
				'album_images'           => $images,
383
				'album_last_image_id'    => $row['image_id'],
384
				'album_last_image_time'  => $row['image_time'],
385
				'album_last_image_name'  => $row['image_name'],
386
				'album_last_username'    => $row['image_username'],
387
				'album_last_user_colour' => $row['image_user_colour'],
388
				'album_last_user_id'     => $row['image_user_id'],
389
			);
390
		}
391
		else
392
		{
393
			// No approved image, so we clear the columns
394
			$sql_ary = array(
395
				'album_images_real'      => $images_real,
396
				'album_images'           => $images,
397
				'album_last_image_id'    => 0,
398
				'album_last_image_time'  => 0,
399
				'album_last_image_name'  => '',
400
				'album_last_username'    => '',
401
				'album_last_user_colour' => '',
402
				'album_last_user_id'     => 0,
403
			);
404
			if ($album_user_id)
405
			{
406
				unset($sql_ary['album_last_user_colour']);
407
			}
408
		}
409
		$this->db->sql_freeresult($result);
410
411
		$sql = 'UPDATE ' . $this->albums_table . ' SET ' . $this->db->sql_build_array('UPDATE', $sql_ary) . '
412
			WHERE album_id = ' . (int) $album_id;
413
		$this->db->sql_query($sql);
414
415
		return $row;
416
	}
417
418
	/**
419
	 * Generate personal album for user, when moving image into it
420
	 *
421
	 * @param $album_name
422
	 * @param $user_id
423
	 * @param $user_colour
424
	 * @param $gallery_user
425
	 * @return string
426
	 */
427
	public function generate_personal_album($album_name, $user_id, $user_colour, $gallery_user)
428
	{
429
		$album_data = array(
430
			'album_name'             => $this->db->sql_escape($album_name),
431
			'parent_id'              => 0,
432
			//left_id and right_id default by db
433
			'album_desc_options'     => 7,
434
			'album_desc'             => '',
435
			'album_parents'          => '',
436
			'album_type'             => (int) \phpbbgallery\core\block::TYPE_UPLOAD,
437
			'album_status'           => (int) \phpbbgallery\core\block::ALBUM_OPEN,
438
			'album_user_id'          => (int) $user_id,
439
			'album_last_username'    => '',
440
			'album_last_user_colour' => $user_colour,
441
		);
442
		$this->db->sql_query('INSERT INTO ' . $this->albums_table . ' ' . $this->db->sql_build_array('INSERT', $album_data));
443
		$personal_album_id = $this->db->sql_nextid();
444
445
		$gallery_user->update_data(array(
446
			'personal_album_id' => $personal_album_id,
447
		));
448
449
		// Fill album CPF.
450
		$cpf_vars = array(
451
			'pf_gallery_palbum' => (int) $personal_album_id,
452
		);
453
		$this->user_cpf->update_profile_field_data((int) $user_id, $cpf_vars);
454
455
		$this->gallery_config->inc('num_pegas', 1);
456
457
		// Update the config for the statistic on the index
458
		$this->gallery_config->set('newest_pega_user_id', $user_id);
459
		$this->gallery_config->set('newest_pega_username', $album_name);
460
		$this->gallery_config->set('newest_pega_user_colour', $user_colour);
461
		$this->gallery_config->set('newest_pega_album_id', $personal_album_id);
462
463
		$this->gallery_cache->destroy('_albums');
464
		$this->gallery_cache->destroy('sql', $this->albums_table);
465
466
		return $personal_album_id;
467
	}
468
469
	/**
470
	 * Create array of album IDs that are public
471
	 */
472 3
	public function get_public_albums()
473
	{
474
		$sql = 'SELECT album_id
475 3
				FROM ' . $this->albums_table . '
476 3
				WHERE album_user_id = ' . (int) \phpbbgallery\core\block::PUBLIC_ALBUM;
477 3
		$result = $this->db->sql_query($sql);
478 3
		$id_ary = array();
479 3
		while ($row = $this->db->sql_fetchrow($result))
480
		{
481 3
			$id_ary[] = (int) $row['album_id'];
482
		}
483 3
		$this->db->sql_freeresult($result);
484 3
		return $id_ary;
485
	}
486
}
487