diff options
Diffstat (limited to 'phpBB/includes')
-rw-r--r-- | phpBB/includes/cron.php | 135 | ||||
-rw-r--r-- | phpBB/includes/cron/standard.php | 163 | ||||
-rw-r--r-- | phpBB/includes/cron_lock.php | 71 | ||||
-rw-r--r-- | phpBB/includes/functions.php | 36 |
4 files changed, 373 insertions, 32 deletions
diff --git a/phpBB/includes/cron.php b/phpBB/includes/cron.php new file mode 100644 index 0000000000..2aa22858b7 --- /dev/null +++ b/phpBB/includes/cron.php @@ -0,0 +1,135 @@ +<?php +/** +* +* @package phpBB3 +* @version $Id$ +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ + exit; +} + +/** +* Cron class +* @package phpBB3 +*/ +class cron +{ + var $tasks = array(); + + function cron() { + global $config, $phpbb_root_path, $phpEx; + $modules = $config['cron_modules']; + $modules = explode(',', $modules); + foreach ($modules as $module) { + // explode will return array("") when exploding an empty string; + // users may also specify something like foo,,bar. + // Account for module being possibly empty + if (!empty($module)) { + // Misspelling or specifying nonexistent modules here may make the board + // unusable due to error messages screwing up header output + include_once($phpbb_root_path . "includes/cron/$module.$phpEx"); + $cron_class = "cron_tasks_$module"; + $object = new $cron_class; + foreach ($object->tasks as $cron_type => $params) { + $params['object'] = $object; + $this->tasks[$cron_type] = $params; + } + } + } + } + + function is_valid_task($cron_type) { + return isset($this->tasks[$cron_type]); + } + + function is_task_runnable($cron_type, $args=null) { + global $config; + $time_now = time(); + $cron_params = $this->tasks[$cron_type]; + if ($cron_params['enable_config'] && !$config[$cron_params['enable_config']]) { + return false; + } + if ($cron_param['custom_condition']) { + $callable = array($cron_params['object'], $cron_type . '_condition'); + if ($args) { + $answer = call_user_func_array($callable, $args); + } else { + $answer = call_user_func($callable); + } + if (!$answer) { + return false; + } + } + if ($time_now - $config[$cron_params['interval_config']] > $config[$cron_params['last_run_config']]) { + return true; + } + return false; + } + + function is_task_shutdown_function_compatible($cron_type) { + $cron_params = $this->tasks[$cron_type]; + if (isset($cron_params['shutdown_function_condition'])) { + return call_user_func(array($cron_params->object, $cron_type . '_shutdown_function_condition')); + } else { + return true; + } + } + + function determine_cron_mode_param() { + global $config; + if ($config['use_system_cron']) { + $mode = 'run_from_system'; + } else { + $mode_param = 'run_from_phpbb'; + } + return $mode_param; + } + + function find_one_runnable_task() { + $mode_param = $this->determine_cron_mode_param(); + foreach ($this->tasks as $cron_type => $cron_params) { + if ($cron_params[$mode_param] && $this->is_task_runnable($cron_type)) { + return $cron_type; + } + } + return null; + } + + function find_all_runnable_tasks() { + $mode_param = $this->determine_cron_mode_param(); + $tasks = array(); + foreach ($this->tasks as $cron_type => $cron_params) { + if ($cron_params[$mode_param] && $this->is_task_runnable($cron_type)) { + $tasks[] = $cron_type; + } + } + return $tasks; + } + + function generate_task_code($cron_type, $args=array()) { + $cron_params = $this->tasks[$cron_type]; + if ($cron_params['custom_code']) { + $code = call_user_func_array(array($cron_params['object'], $cron_type . '_code'), $args); + } else { + $code = $this->generate_generic_task_code($cron_type); + } + return $code; + } + + function generate_generic_task_code($cron_type) { + global $phpbb_root_path, $phpEx; + return '<img src="' . append_sid($phpbb_root_path . 'cron.' . $phpEx, 'cron_type=' . $cron_type) . '" width="1" height="1" alt="cron" />'; + } + + function run_task($cron_type) { + call_user_func(array($this->tasks[$cron_type]['object'], 'run_' . $cron_type)); + } +} diff --git a/phpBB/includes/cron/standard.php b/phpBB/includes/cron/standard.php new file mode 100644 index 0000000000..1cb8738f17 --- /dev/null +++ b/phpBB/includes/cron/standard.php @@ -0,0 +1,163 @@ +<?php +/** +* +* @package phpBB3 +* @version $Id$ +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ + exit; +} + +/** +* Standard cron tasks +* @package phpBB3 +*/ +class cron_tasks_standard +{ + var $tasks = array( + // key: cron type + // values: config name for enable/disable flag, + // whether to check condition function to determine if the task can/should be run, + // config name for interval, + // config name for last run time, + // whether task should be considered in phpbb cron mode, + // whether task should be considered in system cron mode, + // whether task requires special code generation + 'prune_all_forums' => array( + 'custom_condition' => true, + 'run_from_system' => true, + ), + 'prune_forum' => array( + 'custom_condition' => true, + 'custom_code' => true, + ), + 'queue' => array( + 'custom_condition' => true, + 'interval_config' => 'queue_interval_config', + 'last_run_config' => 'last_queue_run', + 'run_from_phpbb' => true, + 'run_from_system' => true, + 'shutdown_function_condition' => true, + ), + 'tidy_cache' => array( + 'custom_condition' => true, + 'interval_config' => 'cache_gc', + 'last_run_config' => 'cache_last_gc', + 'run_from_phpbb' => true, + 'run_from_system' => true, + ), + 'tidy_database' => array( + 'interval_config' => 'database_gc', + 'last_run_config' => 'database_last_gc', + 'run_from_phpbb' => true, + 'run_from_system' => true, + ), + 'tidy_search' => array( + 'interval_config' => 'search_gc', + 'last_run_config' => 'search_last_gc', + 'run_from_phpbb' => true, + 'run_from_system' => true, + ), + 'tidy_sessions' => array( + 'interval_config' => 'session_gc', + 'last_run_config' => 'session_last_gc', + 'run_from_phpbb' => true, + 'run_from_system' => true, + ), + 'tidy_warnings' => array( + 'enable_config' => 'warnings_expire_days', + 'interval_config' => 'warnings_gc', + 'last_run_config' => 'warnings_last_gc', + 'run_from_phpbb' => true, + 'run_from_system' => true, + ), + ); + + function prune_forum_condition($forum_data) { + return $forum_data['enable_prune'] && $forum_data['prune_next'] < time(); + } + + function prune_forum_code($forum_id) { + global $phpbb_root_path, $phpEx; + return '<img src="' . append_sid($phpbb_root_path . 'cron.' . $phpEx, 'cron_type=prune_forum&f=' . $forum_id) . '" alt="cron" width="1" height="1" />'; + } + + function run_prune_forum() { + } + + function queue_condition() { + global $phpbb_root_path, $phpEx; + return file_exists($phpbb_root_path . 'cache/queue.' . $phpEx); + } + + function queue_shutdown_function_condition() { + global $config; + return !$config['smtp_delivery']; + } + + function run_queue() { + global $phpbb_root_path, $phpEx; + include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx); + $queue = new queue(); + $queue->process(); + } + + function tidy_cache_condition() { + global $cache; + return method_exists($cache, 'tidy'); + } + + function run_tidy_cache() { + global $cache; + $cache->tidy(); + } + + function run_tidy_database() { + include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx); + tidy_database(); + } + + function tidy_search_condition() { + global $phpbb_root_path, $phpEx, $config; + + // Select the search method + $search_type = basename($config['search_type']); + + return file_exists($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx); + } + + function run_tidy_search() { + global $phpbb_root_path, $phpEx, $config, $error; + + // Select the search method + $search_type = basename($config['search_type']); + + include_once("{$phpbb_root_path}includes/search/$search_type.$phpEx"); + + // We do some additional checks in the module to ensure it can actually be utilised + $error = false; + $search = new $search_type($error); + + if (!$error) { + $search->tidy(); + } + } + + function run_tidy_sessions() { + global $user; + $user->session_gc(); + } + + function run_tidy_warnings() { + include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx); + tidy_warnings(); + } +} diff --git a/phpBB/includes/cron_lock.php b/phpBB/includes/cron_lock.php new file mode 100644 index 0000000000..2a09590772 --- /dev/null +++ b/phpBB/includes/cron_lock.php @@ -0,0 +1,71 @@ +<?php +/** +* +* @package phpBB3 +* @version $Id$ +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ + exit; +} + +/** +* Cron lock class +* @package phpBB3 +*/ +class cron_lock +{ + function lock() { + global $config, $db; + + if (!isset($config['cron_lock'])) + { + set_config('cron_lock', '0', true); + } + + // make sure cron doesn't run multiple times in parallel + if ($config['cron_lock']) + { + // if the other process is running more than an hour already we have to assume it + // aborted without cleaning the lock + $time = explode(' ', $config['cron_lock']); + $time = $time[0]; + + if ($time + 3600 >= time()) + { + return false; + } + } + + define('CRON_ID', time() . ' ' . unique_id()); + + $sql = 'UPDATE ' . CONFIG_TABLE . " + SET config_value = '" . $db->sql_escape(CRON_ID) . "' + WHERE config_name = 'cron_lock' AND config_value = '" . $db->sql_escape($config['cron_lock']) . "'"; + $db->sql_query($sql); + + // another cron process altered the table between script start and UPDATE query so exit + if ($db->sql_affectedrows() != 1) + { + return false; + } + + return true; + } + + function unlock() { + global $db; + + $sql = 'UPDATE ' . CONFIG_TABLE . " + SET config_value = '0' + WHERE config_name = 'cron_lock' AND config_value = '" . $db->sql_escape(CRON_ID) . "'"; + $db->sql_query($sql); + } +} diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 056d578e75..eb787bfc62 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -4595,7 +4595,7 @@ function page_footer($run_cron = true) // Call cron-type script $call_cron = false; - if (!defined('IN_CRON') && $run_cron && !$config['board_disable']) + if (!defined('IN_CRON') && !$config['use_system_cron'] && $run_cron && !$config['board_disable']) { $call_cron = true; $time_now = (!empty($user->time_now) && is_int($user->time_now)) ? $user->time_now : time(); @@ -4616,40 +4616,12 @@ function page_footer($run_cron = true) // Call cron job? if ($call_cron) { - $cron_type = ''; - - if ($time_now - $config['queue_interval'] > $config['last_queue_run'] && !defined('IN_ADMIN') && file_exists($phpbb_root_path . 'cache/queue.' . $phpEx)) - { - // Process email queue - $cron_type = 'queue'; - } - else if (method_exists($cache, 'tidy') && $time_now - $config['cache_gc'] > $config['cache_last_gc']) - { - // Tidy the cache - $cron_type = 'tidy_cache'; - } - else if ($config['warnings_expire_days'] && ($time_now - $config['warnings_gc'] > $config['warnings_last_gc'])) - { - $cron_type = 'tidy_warnings'; - } - else if ($time_now - $config['database_gc'] > $config['database_last_gc']) - { - // Tidy the database - $cron_type = 'tidy_database'; - } - else if ($time_now - $config['search_gc'] > $config['search_last_gc']) - { - // Tidy the search - $cron_type = 'tidy_search'; - } - else if ($time_now - $config['session_gc'] > $config['session_last_gc']) - { - $cron_type = 'tidy_sessions'; - } + global $cron; + $cron_type = $cron->find_one_runnable_task(); if ($cron_type) { - $template->assign_var('RUN_CRON_TASK', '<img src="' . append_sid($phpbb_root_path . 'cron.' . $phpEx, 'cron_type=' . $cron_type) . '" width="1" height="1" alt="cron" />'); + $template->assign_var('RUN_CRON_TASK', $cron->generate_task_code($cron_type)); } } |