* @copyright 2009 Deon George * @link http://osb.leenooks.net * * @link http://www.agileco.com/ * @copyright 2004-2008 Agileco, LLC. * @license http://www.agileco.com/agilebill/license1-4.txt * @author Tony Landis * @package AgileBill * @subpackage Module:Task */ /** * The main AgileBill Task Class * * @package AgileBill * @subpackage Module:Task */ class task extends OSB_module { /** * Run all scheduled tasks */ public function run_all() { # Ensure that tasks complete and dont hang on running=1 set_time_limit(2*60*60); # Loop through the tasks: global $VAR; $db = &DB(); $result = $db->Execute(sqlSelect($db,'task','*','(running=0 OR running IS NULL) AND status=1')); if ($result && $result->RecordCount() > 0) { include_once(PATH_INCLUDES.'cron/cron.inc.php'); $cron = new cron; while (! $result->EOF) { $_r = false; $_s = (int) $result->fields['date_start']; $_e = (int) $result->fields['date_expire']; $_l = (int) $result->fields['date_run']; $_c = sprintf('%s %s %s %s %s', $result->fields['int_min'], $result->fields['int_hour'], $result->fields['int_month_day'], $result->fields['int_month'], $result->fields['int_week_day']); $_n = (int) time(); if (! $_l > 0) $_l = $_n-86400*365; # Verify it has not expired: if ($_s <= $_n || $_s == '' || $_s == '0') { # Verify it is past the start date: if ($_e >= $_n || $_e == '' || $_e == '0') { # Verify that it is time to run: if ($cron->due($_l,$_n,$_c)) { # Run the task: $this->id = $result->fields['id']; $this->run($VAR); } } } $result->MoveNext(); } } } /** * Run a task * * @uses task_log */ public function run($VAR) { global $VAR,$C_auth,$_ENV,$_SERVER,$_COOKIE,$C_list; $noauth = false; $debug_out = false; if (isset($_ENV['S']) || (isset($_ENV['SESSIONNAME']) && $_ENV['SESSIONNAME'] == 'Console') || (isset($_SERVER['SESSIONNAME']) && $_SERVER['SESSIONNAME'] == 'Console') || (isset($_SERVER['CLIENTNAME']) && $_SERVER['CLIENTNAME'] == 'Console') || empty($_COOKIE)) { $debug_out = true; $noauth = true; } elseif($C_auth->auth_method_by_name('task','run')) { $noauth = true; } else { $noauth = false; } if (isset($this->id)) $id = $this->id; elseif (isset($VAR['id'])) $id = $VAR['id']; else return; # Get task details $db = &DB(); $result = $db->Execute(sqlSelect($db,'task','*',array('id'=>$id))); if (! $result || $result->RecordCount() == 0) return; $type = $result->fields['type']; $cmd = $result->fields['command']; $log = $result->fields['log']; # Record task running $db->Execute(sqlUpdate($db,'task',array('running'=>1),array('id'=>$id))); # Run task switch ($type) { case 0: # Internal function: global $C_method; $arr = explode(":",$cmd); if ($noauth) { # run from console, no auth req $C_method->exe_noauth($arr[0],$arr[1]); } else { # run from web, auth required $C_method->exe($arr[0],$arr[1]); } if ($C_method->result) $result = 1; else $result = 0; @$message = $C_method->error; break; case 1: $message = `$cmd`; $result = 1; break; default: printf('ERROR: Unknown task type [%s]',$type); } # Update last run date & flip run toggle $db->Execute(sqlUpdate($db,'task',array('running'=>0,'date_run'=>time()),array('id'=>$id))); # Store task log if required if ($log && $C_list->is_installed('task_log')) { include_once(PATH_MODULES.'task_log/task_log.inc.php'); $tl = new task_log; $VAR['task_log_task_id'] = $id; $VAR['task_log_result'] = $result; $VAR['task_log_message'] = $message; if (! isset($VAR['_page_current'])) $VAR['_page_current'] = ''; $result = $tl->add($VAR); } # If admin, print success message if (DEFAULT_ADMIN_THEME == SESS_THEME) { global $C_translate,$C_debug; $C_debug->alert($C_translate->translate('true','','')); } } } ?>