dolibarr  13.0.2
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 {
23 
28  public $id;
29 
34  public $signature = '';
35 
40  public $blockchain = '';
41 
46  public $tms = 0;
47 
53  public function __construct($db)
54  {
55  $this->db = $db;
56  }
57 
63  public function getLocalBlockChain()
64  {
65 
66  $block_static = new BlockedLog($this->db);
67 
68  $this->signature = $block_static->getSignature();
69 
70  $blocks = $block_static->getLog('all', 0, 0, 'rowid', 'ASC');
71 
72  $this->blockchain = '';
73 
74  foreach ($blocks as &$b) {
75  $this->blockchain .= $b->signature;
76  }
77 
78  return $this->blockchain;
79  }
80 
86  public function getBlockchainHash()
87  {
88 
89  return md5($this->signature.$this->blockchain);
90  }
91 
98  public function checkBlockchain($hash)
99  {
100 
101  return ($hash === $this->getBlockchainHash());
102  }
103 
110  public function addBlock($block)
111  {
112 
113  $this->blockchain .= $block;
114  }
115 
122  public function checkBlock($block)
123  {
124 
125  if (strlen($block) != 64) return false;
126 
127  $blocks = str_split($this->blockchain, 64);
128 
129  if (!in_array($block, $blocks)) {
130  return true;
131  } else {
132  return false;
133  }
134  }
135 
136 
144  public function fetch($id, $signature = '')
145  {
146 
147  global $langs;
148 
149  dol_syslog(get_class($this)."::fetch id=".$id, LOG_DEBUG);
150 
151  if (empty($id) && empty($signature))
152  {
153  $this->error = 'BadParameter';
154  return -1;
155  }
156 
157  $langs->load("blockedlog");
158 
159  $sql = "SELECT b.rowid, b.signature, b.blockchain, b.tms";
160  $sql .= " FROM ".MAIN_DB_PREFIX."blockedlog_authority as b";
161 
162  if ($id) $sql .= " WHERE b.rowid = ".$id;
163  elseif ($signature)$sql .= " WHERE b.signature = '".$this->db->escape($signature)."'";
164 
165  $resql = $this->db->query($sql);
166  if ($resql)
167  {
168  if ($this->db->num_rows($resql))
169  {
170  $obj = $this->db->fetch_object($resql);
171 
172  $this->id = $obj->rowid;
173  $this->ref = $obj->rowid;
174 
175  $this->signature = $obj->signature;
176  $this->blockchain = $obj->blockchain;
177 
178  $this->tms = $this->db->jdate($obj->tms);
179 
180  return 1;
181  } else {
182  $this->error = $langs->trans("RecordNotFound");
183  return 0;
184  }
185  } else {
186  $this->error = $this->db->error();
187  return -1;
188  }
189  }
190 
197  public function create($user)
198  {
199 
200  global $conf, $langs, $hookmanager;
201 
202  $langs->load('blockedlog');
203 
204  $error = 0;
205 
206  dol_syslog(get_class($this).'::create', LOG_DEBUG);
207 
208  $this->db->begin();
209 
210  $sql = "INSERT INTO ".MAIN_DB_PREFIX."blockedlog_authority (";
211  $sql .= " signature,";
212  $sql .= " blockchain";
213  $sql .= ") VALUES (";
214  $sql .= "'".$this->db->escape($this->signature)."',";
215  $sql .= "'".$this->db->escape($this->blockchain)."'";
216  $sql .= ")";
217 
218  $res = $this->db->query($sql);
219  if ($res)
220  {
221  $id = $this->db->last_insert_id(MAIN_DB_PREFIX."blockedlog_authority");
222 
223  if ($id > 0)
224  {
225  $this->id = $id;
226 
227  $this->db->commit();
228 
229  return $this->id;
230  } else {
231  $this->db->rollback();
232  return -2;
233  }
234  } else {
235  $this->error = $this->db->error();
236  $this->db->rollback();
237  return -1;
238  }
239  }
240 
247  public function update($user)
248  {
249 
250  global $conf, $langs, $hookmanager;
251 
252  $langs->load('blockedlog');
253 
254  $error = 0;
255 
256  dol_syslog(get_class($this).'::create', LOG_DEBUG);
257 
258  $this->db->begin();
259 
260  $sql = "UPDATE ".MAIN_DB_PREFIX."blockedlog_authority SET ";
261  $sql .= " blockchain='".$this->db->escape($this->blockchain)."'";
262  $sql .= " WHERE rowid=".$this->id;
263 
264  $res = $this->db->query($sql);
265  if ($res)
266  {
267  $this->db->commit();
268 
269  return 1;
270  } else {
271  $this->error = $this->db->error();
272  $this->db->rollback();
273  return -1;
274  }
275  }
276 
282  public function syncSignatureWithAuthority()
283  {
284  global $conf, $langs;
285 
286  //TODO create cron task on activation
287 
288  if (empty($conf->global->BLOCKEDLOG_AUTHORITY_URL) || empty($conf->global->BLOCKEDLOG_USE_REMOTE_AUTHORITY)) {
289  $this->error = $langs->trans('NoAuthorityURLDefined');
290  return -2;
291  }
292 
293  require_once DOL_DOCUMENT_ROOT.'/blockedlog/class/blockedlog.class.php';
294 
295  $block_static = new BlockedLog($this->db);
296 
297  $blocks = $block_static->getLog('not_certified', 0, 0, 'rowid', 'ASC');
298 
299  $signature = $block_static->getSignature();
300 
301  foreach ($blocks as &$block) {
302  $url = $conf->global->BLOCKEDLOG_AUTHORITY_URL.'/blockedlog/ajax/authority.php?s='.$signature.'&b='.$block->signature;
303 
304  $res = file_get_contents($url);
305  echo $block->signature.' '.$url.' '.$res.'<br>';
306  if ($res === 'blockalreadyadded' || $res === 'blockadded') {
307  $block->setCertified();
308  } else {
309  $this->error = $langs->trans('ImpossibleToContactAuthority ', $url);
310  return -1;
311  }
312  }
313 
314  return 1;
315  }
316 }
syncSignatureWithAuthority()
For cron to sync to authority.
addBlock($block)
Add a new block to the chain.
update($user)
Create authority in database.
__construct($db)
Constructor.
Class to manage certif authority.
$conf db
API class for accounts.
Definition: inc.php:54
create($user)
Create authority in database.
Class to manage Blocked Log.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
checkBlock($block)
hash already exist into chain ?
checkBlockchain($hash)
Get hash of the block chain to check.
getLocalBlockChain()
Get the blockchain.
fetch($id, $signature= '')
Get object from database.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
getBlockchainHash()
Get hash of the block chain to check.