source: trunk/web/punbb/include/cache.php @ 6

Last change on this file since 6 was 6, checked in by dj3c1t, 12 years ago

mise a jour du trunk

File size: 10.2 KB
Line 
1<?php
2
3/**
4 * Copyright (C) 2008-2011 FluxBB
5 * based on code by Rickard Andersson copyright (C) 2002-2008 PunBB
6 * License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
7 */
8
9// Make sure no one attempts to run this script "directly"
10if (!defined('PUN'))
11        exit;
12
13
14//
15// Generate the config cache PHP script
16//
17function generate_config_cache()
18{
19        global $db;
20
21        // Get the forum config from the DB
22        $result = $db->query('SELECT * FROM '.$db->prefix.'config', true) or error('Unable to fetch forum config', __FILE__, __LINE__, $db->error());
23        while ($cur_config_item = $db->fetch_row($result))
24                $output[$cur_config_item[0]] = $cur_config_item[1];
25
26        // Output config as PHP code
27        $fh = @fopen(FORUM_CACHE_DIR.'cache_config.php', 'wb');
28        if (!$fh)
29                error('Unable to write configuration cache file to cache directory. Please make sure PHP has write access to the directory \''.pun_htmlspecialchars(FORUM_CACHE_DIR).'\'', __FILE__, __LINE__);
30
31        fwrite($fh, '<?php'."\n\n".'define(\'PUN_CONFIG_LOADED\', 1);'."\n\n".'$pun_config = '.var_export($output, true).';'."\n\n".'?>');
32
33        fclose($fh);
34
35        if (function_exists('apc_delete_file'))
36                @apc_delete_file(FORUM_CACHE_DIR.'cache_config.php');
37}
38
39
40//
41// Generate the bans cache PHP script
42//
43function generate_bans_cache()
44{
45        global $db;
46
47        // Get the ban list from the DB
48        $result = $db->query('SELECT * FROM '.$db->prefix.'bans', true) or error('Unable to fetch ban list', __FILE__, __LINE__, $db->error());
49
50        $output = array();
51        while ($cur_ban = $db->fetch_assoc($result))
52                $output[] = $cur_ban;
53
54        // Output ban list as PHP code
55        $fh = @fopen(FORUM_CACHE_DIR.'cache_bans.php', 'wb');
56        if (!$fh)
57                error('Unable to write bans cache file to cache directory. Please make sure PHP has write access to the directory \''.pun_htmlspecialchars(FORUM_CACHE_DIR).'\'', __FILE__, __LINE__);
58
59        fwrite($fh, '<?php'."\n\n".'define(\'PUN_BANS_LOADED\', 1);'."\n\n".'$pun_bans = '.var_export($output, true).';'."\n\n".'?>');
60
61        fclose($fh);
62
63        if (function_exists('apc_delete_file'))
64                @apc_delete_file(FORUM_CACHE_DIR.'cache_bans.php');
65}
66
67
68//
69// Generate the ranks cache PHP script
70//
71function generate_ranks_cache()
72{
73        global $db;
74
75        // Get the rank list from the DB
76        $result = $db->query('SELECT * FROM '.$db->prefix.'ranks ORDER BY min_posts', true) or error('Unable to fetch rank list', __FILE__, __LINE__, $db->error());
77
78        $output = array();
79        while ($cur_rank = $db->fetch_assoc($result))
80                $output[] = $cur_rank;
81
82        // Output ranks list as PHP code
83        $fh = @fopen(FORUM_CACHE_DIR.'cache_ranks.php', 'wb');
84        if (!$fh)
85                error('Unable to write ranks cache file to cache directory. Please make sure PHP has write access to the directory \''.pun_htmlspecialchars(FORUM_CACHE_DIR).'\'', __FILE__, __LINE__);
86
87        fwrite($fh, '<?php'."\n\n".'define(\'PUN_RANKS_LOADED\', 1);'."\n\n".'$pun_ranks = '.var_export($output, true).';'."\n\n".'?>');
88
89        fclose($fh);
90
91        if (function_exists('apc_delete_file'))
92                @apc_delete_file(FORUM_CACHE_DIR.'cache_ranks.php');
93}
94
95
96//
97// Generate quick jump cache PHP scripts
98//
99function generate_quickjump_cache($group_id = false)
100{
101        global $db, $lang_common, $pun_user;
102
103        $groups = array();
104
105        // If a group_id was supplied, we generate the quick jump cache for that group only
106        if ($group_id !== false)
107        {
108                // Is this group even allowed to read forums?
109                $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());
110                $read_board = $db->result($result);
111
112                $groups[$group_id] = $read_board;
113        }
114        else
115        {
116                // A group_id was not supplied, so we generate the quick jump cache for all groups
117                $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());
118                $num_groups = $db->num_rows($result);
119
120                while ($row = $db->fetch_row($result))
121                        $groups[$row[0]] = $row[1];
122        }
123
124        // Loop through the groups in $groups and output the cache for each of them
125        foreach ($groups as $group_id => $read_board)
126        {
127                // Output quick jump as PHP code
128                $fh = @fopen(FORUM_CACHE_DIR.'cache_quickjump_'.$group_id.'.php', 'wb');
129                if (!$fh)
130                        error('Unable to write quick jump cache file to cache directory. Please make sure PHP has write access to the directory \''.pun_htmlspecialchars(FORUM_CACHE_DIR).'\'', __FILE__, __LINE__);
131
132                $output = '<?php'."\n\n".'if (!defined(\'PUN\')) exit;'."\n".'define(\'PUN_QJ_LOADED\', 1);'."\n".'$forum_id = isset($forum_id) ? $forum_id : 0;'."\n\n".'?>';
133
134                if ($read_board == '1')
135                {
136                        $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());
137
138                        if ($db->num_rows($result))
139                        {
140                                $output .= "\t\t\t\t".'<form id="qjump" method="get" action="viewforum.php">'."\n\t\t\t\t\t".'<div><label><span><?php echo $lang_common[\'Jump to\'] ?>'.'<br /></span>'."\n\t\t\t\t\t".'<select name="id" onchange="window.location=(\'viewforum.php?id=\'+this.options[this.selectedIndex].value)">'."\n";
141
142                                $cur_category = 0;
143                                while ($cur_forum = $db->fetch_assoc($result))
144                                {
145                                        if ($cur_forum['cid'] != $cur_category) // A new category since last iteration?
146                                        {
147                                                if ($cur_category)
148                                                        $output .= "\t\t\t\t\t\t".'</optgroup>'."\n";
149
150                                                $output .= "\t\t\t\t\t\t".'<optgroup label="'.pun_htmlspecialchars($cur_forum['cat_name']).'">'."\n";
151                                                $cur_category = $cur_forum['cid'];
152                                        }
153
154                                        $redirect_tag = ($cur_forum['redirect_url'] != '') ? ' &gt;&gt;&gt;' : '';
155                                        $output .= "\t\t\t\t\t\t\t".'<option value="'.$cur_forum['fid'].'"<?php echo ($forum_id == '.$cur_forum['fid'].') ? \' selected="selected"\' : \'\' ?>>'.pun_htmlspecialchars($cur_forum['forum_name']).$redirect_tag.'</option>'."\n";
156                                }
157
158                                $output .= "\t\t\t\t\t\t".'</optgroup>'."\n\t\t\t\t\t".'</select>'."\n\t\t\t\t\t".'<input type="submit" value="<?php echo $lang_common[\'Go\'] ?>" accesskey="g" />'."\n\t\t\t\t\t".'</label></div>'."\n\t\t\t\t".'</form>'."\n";
159                        }
160                }
161
162                fwrite($fh, $output);
163
164                fclose($fh);
165
166                if (function_exists('apc_delete_file'))
167                        @apc_delete_file(FORUM_CACHE_DIR.'cache_quickjump_'.$group_id.'.php');
168        }
169}
170
171
172//
173// Generate the censoring cache PHP script
174//
175function generate_censoring_cache()
176{
177        global $db;
178
179        $result = $db->query('SELECT search_for, replace_with FROM '.$db->prefix.'censoring') or error('Unable to fetch censoring list', __FILE__, __LINE__, $db->error());
180        $num_words = $db->num_rows($result);
181
182        $search_for = $replace_with = array();
183        for ($i = 0; $i < $num_words; $i++)
184        {
185                list($search_for[$i], $replace_with[$i]) = $db->fetch_row($result);
186                $search_for[$i] = '%(?<=[^\p{L}\p{N}])('.str_replace('\*', '[\p{L}\p{N}]*?', preg_quote($search_for[$i], '%')).')(?=[^\p{L}\p{N}])%iu';
187        }
188
189        // Output censored words as PHP code
190        $fh = @fopen(FORUM_CACHE_DIR.'cache_censoring.php', 'wb');
191        if (!$fh)
192                error('Unable to write censoring cache file to cache directory. Please make sure PHP has write access to the directory \''.pun_htmlspecialchars(FORUM_CACHE_DIR).'\'', __FILE__, __LINE__);
193
194        fwrite($fh, '<?php'."\n\n".'define(\'PUN_CENSOR_LOADED\', 1);'."\n\n".'$search_for = '.var_export($search_for, true).';'."\n\n".'$replace_with = '.var_export($replace_with, true).';'."\n\n".'?>');
195
196        fclose($fh);
197
198        if (function_exists('apc_delete_file'))
199                @apc_delete_file(FORUM_CACHE_DIR.'cache_censoring.php');
200}
201
202
203//
204// Generate the stopwords cache PHP script
205//
206function generate_stopwords_cache()
207{
208        $stopwords = array();
209
210        $d = dir(PUN_ROOT.'lang');
211        while (($entry = $d->read()) !== false)
212        {
213                if ($entry{0} == '.')
214                        continue;
215
216                if (is_dir(PUN_ROOT.'lang/'.$entry) && file_exists(PUN_ROOT.'lang/'.$entry.'/stopwords.txt'))
217                        $stopwords = array_merge($stopwords, file(PUN_ROOT.'lang/'.$entry.'/stopwords.txt'));
218        }
219        $d->close();
220
221        // Tidy up and filter the stopwords
222        $stopwords = array_map('pun_trim', $stopwords);
223        $stopwords = array_filter($stopwords);
224
225        // Output stopwords as PHP code
226        $fh = @fopen(FORUM_CACHE_DIR.'cache_stopwords.php', 'wb');
227        if (!$fh)
228                error('Unable to write stopwords cache file to cache directory. Please make sure PHP has write access to the directory \''.pun_htmlspecialchars(FORUM_CACHE_DIR).'\'', __FILE__, __LINE__);
229
230        fwrite($fh, '<?php'."\n\n".'$cache_id = \''.generate_stopwords_cache_id().'\';'."\n".'if ($cache_id != generate_stopwords_cache_id()) return;'."\n\n".'define(\'PUN_STOPWORDS_LOADED\', 1);'."\n\n".'$stopwords = '.var_export($stopwords, true).';'."\n\n".'?>');
231
232        fclose($fh);
233
234        if (function_exists('apc_delete_file'))
235                @apc_delete_file(FORUM_CACHE_DIR.'cache_stopwords.php');
236}
237
238
239//
240// Load some information about the latest registered users
241//
242function generate_users_info_cache()
243{
244        global $db;
245
246        $stats = array();
247
248        $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());
249        $stats['total_users'] = $db->result($result);
250
251        $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());
252        $stats['last_user'] = $db->fetch_assoc($result);
253
254        // Output users info as PHP code
255        $fh = @fopen(FORUM_CACHE_DIR.'cache_users_info.php', 'wb');
256        if (!$fh)
257                error('Unable to write users info cache file to cache directory. Please make sure PHP has write access to the directory \''.pun_htmlspecialchars(FORUM_CACHE_DIR).'\'', __FILE__, __LINE__);
258
259        fwrite($fh, '<?php'."\n\n".'define(\'PUN_USERS_INFO_LOADED\', 1);'."\n\n".'$stats = '.var_export($stats, true).';'."\n\n".'?>');
260
261        fclose($fh);
262
263        if (function_exists('apc_delete_file'))
264                @apc_delete_file(FORUM_CACHE_DIR.'cache_users_info.php');
265}
266
267
268//
269// Delete all feed caches
270//
271function clear_feed_cache()
272{
273        $d = dir(FORUM_CACHE_DIR);
274        while (($entry = $d->read()) !== false)
275        {
276                if (substr($entry, 0, 10) == 'cache_feed' && substr($entry, -4) == '.php')
277                        @unlink(FORUM_CACHE_DIR.$entry);
278        }
279        $d->close();
280}
281
282
283define('FORUM_CACHE_FUNCTIONS_LOADED', true);
Note: See TracBrowser for help on using the repository browser.