|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
namespace XoopsModules\Wgevents; |
|
7
|
|
|
|
|
8
|
|
|
/* |
|
9
|
|
|
You may not change or alter any portion of this comment or credits |
|
10
|
|
|
of supporting developers from this source code or any supporting source code |
|
11
|
|
|
which is considered copyrighted (c) material of the original comment or credit authors. |
|
12
|
|
|
|
|
13
|
|
|
This program is distributed in the hope that it will be useful, |
|
14
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* wgEvents module for xoops |
|
20
|
|
|
* |
|
21
|
|
|
* @copyright 2021 XOOPS Project (https://xoops.org) |
|
22
|
|
|
* @license GPL 2.0 or later |
|
23
|
|
|
* @package wgevents |
|
24
|
|
|
* @since 1.0.0 |
|
25
|
|
|
* @min_xoops 2.5.11 Beta1 |
|
26
|
|
|
* @author Goffy - Wedega - Email:[email protected] - Website:https://xoops.wedega.com |
|
27
|
|
|
*/ |
|
28
|
|
|
|
|
29
|
|
|
use XoopsModules\Wgevents; |
|
30
|
|
|
use XoopsModules\Wgevents\{ |
|
31
|
|
|
Constants, |
|
|
|
|
|
|
32
|
|
|
MailHandler |
|
|
|
|
|
|
33
|
|
|
}; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Class Object Handler Task |
|
37
|
|
|
*/ |
|
38
|
|
|
class TaskHandler extends \XoopsPersistableObjectHandler |
|
39
|
|
|
{ |
|
40
|
|
|
/** |
|
41
|
|
|
* Constructor |
|
42
|
|
|
* |
|
43
|
|
|
* @param \XoopsDatabase $db |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct(\XoopsDatabase $db) |
|
46
|
|
|
{ |
|
47
|
|
|
parent::__construct($db, 'wgevents_task', Task::class, 'id', 'id'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param bool $isNew |
|
52
|
|
|
* |
|
53
|
|
|
* @return object |
|
54
|
|
|
*/ |
|
55
|
|
|
public function create($isNew = true) |
|
56
|
|
|
{ |
|
57
|
|
|
return parent::create($isNew); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* retrieve a field |
|
62
|
|
|
* |
|
63
|
|
|
* @param int $id field id |
|
64
|
|
|
* @param $fields |
|
65
|
|
|
* @return \XoopsObject|null reference to the {@link Get} object |
|
66
|
|
|
*/ |
|
67
|
|
|
public function get($id = null, $fields = null) |
|
68
|
|
|
{ |
|
69
|
|
|
return parent::get($id, $fields); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* get inserted id |
|
74
|
|
|
* |
|
75
|
|
|
* @return int reference to the {@link Get} object |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getInsertId() |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->db->getInsertId(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get Count Task in the database |
|
84
|
|
|
* @param int $start |
|
85
|
|
|
* @param int $limit |
|
86
|
|
|
* @param string $sort |
|
87
|
|
|
* @param string $order |
|
88
|
|
|
* @return int |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getCountTasks($start = 0, $limit = 0, $sort = 'id', $order = 'ASC') |
|
91
|
|
|
{ |
|
92
|
|
|
$crCountTasks = new \CriteriaCompo(); |
|
93
|
|
|
$crCountTasks = $this->getTasksCriteria($crCountTasks, $start, $limit, $sort, $order); |
|
94
|
|
|
return $this->getCount($crCountTasks); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Get All Task in the database |
|
99
|
|
|
* @param int $start |
|
100
|
|
|
* @param int $limit |
|
101
|
|
|
* @param string $sort |
|
102
|
|
|
* @param string $order |
|
103
|
|
|
* @return array |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getAllTasks($start = 0, $limit = 0, $sort = 'id', $order = 'DESC') |
|
106
|
|
|
{ |
|
107
|
|
|
$crAllTasks = new \CriteriaCompo(); |
|
108
|
|
|
$crAllTasks = $this->getTasksCriteria($crAllTasks, $start, $limit, $sort, $order); |
|
109
|
|
|
return $this->getAll($crAllTasks); |
|
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Get Criteria Task |
|
114
|
|
|
* @param $crTasks |
|
115
|
|
|
* @param int $start |
|
116
|
|
|
* @param int $limit |
|
117
|
|
|
* @param string $sort |
|
118
|
|
|
* @param string $order |
|
119
|
|
|
* @return int |
|
120
|
|
|
*/ |
|
121
|
|
|
private function getTasksCriteria($crTasks, $start, $limit, $sort, $order) |
|
122
|
|
|
{ |
|
123
|
|
|
$crTasks->setStart($start); |
|
124
|
|
|
$crTasks->setLimit($limit); |
|
125
|
|
|
$crTasks->setSort($sort); |
|
126
|
|
|
$crTasks->setOrder($order); |
|
127
|
|
|
return $crTasks; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Get count open tasks for cron.php |
|
132
|
|
|
* @return int |
|
133
|
|
|
*/ |
|
134
|
|
|
public function getCountTasksOpen() |
|
135
|
|
|
{ |
|
136
|
|
|
$crTask = new \CriteriaCompo(); |
|
137
|
|
|
$crTask->add(new \Criteria('status', Constants::STATUS_DONE, '<')); |
|
138
|
|
|
|
|
139
|
|
|
return $this->getCount($crTask); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Create a task |
|
144
|
|
|
* @param $type |
|
145
|
|
|
* @param $recipient |
|
146
|
|
|
* @param $params |
|
147
|
|
|
* @return bool |
|
148
|
|
|
*/ |
|
149
|
|
|
public function createTask($type, $recipient, $params) |
|
150
|
|
|
{ |
|
151
|
|
|
$uidCurrent = \is_object($GLOBALS['xoopsUser']) ? (int)$GLOBALS['xoopsUser']->uid() : 0; |
|
152
|
|
|
|
|
153
|
|
|
$taskObj = $this->create(); |
|
154
|
|
|
// Set Vars |
|
155
|
|
|
$taskObj->setVar('type', $type); |
|
156
|
|
|
$taskObj->setVar('params', $params); |
|
157
|
|
|
$taskObj->setVar('recipient', $recipient); |
|
158
|
|
|
$taskObj->setVar('datecreated', time()); |
|
159
|
|
|
$taskObj->setVar('submitter', $uidCurrent); |
|
160
|
|
|
$taskObj->setVar('status', Constants::STATUS_PENDING); |
|
161
|
|
|
|
|
162
|
|
|
// Insert Data |
|
163
|
|
|
return (bool)$this->insert($taskObj); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* process all task if limit is not exceeded |
|
168
|
|
|
* @param $log_level |
|
169
|
|
|
* @return array |
|
170
|
|
|
*/ |
|
171
|
|
|
public function processTasks($log_level = 0) |
|
172
|
|
|
{ |
|
173
|
|
|
$helper = \XoopsModules\Wgevents\Helper::getInstance(); |
|
174
|
|
|
|
|
175
|
|
|
// get limit_hour from primary account |
|
176
|
|
|
$accountHandler = $helper->getHandler('Account'); |
|
177
|
|
|
$limitHour = $accountHandler->getLimitHour(); |
|
178
|
|
|
|
|
179
|
|
|
$resProcess = ''; |
|
180
|
|
|
|
|
181
|
|
|
$crTaskPending = new \CriteriaCompo(); |
|
182
|
|
|
$crTaskPending->add(new \Criteria('status', Constants::STATUS_PENDING)); |
|
183
|
|
|
$tasksCountPending = $this->getCount($crTaskPending); |
|
184
|
|
|
$crTaskPending->setSort('type ASC, id'); |
|
185
|
|
|
$crTaskPending->setOrder('ASC'); |
|
186
|
|
|
|
|
187
|
|
|
// if all works properly there shouldn't be a task type 'processing' left |
|
188
|
|
|
$crTaskProcessing = new \CriteriaCompo(); |
|
189
|
|
|
$crTaskProcessing->add(new \Criteria('status', Constants::STATUS_PROCESSING)); |
|
190
|
|
|
$tasksCountProcessing = $this->getCount($crTaskProcessing); |
|
191
|
|
|
|
|
192
|
|
|
$crTaskDone = new \CriteriaCompo(); |
|
193
|
|
|
$crTaskDone->add(new \Criteria('status', Constants::STATUS_DONE)); |
|
194
|
|
|
$crTaskDone->add(new \Criteria('datedone', time() - 3600, '>')); |
|
195
|
|
|
$tasksCountDone = $this->getCount($crTaskDone); |
|
196
|
|
|
|
|
197
|
|
|
$counterDone = 0; |
|
198
|
|
|
if ($log_level > 0) { |
|
199
|
|
|
$resProcess .= '<br>Start processTasks'; |
|
200
|
|
|
$resProcess .= '<br>time - 3600: ' . \formatTimestamp(time() - 3600, 'm'); |
|
201
|
|
|
if ($tasksCountProcessing > 0) { |
|
202
|
|
|
$resProcess .= '<br><span style="color:#ff0000;font-weight:700">Count status PROCESSING at start: ' . $tasksCountProcessing . '</span>'; |
|
203
|
|
|
} |
|
204
|
|
|
$resProcess .= '<br>Count status PENDING at start: ' . $tasksCountPending; |
|
205
|
|
|
$resProcess .= '<br>Count status DONE at start: ' . $tasksCountDone; |
|
206
|
|
|
} |
|
207
|
|
|
if (($tasksCountPending > 0) && ($tasksCountDone < $limitHour || 0 === $limitHour)) { |
|
208
|
|
|
if ($limitHour > 0) { |
|
209
|
|
|
$crTaskPending->setLimit($limitHour); |
|
210
|
|
|
} |
|
211
|
|
|
$tasksAll = $this->getAll($crTaskPending); |
|
212
|
|
|
$resultMH = 0; |
|
213
|
|
|
foreach (\array_keys($tasksAll) as $i) { |
|
214
|
|
|
// check whether task is still pending |
|
215
|
|
|
// ignore it if meanwhile another one started to process the task |
|
216
|
|
|
if ($log_level > 1) { |
|
217
|
|
|
$resProcess .= '<br>Task key: ' . $i; |
|
218
|
|
|
} |
|
219
|
|
|
if (554 === $resultMH) { |
|
220
|
|
|
$resProcess .= '<br>Skipped Error 554: SMTP limit exceeded'; |
|
221
|
|
|
} elseif ((Constants::STATUS_PENDING == (int)$tasksAll[$i]->getVar('status')) |
|
222
|
|
|
&& ($tasksCountDone < $limitHour || 0 == $limitHour)) { |
|
223
|
|
|
$taskProcessObj = $this->get($i); |
|
224
|
|
|
$taskProcessObj->setVar('status', Constants::STATUS_PROCESSING); |
|
225
|
|
|
if ($this->insert($taskProcessObj)) { |
|
|
|
|
|
|
226
|
|
|
$mailsHandler = new MailHandler(); |
|
227
|
|
|
$mailParams = json_decode($tasksAll[$i]->getVar('params', 'n'), true); |
|
228
|
|
|
$mailParams['recipients'] = $tasksAll[$i]->getVar('recipient'); |
|
229
|
|
|
$mailParams['taskId'] = $i; |
|
230
|
|
|
$mailsHandler->setParams($mailParams); |
|
231
|
|
|
$mailsHandler->setType($tasksAll[$i]->getVar('type')); |
|
232
|
|
|
// send mails |
|
233
|
|
|
$resultMH = $mailsHandler->execute(); |
|
234
|
|
|
unset($mailsHandler); |
|
235
|
|
|
//update task list corresponding the result |
|
236
|
|
|
if (0 === $resultMH) { |
|
237
|
|
|
$taskProcessObj->setVar('status', Constants::STATUS_DONE); |
|
238
|
|
|
$taskProcessObj->setVar('datedone', time()); |
|
239
|
|
|
$counterDone++; |
|
240
|
|
|
if ($log_level > 1) { |
|
241
|
|
|
$resProcess .= ' - done'; |
|
242
|
|
|
} |
|
243
|
|
|
} else { |
|
244
|
|
|
$taskProcessObj->setVar('status', Constants::STATUS_PENDING); |
|
245
|
|
|
if ($log_level > 1) { |
|
246
|
|
|
$resProcess .= ' - failed'; |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
$this->insert($taskProcessObj); |
|
250
|
|
|
} else { |
|
251
|
|
|
$resProcess .= ' - error insert taskProcessObj'; |
|
252
|
|
|
} |
|
253
|
|
|
} |
|
254
|
|
|
unset($taskProcessObj); |
|
255
|
|
|
$taskProcessObj = $this->get($i); |
|
256
|
|
|
//check whether process is still pending, what should not be |
|
257
|
|
|
if (Constants::STATUS_PROCESSING === (int)$taskProcessObj->getVar('status')) { |
|
258
|
|
|
$taskProcessObj->setVar('status', Constants::STATUS_PENDING); |
|
259
|
|
|
$this->insert($taskProcessObj); |
|
260
|
|
|
} |
|
261
|
|
|
// check once more number of done |
|
262
|
|
|
$tasksCountDone = $this->getCount($crTaskDone); |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
// check once more number of open tasks |
|
266
|
|
|
$crTaskOpen = new \CriteriaCompo(); |
|
267
|
|
|
$crTaskOpen->add(new \Criteria('status', Constants::STATUS_DONE, '<')); |
|
268
|
|
|
$tasksCountOpen = $this->getCount($crTaskOpen); |
|
269
|
|
|
|
|
270
|
|
|
if ($log_level > 0) { |
|
271
|
|
|
$resProcess .= '<br>End processTasks'; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
return ['pending' => $tasksCountPending, 'done' => $counterDone, 'resprocess' => $resProcess, 'still_open' => $tasksCountOpen]; |
|
275
|
|
|
} |
|
276
|
|
|
} |
|
277
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: