includes/auth.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Sat, 10 Mar 2012 00:18:56 +0400
changeset 15 8804e4d3de74
parent 0 ceef6d64c21d
permissions -rw-r--r--
Added online gamers
indvdum@0
     1
<?php
indvdum@0
     2
/**
indvdum@0
     3
*
indvdum@0
     4
* @package phpBB3
indvdum@0
     5
* @version $Id$
indvdum@0
     6
* @copyright (c) 2005 phpBB Group
indvdum@0
     7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
indvdum@0
     8
*
indvdum@0
     9
*/
indvdum@0
    10
indvdum@0
    11
/**
indvdum@0
    12
* @ignore
indvdum@0
    13
*/
indvdum@0
    14
if (!defined('IN_PHPBB'))
indvdum@0
    15
{
indvdum@0
    16
	exit;
indvdum@0
    17
}
indvdum@0
    18
indvdum@0
    19
/**
indvdum@0
    20
* Permission/Auth class
indvdum@0
    21
* @package phpBB3
indvdum@0
    22
*/
indvdum@0
    23
class auth
indvdum@0
    24
{
indvdum@0
    25
	var $acl = array();
indvdum@0
    26
	var $cache = array();
indvdum@0
    27
	var $acl_options = array();
indvdum@0
    28
	var $acl_forum_ids = false;
indvdum@0
    29
indvdum@0
    30
	/**
indvdum@0
    31
	* Init permissions
indvdum@0
    32
	*/
indvdum@0
    33
	function acl(&$userdata)
indvdum@0
    34
	{
indvdum@0
    35
		global $db, $cache;
indvdum@0
    36
indvdum@0
    37
		$this->acl = $this->cache = $this->acl_options = array();
indvdum@0
    38
		$this->acl_forum_ids = false;
indvdum@0
    39
indvdum@0
    40
		if (($this->acl_options = $cache->get('_acl_options')) === false)
indvdum@0
    41
		{
indvdum@0
    42
			$sql = 'SELECT auth_option_id, auth_option, is_global, is_local
indvdum@0
    43
				FROM ' . ACL_OPTIONS_TABLE . '
indvdum@0
    44
				ORDER BY auth_option_id';
indvdum@0
    45
			$result = $db->sql_query($sql);
indvdum@0
    46
indvdum@0
    47
			$global = $local = 0;
indvdum@0
    48
			$this->acl_options = array();
indvdum@0
    49
			while ($row = $db->sql_fetchrow($result))
indvdum@0
    50
			{
indvdum@0
    51
				if ($row['is_global'])
indvdum@0
    52
				{
indvdum@0
    53
					$this->acl_options['global'][$row['auth_option']] = $global++;
indvdum@0
    54
				}
indvdum@0
    55
indvdum@0
    56
				if ($row['is_local'])
indvdum@0
    57
				{
indvdum@0
    58
					$this->acl_options['local'][$row['auth_option']] = $local++;
indvdum@0
    59
				}
indvdum@0
    60
indvdum@0
    61
				$this->acl_options['id'][$row['auth_option']] = (int) $row['auth_option_id'];
indvdum@0
    62
				$this->acl_options['option'][(int) $row['auth_option_id']] = $row['auth_option'];
indvdum@0
    63
			}
indvdum@0
    64
			$db->sql_freeresult($result);
indvdum@0
    65
indvdum@0
    66
			$cache->put('_acl_options', $this->acl_options);
indvdum@0
    67
		}
indvdum@0
    68
indvdum@0
    69
		if (!trim($userdata['user_permissions']))
indvdum@0
    70
		{
indvdum@0
    71
			$this->acl_cache($userdata);
indvdum@0
    72
		}
indvdum@0
    73
indvdum@0
    74
		// Fill ACL array
indvdum@0
    75
		$this->_fill_acl($userdata['user_permissions']);
indvdum@0
    76
indvdum@0
    77
		// Verify bitstring length with options provided...
indvdum@0
    78
		$renew = false;
indvdum@0
    79
		$global_length = sizeof($this->acl_options['global']);
indvdum@0
    80
		$local_length = sizeof($this->acl_options['local']);
indvdum@0
    81
indvdum@0
    82
		// Specify comparing length (bitstring is padded to 31 bits)
indvdum@0
    83
		$global_length = ($global_length % 31) ? ($global_length - ($global_length % 31) + 31) : $global_length;
indvdum@0
    84
		$local_length = ($local_length % 31) ? ($local_length - ($local_length % 31) + 31) : $local_length;
indvdum@0
    85
indvdum@0
    86
		// You thought we are finished now? Noooo... now compare them.
indvdum@0
    87
		foreach ($this->acl as $forum_id => $bitstring)
indvdum@0
    88
		{
indvdum@0
    89
			if (($forum_id && strlen($bitstring) != $local_length) || (!$forum_id && strlen($bitstring) != $global_length))
indvdum@0
    90
			{
indvdum@0
    91
				$renew = true;
indvdum@0
    92
				break;
indvdum@0
    93
			}
indvdum@0
    94
		}
indvdum@0
    95
indvdum@0
    96
		// If a bitstring within the list does not match the options, we have a user with incorrect permissions set and need to renew them
indvdum@0
    97
		if ($renew)
indvdum@0
    98
		{
indvdum@0
    99
			$this->acl_cache($userdata);
indvdum@0
   100
			$this->_fill_acl($userdata['user_permissions']);
indvdum@0
   101
		}
indvdum@0
   102
indvdum@0
   103
		return;
indvdum@0
   104
	}
indvdum@0
   105
indvdum@0
   106
	/**
indvdum@0
   107
	* Fill ACL array with relevant bitstrings from user_permissions column
indvdum@0
   108
	* @access private
indvdum@0
   109
	*/
indvdum@0
   110
	function _fill_acl($user_permissions)
indvdum@0
   111
	{
indvdum@0
   112
		$seq_cache = array();
indvdum@0
   113
		$this->acl = array();
indvdum@0
   114
		$user_permissions = explode("\n", $user_permissions);
indvdum@0
   115
indvdum@0
   116
		foreach ($user_permissions as $f => $seq)
indvdum@0
   117
		{
indvdum@0
   118
			if ($seq)
indvdum@0
   119
			{
indvdum@0
   120
				$i = 0;
indvdum@0
   121
indvdum@0
   122
				if (!isset($this->acl[$f]))
indvdum@0
   123
				{
indvdum@0
   124
					$this->acl[$f] = '';
indvdum@0
   125
				}
indvdum@0
   126
indvdum@0
   127
				while ($subseq = substr($seq, $i, 6))
indvdum@0
   128
				{
indvdum@0
   129
					if (isset($seq_cache[$subseq]))
indvdum@0
   130
					{
indvdum@0
   131
						$converted = $seq_cache[$subseq];
indvdum@0
   132
					}
indvdum@0
   133
					else
indvdum@0
   134
					{
indvdum@0
   135
						$converted = $seq_cache[$subseq] = str_pad(base_convert($subseq, 36, 2), 31, 0, STR_PAD_LEFT);
indvdum@0
   136
					}
indvdum@0
   137
indvdum@0
   138
					// We put the original bitstring into the acl array
indvdum@0
   139
					$this->acl[$f] .= $converted;
indvdum@0
   140
					$i += 6;
indvdum@0
   141
				}
indvdum@0
   142
			}
indvdum@0
   143
		}
indvdum@0
   144
	}
indvdum@0
   145
indvdum@0
   146
	/**
indvdum@0
   147
	* Look up an option
indvdum@0
   148
	* if the option is prefixed with !, then the result becomes negated
indvdum@0
   149
	*
indvdum@0
   150
	* If a forum id is specified the local option will be combined with a global option if one exist.
indvdum@0
   151
	* If a forum id is not specified, only the global option will be checked.
indvdum@0
   152
	*/
indvdum@0
   153
	function acl_get($opt, $f = 0)
indvdum@0
   154
	{
indvdum@0
   155
		$negate = false;
indvdum@0
   156
indvdum@0
   157
		if (strpos($opt, '!') === 0)
indvdum@0
   158
		{
indvdum@0
   159
			$negate = true;
indvdum@0
   160
			$opt = substr($opt, 1);
indvdum@0
   161
		}
indvdum@0
   162
indvdum@0
   163
		if (!isset($this->cache[$f][$opt]))
indvdum@0
   164
		{
indvdum@0
   165
			// We combine the global/local option with an OR because some options are global and local.
indvdum@0
   166
			// If the user has the global permission the local one is true too and vice versa
indvdum@0
   167
			$this->cache[$f][$opt] = false;
indvdum@0
   168
indvdum@0
   169
			// Is this option a global permission setting?
indvdum@0
   170
			if (isset($this->acl_options['global'][$opt]))
indvdum@0
   171
			{
indvdum@0
   172
				if (isset($this->acl[0]))
indvdum@0
   173
				{
indvdum@0
   174
					$this->cache[$f][$opt] = $this->acl[0][$this->acl_options['global'][$opt]];
indvdum@0
   175
				}
indvdum@0
   176
			}
indvdum@0
   177
indvdum@0
   178
			// Is this option a local permission setting?
indvdum@0
   179
			// But if we check for a global option only, we won't combine the options...
indvdum@0
   180
			if ($f != 0 && isset($this->acl_options['local'][$opt]))
indvdum@0
   181
			{
indvdum@0
   182
				if (isset($this->acl[$f]) && isset($this->acl[$f][$this->acl_options['local'][$opt]]))
indvdum@0
   183
				{
indvdum@0
   184
					$this->cache[$f][$opt] |= $this->acl[$f][$this->acl_options['local'][$opt]];
indvdum@0
   185
				}
indvdum@0
   186
			}
indvdum@0
   187
		}
indvdum@0
   188
indvdum@0
   189
		// Founder always has all global options set to true...
indvdum@0
   190
		return ($negate) ? !$this->cache[$f][$opt] : $this->cache[$f][$opt];
indvdum@0
   191
	}
indvdum@0
   192
indvdum@0
   193
	/**
indvdum@0
   194
	* Get forums with the specified permission setting
indvdum@0
   195
	* if the option is prefixed with !, then the result becomes nagated
indvdum@0
   196
	*
indvdum@0
   197
	* @param bool $clean set to true if only values needs to be returned which are set/unset
indvdum@0
   198
	*/
indvdum@0
   199
	function acl_getf($opt, $clean = false)
indvdum@0
   200
	{
indvdum@0
   201
		$acl_f = array();
indvdum@0
   202
		$negate = false;
indvdum@0
   203
indvdum@0
   204
		if (strpos($opt, '!') === 0)
indvdum@0
   205
		{
indvdum@0
   206
			$negate = true;
indvdum@0
   207
			$opt = substr($opt, 1);
indvdum@0
   208
		}
indvdum@0
   209
indvdum@0
   210
		// If we retrieve a list of forums not having permissions in, we need to get every forum_id
indvdum@0
   211
		if ($negate)
indvdum@0
   212
		{
indvdum@0
   213
			if ($this->acl_forum_ids === false)
indvdum@0
   214
			{
indvdum@0
   215
				global $db;
indvdum@0
   216
indvdum@0
   217
				$sql = 'SELECT forum_id
indvdum@0
   218
					FROM ' . FORUMS_TABLE;
indvdum@0
   219
indvdum@0
   220
				if (sizeof($this->acl))
indvdum@0
   221
				{
indvdum@0
   222
					$sql .= ' WHERE ' . $db->sql_in_set('forum_id', array_keys($this->acl), true);
indvdum@0
   223
				}
indvdum@0
   224
				$result = $db->sql_query($sql);
indvdum@0
   225
indvdum@0
   226
				$this->acl_forum_ids = array();
indvdum@0
   227
				while ($row = $db->sql_fetchrow($result))
indvdum@0
   228
				{
indvdum@0
   229
					$this->acl_forum_ids[] = $row['forum_id'];
indvdum@0
   230
				}
indvdum@0
   231
				$db->sql_freeresult($result);
indvdum@0
   232
			}
indvdum@0
   233
		}
indvdum@0
   234
indvdum@0
   235
		if (isset($this->acl_options['local'][$opt]))
indvdum@0
   236
		{
indvdum@0
   237
			foreach ($this->acl as $f => $bitstring)
indvdum@0
   238
			{
indvdum@0
   239
				// Skip global settings
indvdum@0
   240
				if (!$f)
indvdum@0
   241
				{
indvdum@0
   242
					continue;
indvdum@0
   243
				}
indvdum@0
   244
indvdum@0
   245
				$allowed = (!isset($this->cache[$f][$opt])) ? $this->acl_get($opt, $f) : $this->cache[$f][$opt];
indvdum@0
   246
indvdum@0
   247
				if (!$clean)
indvdum@0
   248
				{
indvdum@0
   249
					$acl_f[$f][$opt] = ($negate) ? !$allowed : $allowed;
indvdum@0
   250
				}
indvdum@0
   251
				else
indvdum@0
   252
				{
indvdum@0
   253
					if (($negate && !$allowed) || (!$negate && $allowed))
indvdum@0
   254
					{
indvdum@0
   255
						$acl_f[$f][$opt] = 1;
indvdum@0
   256
					}
indvdum@0
   257
				}
indvdum@0
   258
			}
indvdum@0
   259
		}
indvdum@0
   260
indvdum@0
   261
		// If we get forum_ids not having this permission, we need to fill the remaining parts
indvdum@0
   262
		if ($negate && sizeof($this->acl_forum_ids))
indvdum@0
   263
		{
indvdum@0
   264
			foreach ($this->acl_forum_ids as $f)
indvdum@0
   265
			{
indvdum@0
   266
				$acl_f[$f][$opt] = 1;
indvdum@0
   267
			}
indvdum@0
   268
		}
indvdum@0
   269
indvdum@0
   270
		return $acl_f;
indvdum@0
   271
	}
indvdum@0
   272
indvdum@0
   273
	/**
indvdum@0
   274
	* Get local permission state for any forum.
indvdum@0
   275
	*
indvdum@0
   276
	* Returns true if user has the permission in one or more forums, false if in no forum.
indvdum@0
   277
	* If global option is checked it returns the global state (same as acl_get($opt))
indvdum@0
   278
	* Local option has precedence...
indvdum@0
   279
	*/
indvdum@0
   280
	function acl_getf_global($opt)
indvdum@0
   281
	{
indvdum@0
   282
		if (is_array($opt))
indvdum@0
   283
		{
indvdum@0
   284
			// evaluates to true as soon as acl_getf_global is true for one option
indvdum@0
   285
			foreach ($opt as $check_option)
indvdum@0
   286
			{
indvdum@0
   287
				if ($this->acl_getf_global($check_option))
indvdum@0
   288
				{
indvdum@0
   289
					return true;
indvdum@0
   290
				}
indvdum@0
   291
			}
indvdum@0
   292
indvdum@0
   293
			return false;
indvdum@0
   294
		}
indvdum@0
   295
indvdum@0
   296
		if (isset($this->acl_options['local'][$opt]))
indvdum@0
   297
		{
indvdum@0
   298
			foreach ($this->acl as $f => $bitstring)
indvdum@0
   299
			{
indvdum@0
   300
				// Skip global settings
indvdum@0
   301
				if (!$f)
indvdum@0
   302
				{
indvdum@0
   303
					continue;
indvdum@0
   304
				}
indvdum@0
   305
indvdum@0
   306
				// as soon as the user has any permission we're done so return true
indvdum@0
   307
				if ((!isset($this->cache[$f][$opt])) ? $this->acl_get($opt, $f) : $this->cache[$f][$opt])
indvdum@0
   308
				{
indvdum@0
   309
					return true;
indvdum@0
   310
				}
indvdum@0
   311
			}
indvdum@0
   312
		}
indvdum@0
   313
		else if (isset($this->acl_options['global'][$opt]))
indvdum@0
   314
		{
indvdum@0
   315
			return $this->acl_get($opt);
indvdum@0
   316
		}
indvdum@0
   317
indvdum@0
   318
		return false;
indvdum@0
   319
	}
indvdum@0
   320
indvdum@0
   321
	/**
indvdum@0
   322
	* Get permission settings (more than one)
indvdum@0
   323
	*/
indvdum@0
   324
	function acl_gets()
indvdum@0
   325
	{
indvdum@0
   326
		$args = func_get_args();
indvdum@0
   327
		$f = array_pop($args);
indvdum@0
   328
indvdum@0
   329
		if (!is_numeric($f))
indvdum@0
   330
		{
indvdum@0
   331
			$args[] = $f;
indvdum@0
   332
			$f = 0;
indvdum@0
   333
		}
indvdum@0
   334
indvdum@0
   335
		// alternate syntax: acl_gets(array('m_', 'a_'), $forum_id)
indvdum@0
   336
		if (is_array($args[0]))
indvdum@0
   337
		{
indvdum@0
   338
			$args = $args[0];
indvdum@0
   339
		}
indvdum@0
   340
indvdum@0
   341
		$acl = 0;
indvdum@0
   342
		foreach ($args as $opt)
indvdum@0
   343
		{
indvdum@0
   344
			$acl |= $this->acl_get($opt, $f);
indvdum@0
   345
		}
indvdum@0
   346
indvdum@0
   347
		return $acl;
indvdum@0
   348
	}
indvdum@0
   349
indvdum@0
   350
	/**
indvdum@0
   351
	* Get permission listing based on user_id/options/forum_ids
indvdum@0
   352
	*
indvdum@0
   353
	* Be careful when using this function with permissions a_, m_, u_ and f_ !
indvdum@0
   354
	* It may not work correctly. When a user group grants an a_* permission,
indvdum@0
   355
	* e.g. a_foo, but the user's a_foo permission is set to "Never", then
indvdum@0
   356
	* the user does not in fact have the a_ permission.
indvdum@0
   357
	* But the user will still be listed as having the a_ permission.
indvdum@0
   358
	*
indvdum@0
   359
	* For more information see: http://tracker.phpbb.com/browse/PHPBB3-10252
indvdum@0
   360
	*/
indvdum@0
   361
	function acl_get_list($user_id = false, $opts = false, $forum_id = false)
indvdum@0
   362
	{
indvdum@0
   363
		if ($user_id !== false && !is_array($user_id) && $opts === false && $forum_id === false)
indvdum@0
   364
		{
indvdum@0
   365
			$hold_ary = array($user_id => $this->acl_raw_data_single_user($user_id));
indvdum@0
   366
		}
indvdum@0
   367
		else
indvdum@0
   368
		{
indvdum@0
   369
			$hold_ary = $this->acl_raw_data($user_id, $opts, $forum_id);
indvdum@0
   370
		}
indvdum@0
   371
indvdum@0
   372
		$auth_ary = array();
indvdum@0
   373
		foreach ($hold_ary as $user_id => $forum_ary)
indvdum@0
   374
		{
indvdum@0
   375
			foreach ($forum_ary as $forum_id => $auth_option_ary)
indvdum@0
   376
			{
indvdum@0
   377
				foreach ($auth_option_ary as $auth_option => $auth_setting)
indvdum@0
   378
				{
indvdum@0
   379
					if ($auth_setting)
indvdum@0
   380
					{
indvdum@0
   381
						$auth_ary[$forum_id][$auth_option][] = $user_id;
indvdum@0
   382
					}
indvdum@0
   383
				}
indvdum@0
   384
			}
indvdum@0
   385
		}
indvdum@0
   386
indvdum@0
   387
		return $auth_ary;
indvdum@0
   388
	}
indvdum@0
   389
indvdum@0
   390
	/**
indvdum@0
   391
	* Cache data to user_permissions row
indvdum@0
   392
	*/
indvdum@0
   393
	function acl_cache(&$userdata)
indvdum@0
   394
	{
indvdum@0
   395
		global $db;
indvdum@0
   396
indvdum@0
   397
		// Empty user_permissions
indvdum@0
   398
		$userdata['user_permissions'] = '';
indvdum@0
   399
indvdum@0
   400
		$hold_ary = $this->acl_raw_data_single_user($userdata['user_id']);
indvdum@0
   401
indvdum@0
   402
		// Key 0 in $hold_ary are global options, all others are forum_ids
indvdum@0
   403
indvdum@0
   404
		// If this user is founder we're going to force fill the admin options ...
indvdum@0
   405
		if ($userdata['user_type'] == USER_FOUNDER)
indvdum@0
   406
		{
indvdum@0
   407
			foreach ($this->acl_options['global'] as $opt => $id)
indvdum@0
   408
			{
indvdum@0
   409
				if (strpos($opt, 'a_') === 0)
indvdum@0
   410
				{
indvdum@0
   411
					$hold_ary[0][$this->acl_options['id'][$opt]] = ACL_YES;
indvdum@0
   412
				}
indvdum@0
   413
			}
indvdum@0
   414
		}
indvdum@0
   415
indvdum@0
   416
		$hold_str = $this->build_bitstring($hold_ary);
indvdum@0
   417
indvdum@0
   418
		if ($hold_str)
indvdum@0
   419
		{
indvdum@0
   420
			$userdata['user_permissions'] = $hold_str;
indvdum@0
   421
indvdum@0
   422
			$sql = 'UPDATE ' . USERS_TABLE . "
indvdum@0
   423
				SET user_permissions = '" . $db->sql_escape($userdata['user_permissions']) . "',
indvdum@0
   424
					user_perm_from = 0
indvdum@0
   425
				WHERE user_id = " . $userdata['user_id'];
indvdum@0
   426
			$db->sql_query($sql);
indvdum@0
   427
		}
indvdum@0
   428
indvdum@0
   429
		return;
indvdum@0
   430
	}
indvdum@0
   431
indvdum@0
   432
	/**
indvdum@0
   433
	* Build bitstring from permission set
indvdum@0
   434
	*/
indvdum@0
   435
	function build_bitstring(&$hold_ary)
indvdum@0
   436
	{
indvdum@0
   437
		$hold_str = '';
indvdum@0
   438
indvdum@0
   439
		if (sizeof($hold_ary))
indvdum@0
   440
		{
indvdum@0
   441
			ksort($hold_ary);
indvdum@0
   442
indvdum@0
   443
			$last_f = 0;
indvdum@0
   444
indvdum@0
   445
			foreach ($hold_ary as $f => $auth_ary)
indvdum@0
   446
			{
indvdum@0
   447
				$ary_key = (!$f) ? 'global' : 'local';
indvdum@0
   448
indvdum@0
   449
				$bitstring = array();
indvdum@0
   450
				foreach ($this->acl_options[$ary_key] as $opt => $id)
indvdum@0
   451
				{
indvdum@0
   452
					if (isset($auth_ary[$this->acl_options['id'][$opt]]))
indvdum@0
   453
					{
indvdum@0
   454
						$bitstring[$id] = $auth_ary[$this->acl_options['id'][$opt]];
indvdum@0
   455
indvdum@0
   456
						$option_key = substr($opt, 0, strpos($opt, '_') + 1);
indvdum@0
   457
indvdum@0
   458
						// If one option is allowed, the global permission for this option has to be allowed too
indvdum@0
   459
						// example: if the user has the a_ permission this means he has one or more a_* permissions
indvdum@0
   460
						if ($auth_ary[$this->acl_options['id'][$opt]] == ACL_YES && (!isset($bitstring[$this->acl_options[$ary_key][$option_key]]) || $bitstring[$this->acl_options[$ary_key][$option_key]] == ACL_NEVER))
indvdum@0
   461
						{
indvdum@0
   462
							$bitstring[$this->acl_options[$ary_key][$option_key]] = ACL_YES;
indvdum@0
   463
						}
indvdum@0
   464
					}
indvdum@0
   465
					else
indvdum@0
   466
					{
indvdum@0
   467
						$bitstring[$id] = ACL_NEVER;
indvdum@0
   468
					}
indvdum@0
   469
				}
indvdum@0
   470
indvdum@0
   471
				// Now this bitstring defines the permission setting for the current forum $f (or global setting)
indvdum@0
   472
				$bitstring = implode('', $bitstring);
indvdum@0
   473
indvdum@0
   474
				// The line number indicates the id, therefore we have to add empty lines for those ids not present
indvdum@0
   475
				$hold_str .= str_repeat("\n", $f - $last_f);
indvdum@0
   476
indvdum@0
   477
				// Convert bitstring for storage - we do not use binary/bytes because PHP's string functions are not fully binary safe
indvdum@0
   478
				for ($i = 0, $bit_length = strlen($bitstring); $i < $bit_length; $i += 31)
indvdum@0
   479
				{
indvdum@0
   480
					$hold_str .= str_pad(base_convert(str_pad(substr($bitstring, $i, 31), 31, 0, STR_PAD_RIGHT), 2, 36), 6, 0, STR_PAD_LEFT);
indvdum@0
   481
				}
indvdum@0
   482
indvdum@0
   483
				$last_f = $f;
indvdum@0
   484
			}
indvdum@0
   485
			unset($bitstring);
indvdum@0
   486
indvdum@0
   487
			$hold_str = rtrim($hold_str);
indvdum@0
   488
		}
indvdum@0
   489
indvdum@0
   490
		return $hold_str;
indvdum@0
   491
	}
indvdum@0
   492
indvdum@0
   493
	/**
indvdum@0
   494
	* Clear one or all users cached permission settings
indvdum@0
   495
	*/
indvdum@0
   496
	function acl_clear_prefetch($user_id = false)
indvdum@0
   497
	{
indvdum@0
   498
		global $db, $cache;
indvdum@0
   499
indvdum@0
   500
		// Rebuild options cache
indvdum@0
   501
		$cache->destroy('_role_cache');
indvdum@0
   502
indvdum@0
   503
		$sql = 'SELECT *
indvdum@0
   504
			FROM ' . ACL_ROLES_DATA_TABLE . '
indvdum@0
   505
			ORDER BY role_id ASC';
indvdum@0
   506
		$result = $db->sql_query($sql);
indvdum@0
   507
indvdum@0
   508
		$this->role_cache = array();
indvdum@0
   509
		while ($row = $db->sql_fetchrow($result))
indvdum@0
   510
		{
indvdum@0
   511
			$this->role_cache[$row['role_id']][$row['auth_option_id']] = (int) $row['auth_setting'];
indvdum@0
   512
		}
indvdum@0
   513
		$db->sql_freeresult($result);
indvdum@0
   514
indvdum@0
   515
		foreach ($this->role_cache as $role_id => $role_options)
indvdum@0
   516
		{
indvdum@0
   517
			$this->role_cache[$role_id] = serialize($role_options);
indvdum@0
   518
		}
indvdum@0
   519
indvdum@0
   520
		$cache->put('_role_cache', $this->role_cache);
indvdum@0
   521
indvdum@0
   522
		// Now empty user permissions
indvdum@0
   523
		$where_sql = '';
indvdum@0
   524
indvdum@0
   525
		if ($user_id !== false)
indvdum@0
   526
		{
indvdum@0
   527
			$user_id = (!is_array($user_id)) ? $user_id = array((int) $user_id) : array_map('intval', $user_id);
indvdum@0
   528
			$where_sql = ' WHERE ' . $db->sql_in_set('user_id', $user_id);
indvdum@0
   529
		}
indvdum@0
   530
indvdum@0
   531
		$sql = 'UPDATE ' . USERS_TABLE . "
indvdum@0
   532
			SET user_permissions = '',
indvdum@0
   533
				user_perm_from = 0
indvdum@0
   534
			$where_sql";
indvdum@0
   535
		$db->sql_query($sql);
indvdum@0
   536
indvdum@0
   537
		return;
indvdum@0
   538
	}
indvdum@0
   539
indvdum@0
   540
	/**
indvdum@0
   541
	* Get assigned roles
indvdum@0
   542
	*/
indvdum@0
   543
	function acl_role_data($user_type, $role_type, $ug_id = false, $forum_id = false)
indvdum@0
   544
	{
indvdum@0
   545
		global $db;
indvdum@0
   546
indvdum@0
   547
		$roles = array();
indvdum@0
   548
indvdum@0
   549
		$sql_id = ($user_type == 'user') ? 'user_id' : 'group_id';
indvdum@0
   550
indvdum@0
   551
		$sql_ug = ($ug_id !== false) ? ((!is_array($ug_id)) ? "AND a.$sql_id = $ug_id" : 'AND ' . $db->sql_in_set("a.$sql_id", $ug_id)) : '';
indvdum@0
   552
		$sql_forum = ($forum_id !== false) ? ((!is_array($forum_id)) ? "AND a.forum_id = $forum_id" : 'AND ' . $db->sql_in_set('a.forum_id', $forum_id)) : '';
indvdum@0
   553
indvdum@0
   554
		// Grab assigned roles...
indvdum@0
   555
		$sql = 'SELECT a.auth_role_id, a.' . $sql_id . ', a.forum_id
indvdum@0
   556
			FROM ' . (($user_type == 'user') ? ACL_USERS_TABLE : ACL_GROUPS_TABLE) . ' a, ' . ACL_ROLES_TABLE . " r
indvdum@0
   557
			WHERE a.auth_role_id = r.role_id
indvdum@0
   558
				AND r.role_type = '" . $db->sql_escape($role_type) . "'
indvdum@0
   559
				$sql_ug
indvdum@0
   560
				$sql_forum
indvdum@0
   561
			ORDER BY r.role_order ASC";
indvdum@0
   562
		$result = $db->sql_query($sql);
indvdum@0
   563
indvdum@0
   564
		while ($row = $db->sql_fetchrow($result))
indvdum@0
   565
		{
indvdum@0
   566
			$roles[$row[$sql_id]][$row['forum_id']] = $row['auth_role_id'];
indvdum@0
   567
		}
indvdum@0
   568
		$db->sql_freeresult($result);
indvdum@0
   569
indvdum@0
   570
		return $roles;
indvdum@0
   571
	}
indvdum@0
   572
indvdum@0
   573
	/**
indvdum@0
   574
	* Get raw acl data based on user/option/forum
indvdum@0
   575
	*/
indvdum@0
   576
	function acl_raw_data($user_id = false, $opts = false, $forum_id = false)
indvdum@0
   577
	{
indvdum@0
   578
		global $db;
indvdum@0
   579
indvdum@0
   580
		$sql_user = ($user_id !== false) ? ((!is_array($user_id)) ? 'user_id = ' . (int) $user_id : $db->sql_in_set('user_id', array_map('intval', $user_id))) : '';
indvdum@0
   581
		$sql_forum = ($forum_id !== false) ? ((!is_array($forum_id)) ? 'AND a.forum_id = ' . (int) $forum_id : 'AND ' . $db->sql_in_set('a.forum_id', array_map('intval', $forum_id))) : '';
indvdum@0
   582
indvdum@0
   583
		$sql_opts = $sql_opts_select = $sql_opts_from = '';
indvdum@0
   584
		$hold_ary = array();
indvdum@0
   585
indvdum@0
   586
		if ($opts !== false)
indvdum@0
   587
		{
indvdum@0
   588
			$sql_opts_select = ', ao.auth_option';
indvdum@0
   589
			$sql_opts_from = ', ' . ACL_OPTIONS_TABLE . ' ao';
indvdum@0
   590
			$this->build_auth_option_statement('ao.auth_option', $opts, $sql_opts);
indvdum@0
   591
		}
indvdum@0
   592
indvdum@0
   593
		$sql_ary = array();
indvdum@0
   594
indvdum@0
   595
		// Grab non-role settings - user-specific
indvdum@0
   596
		$sql_ary[] = 'SELECT a.user_id, a.forum_id, a.auth_setting, a.auth_option_id' . $sql_opts_select . '
indvdum@0
   597
			FROM ' . ACL_USERS_TABLE . ' a' . $sql_opts_from . '
indvdum@0
   598
			WHERE a.auth_role_id = 0 ' .
indvdum@0
   599
				(($sql_opts_from) ? 'AND a.auth_option_id = ao.auth_option_id ' : '') .
indvdum@0
   600
				(($sql_user) ? 'AND a.' . $sql_user : '') . "
indvdum@0
   601
				$sql_forum
indvdum@0
   602
				$sql_opts";
indvdum@0
   603
indvdum@0
   604
		// Now the role settings - user-specific
indvdum@0
   605
		$sql_ary[] = 'SELECT a.user_id, a.forum_id, r.auth_option_id, r.auth_setting, r.auth_option_id' . $sql_opts_select . '
indvdum@0
   606
			FROM ' . ACL_USERS_TABLE . ' a, ' . ACL_ROLES_DATA_TABLE . ' r' . $sql_opts_from . '
indvdum@0
   607
			WHERE a.auth_role_id = r.role_id ' .
indvdum@0
   608
				(($sql_opts_from) ? 'AND r.auth_option_id = ao.auth_option_id ' : '') .
indvdum@0
   609
				(($sql_user) ? 'AND a.' . $sql_user : '') . "
indvdum@0
   610
				$sql_forum
indvdum@0
   611
				$sql_opts";
indvdum@0
   612
indvdum@0
   613
		foreach ($sql_ary as $sql)
indvdum@0
   614
		{
indvdum@0
   615
			$result = $db->sql_query($sql);
indvdum@0
   616
indvdum@0
   617
			while ($row = $db->sql_fetchrow($result))
indvdum@0
   618
			{
indvdum@0
   619
				$option = ($sql_opts_select) ? $row['auth_option'] : $this->acl_options['option'][$row['auth_option_id']];
indvdum@0
   620
				$hold_ary[$row['user_id']][$row['forum_id']][$option] = $row['auth_setting'];
indvdum@0
   621
			}
indvdum@0
   622
			$db->sql_freeresult($result);
indvdum@0
   623
		}
indvdum@0
   624
indvdum@0
   625
		$sql_ary = array();
indvdum@0
   626
indvdum@0
   627
		// Now grab group settings - non-role specific...
indvdum@0
   628
		$sql_ary[] = 'SELECT ug.user_id, a.forum_id, a.auth_setting, a.auth_option_id' . $sql_opts_select . '
indvdum@0
   629
			FROM ' . ACL_GROUPS_TABLE . ' a, ' . USER_GROUP_TABLE . ' ug, ' . GROUPS_TABLE . ' g' . $sql_opts_from . '
indvdum@0
   630
			WHERE a.auth_role_id = 0 ' .
indvdum@0
   631
				(($sql_opts_from) ? 'AND a.auth_option_id = ao.auth_option_id ' : '') . '
indvdum@0
   632
				AND a.group_id = ug.group_id
indvdum@0
   633
				AND g.group_id = ug.group_id
indvdum@0
   634
				AND ug.user_pending = 0
indvdum@0
   635
				AND NOT (ug.group_leader = 1 AND g.group_skip_auth = 1)
indvdum@0
   636
				' . (($sql_user) ? 'AND ug.' . $sql_user : '') . "
indvdum@0
   637
				$sql_forum
indvdum@0
   638
				$sql_opts";
indvdum@0
   639
indvdum@0
   640
		// Now grab group settings - role specific...
indvdum@0
   641
		$sql_ary[] = 'SELECT ug.user_id, a.forum_id, r.auth_setting, r.auth_option_id' . $sql_opts_select . '
indvdum@0
   642
			FROM ' . ACL_GROUPS_TABLE . ' a, ' . USER_GROUP_TABLE . ' ug, ' . GROUPS_TABLE . ' g, ' . ACL_ROLES_DATA_TABLE . ' r' . $sql_opts_from . '
indvdum@0
   643
			WHERE a.auth_role_id = r.role_id ' .
indvdum@0
   644
				(($sql_opts_from) ? 'AND r.auth_option_id = ao.auth_option_id ' : '') . '
indvdum@0
   645
				AND a.group_id = ug.group_id
indvdum@0
   646
				AND g.group_id = ug.group_id
indvdum@0
   647
				AND ug.user_pending = 0
indvdum@0
   648
				AND NOT (ug.group_leader = 1 AND g.group_skip_auth = 1)
indvdum@0
   649
				' . (($sql_user) ? 'AND ug.' . $sql_user : '') . "
indvdum@0
   650
				$sql_forum
indvdum@0
   651
				$sql_opts";
indvdum@0
   652
indvdum@0
   653
		foreach ($sql_ary as $sql)
indvdum@0
   654
		{
indvdum@0
   655
			$result = $db->sql_query($sql);
indvdum@0
   656
indvdum@0
   657
			while ($row = $db->sql_fetchrow($result))
indvdum@0
   658
			{
indvdum@0
   659
				$option = ($sql_opts_select) ? $row['auth_option'] : $this->acl_options['option'][$row['auth_option_id']];
indvdum@0
   660
indvdum@0
   661
				if (!isset($hold_ary[$row['user_id']][$row['forum_id']][$option]) || (isset($hold_ary[$row['user_id']][$row['forum_id']][$option]) && $hold_ary[$row['user_id']][$row['forum_id']][$option] != ACL_NEVER))
indvdum@0
   662
				{
indvdum@0
   663
					$hold_ary[$row['user_id']][$row['forum_id']][$option] = $row['auth_setting'];
indvdum@0
   664
indvdum@0
   665
					// If we detect ACL_NEVER, we will unset the flag option (within building the bitstring it is correctly set again)
indvdum@0
   666
					if ($row['auth_setting'] == ACL_NEVER)
indvdum@0
   667
					{
indvdum@0
   668
						$flag = substr($option, 0, strpos($option, '_') + 1);
indvdum@0
   669
indvdum@0
   670
						if (isset($hold_ary[$row['user_id']][$row['forum_id']][$flag]) && $hold_ary[$row['user_id']][$row['forum_id']][$flag] == ACL_YES)
indvdum@0
   671
						{
indvdum@0
   672
							unset($hold_ary[$row['user_id']][$row['forum_id']][$flag]);
indvdum@0
   673
indvdum@0
   674
/*							if (in_array(ACL_YES, $hold_ary[$row['user_id']][$row['forum_id']]))
indvdum@0
   675
							{
indvdum@0
   676
								$hold_ary[$row['user_id']][$row['forum_id']][$flag] = ACL_YES;
indvdum@0
   677
							}
indvdum@0
   678
*/
indvdum@0
   679
						}
indvdum@0
   680
					}
indvdum@0
   681
				}
indvdum@0
   682
			}
indvdum@0
   683
			$db->sql_freeresult($result);
indvdum@0
   684
		}
indvdum@0
   685
indvdum@0
   686
		return $hold_ary;
indvdum@0
   687
	}
indvdum@0
   688
indvdum@0
   689
	/**
indvdum@0
   690
	* Get raw user based permission settings
indvdum@0
   691
	*/
indvdum@0
   692
	function acl_user_raw_data($user_id = false, $opts = false, $forum_id = false)
indvdum@0
   693
	{
indvdum@0
   694
		global $db;
indvdum@0
   695
indvdum@0
   696
		$sql_user = ($user_id !== false) ? ((!is_array($user_id)) ? 'user_id = ' . (int) $user_id : $db->sql_in_set('user_id', array_map('intval', $user_id))) : '';
indvdum@0
   697
		$sql_forum = ($forum_id !== false) ? ((!is_array($forum_id)) ? 'AND a.forum_id = ' . (int) $forum_id : 'AND ' . $db->sql_in_set('a.forum_id', array_map('intval', $forum_id))) : '';
indvdum@0
   698
indvdum@0
   699
		$sql_opts = '';
indvdum@0
   700
		$hold_ary = $sql_ary = array();
indvdum@0
   701
indvdum@0
   702
		if ($opts !== false)
indvdum@0
   703
		{
indvdum@0
   704
			$this->build_auth_option_statement('ao.auth_option', $opts, $sql_opts);
indvdum@0
   705
		}
indvdum@0
   706
indvdum@0
   707
		// Grab user settings - non-role specific...
indvdum@0
   708
		$sql_ary[] = 'SELECT a.user_id, a.forum_id, a.auth_setting, a.auth_option_id, ao.auth_option
indvdum@0
   709
			FROM ' . ACL_USERS_TABLE . ' a, ' . ACL_OPTIONS_TABLE . ' ao
indvdum@0
   710
			WHERE a.auth_role_id = 0
indvdum@0
   711
				AND a.auth_option_id = ao.auth_option_id ' .
indvdum@0
   712
				(($sql_user) ? 'AND a.' . $sql_user : '') . "
indvdum@0
   713
				$sql_forum
indvdum@0
   714
				$sql_opts
indvdum@0
   715
			ORDER BY a.forum_id, ao.auth_option";
indvdum@0
   716
indvdum@0
   717
		// Now the role settings - user-specific
indvdum@0
   718
		$sql_ary[] = 'SELECT a.user_id, a.forum_id, r.auth_option_id, r.auth_setting, r.auth_option_id, ao.auth_option
indvdum@0
   719
			FROM ' . ACL_USERS_TABLE . ' a, ' . ACL_ROLES_DATA_TABLE . ' r, ' . ACL_OPTIONS_TABLE . ' ao
indvdum@0
   720
			WHERE a.auth_role_id = r.role_id
indvdum@0
   721
				AND r.auth_option_id = ao.auth_option_id ' .
indvdum@0
   722
				(($sql_user) ? 'AND a.' . $sql_user : '') . "
indvdum@0
   723
				$sql_forum
indvdum@0
   724
				$sql_opts
indvdum@0
   725
			ORDER BY a.forum_id, ao.auth_option";
indvdum@0
   726
indvdum@0
   727
		foreach ($sql_ary as $sql)
indvdum@0
   728
		{
indvdum@0
   729
			$result = $db->sql_query($sql);
indvdum@0
   730
indvdum@0
   731
			while ($row = $db->sql_fetchrow($result))
indvdum@0
   732
			{
indvdum@0
   733
				$hold_ary[$row['user_id']][$row['forum_id']][$row['auth_option']] = $row['auth_setting'];
indvdum@0
   734
			}
indvdum@0
   735
			$db->sql_freeresult($result);
indvdum@0
   736
		}
indvdum@0
   737
indvdum@0
   738
		return $hold_ary;
indvdum@0
   739
	}
indvdum@0
   740
indvdum@0
   741
	/**
indvdum@0
   742
	* Get raw group based permission settings
indvdum@0
   743
	*/
indvdum@0
   744
	function acl_group_raw_data($group_id = false, $opts = false, $forum_id = false)
indvdum@0
   745
	{
indvdum@0
   746
		global $db;
indvdum@0
   747
indvdum@0
   748
		$sql_group = ($group_id !== false) ? ((!is_array($group_id)) ? 'group_id = ' . (int) $group_id : $db->sql_in_set('group_id', array_map('intval', $group_id))) : '';
indvdum@0
   749
		$sql_forum = ($forum_id !== false) ? ((!is_array($forum_id)) ? 'AND a.forum_id = ' . (int) $forum_id : 'AND ' . $db->sql_in_set('a.forum_id', array_map('intval', $forum_id))) : '';
indvdum@0
   750
indvdum@0
   751
		$sql_opts = '';
indvdum@0
   752
		$hold_ary = $sql_ary = array();
indvdum@0
   753
indvdum@0
   754
		if ($opts !== false)
indvdum@0
   755
		{
indvdum@0
   756
			$this->build_auth_option_statement('ao.auth_option', $opts, $sql_opts);
indvdum@0
   757
		}
indvdum@0
   758
indvdum@0
   759
		// Grab group settings - non-role specific...
indvdum@0
   760
		$sql_ary[] = 'SELECT a.group_id, a.forum_id, a.auth_setting, a.auth_option_id, ao.auth_option
indvdum@0
   761
			FROM ' . ACL_GROUPS_TABLE . ' a, ' . ACL_OPTIONS_TABLE . ' ao
indvdum@0
   762
			WHERE a.auth_role_id = 0
indvdum@0
   763
				AND a.auth_option_id = ao.auth_option_id ' .
indvdum@0
   764
				(($sql_group) ? 'AND a.' . $sql_group : '') . "
indvdum@0
   765
				$sql_forum
indvdum@0
   766
				$sql_opts
indvdum@0
   767
			ORDER BY a.forum_id, ao.auth_option";
indvdum@0
   768
indvdum@0
   769
		// Now grab group settings - role specific...
indvdum@0
   770
		$sql_ary[] = 'SELECT a.group_id, a.forum_id, r.auth_setting, r.auth_option_id, ao.auth_option
indvdum@0
   771
			FROM ' . ACL_GROUPS_TABLE . ' a, ' . ACL_ROLES_DATA_TABLE . ' r, ' . ACL_OPTIONS_TABLE . ' ao
indvdum@0
   772
			WHERE a.auth_role_id = r.role_id
indvdum@0
   773
				AND r.auth_option_id = ao.auth_option_id ' .
indvdum@0
   774
				(($sql_group) ? 'AND a.' . $sql_group : '') . "
indvdum@0
   775
				$sql_forum
indvdum@0
   776
				$sql_opts
indvdum@0
   777
			ORDER BY a.forum_id, ao.auth_option";
indvdum@0
   778
indvdum@0
   779
		foreach ($sql_ary as $sql)
indvdum@0
   780
		{
indvdum@0
   781
			$result = $db->sql_query($sql);
indvdum@0
   782
indvdum@0
   783
			while ($row = $db->sql_fetchrow($result))
indvdum@0
   784
			{
indvdum@0
   785
				$hold_ary[$row['group_id']][$row['forum_id']][$row['auth_option']] = $row['auth_setting'];
indvdum@0
   786
			}
indvdum@0
   787
			$db->sql_freeresult($result);
indvdum@0
   788
		}
indvdum@0
   789
indvdum@0
   790
		return $hold_ary;
indvdum@0
   791
	}
indvdum@0
   792
indvdum@0
   793
	/**
indvdum@0
   794
	* Get raw acl data based on user for caching user_permissions
indvdum@0
   795
	* This function returns the same data as acl_raw_data(), but without the user id as the first key within the array.
indvdum@0
   796
	*/
indvdum@0
   797
	function acl_raw_data_single_user($user_id)
indvdum@0
   798
	{
indvdum@0
   799
		global $db, $cache;
indvdum@0
   800
indvdum@0
   801
		// Check if the role-cache is there
indvdum@0
   802
		if (($this->role_cache = $cache->get('_role_cache')) === false)
indvdum@0
   803
		{
indvdum@0
   804
			$this->role_cache = array();
indvdum@0
   805
indvdum@0
   806
			// We pre-fetch roles
indvdum@0
   807
			$sql = 'SELECT *
indvdum@0
   808
				FROM ' . ACL_ROLES_DATA_TABLE . '
indvdum@0
   809
				ORDER BY role_id ASC';
indvdum@0
   810
			$result = $db->sql_query($sql);
indvdum@0
   811
indvdum@0
   812
			while ($row = $db->sql_fetchrow($result))
indvdum@0
   813
			{
indvdum@0
   814
				$this->role_cache[$row['role_id']][$row['auth_option_id']] = (int) $row['auth_setting'];
indvdum@0
   815
			}
indvdum@0
   816
			$db->sql_freeresult($result);
indvdum@0
   817
indvdum@0
   818
			foreach ($this->role_cache as $role_id => $role_options)
indvdum@0
   819
			{
indvdum@0
   820
				$this->role_cache[$role_id] = serialize($role_options);
indvdum@0
   821
			}
indvdum@0
   822
indvdum@0
   823
			$cache->put('_role_cache', $this->role_cache);
indvdum@0
   824
		}
indvdum@0
   825
indvdum@0
   826
		$hold_ary = array();
indvdum@0
   827
indvdum@0
   828
		// Grab user-specific permission settings
indvdum@0
   829
		$sql = 'SELECT forum_id, auth_option_id, auth_role_id, auth_setting
indvdum@0
   830
			FROM ' . ACL_USERS_TABLE . '
indvdum@0
   831
			WHERE user_id = ' . $user_id;
indvdum@0
   832
		$result = $db->sql_query($sql);
indvdum@0
   833
indvdum@0
   834
		while ($row = $db->sql_fetchrow($result))
indvdum@0
   835
		{
indvdum@0
   836
			// If a role is assigned, assign all options included within this role. Else, only set this one option.
indvdum@0
   837
			if ($row['auth_role_id'])
indvdum@0
   838
			{
indvdum@0
   839
				$hold_ary[$row['forum_id']] = (empty($hold_ary[$row['forum_id']])) ? unserialize($this->role_cache[$row['auth_role_id']]) : $hold_ary[$row['forum_id']] + unserialize($this->role_cache[$row['auth_role_id']]);
indvdum@0
   840
			}
indvdum@0
   841
			else
indvdum@0
   842
			{
indvdum@0
   843
				$hold_ary[$row['forum_id']][$row['auth_option_id']] = $row['auth_setting'];
indvdum@0
   844
			}
indvdum@0
   845
		}
indvdum@0
   846
		$db->sql_freeresult($result);
indvdum@0
   847
indvdum@0
   848
		// Now grab group-specific permission settings
indvdum@0
   849
		$sql = 'SELECT a.forum_id, a.auth_option_id, a.auth_role_id, a.auth_setting
indvdum@0
   850
			FROM ' . ACL_GROUPS_TABLE . ' a, ' . USER_GROUP_TABLE . ' ug, ' . GROUPS_TABLE . ' g
indvdum@0
   851
			WHERE a.group_id = ug.group_id
indvdum@0
   852
				AND g.group_id = ug.group_id
indvdum@0
   853
				AND ug.user_pending = 0
indvdum@0
   854
				AND NOT (ug.group_leader = 1 AND g.group_skip_auth = 1)
indvdum@0
   855
				AND ug.user_id = ' . $user_id;
indvdum@0
   856
		$result = $db->sql_query($sql);
indvdum@0
   857
indvdum@0
   858
		while ($row = $db->sql_fetchrow($result))
indvdum@0
   859
		{
indvdum@0
   860
			if (!$row['auth_role_id'])
indvdum@0
   861
			{
indvdum@0
   862
				$this->_set_group_hold_ary($hold_ary[$row['forum_id']], $row['auth_option_id'], $row['auth_setting']);
indvdum@0
   863
			}
indvdum@0
   864
			else if (!empty($this->role_cache[$row['auth_role_id']]))
indvdum@0
   865
			{
indvdum@0
   866
				foreach (unserialize($this->role_cache[$row['auth_role_id']]) as $option_id => $setting)
indvdum@0
   867
				{
indvdum@0
   868
					$this->_set_group_hold_ary($hold_ary[$row['forum_id']], $option_id, $setting);
indvdum@0
   869
				}
indvdum@0
   870
			}
indvdum@0
   871
		}
indvdum@0
   872
		$db->sql_freeresult($result);
indvdum@0
   873
indvdum@0
   874
		return $hold_ary;
indvdum@0
   875
	}
indvdum@0
   876
indvdum@0
   877
	/**
indvdum@0
   878
	* Private function snippet for setting a specific piece of the hold_ary
indvdum@0
   879
	*/
indvdum@0
   880
	function _set_group_hold_ary(&$hold_ary, $option_id, $setting)
indvdum@0
   881
	{
indvdum@0
   882
		if (!isset($hold_ary[$option_id]) || (isset($hold_ary[$option_id]) && $hold_ary[$option_id] != ACL_NEVER))
indvdum@0
   883
		{
indvdum@0
   884
			$hold_ary[$option_id] = $setting;
indvdum@0
   885
indvdum@0
   886
			// If we detect ACL_NEVER, we will unset the flag option (within building the bitstring it is correctly set again)
indvdum@0
   887
			if ($setting == ACL_NEVER)
indvdum@0
   888
			{
indvdum@0
   889
				$flag = substr($this->acl_options['option'][$option_id], 0, strpos($this->acl_options['option'][$option_id], '_') + 1);
indvdum@0
   890
				$flag = (int) $this->acl_options['id'][$flag];
indvdum@0
   891
indvdum@0
   892
				if (isset($hold_ary[$flag]) && $hold_ary[$flag] == ACL_YES)
indvdum@0
   893
				{
indvdum@0
   894
					unset($hold_ary[$flag]);
indvdum@0
   895
indvdum@0
   896
/*					This is uncommented, because i suspect this being slightly wrong due to mixed permission classes being possible
indvdum@0
   897
					if (in_array(ACL_YES, $hold_ary))
indvdum@0
   898
					{
indvdum@0
   899
						$hold_ary[$flag] = ACL_YES;
indvdum@0
   900
					}*/
indvdum@0
   901
				}
indvdum@0
   902
			}
indvdum@0
   903
		}
indvdum@0
   904
	}
indvdum@0
   905
indvdum@0
   906
	/**
indvdum@0
   907
	* Authentication plug-ins is largely down to Sergey Kanareykin, our thanks to him.
indvdum@0
   908
	*/
indvdum@0
   909
	function login($username, $password, $autologin = false, $viewonline = 1, $admin = 0)
indvdum@0
   910
	{
indvdum@0
   911
		global $config, $db, $user, $phpbb_root_path, $phpEx;
indvdum@0
   912
indvdum@0
   913
		$method = trim(basename($config['auth_method']));
indvdum@0
   914
		include_once($phpbb_root_path . 'includes/auth/auth_' . $method . '.' . $phpEx);
indvdum@0
   915
indvdum@0
   916
		$method = 'login_' . $method;
indvdum@0
   917
		if (function_exists($method))
indvdum@0
   918
		{
indvdum@0
   919
			$login = $method($username, $password, $user->ip, $user->browser, $user->forwarded_for);
indvdum@0
   920
indvdum@0
   921
			// If the auth module wants us to create an empty profile do so and then treat the status as LOGIN_SUCCESS
indvdum@0
   922
			if ($login['status'] == LOGIN_SUCCESS_CREATE_PROFILE)
indvdum@0
   923
			{
indvdum@0
   924
				// we are going to use the user_add function so include functions_user.php if it wasn't defined yet
indvdum@0
   925
				if (!function_exists('user_add'))
indvdum@0
   926
				{
indvdum@0
   927
					include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
indvdum@0
   928
				}
indvdum@0
   929
indvdum@0
   930
				user_add($login['user_row'], (isset($login['cp_data'])) ? $login['cp_data'] : false);
indvdum@0
   931
indvdum@0
   932
				$sql = 'SELECT user_id, username, user_password, user_passchg, user_email, user_type
indvdum@0
   933
					FROM ' . USERS_TABLE . "
indvdum@0
   934
					WHERE username_clean = '" . $db->sql_escape(utf8_clean_string($username)) . "'";
indvdum@0
   935
				$result = $db->sql_query($sql);
indvdum@0
   936
				$row = $db->sql_fetchrow($result);
indvdum@0
   937
				$db->sql_freeresult($result);
indvdum@0
   938
indvdum@0
   939
				if (!$row)
indvdum@0
   940
				{
indvdum@0
   941
					return array(
indvdum@0
   942
						'status'		=> LOGIN_ERROR_EXTERNAL_AUTH,
indvdum@0
   943
						'error_msg'		=> 'AUTH_NO_PROFILE_CREATED',
indvdum@0
   944
						'user_row'		=> array('user_id' => ANONYMOUS),
indvdum@0
   945
					);
indvdum@0
   946
				}
indvdum@0
   947
indvdum@0
   948
				$login = array(
indvdum@0
   949
					'status'	=> LOGIN_SUCCESS,
indvdum@0
   950
					'error_msg'	=> false,
indvdum@0
   951
					'user_row'	=> $row,
indvdum@0
   952
				);
indvdum@0
   953
			}
indvdum@0
   954
indvdum@0
   955
			// If login succeeded, we will log the user in... else we pass the login array through...
indvdum@0
   956
			if ($login['status'] == LOGIN_SUCCESS)
indvdum@0
   957
			{
indvdum@0
   958
				$old_session_id = $user->session_id;
indvdum@0
   959
indvdum@0
   960
				if ($admin)
indvdum@0
   961
				{
indvdum@0
   962
					global $SID, $_SID;
indvdum@0
   963
indvdum@0
   964
					$cookie_expire = time() - 31536000;
indvdum@0
   965
					$user->set_cookie('u', '', $cookie_expire);
indvdum@0
   966
					$user->set_cookie('sid', '', $cookie_expire);
indvdum@0
   967
					unset($cookie_expire);
indvdum@0
   968
indvdum@0
   969
					$SID = '?sid=';
indvdum@0
   970
					$user->session_id = $_SID = '';
indvdum@0
   971
				}
indvdum@0
   972
indvdum@0
   973
				$result = $user->session_create($login['user_row']['user_id'], $admin, $autologin, $viewonline);
indvdum@0
   974
indvdum@0
   975
				// Successful session creation
indvdum@0
   976
				if ($result === true)
indvdum@0
   977
				{
indvdum@0
   978
					// If admin re-authentication we remove the old session entry because a new one has been created...
indvdum@0
   979
					if ($admin)
indvdum@0
   980
					{
indvdum@0
   981
						// the login array is used because the user ids do not differ for re-authentication
indvdum@0
   982
						$sql = 'DELETE FROM ' . SESSIONS_TABLE . "
indvdum@0
   983
							WHERE session_id = '" . $db->sql_escape($old_session_id) . "'
indvdum@0
   984
							AND session_user_id = {$login['user_row']['user_id']}";
indvdum@0
   985
						$db->sql_query($sql);
indvdum@0
   986
					}
indvdum@0
   987
indvdum@0
   988
					return array(
indvdum@0
   989
						'status'		=> LOGIN_SUCCESS,
indvdum@0
   990
						'error_msg'		=> false,
indvdum@0
   991
						'user_row'		=> $login['user_row'],
indvdum@0
   992
					);
indvdum@0
   993
				}
indvdum@0
   994
indvdum@0
   995
				return array(
indvdum@0
   996
					'status'		=> LOGIN_BREAK,
indvdum@0
   997
					'error_msg'		=> $result,
indvdum@0
   998
					'user_row'		=> $login['user_row'],
indvdum@0
   999
				);
indvdum@0
  1000
			}
indvdum@0
  1001
indvdum@0
  1002
			return $login;
indvdum@0
  1003
		}
indvdum@0
  1004
indvdum@0
  1005
		trigger_error('Authentication method not found', E_USER_ERROR);
indvdum@0
  1006
	}
indvdum@0
  1007
indvdum@0
  1008
	/**
indvdum@0
  1009
	* Fill auth_option statement for later querying based on the supplied options
indvdum@0
  1010
	*/
indvdum@0
  1011
	function build_auth_option_statement($key, $auth_options, &$sql_opts)
indvdum@0
  1012
	{
indvdum@0
  1013
		global $db;
indvdum@0
  1014
indvdum@0
  1015
		if (!is_array($auth_options))
indvdum@0
  1016
		{
indvdum@0
  1017
			if (strpos($auth_options, '%') !== false)
indvdum@0
  1018
			{
indvdum@0
  1019
				$sql_opts = "AND $key " . $db->sql_like_expression(str_replace('%', $db->any_char, $auth_options));
indvdum@0
  1020
			}
indvdum@0
  1021
			else
indvdum@0
  1022
			{
indvdum@0
  1023
				$sql_opts = "AND $key = '" . $db->sql_escape($auth_options) . "'";
indvdum@0
  1024
			}
indvdum@0
  1025
		}
indvdum@0
  1026
		else
indvdum@0
  1027
		{
indvdum@0
  1028
			$is_like_expression = false;
indvdum@0
  1029
indvdum@0
  1030
			foreach ($auth_options as $option)
indvdum@0
  1031
			{
indvdum@0
  1032
				if (strpos($option, '%') !== false)
indvdum@0
  1033
				{
indvdum@0
  1034
					$is_like_expression = true;
indvdum@0
  1035
				}
indvdum@0
  1036
			}
indvdum@0
  1037
indvdum@0
  1038
			if (!$is_like_expression)
indvdum@0
  1039
			{
indvdum@0
  1040
				$sql_opts = 'AND ' . $db->sql_in_set($key, $auth_options);
indvdum@0
  1041
			}
indvdum@0
  1042
			else
indvdum@0
  1043
			{
indvdum@0
  1044
				$sql = array();
indvdum@0
  1045
indvdum@0
  1046
				foreach ($auth_options as $option)
indvdum@0
  1047
				{
indvdum@0
  1048
					if (strpos($option, '%') !== false)
indvdum@0
  1049
					{
indvdum@0
  1050
						$sql[] = $key . ' ' . $db->sql_like_expression(str_replace('%', $db->any_char, $option));
indvdum@0
  1051
					}
indvdum@0
  1052
					else
indvdum@0
  1053
					{
indvdum@0
  1054
						$sql[] = $key . " = '" . $db->sql_escape($option) . "'";
indvdum@0
  1055
					}
indvdum@0
  1056
				}
indvdum@0
  1057
indvdum@0
  1058
				$sql_opts = 'AND (' . implode(' OR ', $sql) . ')';
indvdum@0
  1059
			}
indvdum@0
  1060
		}
indvdum@0
  1061
	}
indvdum@0
  1062
}
indvdum@0
  1063
indvdum@0
  1064
?>