dolibarr  9.0.0
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 <http://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  }
132  else{
133  return false;
134  }
135  }
136 
137 
145  public function fetch($id, $signature='')
146  {
147 
148  global $langs;
149 
150  dol_syslog(get_class($this)."::fetch id=".$id, LOG_DEBUG);
151 
152  if (empty($id) && empty($signature))
153  {
154  $this->error='BadParameter';
155  return -1;
156  }
157 
158  $langs->load("blockedlog");
159 
160  $sql = "SELECT b.rowid, b.signature, b.blockchain, b.tms";
161  $sql.= " FROM ".MAIN_DB_PREFIX."blockedlog_authority as b";
162 
163  if ($id) $sql.= " WHERE b.rowid = ". $id;
164  else if($signature)$sql.= " WHERE b.signature = '". $this->db->escape( $signature ) ."'" ;
165 
166  $resql=$this->db->query($sql);
167  if ($resql)
168  {
169  if ($this->db->num_rows($resql))
170  {
171  $obj = $this->db->fetch_object($resql);
172 
173  $this->id = $obj->rowid;
174  $this->ref = $obj->rowid;
175 
176  $this->signature = $obj->signature;
177  $this->blockchain = $obj->blockchain;
178 
179  $this->tms = $this->db->jdate($obj->tms);
180 
181  return 1;
182  }
183  else
184  {
185  $this->error=$langs->trans("RecordNotFound");
186  return 0;
187  }
188  }
189  else
190  {
191  $this->error=$this->db->error();
192  return -1;
193  }
194  }
195 
202  public function create($user)
203  {
204 
205  global $conf,$langs,$hookmanager;
206 
207  $langs->load('blockedlog');
208 
209  $error=0;
210 
211  dol_syslog(get_class($this).'::create', LOG_DEBUG);
212 
213  $this->db->begin();
214 
215  $sql = "INSERT INTO ".MAIN_DB_PREFIX."blockedlog_authority (";
216  $sql.= " signature,";
217  $sql.= " blockchain";
218  $sql.= ") VALUES (";
219  $sql.= "'".$this->db->escape($this->signature)."',";
220  $sql.= "'".$this->db->escape($this->blockchain)."'";
221  $sql.= ")";
222 
223  $res = $this->db->query($sql);
224  if ($res)
225  {
226  $id = $this->db->last_insert_id(MAIN_DB_PREFIX."blockedlog_authority");
227 
228  if ($id > 0)
229  {
230  $this->id = $id;
231 
232  $this->db->commit();
233 
234  return $this->id;
235  }
236  else
237  {
238  $this->db->rollback();
239  return -2;
240  }
241  }
242  else
243  {
244  $this->error=$this->db->error();
245  $this->db->rollback();
246  return -1;
247  }
248  }
249 
256  public function update($user)
257  {
258 
259  global $conf,$langs,$hookmanager;
260 
261  $langs->load('blockedlog');
262 
263  $error=0;
264 
265  dol_syslog(get_class($this).'::create', LOG_DEBUG);
266 
267  $this->db->begin();
268 
269  $sql = "UPDATE ".MAIN_DB_PREFIX."blockedlog_authority SET ";
270  $sql.= " blockchain='".$this->db->escape($this->blockchain)."'";
271  $sql.= " WHERE rowid=".$this->id;
272 
273  $res = $this->db->query($sql);
274  if ($res)
275  {
276  $this->db->commit();
277 
278  return 1;
279  }
280  else
281  {
282  $this->error=$this->db->error();
283  $this->db->rollback();
284  return -1;
285  }
286  }
287 
293  public function syncSignatureWithAuthority()
294  {
295  global $conf, $langs;
296 
297  //TODO create cron task on activation
298 
299  if(empty($conf->global->BLOCKEDLOG_AUTHORITY_URL) || empty($conf->global->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  foreach($blocks as &$block) {
313 
314  $url = $conf->global->BLOCKEDLOG_AUTHORITY_URL.'/blockedlog/ajax/authority.php?s='.$signature.'&b='.$block->signature;
315 
316  $res = file_get_contents($url);
317  echo $block->signature.' '.$url. ' '.$res.'<br>';
318  if($res === 'blockalreadyadded' || $res === 'blockadded') {
319 
320  $block->setCertified();
321  }
322  else {
323 
324  $this->error = $langs->trans('ImpossibleToContactAuthority ',$url);
325  return -1;
326  }
327  }
328 
329  return 1;
330  }
331 }
syncSignatureWithAuthority()
For cron to sync to authority.
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->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) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
addBlock($block)
Add a new block to the chain.
update($user)
Create authority in database.
__construct($db)
Constructor.
Class to manage certif authority.
fetch($id, $signature='')
Get object from database.
create($user)
Create authority in database.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
Class to manage Blocked Log.
checkBlock($block)
hash already exist into chain ?
checkBlockchain($hash)
Get hash of the block chain to check.
getLocalBlockChain()
Get the blockchain.
getBlockchainHash()
Get hash of the block chain to check.