1 | <?php |
---|
2 | /**************************************************************************\ |
---|
3 | * eGroupWare Admin - Timed Asynchron Services for eGroupWare * |
---|
4 | * Written by Ralf Becker <RalfBecker@outdoor-training.de> * |
---|
5 | * Class to admin cron-job like timed calls of eGroupWare methods * |
---|
6 | * -------------------------------------------------------------------------* |
---|
7 | * This library is part of the eGroupWare API * |
---|
8 | * http://www.egroupware.org/ * |
---|
9 | * ------------------------------------------------------------------------ * |
---|
10 | * This library is free software; you can redistribute it and/or modify it * |
---|
11 | * under the terms of the GNU Lesser General Public License as published by * |
---|
12 | * the Free Software Foundation; either version 2.1 of the License, * |
---|
13 | * or any later version. * |
---|
14 | * This library is distributed in the hope that it will be useful, but * |
---|
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * |
---|
17 | * See the GNU Lesser General Public License for more details. * |
---|
18 | * You should have received a copy of the GNU Lesser General Public License * |
---|
19 | * along with this library; if not, write to the Free Software Foundation, * |
---|
20 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
---|
21 | \**************************************************************************/ |
---|
22 | |
---|
23 | |
---|
24 | class uiasyncservice |
---|
25 | { |
---|
26 | var $public_functions = array( |
---|
27 | 'index' => True, |
---|
28 | ); |
---|
29 | function uiasyncservice() |
---|
30 | { |
---|
31 | if (!is_object($GLOBALS['phpgw']->asyncservice)) |
---|
32 | { |
---|
33 | $GLOBALS['phpgw']->asyncservice = CreateObject('phpgwapi.asyncservice'); |
---|
34 | } |
---|
35 | } |
---|
36 | |
---|
37 | function index() |
---|
38 | { |
---|
39 | if ($GLOBALS['phpgw']->acl->check('asyncservice_access',1,'admin')) |
---|
40 | { |
---|
41 | $GLOBALS['phpgw']->redirect_link('/index.php'); |
---|
42 | } |
---|
43 | $GLOBALS['phpgw_info']['flags']['app_header'] = lang('Admin').' - '.lang('Asynchronous timed services'); |
---|
44 | if(!@is_object($GLOBALS['phpgw']->js)) |
---|
45 | { |
---|
46 | $GLOBALS['phpgw']->js = CreateObject('phpgwapi.javascript'); |
---|
47 | } |
---|
48 | $GLOBALS['phpgw']->js->validate_file('jscode','openwindow','admin'); |
---|
49 | //$GLOBALS['phpgw']->common->phpgw_header(); |
---|
50 | //echo parse_navbar(); |
---|
51 | |
---|
52 | $async = $GLOBALS['phpgw']->asyncservice; // use an own instance, as we might set debug=True |
---|
53 | |
---|
54 | $async->debug = !!$_POST['debug']; |
---|
55 | |
---|
56 | $units = array( |
---|
57 | 'year' => lang('Year'), |
---|
58 | 'month' => lang('Month'), |
---|
59 | 'day' => lang('Day'), |
---|
60 | 'dow' => lang('Day of week<br>(0-6, 0=Sun)'), |
---|
61 | 'hour' => lang('Hour<br>(0-23)'), |
---|
62 | 'min' => lang('Minute') |
---|
63 | ); |
---|
64 | |
---|
65 | if ($_POST['send'] || $_POST['test'] || $_POST['cancel'] || $_POST['install'] || $_POST['deinstall'] || $_POST['update'] || isset($_POST['asyncservice'])) |
---|
66 | { |
---|
67 | $times = array(); |
---|
68 | foreach($units as $u => $ulabel) |
---|
69 | { |
---|
70 | if ($_POST[$u] !== '') |
---|
71 | { |
---|
72 | $times[$u] = $_POST[$u]; |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | if ($_POST['test']) |
---|
77 | { |
---|
78 | $prefs = $GLOBALS['phpgw']->preferences->create_email_preferences(); |
---|
79 | if (!$async->set_timer($times,'test','admin.uiasyncservice.test',$prefs['email']['address'])) |
---|
80 | { |
---|
81 | echo '<p><b>'.lang("Error setting timer, wrong syntax or maybe there's one already running !!!")."</b></p>\n"; |
---|
82 | } |
---|
83 | unset($prefs); |
---|
84 | } |
---|
85 | if ($_POST['cancel']) |
---|
86 | { |
---|
87 | if (!$async->cancel_timer('test')) |
---|
88 | { |
---|
89 | echo '<p><b>'.lang("Error canceling timer, maybe there's none set !!!")."</b></p>\n"; |
---|
90 | } |
---|
91 | } |
---|
92 | if ($_POST['install'] || $_POST['deinstall']) |
---|
93 | { |
---|
94 | if (!($install = $async->install($_POST['install'] ? $times : False))) |
---|
95 | { |
---|
96 | echo '<p><b>'.lang('Error: %1 not found or other error !!!',$async->crontab)."</b></p>\n"; |
---|
97 | } |
---|
98 | $_POST['asyncservice'] = $_POST['deinstall'] ? 'fallback' : 'crontab'; |
---|
99 | } |
---|
100 | } |
---|
101 | else |
---|
102 | { |
---|
103 | $times = array('min' => '*/5'); // set some default |
---|
104 | } |
---|
105 | echo '<form action="'.$GLOBALS['phpgw']->link('/index.php',array('menuaction'=>'admin.uiasyncservice.index')).'" method="POST">'."\n<p>"; |
---|
106 | echo '<div style="text-align: left; margin: 10px;">'."\n"; |
---|
107 | |
---|
108 | $last_run = $async->last_check_run(); |
---|
109 | $lr_date = $last_run['end'] ? $GLOBALS['phpgw']->common->show_date($last_run['end']) : lang('never'); |
---|
110 | echo '<p><b>'.lang('Async services last executed').'</b>: '.$lr_date.' ('.$last_run['run_by'].")</p>\n<hr>\n"; |
---|
111 | |
---|
112 | if (isset($_POST['asyncservice']) && $_POST['asyncservice'] != $GLOBALS['phpgw_info']['server']['asyncservice']) |
---|
113 | { |
---|
114 | $config = CreateObject('phpgwapi.config','phpgwapi'); |
---|
115 | $config->read_repository(); |
---|
116 | $config->value('asyncservice',$GLOBALS['phpgw_info']['server']['asyncservice']=$_POST['asyncservice']); |
---|
117 | $config->save_repository(); |
---|
118 | unset($config); |
---|
119 | } |
---|
120 | if (!$async->only_fallback) |
---|
121 | { |
---|
122 | $installed = $async->installed(); |
---|
123 | if (is_array($installed) && isset($installed['cronline'])) |
---|
124 | { |
---|
125 | $async_use['cron'] = lang('crontab only (recomended)'); |
---|
126 | } |
---|
127 | } |
---|
128 | $async_use[''] = lang('fallback (after each pageview)'); |
---|
129 | $async_use['off'] = lang('disabled (not recomended)'); |
---|
130 | echo '<p><b>'.lang('Run Asynchronous services').'</b>'. |
---|
131 | ' <select name="asyncservice" onChange="this.form.submit();">'; |
---|
132 | foreach ($async_use as $key => $label) |
---|
133 | { |
---|
134 | $selected = $key == $GLOBALS['phpgw_info']['server']['asyncservice'] ? ' selected' : ''; |
---|
135 | echo "<option value=\"$key\"$selected>$label</option>\n"; |
---|
136 | } |
---|
137 | echo "</select>\n"; |
---|
138 | |
---|
139 | if (is_array($installed) && isset($installed['cronline'])) |
---|
140 | { |
---|
141 | echo ' <input type="submit" name="deinstall" value="'.lang('Deinstall crontab')."\">\n"; |
---|
142 | } |
---|
143 | echo "</p>\n"; |
---|
144 | |
---|
145 | if ($async->only_fallback) |
---|
146 | { |
---|
147 | echo '<p>'.lang('Under windows you can only use the fallback mode at the moment. Fallback means the jobs get only checked after each page-view !!!')."</p>\n"; |
---|
148 | } |
---|
149 | else |
---|
150 | { |
---|
151 | echo '<p>'.lang('Installed crontab').": \n"; |
---|
152 | |
---|
153 | if (is_array($installed) && isset($installed['cronline'])) |
---|
154 | { |
---|
155 | echo "$installed[cronline]</p>"; |
---|
156 | } |
---|
157 | elseif ($installed === 0) |
---|
158 | { |
---|
159 | echo '<b>'.lang('%1 not found or not executable !!!',$async->crontab)."</b></p>\n"; |
---|
160 | } |
---|
161 | else |
---|
162 | { |
---|
163 | echo '<b>'.lang('asyncservices not yet installed or other error (%1) !!!',$installed['error'])."</b></p>\n"; |
---|
164 | } |
---|
165 | echo '<p><input type="submit" name="install" value="'.lang('Install crontab')."\">\n". |
---|
166 | lang("for the times below (empty values count as '*', all empty = every minute)")."</p>\n"; |
---|
167 | } |
---|
168 | |
---|
169 | echo "<hr><table border=0><tr>\n"; |
---|
170 | foreach ($units as $u => $ulabel) |
---|
171 | { |
---|
172 | echo " <td>$ulabel</td><td><input name=\"$u\" value=\"$times[$u]\" size=5> </td>\n"; |
---|
173 | } |
---|
174 | echo "</tr><tr>\n <td colspan=4>\n"; |
---|
175 | echo ' <input type="submit" name="send" value="'.lang('Calculate next run').'"></td>'."\n"; |
---|
176 | echo ' <td colspan="8"><input type="checkbox" name="debug" value="1"'.($_POST['debug'] ? ' checked' : '')."> \n". |
---|
177 | lang('Enable debug-messages')."</td>\n</tr></table>\n"; |
---|
178 | |
---|
179 | if ($_POST['send']) |
---|
180 | { |
---|
181 | $next = $async->next_run($times,True); |
---|
182 | |
---|
183 | echo "<p>asyncservice::next_run(";print_r($times);echo")=".($next === False ? 'False':"'$next'=".$GLOBALS['phpgw']->common->show_date($next))."</p>\n"; |
---|
184 | } |
---|
185 | echo '<hr><p><input type="submit" name="cancel" value="'.lang('Cancel TestJob!')."\"> \n"; |
---|
186 | echo '<input type="submit" name="test" value="'.lang('Start TestJob!')."\">\n"; |
---|
187 | echo lang('for the times above')."</p>\n"; |
---|
188 | echo '<p>'.lang('The TestJob sends you a mail everytime it is called.')."</p>\n"; |
---|
189 | |
---|
190 | echo '<hr><p><b>'.lang('Jobs').":</b>\n"; |
---|
191 | if ($jobs = $async->read('%')) |
---|
192 | { |
---|
193 | 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"; |
---|
194 | foreach($jobs as $job) |
---|
195 | { |
---|
196 | //echo "<tr>\n<td>$job[id]</td><td>".$GLOBALS['phpgw']->common->show_date($job['next'])."</td><td>"; |
---|
197 | echo "<tr>\n<td>$job[id]</td><td>".date("d/m/Y H:i", $job['next'])."</td><td>"; |
---|
198 | //print_r($GLOBALS['phpgw']->common->show_date($job['times'])); |
---|
199 | print_r(date("d/m/Y H:i", $job['times'])); |
---|
200 | echo "</td><td>$job[method]</td><td>"; |
---|
201 | print_r($job['data']); |
---|
202 | echo "</td><td align=\"center\">".$GLOBALS['phpgw']->accounts->id2name($job[account_id])."</td></tr>\n"; |
---|
203 | } |
---|
204 | echo "</table>\n"; |
---|
205 | } |
---|
206 | else |
---|
207 | { |
---|
208 | echo lang('No jobs in the database !!!')."</p>\n"; |
---|
209 | } |
---|
210 | echo '<p><input type="submit" name="update" value="'.lang('Update').'"></p>'."\n"; |
---|
211 | echo "</form>\n"; |
---|
212 | |
---|
213 | } |
---|
214 | |
---|
215 | function test($to) |
---|
216 | { |
---|
217 | if (!is_object($GLOBALS['phpgw']->send)) |
---|
218 | { |
---|
219 | $GLOBALS['phpgw']->send = CreateObject('phpgwapi.send'); |
---|
220 | } |
---|
221 | $returncode = $GLOBALS['phpgw']->send->msg('email',$to,$subject='Asynchronous timed services','Greatings from cron ;-)'); |
---|
222 | |
---|
223 | if (!$returncode) // not nice, but better than failing silently |
---|
224 | { |
---|
225 | echo "<p>bocalendar::send_update: sending message to '$to' subject='$subject' failed !!!<br>\n"; |
---|
226 | echo $GLOBALS['phpgw']->send->err['desc']."</p>\n"; |
---|
227 | } |
---|
228 | //print_r($GLOBALS['phpgw_info']['user']); |
---|
229 | } |
---|
230 | } |
---|