PageRenderTime 38ms CodeModel.GetById 15ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/upload/admin/admin_cron.php

http://torrentpier2.googlecode.com/
PHP | 235 lines | 204 code | 28 blank | 3 comment | 39 complexity | a2849c18fdc34ad7a20035e106f61d52 MD5 | raw file
  1<?php
  2
  3// ACP Header - START
  4if (!empty($setmodules))
  5{
  6	if (IS_SUPER_ADMIN)
  7	{
  8		$module['TorrentPier']['Cron'] = basename(__FILE__) . '?mode=list';
  9	}
 10	return;
 11}
 12
 13$mode = isset($_GET['mode']) ? $_GET['mode'] : '';
 14$job_id = isset($_GET['id']) ? (int) $_GET['id'] : '';
 15$submit  = isset($_POST['submit']);
 16$jobs = isset($_POST['select']) ? implode(',', $_POST['select']) : '';
 17$cron_action = isset($_POST['cron_action']) ? $_POST['cron_action'] : '';
 18
 19if ($mode == 'run' && !$job_id)
 20{
 21	define('BB_ROOT', './../');
 22	require(BB_ROOT.'common.php');
 23	$user->session_start();
 24	redirect('admin/'.basename(__FILE__) . '?mode=list');
 25}
 26else require('./pagestart.php');
 27// ACP Header - END
 28
 29if (!IS_SUPER_ADMIN) bb_die($lang['NOT_ADMIN']);
 30
 31require(INC_DIR .'functions_admin_torrent.php');
 32require(INC_DIR .'functions_admin_cron.php');
 33
 34$sql = DB()->fetch_rowset("SELECT * FROM ". BB_CONFIG ." WHERE config_name = 'cron_enabled' OR config_name = 'cron_check_interval'");
 35
 36foreach ($sql as $row)
 37{
 38	$config_name = $row['config_name'];
 39	$config_value = $row['config_value'];
 40	$default_config[$config_name] = $config_value;
 41
 42	$new[$config_name] = isset($_POST[$config_name]) ? $_POST[$config_name] : $default_config[$config_name];
 43
 44	if (isset($_POST['submit']) && $row['config_value'] != $new[$config_name])
 45	{
 46		 bb_update_config(array($config_name => $new[$config_name]));
 47	}
 48}
 49
 50$template->assign_vars(array(
 51	'CRON_ENABLED' => ($new['cron_enabled']) ? true : false,
 52	'CRON_CHECK_INTERVAL' => $new['cron_check_interval'],
 53));
 54
 55switch($mode)
 56{
 57	case 'list':
 58		$sql = DB()->fetch_rowset("SELECT * FROM ". BB_CRON ." ORDER BY cron_id");
 59
 60		foreach ($sql as $i => $row)
 61		{
 62			$template->assign_block_vars('list', array(
 63				'ROW_CLASS'	  => !($i % 2) ? 'row2' : 'row1',
 64				'JOB_ID'      => $i + 1,
 65				'CRON_ID'     => $row['cron_id'],
 66				'CRON_ACTIVE' => $row['cron_active'] ? '<img src="../images/icon_run.gif" alt="'. $lang['YES'] .'" />' : '<img src="../images/icon_delete.gif" alt="'. $lang['NO'] .'" />',
 67				'CRON_TITLE'  => $row['cron_title'],
 68				'CRON_SCRIPT' => $row['cron_script'],
 69				'SCHEDULE'    => $row['schedule'] ? $lang['SCHEDULE'][$row['schedule']] : '<b class="leech">'. $lang['NOSELECT'] .'</b>',
 70				'RUN_DAY'     => $row['run_day'],
 71				'LAST_RUN'    => $row['last_run'],
 72				'NEXT_RUN'    => $row['next_run'],
 73				'RUN_COUNT'   => $row['run_counter'],
 74			));
 75		}
 76
 77		$template->assign_vars(array(
 78			'TPL_CRON_LIST' => true,
 79			'S_CRON_ACTION' => 'admin_cron.php',
 80			'S_MODE'        => 'list',
 81		));
 82
 83		//detect cron status
 84		if (@file_exists('../triggers/cron_running'))
 85		{
 86			$template->assign_vars(array(
 87				'CRON_RUNNING' => true,
 88			));
 89		}
 90	break;
 91
 92	case 'repair':
 93		if (@file_exists('../triggers/cron_running'))
 94		{
 95			rename("../triggers/cron_running", "../triggers/cron_allowed");
 96		}
 97		redirect('admin/'.basename(__FILE__) . '?mode=list');
 98	break;
 99
100	case 'run':
101		run_jobs($job_id);
102		redirect('admin/'.basename(__FILE__) . '?mode=list');
103	break;
104
105	case 'edit':
106		$sql = DB()->fetch_rowset("SELECT * FROM ". BB_CRON ." WHERE cron_id = $job_id");
107
108		foreach ($sql as $row)
109		{
110			$template->assign_vars(array(
111				'CRON_ID'         => $row['cron_id'],
112				'CRON_ACTIVE'     => $row['cron_active'],
113				'CRON_TITLE'      => $row['cron_title'],
114				'CRON_SCRIPT'     => $row['cron_script'],
115				'SCHEDULE'        => $row['schedule'] ? $lang['SCHEDULE'][$row['schedule']] : '',
116				'RUN_DAY'         => $row['run_day'],
117				'RUN_TIME'        => $row['run_time'],
118				'RUN_ORDER'       => $row['run_order'],
119				'LAST_RUN'        => $row['last_run'],
120				'NEXT_RUN'        => $row['next_run'],
121				'RUN_INTERVAL'    => $row['run_interval'],
122				'LOG_ENABLED'     => $row['log_enabled'],
123				'LOG_FILE'        => $row['log_file'],
124				'LOG_SQL_QUERIES' => $row['log_sql_queries'],
125				'DISABLE_BOARD'   => $row['disable_board'],
126				'RUN_COUNTER'     => $row['run_counter'],
127			));
128		}
129
130		$run_day = array($lang['DELTA_TIME']['INTERVALS']['mday'][0] => 0);
131		for ($i = 1; $i <= 28; $i++)
132		{
133			$run_day[$i] = $i;
134		}
135
136		$schedule = array($lang['SCHEDULE']['select'] => 0);
137		foreach ($lang['SCHEDULE'] as $type => $key)
138		{
139			$schedule[$key] = $type;
140		}
141
142		$template->assign_vars(array(
143			'TPL_CRON_EDIT'     => true,
144			'S_CRON_ACTION'     => 'admin_cron.php',
145			'S_MODE'            => 'edit',
146			'SCHEDULE'          => build_select('schedule', $schedule, $row['schedule']),
147			'RUN_DAY'           => build_select('run_day', $run_day, $row['run_day']),
148			'L_CRON_EDIT_HEAD'  => $lang['CRON_EDIT_HEAD_EDIT'],
149		));
150	break;
151
152	case 'add':
153		$run_day = array($lang['DELTA_TIME']['INTERVALS']['mday'][0] => 0);
154		for ($i = 1; $i <= 28; $i++)
155		{
156			$run_day[$i] = $i;
157		}
158
159		$schedule = array();
160		foreach ($lang['SCHEDULE'] as $type => $key)
161		{
162			$schedule[$key] = $type;
163		}
164
165		$template->assign_vars(array(
166			'TPL_CRON_EDIT'     => true,
167			'S_CRON_ACTION'     => 'admin_cron.php',
168			'S_MODE'            => 'add',
169			'SCHEDULE'          => build_select('schedule', $schedule, 'select', null, null),
170			'RUN_DAY'           => build_select('run_day', $run_day, 0, null, null),
171			'L_CRON_EDIT_HEAD'  => $lang['CRON_EDIT_HEAD_ADD'],
172			'CRON_ID'           => 'none',
173			'CRON_ACTIVE'       => 1,
174			'CRON_TITLE'        => '',
175			'CRON_SCRIPT'       => '',
176			'RUN_TIME'          => '',
177			'RUN_ORDER'         => 255,
178			'LAST_RUN'          => '0000-00-00 00:00:00',
179			'NEXT_RUN'          => '0000-00-00 00:00:00',
180			'RUN_INTERVAL'      => '',
181			'LOG_ENABLED'       => 0,
182			'LOG_FILE'          => '',
183			'LOG_SQL_QUERIES'   => 0,
184			'DISABLE_BOARD'     => 0,
185			'RUN_COUNTER'       => 0,
186		));
187	break;
188
189	case 'delete':
190		delete_jobs($job_id);
191		$message = $lang['JOB_REMOVED'] . '<br /><br />' . sprintf($lang['CLICK_RETURN_JOBS'], '<a href="admin_cron.php?mode=list">', '</a>') . '<br /><br />' . sprintf($lang['CLICK_RETURN_ADMIN_INDEX'], '<a href="index.php?pane=right">', '</a>');
192		message_die(GENERAL_MESSAGE, $message);
193	break;
194}
195
196if ($submit)
197{
198	if ($_POST['mode'] == 'list')
199	{
200		if ($cron_action == 'run' && $jobs)
201		{
202			run_jobs($jobs);
203		}
204		else if ($cron_action == 'delete' && $jobs)
205		{
206			delete_jobs($jobs);
207		}
208		else if (($cron_action == 'disable' || $cron_action == 'enable') && $jobs)
209		{
210			toggle_active($jobs, $cron_action);
211		}
212		redirect('admin/'.basename(__FILE__) . '?mode=list');
213	}
214	else if (validate_cron_post($_POST) == 1)
215	{
216		if($_POST['mode'] == 'edit')
217		{
218			update_cron_job($_POST);
219		}
220		else if ($_POST['mode'] == 'add')
221		{
222			insert_cron_job($_POST);
223		}
224		else bb_die();
225
226		redirect('admin/'.basename(__FILE__) . '?mode=list');
227	}
228	else
229	{
230		$message = validate_cron_post($_POST);
231		message_die(GENERAL_MESSAGE, $message);
232	}
233}
234
235print_page('admin_cron.tpl', 'admin');