erm, revert last commit and add relevant comment. ;) This actually allows us to do...
[phpbb.git] / phpBB / cron.php
blobb892084a55952b61c10dd879a2bf79ed772c2d00
1 <?php
2 /**
4 * @package phpBB3
5 * @version $Id$
6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
9 */
11 /**
13 define('IN_PHPBB', true);
14 define('IN_CRON', true);
15 if (!defined('PHPBB_ROOT_PATH')) define('PHPBB_ROOT_PATH', './');
16 if (!defined('PHP_EXT')) define('PHP_EXT', substr(strrchr(__FILE__, '.'), 1));
17 include(PHPBB_ROOT_PATH . 'common.' . PHP_EXT);
19 // Do not update users last page entry
20 $user->session_begin(false);
21 $auth->acl($user->data);
23 $cron_type = request_var('cron_type', '');
24 $use_shutdown_function = (@function_exists('register_shutdown_function')) ? true : false;
26 // Output transparent gif
27 header('Cache-Control: no-cache');
28 header('Content-type: image/gif');
29 header('Content-length: 43');
31 echo base64_decode('R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==');
33 // test without flush ;)
34 // flush();
37 if (!isset(phpbb::$config['cron_lock']))
39 set_config('cron_lock', '0', true);
42 // make sure cron doesn't run multiple times in parallel
43 if (phpbb::$config['cron_lock'])
45 // if the other process is running more than an hour already we have to assume it
46 // aborted without cleaning the lock
47 $time = explode(' ', phpbb::$config['cron_lock']);
48 $time = $time[0];
50 if ($time + 3600 >= time())
52 exit;
56 define('CRON_ID', time() . ' ' . unique_id());
58 $sql = 'UPDATE ' . CONFIG_TABLE . "
59 SET config_value = '" . $db->sql_escape(CRON_ID) . "'
60 WHERE config_name = 'cron_lock' AND config_value = '" . $db->sql_escape(phpbb::$config['cron_lock']) . "'";
61 $db->sql_query($sql);
63 // another cron process altered the table between script start and UPDATE query so exit
64 if ($db->sql_affectedrows() != 1)
66 exit;
69 /**
70 * Run cron-like action
71 * Real cron-based layer will be introduced in 3.2
73 switch ($cron_type)
75 case 'queue':
77 if (time() - phpbb::$config['queue_interval'] <= phpbb::$config['last_queue_run'] || !file_exists(PHPBB_ROOT_PATH . 'cache/queue.' . PHP_EXT))
79 break;
82 // A user reported using the mail() function while using shutdown does not work. We do not want to risk that.
83 if ($use_shutdown_function && !phpbb::$config['smtp_delivery'])
85 $use_shutdown_function = false;
88 include_once(PHPBB_ROOT_PATH . 'includes/functions_messenger.' . PHP_EXT);
89 $queue = new queue();
91 if ($use_shutdown_function)
93 register_shutdown_function(array(&$queue, 'process'));
95 else
97 $queue->process();
100 break;
102 case 'tidy_cache':
104 if (time() - phpbb::$config['cache_gc'] <= phpbb::$config['cache_last_gc'] || !method_exists(phpbb::$acm, 'tidy'))
106 break;
109 if ($use_shutdown_function)
111 register_shutdown_function(array(&phpbb::$acm, 'tidy'));
113 else
115 phpbb::$acm->tidy();
118 break;
120 case 'tidy_search':
122 // Select the search method
123 $search_type = basename(phpbb::$config['search_type']);
125 if (time() - phpbb::$config['search_gc'] <= phpbb::$config['search_last_gc'] || !file_exists(PHPBB_ROOT_PATH . 'includes/search/' . $search_type . '.' . PHP_EXT))
127 break;
130 include_once(PHPBB_ROOT_PATH . "includes/search/$search_type." . PHP_EXT);
132 // We do some additional checks in the module to ensure it can actually be utilised
133 $error = false;
134 $search = new $search_type($error);
136 if ($error)
138 break;
141 if ($use_shutdown_function)
143 register_shutdown_function(array(&$search, 'tidy'));
145 else
147 $search->tidy();
150 break;
152 case 'tidy_warnings':
154 if (time() - phpbb::$config['warnings_gc'] <= phpbb::$config['warnings_last_gc'])
156 break;
159 include_once(PHPBB_ROOT_PATH . 'includes/functions_admin.' . PHP_EXT);
161 if ($use_shutdown_function)
163 register_shutdown_function('tidy_warnings');
165 else
167 tidy_warnings();
170 break;
172 case 'tidy_database':
174 if (time() - phpbb::$config['database_gc'] <= phpbb::$config['database_last_gc'])
176 break;
179 include_once(PHPBB_ROOT_PATH . 'includes/functions_admin.' . PHP_EXT);
181 if ($use_shutdown_function)
183 register_shutdown_function('tidy_database');
185 else
187 tidy_database();
190 break;
192 case 'tidy_sessions':
194 if (time() - phpbb::$config['session_gc'] <= phpbb::$config['session_last_gc'])
196 break;
199 if ($use_shutdown_function)
201 register_shutdown_function(array(&$user, 'session_gc'));
203 else
205 $user->session_gc();
208 break;
210 case 'prune_forum':
212 $forum_id = request_var('f', 0);
214 $sql = 'SELECT forum_id, prune_next, enable_prune, prune_days, prune_viewed, forum_flags, prune_freq
215 FROM ' . FORUMS_TABLE . "
216 WHERE forum_id = $forum_id";
217 $result = $db->sql_query($sql);
218 $row = $db->sql_fetchrow($result);
219 $db->sql_freeresult($result);
221 if (!$row)
223 break;
226 // Do the forum Prune thang
227 if ($row['prune_next'] < time() && $row['enable_prune'])
229 include_once(PHPBB_ROOT_PATH . 'includes/functions_admin.' . PHP_EXT);
231 if ($row['prune_days'])
233 if ($use_shutdown_function)
235 register_shutdown_function('auto_prune', $row['forum_id'], 'posted', $row['forum_flags'], $row['prune_days'], $row['prune_freq']);
237 else
239 auto_prune($row['forum_id'], 'posted', $row['forum_flags'], $row['prune_days'], $row['prune_freq']);
243 if ($row['prune_viewed'])
245 if ($use_shutdown_function)
247 register_shutdown_function('auto_prune', $row['forum_id'], 'viewed', $row['forum_flags'], $row['prune_viewed'], $row['prune_freq']);
249 else
251 auto_prune($row['forum_id'], 'viewed', $row['forum_flags'], $row['prune_viewed'], $row['prune_freq']);
256 break;
259 // Unloading cache and closing db after having done the dirty work.
260 if ($use_shutdown_function)
262 register_shutdown_function('unlock_cron');
263 register_shutdown_function('garbage_collection');
265 else
267 unlock_cron();
268 garbage_collection();
271 exit;
275 * Unlock cron script
277 function unlock_cron()
279 global $db;
281 $sql = 'UPDATE ' . CONFIG_TABLE . "
282 SET config_value = '0'
283 WHERE config_name = 'cron_lock' AND config_value = '" . $db->sql_escape(CRON_ID) . "'";
284 $db->sql_query($sql);