cron.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Wed, 07 Mar 2012 23:06:09 +0400
changeset 6 40f90a92f114
parent 0 ceef6d64c21d
permissions -rw-r--r--
Game logo
     1 <?php
     2 /**
     3 *
     4 * @package phpBB3
     5 * @version $Id$
     6 * @copyright (c) 2005 phpBB Group
     7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
     8 *
     9 */
    10 
    11 /**
    12 */
    13 define('IN_PHPBB', true);
    14 define('IN_CRON', true);
    15 $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
    16 $phpEx = substr(strrchr(__FILE__, '.'), 1);
    17 include($phpbb_root_path . 'common.' . $phpEx);
    18 
    19 // Do not update users last page entry
    20 $user->session_begin(false);
    21 $auth->acl($user->data);
    22 
    23 $cron_type = request_var('cron_type', '');
    24 
    25 // Output transparent gif
    26 header('Cache-Control: no-cache');
    27 header('Content-type: image/gif');
    28 header('Content-length: 43');
    29 
    30 echo base64_decode('R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==');
    31 
    32 // Flush here to prevent browser from showing the page as loading while running cron.
    33 flush();
    34 
    35 if (!isset($config['cron_lock']))
    36 {
    37 	set_config('cron_lock', '0', true);
    38 }
    39 
    40 // make sure cron doesn't run multiple times in parallel
    41 if ($config['cron_lock'])
    42 {
    43 	// if the other process is running more than an hour already we have to assume it
    44 	// aborted without cleaning the lock
    45 	$time = explode(' ', $config['cron_lock']);
    46 	$time = $time[0];
    47 
    48 	if ($time + 3600 >= time())
    49 	{
    50 		exit;
    51 	}
    52 }
    53 
    54 define('CRON_ID', time() . ' ' . unique_id());
    55 
    56 $sql = 'UPDATE ' . CONFIG_TABLE . "
    57 	SET config_value = '" . $db->sql_escape(CRON_ID) . "'
    58 	WHERE config_name = 'cron_lock' AND config_value = '" . $db->sql_escape($config['cron_lock']) . "'";
    59 $db->sql_query($sql);
    60 
    61 // another cron process altered the table between script start and UPDATE query so exit
    62 if ($db->sql_affectedrows() != 1)
    63 {
    64 	exit;
    65 }
    66 
    67 /**
    68 * Run cron-like action
    69 * Real cron-based layer will be introduced in 3.2
    70 */
    71 switch ($cron_type)
    72 {
    73 	case 'queue':
    74 
    75 		if (time() - $config['queue_interval'] <= $config['last_queue_run'] || !file_exists($phpbb_root_path . 'cache/queue.' . $phpEx))
    76 		{
    77 			break;
    78 		}
    79 
    80 		include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
    81 		$queue = new queue();
    82 
    83 		$queue->process();
    84 
    85 	break;
    86 
    87 	case 'tidy_cache':
    88 
    89 		if (time() - $config['cache_gc'] <= $config['cache_last_gc'] || !method_exists($cache, 'tidy'))
    90 		{
    91 			break;
    92 		}
    93 
    94 		$cache->tidy();
    95 
    96 	break;
    97 
    98 	case 'tidy_search':
    99 		
   100 		// Select the search method
   101 		$search_type = basename($config['search_type']);
   102 
   103 		if (time() - $config['search_gc'] <= $config['search_last_gc'] || !file_exists($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx))
   104 		{
   105 			break;
   106 		}
   107 
   108 		include_once("{$phpbb_root_path}includes/search/$search_type.$phpEx");
   109 
   110 		// We do some additional checks in the module to ensure it can actually be utilised
   111 		$error = false;
   112 		$search = new $search_type($error);
   113 
   114 		if ($error)
   115 		{
   116 			break;
   117 		}
   118 
   119 		$search->tidy();
   120 
   121 	break;
   122 
   123 	case 'tidy_warnings':
   124 
   125 		if (time() - $config['warnings_gc'] <= $config['warnings_last_gc'])
   126 		{
   127 			break;
   128 		}
   129 
   130 		include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
   131 
   132 		tidy_warnings();
   133 
   134 	break;
   135 
   136 	case 'tidy_database':
   137 
   138 		if (time() - $config['database_gc'] <= $config['database_last_gc'])
   139 		{
   140 			break;
   141 		}
   142 
   143 		include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
   144 
   145 		tidy_database();
   146 
   147 	break;
   148 
   149 	case 'tidy_sessions':
   150 
   151 		if (time() - $config['session_gc'] <= $config['session_last_gc'])
   152 		{
   153 			break;
   154 		}
   155 
   156 		$user->session_gc();
   157 
   158 	break;
   159 
   160 	case 'prune_forum':
   161 
   162 		$forum_id = request_var('f', 0);
   163 
   164 		$sql = 'SELECT forum_id, prune_next, enable_prune, prune_days, prune_viewed, forum_flags, prune_freq
   165 			FROM ' . FORUMS_TABLE . "
   166 			WHERE forum_id = $forum_id";
   167 		$result = $db->sql_query($sql);
   168 		$row = $db->sql_fetchrow($result);
   169 		$db->sql_freeresult($result);
   170 
   171 		if (!$row)
   172 		{
   173 			break;
   174 		}
   175 
   176 		// Do the forum Prune thang
   177 		if ($row['prune_next'] < time() && $row['enable_prune'])
   178 		{
   179 			include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
   180 
   181 			if ($row['prune_days'])
   182 			{
   183 				auto_prune($row['forum_id'], 'posted', $row['forum_flags'], $row['prune_days'], $row['prune_freq']);
   184 			}
   185 
   186 			if ($row['prune_viewed'])
   187 			{
   188 				auto_prune($row['forum_id'], 'viewed', $row['forum_flags'], $row['prune_viewed'], $row['prune_freq']);
   189 			}
   190 		}
   191 
   192 	break;
   193 }
   194 
   195 // Unloading cache and closing db after having done the dirty work.
   196 unlock_cron();
   197 garbage_collection();
   198 
   199 exit;
   200 
   201 
   202 /**
   203 * Unlock cron script
   204 */
   205 function unlock_cron()
   206 {
   207 	global $db;
   208 
   209 	$sql = 'UPDATE ' . CONFIG_TABLE . "
   210 		SET config_value = '0'
   211 		WHERE config_name = 'cron_lock' AND config_value = '" . $db->sql_escape(CRON_ID) . "'";
   212 	$db->sql_query($sql);
   213 }
   214 
   215 ?>