cron.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/cron.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,215 @@
     1.4 +<?php
     1.5 +/**
     1.6 +*
     1.7 +* @package phpBB3
     1.8 +* @version $Id$
     1.9 +* @copyright (c) 2005 phpBB Group
    1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
    1.11 +*
    1.12 +*/
    1.13 +
    1.14 +/**
    1.15 +*/
    1.16 +define('IN_PHPBB', true);
    1.17 +define('IN_CRON', true);
    1.18 +$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
    1.19 +$phpEx = substr(strrchr(__FILE__, '.'), 1);
    1.20 +include($phpbb_root_path . 'common.' . $phpEx);
    1.21 +
    1.22 +// Do not update users last page entry
    1.23 +$user->session_begin(false);
    1.24 +$auth->acl($user->data);
    1.25 +
    1.26 +$cron_type = request_var('cron_type', '');
    1.27 +
    1.28 +// Output transparent gif
    1.29 +header('Cache-Control: no-cache');
    1.30 +header('Content-type: image/gif');
    1.31 +header('Content-length: 43');
    1.32 +
    1.33 +echo base64_decode('R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==');
    1.34 +
    1.35 +// Flush here to prevent browser from showing the page as loading while running cron.
    1.36 +flush();
    1.37 +
    1.38 +if (!isset($config['cron_lock']))
    1.39 +{
    1.40 +	set_config('cron_lock', '0', true);
    1.41 +}
    1.42 +
    1.43 +// make sure cron doesn't run multiple times in parallel
    1.44 +if ($config['cron_lock'])
    1.45 +{
    1.46 +	// if the other process is running more than an hour already we have to assume it
    1.47 +	// aborted without cleaning the lock
    1.48 +	$time = explode(' ', $config['cron_lock']);
    1.49 +	$time = $time[0];
    1.50 +
    1.51 +	if ($time + 3600 >= time())
    1.52 +	{
    1.53 +		exit;
    1.54 +	}
    1.55 +}
    1.56 +
    1.57 +define('CRON_ID', time() . ' ' . unique_id());
    1.58 +
    1.59 +$sql = 'UPDATE ' . CONFIG_TABLE . "
    1.60 +	SET config_value = '" . $db->sql_escape(CRON_ID) . "'
    1.61 +	WHERE config_name = 'cron_lock' AND config_value = '" . $db->sql_escape($config['cron_lock']) . "'";
    1.62 +$db->sql_query($sql);
    1.63 +
    1.64 +// another cron process altered the table between script start and UPDATE query so exit
    1.65 +if ($db->sql_affectedrows() != 1)
    1.66 +{
    1.67 +	exit;
    1.68 +}
    1.69 +
    1.70 +/**
    1.71 +* Run cron-like action
    1.72 +* Real cron-based layer will be introduced in 3.2
    1.73 +*/
    1.74 +switch ($cron_type)
    1.75 +{
    1.76 +	case 'queue':
    1.77 +
    1.78 +		if (time() - $config['queue_interval'] <= $config['last_queue_run'] || !file_exists($phpbb_root_path . 'cache/queue.' . $phpEx))
    1.79 +		{
    1.80 +			break;
    1.81 +		}
    1.82 +
    1.83 +		include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
    1.84 +		$queue = new queue();
    1.85 +
    1.86 +		$queue->process();
    1.87 +
    1.88 +	break;
    1.89 +
    1.90 +	case 'tidy_cache':
    1.91 +
    1.92 +		if (time() - $config['cache_gc'] <= $config['cache_last_gc'] || !method_exists($cache, 'tidy'))
    1.93 +		{
    1.94 +			break;
    1.95 +		}
    1.96 +
    1.97 +		$cache->tidy();
    1.98 +
    1.99 +	break;
   1.100 +
   1.101 +	case 'tidy_search':
   1.102 +		
   1.103 +		// Select the search method
   1.104 +		$search_type = basename($config['search_type']);
   1.105 +
   1.106 +		if (time() - $config['search_gc'] <= $config['search_last_gc'] || !file_exists($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx))
   1.107 +		{
   1.108 +			break;
   1.109 +		}
   1.110 +
   1.111 +		include_once("{$phpbb_root_path}includes/search/$search_type.$phpEx");
   1.112 +
   1.113 +		// We do some additional checks in the module to ensure it can actually be utilised
   1.114 +		$error = false;
   1.115 +		$search = new $search_type($error);
   1.116 +
   1.117 +		if ($error)
   1.118 +		{
   1.119 +			break;
   1.120 +		}
   1.121 +
   1.122 +		$search->tidy();
   1.123 +
   1.124 +	break;
   1.125 +
   1.126 +	case 'tidy_warnings':
   1.127 +
   1.128 +		if (time() - $config['warnings_gc'] <= $config['warnings_last_gc'])
   1.129 +		{
   1.130 +			break;
   1.131 +		}
   1.132 +
   1.133 +		include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
   1.134 +
   1.135 +		tidy_warnings();
   1.136 +
   1.137 +	break;
   1.138 +
   1.139 +	case 'tidy_database':
   1.140 +
   1.141 +		if (time() - $config['database_gc'] <= $config['database_last_gc'])
   1.142 +		{
   1.143 +			break;
   1.144 +		}
   1.145 +
   1.146 +		include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
   1.147 +
   1.148 +		tidy_database();
   1.149 +
   1.150 +	break;
   1.151 +
   1.152 +	case 'tidy_sessions':
   1.153 +
   1.154 +		if (time() - $config['session_gc'] <= $config['session_last_gc'])
   1.155 +		{
   1.156 +			break;
   1.157 +		}
   1.158 +
   1.159 +		$user->session_gc();
   1.160 +
   1.161 +	break;
   1.162 +
   1.163 +	case 'prune_forum':
   1.164 +
   1.165 +		$forum_id = request_var('f', 0);
   1.166 +
   1.167 +		$sql = 'SELECT forum_id, prune_next, enable_prune, prune_days, prune_viewed, forum_flags, prune_freq
   1.168 +			FROM ' . FORUMS_TABLE . "
   1.169 +			WHERE forum_id = $forum_id";
   1.170 +		$result = $db->sql_query($sql);
   1.171 +		$row = $db->sql_fetchrow($result);
   1.172 +		$db->sql_freeresult($result);
   1.173 +
   1.174 +		if (!$row)
   1.175 +		{
   1.176 +			break;
   1.177 +		}
   1.178 +
   1.179 +		// Do the forum Prune thang
   1.180 +		if ($row['prune_next'] < time() && $row['enable_prune'])
   1.181 +		{
   1.182 +			include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
   1.183 +
   1.184 +			if ($row['prune_days'])
   1.185 +			{
   1.186 +				auto_prune($row['forum_id'], 'posted', $row['forum_flags'], $row['prune_days'], $row['prune_freq']);
   1.187 +			}
   1.188 +
   1.189 +			if ($row['prune_viewed'])
   1.190 +			{
   1.191 +				auto_prune($row['forum_id'], 'viewed', $row['forum_flags'], $row['prune_viewed'], $row['prune_freq']);
   1.192 +			}
   1.193 +		}
   1.194 +
   1.195 +	break;
   1.196 +}
   1.197 +
   1.198 +// Unloading cache and closing db after having done the dirty work.
   1.199 +unlock_cron();
   1.200 +garbage_collection();
   1.201 +
   1.202 +exit;
   1.203 +
   1.204 +
   1.205 +/**
   1.206 +* Unlock cron script
   1.207 +*/
   1.208 +function unlock_cron()
   1.209 +{
   1.210 +	global $db;
   1.211 +
   1.212 +	$sql = 'UPDATE ' . CONFIG_TABLE . "
   1.213 +		SET config_value = '0'
   1.214 +		WHERE config_name = 'cron_lock' AND config_value = '" . $db->sql_escape(CRON_ID) . "'";
   1.215 +	$db->sql_query($sql);
   1.216 +}
   1.217 +
   1.218 +?>
   1.219 \ No newline at end of file