dolibarr  7.0.0-beta
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  }
58 
64  public function getLocalBlockChain() {
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 
79  return $this->blockchain;
80  }
81 
87  public function getBlockchainHash() {
88 
89  return md5($this->signature.$this->blockchain);
90 
91  }
92 
99  public function checkBlockchain($hash) {
100 
101  return ($hash === $this->getBlockchainHash() );
102 
103  }
104 
110  public function addBlock($block) {
111 
112  $this->blockchain.=$block;
113 
114  }
115 
122  public function checkBlock($block) {
123 
124  if(strlen($block)!=64) return false;
125 
126  $blocks = str_split($this->blockchain,64);
127 
128  if(!in_array($block,$blocks)) {
129  return true;
130  }
131  else{
132  return false;
133  }
134  }
135 
136 
144  public function fetch($id, $signature='') {
145 
146  global $langs;
147 
148  dol_syslog(get_class($this)."::fetch id=".$id, LOG_DEBUG);
149 
150  if (empty($id) && empty($signature))
151  {
152  $this->error='BadParameter';
153  return -1;
154  }
155 
156  $langs->load("blockedlog");
157 
158  $sql = "SELECT b.rowid, b.signature, b.blockchain, b.tms";
159  $sql.= " FROM ".MAIN_DB_PREFIX."blockedlog_authority as b";
160 
161  if ($id) $sql.= " WHERE b.rowid = ". $id;
162  else if($signature)$sql.= " WHERE b.signature = '". $this->db->escape( $signature ) ."'" ;
163 
164  $resql=$this->db->query($sql);
165  if ($resql)
166  {
167  if ($this->db->num_rows($resql))
168  {
169  $obj = $this->db->fetch_object($resql);
170 
171  $this->id = $obj->rowid;
172  $this->ref = $obj->rowid;
173 
174  $this->signature = $obj->signature;
175  $this->blockchain = $obj->blockchain;
176 
177  $this->tms = $this->db->jdate($obj->tms);
178 
179  return 1;
180  }
181  else
182  {
183  $this->error=$langs->trans("RecordNotFound");
184  return 0;
185  }
186  }
187  else
188  {
189  $this->error=$this->db->error();
190  return -1;
191  }
192 
193  }
194 
201  public function create($user) {
202 
203  global $conf,$langs,$hookmanager;
204 
205  $langs->load('blockedlog');
206 
207  $error=0;
208 
209  dol_syslog(get_class($this).'::create', LOG_DEBUG);
210 
211  $this->db->begin();
212 
213  $sql = "INSERT INTO ".MAIN_DB_PREFIX."blockedlog_authority (";
214  $sql.= " signature,";
215  $sql.= " blockchain";
216  $sql.= ") VALUES (";
217  $sql.= "'".$this->db->escape($this->signature)."',";
218  $sql.= "'".$this->db->escape($this->blockchain)."'";
219  $sql.= ")";
220 
221  $res = $this->db->query($sql);
222  if ($res)
223  {
224  $id = $this->db->last_insert_id(MAIN_DB_PREFIX."blockedlog_authority");
225 
226  if ($id > 0)
227  {
228  $this->id = $id;
229 
230  $this->db->commit();
231 
232  return $this->id;
233  }
234  else
235  {
236  $this->db->rollback();
237  return -2;
238  }
239  }
240  else
241  {
242  $this->error=$this->db->error();
243  $this->db->rollback();
244  return -1;
245  }
246 
247  }
248 
255  public function update($user) {
256 
257  global $conf,$langs,$hookmanager;
258 
259  $langs->load('blockedlog');
260 
261  $error=0;
262 
263  dol_syslog(get_class($this).'::create', LOG_DEBUG);
264 
265  $this->db->begin();
266 
267  $sql = "UPDATE ".MAIN_DB_PREFIX."blockedlog_authority SET ";
268  $sql.= " blockchain='".$this->db->escape($this->blockchain)."'";
269  $sql.= " WHERE rowid=".$this->id;
270 
271  $res = $this->db->query($sql);
272  if ($res)
273  {
274  $this->db->commit();
275 
276  return 1;
277  }
278  else
279  {
280  $this->error=$this->db->error();
281  $this->db->rollback();
282  return -1;
283  }
284 
285  }
286 
292  public function syncSignatureWithAuthority() {
293  global $conf, $langs;
294 
295  //TODO create cron task on activation
296 
297  if(empty($conf->global->BLOCKEDLOG_AUTHORITY_URL) || empty($conf->global->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  foreach($blocks as &$block) {
311 
312  $url = $conf->global->BLOCKEDLOG_AUTHORITY_URL.'/blockedlog/ajax/authority.php?s='.$signature.'&b='.$block->signature;
313 
314  $res = file_get_contents($url);
315  echo $block->signature.' '.$url. ' '.$res.'<br>';
316  if($res === 'blockalreadyadded' || $res === 'blockadded') {
317 
318  $block->setCertified();
319 
320  }
321  else {
322 
323  $this->error = $langs->trans('ImpossibleToContactAuthority ',$url);
324  return -1;
325  }
326 
327 
328  }
329 
330  return 1;
331  }
332 
333 }
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.
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.
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:1013
getBlockchainHash()
Get hash of the block chain to check.