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