2008-11-26 22:50:40 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* AgileBill - Open Billing Software
|
|
|
|
*
|
|
|
|
* This body of work is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the Open AgileBill License
|
|
|
|
* License as published at http://www.agileco.com/agilebill/license1-4.txt
|
2009-08-03 04:10:16 +00:00
|
|
|
*
|
|
|
|
* Originally authored by Tony Landis, AgileBill LLC
|
|
|
|
*
|
|
|
|
* Recent modifications by Deon George
|
|
|
|
*
|
|
|
|
* @author Deon George <deonATleenooksDOTnet>
|
|
|
|
* @copyright 2009 Deon George
|
|
|
|
* @link http://osb.leenooks.net
|
2008-11-26 22:50:40 +00:00
|
|
|
*
|
|
|
|
* @link http://www.agileco.com/
|
|
|
|
* @copyright 2004-2008 Agileco, LLC.
|
|
|
|
* @license http://www.agileco.com/agilebill/license1-4.txt
|
2009-08-03 04:10:16 +00:00
|
|
|
* @author Tony Landis <tony@agileco.com>
|
2008-11-26 22:50:40 +00:00
|
|
|
* @package AgileBill
|
2009-08-03 04:10:16 +00:00
|
|
|
* @subpackage Module:Task
|
2008-11-26 22:50:40 +00:00
|
|
|
*/
|
|
|
|
|
2009-08-03 04:10:16 +00:00
|
|
|
/**
|
|
|
|
* The main AgileBill Task Class
|
|
|
|
*
|
|
|
|
* @package AgileBill
|
|
|
|
* @subpackage Module:Task
|
2010-11-29 22:41:08 +00:00
|
|
|
* @todo Change the debug printing to use central debugging calls ie: C_alert
|
2009-08-03 04:10:16 +00:00
|
|
|
*/
|
|
|
|
class task extends OSB_module {
|
2010-11-29 22:41:08 +00:00
|
|
|
# Schedule Task ID
|
|
|
|
private $id = null;
|
|
|
|
# Does this task need authorisation
|
|
|
|
private $noauth = false;
|
|
|
|
|
2009-08-03 04:10:16 +00:00
|
|
|
/**
|
|
|
|
* Run all scheduled tasks
|
2010-11-29 22:41:08 +00:00
|
|
|
*
|
|
|
|
* @uses cron;
|
2009-08-03 04:10:16 +00:00
|
|
|
*/
|
|
|
|
public function run_all() {
|
2010-11-29 22:41:08 +00:00
|
|
|
include_once(PATH_INCLUDES.'cron/cron.inc.php');
|
|
|
|
$cron = new cron;
|
|
|
|
|
2009-08-03 04:10:16 +00:00
|
|
|
# Ensure that tasks complete and dont hang on running=1
|
|
|
|
set_time_limit(2*60*60);
|
2008-11-26 22:50:40 +00:00
|
|
|
|
|
|
|
# Loop through the tasks:
|
2010-11-29 22:41:08 +00:00
|
|
|
foreach ($this->sql_GetRecords(array('where'=>'(running=0 OR running IS NULL) AND status=1')) as $result) {
|
|
|
|
# Initialise the task
|
|
|
|
$this->id = $result['id'];
|
|
|
|
$this->noauth = false;
|
|
|
|
|
|
|
|
if ($this->debug)
|
|
|
|
printf("%s: Selecting Job [%s] (%s)\n",__METHOD__,$result['command'],$this->id);
|
|
|
|
|
|
|
|
$task = array();
|
2011-08-16 02:27:19 +00:00
|
|
|
$task['start'] = 0;
|
|
|
|
$task['end'] = 0;
|
2010-11-29 22:41:08 +00:00
|
|
|
$task['lastrun'] = (int)$result['date_run'];
|
|
|
|
$task['cron'] = sprintf('%s %s %s %s %s',
|
|
|
|
$result['int_min'],
|
|
|
|
$result['int_hour'],
|
|
|
|
$result['int_month_day'],
|
|
|
|
$result['int_month'],
|
|
|
|
$result['int_week_day']);
|
|
|
|
$task['now'] = (int)time();
|
|
|
|
|
|
|
|
# Default the last run time, if it isnt set
|
|
|
|
if (! $task['lastrun'] > 0)
|
|
|
|
$task['lastrun'] = $task['now']-86400*365;
|
|
|
|
|
|
|
|
# Verify it has not expired:
|
|
|
|
if ($task['start'] <= $task['now'] || $task['start'] == '' || $task['start'] == '0') {
|
|
|
|
# Verify it is past the start date:
|
|
|
|
if ($task['end'] >= $task['now'] || $task['end'] == '' || $task['end'] == '0') {
|
|
|
|
# Verify that it is time to run:
|
|
|
|
if ($cron->due($task['lastrun'],$task['now'],$task['cron'])) {
|
|
|
|
# Run the task:
|
|
|
|
if ($this->debug)
|
|
|
|
printf("%s: Running Job [%s] (%s)\n",__METHOD__,$result['command'],$this->id);
|
|
|
|
|
|
|
|
$this->run($this->VAR);
|
2008-11-26 22:50:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-03 04:10:16 +00:00
|
|
|
/**
|
|
|
|
* Run a task
|
|
|
|
*
|
|
|
|
* @uses task_log
|
|
|
|
*/
|
|
|
|
public function run($VAR) {
|
2010-11-29 22:41:08 +00:00
|
|
|
global $VAR,$C_auth,$_ENV,$_SERVER,$_COOKIE,$C_list,$C_method;
|
|
|
|
$db = &DB();
|
2008-11-26 22:50:40 +00:00
|
|
|
|
2010-11-29 22:41:08 +00:00
|
|
|
# Check if we are on a console, we'll debug output if we are
|
2009-08-03 04:10:16 +00:00
|
|
|
if (isset($_ENV['S']) ||
|
|
|
|
(isset($_ENV['SESSIONNAME']) && $_ENV['SESSIONNAME'] == 'Console') ||
|
|
|
|
(isset($_SERVER['SESSIONNAME']) && $_SERVER['SESSIONNAME'] == 'Console') ||
|
|
|
|
(isset($_SERVER['CLIENTNAME']) && $_SERVER['CLIENTNAME'] == 'Console') ||
|
|
|
|
empty($_COOKIE)) {
|
|
|
|
|
2010-11-29 22:41:08 +00:00
|
|
|
$this->debug = true;
|
|
|
|
$this->noauth = true;
|
2009-08-03 04:10:16 +00:00
|
|
|
|
2010-11-29 22:41:08 +00:00
|
|
|
# Can this task be run without authorisation
|
|
|
|
} elseif($C_auth->auth_method_by_name('task','run')) {
|
|
|
|
$this->noauth = true;
|
2008-11-26 22:50:40 +00:00
|
|
|
}
|
|
|
|
|
2010-11-29 22:41:08 +00:00
|
|
|
if (! isset($this->id)) {
|
|
|
|
if (isset($VAR['id']))
|
|
|
|
$this->id = $VAR['id'];
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
2008-11-26 22:50:40 +00:00
|
|
|
|
|
|
|
# Get task details
|
2010-11-29 22:41:08 +00:00
|
|
|
if (! $task = $this->sql_GetRecords(array('where'=>array('id'=>$this->id))))
|
|
|
|
return false;
|
|
|
|
$task = array_shift($task);
|
2008-11-26 22:50:40 +00:00
|
|
|
|
2009-08-03 04:10:16 +00:00
|
|
|
# Record task running
|
2010-11-29 22:41:08 +00:00
|
|
|
$db->Execute(sqlUpdate($db,'task',array('running'=>1),array('id'=>$this->id)));
|
|
|
|
|
|
|
|
if ($this->debug)
|
|
|
|
printf("%s: Running Task [%s]\n",__METHOD__,$task['command']);
|
2008-11-26 22:50:40 +00:00
|
|
|
|
|
|
|
# Run task
|
2010-11-29 22:41:08 +00:00
|
|
|
switch ($task['type']) {
|
|
|
|
# Internal function
|
2009-08-03 04:10:16 +00:00
|
|
|
case 0:
|
2010-11-29 22:41:08 +00:00
|
|
|
$cm = explode(':',$task['command']);
|
|
|
|
|
|
|
|
if ($this->noauth)
|
|
|
|
$C_method->exe_noauth($cm[0],$cm[1]);
|
|
|
|
else
|
|
|
|
$C_method->exe($cm[0],$cm[1]);
|
2008-11-26 22:50:40 +00:00
|
|
|
|
2009-08-03 04:10:16 +00:00
|
|
|
if ($C_method->result)
|
|
|
|
$result = 1;
|
|
|
|
else
|
|
|
|
$result = 0;
|
2008-11-26 22:50:40 +00:00
|
|
|
|
2010-11-29 22:41:08 +00:00
|
|
|
$message = isset($C_method->error) ? $C_method->error : '';
|
|
|
|
|
2009-08-03 04:10:16 +00:00
|
|
|
break;
|
|
|
|
|
2010-11-29 22:41:08 +00:00
|
|
|
# Run System Command:
|
2009-08-03 04:10:16 +00:00
|
|
|
case 1:
|
2010-11-29 22:41:08 +00:00
|
|
|
$message = `{$task['command']}`;
|
2008-11-26 22:50:40 +00:00
|
|
|
$result = 1;
|
2009-08-03 04:10:16 +00:00
|
|
|
break;
|
2008-11-26 22:50:40 +00:00
|
|
|
|
2009-08-03 04:10:16 +00:00
|
|
|
default:
|
2010-11-29 22:41:08 +00:00
|
|
|
printf('ERROR: Unknown task type [%s]',$result['type']);
|
2009-08-03 04:10:16 +00:00
|
|
|
}
|
2008-11-26 22:50:40 +00:00
|
|
|
|
|
|
|
# Update last run date & flip run toggle
|
2010-11-29 22:41:08 +00:00
|
|
|
$db->Execute(sqlUpdate($db,'task',array('running'=>0,'date_run'=>time()),array('id'=>$this->id)));
|
2008-11-26 22:50:40 +00:00
|
|
|
|
|
|
|
# Store task log if required
|
2010-11-29 22:41:08 +00:00
|
|
|
if ($task['log'] && $C_list->is_installed('task_log')) {
|
2009-08-03 04:10:16 +00:00
|
|
|
include_once(PATH_MODULES.'task_log/task_log.inc.php');
|
|
|
|
$tl = new task_log;
|
|
|
|
|
2010-11-29 22:41:08 +00:00
|
|
|
$tl->add(array(
|
|
|
|
'task_log_task_id'=>$this->id,
|
|
|
|
'task_log_result'=>$result,
|
|
|
|
'task_log_message'=>$message,
|
|
|
|
'_page_current'=>isset($VAR['_page_current']) ? $VAR['_page_current'] : '',
|
|
|
|
'_noredirect'=>1
|
|
|
|
));
|
2008-11-26 22:50:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# If admin, print success message
|
2009-08-03 04:10:16 +00:00
|
|
|
if (DEFAULT_ADMIN_THEME == SESS_THEME) {
|
|
|
|
global $C_translate,$C_debug;
|
2010-11-29 22:41:08 +00:00
|
|
|
|
2009-08-03 04:10:16 +00:00
|
|
|
$C_debug->alert($C_translate->translate('true','',''));
|
2008-11-26 22:50:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-08-03 04:10:16 +00:00
|
|
|
?>
|