dolibarr 18.0.6
authority.class.php
1<?php
2/* Copyright (C) 2017 ATM Consulting <contact@atm-consulting.fr>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
22{
27 public $db;
28
33 public $id;
34
39 public $signature = '';
40
45 public $blockchain = '';
46
51 public $tms = 0;
52
57 public $error;
58
64 public function __construct($db)
65 {
66 $this->db = $db;
67 }
68
74 public function getLocalBlockChain()
75 {
76
77 $block_static = new BlockedLog($this->db);
78
79 $this->signature = $block_static->getSignature();
80
81 $blocks = $block_static->getLog('all', 0, 0, 'rowid', 'ASC');
82
83 $this->blockchain = '';
84
85 if (is_array($blocks)) {
86 foreach ($blocks as &$b) {
87 $this->blockchain .= $b->signature;
88 }
89 }
90
91 return $this->blockchain;
92 }
93
99 public function getBlockchainHash()
100 {
101
102 return md5($this->signature.$this->blockchain);
103 }
104
111 public function checkBlockchain($hash)
112 {
113
114 return ($hash === $this->getBlockchainHash());
115 }
116
123 public function addBlock($block)
124 {
125
126 $this->blockchain .= $block;
127 }
128
135 public function checkBlock($block)
136 {
137
138 if (strlen($block) != 64) {
139 return false;
140 }
141
142 $blocks = str_split($this->blockchain, 64);
143
144 if (!in_array($block, $blocks)) {
145 return true;
146 } else {
147 return false;
148 }
149 }
150
151
159 public function fetch($id, $signature = '')
160 {
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 $this->error = $langs->trans("RecordNotFound");
198 return 0;
199 }
200 } else {
201 $this->error = $this->db->error();
202 return -1;
203 }
204 }
205
212 public function create($user)
213 {
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
263 global $conf, $langs, $hookmanager;
264
265 $langs->load('blockedlog');
266
267 $error = 0;
268
269 dol_syslog(get_class($this).'::create', LOG_DEBUG);
270
271 $this->db->begin();
272
273 $sql = "UPDATE ".MAIN_DB_PREFIX."blockedlog_authority SET ";
274 $sql .= " blockchain='".$this->db->escape($this->blockchain)."'";
275 $sql .= " WHERE rowid=".((int) $this->id);
276
277 $res = $this->db->query($sql);
278 if ($res) {
279 $this->db->commit();
280
281 return 1;
282 } else {
283 $this->error = $this->db->error();
284 $this->db->rollback();
285 return -1;
286 }
287 }
288
295 {
296 global $conf, $langs;
297
298 //TODO create cron task on activation
299
300 if (empty($conf->global->BLOCKEDLOG_AUTHORITY_URL) || empty($conf->global->BLOCKEDLOG_USE_REMOTE_AUTHORITY)) {
301 $this->error = $langs->trans('NoAuthorityURLDefined');
302 return -2;
303 }
304
305 require_once DOL_DOCUMENT_ROOT.'/blockedlog/class/blockedlog.class.php';
306
307 $block_static = new BlockedLog($this->db);
308
309 $blocks = $block_static->getLog('not_certified', 0, 0, 'rowid', 'ASC');
310
311 $signature = $block_static->getSignature();
312
313 if (is_array($blocks)) {
314 foreach ($blocks as &$block) {
315 $url = $conf->global->BLOCKEDLOG_AUTHORITY_URL.'/blockedlog/ajax/authority.php?s='.$signature.'&b='.$block->signature;
316
317 $res = getURLContent($url);
318 echo $block->signature.' '.$url.' '.$res['content'].'<br>';
319 if ($res['content'] === 'blockalreadyadded' || $res['content'] === 'blockadded') {
320 $block->setCertified();
321 } else {
322 $this->error = $langs->trans('ImpossibleToContactAuthority ', $url);
323 return -1;
324 }
325 }
326 }
327
328 return 1;
329 }
330}
$object ref
Definition info.php:78
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.
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)
Function to get a content from an URL (use proxy if proxy defined).