Issues (4868)

admin/inc/class.admin_asyncservice.inc.php (3 issues)

1
<?php
2
/**
3
 * EGgroupware admin - Timed Asynchron Services
4
 *
5
 * @link http://www.egroupware.org
6
 * @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
7
 * @package admin
8
 * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
9
 * @version $Id$
10
 */
11
12
use EGroupware\Api;
13
use EGroupware\Api\Egw;
14
15
/**
16
 * Class to admin cron-job like timed calls of eGroupWare methods
17
 */
18
class admin_asyncservice
19
{
20
	var $public_functions = array(
21
		'index' => True,
22
	);
23
24
	function index()
25
	{
26
		if ($GLOBALS['egw']->acl->check('asyncservice_acc',1,'admin'))
27
		{
28
			Egw::redirect_link('/index.php');
29
		}
30
		$GLOBALS['egw_info']['flags']['app_header'] = lang('Admin').' - '.lang('Asynchronous timed services');
31
32
		echo $GLOBALS['egw']->framework->header();
33
34
		$async = $GLOBALS['egw']->asyncservice;	// use an own instance, as we might set debug=True
35
36
		$async->debug = !!$_POST['debug'];
37
38
		$units = array(
39
			'year'  => lang('Year'),
40
			'month' => lang('Month'),
41
			'day'   => lang('Day'),
42
			'dow'   => lang('Day of week<br>(0-6, 0=Sun)'),
43
			'hour'  => lang('Hour<br>(0-23)'),
44
			'min'   => lang('Minute')
45
		);
46
47
		if ($_POST['send'] || $_POST['test'] || $_POST['cancel'] || $_POST['install'] || $_POST['deinstall'] || $_POST['update'] || isset($_POST['asyncservice']))
48
		{
49
			$times = array();
50
			foreach($units as $u => $ulabel)
51
			{
52
				if ($_POST[$u] !== '')
53
				{
54
					$times[$u] = $_POST[$u];
55
				}
56
			}
57
58
			if ($_POST['test'])
59
			{
60
				if (strpos($GLOBALS['egw_info']['user']['account_email'],'@') === false)
61
				{
62
					echo '<p><b>'.htmlspecialchars(lang("You have no email address for your user set !!!"))."</b></p>\n";
63
				}
64
				elseif (!$async->set_timer($times,'test','admin.admin_asyncservice.test',$GLOBALS['egw_info']['user']['account_email']))
65
				{
66
					echo '<p><b>'.htmlspecialchars(lang("Error setting timer, wrong syntax or maybe there's one already running !!!"))."</b></p>\n";
67
				}
68
			}
69
			if ($_POST['cancel'])
70
			{
71
				if (!$async->cancel_timer('test'))
72
				{
73
					echo '<p><b>'.htmlspecialchars(lang("Error canceling timer, maybe there's none set !!!"))."</b></p>\n";
74
				}
75
			}
76
			if ($_POST['install'] || $_POST['deinstall'])
77
			{
78
				if (!($install = $async->install($_POST['install'] ? $times : False)))
0 ignored issues
show
The assignment to $install is dead and can be removed.
Loading history...
79
				{
80
					echo '<p><b>'.htmlspecialchars(lang('Error: %1 not found or other error !!!',$async->crontab))."</b></p>\n";
0 ignored issues
show
The call to lang() has too many arguments starting with $async->crontab. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

80
					echo '<p><b>'.htmlspecialchars(/** @scrutinizer ignore-call */ lang('Error: %1 not found or other error !!!',$async->crontab))."</b></p>\n";

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
81
				}
82
				$_POST['asyncservice'] = $_POST['deinstall'] ? 'fallback' : 'crontab';
83
			}
84
		}
85
		else
86
		{
87
			$times = array('min' => '*/5');		// set some default
88
		}
89
		echo '<form action="'.$GLOBALS['egw']->link('/index.php',array('menuaction'=>'admin.admin_asyncservice.index')).'" method="POST">'."\n<p>";
90
		echo '<div style="text-align: left; margin: 10px;">'."\n";
91
92
		$last_run = $async->last_check_run();
93
		$lr_date = $last_run['end'] ? Api\DateTime::server2user($last_run['end'],'') : lang('never');
94
		echo '<p><b>'. htmlspecialchars(lang('Async services last executed')).'</b>: '.
95
			$lr_date.' ('.htmlspecialchars($last_run['run_by']).")</p>\n<hr>\n";
96
97
		if (isset($_POST['asyncservice']) && $_POST['asyncservice'] != $GLOBALS['egw_info']['server']['asyncservice'])
98
		{
99
			Api\Config::save_value('asyncservice', $GLOBALS['egw_info']['server']['asyncservice']=$_POST['asyncservice'], 'phpgwapi');
100
		}
101
		if (!$async->only_fallback)
102
		{
103
			$installed = $async->installed();
104
			if (is_array($installed) && isset($installed['cronline']))
105
			{
106
				$async_use['cron'] = lang('crontab only (recomended)');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$async_use was never initialized. Although not strictly required by PHP, it is generally a good practice to add $async_use = array(); before regardless.
Loading history...
107
			}
108
		}
109
		$async_use['']    = lang('fallback (after each pageview)');
110
		$async_use['off'] = lang('disabled (not recomended)');
111
		echo '<p><b>'.htmlspecialchars(lang('Run Asynchronous services')).'</b>'.
112
			' <select name="asyncservice" onChange="this.form.submit();">';
113
		foreach ($async_use as $key => $label)
114
		{
115
			$selected = $key == $GLOBALS['egw_info']['server']['asyncservice'] ? ' selected' : '';
116
			echo "<option value=\"$key\"$selected>".htmlspecialchars($label)."</option>\n";
117
		}
118
		echo "</select>\n";
119
120
		if (is_array($installed) && isset($installed['cronline']))
121
		{
122
			echo ' &nbsp; <input type="submit" name="deinstall" value="'.htmlspecialchars(lang('Deinstall crontab'))."\">\n";
123
		}
124
		echo "</p>\n";
125
126
		if ($async->only_fallback)
127
		{
128
			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";
129
		}
130
		else
131
		{
132
			echo '<p>'.htmlspecialchars(lang('Installed crontab')).": \n";
133
134
			if (is_array($installed) && isset($installed['cronline']))
135
			{
136
				echo "$installed[cronline]</p>";
137
			}
138
			elseif ($installed === 0)
139
			{
140
				echo '<b>'.htmlspecialchars(lang('%1 not found or not executable !!!',$async->crontab))."</b></p>\n";
141
			}
142
			else
143
			{
144
				echo '<b>'.htmlspecialchars(lang('asyncservices not yet installed or other error (%1) !!!',$installed['error']))."</b></p>\n";
145
			}
146
			echo '<p><input type="submit" name="install" value="'.htmlspecialchars(lang('Install crontab'))."\">\n".
147
				htmlspecialchars(lang("for the times below (empty values count as '*', all empty = every minute)"))."</p>\n";
148
		}
149
150
		echo "<hr><table border=0><tr>\n";
151
		foreach ($units as $u => $ulabel)
152
		{
153
			echo " <td>$ulabel</td><td><input name=\"$u\" value=\"".htmlspecialchars($times[$u])."\" size=5> &nbsp; </td>\n";
154
		}
155
		echo "</tr><tr>\n <td colspan=4>\n";
156
		echo ' <input type="submit" name="send" value="'.htmlspecialchars(lang('Calculate next run')).'"></td>'."\n";
157
		echo ' <td colspan="8"><input type="checkbox" name="debug" value="1"'.($_POST['debug'] ? ' checked' : '')."> \n".
158
			htmlspecialchars(lang('Enable debug-messages'))."</td>\n</tr></table>\n";
159
160
		if ($_POST['send'])
161
		{
162
			$next = $async->next_run($times,True);
163
164
			echo "<p>asyncservice::next_run(". htmlspecialchars(json_encode($times, JSON_UNESCAPED_SLASHES)).")=".($next === False ? 'False':"$next=".Api\DateTime::server2user($next,''))."</p>\n";
165
		}
166
		echo '<hr><p><input type="submit" name="cancel" value="'.htmlspecialchars(lang('Cancel TestJob!'))."\"> &nbsp;\n";
167
		echo '<input type="submit" name="test" value="'.htmlspecialchars(lang('Start TestJob!'))."\">\n";
168
		echo lang('for the times above')."</p>\n";
169
		echo '<p>'.lang('The TestJob sends you a mail everytime it is called.')."</p>\n";
170
171
		echo '<hr><p><b>'.lang('Jobs').":</b>\n";
172
		if (($jobs = $async->read('%')))
173
		{
174
			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";
175
			foreach($jobs as $job)
176
			{
177
				echo "<tr>\n<td>$job[id]</td><td>".Api\DateTime::server2user($job['next'],'')."</td>\n";
178
				echo "<td>".htmlspecialchars(json_encode($job['times'], JSON_UNESCAPED_SLASHES))."</td>\n";
179
				echo "</td><td>".htmlspecialchars(str_replace('EGroupware\\', '', $job['method']))."</td>\n<td";
180
				$data = is_array($job['data']) ? json_encode($job['data'], JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE) : $job['data'];
181
				if (strlen($data) >= 64)
182
				{
183
					echo ' title="'.htmlspecialchars(json_encode($job['data'], JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT)).'"';
184
					$data = substr($data, 0, 60).'...';
185
				}
186
				echo ">". htmlspecialchars($data)."</td>\n";
187
				echo "<td align=\"center\">".htmlspecialchars($GLOBALS['egw']->accounts->id2name($job['account_id']))."</td></tr>\n";
188
			}
189
			echo "</table>\n";
190
		}
191
		else
192
		{
193
			echo lang('No jobs in the database !!!')."</p>\n";
194
		}
195
		echo '<p><input type="submit" name="update" value="'.htmlspecialchars(lang('Update')).'"></p>'."\n";
196
		echo "</form>\n";
197
		echo $GLOBALS['egw']->framework->footer();
198
	}
199
200
	/**
201
	 * Callback for test-job
202
	 *
203
	 * @param string $to email address to send mail to
204
	 */
205
	function test($to)
206
	{
207
		try {
208
			$mail = new Api\Mailer();
209
			$mail->setBody('Greetings from cron ;-)');
210
			$mail->addHeader('Subject', 'Asynchronous timed services');
211
			$mail->addAddress($to);
212
			$mail->send();
213
		}
214
		catch (Exception $e) {
215
			_egw_log_exception($e);
216
		}
217
	}
218
}
219