dolibarr 21.0.0-alpha
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
38 public $ref;
39
44 public $signature = '';
45
50 public $blockchain = '';
51
56 public $tms = 0;
57
62 public $error;
63
69 public function __construct($db)
70 {
71 $this->db = $db;
72 }
73
79 public function getLocalBlockChain()
80 {
81 $block_static = new BlockedLog($this->db);
82
83 $this->signature = $block_static->getSignature();
84
85 $blocks = $block_static->getLog('all', 0, 0, 'rowid', 'ASC');
86
87 $this->blockchain = '';
88
89 if (is_array($blocks)) {
90 foreach ($blocks as &$b) {
91 $this->blockchain .= $b->signature;
92 }
93 }
94
95 return $this->blockchain;
96 }
97
103 public function getBlockchainHash()
104 {
105 return md5($this->signature.$this->blockchain);
106 }
107
114 public function checkBlockchain($hash)
115 {
116 return ($hash === $this->getBlockchainHash());
117 }
118
125 public function addBlock($block)
126 {
127 $this->blockchain .= $block;
128 }
129
136 public function checkBlock($block)
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 global $langs;
162
163 dol_syslog(get_class($this)."::fetch id=".((int) $id), LOG_DEBUG);
164
165 if (empty($id) && empty($signature)) {
166 $this->error = 'BadParameter';
167 return -1;
168 }
169
170 $langs->load("blockedlog");
171
172 $sql = "SELECT b.rowid, b.signature, b.blockchain, b.tms";
173 $sql .= " FROM ".MAIN_DB_PREFIX."blockedlog_authority as b";
174
175 if ($id) {
176 $sql .= " WHERE b.rowid = ".((int) $id);
177 } elseif ($signature) {
178 $sql .= " WHERE b.signature = '".$this->db->escape($signature)."'";
179 }
180
181 $resql = $this->db->query($sql);
182 if ($resql) {
183 if ($this->db->num_rows($resql)) {
184 $obj = $this->db->fetch_object($resql);
185
186 $this->id = $obj->rowid;
187 $this->ref = $obj->rowid;
188
189 $this->signature = $obj->signature;
190 $this->blockchain = $obj->blockchain;
191
192 $this->tms = $this->db->jdate($obj->tms);
193
194 return 1;
195 } else {
196 $this->error = $langs->trans("RecordNotFound");
197 return 0;
198 }
199 } else {
200 $this->error = $this->db->error();
201 return -1;
202 }
203 }
204
211 public function create($user)
212 {
213 global $conf, $langs, $hookmanager;
214
215 $langs->load('blockedlog');
216
217 $error = 0;
218
219 dol_syslog(get_class($this).'::create', LOG_DEBUG);
220
221 $this->db->begin();
222
223 $sql = "INSERT INTO ".MAIN_DB_PREFIX."blockedlog_authority (";
224 $sql .= " signature,";
225 $sql .= " blockchain";
226 $sql .= ") VALUES (";
227 $sql .= "'".$this->db->escape($this->signature)."',";
228 $sql .= "'".$this->db->escape($this->blockchain)."'";
229 $sql .= ")";
230
231 $res = $this->db->query($sql);
232 if ($res) {
233 $id = $this->db->last_insert_id(MAIN_DB_PREFIX."blockedlog_authority");
234
235 if ($id > 0) {
236 $this->id = $id;
237
238 $this->db->commit();
239
240 return $this->id;
241 } else {
242 $this->db->rollback();
243 return -2;
244 }
245 } else {
246 $this->error = $this->db->error();
247 $this->db->rollback();
248 return -1;
249 }
250 }
251
258 public function update($user)
259 {
260 global $conf, $langs, $hookmanager;
261
262 $langs->load('blockedlog');
263
264 $error = 0;
265
266 dol_syslog(get_class($this).'::create', LOG_DEBUG);
267
268 $this->db->begin();
269
270 $sql = "UPDATE ".MAIN_DB_PREFIX."blockedlog_authority SET ";
271 $sql .= " blockchain='".$this->db->escape($this->blockchain)."'";
272 $sql .= " WHERE rowid=".((int) $this->id);
273
274 $res = $this->db->query($sql);
275 if ($res) {
276 $this->db->commit();
277
278 return 1;
279 } else {
280 $this->error = $this->db->error();
281 $this->db->rollback();
282 return -1;
283 }
284 }
285
292 {
293 global $conf, $langs;
294
295 //TODO create cron task on activation
296
297 if (!getDolGlobalString('BLOCKEDLOG_AUTHORITY_URL') || !getDolGlobalString('BLOCKEDLOG_USE_REMOTE_AUTHORITY')) {
298 $this->error = $langs->trans('NoAuthorityURLDefined');
299 return -2;
300 }
301
302 require_once DOL_DOCUMENT_ROOT.'/blockedlog/class/blockedlog.class.php';
303
304 $block_static = new BlockedLog($this->db);
305
306 $blocks = $block_static->getLog('not_certified', 0, 0, 'rowid', 'ASC');
307
308 $signature = $block_static->getSignature();
309
310 if (is_array($blocks)) {
311 foreach ($blocks as &$block) {
312 $url = getDolGlobalString('BLOCKEDLOG_AUTHORITY_URL') . '/blockedlog/ajax/authority.php?s='.$signature.'&b='.$block->signature;
313
314 $res = getURLContent($url);
315 echo $block->signature.' '.$url.' '.$res['content'].'<br>';
316 if ($res['content'] === 'blockalreadyadded' || $res['content'] === 'blockadded') {
317 $block->setCertified();
318 } else {
319 $this->error = $langs->trans('ImpossibleToContactAuthority', $url);
320 return -1;
321 }
322 }
323 }
324
325 return 1;
326 }
327}
$id
Definition account.php:39
$object ref
Definition info.php:79
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)
Function to get a content from an URL (use proxy if proxy defined).