2003-07-21 20:21:30 +02:00
< ? php
2009-04-03 09:02:08 +02:00
/**
2014-10-17 15:51:28 +02:00
* EGgroupware admin - Timed Asynchron Services
2009-04-03 09:02:08 +02:00
*
* @ link http :// www . egroupware . org
* @ author Ralf Becker < RalfBecker - AT - outdoor - training . de >
* @ package admin
* @ license http :// opensource . org / licenses / gpl - license . php GPL - GNU General Public License
* @ version $Id $
*/
2016-04-27 21:12:20 +02:00
use EGroupware\Api ;
use EGroupware\Api\Egw ;
2009-04-03 09:02:08 +02:00
/**
* Class to admin cron - job like timed calls of eGroupWare methods
*/
2016-05-14 21:04:03 +02:00
class admin_asyncservice
2009-04-03 09:02:08 +02:00
{
var $public_functions = array (
'index' => True ,
);
function index ()
2003-07-21 20:21:30 +02:00
{
2015-08-04 11:55:29 +02:00
if ( $GLOBALS [ 'egw' ] -> acl -> check ( 'asyncservice_acc' , 1 , 'admin' ))
2003-07-21 20:21:30 +02:00
{
2016-04-27 21:12:20 +02:00
Egw :: redirect_link ( '/index.php' );
2009-04-03 09:02:08 +02:00
}
$GLOBALS [ 'egw_info' ][ 'flags' ][ 'app_header' ] = lang ( 'Admin' ) . ' - ' . lang ( 'Asynchronous timed services' );
2008-03-25 17:37:37 +01:00
2016-05-14 21:04:03 +02:00
echo $GLOBALS [ 'egw' ] -> framework -> header ();
2003-07-21 20:21:30 +02:00
2021-03-28 20:48:55 +02:00
$async = new Api\Asyncservice (); // use an own instance, as we might set debug=True
2003-07-21 20:21:30 +02:00
2009-04-03 09:02:08 +02:00
$async -> debug = !! $_POST [ 'debug' ];
2003-07-21 20:21:30 +02:00
2009-04-03 09:02:08 +02:00
$units = array (
'year' => lang ( 'Year' ),
'month' => lang ( 'Month' ),
'day' => lang ( 'Day' ),
'dow' => lang ( 'Day of week<br>(0-6, 0=Sun)' ),
'hour' => lang ( 'Hour<br>(0-23)' ),
'min' => lang ( 'Minute' )
);
2003-07-21 20:21:30 +02:00
2009-04-03 09:02:08 +02:00
if ( $_POST [ 'send' ] || $_POST [ 'test' ] || $_POST [ 'cancel' ] || $_POST [ 'install' ] || $_POST [ 'deinstall' ] || $_POST [ 'update' ] || isset ( $_POST [ 'asyncservice' ]))
{
$times = array ();
foreach ( $units as $u => $ulabel )
2003-07-21 20:21:30 +02:00
{
2009-04-03 09:02:08 +02:00
if ( $_POST [ $u ] !== '' )
2003-07-21 20:21:30 +02:00
{
2009-04-03 09:02:08 +02:00
$times [ $u ] = $_POST [ $u ];
2003-07-21 20:21:30 +02:00
}
2009-04-03 09:02:08 +02:00
}
2003-07-21 20:21:30 +02:00
2009-04-03 09:02:08 +02:00
if ( $_POST [ 'test' ])
{
2013-07-04 15:29:22 +02:00
if ( strpos ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_email' ], '@' ) === false )
2010-02-03 04:40:32 +01:00
{
2018-09-20 11:44:09 +02:00
echo '<p><b>' . htmlspecialchars ( lang ( " You have no email address for your user set !!! " )) . " </b></p> \n " ;
2010-02-03 04:40:32 +01:00
}
2016-05-14 21:04:03 +02:00
elseif ( ! $async -> set_timer ( $times , 'test' , 'admin.admin_asyncservice.test' , $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_email' ]))
2003-07-21 20:21:30 +02:00
{
2018-09-20 11:44:09 +02:00
echo '<p><b>' . htmlspecialchars ( lang ( " Error setting timer, wrong syntax or maybe there's one already running !!! " )) . " </b></p> \n " ;
2003-07-21 20:21:30 +02:00
}
}
2009-04-03 09:02:08 +02:00
if ( $_POST [ 'cancel' ])
2003-07-21 20:21:30 +02:00
{
2009-04-03 09:02:08 +02:00
if ( ! $async -> cancel_timer ( 'test' ))
{
2018-09-20 11:44:09 +02:00
echo '<p><b>' . htmlspecialchars ( lang ( " Error canceling timer, maybe there's none set !!! " )) . " </b></p> \n " ;
2009-04-03 09:02:08 +02:00
}
2003-07-21 20:21:30 +02:00
}
2009-04-03 09:02:08 +02:00
if ( $_POST [ 'install' ] || $_POST [ 'deinstall' ])
2003-07-21 20:21:30 +02:00
{
2009-04-03 09:02:08 +02:00
if ( ! ( $install = $async -> install ( $_POST [ 'install' ] ? $times : False )))
2003-07-21 20:21:30 +02:00
{
2018-09-20 11:44:09 +02:00
echo '<p><b>' . htmlspecialchars ( lang ( 'Error: %1 not found or other error !!!' , $async -> crontab )) . " </b></p> \n " ;
2003-07-21 20:21:30 +02:00
}
2009-04-03 09:02:08 +02:00
$_POST [ 'asyncservice' ] = $_POST [ 'deinstall' ] ? 'fallback' : 'crontab' ;
2003-07-21 20:21:30 +02:00
}
2009-04-03 09:02:08 +02:00
}
else
{
$times = array ( 'min' => '*/5' ); // set some default
}
2016-05-14 21:04:03 +02:00
echo '<form action="' . $GLOBALS [ 'egw' ] -> link ( '/index.php' , array ( 'menuaction' => 'admin.admin_asyncservice.index' )) . '" method="POST">' . " \n <p> " ;
2009-04-03 09:02:08 +02:00
echo '<div style="text-align: left; margin: 10px;">' . " \n " ;
$last_run = $async -> last_check_run ();
2016-05-17 17:18:32 +02:00
$lr_date = $last_run [ 'end' ] ? Api\DateTime :: server2user ( $last_run [ 'end' ], '' ) : lang ( 'never' );
2018-09-20 11:44:09 +02:00
echo '<p><b>' . htmlspecialchars ( lang ( 'Async services last executed' )) . '</b>: ' .
$lr_date . ' (' . htmlspecialchars ( $last_run [ 'run_by' ]) . " )</p> \n <hr> \n " ;
2009-04-03 09:02:08 +02:00
if ( isset ( $_POST [ 'asyncservice' ]) && $_POST [ 'asyncservice' ] != $GLOBALS [ 'egw_info' ][ 'server' ][ 'asyncservice' ])
{
2016-04-27 21:12:20 +02:00
Api\Config :: save_value ( 'asyncservice' , $GLOBALS [ 'egw_info' ][ 'server' ][ 'asyncservice' ] = $_POST [ 'asyncservice' ], 'phpgwapi' );
2009-04-03 09:02:08 +02:00
}
if ( ! $async -> only_fallback )
{
$installed = $async -> installed ();
if ( is_array ( $installed ) && isset ( $installed [ 'cronline' ]))
2003-07-21 20:21:30 +02:00
{
2009-04-03 09:02:08 +02:00
$async_use [ 'cron' ] = lang ( 'crontab only (recomended)' );
2003-07-21 20:21:30 +02:00
}
2009-04-03 09:02:08 +02:00
}
$async_use [ '' ] = lang ( 'fallback (after each pageview)' );
$async_use [ 'off' ] = lang ( 'disabled (not recomended)' );
2018-09-20 11:44:09 +02:00
echo '<p><b>' . htmlspecialchars ( lang ( 'Run Asynchronous services' )) . '</b>' .
2009-04-03 09:02:08 +02:00
' <select name="asyncservice" onChange="this.form.submit();">' ;
foreach ( $async_use as $key => $label )
{
$selected = $key == $GLOBALS [ 'egw_info' ][ 'server' ][ 'asyncservice' ] ? ' selected' : '' ;
2018-09-20 11:44:09 +02:00
echo " <option value= \" $key\ " $selected > " .htmlspecialchars( $label ). " </ option > \n " ;
2009-04-03 09:02:08 +02:00
}
echo " </select> \n " ;
if ( is_array ( $installed ) && isset ( $installed [ 'cronline' ]))
{
2018-09-20 11:44:09 +02:00
echo ' <input type="submit" name="deinstall" value="' . htmlspecialchars ( lang ( 'Deinstall crontab' )) . " \" > \n " ;
2009-04-03 09:02:08 +02:00
}
echo " </p> \n " ;
if ( $async -> only_fallback )
{
2018-09-20 11:44:09 +02:00
echo '<p>' . 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 !!!' , '<a href="http://www.egroupware.org/wiki/TimedAsyncServicesWindows" target="_blank">' , '</a>' )) . " </p> \n " ;
2009-04-03 09:02:08 +02:00
}
else
{
2018-09-20 11:44:09 +02:00
echo '<p>' . htmlspecialchars ( lang ( 'Installed crontab' )) . " : \n " ;
2003-09-13 16:39:30 +02:00
if ( is_array ( $installed ) && isset ( $installed [ 'cronline' ]))
{
2009-04-03 09:02:08 +02:00
echo " $installed[cronline] </p> " ;
2003-09-13 16:39:30 +02:00
}
2009-04-03 09:02:08 +02:00
elseif ( $installed === 0 )
2003-07-21 20:21:30 +02:00
{
2018-09-20 11:44:09 +02:00
echo '<b>' . htmlspecialchars ( lang ( '%1 not found or not executable !!!' , $async -> crontab )) . " </b></p> \n " ;
2003-07-21 20:21:30 +02:00
}
else
{
2018-09-20 11:44:09 +02:00
echo '<b>' . htmlspecialchars ( lang ( 'asyncservices not yet installed or other error (%1) !!!' , $installed [ 'error' ])) . " </b></p> \n " ;
2003-07-21 20:21:30 +02:00
}
2018-09-20 11:44:09 +02:00
echo '<p><input type="submit" name="install" value="' . htmlspecialchars ( lang ( 'Install crontab' )) . " \" > \n " .
htmlspecialchars ( lang ( " for the times below (empty values count as '*', all empty = every minute) " )) . " </p> \n " ;
2009-04-03 09:02:08 +02:00
}
2003-07-21 20:21:30 +02:00
2009-04-03 09:02:08 +02:00
echo " <hr><table border=0><tr> \n " ;
foreach ( $units as $u => $ulabel )
{
2018-09-20 11:44:09 +02:00
echo " <td> $ulabel </td><td><input name= \" $u\ " value = \ " " . htmlspecialchars ( $times [ $u ]) . " \" size=5> </td> \n " ;
2009-04-03 09:02:08 +02:00
}
echo " </tr><tr> \n <td colspan=4> \n " ;
2018-09-20 11:44:09 +02:00
echo ' <input type="submit" name="send" value="' . htmlspecialchars ( lang ( 'Calculate next run' )) . '"></td>' . " \n " ;
2009-04-03 09:02:08 +02:00
echo ' <td colspan="8"><input type="checkbox" name="debug" value="1"' . ( $_POST [ 'debug' ] ? ' checked' : '' ) . " > \n " .
2018-09-20 11:44:09 +02:00
htmlspecialchars ( lang ( 'Enable debug-messages' )) . " </td> \n </tr></table> \n " ;
2003-07-21 20:21:30 +02:00
2009-04-03 09:02:08 +02:00
if ( $_POST [ 'send' ])
{
$next = $async -> next_run ( $times , True );
2003-07-21 20:21:30 +02:00
2018-09-20 11:44:09 +02:00
echo " <p>asyncservice::next_run( " . htmlspecialchars ( json_encode ( $times , JSON_UNESCAPED_SLASHES )) . " )= " . ( $next === False ? 'False' : " $next = " . Api\DateTime :: server2user ( $next , '' )) . " </p> \n " ;
2009-04-03 09:02:08 +02:00
}
2018-09-20 11:44:09 +02:00
echo '<hr><p><input type="submit" name="cancel" value="' . htmlspecialchars ( lang ( 'Cancel TestJob!' )) . " \" > \n " ;
echo '<input type="submit" name="test" value="' . htmlspecialchars ( lang ( 'Start TestJob!' )) . " \" > \n " ;
2009-04-03 09:02:08 +02:00
echo lang ( 'for the times above' ) . " </p> \n " ;
echo '<p>' . lang ( 'The TestJob sends you a mail everytime it is called.' ) . " </p> \n " ;
2003-07-21 20:21:30 +02:00
2009-04-03 09:02:08 +02:00
echo '<hr><p><b>' . lang ( 'Jobs' ) . " :</b> \n " ;
2014-01-28 10:56:12 +01:00
if (( $jobs = $async -> read ( '%' )))
2009-04-03 09:02:08 +02:00
{
2016-05-17 17:18:32 +02:00
echo " <table border=1> \n <tr> \n <th>Id</th><th style='width:18ex;'> " . lang ( 'Next run' ) . '</th><th>' . lang ( 'Times' ) . '</th><th>' . lang ( 'Method' ) . '</th><th>' . lang ( 'Data' ) . " </th><th> " . lang ( 'LoginID' ) . " </th></tr> \n " ;
2009-04-03 09:02:08 +02:00
foreach ( $jobs as $job )
2003-07-21 20:21:30 +02:00
{
2018-09-20 11:44:09 +02:00
echo " <tr> \n <td> $job[id] </td><td> " . Api\DateTime :: server2user ( $job [ 'next' ], '' ) . " </td> \n " ;
echo " <td> " . htmlspecialchars ( json_encode ( $job [ 'times' ], JSON_UNESCAPED_SLASHES )) . " </td> \n " ;
echo " </td><td> " . htmlspecialchars ( str_replace ( 'EGroupware\\' , '' , $job [ 'method' ])) . " </td> \n <td " ;
$data = is_array ( $job [ 'data' ]) ? json_encode ( $job [ 'data' ], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) : $job [ 'data' ];
if ( strlen ( $data ) >= 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 ) . " </td> \n " ;
echo " <td align= \" center \" > " . htmlspecialchars ( $GLOBALS [ 'egw' ] -> accounts -> id2name ( $job [ 'account_id' ])) . " </td></tr> \n " ;
2003-07-21 20:21:30 +02:00
}
2009-04-03 09:02:08 +02:00
echo " </table> \n " ;
2003-07-21 20:21:30 +02:00
}
2009-04-03 09:02:08 +02:00
else
2003-07-21 20:21:30 +02:00
{
2009-04-03 09:02:08 +02:00
echo lang ( 'No jobs in the database !!!' ) . " </p> \n " ;
}
2018-09-20 11:44:09 +02:00
echo '<p><input type="submit" name="update" value="' . htmlspecialchars ( lang ( 'Update' )) . '"></p>' . " \n " ;
2009-04-03 09:02:08 +02:00
echo " </form> \n " ;
2016-05-14 21:04:03 +02:00
echo $GLOBALS [ 'egw' ] -> framework -> footer ();
2009-04-03 09:02:08 +02:00
}
2016-06-08 15:15:01 +02:00
/**
* Callback for test - job
*
* @ param string $to email address to send mail to
*/
2009-04-03 09:02:08 +02:00
function test ( $to )
{
2016-06-08 15:15:01 +02:00
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 );
2003-07-21 20:21:30 +02:00
}
}
2009-04-03 09:02:08 +02:00
}