includes/cache.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Sat, 10 Mar 2012 00:47:37 +0400
changeset 16 d6012dd9347f
parent 0 ceef6d64c21d
permissions -rw-r--r--
Removing old cubometry-style style
indvdum@0
     1
<?php
indvdum@0
     2
/**
indvdum@0
     3
*
indvdum@0
     4
* @package acm
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
* Class for grabbing/handling cached entries, extends acm_file or acm_db depending on the setup
indvdum@0
    21
* @package acm
indvdum@0
    22
*/
indvdum@0
    23
class cache extends acm
indvdum@0
    24
{
indvdum@0
    25
	/**
indvdum@0
    26
	* Get config values
indvdum@0
    27
	*/
indvdum@0
    28
	function obtain_config()
indvdum@0
    29
	{
indvdum@0
    30
		global $db;
indvdum@0
    31
indvdum@0
    32
		if (($config = $this->get('config')) !== false)
indvdum@0
    33
		{
indvdum@0
    34
			$sql = 'SELECT config_name, config_value
indvdum@0
    35
				FROM ' . CONFIG_TABLE . '
indvdum@0
    36
				WHERE is_dynamic = 1';
indvdum@0
    37
			$result = $db->sql_query($sql);
indvdum@0
    38
indvdum@0
    39
			while ($row = $db->sql_fetchrow($result))
indvdum@0
    40
			{
indvdum@0
    41
				$config[$row['config_name']] = $row['config_value'];
indvdum@0
    42
			}
indvdum@0
    43
			$db->sql_freeresult($result);
indvdum@0
    44
		}
indvdum@0
    45
		else
indvdum@0
    46
		{
indvdum@0
    47
			$config = $cached_config = array();
indvdum@0
    48
indvdum@0
    49
			$sql = 'SELECT config_name, config_value, is_dynamic
indvdum@0
    50
				FROM ' . CONFIG_TABLE;
indvdum@0
    51
			$result = $db->sql_query($sql);
indvdum@0
    52
indvdum@0
    53
			while ($row = $db->sql_fetchrow($result))
indvdum@0
    54
			{
indvdum@0
    55
				if (!$row['is_dynamic'])
indvdum@0
    56
				{
indvdum@0
    57
					$cached_config[$row['config_name']] = $row['config_value'];
indvdum@0
    58
				}
indvdum@0
    59
indvdum@0
    60
				$config[$row['config_name']] = $row['config_value'];
indvdum@0
    61
			}
indvdum@0
    62
			$db->sql_freeresult($result);
indvdum@0
    63
indvdum@0
    64
			$this->put('config', $cached_config);
indvdum@0
    65
		}
indvdum@0
    66
indvdum@0
    67
		return $config;
indvdum@0
    68
	}
indvdum@0
    69
indvdum@0
    70
	/**
indvdum@0
    71
	* Obtain list of naughty words and build preg style replacement arrays for use by the
indvdum@0
    72
	* calling script
indvdum@0
    73
	*/
indvdum@0
    74
	function obtain_word_list()
indvdum@0
    75
	{
indvdum@0
    76
		global $db;
indvdum@0
    77
indvdum@0
    78
		if (($censors = $this->get('_word_censors')) === false)
indvdum@0
    79
		{
indvdum@0
    80
			$sql = 'SELECT word, replacement
indvdum@0
    81
				FROM ' . WORDS_TABLE;
indvdum@0
    82
			$result = $db->sql_query($sql);
indvdum@0
    83
indvdum@0
    84
			$censors = array();
indvdum@0
    85
			while ($row = $db->sql_fetchrow($result))
indvdum@0
    86
			{
indvdum@0
    87
				$censors['match'][] = get_censor_preg_expression($row['word']);
indvdum@0
    88
				$censors['replace'][] = $row['replacement'];
indvdum@0
    89
			}
indvdum@0
    90
			$db->sql_freeresult($result);
indvdum@0
    91
indvdum@0
    92
			$this->put('_word_censors', $censors);
indvdum@0
    93
		}
indvdum@0
    94
indvdum@0
    95
		return $censors;
indvdum@0
    96
	}
indvdum@0
    97
indvdum@0
    98
	/**
indvdum@0
    99
	* Obtain currently listed icons
indvdum@0
   100
	*/
indvdum@0
   101
	function obtain_icons()
indvdum@0
   102
	{
indvdum@0
   103
		if (($icons = $this->get('_icons')) === false)
indvdum@0
   104
		{
indvdum@0
   105
			global $db;
indvdum@0
   106
indvdum@0
   107
			// Topic icons
indvdum@0
   108
			$sql = 'SELECT *
indvdum@0
   109
				FROM ' . ICONS_TABLE . '
indvdum@0
   110
				ORDER BY icons_order';
indvdum@0
   111
			$result = $db->sql_query($sql);
indvdum@0
   112
indvdum@0
   113
			$icons = array();
indvdum@0
   114
			while ($row = $db->sql_fetchrow($result))
indvdum@0
   115
			{
indvdum@0
   116
				$icons[$row['icons_id']]['img'] = $row['icons_url'];
indvdum@0
   117
				$icons[$row['icons_id']]['width'] = (int) $row['icons_width'];
indvdum@0
   118
				$icons[$row['icons_id']]['height'] = (int) $row['icons_height'];
indvdum@0
   119
				$icons[$row['icons_id']]['display'] = (bool) $row['display_on_posting'];
indvdum@0
   120
			}
indvdum@0
   121
			$db->sql_freeresult($result);
indvdum@0
   122
indvdum@0
   123
			$this->put('_icons', $icons);
indvdum@0
   124
		}
indvdum@0
   125
indvdum@0
   126
		return $icons;
indvdum@0
   127
	}
indvdum@0
   128
indvdum@0
   129
	/**
indvdum@0
   130
	* Obtain ranks
indvdum@0
   131
	*/
indvdum@0
   132
	function obtain_ranks()
indvdum@0
   133
	{
indvdum@0
   134
		if (($ranks = $this->get('_ranks')) === false)
indvdum@0
   135
		{
indvdum@0
   136
			global $db;
indvdum@0
   137
indvdum@0
   138
			$sql = 'SELECT *
indvdum@0
   139
				FROM ' . RANKS_TABLE . '
indvdum@0
   140
				ORDER BY rank_min DESC';
indvdum@0
   141
			$result = $db->sql_query($sql);
indvdum@0
   142
indvdum@0
   143
			$ranks = array();
indvdum@0
   144
			while ($row = $db->sql_fetchrow($result))
indvdum@0
   145
			{
indvdum@0
   146
				if ($row['rank_special'])
indvdum@0
   147
				{
indvdum@0
   148
					$ranks['special'][$row['rank_id']] = array(
indvdum@0
   149
						'rank_title'	=>	$row['rank_title'],
indvdum@0
   150
						'rank_image'	=>	$row['rank_image']
indvdum@0
   151
					);
indvdum@0
   152
				}
indvdum@0
   153
				else
indvdum@0
   154
				{
indvdum@0
   155
					$ranks['normal'][] = array(
indvdum@0
   156
						'rank_title'	=>	$row['rank_title'],
indvdum@0
   157
						'rank_min'		=>	$row['rank_min'],
indvdum@0
   158
						'rank_image'	=>	$row['rank_image']
indvdum@0
   159
					);
indvdum@0
   160
				}
indvdum@0
   161
			}
indvdum@0
   162
			$db->sql_freeresult($result);
indvdum@0
   163
indvdum@0
   164
			$this->put('_ranks', $ranks);
indvdum@0
   165
		}
indvdum@0
   166
indvdum@0
   167
		return $ranks;
indvdum@0
   168
	}
indvdum@0
   169
indvdum@0
   170
	/**
indvdum@0
   171
	* Obtain allowed extensions
indvdum@0
   172
	*
indvdum@0
   173
	* @param mixed $forum_id If false then check for private messaging, if int then check for forum id. If true, then only return extension informations.
indvdum@0
   174
	*
indvdum@0
   175
	* @return array allowed extensions array.
indvdum@0
   176
	*/
indvdum@0
   177
	function obtain_attach_extensions($forum_id)
indvdum@0
   178
	{
indvdum@0
   179
		if (($extensions = $this->get('_extensions')) === false)
indvdum@0
   180
		{
indvdum@0
   181
			global $db;
indvdum@0
   182
indvdum@0
   183
			$extensions = array(
indvdum@0
   184
				'_allowed_post'	=> array(),
indvdum@0
   185
				'_allowed_pm'	=> array(),
indvdum@0
   186
			);
indvdum@0
   187
indvdum@0
   188
			// The rule is to only allow those extensions defined. ;)
indvdum@0
   189
			$sql = 'SELECT e.extension, g.*
indvdum@0
   190
				FROM ' . EXTENSIONS_TABLE . ' e, ' . EXTENSION_GROUPS_TABLE . ' g
indvdum@0
   191
				WHERE e.group_id = g.group_id
indvdum@0
   192
					AND (g.allow_group = 1 OR g.allow_in_pm = 1)';
indvdum@0
   193
			$result = $db->sql_query($sql);
indvdum@0
   194
indvdum@0
   195
			while ($row = $db->sql_fetchrow($result))
indvdum@0
   196
			{
indvdum@0
   197
				$extension = strtolower(trim($row['extension']));
indvdum@0
   198
indvdum@0
   199
				$extensions[$extension] = array(
indvdum@0
   200
					'display_cat'	=> (int) $row['cat_id'],
indvdum@0
   201
					'download_mode'	=> (int) $row['download_mode'],
indvdum@0
   202
					'upload_icon'	=> trim($row['upload_icon']),
indvdum@0
   203
					'max_filesize'	=> (int) $row['max_filesize'],
indvdum@0
   204
					'allow_group'	=> $row['allow_group'],
indvdum@0
   205
					'allow_in_pm'	=> $row['allow_in_pm'],
indvdum@0
   206
				);
indvdum@0
   207
indvdum@0
   208
				$allowed_forums = ($row['allowed_forums']) ? unserialize(trim($row['allowed_forums'])) : array();
indvdum@0
   209
indvdum@0
   210
				// Store allowed extensions forum wise
indvdum@0
   211
				if ($row['allow_group'])
indvdum@0
   212
				{
indvdum@0
   213
					$extensions['_allowed_post'][$extension] = (!sizeof($allowed_forums)) ? 0 : $allowed_forums;
indvdum@0
   214
				}
indvdum@0
   215
indvdum@0
   216
				if ($row['allow_in_pm'])
indvdum@0
   217
				{
indvdum@0
   218
					$extensions['_allowed_pm'][$extension] = 0;
indvdum@0
   219
				}
indvdum@0
   220
			}
indvdum@0
   221
			$db->sql_freeresult($result);
indvdum@0
   222
indvdum@0
   223
			$this->put('_extensions', $extensions);
indvdum@0
   224
		}
indvdum@0
   225
indvdum@0
   226
		// Forum post
indvdum@0
   227
		if ($forum_id === false)
indvdum@0
   228
		{
indvdum@0
   229
			// We are checking for private messages, therefore we only need to get the pm extensions...
indvdum@0
   230
			$return = array('_allowed_' => array());
indvdum@0
   231
indvdum@0
   232
			foreach ($extensions['_allowed_pm'] as $extension => $check)
indvdum@0
   233
			{
indvdum@0
   234
				$return['_allowed_'][$extension] = 0;
indvdum@0
   235
				$return[$extension] = $extensions[$extension];
indvdum@0
   236
			}
indvdum@0
   237
indvdum@0
   238
			$extensions = $return;
indvdum@0
   239
		}
indvdum@0
   240
		else if ($forum_id === true)
indvdum@0
   241
		{
indvdum@0
   242
			return $extensions;
indvdum@0
   243
		}
indvdum@0
   244
		else
indvdum@0
   245
		{
indvdum@0
   246
			$forum_id = (int) $forum_id;
indvdum@0
   247
			$return = array('_allowed_' => array());
indvdum@0
   248
indvdum@0
   249
			foreach ($extensions['_allowed_post'] as $extension => $check)
indvdum@0
   250
			{
indvdum@0
   251
				// Check for allowed forums
indvdum@0
   252
				if (is_array($check))
indvdum@0
   253
				{
indvdum@0
   254
					$allowed = (!in_array($forum_id, $check)) ? false : true;
indvdum@0
   255
				}
indvdum@0
   256
				else
indvdum@0
   257
				{
indvdum@0
   258
					$allowed = true;
indvdum@0
   259
				}
indvdum@0
   260
indvdum@0
   261
				if ($allowed)
indvdum@0
   262
				{
indvdum@0
   263
					$return['_allowed_'][$extension] = 0;
indvdum@0
   264
					$return[$extension] = $extensions[$extension];
indvdum@0
   265
				}
indvdum@0
   266
			}
indvdum@0
   267
indvdum@0
   268
			$extensions = $return;
indvdum@0
   269
		}
indvdum@0
   270
indvdum@0
   271
		if (!isset($extensions['_allowed_']))
indvdum@0
   272
		{
indvdum@0
   273
			$extensions['_allowed_'] = array();
indvdum@0
   274
		}
indvdum@0
   275
indvdum@0
   276
		return $extensions;
indvdum@0
   277
	}
indvdum@0
   278
indvdum@0
   279
	/**
indvdum@0
   280
	* Obtain active bots
indvdum@0
   281
	*/
indvdum@0
   282
	function obtain_bots()
indvdum@0
   283
	{
indvdum@0
   284
		if (($bots = $this->get('_bots')) === false)
indvdum@0
   285
		{
indvdum@0
   286
			global $db;
indvdum@0
   287
indvdum@0
   288
			switch ($db->sql_layer)
indvdum@0
   289
			{
indvdum@0
   290
				case 'mssql':
indvdum@0
   291
				case 'mssql_odbc':
indvdum@0
   292
				case 'mssqlnative':
indvdum@0
   293
					$sql = 'SELECT user_id, bot_agent, bot_ip
indvdum@0
   294
						FROM ' . BOTS_TABLE . '
indvdum@0
   295
						WHERE bot_active = 1
indvdum@0
   296
					ORDER BY LEN(bot_agent) DESC';
indvdum@0
   297
				break;
indvdum@0
   298
indvdum@0
   299
				case 'firebird':
indvdum@0
   300
					$sql = 'SELECT user_id, bot_agent, bot_ip
indvdum@0
   301
						FROM ' . BOTS_TABLE . '
indvdum@0
   302
						WHERE bot_active = 1
indvdum@0
   303
					ORDER BY CHAR_LENGTH(bot_agent) DESC';
indvdum@0
   304
				break;
indvdum@0
   305
indvdum@0
   306
				// LENGTH supported by MySQL, IBM DB2 and Oracle for sure...
indvdum@0
   307
				default:
indvdum@0
   308
					$sql = 'SELECT user_id, bot_agent, bot_ip
indvdum@0
   309
						FROM ' . BOTS_TABLE . '
indvdum@0
   310
						WHERE bot_active = 1
indvdum@0
   311
					ORDER BY LENGTH(bot_agent) DESC';
indvdum@0
   312
				break;
indvdum@0
   313
			}
indvdum@0
   314
			$result = $db->sql_query($sql);
indvdum@0
   315
indvdum@0
   316
			$bots = array();
indvdum@0
   317
			while ($row = $db->sql_fetchrow($result))
indvdum@0
   318
			{
indvdum@0
   319
				$bots[] = $row;
indvdum@0
   320
			}
indvdum@0
   321
			$db->sql_freeresult($result);
indvdum@0
   322
indvdum@0
   323
			$this->put('_bots', $bots);
indvdum@0
   324
		}
indvdum@0
   325
indvdum@0
   326
		return $bots;
indvdum@0
   327
	}
indvdum@0
   328
indvdum@0
   329
	/**
indvdum@0
   330
	* Obtain cfg file data
indvdum@0
   331
	*/
indvdum@0
   332
	function obtain_cfg_items($theme)
indvdum@0
   333
	{
indvdum@0
   334
		global $config, $phpbb_root_path;
indvdum@0
   335
indvdum@0
   336
		$parsed_items = array(
indvdum@0
   337
			'theme'		=> array(),
indvdum@0
   338
			'template'	=> array(),
indvdum@0
   339
			'imageset'	=> array()
indvdum@0
   340
		);
indvdum@0
   341
indvdum@0
   342
		foreach ($parsed_items as $key => $parsed_array)
indvdum@0
   343
		{
indvdum@0
   344
			$parsed_array = $this->get('_cfg_' . $key . '_' . $theme[$key . '_path']);
indvdum@0
   345
indvdum@0
   346
			if ($parsed_array === false)
indvdum@0
   347
			{
indvdum@0
   348
				$parsed_array = array();
indvdum@0
   349
			}
indvdum@0
   350
indvdum@0
   351
			$reparse = false;
indvdum@0
   352
			$filename = $phpbb_root_path . 'styles/' . $theme[$key . '_path'] . '/' . $key . '/' . $key . '.cfg';
indvdum@0
   353
indvdum@0
   354
			if (!file_exists($filename))
indvdum@0
   355
			{
indvdum@0
   356
				continue;
indvdum@0
   357
			}
indvdum@0
   358
indvdum@0
   359
			if (!isset($parsed_array['filetime']) || (($config['load_tplcompile'] && @filemtime($filename) > $parsed_array['filetime'])))
indvdum@0
   360
			{
indvdum@0
   361
				$reparse = true;
indvdum@0
   362
			}
indvdum@0
   363
indvdum@0
   364
			// Re-parse cfg file
indvdum@0
   365
			if ($reparse)
indvdum@0
   366
			{
indvdum@0
   367
				$parsed_array = parse_cfg_file($filename);
indvdum@0
   368
				$parsed_array['filetime'] = @filemtime($filename);
indvdum@0
   369
indvdum@0
   370
				$this->put('_cfg_' . $key . '_' . $theme[$key . '_path'], $parsed_array);
indvdum@0
   371
			}
indvdum@0
   372
			$parsed_items[$key] = $parsed_array;
indvdum@0
   373
		}
indvdum@0
   374
indvdum@0
   375
		return $parsed_items;
indvdum@0
   376
	}
indvdum@0
   377
indvdum@0
   378
	/**
indvdum@0
   379
	* Obtain disallowed usernames
indvdum@0
   380
	*/
indvdum@0
   381
	function obtain_disallowed_usernames()
indvdum@0
   382
	{
indvdum@0
   383
		if (($usernames = $this->get('_disallowed_usernames')) === false)
indvdum@0
   384
		{
indvdum@0
   385
			global $db;
indvdum@0
   386
indvdum@0
   387
			$sql = 'SELECT disallow_username
indvdum@0
   388
				FROM ' . DISALLOW_TABLE;
indvdum@0
   389
			$result = $db->sql_query($sql);
indvdum@0
   390
indvdum@0
   391
			$usernames = array();
indvdum@0
   392
			while ($row = $db->sql_fetchrow($result))
indvdum@0
   393
			{
indvdum@0
   394
				$usernames[] = str_replace('%', '.*?', preg_quote(utf8_clean_string($row['disallow_username']), '#'));
indvdum@0
   395
			}
indvdum@0
   396
			$db->sql_freeresult($result);
indvdum@0
   397
indvdum@0
   398
			$this->put('_disallowed_usernames', $usernames);
indvdum@0
   399
		}
indvdum@0
   400
indvdum@0
   401
		return $usernames;
indvdum@0
   402
	}
indvdum@0
   403
indvdum@0
   404
	/**
indvdum@0
   405
	* Obtain hooks...
indvdum@0
   406
	*/
indvdum@0
   407
	function obtain_hooks()
indvdum@0
   408
	{
indvdum@0
   409
		global $phpbb_root_path, $phpEx;
indvdum@0
   410
indvdum@0
   411
		if (($hook_files = $this->get('_hooks')) === false)
indvdum@0
   412
		{
indvdum@0
   413
			$hook_files = array();
indvdum@0
   414
indvdum@0
   415
			// Now search for hooks...
indvdum@0
   416
			$dh = @opendir($phpbb_root_path . 'includes/hooks/');
indvdum@0
   417
indvdum@0
   418
			if ($dh)
indvdum@0
   419
			{
indvdum@0
   420
				while (($file = readdir($dh)) !== false)
indvdum@0
   421
				{
indvdum@0
   422
					if (strpos($file, 'hook_') === 0 && substr($file, -(strlen($phpEx) + 1)) === '.' . $phpEx)
indvdum@0
   423
					{
indvdum@0
   424
						$hook_files[] = substr($file, 0, -(strlen($phpEx) + 1));
indvdum@0
   425
					}
indvdum@0
   426
				}
indvdum@0
   427
				closedir($dh);
indvdum@0
   428
			}
indvdum@0
   429
indvdum@0
   430
			$this->put('_hooks', $hook_files);
indvdum@0
   431
		}
indvdum@0
   432
indvdum@0
   433
		return $hook_files;
indvdum@0
   434
	}
indvdum@0
   435
}
indvdum@0
   436
indvdum@0
   437
?>