aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/includes
diff options
context:
space:
mode:
Diffstat (limited to 'phpBB/includes')
-rw-r--r--phpBB/includes/acm/cache_file.php244
-rw-r--r--phpBB/includes/functions.php47
-rw-r--r--phpBB/includes/page_header.php14
-rw-r--r--phpBB/includes/page_tail.php2
-rw-r--r--phpBB/includes/session.php9
5 files changed, 288 insertions, 28 deletions
diff --git a/phpBB/includes/acm/cache_file.php b/phpBB/includes/acm/cache_file.php
new file mode 100644
index 0000000000..a840274e01
--- /dev/null
+++ b/phpBB/includes/acm/cache_file.php
@@ -0,0 +1,244 @@
+<?php
+/***************************************************************************
+ * cache_file.php
+ * -------------------
+ * begin : Saturday, Feb 13, 2001
+ * copyright : (C) 2001 The phpBB Group
+ * email : support@phpbb.com
+ *
+ * $Id$
+ *
+ ***************************************************************************/
+
+/***************************************************************************
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ ***************************************************************************/
+
+//
+// This class is part of the Advanced Cache Manager
+//
+
+class acm
+{
+ var $vars = '';
+ var $vars_ts = array();
+ var $modified = FALSE;
+
+ var $sql_rowset = array();
+ var $sql_rowset_index = array();
+
+ function acm()
+ {
+ //$this->load_cache();
+ $this->cache_dir = $phpbb_root_path . 'cache/';
+ }
+
+ function tidy($expire_time = 0)
+ {
+ global $phpEx;
+
+ $dir = opendir($this->cache_dir);
+ while ($entry = readdir($dir))
+ {
+ if ($entry{0} == '.')
+ {
+ continue;
+ }
+ if (!$expire_time || time() - $expire_time >= filemtime($this->cache_dir . $entry))
+ {
+ unlink($this->cache_dir . $entry);
+ }
+ }
+ if (file_exists($this->cache_dir . 'global.' . $phpEx))
+ {
+ foreach ($this->vars_ts as $varname => $timestamp)
+ {
+ if (time() - $expire_time >= $timestamp)
+ {
+ $this->destroy($varname);
+ }
+ }
+ }
+ else
+ {
+ $this->vars = $this->vars_ts = array();
+ $this->modified = TRUE;
+ }
+ }
+
+ function save($varname, $var)
+ {
+ $this->vars[$varname] = $var;
+ $this->vars_ts[$varname] = time();
+ $this->modified = TRUE;
+ }
+
+ function destroy($varname)
+ {
+ if (isset($this->vars[$varname]))
+ {
+ $this->modified = TRUE;
+ unset($this->vars[$varname]);
+ unset($this->vars_ts[$varname]);
+ }
+ }
+
+ function load($varname, $expire_time = 0)
+ {
+ if (!is_array($this->vars))
+ {
+ $this->load_cache();
+ }
+ if (isset($this->vars[$varname]))
+ {
+ if ($expire_time && time() - $this->vars_ts[$varname] > $expire_time)
+ {
+ $this->destroy($varname);
+ return null;
+ }
+ return $this->vars[$varname];
+ }
+
+ return null;
+ }
+
+ function exists($varname, $expire_time = 0)
+ {
+ if (!is_array($this->vars))
+ {
+ $this->load_cache();
+ }
+
+ if ($expire_time > 0)
+ {
+ if (!isset($this->vars[$varname]))
+ {
+ return FALSE;
+ }
+ if ($this->vars_ts[$varname] <= time() - $expire_time)
+ {
+ $this->destroy($varname);
+ return FALSE;
+ }
+ }
+
+ return isset($this->vars[$varname]);
+ }
+
+ function load_cache()
+ {
+ global $phpEx;
+
+ $this->vars = array();
+ @include($this->cache_dir . 'global.' . $phpEx);
+ }
+
+ function save_cache()
+ {
+ if (!$this->modified)
+ {
+ return;
+ }
+
+ global $phpEx;
+ $file = '<?php $this->vars=' . $this->format_array($this->vars) . ";\n\$this->vars_ts=" . $this->format_array($this->vars_ts) . ' ?>';
+
+ $fp = fopen($this->cache_dir . 'global.' . $phpEx, 'wb');
+ fwrite($fp, $file);
+ fclose($fp);
+ }
+
+ function format_array($array)
+ {
+ $lines = array();
+ foreach ($array as $k => $v)
+ {
+ if (is_array($v))
+ {
+ $lines[] = "'$k'=>" . $this->format_array($v);
+ }
+ elseif (is_int($v))
+ {
+ $lines[] = "'$k'=>$v";
+ }
+ elseif (is_bool($v))
+ {
+ $lines[] = "'$k'=>" . (($v) ? 'TRUE' : 'FALSE');
+ }
+ else
+ {
+ $lines[] = "'$k'=>'" . str_replace('\\\\', '\\\\\\\\', str_replace("'", "\'", $v)) . "'";
+ }
+ }
+ return 'array(' . implode(',', $lines) . ')';
+ }
+
+ function sql_load($query)
+ {
+ global $db, $phpEx;
+ if (!file_exists($this->cache_dir . md5($query) . '.' . $phpEx))
+ {
+ return false;
+ }
+
+ include($this->cache_dir . md5($query) . '.' . $phpEx);
+
+ $query_id = 'Cache id #' . count($this->sql_rowset);
+ $this->sql_rowset[$query_id] = $rowset;
+ $this->sql_rowset_index[$query_id] = 0;
+ $db->query_result = $query_id;
+
+ return true;
+ }
+
+ function sql_save($query, $result)
+ {
+ global $db, $phpEx;
+ $fp = fopen($this->cache_dir . md5($query) . '.' . $phpEx, 'wb');
+
+ $lines = array();
+ $query_id = 'Cache id #' . count($this->sql_rowset);
+ $this->sql_rowset[$query_id] = array();
+ $this->sql_rowset_index[$query_id] = 0;
+ $db->query_result = $query_id;
+
+ while ($row = $db->sql_fetchrow($result))
+ {
+ $this->sql_rowset[$query_id][] = $row;
+
+ $line = 'array(';
+ foreach ($row as $key => $val)
+ {
+ $line .= "'$key'=>'" . str_replace('\\\\', '\\\\\\\\', str_replace("'", "\'", $val)) . "',";
+ }
+ $lines[] = substr($line, 0, -1) . ')';
+ }
+
+ fwrite($fp, "<?php\n\n/*\n$query\n*/\n\n\$rowset = array(" . implode(',', $lines) . ') ?>');
+ fclose($fp);
+ }
+
+ function sql_exists($query_id)
+ {
+ return isset($this->sql_rowset[$query_id]);
+ }
+
+ function sql_fetchrow($query_id)
+ {
+ if (!isset($this->sql_rowset[$query_id][$this->sql_rowset_index[$query_id]]))
+ {
+ return false;
+ }
+
+ $row = $this->sql_rowset[$query_id][$this->sql_rowset_index[$query_id]];
+ ++$this->sql_rowset_index[$query_id];
+
+ return $row;
+ }
+}
+?> \ No newline at end of file
diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php
index ec8301ae88..8689ca7b2a 100644
--- a/phpBB/includes/functions.php
+++ b/phpBB/includes/functions.php
@@ -29,6 +29,18 @@ function sql_quote($msg)
return str_replace("\'", "''", $msg);
}
+function set_config($config_name, $config_value)
+{
+ global $db, $cache, $config;
+ $config[$config_name] = $config_value;
+ $cache->save('config', $config);
+
+ $sql = 'UPDATE ' . CONFIG_TABLE . "
+ SET config_value = '" . sql_escape($config_value) . "'
+ WHERE config_name = '$config_name'";
+ $db->sql_query($sql);
+}
+
function get_userdata($user)
{
global $db;
@@ -140,7 +152,7 @@ function generate_forum_nav(&$forum_data)
// Obtain list of moderators of each forum
function get_moderators(&$forum_moderators, $forum_id = false)
{
- global $SID, $db, $acl_options, $phpEx;
+ global $cache, $SID, $db, $acl_options, $phpEx;
if (!empty($forum_id) && is_array($forum_id))
{
@@ -189,7 +201,9 @@ function make_jumpbox($action, $forum_id = false)
$sql = 'SELECT forum_id, forum_name, forum_postable, left_id, right_id
FROM ' . FORUMS_TABLE . '
ORDER BY left_id ASC';
- $result = $db->sql_query($sql);
+
+ // Cache the forums list for 60 seconds
+ $result = $db->sql_query($sql, 60);
$right = $cat_right = 0;
$padding = $forum_list = $holding = '';
@@ -646,16 +660,27 @@ function on_page($num_items, $per_page, $start)
// to return both sets of arrays
function obtain_word_list(&$orig_word, &$replacement_word)
{
- global $db;
+ global $db, $cache;
+ if ($cache->exists('word_censors'))
+ {
+ $words = $cache->load('word_censors');
+ $orig_word = $words['orig'];
+ $replacement_word = $words['replacement'];
+ }
+ else
+ {
+ $sql = "SELECT word, replacement
+ FROM " . WORDS_TABLE;
+ $result = $db->sql_query($sql);
- $sql = "SELECT word, replacement
- FROM " . WORDS_TABLE;
- $result = $db->sql_query($sql);
+ while ($row = $db->sql_fetchrow($result))
+ {
+ $orig_word[] = '#\b(' . str_replace('\*', '\w*?', preg_quote($row['word'], '#')) . ')\b#i';
+ $replacement_word[] = $row['replacement'];
+ }
- while ($row = $db->sql_fetchrow($result))
- {
- $orig_word[] = '#\b(' . str_replace('\*', '\w*?', preg_quote($row['word'], '#')) . ')\b#i';
- $replacement_word[] = $row['replacement'];
+ $words = array('orig' => $orig_word, 'replacement' => $replacement_word);
+ $cache->save('word_censors', $words);
}
return true;
@@ -833,7 +858,7 @@ function validate_optional_fields(&$icq, &$aim, &$msnm, &$yim, &$website, &$loca
// Error and message handler, call with trigger_error if reqd
function msg_handler($errno, $msg_text, $errfile, $errline)
{
- global $db, $auth, $template, $config, $user, $nav_links;
+ global $cache, $db, $auth, $template, $config, $user, $nav_links;
global $phpEx, $phpbb_root_path, $starttime;
switch ($errno)
diff --git a/phpBB/includes/page_header.php b/phpBB/includes/page_header.php
index 55504d505b..a4806eaf4d 100644
--- a/phpBB/includes/page_header.php
+++ b/phpBB/includes/page_header.php
@@ -139,18 +139,8 @@ $total_online_users = $logged_visible_online + $logged_hidden_online + $guests_o
if ($total_online_users > $config['record_online_users'])
{
- $config['record_online_users'] = $total_online_users;
- $config['record_online_date'] = time();
-
- $sql = "UPDATE " . CONFIG_TABLE . "
- SET config_value = '$total_online_users'
- WHERE config_name = 'record_online_users'";
- $db->sql_query($sql);
-
- $sql = "UPDATE " . CONFIG_TABLE . "
- SET config_value = '" . $config['record_online_date'] . "'
- WHERE config_name = 'record_online_date'";
- $db->sql_query($sql);
+ set_config('record_online_users', $total_online_users);
+ set_config('record_online_date', time());
}
if ($total_online_users == 0)
diff --git a/phpBB/includes/page_tail.php b/phpBB/includes/page_tail.php
index 32de512e4f..9c7f03a330 100644
--- a/phpBB/includes/page_tail.php
+++ b/phpBB/includes/page_tail.php
@@ -51,6 +51,8 @@ $template->assign_vars(array(
'DEBUG_OUTPUT' => (defined('DEBUG')) ? $debug_output : ''
));
+
+$cache->save_cache();
$template->display('body');
exit;
diff --git a/phpBB/includes/session.php b/phpBB/includes/session.php
index b87a10b725..d642b79bd0 100644
--- a/phpBB/includes/session.php
+++ b/phpBB/includes/session.php
@@ -326,10 +326,7 @@ class session
{
// Less than 5 sessions, update gc timer ... else we want gc
// called again to delete other sessions
- $sql = "UPDATE " . CONFIG_TABLE . "
- SET config_value = '$current_time'
- WHERE config_name = 'session_last_gc'";
- $db->sql_query($sql);
+ set_config('session_last_gc', $current_time);
}
return;
@@ -442,7 +439,9 @@ class user extends session
AND t.template_id = s.template_id
AND c.theme_id = s.style_id
AND i.imageset_id = s.imageset_id";
- $result = $db->sql_query($sql);
+
+ // Cache this query for 60 seconds
+ $result = $db->sql_query($sql, 60);
if (!($this->theme = $db->sql_fetchrow($result)))
{