This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/modules/task/task.inc.php

182 lines
4.4 KiB
PHP
Raw Normal View History

<?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
*
* @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>
* @package AgileBill
2009-08-03 04:10:16 +00:00
* @subpackage Module:Task
*/
2009-08-03 04:10:16 +00:00
/**
* 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;
2009-08-03 04:10:16 +00:00
$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;
2009-08-03 04:10:16 +00:00
while (! $result->EOF) {
$_r = false;
$_s = (int) $result->fields['date_start'];
$_e = (int) $result->fields['date_expire'];
2009-08-03 04:10:16 +00:00
$_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();
2009-08-03 04:10:16 +00:00
if (! $_l > 0)
$_l = $_n-86400*365;
# Verify it has not expired:
2009-08-03 04:10:16 +00:00
if ($_s <= $_n || $_s == '' || $_s == '0') {
# Verify it is past the start date:
2009-08-03 04:10:16 +00:00
if ($_e >= $_n || $_e == '' || $_e == '0') {
# Verify that it is time to run:
2009-08-03 04:10:16 +00:00
if ($cron->due($_l,$_n,$_c)) {
# Run the task:
$this->id = $result->fields['id'];
2009-08-03 04:10:16 +00:00
$this->run($VAR);
}
}
}
$result->MoveNext();
}
}
}
2009-08-03 04:10:16 +00:00
/**
* Run a task
*
* @uses task_log
*/
public function run($VAR) {
global $VAR,$C_auth,$_ENV,$_SERVER,$_COOKIE,$C_list;
$noauth = false;
$debug_out = false;
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)) {
$debug_out = true;
$noauth = true;
2009-08-03 04:10:16 +00:00
} elseif($C_auth->auth_method_by_name('task','run')) {
$noauth = true;
2009-08-03 04:10:16 +00:00
} else {
$noauth = false;
}
if (isset($this->id))
2009-08-03 04:10:16 +00:00
$id = $this->id;
elseif (isset($VAR['id']))
$id = $VAR['id'];
else
return;
# Get task details
2009-08-03 04:10:16 +00:00
$db = &DB();
$result = $db->Execute(sqlSelect($db,'task','*',array('id'=>$id)));
2009-08-03 04:10:16 +00:00
if (! $result || $result->RecordCount() == 0)
return;
$type = $result->fields['type'];
$cmd = $result->fields['command'];
$log = $result->fields['log'];
2009-08-03 04:10:16 +00:00
# Record task running
$db->Execute(sqlUpdate($db,'task',array('running'=>1),array('id'=>$id)));
# Run task
2009-08-03 04:10:16 +00:00
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]);
}
2009-08-03 04:10:16 +00:00
if ($C_method->result)
$result = 1;
else
$result = 0;
2009-08-03 04:10:16 +00:00
@$message = $C_method->error;
break;
case 1:
$message = `$cmd`;
$result = 1;
2009-08-03 04:10:16 +00:00
break;
2009-08-03 04:10:16 +00:00
default:
printf('ERROR: Unknown task type [%s]',$type);
}
# Update last run date & flip run toggle
2009-08-03 04:10:16 +00:00
$db->Execute(sqlUpdate($db,'task',array('running'=>0,'date_run'=>time()),array('id'=>$id)));
# Store task log if required
2009-08-03 04:10:16 +00:00
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
2009-08-03 04:10:16 +00:00
if (DEFAULT_ADMIN_THEME == SESS_THEME) {
global $C_translate,$C_debug;
$C_debug->alert($C_translate->translate('true','',''));
}
}
}
2009-08-03 04:10:16 +00:00
?>