auth::restrict_pegas()   C
last analyzed

Complexity

Conditions 16
Paths 17

Size

Total Lines 60
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 143.7096

Importance

Changes 0
Metric Value
eloc 28
c 0
b 0
f 0
dl 0
loc 60
ccs 6
cts 29
cp 0.2069
rs 5.5666
cc 16
nc 17
nop 1
crap 143.7096

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\auth;
14
15
class auth
16
{
17
	const SETTING_PERMISSIONS	= -39839;
18
	const PERSONAL_ALBUM		= -3;
19
	const OWN_ALBUM				= -2;
20
	const PUBLIC_ALBUM			= 0;
21
22
	const ACCESS_ALL			= 0;
23
	const ACCESS_REGISTERED		= 1;
24
	const ACCESS_NOT_FOES		= 2;
25
	const ACCESS_FRIENDS		= 3;
26
	const ACCESS_SPECIAL_FRIENDS	= 4;
27
28
	// ACL - slightly different
29
	const ACL_NO		= 0;
30
	const ACL_YES		= 1;
31
	const ACL_NEVER		= 2;
32
33
	static protected $_permission_i = array('i_view', 'i_watermark', 'i_upload', 'i_approve', 'i_edit', 'i_delete', 'i_report', 'i_rate');
34
	static protected $_permission_c = array('c_read', 'c_post', 'c_edit', 'c_delete');
35
	static protected $_permission_m = array('m_comments', 'm_delete', 'm_edit', 'm_move', 'm_report', 'm_status');
36
	static protected $_permission_misc = array('a_list', 'i_count', 'i_unlimited', 'a_count', 'a_unlimited', 'a_restrict');
37
	static protected $_permissions = array();
38
	static protected $_permissions_flipped = array();
39
40
	protected $_auth_data = array();
41
	protected $_auth_data_never = array();
42
43
	protected $acl_cache = array();
44
45
	/**
46
	* Cache object
47
	* @var \phpbbgallery\core\cache
48
	*/
49
	protected $cache;
50
51
	/**
52
	* Database object
53
	* @var \phpbb\db\driver\driver
0 ignored issues
show
Bug introduced by
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...
54
	*/
55
	protected $db;
56
57
	/**
58
	* Gallery user object
59
	* @var \phpbbgallery\core\user
60
	*/
61
	protected $user;
62
63
	/**
64
	* phpBB user object
65
	* @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...
66
	*/
67
	protected $phpbb_user;
68
69
	/**
70
	* phpBB auth object
71
	* @var \phpbb\auth\auth
0 ignored issues
show
Bug introduced by
The type phpbb\auth\auth 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...
72
	*/
73
	protected $auth;
74
75
	/**
76
	* Gallery permissions table
77
	* @var string
78
	*/
79
	protected $table_permissions;
80
81
	/**
82
	* Gallery permission roles table
83
	* @var string
84
	*/
85
	protected $table_roles;
86
87
	/**
88
	* Gallery users table
89
	* @var string
90
	*/
91
	protected $table_users;
92
93
	/**
94
	* Gallery albums table
95
	* @var string
96
	*/
97
	protected $table_albums;
98
99
	/**
100
	 * Construct
101
	 *
102
	 * @param    \phpbbgallery\core\cache $cache Cache object
103
	 * @param \phpbb\db\driver\driver|\phpbb\db\driver\driver_interface $db Database object
104
	 * @param    \phpbbgallery\core\user $user Gallery user object
105
	 * @param \phpbb\user $phpbb_user
106
	 * @param \phpbb\auth\auth $auth
107
	 * @param    string $permissions_table Gallery permissions table
108
	 * @param    string $roles_table Gallery permission roles table
109
	 * @param    string $users_table Gallery users table
110
	 * @param $albums_table
111
	 */
112 140
	public function __construct(\phpbbgallery\core\cache $cache, \phpbb\db\driver\driver_interface $db, \phpbbgallery\core\user $user, \phpbb\user $phpbb_user, \phpbb\auth\auth $auth,
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...
113
	$permissions_table, $roles_table, $users_table, $albums_table)
114
	{
115 140
		$this->cache = $cache;
116 140
		$this->db = $db;
117 140
		$this->user = $user;
118 140
		$this->phpbb_user = $phpbb_user;
119 140
		$this->auth = $auth;
120 140
		$this->table_permissions = $permissions_table;
121 140
		$this->table_roles = $roles_table;
122 140
		$this->table_users = $users_table;
123 140
		$this->table_albums = $albums_table;
124
125 140
		self::$_permissions = array_merge(self::$_permission_i, self::$_permission_c, self::$_permission_m, self::$_permission_misc);
126 140
		self::$_permissions_flipped = array_flip(array_merge(self::$_permissions, array('m_')));
127 140
		self::$_permissions_flipped['i_count'] = 'i_count';
128 140
		self::$_permissions_flipped['a_count'] = 'a_count';
129 140
	}
130
131
	public function get_setting_permissions()
132
	{
133
		return self::SETTING_PERMISSIONS;
134
	}
135
136
	public function get_personal_album()
137
	{
138
		return self::PERSONAL_ALBUM;
139
	}
140
141 1
	public function get_own_album()
142
	{
143 1
		return self::OWN_ALBUM;
144
	}
145
146 109
	public function load_user_permissions($user_id, $album_id = false)
147
	{
148 109
		$cached_permissions = $this->user->get_data('user_permissions');
149 109
		if (($user_id == $this->user->user_id) && !empty($cached_permissions))
150
		{
151 7
			$this->unserialize_auth_data($cached_permissions);
152 7
			return;
153
		}
154
155 109
		else if ($user_id != $this->user->user_id)
156
		{
157 109
			$this->user->set_user_id($user_id);
158 109
			$cached_permissions = $this->user->get_data('user_permissions');
159 109
			if (!empty($cached_permissions))
160
			{
161
				$this->unserialize_auth_data($cached_permissions);
162
				return;
163
			}
164
		}
165
		/*else {
166
			$this->user->set_user_id($user_id);
167
			$cached_permissions = $this->user->get_data('user_permissions');
168
			$this->unserialize_auth_data($cached_permissions);
169
			return;
170
		}*/
171 109
		$this->query_auth_data($user_id);
172 109
	}
173
174
	/**
175
	 * Query the permissions for a given user and store them in the database.
176
	 * @param $user_id
177
	 */
178 109
	protected function query_auth_data($user_id)
179
	{
180
		//$albums = array();//@todo $this->cache->obtain_album_list();
181 109
		$albums = $this->cache->get('albums');
182 109
		$user_groups_ary = self::get_usergroups($user_id);
0 ignored issues
show
Bug Best Practice introduced by
The method phpbbgallery\core\auth\auth::get_usergroups() is not static, but was called statically. ( Ignorable by Annotation )

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

182
		/** @scrutinizer ignore-call */ 
183
  $user_groups_ary = self::get_usergroups($user_id);
Loading history...
183
184 109
		$sql_select = '';
185 109
		foreach (self::$_permissions as $permission)
186
		{
187 109
			$sql_select .= " MAX($permission) as $permission,";
188
		}
189
190 109
		$this->_auth_data[self::OWN_ALBUM]				= new \phpbbgallery\core\auth\set();
191 109
		$this->_auth_data_never[self::OWN_ALBUM]		= new \phpbbgallery\core\auth\set();
192 109
		$this->_auth_data[self::PERSONAL_ALBUM]			= new \phpbbgallery\core\auth\set();
193 109
		$this->_auth_data_never[self::PERSONAL_ALBUM]	= new \phpbbgallery\core\auth\set();
194
195 109
		foreach ($albums as $album)
196
		{
197 109
			if ($album['album_user_id'] == self::PUBLIC_ALBUM)
198
			{
199 109
				$this->_auth_data[$album['album_id']]		= new \phpbbgallery\core\auth\set();
200 109
				$this->_auth_data_never[$album['album_id']]	= new \phpbbgallery\core\auth\set();
201
			}
202
		}
203
204
		$sql_array = array(
205 109
			'SELECT'		=> "p.perm_album_id, $sql_select p.perm_system",
206 109
			'FROM'			=> array($this->table_permissions => 'p'),
207
208
			'LEFT_JOIN'		=> array(
209
				array(
210 109
					'FROM'		=> array($this->table_roles => 'pr'),
211 109
					'ON'		=> 'p.perm_role_id = pr.role_id',
212
				),
213
			),
214
215 109
			'WHERE'			=> 'p.perm_user_id = ' . $user_id . ' OR ' . $this->db->sql_in_set('p.perm_group_id', $user_groups_ary, false, true),
216 109
			'GROUP_BY'		=> 'p.perm_system, p.perm_album_id',
217 109
			'ORDER_BY'		=> 'p.perm_system DESC, p.perm_album_id ASC',
218
		);
219 109
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
220
221 109
		$this->db->sql_return_on_error(true);
222 109
		$result = $this->db->sql_query($sql);
223
224 109
		if ($this->db->get_sql_error_triggered())
225
		{
226
			trigger_error('DATABASE_NOT_UPTODATE');
227
228
		}
229 109
		$this->db->sql_return_on_error(false);
230
231 109
		while ($row = $this->db->sql_fetchrow($result))
232
		{
233 106
			switch ($row['perm_system'])
234
			{
235 106
				case self::PERSONAL_ALBUM:
236 11
					$this->store_acl_row(self::PERSONAL_ALBUM, $row);
237 11
				break;
238
239 106
				case self::OWN_ALBUM:
240 11
					$this->store_acl_row(self::OWN_ALBUM, $row);
241 11
				break;
242
243 106
				case self::PUBLIC_ALBUM:
244 106
					$this->store_acl_row(((int) $row['perm_album_id']), $row);
245 106
				break;
246
			}
247
		}
248 109
		$this->db->sql_freeresult($result);
249
250 109
		$this->merge_acl_row();
251
252 109
		$this->restrict_pegas($user_id);
253
254 109
		$this->set_user_permissions($user_id, $this->_auth_data);
255 109
	}
256
257
	/**
258
	 * Serialize the auth-data sop we can store it.
259
	 *
260
	 * Line-Format:    bitfields:i_count:a_count::album_id(s)
261
	 * Samples:        8912837:0:10::-3
262
	 *                9961469:20:0::1:23:42
263
	 * @param $auth_data
264
	 * @return string
265
	 */
266 109
	protected function serialize_auth_data($auth_data)
267
	{
268 109
		$acl_array = array();
269
270 109
		foreach ($auth_data as $a_id => $obj)
271
		{
272 109
			$key = $obj->get_bits() . ':' . $obj->get_count('i_count') . ':' . $obj->get_count('a_count');
273 109
			if (!isset($acl_array[$key]))
274
			{
275 109
				$acl_array[$key] = $key . '::' . $a_id;
276
			}
277
			else
278
			{
279 109
				$acl_array[$key] .= ':' . $a_id;
280
			}
281
		}
282
283 109
		return implode("\n", $acl_array);
284
	}
285
286
	/**
287
	 * Unserialize the stored auth-data
288
	 * @param $serialized_data
289
	 */
290 7
	protected function unserialize_auth_data($serialized_data)
291
	{
292 7
		$acl_array = explode("\n", $serialized_data);
293
294 7
		foreach ($acl_array as $acl_row)
295
		{
296 7
			list ($acls, $a_ids) = explode('::', $acl_row);
297 7
			list ($bits, $i_count, $a_count) = explode(':', $acls);
298
299 7
			foreach (explode(':', $a_ids) as $a_id)
300
			{
301 7
				$this->_auth_data[$a_id] = new \phpbbgallery\core\auth\set($bits, $i_count, $a_count);
302
			}
303
		}
304 7
	}
305
306
	/**
307
	 * Stores an acl-row into the _auth_data-array.
308
	 * @param $album_id
309
	 * @param $data
310
	 */
311 106
	protected function store_acl_row($album_id, $data)
312
	{
313 106
		if (!isset($this->_auth_data[$album_id]))
314
		{
315
			// The album we have permissions for does not exist any more, so do nothing.
316
			return;
317
		}
318
319 106
		foreach (self::$_permissions as $permission)
320
		{
321 106
			if (strpos($permission, '_count') === false)
322
			{
323 106
				if ($data[$permission] == self::ACL_NEVER)
324
				{
325
					$this->_auth_data_never[$album_id]->set_bit(self::$_permissions_flipped[$permission], true);
326
				}
327 106
				else if ($data[$permission] == self::ACL_YES)
328
				{
329 106
					$this->_auth_data[$album_id]->set_bit(self::$_permissions_flipped[$permission], true);
330 106
					if (substr($permission, 0, 2) == 'm_')
331
					{
332 106
						$this->_auth_data[$album_id]->set_bit(self::$_permissions_flipped['m_'], true);
333
					}
334
				}
335
			}
336
			else
337
			{
338 106
				$this->_auth_data[$album_id]->set_count($permission, $data[$permission]);
339
			}
340
		}
341 106
	}
342
343
	/**
344
	* Merge the NEVER-options into the YES-options by removing the YES, if it is set.
345
	*/
346 109
	protected function merge_acl_row()
347
	{
348 109
		foreach ($this->_auth_data as $album_id => $obj)
349
		{
350 109
			foreach (self::$_permissions as $acl)
351
			{
352 109
				if (strpos('_count', $acl) === false)
353
				{
354 109
					$bit = self::$_permissions_flipped[$acl];
355
					// If the yes and the never bit are set, we overwrite the yes with a false.
356 109
					if ($obj->get_bit($bit) && $this->_auth_data_never[$album_id]->get_bit($bit))
357
					{
358
						$obj->set_bit($bit, false);
359
					}
360
				}
361
			}
362
		}
363 109
	}
364
365
	/**
366
	 * Restrict the access to personal galleries, if the user is not a moderator.
367
	 * @param $user_id
368
	 */
369 109
	protected function restrict_pegas($user_id)
370
	{
371 109
		if (($user_id != ANONYMOUS) && $this->_auth_data[self::PERSONAL_ALBUM]->get_bit(self::$_permissions_flipped['m_']))
0 ignored issues
show
Bug introduced by
The constant phpbbgallery\core\auth\ANONYMOUS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
372
		{
373
			// No restrictions for moderators.
374 11
			return;
375
		}
376
377 98
		$zebra = null;
378
379 98
		$albums = array();//@todo $this->cache->obtain_album_list();
380 98
		foreach ($albums as $album)
381
		{
382
			if (!$album['album_auth_access'] || ($album['album_user_id'] == self::PUBLIC_ALBUM))# || ($album['album_user_id'] == $user_id))
383
			{
384
				continue;
385
			}
386
			else if ($user_id == ANONYMOUS)
387
			{
388
				// Level 1: No guests
389
				$this->_auth_data[$album['album_id']] = new \phpbbgallery\core\auth\set();
390
				continue;
391
			}
392
			else if ($album['album_auth_access'] == self::ACCESS_NOT_FOES)
393
			{
394
				if ($zebra == null)
395
				{
396
					$zebra = self::get_user_zebra($user_id);
0 ignored issues
show
Bug Best Practice introduced by
The method phpbbgallery\core\auth\auth::get_user_zebra() is not static, but was called statically. ( Ignorable by Annotation )

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

396
					/** @scrutinizer ignore-call */ 
397
     $zebra = self::get_user_zebra($user_id);
Loading history...
397
				}
398
				if (in_array($album['album_user_id'], $zebra['foe']))
399
				{
400
					// Level 2: No foes allowed
401
					$this->_auth_data[$album['album_id']] = new \phpbbgallery\core\auth\set();
402
					continue;
403
				}
404
			}
405
			else if ($album['album_auth_access'] == self::ACCESS_SPECIAL_FRIENDS)
406
			{
407
				if ($zebra == null)
408
				{
409
					$zebra = self::get_user_zebra($user_id);
410
				}
411
				if (!in_array($album['album_user_id'], $zebra['bff']))
412
				{
413
					// Level 4: Only special friends allowed
414
					$this->_auth_data[$album['album_id']] = new \phpbbgallery\core\auth\set();
415
					continue;
416
				}
417
			}
418
			else if ($album['album_auth_access'] == self::ACCESS_FRIENDS)
419
			{
420
				if ($zebra == null)
421
				{
422
					$zebra = self::get_user_zebra($user_id);
423
				}
424
				if (!in_array($album['album_user_id'], $zebra['friend']))
425
				{
426
					// Level 3: Only friends allowed
427
					$this->_auth_data[$album['album_id']] = new \phpbbgallery\core\auth\set();
428
					continue;
429
				}
430
			}
431
		}
432 98
	}
433
434
	/**
435
	 * Get the users, which added our user as friend and/or foe
436
	 * @param $user_id
437
	 * @return array
438
	 */
439 89
	public function get_user_zebra($user_id)
440
	{
441
442 89
		$zebra = array('foe' => array(), 'friend' => array(), 'bff' => array());
443
		$sql = 'SELECT *
444 89
			FROM ' . ZEBRA_TABLE . '
0 ignored issues
show
Bug introduced by
The constant phpbbgallery\core\auth\ZEBRA_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
445 89
			WHERE zebra_id = ' . (int) $user_id;
446 89
		$result = $this->db->sql_query($sql);
447 89
		while ($row = $this->db->sql_fetchrow($result))
448
		{
449 87
			if ($row['foe'])
450
			{
451 75
				$zebra['foe'][] = (int) $row['user_id'];
452
			}
453
			else
454
			{
455 87
				if (isset($row['bff']))
456
				{
457
					if ($row['bff'])
458
					{
459
						$zebra['bff'][] = (int) $row['user_id'];
460
					}
461
					else
462
					{
463
						$zebra['friend'][] = (int) $row['user_id'];
464
					}
465
				}
466
				else
467
				{
468 87
					$zebra['friend'][] = (int) $row['user_id'];
469
				}
470
			}
471
		}
472 89
		$this->db->sql_freeresult($result);
473 89
		return $zebra;
474
	}
475
	public function get_user_foes($user_id)
476
	{
477
		$foes = array();
478
		$sql = 'SELECT * 
479
		FROM ' . ZEBRA_TABLE . '
0 ignored issues
show
Bug introduced by
The constant phpbbgallery\core\auth\ZEBRA_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
480
		WHERE user_id = ' . (int) $user_id . '
481
		AND foe = 1';
482
		$result = $this->db->sql_query($sql);
483
		while ($row = $this->db->sql_fetchrow($result))
484
		{
485
			$foes[] = (int) $row['zebra_id'];
486
		}
487
		return $foes;
488
	}
489
490
	/**
491
	 * Get zebra state
492
	 * @param $zebra_array
493
	 * @param $album_author
494
	 * @param $album_id
495
	 * @return int
496
	 */
497 84
	public function get_zebra_state($zebra_array, $album_author, $album_id)
498
	{
499 84
		$state = 0;
500
		// if we check for ourselves or user is mod or admin - make biggest possible step
501 84
		if ($this->phpbb_user->data['user_id'] == $album_author || $this->acl_check('m_', $album_author, $album_id) || $this->auth->acl_get('a_user'))
502
		{
503 84
			$state = 5;
504
		}
505
		//If user is not anon - we will check ... else its state is 0
506 75
		else if ($this->phpbb_user->data['user_id'] != ANONYMOUS)
0 ignored issues
show
Bug introduced by
The constant phpbbgallery\core\auth\ANONYMOUS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
507
		{
508 75
			if (in_array($album_author, $zebra_array['foe']))
509
			{
510
				$state = 1;
511
			}
512 75
			else if (in_array($album_author, $zebra_array['friend']))
513
			{
514 75
				$state = 3;
515
			}
516
			else if (in_array($album_author, $zebra_array['bff']))
517
			{
518
				$state = 4;
519
			}
520
			else
521
			{
522
				$state = 2;
523
			}
524
		}
525 84
		return (int) $state;
526
	}
527
528
	/**
529
	 * Get groups a user is member from.
530
	 * @param $user_id
531
	 * @return array
532
	 */
533 109
	public function get_usergroups($user_id)
534
	{
535 109
		$groups_ary = array();
536
537
		$sql = 'SELECT ug.group_id
538 109
			FROM ' . USER_GROUP_TABLE . ' ug
0 ignored issues
show
Bug introduced by
The constant phpbbgallery\core\auth\USER_GROUP_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
539 109
			LEFT JOIN ' . GROUPS_TABLE . ' g
0 ignored issues
show
Bug introduced by
The constant phpbbgallery\core\auth\GROUPS_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
540
				ON (ug.group_id = g.group_id)
541 109
			WHERE ug.user_id = ' . (int) $user_id . '
542
				AND ug.user_pending = 0
543
				AND g.group_skip_auth = 0';
544 109
		$result = $this->db->sql_query($sql);
545
546 109
		while ($row = $this->db->sql_fetchrow($result))
547
		{
548 106
			$groups_ary[] = $row['group_id'];
549
		}
550 109
		$this->db->sql_freeresult($result);
551
552 109
		return $groups_ary;
553
	}
554
555
	/**
556
	 * Sets the permissions-cache in users-table to given array.
557
	 * @param $user_ids
558
	 * @param bool $permissions
559
	 */
560 109
	public function set_user_permissions($user_ids, $permissions = false)
561
	{
562 109
		$sql_set = (is_array($permissions)) ? $this->db->sql_escape($this->serialize_auth_data($permissions)) : '';
0 ignored issues
show
introduced by
The condition is_array($permissions) is always false.
Loading history...
563 109
		$sql_where = '';
564 109
		if (is_array($user_ids))
565
		{
566
			$sql_where = 'WHERE ' . $this->db->sql_in_set('user_id', array_map('intval', $user_ids));
567
		}
568 109
		else if ($user_ids == 'all')
569
		{
570
			$sql_where = '';
571
		}
572
		else
573
		{
574 109
			$sql_where = 'WHERE user_id = ' . (int) $user_ids;
575
		}
576
577 109
		if ($this->user->is_user($user_ids))
578
		{
579 109
			$this->user->set_permissions_changed(time());
580
		}
581
582 109
			$sql = 'UPDATE ' . $this->table_users . "
583 109
				SET user_permissions = '" . $sql_set . "',
584 109
					user_permissions_changed = " . (int) time() . '
585 109
				' . $sql_where;
586 109
			$this->db->sql_query($sql);
587 109
	}
588
589
	/**
590
	* Get permission
591
	*
592
	* @param	string	$acl	One of the permissions, Exp: i_view
593
	* @param	int		$a_id	The album_id, from which we want to have the permissions
594
	* @param	int		$u_id	The user_id from the album-owner. If not specified we need to get it from the cache.
595
	*
596
	* @return	bool			Is the user allowed to do the $acl?
597
	*/
598 93
	public function acl_check($acl, $a_id, $u_id = -1)
599
	{
600 93
		$bit = self::$_permissions_flipped[$acl];
601
602 93
		if ($bit < 0)
603
		{
604
			$bit = $acl;
605
		}
606
607 93
		if (isset($this->acl_cache[$a_id][$bit]))
608
		{
609 78
			return $this->acl_cache[$a_id][$bit];
610
		}
611
612
		// Do we have a function call without $album_user_id ?
613 93
		if (($u_id < self::PUBLIC_ALBUM) && ($a_id > 0))
614
		{
615
			static $_album_list;
616
			// Yes, from viewonline.php
617
			if (!$_album_list)
618
			{
619
				$_album_list = $this->cache->get_albums();
620
			}
621
			if (!isset($_album_list[$a_id]))
622
			{
623
				// Do not give permissions, if the album does not exist.
624
				return false;
625
			}
626
			$u_id = $_album_list[$a_id]['album_user_id'];
627
		}
628
629 93
		$get_acl = 'get_bit';
630 93
		if (!is_int($bit))
631
		{
632
			$get_acl = 'get_count';
633
		}
634 93
		$p_id = $a_id;
635 93
		if ($u_id)
636
		{
637 84
			$this->user->set_user_id($this->phpbb_user->data['user_id']);
638 84
			if ($this->user->is_user($u_id))
639
			{
640 2
				$p_id = self::OWN_ALBUM;
641
			}
642
			else
643
			{
644 84
				if (!isset($this->_auth_data[$a_id]))
645
				{
646 74
					$p_id = self::PERSONAL_ALBUM;
647
				}
648
			}
649
		}
650
651 93
		if (isset($this->_auth_data[$p_id]))
652
		{
653 93
			$this->acl_cache[$a_id][$bit] = $this->_auth_data[$p_id]->$get_acl($bit);
654 93
			return $this->acl_cache[$a_id][$bit];
655
		}
656
		return false;
657
	}
658
659
	/**
660
	* Does the user have the permission for any album?
661
	*
662
	* @param	string	$acl			One of the permissions, Exp: i_view; *_count permissions are not allowed!
663
	*
664
	* @return	bool			Is the user allowed to do the $acl?
665
	*/
666 11
	public function acl_check_global($acl)
667
	{
668 11
		$bit = self::$_permissions_flipped[$acl];
669 11
		if (!is_int($bit))
670
		{
671
			// No support for *_count permissions.
672
			return false;
673
		}
674
675 11
		if ($this->_auth_data[self::OWN_ALBUM]->get_bit($bit))
676
		{
677 5
			return true;
678
		}
679 6
		if ($this->_auth_data[self::PERSONAL_ALBUM]->get_bit($bit))
680
		{
681
			return true;
682
		}
683
684 6
		$albums = $this->cache->get_albums();
685 6
		foreach ($albums as $album)
686
		{
687 6
			if (!$album['album_user_id'] && $this->_auth_data[$album['album_id']]->get_bit($bit))
688
			{
689 3
				return true;
690
			}
691
		}
692
693 3
		return false;
694
	}
695
696
	/**
697
	* Get albums by permission
698
	*
699
	* @param	string	$acl			One of the permissions, Exp: i_view; *_count permissions are not allowed!
700
	* @param	string	$return			Type of the return value. array returns an array, else it's a string.
701
	*									bool means it only checks whether the user has the permission anywhere.
702
	* @param	bool	$display_in_rrc	Only return albums, that have the display_in_rrc-flag set.
703
	* @param	bool	$display_pegas	Include personal galleries in the list.
704
	*
705
	* @return	mixed					$album_ids, either as list or array.
706
	*/
707 94
	public function acl_album_ids($acl, $return = 'array', $display_in_rrc = false, $display_pegas = true)
708
	{
709 94
		$bit = self::$_permissions_flipped[$acl];
710 94
		if (!is_int($bit))
711
		{
712
			// No support for *_count permissions.
713
			return ($return == 'array') ? array() : '';
714
		}
715
716 94
		$album_list = '';
717 94
		$album_array = array();
718 94
		$albums = $this->cache->get_albums();
719 94
		foreach ($albums as $album)
720
		{
721 94
			if ($this->user->is_user($album['album_user_id']))
722
			{
723 92
				$a_id = self::OWN_ALBUM;
724
			}
725 94
			else if ($album['album_user_id'] > self::PUBLIC_ALBUM)
726
			{
727 94
				$a_id = self::PERSONAL_ALBUM;
728
			}
729
			else
730
			{
731 94
				$a_id = $album['album_id'];
732
			}
733 94
			if ($this->_auth_data[$a_id]->get_bit($bit) && (!$display_in_rrc || ($display_in_rrc && $album['display_in_rrc'])) && ($display_pegas || ($album['album_user_id'] == self::PUBLIC_ALBUM)))
734
			{
735 91
				if ($return == 'bool')
736
				{
737
					return true;
738
				}
739 91
				$album_list .= (($album_list) ? ', ' : '') . $album['album_id'];
740 91
				$album_array[] = (int) $album['album_id'];
741
			}
742
		}
743
744 94
		if ($return == 'bool')
745
		{
746
			return false;
747
		}
748
749 94
		return ($return == 'array') ? $album_array : $album_list;
750
	}
751
752
	/**
753
	 * Get all user IDs that have specific ACL for album
754
	 *
755
	 * @param    string $acl      One of the permissions, Exp: i_view; *_count permissions are not allowed!
756
	 * @param    int    $album_id Album ID we want info for
757
	 *
758
	 * return    array    $user_ids    Return user IDs as array
759
	 * @return array
760
	 */
761 3
	public function acl_users_ids($acl, $album_id)
762
	{
763 3
		if (strstr($acl, '_count') != 0)
764
		{
765
			return array();
766
		}
767
		// Let's load album data
768 3
		$sql = 'SELECT * FROM ' . $this->table_albums . ' WHERE album_id = ' . (int) $album_id;
769 3
		$result = $this->db->sql_query($sql);
770 3
		$album_data = $this->db->sql_fetchrow($result);
771 3
		$this->db->sql_freeresult($result);
772
773
		// Let's request roles
774
		// If album user_id is different then 0 then this is user album.
775
		// So we need to request all roles for perm_system -2(own) and -3(user)
776 3
		if ($album_data['album_user_id'] != 0)
777
		{
778
			$sql = 'SELECT * FROM ' . $this->table_permissions . ' WHERE ' . $this->db->sql_in_set('perm_system', array(-2, -3));
779
		}
780
		else
781
		{
782 3
			$sql = 'SELECT * FROM ' . $this->table_permissions . ' WHERE perm_album_id = ' . (int) $album_id;
783
		}
784
785 3
		$result = $this->db->sql_query($sql);
786 3
		$roles_id = array();
787
		// Now we build the array to test
788 3
		while ($row = $this->db->sql_fetchrow($result))
789
		{
790 3
			$roles_id['roles'][] = (int) $row['perm_role_id'];
791 3
			$roles_id[$row['perm_role_id']]['user_id'][] = (int) $row['perm_user_id'];
792 3
			$roles_id[$row['perm_role_id']]['group_id'][] = (int) $row['perm_group_id'];
793
		}
794 3
		$this->db->sql_freeresult($result);
795
796
		// Now we will select the roles that have the set ACL
797 3
		$sql = 'SELECT role_id FROM ' . $this->table_roles . ' WHERE ' . $acl . ' = 1 and ' . $this->db->sql_in_set('role_id', $roles_id['roles'], false, true);
798 3
		$result = $this->db->sql_query($sql);
799 3
		$roles = array();
800 3
		while ($row = $this->db->sql_fetchrow($result))
801
		{
802 3
			$roles[] = (int) $row['role_id'];
803
		}
804 3
		$this->db->sql_freeresult($result);
805
806
		// Let's cycle trough roles and build user_ids with user_ids from roles
807 3
		$user_ids = array();
808 3
		foreach ($roles as $id)
809
		{
810 3
			$user_ids = array_merge($user_ids, $roles_id[$id]['user_id']);
811
			// Let's query groups
812 3
			$sql = 'SELECT * FROM ' . USER_GROUP_TABLE . ' WHERE ' . $this->db->sql_in_set('group_id', $roles_id[$id]['group_id'], false, true);
0 ignored issues
show
Bug introduced by
The constant phpbbgallery\core\auth\USER_GROUP_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
813 3
			$result = $this->db->sql_query($sql);
814 3
			while ($row = $this->db->sql_fetchrow($result))
815
			{
816 3
				if ($row['user_pending'] == 0)
817
				{
818 3
					$user_ids[] = $row['user_id'];
819
				}
820
			}
821 3
			$this->db->sql_freeresult($result);
822
		}
823
824
		// Now we cycle the $user_ids to remove 0 and make ids unique
825 3
		$returning_value = array();
826 3
		foreach ($user_ids as $id)
827
		{
828 3
			if ($id != 0)
829
			{
830 3
				$returning_value[$id] = (int) $id;
831
			}
832
		}
833
834 3
		$user_ids = array();
835 3
		foreach ($returning_value as $id)
836
		{
837 3
			$user_ids[] = (int) $id;
838
		}
839 3
		return $user_ids;
840
	}
841
842
	/*
843
	* Get all albums that user has no access
844
	* return array	$exclude All albums we have no access due to zebra restrictions
845
	*/
846 76
	public function get_exclude_zebra()
847
	{
848 76
		$zebra_array = $this->get_user_zebra($this->phpbb_user->data['user_id']);
849 76
		$foes = array();
850 76
		if ($this->user->get_data('rrc_zebra'))
851
		{
852
			$foes = $this->get_user_foes($this->phpbb_user->data['user_id']);
853
		}
854 76
		$albums = $this->cache->get_albums();
855 76
		$exclude = array();
856 76
		foreach ($albums as $album)
857
		{
858
			// There is zebra only for users
859 76
			if ($album['album_type'] == 1 && $album['album_user_id'] > 0 && ($this->get_zebra_state($zebra_array, $album['album_user_id'], $album['album_id']) < $album['album_auth_access'] || in_array($album['album_user_id'], $foes)))
860
			{
861
				$exclude[] = (int) $album['album_id'];
862
			}
863
		}
864 76
		return $exclude;
865
	}
866
}
867