Passed
Push — master ( 45da19...9278c7 )
by Stanislav
41:52 queued 26:52
created

auth::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 17
ccs 14
cts 14
cp 1
rs 9.8333
cc 1
nc 1
nop 9
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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

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

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