2003-07-21 20:21:30 +02:00
< ? php
/************************************************************************** \
2004-01-27 00:26:19 +01:00
* eGroupWare Admin - Timed Asynchron Services for eGroupWare *
2003-07-21 20:21:30 +02:00
* Written by Ralf Becker < RalfBecker @ outdoor - training . de > *
2004-01-27 00:26:19 +01:00
* Class to admin cron - job like timed calls of eGroupWare methods *
2003-07-21 20:21:30 +02:00
* -------------------------------------------------------------------------*
2004-01-27 00:26:19 +01:00
* This library is part of the eGroupWare API *
* http :// www . egroupware . org / *
2003-07-21 20:21:30 +02:00
* ------------------------------------------------------------------------ *
* This library is free software ; you can redistribute it and / or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation ; either version 2.1 of the License , *
* or any later version . *
* This library is distributed in the hope that it will be useful , but *
* WITHOUT ANY WARRANTY ; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . *
* See the GNU Lesser General Public License for more details . *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library ; if not , write to the Free Software Foundation , *
* Inc . , 59 Temple Place , Suite 330 , Boston , MA 02111 - 1307 USA *
\ **************************************************************************/
/* $Id$ */
class uiasyncservice
{
var $public_functions = array (
'index' => True ,
);
function index ()
{
2005-10-14 19:03:16 +02:00
if ( $GLOBALS [ 'egw' ] -> acl -> check ( 'asyncservice_access' , 1 , 'admin' ))
2003-07-21 20:21:30 +02:00
{
2005-10-14 19:03:16 +02:00
$GLOBALS [ 'egw' ] -> redirect_link ( '/index.php' );
2003-07-21 20:21:30 +02:00
}
2005-10-14 19:03:16 +02:00
$GLOBALS [ 'egw_info' ][ 'flags' ][ 'app_header' ] = lang ( 'Admin' ) . ' - ' . lang ( 'Asynchronous timed services' );
2008-03-25 17:37:37 +01:00
2005-10-14 19:03:16 +02:00
$GLOBALS [ 'egw' ] -> js -> validate_file ( 'jscode' , 'openwindow' , 'admin' );
$GLOBALS [ 'egw' ] -> common -> egw_header ();
2003-07-21 20:21:30 +02:00
echo parse_navbar ();
2005-10-14 19:03:16 +02:00
$async = $GLOBALS [ 'egw' ] -> asyncservice ; // use an own instance, as we might set debug=True
2003-07-21 20:21:30 +02:00
$async -> debug = !! $_POST [ 'debug' ];
$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-09-13 16:39:30 +02:00
if ( $_POST [ 'send' ] || $_POST [ 'test' ] || $_POST [ 'cancel' ] || $_POST [ 'install' ] || $_POST [ 'deinstall' ] || $_POST [ 'update' ] || isset ( $_POST [ 'asyncservice' ]))
2003-07-21 20:21:30 +02:00
{
$times = array ();
foreach ( $units as $u => $ulabel )
{
if ( $_POST [ $u ] !== '' )
{
$times [ $u ] = $_POST [ $u ];
}
}
if ( $_POST [ 'test' ])
{
2005-10-14 19:03:16 +02:00
if ( ! $async -> set_timer ( $times , 'test' , 'admin.uiasyncservice.test' , $GLOBALS [ 'egw_info' ][ 'user' ][ 'email' ]))
2003-07-21 20:21:30 +02:00
{
echo '<p><b>' . lang ( " Error setting timer, wrong syntax or maybe there's one already running !!! " ) . " </b></p> \n " ;
}
}
if ( $_POST [ 'cancel' ])
{
if ( ! $async -> cancel_timer ( 'test' ))
{
echo '<p><b>' . lang ( " Error canceling timer, maybe there's none set !!! " ) . " </b></p> \n " ;
}
}
2003-09-13 16:39:30 +02:00
if ( $_POST [ 'install' ] || $_POST [ 'deinstall' ])
2003-07-21 20:21:30 +02:00
{
2003-09-13 16:39:30 +02:00
if ( ! ( $install = $async -> install ( $_POST [ 'install' ] ? $times : False )))
2003-07-21 20:21:30 +02:00
{
echo '<p><b>' . lang ( 'Error: %1 not found or other error !!!' , $async -> crontab ) . " </b></p> \n " ;
}
2003-09-13 16:39:30 +02:00
$_POST [ 'asyncservice' ] = $_POST [ 'deinstall' ] ? 'fallback' : 'crontab' ;
2003-07-21 20:21:30 +02:00
}
}
else
{
$times = array ( 'min' => '*/5' ); // set some default
}
2005-10-14 19:03:16 +02:00
echo '<form action="' . $GLOBALS [ 'egw' ] -> link ( '/index.php' , array ( 'menuaction' => 'admin.uiasyncservice.index' )) . '" method="POST">' . " \n <p> " ;
2003-07-21 20:21:30 +02:00
echo '<div style="text-align: left; margin: 10px;">' . " \n " ;
$last_run = $async -> last_check_run ();
2005-10-14 19:03:16 +02:00
$lr_date = $last_run [ 'end' ] ? $GLOBALS [ 'egw' ] -> common -> show_date ( $last_run [ 'end' ]) : lang ( 'never' );
2003-07-21 20:21:30 +02:00
echo '<p><b>' . lang ( 'Async services last executed' ) . '</b>: ' . $lr_date . ' (' . $last_run [ 'run_by' ] . " )</p> \n <hr> \n " ;
2005-10-14 19:03:16 +02:00
if ( isset ( $_POST [ 'asyncservice' ]) && $_POST [ 'asyncservice' ] != $GLOBALS [ 'egw_info' ][ 'server' ][ 'asyncservice' ])
2003-07-21 20:21:30 +02:00
{
2005-10-14 19:03:16 +02:00
$config =& CreateObject ( 'phpgwapi.config' , 'phpgwapi' );
2003-07-21 20:21:30 +02:00
$config -> read_repository ();
2005-10-14 19:03:16 +02:00
$config -> value ( 'asyncservice' , $GLOBALS [ 'egw_info' ][ 'server' ][ 'asyncservice' ] = $_POST [ 'asyncservice' ]);
2003-07-21 20:21:30 +02:00
$config -> save_repository ();
unset ( $config );
}
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 '<p><b>' . lang ( 'Run Asynchronous services' ) . '</b>' .
' <select name="asyncservice" onChange="this.form.submit();">' ;
foreach ( $async_use as $key => $label )
{
2005-10-14 19:03:16 +02:00
$selected = $key == $GLOBALS [ 'egw_info' ][ 'server' ][ 'asyncservice' ] ? ' selected' : '' ;
2003-07-21 20:21:30 +02:00
echo " <option value= \" $key\ " $selected > $label </ option > \n " ;
}
2003-09-13 16:39:30 +02:00
echo " </select> \n " ;
if ( is_array ( $installed ) && isset ( $installed [ 'cronline' ]))
{
echo ' <input type="submit" name="deinstall" value="' . lang ( 'Deinstall crontab' ) . " \" > \n " ;
}
echo " </p> \n " ;
2003-07-21 20:21:30 +02:00
if ( $async -> only_fallback )
{
2004-07-31 21:37:58 +02:00
echo '<p>' . 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 " ;
2003-07-21 20:21:30 +02:00
}
else
{
echo '<p>' . lang ( 'Installed crontab' ) . " : \n " ;
if ( is_array ( $installed ) && isset ( $installed [ 'cronline' ]))
{
echo " $installed[cronline] </p> " ;
}
elseif ( $installed === 0 )
{
echo '<b>' . lang ( '%1 not found or not executable !!!' , $async -> crontab ) . " </b></p> \n " ;
}
else
{
echo '<b>' . lang ( 'asyncservices not yet installed or other error (%1) !!!' , $installed [ 'error' ]) . " </b></p> \n " ;
}
echo '<p><input type="submit" name="install" value="' . lang ( 'Install crontab' ) . " \" > \n " .
lang ( " for the times below (empty values count as '*', all empty = every minute) " ) . " </p> \n " ;
}
echo " <hr><table border=0><tr> \n " ;
foreach ( $units as $u => $ulabel )
{
echo " <td> $ulabel </td><td><input name= \" $u\ " value = \ " $times[$u] \" size=5> </td> \n " ;
}
echo " </tr><tr> \n <td colspan=4> \n " ;
echo ' <input type="submit" name="send" value="' . lang ( 'Calculate next run' ) . '"></td>' . " \n " ;
echo ' <td colspan="8"><input type="checkbox" name="debug" value="1"' . ( $_POST [ 'debug' ] ? ' checked' : '' ) . " > \n " .
lang ( 'Enable debug-messages' ) . " </td> \n </tr></table> \n " ;
if ( $_POST [ 'send' ])
{
$next = $async -> next_run ( $times , True );
2005-10-14 19:03:16 +02:00
echo " <p>asyncservice::next_run( " ; print_r ( $times ); echo " )= " . ( $next === False ? 'False' : " ' $next '= " . $GLOBALS [ 'egw' ] -> common -> show_date ( $next )) . " </p> \n " ;
2003-07-21 20:21:30 +02:00
}
echo '<hr><p><input type="submit" name="cancel" value="' . lang ( 'Cancel TestJob!' ) . " \" > \n " ;
echo '<input type="submit" name="test" value="' . lang ( 'Start TestJob!' ) . " \" > \n " ;
echo lang ( 'for the times above' ) . " </p> \n " ;
echo '<p>' . lang ( 'The TestJob sends you a mail everytime it is called.' ) . " </p> \n " ;
echo '<hr><p><b>' . lang ( 'Jobs' ) . " :</b> \n " ;
if ( $jobs = $async -> read ( '%' ))
{
echo " <table border=1> \n <tr> \n <th>Id</th><th> " . lang ( 'Next run' ) . '</th><th>' . lang ( 'Times' ) . '</th><th>' . lang ( 'Method' ) . '</th><th>' . lang ( 'Data' ) . " </th><th> " . lang ( 'LoginID' ) . " </th></tr> \n " ;
foreach ( $jobs as $job )
{
2005-10-14 19:03:16 +02:00
echo " <tr> \n <td> $job[id] </td><td> " . $GLOBALS [ 'egw' ] -> common -> show_date ( $job [ 'next' ]) . " </td><td> " ;
2003-07-21 20:21:30 +02:00
print_r ( $job [ 'times' ]);
echo " </td><td> $job[method] </td><td> " ;
print_r ( $job [ 'data' ]);
2005-10-14 19:03:16 +02:00
echo " </td><td align= \" center \" > " . $GLOBALS [ 'egw' ] -> accounts -> id2name ( $job [ account_id ]) . " </td></tr> \n " ;
2003-07-21 20:21:30 +02:00
}
echo " </table> \n " ;
}
else
{
echo lang ( 'No jobs in the database !!!' ) . " </p> \n " ;
}
echo '<p><input type="submit" name="update" value="' . lang ( 'Update' ) . '"></p>' . " \n " ;
echo " </form> \n " ;
}
function test ( $to )
{
2005-10-14 19:03:16 +02:00
$returncode = $GLOBALS [ 'egw' ] -> send -> msg ( 'email' , $to , $subject = 'Asynchronous timed services' , 'Greatings from cron ;-)' );
2003-07-21 20:21:30 +02:00
if ( ! $returncode ) // not nice, but better than failing silently
{
echo " <p>bocalendar::send_update: sending message to ' $to ' subject=' $subject ' failed !!!<br> \n " ;
2005-10-14 19:03:16 +02:00
echo $GLOBALS [ 'egw' ] -> send -> err [ 'desc' ] . " </p> \n " ;
2003-07-21 20:21:30 +02:00
}
2005-10-14 19:03:16 +02:00
//print_r($GLOBALS['egw_info']['user']);
2003-07-21 20:21:30 +02:00
}
}