dolibarr 22.0.5
authority.class.php
1<?php
2/* Copyright (C) 2017 ATM Consulting <contact@atm-consulting.fr>
3 * Copyright (C) 2025 Frédéric France <frederic.france@free.fr>
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
23{
28 public $db;
29
34 public $id;
35
39 public $ref;
40
45 public $signature = '';
46
51 public $blockchain = '';
52
57 public $tms = 0;
58
63 public $error;
64
70 public function __construct($db)
71 {
72 $this->db = $db;
73 }
74
80 public function getLocalBlockChain()
81 {
82 $block_static = new BlockedLog($this->db);
83
84 $this->signature = $block_static->getSignature();
85
86 $blocks = $block_static->getLog('all', 0, 0, 'rowid', 'ASC');
87
88 $this->blockchain = '';
89
90 if (is_array($blocks)) {
91 foreach ($blocks as &$b) {
92 $this->blockchain .= $b->signature;
93 }
94 }
95
96 return $this->blockchain;
97 }
98
104 public function getBlockchainHash()
105 {
106 return md5($this->signature.$this->blockchain);
107 }
108
115 public function checkBlockchain($hash)
116 {
117 return ($hash === $this->getBlockchainHash());
118 }
119
126 public function addBlock($block)
127 {
128 $this->blockchain .= $block;
129 }
130
137 public function checkBlock($block)
138 {
139 if (strlen($block) != 64) {
140 return false;
141 }
142
143 $blocks = str_split($this->blockchain, 64);
144
145 if (!in_array($block, $blocks)) {
146 return true;
147 } else {
148 return false;
149 }
150 }
151
152
160 public function fetch($id, $signature = '')
161 {
162 global $langs;
163
164 dol_syslog(get_class($this)."::fetch id=".((int) $id), LOG_DEBUG);
165
166 if (empty($id) && empty($signature)) {
167 $this->error = 'BadParameter';
168 return -1;
169 }
170
171 $langs->load("blockedlog");
172
173 $sql = "SELECT b.rowid, b.signature, b.blockchain, b.tms";
174 $sql .= " FROM ".MAIN_DB_PREFIX."blockedlog_authority as b";
175
176 if ($id) {
177 $sql .= " WHERE b.rowid = ".((int) $id);
178 } elseif ($signature) {
179 $sql .= " WHERE b.signature = '".$this->db->escape($signature)."'";
180 }
181
182 $resql = $this->db->query($sql);
183 if ($resql) {
184 if ($this->db->num_rows($resql)) {
185 $obj = $this->db->fetch_object($resql);
186
187 $this->id = $obj->rowid;
188 $this->ref = $obj->rowid;
189
190 $this->signature = $obj->signature;
191 $this->blockchain = $obj->blockchain;
192
193 $this->tms = $this->db->jdate($obj->tms);
194
195 return 1;
196 } else {
197 $langs->load("errors");
198 $this->error = $langs->trans("ErrorRecordNotFound");
199 return 0;
200 }
201 } else {
202 $this->error = $this->db->error();
203 return -1;
204 }
205 }
206
213 public function create($user)
214 {
215 global $conf, $langs, $hookmanager;
216
217 $langs->load('blockedlog');
218
219 $error = 0;
220
221 dol_syslog(get_class($this).'::create', LOG_DEBUG);
222
223 $this->db->begin();
224
225 $sql = "INSERT INTO ".MAIN_DB_PREFIX."blockedlog_authority (";
226 $sql .= " signature,";
227 $sql .= " blockchain";
228 $sql .= ") VALUES (";
229 $sql .= "'".$this->db->escape($this->signature)."',";
230 $sql .= "'".$this->db->escape($this->blockchain)."'";
231 $sql .= ")";
232
233 $res = $this->db->query($sql);
234 if ($res) {
235 $id = $this->db->last_insert_id(MAIN_DB_PREFIX."blockedlog_authority");
236
237 if ($id > 0) {
238 $this->id = $id;
239
240 $this->db->commit();
241
242 return $this->id;
243 } else {
244 $this->db->rollback();
245 return -2;
246 }
247 } else {
248 $this->error = $this->db->error();
249 $this->db->rollback();
250 return -1;
251 }
252 }
253
260 public function update($user)
261 {
262 global $conf, $langs, $hookmanager;
263
264 $langs->load('blockedlog');
265
266 $error = 0;
267
268 dol_syslog(get_class($this).'::create', LOG_DEBUG);
269
270 $this->db->begin();
271
272 $sql = "UPDATE ".MAIN_DB_PREFIX."blockedlog_authority SET ";
273 $sql .= " blockchain='".$this->db->escape($this->blockchain)."'";
274 $sql .= " WHERE rowid=".((int) $this->id);
275
276 $res = $this->db->query($sql);
277 if ($res) {
278 $this->db->commit();
279
280 return 1;
281 } else {
282 $this->error = $this->db->error();
283 $this->db->rollback();
284 return -1;
285 }
286 }
287
294 {
295 global $conf, $langs;
296
297 //TODO create cron task on activation
298
299 if (!getDolGlobalString('BLOCKEDLOG_AUTHORITY_URL') || !getDolGlobalString('BLOCKEDLOG_USE_REMOTE_AUTHORITY')) {
300 $this->error = $langs->trans('NoAuthorityURLDefined');
301 return -2;
302 }
303
304 require_once DOL_DOCUMENT_ROOT.'/blockedlog/class/blockedlog.class.php';
305
306 $block_static = new BlockedLog($this->db);
307
308 $blocks = $block_static->getLog('not_certified', 0, 0, 'rowid', 'ASC');
309
310 $signature = $block_static->getSignature();
311
312 if (is_array($blocks)) {
313 foreach ($blocks as &$block) {
314 $url = getDolGlobalString('BLOCKEDLOG_AUTHORITY_URL') . '/blockedlog/ajax/authority.php?s='.$signature.'&b='.$block->signature;
315
316 $res = getURLContent($url);
317 echo $block->signature.' '.$url.' '.$res['content'].'<br>';
318 if ($res['content'] === 'blockalreadyadded' || $res['content'] === 'blockadded') {
319 $block->setCertified();
320 } else {
321 $this->error = $langs->trans('ImpossibleToContactAuthority', $url);
322 return -1;
323 }
324 }
325 }
326
327 return 1;
328 }
329}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:48
$object ref
Definition info.php:90
Class to manage certif authority.
fetch($id, $signature='')
Get object from database.
checkBlock($block)
hash already exist into chain ?
create($user)
Create authority in database.
__construct($db)
Constructor.
getLocalBlockChain()
Get the blockchain.
syncSignatureWithAuthority()
For cron to sync to authority.
update($user)
Create authority in database.
checkBlockchain($hash)
Get hash of the block chain to check.
addBlock($block)
Add a new block to the chain.
getBlockchainHash()
Get hash of the block chain to check.
Class to manage Blocked Log.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getURLContent($url, $postorget='GET', $param='', $followlocation=1, $addheaders=array(), $allowedschemes=array('http', 'https'), $localurl=0, $ssl_verifypeer=-1, $timeoutconnect=0, $timeoutresponse=0)
Function to get a content from an URL (use proxy if proxy defined).
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79