|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* Copyright (C) 2017 ATM Consulting <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software; you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU General Public License as published by |
|
7
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
8
|
|
|
* (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU General Public License |
|
16
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace DoliModules\BlockedLog\Model; |
|
20
|
|
|
|
|
21
|
|
|
use DoliDB; |
|
22
|
|
|
use User; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Class to manage certif authority |
|
26
|
|
|
*/ |
|
27
|
|
|
class BlockedLogAuthority |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* DoliDB |
|
31
|
|
|
* @var DoliDB |
|
32
|
|
|
*/ |
|
33
|
|
|
public $db; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Id of the authority |
|
37
|
|
|
* @var int |
|
38
|
|
|
*/ |
|
39
|
|
|
public $id; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var string Ref of the authority |
|
43
|
|
|
*/ |
|
44
|
|
|
public $ref; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Unique fingerprint of the blockchain to store |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
public $signature = ''; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Entire fingerprints blockchain |
|
54
|
|
|
* @var string |
|
55
|
|
|
*/ |
|
56
|
|
|
public $blockchain = ''; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* timestamp |
|
60
|
|
|
* @var int |
|
61
|
|
|
*/ |
|
62
|
|
|
public $tms = 0; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Error message |
|
66
|
|
|
* @var string |
|
67
|
|
|
*/ |
|
68
|
|
|
public $error; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Constructor |
|
72
|
|
|
* |
|
73
|
|
|
* @param DoliDB $db Database handler |
|
74
|
|
|
*/ |
|
75
|
|
|
public function __construct($db) |
|
76
|
|
|
{ |
|
77
|
|
|
$this->db = $db; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Get the blockchain |
|
82
|
|
|
* |
|
83
|
|
|
* @return string blockchain |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getLocalBlockChain() |
|
86
|
|
|
{ |
|
87
|
|
|
$block_static = new BlockedLog($this->db); |
|
88
|
|
|
|
|
89
|
|
|
$this->signature = $block_static->getSignature(); |
|
90
|
|
|
|
|
91
|
|
|
$blocks = $block_static->getLog('all', 0, 0, 'rowid', 'ASC'); |
|
92
|
|
|
|
|
93
|
|
|
$this->blockchain = ''; |
|
94
|
|
|
|
|
95
|
|
|
if (is_array($blocks)) { |
|
96
|
|
|
foreach ($blocks as &$b) { |
|
97
|
|
|
$this->blockchain .= $b->signature; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $this->blockchain; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Get hash of the block chain to check |
|
106
|
|
|
* |
|
107
|
|
|
* @return string hash md5 of blockchain |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getBlockchainHash() |
|
110
|
|
|
{ |
|
111
|
|
|
return md5($this->signature . $this->blockchain); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Get hash of the block chain to check |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $hash hash md5 of blockchain to test |
|
118
|
|
|
* |
|
119
|
|
|
* @return boolean |
|
120
|
|
|
*/ |
|
121
|
|
|
public function checkBlockchain($hash) |
|
122
|
|
|
{ |
|
123
|
|
|
return ($hash === $this->getBlockchainHash()); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Add a new block to the chain |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $block new block to chain |
|
130
|
|
|
* |
|
131
|
|
|
* @return void |
|
132
|
|
|
*/ |
|
133
|
|
|
public function addBlock($block) |
|
134
|
|
|
{ |
|
135
|
|
|
$this->blockchain .= $block; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* hash already exist into chain ? |
|
140
|
|
|
* |
|
141
|
|
|
* @param string $block new block to chain |
|
142
|
|
|
* |
|
143
|
|
|
* @return boolean |
|
144
|
|
|
*/ |
|
145
|
|
|
public function checkBlock($block) |
|
146
|
|
|
{ |
|
147
|
|
|
if (strlen($block) != 64) { |
|
148
|
|
|
return false; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
$blocks = str_split($this->blockchain, 64); |
|
152
|
|
|
|
|
153
|
|
|
if (!in_array($block, $blocks)) { |
|
154
|
|
|
return true; |
|
155
|
|
|
} else { |
|
156
|
|
|
return false; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Get object from database |
|
163
|
|
|
* |
|
164
|
|
|
* @param int $id Id of object to load |
|
165
|
|
|
* @param string $signature Signature of object to load |
|
166
|
|
|
* |
|
167
|
|
|
* @return int >0 if OK, <0 if KO, 0 if not found |
|
168
|
|
|
*/ |
|
169
|
|
|
public function fetch($id, $signature = '') |
|
170
|
|
|
{ |
|
171
|
|
|
global $langs; |
|
172
|
|
|
|
|
173
|
|
|
dol_syslog(get_class($this) . "::fetch id=" . ((int) $id), LOG_DEBUG); |
|
174
|
|
|
|
|
175
|
|
|
if (empty($id) && empty($signature)) { |
|
176
|
|
|
$this->error = 'BadParameter'; |
|
177
|
|
|
return -1; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
$langs->load("blockedlog"); |
|
181
|
|
|
|
|
182
|
|
|
$sql = "SELECT b.rowid, b.signature, b.blockchain, b.tms"; |
|
183
|
|
|
$sql .= " FROM " . MAIN_DB_PREFIX . "blockedlog_authority as b"; |
|
184
|
|
|
|
|
185
|
|
|
if ($id) { |
|
186
|
|
|
$sql .= " WHERE b.rowid = " . ((int) $id); |
|
187
|
|
|
} elseif ($signature) { |
|
188
|
|
|
$sql .= " WHERE b.signature = '" . $this->db->escape($signature) . "'"; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
$resql = $this->db->query($sql); |
|
192
|
|
|
if ($resql) { |
|
193
|
|
|
if ($this->db->num_rows($resql)) { |
|
194
|
|
|
$obj = $this->db->fetch_object($resql); |
|
195
|
|
|
|
|
196
|
|
|
$this->id = $obj->rowid; |
|
197
|
|
|
$this->ref = $obj->rowid; |
|
198
|
|
|
|
|
199
|
|
|
$this->signature = $obj->signature; |
|
200
|
|
|
$this->blockchain = $obj->blockchain; |
|
201
|
|
|
|
|
202
|
|
|
$this->tms = $this->db->jdate($obj->tms); |
|
|
|
|
|
|
203
|
|
|
|
|
204
|
|
|
return 1; |
|
205
|
|
|
} else { |
|
206
|
|
|
$this->error = $langs->trans("RecordNotFound"); |
|
207
|
|
|
return 0; |
|
208
|
|
|
} |
|
209
|
|
|
} else { |
|
210
|
|
|
$this->error = $this->db->error(); |
|
211
|
|
|
return -1; |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Create authority in database. |
|
217
|
|
|
* |
|
218
|
|
|
* @param User $user Object user that create |
|
219
|
|
|
* |
|
220
|
|
|
* @return int Return integer <0 if KO, >0 if OK |
|
221
|
|
|
*/ |
|
222
|
|
|
public function create($user) |
|
223
|
|
|
{ |
|
224
|
|
|
global $conf, $langs, $hookmanager; |
|
225
|
|
|
|
|
226
|
|
|
$langs->load('blockedlog'); |
|
227
|
|
|
|
|
228
|
|
|
$error = 0; |
|
229
|
|
|
|
|
230
|
|
|
dol_syslog(get_class($this) . '::create', LOG_DEBUG); |
|
231
|
|
|
|
|
232
|
|
|
$this->db->begin(); |
|
233
|
|
|
|
|
234
|
|
|
$sql = "INSERT INTO " . MAIN_DB_PREFIX . "blockedlog_authority ("; |
|
235
|
|
|
$sql .= " signature,"; |
|
236
|
|
|
$sql .= " blockchain"; |
|
237
|
|
|
$sql .= ") VALUES ("; |
|
238
|
|
|
$sql .= "'" . $this->db->escape($this->signature) . "',"; |
|
239
|
|
|
$sql .= "'" . $this->db->escape($this->blockchain) . "'"; |
|
240
|
|
|
$sql .= ")"; |
|
241
|
|
|
|
|
242
|
|
|
$res = $this->db->query($sql); |
|
243
|
|
|
if ($res) { |
|
244
|
|
|
$id = $this->db->last_insert_id(MAIN_DB_PREFIX . "blockedlog_authority"); |
|
245
|
|
|
|
|
246
|
|
|
if ($id > 0) { |
|
247
|
|
|
$this->id = $id; |
|
248
|
|
|
|
|
249
|
|
|
$this->db->commit(); |
|
250
|
|
|
|
|
251
|
|
|
return $this->id; |
|
252
|
|
|
} else { |
|
253
|
|
|
$this->db->rollback(); |
|
254
|
|
|
return -2; |
|
255
|
|
|
} |
|
256
|
|
|
} else { |
|
257
|
|
|
$this->error = $this->db->error(); |
|
258
|
|
|
$this->db->rollback(); |
|
259
|
|
|
return -1; |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Create authority in database. |
|
265
|
|
|
* |
|
266
|
|
|
* @param User $user Object user that create |
|
267
|
|
|
* |
|
268
|
|
|
* @return int Return integer <0 if KO, >0 if OK |
|
269
|
|
|
*/ |
|
270
|
|
|
public function update($user) |
|
271
|
|
|
{ |
|
272
|
|
|
global $conf, $langs, $hookmanager; |
|
273
|
|
|
|
|
274
|
|
|
$langs->load('blockedlog'); |
|
275
|
|
|
|
|
276
|
|
|
$error = 0; |
|
277
|
|
|
|
|
278
|
|
|
dol_syslog(get_class($this) . '::create', LOG_DEBUG); |
|
279
|
|
|
|
|
280
|
|
|
$this->db->begin(); |
|
281
|
|
|
|
|
282
|
|
|
$sql = "UPDATE " . MAIN_DB_PREFIX . "blockedlog_authority SET "; |
|
283
|
|
|
$sql .= " blockchain='" . $this->db->escape($this->blockchain) . "'"; |
|
284
|
|
|
$sql .= " WHERE rowid=" . ((int) $this->id); |
|
285
|
|
|
|
|
286
|
|
|
$res = $this->db->query($sql); |
|
287
|
|
|
if ($res) { |
|
288
|
|
|
$this->db->commit(); |
|
289
|
|
|
|
|
290
|
|
|
return 1; |
|
291
|
|
|
} else { |
|
292
|
|
|
$this->error = $this->db->error(); |
|
293
|
|
|
$this->db->rollback(); |
|
294
|
|
|
return -1; |
|
295
|
|
|
} |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* For cron to sync to authority. |
|
300
|
|
|
* |
|
301
|
|
|
* @return int Return integer <0 if KO, >0 if OK |
|
302
|
|
|
*/ |
|
303
|
|
|
public function syncSignatureWithAuthority() |
|
304
|
|
|
{ |
|
305
|
|
|
global $conf, $langs; |
|
306
|
|
|
|
|
307
|
|
|
//TODO create cron task on activation |
|
308
|
|
|
|
|
309
|
|
|
if (!getDolGlobalString('BLOCKEDLOG_AUTHORITY_URL') || !getDolGlobalString('BLOCKEDLOG_USE_REMOTE_AUTHORITY')) { |
|
310
|
|
|
$this->error = $langs->trans('NoAuthorityURLDefined'); |
|
311
|
|
|
return -2; |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
require_once DOL_DOCUMENT_ROOT . '/blockedlog/class/blockedlog.class.php'; |
|
315
|
|
|
|
|
316
|
|
|
$block_static = new BlockedLog($this->db); |
|
317
|
|
|
|
|
318
|
|
|
$blocks = $block_static->getLog('not_certified', 0, 0, 'rowid', 'ASC'); |
|
319
|
|
|
|
|
320
|
|
|
$signature = $block_static->getSignature(); |
|
321
|
|
|
|
|
322
|
|
|
if (is_array($blocks)) { |
|
323
|
|
|
foreach ($blocks as &$block) { |
|
324
|
|
|
$url = getDolGlobalString('BLOCKEDLOG_AUTHORITY_URL') . '/blockedlog/ajax/authority.php?s=' . $signature . '&b=' . $block->signature; |
|
325
|
|
|
|
|
326
|
|
|
$res = getURLContent($url); |
|
327
|
|
|
echo $block->signature . ' ' . $url . ' ' . $res['content'] . '<br>'; |
|
328
|
|
|
if ($res['content'] === 'blockalreadyadded' || $res['content'] === 'blockadded') { |
|
329
|
|
|
$block->setCertified(); |
|
330
|
|
|
} else { |
|
331
|
|
|
$this->error = $langs->trans('ImpossibleToContactAuthority', $url); |
|
332
|
|
|
return -1; |
|
333
|
|
|
} |
|
334
|
|
|
} |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
return 1; |
|
338
|
|
|
} |
|
339
|
|
|
} |
|
340
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.