* @package admin * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License * @version $Id$ */ use EGroupware\Api; use EGroupware\Api\Egw; /** * Class to admin cron-job like timed calls of eGroupWare methods */ class admin_asyncservice { var $public_functions = array( 'index' => True, ); function index() { if ($GLOBALS['egw']->acl->check('asyncservice_acc',1,'admin')) { Egw::redirect_link('/index.php'); } $GLOBALS['egw_info']['flags']['app_header'] = lang('Admin').' - '.lang('Asynchronous timed services'); echo $GLOBALS['egw']->framework->header(); $async = new Api\Asyncservice(); // use an own instance, as we might set debug=True $async->debug = !!$_POST['debug']; $units = array( 'year' => lang('Year'), 'month' => lang('Month'), 'day' => lang('Day'), 'dow' => lang('Day of week
(0-6, 0=Sun)'), 'hour' => lang('Hour
(0-23)'), 'min' => lang('Minute') ); if ($_POST['send'] || $_POST['test'] || $_POST['cancel'] || $_POST['install'] || $_POST['deinstall'] || $_POST['update'] || isset($_POST['asyncservice'])) { $times = array(); foreach($units as $u => $ulabel) { if ($_POST[$u] !== '') { $times[$u] = $_POST[$u]; } } if ($_POST['test']) { if (strpos($GLOBALS['egw_info']['user']['account_email'],'@') === false) { echo '

'.htmlspecialchars(lang("You have no email address for your user set !!!"))."

\n"; } elseif (!$async->set_timer($times,'test','admin.admin_asyncservice.test',$GLOBALS['egw_info']['user']['account_email'])) { echo '

'.htmlspecialchars(lang("Error setting timer, wrong syntax or maybe there's one already running !!!"))."

\n"; } } if ($_POST['cancel']) { if (!$async->cancel_timer('test')) { echo '

'.htmlspecialchars(lang("Error canceling timer, maybe there's none set !!!"))."

\n"; } } if ($_POST['install'] || $_POST['deinstall']) { if (!($install = $async->install($_POST['install'] ? $times : False))) { echo '

'.htmlspecialchars(lang('Error: %1 not found or other error !!!',$async->crontab))."

\n"; } $_POST['asyncservice'] = $_POST['deinstall'] ? 'fallback' : 'crontab'; } } else { $times = array('min' => '*/5'); // set some default } echo '
'."\n

"; echo '

'."\n"; $last_run = $async->last_check_run(); $lr_date = $last_run['end'] ? Api\DateTime::server2user($last_run['end'],'') : lang('never'); echo '

'. htmlspecialchars(lang('Async services last executed')).': '. $lr_date.' ('.htmlspecialchars($last_run['run_by']).")

\n
\n"; if (isset($_POST['asyncservice']) && $_POST['asyncservice'] != $GLOBALS['egw_info']['server']['asyncservice']) { Api\Config::save_value('asyncservice', $GLOBALS['egw_info']['server']['asyncservice']=$_POST['asyncservice'], 'phpgwapi'); } if (!$async->only_fallback) { $installed = $async->installed(); if (is_array($installed) && isset($installed['cronline'])) { $async_use['cron'] = lang('crontab only (recomended)'); } } $async_use[''] = lang('fallback (after each pageview)'); $async_use['off'] = lang('disabled (not recomended)'); echo '

'.htmlspecialchars(lang('Run Asynchronous services')).''. ' \n"; if (is_array($installed) && isset($installed['cronline'])) { echo '   \n"; } echo "

\n"; if ($async->only_fallback) { echo '

'.htmlspecialchars(lang('Under windows you need to install the asyncservice %1manually%2 or use the fallback mode. Fallback means the jobs get only checked after each page-view !!!','',''))."

\n"; } else { echo '

'.htmlspecialchars(lang('Installed crontab')).": \n"; if (is_array($installed) && isset($installed['cronline'])) { echo "$installed[cronline]

"; } elseif ($installed === 0) { echo ''.htmlspecialchars(lang('%1 not found or not executable !!!',$async->crontab))."

\n"; } else { echo ''.htmlspecialchars(lang('asyncservices not yet installed or other error (%1) !!!',$installed['error']))."

\n"; } echo '

\n". htmlspecialchars(lang("for the times below (empty values count as '*', all empty = every minute)"))."

\n"; } echo "
\n"; foreach ($units as $u => $ulabel) { echo " \n"; } echo "\n '."\n"; echo ' \n
$ulabel  
\n"; echo ' \n". htmlspecialchars(lang('Enable debug-messages'))."
\n"; if ($_POST['send']) { $next = $async->next_run($times,True); echo "

asyncservice::next_run(". htmlspecialchars(json_encode($times, JSON_UNESCAPED_SLASHES)).")=".($next === False ? 'False':"$next=".Api\DateTime::server2user($next,''))."

\n"; } echo '

 \n"; echo '\n"; echo lang('for the times above')."

\n"; echo '

'.lang('The TestJob sends you a mail everytime it is called.')."

\n"; echo '

'.lang('Jobs').":\n"; if (($jobs = $async->read('%'))) { echo "\n\n\n"; foreach($jobs as $job) { echo "\n\n"; echo "\n"; echo "\n= 64) { echo ' title="'.htmlspecialchars(json_encode($job['data'], JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT)).'"'; $data = substr($data, 0, 60).'...'; } echo ">". htmlspecialchars($data)."\n"; echo "\n"; } echo "
Id".lang('Next run').''.lang('Times').''.lang('Method').''.lang('Data')."".lang('LoginID')."
$job[id]".Api\DateTime::server2user($job['next'],'')."".htmlspecialchars(json_encode($job['times'], JSON_UNESCAPED_SLASHES))."".htmlspecialchars(str_replace('EGroupware\\', '', $job['method']))."".htmlspecialchars($GLOBALS['egw']->accounts->id2name($job['account_id']))."
\n"; } else { echo lang('No jobs in the database !!!')."

\n"; } echo '

'."\n"; echo "\n"; echo $GLOBALS['egw']->framework->footer(); } /** * Callback for test-job * * @param string $to email address to send mail to */ function test($to) { try { $mail = new Api\Mailer(); $mail->setBody('Greetings from cron ;-)'); $mail->addHeader('Subject', 'Asynchronous timed services'); $mail->addAddress($to); $mail->send(); } catch (Exception $e) { _egw_log_exception($e); } } }