Ignore:
Timestamp:
Nov 14, 2011, 11:17:15 PM (12 years ago)
Author:
dj3c1t
Message:

passage a Fluxbb 1.4.7

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/rsr.v5.1.dev/web/punbb/include/cache.php

    r1 r3  
    11<?php
    2 /***********************************************************************
    3 
    4   Copyright (C) 2002-2005  Rickard Andersson (rickard@punbb.org)
    5 
    6   This file is part of PunBB.
    7 
    8   PunBB is free software; you can redistribute it and/or modify it
    9   under the terms of the GNU General Public License as published
    10   by the Free Software Foundation; either version 2 of the License,
    11   or (at your option) any later version.
    12 
    13   PunBB is distributed in the hope that it will be useful, but
    14   WITHOUT ANY WARRANTY; without even the implied warranty of
    15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16   GNU General Public License for more details.
    17 
    18   You should have received a copy of the GNU General Public License
    19   along with this program; if not, write to the Free Software
    20   Foundation, Inc., 59 Temple Place, Suite 330, Boston,
    21   MA  02111-1307  USA
    22 
    23 ************************************************************************/
    24 
     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 */
    258
    269// Make sure no one attempts to run this script "directly"
     
    3013
    3114//
    32 // If we are running pre PHP 4.2.0, we add our own implementation of var_export
    33 //
    34 if (!function_exists('var_export'))
    35 {
    36         function var_export()
    37         {
    38                 $args = func_get_args();
    39                 $indent = (isset($args[2])) ? $args[2] : '';
    40 
    41                 if (is_array($args[0]))
    42                 {
    43                         $output = 'array ('."\n";
    44 
    45                         foreach ($args[0] as $k => $v)
    46                         {
    47                                 if (is_numeric($k))
    48                                         $output .= $indent.'  '.$k.' => ';
    49                                 else
    50                                         $output .= $indent.'  \''.str_replace('\'', '\\\'', str_replace('\\', '\\\\', $k)).'\' => ';
    51 
    52                                 if (is_array($v))
    53                                         $output .= var_export($v, true, $indent.'  ');
    54                                 else
    55                                 {
    56                                         if (gettype($v) != 'string' && !empty($v))
    57                                                 $output .= $v.','."\n";
    58                                         else
    59                                                 $output .= '\''.str_replace('\'', '\\\'', str_replace('\\', '\\\\', $v)).'\','."\n";
    60                                 }
    61                         }
    62 
    63                         $output .= ($indent != '') ? $indent.'),'."\n" : ')';
    64                 }
    65                 else
    66                         $output = $args[0];
    67 
    68                 if ($args[1] == true)
    69                         return $output;
    70                 else
    71                         echo $output;
    72         }
    73 }
    74 
    75 
    76 //
    7715// Generate the config cache PHP script
    7816//
     
    8220
    8321        // Get the forum config from the DB
    84         $result = $db->query('SELECT * FROM '.$db->prefix.'config', true) or error('Impossible de retrouver la configuration des forums', __FILE__, __LINE__, $db->error());
     22        $result = $db->query('SELECT * FROM '.$db->prefix.'config', true) or error('Unable to fetch forum config', __FILE__, __LINE__, $db->error());
    8523        while ($cur_config_item = $db->fetch_row($result))
    8624                $output[$cur_config_item[0]] = $cur_config_item[1];
    8725
    8826        // Output config as PHP code
    89         $fh = @fopen(PUN_ROOT.'cache/cache_config.php', 'wb');
    90         if (!$fh)
    91                 error('Impossible d\'écrire le fichier cache de configuration dans le répertoire cache. Veuillez vous assurer que PHP a l\'accÚs en écrire au répertoire \'cache\'', __FILE__, __LINE__);
     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__);
    9230
    9331        fwrite($fh, '<?php'."\n\n".'define(\'PUN_CONFIG_LOADED\', 1);'."\n\n".'$pun_config = '.var_export($output, true).';'."\n\n".'?>');
    9432
    9533        fclose($fh);
     34
     35        if (function_exists('apc_delete_file'))
     36                @apc_delete_file(FORUM_CACHE_DIR.'cache_config.php');
    9637}
    9738
     
    10546
    10647        // Get the ban list from the DB
    107         $result = $db->query('SELECT * FROM '.$db->prefix.'bans', true) or error('Impossible de retrouver la liste de bannissements', __FILE__, __LINE__, $db->error());
     48        $result = $db->query('SELECT * FROM '.$db->prefix.'bans', true) or error('Unable to fetch ban list', __FILE__, __LINE__, $db->error());
    10849
    10950        $output = array();
     
    11253
    11354        // Output ban list as PHP code
    114         $fh = @fopen(PUN_ROOT.'cache/cache_bans.php', 'wb');
    115         if (!$fh)
    116                 error('Impossible d\'écrire le fichier cache de bannissements dans le répertoire cache. Veuillez vous assurer que PHP a l\'accÚs en écrire au répertoire \'cache\'', __FILE__, __LINE__);
     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__);
    11758
    11859        fwrite($fh, '<?php'."\n\n".'define(\'PUN_BANS_LOADED\', 1);'."\n\n".'$pun_bans = '.var_export($output, true).';'."\n\n".'?>');
    11960
    12061        fclose($fh);
     62
     63        if (function_exists('apc_delete_file'))
     64                @apc_delete_file(FORUM_CACHE_DIR.'cache_bans.php');
    12165}
    12266
     
    13074
    13175        // Get the rank list from the DB
    132         $result = $db->query('SELECT * FROM '.$db->prefix.'ranks ORDER BY min_posts', true) or error('Impossible de retrouver la liste des rangs utilisateurs', __FILE__, __LINE__, $db->error());
     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());
    13377
    13478        $output = array();
     
    13781
    13882        // Output ranks list as PHP code
    139         $fh = @fopen(PUN_ROOT.'cache/cache_ranks.php', 'wb');
    140         if (!$fh)
    141                 error('Impossible d\'écrire le fichier de cache de rangs utilisateurs dans le répertoire cache. Veuillez vous assurer que PHP a l\'accÚs en écrire au répertoire \'cache\'', __FILE__, __LINE__);
     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__);
    14286
    14387        fwrite($fh, '<?php'."\n\n".'define(\'PUN_RANKS_LOADED\', 1);'."\n\n".'$pun_ranks = '.var_export($output, true).';'."\n\n".'?>');
    14488
    14589        fclose($fh);
    146 }
    147 
    148 
    149 //
    150 // Generate quickjump cache PHP scripts
     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
    15198//
    15299function generate_quickjump_cache($group_id = false)
     
    154101        global $db, $lang_common, $pun_user;
    155102
    156         // If a group_id was supplied, we generate the quickjump cache for that group only
     103        $groups = array();
     104
     105        // If a group_id was supplied, we generate the quick jump cache for that group only
    157106        if ($group_id !== false)
    158                 $groups[0] = $group_id;
     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        }
    159114        else
    160115        {
    161                 // A group_id was now supplied, so we generate the quickjump cache for all groups
    162                 $result = $db->query('SELECT g_id FROM '.$db->prefix.'groups') or error('Impossible de retrouver la liste des groupes utilisateurs', __FILE__, __LINE__, $db->error());
     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());
    163118                $num_groups = $db->num_rows($result);
    164119
    165                 for ($i = 0; $i < $num_groups; ++$i)
    166                         $groups[] = $db->result($result, $i);
     120                while ($row = $db->fetch_row($result))
     121                        $groups[$row[0]] = $row[1];
    167122        }
    168123
    169124        // Loop through the groups in $groups and output the cache for each of them
    170         while (list(, $group_id) = @each($groups))
    171         {
    172                 // Output quickjump as PHP code
    173                 $fh = @fopen(PUN_ROOT.'cache/cache_quickjump_'.$group_id.'.php', 'wb');
     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');
    174129                if (!$fh)
    175                         error('Impossible d\'écrire le fichier cache de saut rapide de forums dans le répertoire cache. Veuillez vous assurer que PHP a l\'accÚs en écrire au répertoire \'cache\'', __FILE__, __LINE__);
    176 
    177                 $output = '<?php'."\n\n".'if (!defined(\'PUN\')) exit;'."\n".'define(\'PUN_QJ_LOADED\', 1);'."\n\n".'?>';
    178                 $output .= "\t\t\t\t".'<form id="qjump" method="get" action="viewforum.php">'."\n\t\t\t\t\t".'<div><label><?php echo $lang_common[\'Jump to\'] ?>'."\n\n\t\t\t\t\t".'<br /><select name="id" onchange="window.location=(\'viewforum.php?id=\'+this.options[this.selectedIndex].value)">'."\n";
    179 
    180 
    181                 $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', true) or error('Impossible de retrouver la liste des catégories et des forums', __FILE__, __LINE__, $db->error());
    182 
    183                 $cur_category = 0;
    184                 while ($cur_forum = $db->fetch_assoc($result))
     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')
    185135                {
    186                         if ($cur_forum['cid'] != $cur_category) // A new category since last iteration?
     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))
    187139                        {
    188                                 if ($cur_category)
    189                                         $output .= "\t\t\t\t\t\t".'</optgroup>'."\n";
    190 
    191                                 $output .= "\t\t\t\t\t\t".'<optgroup label="'.pun_htmlspecialchars($cur_forum['cat_name']).'">'."\n";
    192                                 $cur_category = $cur_forum['cid'];
     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";
    193159                        }
    194 
    195                         $redirect_tag = ($cur_forum['redirect_url'] != '') ? ' &gt;&gt;&gt;' : '';
    196                         $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";
    197160                }
    198161
    199                 $output .= "\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";
    200 
    201162                fwrite($fh, $output);
    202163
    203164                fclose($fh);
    204         }
    205 }
     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 TracChangeset for help on using the changeset viewer.