From 3b06ee0d381dc1be5f40ca98ad4278046d869d21 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Sun, 17 Nov 2019 20:57:39 +0100 Subject: checked in initial customized verison for Archlinux32 --- include/cache.php | 263 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 263 insertions(+) create mode 100644 include/cache.php (limited to 'include/cache.php') diff --git a/include/cache.php b/include/cache.php new file mode 100644 index 0000000..c1947ae --- /dev/null +++ b/include/cache.php @@ -0,0 +1,263 @@ +query('SELECT * FROM '.$db->prefix.'config', true) or error('Unable to fetch forum config', __FILE__, __LINE__, $db->error()); + + $output = array(); + while ($cur_config_item = $db->fetch_row($result)) + $output[$cur_config_item[0]] = $cur_config_item[1]; + + // Output config as PHP code + $content = ''; + fluxbb_write_cache_file('cache_config.php', $content); +} + + +// +// Generate the bans cache PHP script +// +function generate_bans_cache() +{ + global $db; + + // Get the ban list from the DB + $result = $db->query('SELECT * FROM '.$db->prefix.'bans', true) or error('Unable to fetch ban list', __FILE__, __LINE__, $db->error()); + + $output = array(); + while ($cur_ban = $db->fetch_assoc($result)) + $output[] = $cur_ban; + + // Output ban list as PHP code + $content = ''; + fluxbb_write_cache_file('cache_bans.php', $content); +} + + +// +// Generate quick jump cache PHP scripts +// +function generate_quickjump_cache($group_id = false) +{ + global $db, $lang_common; + + $groups = array(); + + // If a group_id was supplied, we generate the quick jump cache for that group only + if ($group_id !== false) + { + // Is this group even allowed to read forums? + $result = $db->query('SELECT g_read_board FROM '.$db->prefix.'groups WHERE g_id='.$group_id) or error('Unable to fetch user group read permission', __FILE__, __LINE__, $db->error()); + $read_board = $db->result($result); + + $groups[$group_id] = $read_board; + } + else + { + // A group_id was not supplied, so we generate the quick jump cache for all groups + $result = $db->query('SELECT g_id, g_read_board FROM '.$db->prefix.'groups') or error('Unable to fetch user group list', __FILE__, __LINE__, $db->error()); + + while ($row = $db->fetch_row($result)) + $groups[$row[0]] = $row[1]; + } + + // Loop through the groups in $groups and output the cache for each of them + foreach ($groups as $group_id => $read_board) + { + // Output quick jump as PHP code + $output = ''; + + if ($read_board == '1') + { + $result = $db->query('SELECT c.id AS cid, c.cat_name, f.id AS fid, f.forum_name, f.redirect_url FROM '.$db->prefix.'categories AS c INNER JOIN '.$db->prefix.'forums AS f ON c.id=f.cat_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$group_id.') WHERE fp.read_forum IS NULL OR fp.read_forum=1 ORDER BY c.disp_position, c.id, f.disp_position') or error('Unable to fetch category/forum list', __FILE__, __LINE__, $db->error()); + + if ($db->num_rows($result)) + { + $output .= "\t\t\t\t".'
'."\n\t\t\t\t\t".'
'."\n\t\t\t\t\t".''."\n\t\t\t\t\t".'
'."\n\t\t\t\t".'
'."\n"; + } + } + + fluxbb_write_cache_file('cache_quickjump_'.$group_id.'.php', $output); + } +} + + +// +// Generate the censoring cache PHP script +// +function generate_censoring_cache() +{ + global $db; + + $result = $db->query('SELECT search_for, replace_with FROM '.$db->prefix.'censoring') or error('Unable to fetch censoring list', __FILE__, __LINE__, $db->error()); + $num_words = $db->num_rows($result); + + $search_for = $replace_with = array(); + for ($i = 0; $i < $num_words; $i++) + { + list($search_for[$i], $replace_with[$i]) = $db->fetch_row($result); + $search_for[$i] = '%(?<=[^\p{L}\p{N}])('.str_replace('\*', '[\p{L}\p{N}]*?', preg_quote($search_for[$i], '%')).')(?=[^\p{L}\p{N}])%iu'; + } + + // Output censored words as PHP code + $content = ''; + fluxbb_write_cache_file('cache_censoring.php', $content); +} + + +// +// Generate the stopwords cache PHP script +// +function generate_stopwords_cache() +{ + $stopwords = array(); + + $d = dir(PUN_ROOT.'lang'); + while (($entry = $d->read()) !== false) + { + if ($entry{0} == '.') + continue; + + if (is_dir(PUN_ROOT.'lang/'.$entry) && file_exists(PUN_ROOT.'lang/'.$entry.'/stopwords.txt')) + $stopwords = array_merge($stopwords, file(PUN_ROOT.'lang/'.$entry.'/stopwords.txt')); + } + $d->close(); + + // Tidy up and filter the stopwords + $stopwords = array_map('pun_trim', $stopwords); + $stopwords = array_filter($stopwords); + + // Output stopwords as PHP code + $content = ''; + fluxbb_write_cache_file('cache_stopwords.php', $content); +} + + +// +// Load some information about the latest registered users +// +function generate_users_info_cache() +{ + global $db; + + $stats = array(); + + $result = $db->query('SELECT COUNT(id)-1 FROM '.$db->prefix.'users WHERE group_id!='.PUN_UNVERIFIED) or error('Unable to fetch total user count', __FILE__, __LINE__, $db->error()); + $stats['total_users'] = $db->result($result); + + $result = $db->query('SELECT id, username FROM '.$db->prefix.'users WHERE group_id!='.PUN_UNVERIFIED.' ORDER BY registered DESC LIMIT 1') or error('Unable to fetch newest registered user', __FILE__, __LINE__, $db->error()); + $stats['last_user'] = $db->fetch_assoc($result); + + // Output users info as PHP code + $content = ''; + fluxbb_write_cache_file('cache_users_info.php', $content); +} + + +// +// Generate the admins cache PHP script +// +function generate_admins_cache() +{ + global $db; + + // Get admins from the DB + $result = $db->query('SELECT id FROM '.$db->prefix.'users WHERE group_id='.PUN_ADMIN) or error('Unable to fetch users info', __FILE__, __LINE__, $db->error()); + + $output = array(); + while ($row = $db->fetch_row($result)) + $output[] = $row[0]; + + // Output admin list as PHP code + $content = ''; + fluxbb_write_cache_file('cache_admins.php', $content); +} + + +// +// Safely write out a cache file. +// +function fluxbb_write_cache_file($file, $content) +{ + $fh = @fopen(FORUM_CACHE_DIR.$file, 'wb'); + if (!$fh) + error('Unable to write cache file '.pun_htmlspecialchars($file).' to cache directory. Please make sure PHP has write access to the directory \''.pun_htmlspecialchars(FORUM_CACHE_DIR).'\'', __FILE__, __LINE__); + + flock($fh, LOCK_EX); + ftruncate($fh, 0); + + fwrite($fh, $content); + + flock($fh, LOCK_UN); + fclose($fh); + + fluxbb_invalidate_cached_file(FORUM_CACHE_DIR.$file); +} + + +// +// Delete all feed caches +// +function clear_feed_cache() +{ + $d = dir(FORUM_CACHE_DIR); + while (($entry = $d->read()) !== false) + { + if (substr($entry, 0, 10) == 'cache_feed' && substr($entry, -4) == '.php') + { + @unlink(FORUM_CACHE_DIR.$entry); + fluxbb_invalidate_cached_file(FORUM_CACHE_DIR.$entry); + } + } + $d->close(); +} + + +// +// Invalidate updated php files that are cached by an opcache +// +function fluxbb_invalidate_cached_file($file) +{ + if (function_exists('opcache_invalidate')) + opcache_invalidate($file, true); + elseif (function_exists('apc_delete_file')) + @apc_delete_file($file); +} + + +define('FORUM_CACHE_FUNCTIONS_LOADED', true); -- cgit v1.2.3-54-g00ecf