|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* sysPass |
|
4
|
|
|
* |
|
5
|
|
|
* @author nuxsmin |
|
6
|
|
|
* @link https://syspass.org |
|
7
|
|
|
* @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org |
|
8
|
|
|
* |
|
9
|
|
|
* This file is part of sysPass. |
|
10
|
|
|
* |
|
11
|
|
|
* sysPass is free software: you can redistribute it and/or modify |
|
12
|
|
|
* it under the terms of the GNU General Public License as published by |
|
13
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
14
|
|
|
* (at your option) any later version. |
|
15
|
|
|
* |
|
16
|
|
|
* sysPass is distributed in the hope that it will be useful, |
|
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
19
|
|
|
* GNU General Public License for more details. |
|
20
|
|
|
* |
|
21
|
|
|
* You should have received a copy of the GNU General Public License |
|
22
|
|
|
* along with sysPass. If not, see <http://www.gnu.org/licenses/>. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace SP\Services\Task; |
|
26
|
|
|
|
|
27
|
|
|
use SP\Core\Context\SessionContext; |
|
28
|
|
|
use SP\Core\Messages\TaskMessage; |
|
29
|
|
|
use SP\Storage\File\FileException; |
|
30
|
|
|
use SP\Storage\File\FileHandler; |
|
31
|
|
|
use SP\Util\Util; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Class Task |
|
35
|
|
|
* |
|
36
|
|
|
* @package SP\Core |
|
37
|
|
|
*/ |
|
38
|
|
|
final class Task |
|
39
|
|
|
{ |
|
40
|
|
|
/** |
|
41
|
|
|
* @var string Nombre de la tarea |
|
42
|
|
|
*/ |
|
43
|
|
|
private $name; |
|
44
|
|
|
/** |
|
45
|
|
|
* @var string ID de la tarea |
|
46
|
|
|
*/ |
|
47
|
|
|
private $taskId; |
|
48
|
|
|
/** |
|
49
|
|
|
* @var FileHandler |
|
50
|
|
|
*/ |
|
51
|
|
|
private $fileOut; |
|
52
|
|
|
/** |
|
53
|
|
|
* @var FileHandler |
|
54
|
|
|
*/ |
|
55
|
|
|
private $fileTask; |
|
56
|
|
|
/** |
|
57
|
|
|
* @var int Intérvalo en segundos |
|
58
|
|
|
*/ |
|
59
|
|
|
private $interval = 5; |
|
60
|
|
|
/** |
|
61
|
|
|
* @var bool Si se ha inicializado para escribir en el archivo |
|
62
|
|
|
*/ |
|
63
|
|
|
private $initialized = false; |
|
64
|
|
|
/** |
|
65
|
|
|
* @var string |
|
66
|
|
|
*/ |
|
67
|
|
|
private $uid; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Task constructor. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $name Nombre de la tarea |
|
73
|
|
|
* @param string $id |
|
74
|
|
|
*/ |
|
75
|
|
|
public function __construct($name, $id) |
|
76
|
|
|
{ |
|
77
|
|
|
$this->name = $name; |
|
78
|
|
|
$this->taskId = $id; |
|
79
|
|
|
$this->initialized = $this->checkFile(); |
|
80
|
|
|
$this->uid = $this->genUid(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Comprobar si se puede escribir en el archivo |
|
85
|
|
|
* |
|
86
|
|
|
* @return bool |
|
87
|
|
|
*/ |
|
88
|
|
|
private function checkFile() |
|
89
|
|
|
{ |
|
90
|
|
|
$tempDir = Util::getTempDir(); |
|
91
|
|
|
|
|
92
|
|
|
if ($tempDir !== false) { |
|
93
|
|
|
$this->fileOut = new FileHandler($tempDir . DIRECTORY_SEPARATOR . $this->taskId . '.out'); |
|
|
|
|
|
|
94
|
|
|
$this->fileTask = new FileHandler($tempDir . DIRECTORY_SEPARATOR . $this->taskId . '.task'); |
|
95
|
|
|
|
|
96
|
|
|
$this->deleteTaskFiles(); |
|
97
|
|
|
|
|
98
|
|
|
return true; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return false; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Eliminar los archivos de la tarea no usados |
|
106
|
|
|
*/ |
|
107
|
|
|
private function deleteTaskFiles() |
|
108
|
|
|
{ |
|
109
|
|
|
$filesOut = dirname($this->fileOut->getFile()) . DIRECTORY_SEPARATOR . $this->taskId . '*.out'; |
|
110
|
|
|
$filesTask = dirname($this->fileTask->getFile()) . DIRECTORY_SEPARATOR . $this->taskId . '*.task'; |
|
111
|
|
|
|
|
112
|
|
|
array_map('unlink', array_merge(glob($filesOut), glob($filesTask))); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return string |
|
117
|
|
|
*/ |
|
118
|
|
|
public function genUid() |
|
119
|
|
|
{ |
|
120
|
|
|
return md5($this->name . $this->taskId); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Generar un ID de tarea |
|
125
|
|
|
* |
|
126
|
|
|
* @param $name |
|
127
|
|
|
* |
|
128
|
|
|
* @return string |
|
129
|
|
|
*/ |
|
130
|
|
|
public static function genTaskId($name) |
|
131
|
|
|
{ |
|
132
|
|
|
return uniqid($name); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Escribir el tado de la tarea a un archivo |
|
137
|
|
|
* |
|
138
|
|
|
* @param TaskMessage $message |
|
139
|
|
|
* |
|
140
|
|
|
* @return bool |
|
141
|
|
|
*/ |
|
142
|
|
|
public function writeStatusAndFlush(TaskMessage $message) |
|
143
|
|
|
{ |
|
144
|
|
|
try { |
|
145
|
|
|
if ($this->initialized === true) { |
|
146
|
|
|
$this->fileOut->save($message->composeText()); |
|
147
|
|
|
return true; |
|
148
|
|
|
} |
|
149
|
|
|
} catch (FileException $e) { |
|
150
|
|
|
processException($e); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return false; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Escribir un mensaje en el archivo de la tarea en formato JSON |
|
158
|
|
|
* |
|
159
|
|
|
* @param TaskMessage $message |
|
160
|
|
|
*/ |
|
161
|
|
|
public function writeJsonStatusAndFlush(TaskMessage $message) |
|
162
|
|
|
{ |
|
163
|
|
|
try { |
|
164
|
|
|
if ($this->initialized === true) { |
|
165
|
|
|
$this->fileOut->save($message->composeJson()); |
|
166
|
|
|
} |
|
167
|
|
|
} catch (FileException $e) { |
|
168
|
|
|
processException($e); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Iniciar la tarea |
|
174
|
|
|
*/ |
|
175
|
|
|
public function end() |
|
176
|
|
|
{ |
|
177
|
|
|
try { |
|
178
|
|
|
logger("End Task: {$this->name}"); |
|
179
|
|
|
|
|
180
|
|
|
$this->unregister(); |
|
181
|
|
|
|
|
182
|
|
|
$this->fileOut->delete(); |
|
183
|
|
|
} catch (FileException $e) { |
|
184
|
|
|
processException($e); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Desregistrar la tarea en la sesión |
|
190
|
|
|
* |
|
191
|
|
|
* @throws FileException |
|
192
|
|
|
*/ |
|
193
|
|
|
public function unregister() |
|
194
|
|
|
{ |
|
195
|
|
|
logger("Unregister Task: {$this->name}"); |
|
196
|
|
|
|
|
197
|
|
|
$this->fileTask->delete(); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @return int |
|
202
|
|
|
*/ |
|
203
|
|
|
public function getInterval() |
|
204
|
|
|
{ |
|
205
|
|
|
return $this->interval; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* @param int $interval |
|
210
|
|
|
* |
|
211
|
|
|
* @return Task |
|
212
|
|
|
*/ |
|
213
|
|
|
public function setInterval($interval) |
|
214
|
|
|
{ |
|
215
|
|
|
$this->interval = $interval; |
|
216
|
|
|
|
|
217
|
|
|
return $this; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* @return string |
|
222
|
|
|
*/ |
|
223
|
|
|
public function getTaskId() |
|
224
|
|
|
{ |
|
225
|
|
|
return $this->taskId; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* @return FileHandler |
|
230
|
|
|
*/ |
|
231
|
|
|
public function getFileOut(): FileHandler |
|
232
|
|
|
{ |
|
233
|
|
|
return $this->fileOut; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Register a task |
|
238
|
|
|
* |
|
239
|
|
|
* @return Task |
|
240
|
|
|
* @throws \SP\Storage\File\FileException |
|
241
|
|
|
*/ |
|
242
|
|
|
public function register() |
|
243
|
|
|
{ |
|
244
|
|
|
logger("Register Task: {$this->name}"); |
|
245
|
|
|
|
|
246
|
|
|
$this->fileTask->save(serialize($this)); |
|
247
|
|
|
|
|
248
|
|
|
return $this; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Register a task |
|
253
|
|
|
* |
|
254
|
|
|
* Session is locked in order to allow other scripts execution |
|
255
|
|
|
* |
|
256
|
|
|
* @return Task |
|
257
|
|
|
* @throws \SP\Storage\File\FileException |
|
258
|
|
|
*/ |
|
259
|
|
|
public function registerSession() |
|
260
|
|
|
{ |
|
261
|
|
|
logger("Register Task (session): {$this->name}"); |
|
262
|
|
|
|
|
263
|
|
|
$this->fileTask->save(serialize($this)); |
|
264
|
|
|
|
|
265
|
|
|
SessionContext::close(); |
|
266
|
|
|
|
|
267
|
|
|
return $this; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* @return string |
|
272
|
|
|
*/ |
|
273
|
|
|
public function getUid(): string |
|
274
|
|
|
{ |
|
275
|
|
|
return $this->uid; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* @return FileHandler |
|
280
|
|
|
*/ |
|
281
|
|
|
public function getFileTask(): FileHandler |
|
282
|
|
|
{ |
|
283
|
|
|
return $this->fileTask; |
|
284
|
|
|
} |
|
285
|
|
|
} |