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 "
Incorrect access
You cannot access this file directly. If you have recently upgraded, make sure you upgraded 'admin.php'.";
exit();
}
class ad_task_manager {
var $functions = "";
function auto_run()
{
global $ibforums, $DB, $std;
//-----------------------------------------
// Require and RUN !! THERES A BOMB
//-----------------------------------------
require_once( ROOT_PATH.'sources/lib/task_functions.php' );
$this->functions = new task_functions();
$ibforums->admin->page_detail = "The task manager contains all your scheduled tasks.
Please note that as these tasks are run when the board is accessed, the next run time is to be used as a guide only and depends on the traffic your board gets.";
$ibforums->admin->page_title = "Task Manager";
//-----------------------------------------
// Kill globals - globals bad, Homer good.
//-----------------------------------------
$tmp_in = array_merge( $_GET, $_POST, $_COOKIE );
foreach ( $tmp_in as $k => $v )
{
unset($$k);
}
$ibforums->admin->nav[] = array( 'act=task', 'Task Manager Home' );
//-----------------------------------------
// What to do...
//-----------------------------------------
switch($ibforums->input['code'])
{
case 'edit':
$this->task_form('edit');
break;
case 'doedittask':
$this->do_save('edit');
break;
case 'addtask':
$this->task_form('add');
break;
case 'doaddtask':
$this->do_save('add');
break;
case 'delete':
$this->delete_task();
break;
case 'run':
$this->run_task();
break;
case 'log':
$this->task_log_setup();
break;
case 'showlog':
$this->task_log_show();
break;
case 'deletelog':
$this->task_log_delete();
break;
default:
$this->show_tasks();
break;
}
}
//-----------------------------------------
// TASK LOG DELETE
//-----------------------------------------
function task_log_delete()
{
global $ibforums, $DB, $std;
//-----------------------------------------
// SHOW 'EM
//-----------------------------------------
$prune = $ibforums->input['task_prune'] ? $ibforums->input['task_prune'] : 30;
$prune = time() - ( $prune * 86400 );
if ( $ibforums->input['task_id'] != -1 )
{
$where = "task_title='".$ibforums->input['task_id']."' AND log_date < $prune";
}
else
{
$where = "log_date < $prune";
}
$DB->simple_exec_query( array( 'delete' => 'task_logs', 'where' => $where ) );
$ibforums->main_msg = 'Selected Task Logs Removed';
$this->task_log_setup();
}
//-----------------------------------------
// TASK LOG SHOW
//-----------------------------------------
function task_log_show()
{
global $ibforums, $DB, $std;
//-----------------------------------------
// SHOW 'EM
//-----------------------------------------
$limit = $ibforums->input['task_count'] ? $ibforums->input['task_count'] : 30;
$limit = $limit > 150 ? 150 : $limit;
if ( $ibforums->input['task_id'] != -1 )
{
$DB->simple_construct( array( 'select' => '*', 'from' => 'task_logs', 'where' => "log_title='".$ibforums->input['task_id']."'", 'order' => 'log_date DESC', 'limit' => '0,'.$limit ) );
}
else
{
$DB->simple_construct( array( 'select' => '*', 'from' => 'task_logs', 'order' => 'log_date DESC', 'limit' => '0,'.$limit ) );
}
$DB->simple_exec();
$ibforums->adskin->td_header[] = array( "Task Run" , "20%" );
$ibforums->adskin->td_header[] = array( "Date Run" , "35%" );
$ibforums->adskin->td_header[] = array( "Log Info" , "45%" );
$ibforums->html .= $ibforums->adskin->start_table( "Selected Task Logs" );
if ( $DB->get_num_rows() )
{
while ( $row = $DB->fetch_row() )
{
$ibforums->html .= $ibforums->adskin->add_td_row( array( "{$row['log_title']}",
$ibforums->admin->get_date( $row['log_date'], 'SHORT' ),
"{$row['log_desc']}",
) );
}
}
else
{
$ibforums->html .= $ibforums->adskin->add_td_basic("No results");
}
$ibforums->html .= $ibforums->adskin->end_table();
$ibforums->admin->output();
}
//-----------------------------------------
// TASK LOG START
//-----------------------------------------
function task_log_setup()
{
global $ibforums, $DB, $std;
//-----------------------------------------
// Some set up
//-----------------------------------------
$tasks = array( 0 => array( -1, 'All tasks' ) );
$DB->simple_construct( array( 'select' => '*', 'from' => 'task_manager', 'order' => 'task_title' ) );
$DB->simple_exec();
while ( $pee = $DB->fetch_row() )
{
$tasks[] = array( $pee['task_title'], $pee['task_title'] );
}
//-----------------------------------------
// LAST FIVE ACTIONS
//-----------------------------------------
$DB->simple_construct( array( 'select' => '*', 'from' => 'task_logs', 'order' => 'log_date DESC', 'limit' => array(0,5) ) );
$DB->simple_exec();
$ibforums->adskin->td_header[] = array( "Task Run" , "20%" );
$ibforums->adskin->td_header[] = array( "Date Run" , "35%" );
$ibforums->adskin->td_header[] = array( "Log Info" , "45%" );
$ibforums->html .= $ibforums->adskin->start_table( "Last 5 Tasks Run" );
if ( $DB->get_num_rows() )
{
while ( $row = $DB->fetch_row() )
{
$ibforums->html .= $ibforums->adskin->add_td_row( array( "{$row['log_title']}",
$ibforums->admin->get_date( $row['log_date'], 'SHORT' ),
"{$row['log_desc']}",
) );
}
}
else
{
$ibforums->html .= $ibforums->adskin->add_td_basic("No results");
}
$ibforums->html .= $ibforums->adskin->end_table();
//-----------------------------------------
// Show more...
//-----------------------------------------
$ibforums->html .= $ibforums->adskin->start_form( array( 1 => array( 'act' , 'task' ),
2 => array( 'code' , 'showlog' ),
) );
$ibforums->adskin->td_header[] = array( " " , "60%" );
$ibforums->adskin->td_header[] = array( " " , "40%" );
$ibforums->html .= $ibforums->adskin->start_table( "View Task Logs" );
$ibforums->html .= $ibforums->adskin->add_td_row( array( "View logs for task:",
$ibforums->adskin->form_dropdown( 'task_id', $tasks )
) );
$ibforums->html .= $ibforums->adskin->add_td_row( array( "Show how many log entries?",
$ibforums->adskin->form_input( 'task_count', '30' )
) );
$ibforums->html .= $ibforums->adskin->end_form('View Logs');
$ibforums->html .= $ibforums->adskin->end_table();
//-----------------------------------------
// Delete...
//-----------------------------------------
$ibforums->html .= $ibforums->adskin->start_form( array( 1 => array( 'act' , 'task' ),
2 => array( 'code' , 'deletelog' ),
) );
$ibforums->adskin->td_header[] = array( " " , "60%" );
$ibforums->adskin->td_header[] = array( " " , "40%" );
$ibforums->html .= $ibforums->adskin->start_table( "DELETE Task Logs" );
$ibforums->html .= $ibforums->adskin->add_td_row( array( "Delete logs for task:",
$ibforums->adskin->form_dropdown( 'task_id', $tasks )
) );
$ibforums->html .= $ibforums->adskin->add_td_row( array( "Delete logs older than (in days)?",
$ibforums->adskin->form_input( 'task_prune', '30' )
) );
$ibforums->html .= $ibforums->adskin->end_form('DELETE Logs');
$ibforums->html .= $ibforums->adskin->end_table();
$ibforums->admin->output();
}
//-----------------------------------------
// RUN TASK
//-----------------------------------------
function run_task()
{
global $ibforums, $DB, $std;
if ( ! $ibforums->input['id'] )
{
$ibforums->main_msg = 'No ID was passed, cannot save';
$this->show_tasks();
}
$this_task = $DB->simple_exec_query( array( 'select' => '*', 'from' => 'task_manager', 'where' => 'task_id='.$ibforums->input['id'] ) );
if ( ! $this_task['task_id'] )
{
$ibforums->main_msg = 'No task to run.';
$this->show_tasks();
}
if ( ! $this_task['task_enabled'] )
{
$ibforums->main_msg = "This task has been disabled. Please enable the task before running it.";
$this->show_tasks();
}
//-----------------------------------------
// Get new instance of functions
//-----------------------------------------
$func = new task_functions();
$newdate = $func->generate_next_run($this_task);
$DB->do_update( 'task_manager', array( 'task_next_run' => $newdate ), "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();
$ibforums->main_msg = 'Task run successfully';
$this->show_tasks();
}
else
{
$ibforums->main_msg = 'Cannot locate: '.$func->root_path.'sources/tasks/'.$this_task['task_file'];
$this->show_tasks();
}
}
//-----------------------------------------
// DELETE TASK
//-----------------------------------------
function delete_task()
{
global $ibforums, $DB, $std;
if ( ! $ibforums->input['id'] )
{
$ibforums->main_msg = 'No ID was passed, cannot save';
$this->show_tasks();
}
$DB->simple_exec_query( array( 'delete' => 'task_manager', 'where' => 'task_id='.$ibforums->input['id'] ) );
$this->functions->save_next_run_stamp();
$ibforums->main_msg = 'Task deleted';
$this->show_tasks();
}
//-----------------------------------------
// DO SAVE
//-----------------------------------------
function do_save($type='edit')
{
global $ibforums, $DB, $std;
if ( $type == 'edit' )
{
if ( ! $ibforums->input['id'] )
{
$ibforums->main_msg = 'No ID was passed, cannot save';
$this->task_form();
}
}
if ( ! $ibforums->input['task_title'] )
{
$ibforums->main_msg = 'You must enter a task title.';
$this->task_form();
}
if ( ! $ibforums->input['task_file'] )
{
$ibforums->main_msg = 'You must enter a filename for this task to run';
$this->task_form();
}
//-----------------------------------------
// Compile task
//-----------------------------------------
$save = array( 'task_title' => $ibforums->input['task_title'],
'task_description' => $ibforums->input['task_description'],
'task_file' => $ibforums->input['task_file'],
'task_week_day' => $ibforums->input['task_week_day'],
'task_month_day' => $ibforums->input['task_month_day'],
'task_hour' => $ibforums->input['task_hour'],
'task_minute' => $ibforums->input['task_minute'],
'task_log' => $ibforums->input['task_log'],
'task_cronkey' => $ibforums->input['task_cronkey'] ? $ibforums->input['task_cronkey'] : md5(microtime()),
'task_enabled' => $ibforums->input['task_enabled'],
'task_key' => $ibforums->input['task_key'],
'task_safemode' => $ibforums->input['task_safemode'],
);
//-----------------------------------------
// Get next run date...
//-----------------------------------------
$save['task_next_run'] = $this->functions->generate_next_run( $save );
if ( $type == 'edit' )
{
$DB->do_update( 'task_manager', $save, 'task_id='.$ibforums->input['id'] );
$ibforums->main_msg = 'Task Edited Successfully';
}
else
{
$DB->do_insert( 'task_manager', $save );
$ibforums->main_msg = 'Task Saved Successfully';
}
$this->functions->save_next_run_stamp();
$this->show_tasks();
}
//-----------------------------------------
// EDIT TASK
//-----------------------------------------
function task_form($type='edit')
{
global $ibforums, $DB, $std;
if ( $type == 'edit' )
{
//-----------------------------------------
// Get task
//-----------------------------------------
$id = intval($ibforums->input['id']);
$this_task = $DB->simple_exec_query( array( 'select' => '*', 'from' => 'task_manager', 'where' => 'task_id='.$id ) );
$button = 'Edit this task';
$code = 'doedittask';
$title = 'Editing task: '.$this_task['task_title'];
}
else
{
$this_task = array();
$button = 'Add this task';
$code = 'doaddtask';
$title = 'Adding a new task';
}
//-----------------------------------------
// Create drop downs
//-----------------------------------------
$dd_minute = array( 0 => array( '-1', 'Every Minute' ) );
$dd_hour = array( 0 => array( '-1', 'Every Hour' ), 1 => array( '0', '0 - Midnight' ) );
$dd_wday = array( 0 => array( '-1', 'Every Week Day' ) );
$dd_mday = array( 0 => array( '-1', 'Every Day of the Month' ) );
$dd_month = array( 0 => array( '-1', 'Every Month' ) );
for( $i = 0 ; $i < 60; $i++ )
{
$dd_minute[] = array( $i, $i );
}
for( $i = 1 ; $i < 24; $i++ )
{
if ( $i < 12 )
{
$ampm = $i.' am';
}
else if ( $i == 12 )
{
$ampm = 'Midday';
}
else
{
$ampm = $i - 12 . ' pm';
}
$dd_hour[] = array( $i, $i. ' - ('.$ampm.')' );
}
for( $i = 1 ; $i < 32; $i++ )
{
$dd_mday[] = array( $i, $i );
}
$dd_wday[] = array( '0', 'Sunday' );
$dd_wday[] = array( '1', 'Monday' );
$dd_wday[] = array( '2', 'Tuesday' );
$dd_wday[] = array( '3', 'Wednesday' );
$dd_wday[] = array( '4', 'Thursday' );
$dd_wday[] = array( '5', 'Friday' );
$dd_wday[] = array( '6', 'Saturday' );
//-----------------------------------------
// START FORM
//-----------------------------------------
$ibforums->html .= $ibforums->adskin->start_form( array( 1 => array( 'act' , 'task' ),
2 => array( 'code' , $code ),
3 => array( 'id' , $id ),
4 => array( 'task_cronkey', $this_task['task_cronkey'] ),
) );
$ibforums->html .= "";
$input = "";
$ibforums->adskin->td_header[] = array( " " , "60%" );
$ibforums->adskin->td_header[] = array( " " , "40%" );
$ibforums->html .= $ibforums->adskin->start_table( "" );
$ibforums->html .= $ibforums->adskin->add_td_row( array( "Task Title",
$ibforums->adskin->form_input( 'task_title', $_POST['task_title'] ? $_POST['task_title'] : $this_task['task_title'] )
) );
$ibforums->html .= $ibforums->adskin->add_td_row( array( "Task Short Description",
$ibforums->adskin->form_input( 'task_description', $_POST['task_description'] ? $_POST['task_description'] : $this_task['task_description'] )
) );
$ibforums->html .= $ibforums->adskin->add_td_row( array( "Task PHP File To RunThis is the PHP file that is run when the task is run.
",
"./sources/tasks/ ".$ibforums->adskin->form_simple_input( 'task_file', $_POST['task_file'] ? $_POST['task_file'] : $this_task['task_file'], '20' )
) );
$ibforums->html .= $ibforums->adskin->add_td_row( array( "Task Time: MinutesChoose 'Every Minute' to run each minute or a number for a specific minute of an hour
",
$ibforums->adskin->form_dropdown( 'task_minute', $dd_minute, $_POST['task_minute'] ? $_POST['task_minute'] : $this_task['task_minute'], 'onchange="updatepreview()"' )
) );
$ibforums->html .= $ibforums->adskin->add_td_row( array( "Task Time: HoursChoose 'Every Hour' to run each hour or a number for a specific hour of a day
",
$ibforums->adskin->form_dropdown( 'task_hour', $dd_hour, $_POST['task_hour'] ? $_POST['task_hour'] : $this_task['task_hour'], 'onchange="updatepreview()"' )
) );
$ibforums->html .= $ibforums->adskin->add_td_row( array( "Task Time: Week DayChoose 'Every Day' to run each day or a week day for a specific week day of a month
",
$ibforums->adskin->form_dropdown( 'task_week_day', $dd_wday, $_POST['task_week_day'] ? $_POST['task_week_day'] : $this_task['task_week_day'], 'onchange="updatepreview()"' )
) );
$ibforums->html .= $ibforums->adskin->add_td_row( array( "Task Time: Month DayChoose 'Every Day' to run each day or a month day for a specific month day of a month
",
$ibforums->adskin->form_dropdown( 'task_month_day', $dd_mday, $_POST['task_month_day'] ? $_POST['task_month_day'] : $this_task['task_month_day'], 'onchange="updatepreview()"' )
) );
$ibforums->html .= $ibforums->adskin->add_td_row( array( "Enable Task LoggingWill write to the task log each time the task is run, not recommended for regular tasks run every few minutes.
",
$ibforums->adskin->form_yes_no( 'task_log', $_POST['task_log'] ? $_POST['task_log'] : $this_task['task_log'] )
) );
$ibforums->html .= $ibforums->adskin->add_td_row( array( "Enable Task?If you are using CRON, you might wish to disable this task from the internal manager.
",
$ibforums->adskin->form_yes_no( 'task_enabled', $_POST['task_enabled'] ? $_POST['task_enabled'] : $this_task['task_enabled'] )
) );
if ( IN_DEV )
{
$ibforums->html .= $ibforums->adskin->add_td_row( array( "Task KeyThis is used to call a task where the ID of the task might change
",
$ibforums->adskin->form_input( 'task_key', $_POST['task_key'] ? $_POST['task_key'] : $this_task['task_key'] )
) );
$ibforums->html .= $ibforums->adskin->add_td_row( array( "Task Safe ModeIf set to 'yes', this will not be editable by admins
",
$ibforums->adskin->form_yes_no( 'task_safemode', $_POST['task_safemode'] ? $_POST['task_safemode'] : $this_task['task_safemode'] )
) );
}
$ibforums->html .= $ibforums->adskin->end_table();
$ibforums->html .= "
";
//-----------------------------------------
$ibforums->admin->output();
}
//-----------------------------------------
// SHOW TASKS
//-----------------------------------------
function show_tasks()
{
global $ibforums, $DB, $std;
//-----------------------------------------
// REBUILD CACHES
//-----------------------------------------
$ibforums->html .= $ibforums->adskin->js_checkdelete("Are you sure you wish to remove this task?");
$ibforums->html .= $ibforums->adskin->start_form( array( 1 => array( 'act' , 'task' ),
2 => array( 'code' , 'addtask' ),
) );
$ibforums->adskin->td_header[] = array( "Title" , "40%" );
$ibforums->adskin->td_header[] = array( "Next Run" , "25%" );
$ibforums->adskin->td_header[] = array( "Min" , "5%" );
$ibforums->adskin->td_header[] = array( "Hour" , "5%" );
$ibforums->adskin->td_header[] = array( "MDay" , "5%" );
$ibforums->adskin->td_header[] = array( "WDay" , "5%" );
$ibforums->adskin->td_header[] = array( "Options" , "25%" );
$ibforums->html .= $ibforums->adskin->start_table( "Your scheduled tasks" );
$DB->simple_construct( array( 'select' => '*', 'from' => 'task_manager', 'order' => 'task_safemode, task_next_run' ) );
$DB->simple_exec();
while ( $row = $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'] )
{
$image = 'task_run_now.gif';
}
else
{
$image= 'task_run.gif';
}
$class = "";
$title = "";
$next_run = gmdate( 'jS F Y - h:i A', $row['task_next_run'] );
if ( $row['task_enabled'] != 1 )
{
$class = " style='color:gray'";
$title = " (Disabled)";
$next_run = "
$next_run";
}
if ( $row['task_safemode'] and ! IN_DEV )
{
$deletebutton = '--';
$editbutton = '--';
}
else
{
$deletebutton = "
";
$editbutton = $ibforums->adskin->js_make_button( 'Edit' , $ibforums->base_url.'&act=task&code=edit&id='.$row['task_id'], 'realbutton' );
}
$ibforums->html .= $ibforums->adskin->add_td_row( array( "
|
{$row['task_title']}{$title} {$row['task_description']}
curl -s -o /dev/null {$ibforums->vars['board_url']}/index.{$ibforums->vars['php_ext']}?act=task&ck={$row['task_cronkey']}
|
|
",
"
". $next_run ."",
"
".$row['task_minute'] ."",
"
".$row['task_hour'] ."",
"
".$row['task_month_day']."",
"
".$row['task_week_day'] ."",
"
{$editbutton} {$deletebutton}"
) );
}
$ibforums->html .= $ibforums->adskin->end_form("Add a new task");
$ibforums->html .= $ibforums->adskin->end_table();
$std->offset_set = 0;
$ibforums->html .= "
All times GMT. GMT time now is: ".gmdate( 'jS F Y - h:i A' )."
";
//-----------------------------------------
//-------------------------------
$ibforums->admin->output();
}
}
?>