$Date: 2005-10-28 15:18:16 +0100 (Fri, 28 Oct 2005) $
| > $Revision: 67 $
| > $Author: bfarber $
+---------------------------------------------------------------------------
|
| > Task Manager
| > Module written by Matt Mecham
| > Date started: 27th January 2004
|
| > Module Version Number: 1.0.0
| > DBA Checked: Tue 25th May 2004
+--------------------------------------------------------------------------
*/
if ( ! defined( 'IN_ACP' ) )
{
print "
Неверный вход
У Вас нет доступа к директиве этого файла. Если Вы проводили обновления, убедитесь, что не забыли обновить 'admin.php'.";
exit();
}
class ad_task_manager
{
# Global
var $ipsclass;
var $functions = "";
/**
* Section title name
*
* @var string
*/
var $perm_main = "tools";
/**
* Section title name
*
* @var string
*/
var $perm_child = "task";
function auto_run()
{
$this->ipsclass->form_code = "{$this->ipsclass->form_code}";
//-----------------------------------------
// Require and RUN !! THERES A BOMB
//-----------------------------------------
require_once( ROOT_PATH.'sources/lib/func_taskmanager.php' );
$this->functions = new func_taskmanager();
$this->functions->ipsclass =& $this->ipsclass;
//-----------------------------------------
// Load skin
//-----------------------------------------
$this->html = $this->ipsclass->acp_load_template('cp_skin_taskmanager');
//-----------------------------------------
// Continue
//-----------------------------------------
$this->ipsclass->admin->page_detail = "Эта секция содержит все ваши установленные задачи.
Пожалуйста, учтите, что все эти задачи запускаются только тогда, когда ваш форум доступен посетителям, следующий запуск происходит только при вызове форума.";
$this->ipsclass->admin->page_title = "Управление задачами";
//-----------------------------------------
// Kill globals - globals bad, Homer good.
//-----------------------------------------
$tmp_in = array_merge( $_GET, $_POST, $_COOKIE );
foreach ( $tmp_in as $k => $v )
{
unset($$k);
}
$this->ipsclass->admin->nav[] = array( $this->ipsclass->form_code, 'Управление задачами' );
//-----------------------------------------
// Using "do"?
//-----------------------------------------
if ( $this->ipsclass->input['j_do'] )
{
$this->ipsclass->input['code'] = $this->ipsclass->input['j_do'];
}
else if ( $this->ipsclass->input['do'] )
{
$this->ipsclass->input['code'] = $this->ipsclass->input['do'];
}
//-----------------------------------------
// What to do...
//-----------------------------------------
switch($this->ipsclass->input['code'])
{
case 'task_edit':
$this->ipsclass->admin->cp_permission_check( $this->perm_main.'|'.$this->perm_child.':edit' );
$this->task_form('edit');
break;
case 'task_add':
$this->ipsclass->admin->cp_permission_check( $this->perm_main.'|'.$this->perm_child.':add' );
$this->task_form('add');
break;
case 'task_edit_do':
$this->ipsclass->admin->cp_permission_check( $this->perm_main.'|'.$this->perm_child.':edit' );
$this->task_do_save('edit');
break;
case 'task_add_do':
$this->ipsclass->admin->cp_permission_check( $this->perm_main.'|'.$this->perm_child.':add' );
$this->task_do_save('add');
break;
case 'task_delete':
$this->ipsclass->admin->cp_permission_check( $this->perm_main.'|'.$this->perm_child.':remove' );
$this->task_delete_task();
break;
case 'task_run_now':
$this->ipsclass->admin->cp_permission_check( $this->perm_main.'|'.$this->perm_child.':runnow' );
$this->task_run_task();
break;
case 'task_unlock':
$this->ipsclass->admin->cp_permission_check( $this->perm_main.'|'.$this->perm_child.':edit' );
$this->task_unlock();
break;
case 'log':
$this->ipsclass->admin->cp_permission_check( $this->perm_main.'|'.$this->perm_child.':log' );
$this->task_log_setup();
break;
case 'showlog':
$this->ipsclass->admin->cp_permission_check( $this->perm_main.'|'.$this->perm_child.':log' );
$this->task_log_show();
break;
case 'deletelog':
$this->ipsclass->admin->cp_permission_check( $this->perm_main.'|'.$this->perm_child.':remove' );
$this->task_log_delete();
break;
default:
$this->ipsclass->admin->cp_permission_check( $this->perm_main.'|'.$this->perm_child.':' );
$this->task_show_tasks();
break;
}
}
/*-------------------------------------------------------------------------*/
// TASK LOG DELETE
/*-------------------------------------------------------------------------*/
function task_log_delete()
{
//-----------------------------------------
// SHOW 'EM
//-----------------------------------------
$prune = $this->ipsclass->input['task_prune'] ? $this->ipsclass->input['task_prune'] : 30;
$prune = time() - ( $prune * 86400 );
if ( $this->ipsclass->input['task_id'] != -1 )
{
$where = "log_title='".$this->ipsclass->input['task_id']."' AND log_date < $prune";
}
else
{
$where = "log_date < $prune";
}
$this->ipsclass->DB->simple_exec_query( array( 'delete' => 'task_logs', 'where' => $where ) );
$this->ipsclass->main_msg = 'Выбранные логи выполненных задач удалены';
$this->task_log_setup();
}
/*-------------------------------------------------------------------------*/
// TASK LOG SHOW
/*-------------------------------------------------------------------------*/
function task_log_show()
{
//-----------------------------------------
// SHOW 'EM
//-----------------------------------------
$limit = $this->ipsclass->input['task_count'] ? $this->ipsclass->input['task_count'] : 30;
$limit = $limit > 150 ? 150 : $limit;
if ( $this->ipsclass->input['task_id'] != -1 )
{
$this->ipsclass->DB->simple_construct( array( 'select' => '*', 'from' => 'task_logs', 'where' => "log_title='".$this->ipsclass->input['task_id']."'", 'order' => 'log_date DESC', 'limit' => array(0,$limit) ) );
}
else
{
$this->ipsclass->DB->simple_construct( array( 'select' => '*', 'from' => 'task_logs', 'order' => 'log_date DESC', 'limit' => array(0,$limit) ) );
}
$this->ipsclass->DB->simple_exec();
$this->ipsclass->adskin->td_header[] = array( "Выполненная задача" , "20%" );
$this->ipsclass->adskin->td_header[] = array( "Дата и Время" , "35%" );
$this->ipsclass->adskin->td_header[] = array( "Информация", "45%" );
$this->ipsclass->html .= $this->ipsclass->adskin->start_table( "Выбранные логи задач" );
if ( $this->ipsclass->DB->get_num_rows() )
{
while ( $row = $this->ipsclass->DB->fetch_row() )
{
$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array( "{$row['log_title']}",
$this->ipsclass->admin->get_date( $row['log_date'], 'SHORT' ),
"{$row['log_desc']}",
) );
}
}
else
{
$this->ipsclass->html .= $this->ipsclass->adskin->add_td_basic("Нет записей");
}
$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
$this->ipsclass->admin->output();
}
/*-------------------------------------------------------------------------*/
// TASK LOG START
/*-------------------------------------------------------------------------*/
function task_log_setup()
{
//-----------------------------------------
// Some set up
//-----------------------------------------
$tasks = array( 0 => array( -1, 'Все задачи' ) );
$this->ipsclass->DB->simple_construct( array( 'select' => '*', 'from' => 'task_manager', 'order' => 'task_title' ) );
$this->ipsclass->DB->simple_exec();
while ( $pee = $this->ipsclass->DB->fetch_row() )
{
$tasks[] = array( $pee['task_title'], $pee['task_title'] );
}
//-----------------------------------------
// LAST FIVE ACTIONS
//-----------------------------------------
$this->ipsclass->DB->simple_construct( array( 'select' => '*', 'from' => 'task_logs', 'order' => 'log_date DESC', 'limit' => array(0,5) ) );
$this->ipsclass->DB->simple_exec();
$this->ipsclass->adskin->td_header[] = array( "Выполненная задача" , "20%" );
$this->ipsclass->adskin->td_header[] = array( "Дата Время" , "35%" );
$this->ipsclass->adskin->td_header[] = array( "Информация" , "45%" );
$this->ipsclass->html .= $this->ipsclass->adskin->start_table( "5 последних запущенных задач" );
if ( $this->ipsclass->DB->get_num_rows() )
{
while ( $row = $this->ipsclass->DB->fetch_row() )
{
$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array( "{$row['log_title']}",
$this->ipsclass->admin->get_date( $row['log_date'], 'SHORT' ),
"{$row['log_desc']}",
) );
}
}
else
{
$this->ipsclass->html .= $this->ipsclass->adskin->add_td_basic("Нет записей");
}
$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
//-----------------------------------------
// Show more...
//-----------------------------------------
$this->ipsclass->html .= $this->ipsclass->adskin->start_form( array( 1 => array( 'act' , 'task' ),
2 => array( 'code' , 'showlog' ),
4 => array( 'section', $this->ipsclass->section_code ),
) );
$this->ipsclass->adskin->td_header[] = array( " " , "60%" );
$this->ipsclass->adskin->td_header[] = array( " " , "40%" );
$this->ipsclass->html .= $this->ipsclass->adskin->start_table( "Просмотр логов выполненных задач" );
$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array( "Просмотр логов выполненной задачи:",
$this->ipsclass->adskin->form_dropdown( 'task_id', $tasks )
) );
$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array( "Сколько вывести записей?",
$this->ipsclass->adskin->form_input( 'task_count', '30' )
) );
$this->ipsclass->html .= $this->ipsclass->adskin->end_form('Смотреть логи');
$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
//-----------------------------------------
// Delete...
//-----------------------------------------
$this->ipsclass->html .= $this->ipsclass->adskin->start_form( array( 1 => array( 'act' , 'task' ),
2 => array( 'code' , 'deletelog' ),
4 => array( 'section', $this->ipsclass->section_code ),
) );
$this->ipsclass->adskin->td_header[] = array( " " , "60%" );
$this->ipsclass->adskin->td_header[] = array( " " , "40%" );
$this->ipsclass->html .= $this->ipsclass->adskin->start_table( "Удалить логи выполненной задачи" );
$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array( "Удалить логи задачи:",
$this->ipsclass->adskin->form_dropdown( 'task_id', $tasks )
) );
$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array( "Удалить логи старше, чем (в днях):",
$this->ipsclass->adskin->form_input( 'task_prune', '30' )
) );
$this->ipsclass->html .= $this->ipsclass->adskin->end_form('Удалить логи');
$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
$this->ipsclass->admin->output();
}
/*-------------------------------------------------------------------------*/
// UNLOCK TASK
/*-------------------------------------------------------------------------*/
function task_unlock()
{
//-----------------------------------------
// INIT
//-----------------------------------------
$task_id = intval( $this->ipsclass->input['task_id'] );
$this->ipsclass->DB->do_update( 'task_manager', array( 'task_locked' => 0 ), "task_id=".$task_id );
$this->ipsclass->main_msg = 'Закрытые задачи удалены';
$this->task_show_tasks();
}
/*-------------------------------------------------------------------------*/
// RUN TASK
/*-------------------------------------------------------------------------*/
function task_run_task()
{
//-----------------------------------------
// INIT
//-----------------------------------------
$task_id = intval( $this->ipsclass->input['task_id'] );
//-----------------------------------------
// Check
//-----------------------------------------
if ( ! $task_id )
{
$this->ipsclass->main_msg = 'Не указан ID задачи, действие не выполнено';
$this->task_show_tasks();
}
$this_task = $this->ipsclass->DB->simple_exec_query( array( 'select' => '*', 'from' => 'task_manager', 'where' => 'task_id='.$task_id ) );
if ( ! $this_task['task_id'] )
{
$this->ipsclass->main_msg = 'Нет задач для выполнения.';
$this->show_tasks();
}
if ( ! $this_task['task_enabled'] )
{
$this->ipsclass->main_msg = "Эта задача отключена. Пожалуйста, включите ее до запуска.";
$this->task_show_tasks();
}
//-----------------------------------------
// Get new instance of functions
//-----------------------------------------
$func = new func_taskmanager();
$func->ipsclass =& $this->ipsclass;
//-----------------------------------------
// Locked?
//-----------------------------------------
if ( $this_task['task_locked'] > 0 )
{
$this->ipsclass->main_msg = "Эта задача была закрыта в ". gmdate( 'j M Y - G:i', $this_task['task_locked'] ) ." и не может быть запущена пока Вы ее не откроете снова.";
$this->task_show_tasks();
}
$newdate = $func->generate_next_run($this_task);
$this->ipsclass->DB->do_update( 'task_manager', array( 'task_next_run' => $newdate, 'task_locked' => time() ), "task_id=".$this_task['task_id'] );
$func->save_next_run_stamp();
$func->root_path = ROOT_PATH;
if ( file_exists( $func->root_path.'sources/tasks/'.$this_task['task_file'] ) )
{
require_once( $func->root_path.'sources/tasks/'.$this_task['task_file'] );
$myobj = new task_item();
$myobj->register_class( $func );
$myobj->pass_task( $this_task );
$myobj->run_task();
$this->ipsclass->main_msg = 'Задача успешно запущена';
$this->task_show_tasks();
}
else
{
$this->ipsclass->main_msg = 'Невозможно найти: '.$func->root_path.'sources/tasks/'.$this_task['task_file'];
$this->task_show_tasks();
}
}
/*-------------------------------------------------------------------------*/
// DELETE TASK
/*-------------------------------------------------------------------------*/
function task_delete_task()
{
//-----------------------------------------
// INIT
//-----------------------------------------
$task_id = intval( $this->ipsclass->input['task_id'] );
//-----------------------------------------
// Check
//-----------------------------------------
$task = $this->ipsclass->DB->simple_exec_query( array( 'select' => '*', 'from' => 'task_manager', 'where' => "task_id=$task_id" ) );
if ( $task['task_safemode'] and ! IN_DEV )
{
$this->ipsclass->main_msg = "Вы не можете удалить эту задачу.";
$this->task_show_tasks();
return;
}
//-----------------------------------------
// Remove from the DB
//-----------------------------------------
$this->ipsclass->DB->simple_exec_query( array( 'delete' => 'task_manager', 'where' => 'task_id='.$task_id ) );
$this->functions->save_next_run_stamp();
$this->ipsclass->main_msg = 'Задача удалена!';
$this->task_show_tasks();
}
/*-------------------------------------------------------------------------*/
// DO SAVE
/*-------------------------------------------------------------------------*/
function task_do_save($type='edit')
{
//-----------------------------------------
// INIT
//-----------------------------------------
$task_id = intval($this->ipsclass->input['task_id']);
$task_cronkey = $this->ipsclass->input['task_cronkey'];
//-----------------------------------------
// Check...
//-----------------------------------------
if ( $type == 'edit' )
{
if ( ! $task_id )
{
$this->ipsclass->main_msg = 'Не указан ID задачи, действие не выполнено';
$this->task_form();
}
}
if ( ! $this->ipsclass->input['task_title'] )
{
$this->ipsclass->main_msg = 'Вы должны ввести название задачи.';
$this->task_form();
}
if ( ! $this->ipsclass->input['task_file'] )
{
$this->ipsclass->main_msg = 'Вы должны ввести имя файла для исполнения';
$this->task_form();
}
//-----------------------------------------
// Check the task file...
//-----------------------------------------
$this->ipsclass->input['task_file'] = str_replace( '../', '', $this->ipsclass->input['task_file'] );
//-----------------------------------------
// Compile task
//-----------------------------------------
$save = array( 'task_title' => $this->ipsclass->input['task_title'],
'task_description' => $this->ipsclass->input['task_description'],
'task_file' => $this->ipsclass->input['task_file'],
'task_week_day' => $this->ipsclass->input['task_week_day'],
'task_month_day' => $this->ipsclass->input['task_month_day'],
'task_hour' => $this->ipsclass->input['task_hour'],
'task_minute' => $this->ipsclass->input['task_minute'],
'task_log' => $this->ipsclass->input['task_log'],
'task_cronkey' => $this->ipsclass->input['task_cronkey'] ? $task_cronkey : md5(microtime()),
'task_enabled' => $this->ipsclass->input['task_enabled']
);
if ( IN_DEV )
{
$save['task_key'] = $this->ipsclass->input['task_key'];
$save['task_safemode'] = $this->ipsclass->input['task_safemode'];
}
//-----------------------------------------
// Get next run date...
//-----------------------------------------
$save['task_next_run'] = $this->functions->generate_next_run( $save );
if ( $type == 'edit' )
{
$this->ipsclass->DB->do_update( 'task_manager', $save, 'task_id='.$task_id );
$this->ipsclass->main_msg = 'Задача успешно изменена';
}
else
{
$this->ipsclass->DB->do_insert( 'task_manager', $save );
$this->ipsclass->main_msg = 'Задача успешно сохранена';
}
$this->functions->save_next_run_stamp();
$this->task_show_tasks();
}
/*-------------------------------------------------------------------------*/
// Task Manager: Form
/*-------------------------------------------------------------------------*/
function task_form($type='edit')
{
//-----------------------------------------
// Init Vars
//-----------------------------------------
$form = array();
$task_id = intval( $this->ipsclass->input['task_id'] );
# Drop downs
$dropdows = array();
//-----------------------------------------
// Edit or add?
//-----------------------------------------
if ( $type == 'edit' )
{
$button = "Отредактировать задачу";
$formbit = "task_edit_do";
$this->ipsclass->html_help_title = "";
$this->ipsclass->html_help_msg = "";
$task = $this->ipsclass->DB->simple_exec_query( array( 'select' => '*', 'from' => 'task_manager', 'where' => "task_id=$task_id" ) );
if ( $task['task_safemode'] and ! IN_DEV )
{
$this->ipsclass->main_msg = "Вы не можете изменить эту задачу.";
$this->task_show_tasks();
return;
}
$title = "Изменение задачи: ".$group['cb_group_name'];
}
else
{
$button = "Добавить эту задачу";
$formbit = "task_add_do";
$this->ipsclass->html_help_title = "";
$this->ipsclass->html_help_msg = "";
$task = array();
$title = "Добавление задачи";
}
//-----------------------------------------
// Create drop downs
//-----------------------------------------
$dropdown['_minute'] = array( 0 => array( '-1', 'Каждую минуту' ) );
$dropdown['_hour'] = array( 0 => array( '-1', 'Каждый час' ), 1 => array( '0', '0 - Полночь' ) );
$dropdown['_wday'] = array( 0 => array( '-1', 'Каждый день недели' ) );
$dropdown['_mday'] = array( 0 => array( '-1', 'Каждый день месяца' ) );
for( $i = 0 ; $i < 60; $i++ )
{
$dropdown['_minute'][] = array( $i, $i );
}
for( $i = 1 ; $i < 24; $i++ )
{
if ( $i < 12 )
{
$ampm = $i.' am';
}
else if ( $i == 12 )
{
$ampm = 'Полдень';
}
else
{
$ampm = $i - 12 . ' pm';
}
$dropdown['_hour'][] = array( $i, $i. ' - ('.$ampm.')' );
}
for( $i = 1 ; $i < 32; $i++ )
{
$dropdown['_mday'][] = array( $i, $i );
}
$dropdown['_wday'][] = array( '0', 'Воскресенье' );
$dropdown['_wday'][] = array( '1', 'Понедельник' );
$dropdown['_wday'][] = array( '2', 'Вторник' );
$dropdown['_wday'][] = array( '3', 'Среда' );
$dropdown['_wday'][] = array( '4', 'Четверг' );
$dropdown['_wday'][] = array( '5', 'Пятница' );
$dropdown['_wday'][] = array( '6', 'Суббота' );
//-----------------------------------------
// Form elements
//-----------------------------------------
$form['task_title'] = $this->ipsclass->adskin->form_input( 'task_title' , $_POST['task_title'] ? $_POST['task_title'] : $task['task_title'] );
$form['task_description'] = $this->ipsclass->adskin->form_input( 'task_description', $_POST['task_description'] ? $_POST['task_description'] : $task['task_description'] );
$form['task_file'] = $this->ipsclass->adskin->form_simple_input( 'task_file' , $_POST['task_file'] ? $_POST['task_file'] : $task['task_file'] , '20' );
$form['task_minute'] = $this->ipsclass->adskin->form_dropdown( 'task_minute' , $dropdown['_minute'] , $_POST['task_minute'] ? $_POST['task_minute'] : $task['task_minute'] , 'onchange="updatepreview()"' );
$form['task_hour'] = $this->ipsclass->adskin->form_dropdown( 'task_hour' , $dropdown['_hour'] , $_POST['task_hour'] ? $_POST['task_hour'] : $task['task_hour'] , 'onchange="updatepreview()"' );
$form['task_week_day'] = $this->ipsclass->adskin->form_dropdown( 'task_week_day' , $dropdown['_wday'] , $_POST['task_week_day'] ? $_POST['task_week_day'] : $task['task_week_day'] , 'onchange="updatepreview()"' );
$form['task_month_day'] = $this->ipsclass->adskin->form_dropdown( 'task_month_day' , $dropdown['_mday'] , $_POST['task_month_day'] ? $_POST['task_month_day'] : $task['task_month_day'], 'onchange="updatepreview()"' );
$form['task_log'] = $this->ipsclass->adskin->form_yes_no( 'task_log' , $_POST['task_log'] ? $_POST['task_log'] : $task['task_log'] );
$form['task_enabled'] = $this->ipsclass->adskin->form_yes_no( 'task_enabled' , $_POST['task_enabled'] ? $_POST['task_enabled'] : $task['task_enabled'] );
if ( IN_DEV )
{
$form['task_key'] = $this->ipsclass->adskin->form_input( 'task_key' , $_POST['task_key'] ? $_POST['task_key'] : $task['task_key'] );
$form['task_safemode'] = $this->ipsclass->adskin->form_yes_no( 'task_safemode', $_POST['task_safemode'] ? $_POST['task_safemode'] : $task['task_safemode'] );
}
$this->ipsclass->html .= $this->html->task_manager_form( $form, $button, $formbit, $type, $title, $task );
$this->ipsclass->admin->output();
}
/*-------------------------------------------------------------------------*/
// Task Manager: Overview
/*-------------------------------------------------------------------------*/
function task_show_tasks()
{
//-----------------------------------------
// INIT
//-----------------------------------------
$row = array();
$content = "";
//-----------------------------------------
// List tasks (pointless comment)
//-----------------------------------------
$this->ipsclass->DB->simple_construct( array( 'select' => '*', 'from' => 'task_manager', 'order' => 'task_safemode, task_next_run' ) );
$this->ipsclass->DB->simple_exec();
while ( $row = $this->ipsclass->DB->fetch_row() )
{
$row['task_minute'] = $row['task_minute'] != '-1' ? $row['task_minute'] : '-';
$row['task_hour'] = $row['task_hour'] != '-1' ? $row['task_hour'] : '-';
$row['task_month_day'] = $row['task_month_day'] != '-1' ? $row['task_month_day'] : '-';
$row['task_week_day'] = $row['task_week_day'] != '-1' ? $row['task_week_day'] : '-';
if ( time() > $row['task_next_run'] )
{
$row['_image'] = 'task_run_now.gif';
}
else
{
$row['_image'] = 'task_run.gif';
}
$row['_next_run'] = gmdate( 'j.n.Y, G:i', $row['task_next_run'] );
if ( $row['task_enabled'] != 1 )
{
$row['_class'] = " style='color:gray'";
$row['_title'] = " (Отключено)";
$row['_next_run'] = "{$row['_next_run']}";
}
$content .= $this->html->task_manager_row( $row );
}
//-------------------------------
// Print it
//-------------------------------
$this->ipsclass->html .= $this->html->task_manager_wrapper( $content, gmdate( 'j.n.Y, G:i' ) );
$this->ipsclass->admin->output();
}
}
?>