dolibarr  7.0.0-beta
blockedlog.class.php
1 <?php
2 /* Copyright (C) 2017 ATM Consulting <contact@atm-consulting.fr>
3  * Copyright (C) 2017 Laurent Destailleur <eldy@destailleur.fr>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * See https://medium.com/@lhartikk/a-blockchain-in-200-lines-of-code-963cc1cc0e54
19  */
20 
26 {
31  public $id;
36  public $entity;
37 
38  public $error = '';
39  public $errors = array();
40 
45  public $signature = '';
46 
51  public $signature_line = '';
52 
53  public $amounts = null;
54 
59  public $action = '';
60 
65  public $element = '';
66 
71  public $fk_object = 0;
72 
77  public $certified = false;
78 
83  public $fk_user = 0;
84 
85  public $date_creation;
86  public $date_modification;
87 
88  public $date_object = 0;
89 
90  public $ref_object = '';
91 
92  public $object_data = null;
93 
98  public $trackedevents = array();
99 
100 
101 
107  public function __construct(DoliDB $db)
108  {
109  global $conf;
110 
111  $this->db = $db;
112 
113  $this->trackedevents = array();
114 
115  if ($conf->facture->enabled) $this->trackedevents['BILL_VALIDATE']='logBILL_VALIDATE';
116  if ($conf->facture->enabled) $this->trackedevents['BILL_DELETE']='logBILL_DELETE';
117  if ($conf->facture->enabled) $this->trackedevents['BILL_SENTBYMAIL']='logBILL_SENTBYMAIL';
118  if ($conf->facture->enabled) $this->trackedevents['DOC_DOWNLOAD']='BlockedLogBillDownload';
119  if ($conf->facture->enabled) $this->trackedevents['DOC_PREVIEW']='BlockedLogBillPreview';
120 
121  if ($conf->facture->enabled) $this->trackedevents['PAYMENT_CUSTOMER_CREATE']='logPAYMENT_CUSTOMER_CREATE';
122  if ($conf->facture->enabled) $this->trackedevents['PAYMENT_CUSTOMER_DELETE']='logPAYMENT_CUSTOMER_DELETE';
123 
124  /* Supplier
125  if ($conf->fournisseur->enabled) $this->trackedevents['BILL_SUPPLIER_VALIDATE']='BlockedLogSupplierBillValidate';
126  if ($conf->fournisseur->enabled) $this->trackedevents['BILL_SUPPLIER_DELETE']='BlockedLogSupplierBillDelete';
127  if ($conf->fournisseur->enabled) $this->trackedevents['BILL_SUPPLIER_SENTBYMAIL']='BlockedLogSupplierBillSentByEmail'; // Trigger key does not exists, we want just into array to list it as done
128  if ($conf->fournisseur->enabled) $this->trackedevents['SUPPLIER_DOC_DOWNLOAD']='BlockedLogSupplierBillDownload'; // Trigger key does not exists, we want just into array to list it as done
129  if ($conf->fournisseur->enabled) $this->trackedevents['SUPPLIER_DOC_PREVIEW']='BlockedLogSupplierBillPreview'; // Trigger key does not exists, we want just into array to list it as done
130 
131  if ($conf->fournisseur->enabled) $this->trackedevents['PAYMENT_SUPPLIER_CREATE']='BlockedLogSupplierBillPaymentCreate';
132  if ($conf->fournisseur->enabled) $this->trackedevents['PAYMENT_SUPPLIER_DELETE']='BlockedLogsupplierBillPaymentCreate';
133  */
134 
135  if ($conf->don->enabled) $this->trackedevents['DON_VALIDATE']='logDON_VALIDATE';
136  if ($conf->don->enabled) $this->trackedevents['DON_DELETE']='logDON_DELETE';
137  //if ($conf->don->enabled) $this->trackedevents['DON_SENTBYMAIL']='logDON_SENTBYMAIL';
138 
139  if ($conf->don->enabled) $this->trackedevents['DONATION_PAYMENT_CREATE']='logDONATION_PAYMENT_CREATE';
140  if ($conf->don->enabled) $this->trackedevents['DONATION_PAYMENT_DELETE']='logDONATION_PAYMENT_DELETE';
141 
142  /*
143  if ($conf->salary->enabled) $this->trackedevents['PAYMENT_SALARY_CREATE']='BlockedLogSalaryPaymentCreate';
144  if ($conf->salary->enabled) $this->trackedevents['PAYMENT_SALARY_MODIFY']='BlockedLogSalaryPaymentCreate';
145  if ($conf->salary->enabled) $this->trackedevents['PAYMENT_SALARY_DELETE']='BlockedLogSalaryPaymentCreate';
146  */
147 
148  if ($conf->adherent->enabled) $this->trackedevents['MEMBER_SUBSCRIPTION_CREATE']='logMEMBER_SUBSCRIPTION_CREATE';
149  if ($conf->adherent->enabled) $this->trackedevents['MEMBER_SUBSCRIPTION_MODIFY']='logMEMBER_SUBSCRIPTION_MODIFY';
150  if ($conf->adherent->enabled) $this->trackedevents['MEMBER_SUBSCRIPTION_DELETE']='logMEMBER_SUBSCRIPTION_DELETE';
151 
152  /*
153  $trackedevents['PAYMENT_VARIOUS_CREATE']='BlockedLogVariousPaymentCreate';
154  $trackedevents['PAYMENT_VARIOUS_MODIFY']='BlockedLogVariousPaymentModify';
155  $trackedevents['PAYMENT_VARIOUS_DELETE']='BlockedLogVariousPaymentDelete';
156  */
157  }
158 
162  public function getObjectLink()
163  {
164  global $langs;
165 
166  if($this->element === 'facture') {
167  require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
168 
169  $object = new Facture($this->db);
170  if ($object->fetch($this->fk_object)>0) {
171  return $object->getNomUrl(1);
172  }
173  else{
174  $this->error++;
175  }
176  }
177  if($this->element === 'invoice_supplier') {
178  require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
179 
180  $object = new FactureFournisseur($this->db);
181  if ($object->fetch($this->fk_object)>0) {
182  return $object->getNomUrl(1);
183  }
184  else{
185  $this->error++;
186  }
187  }
188  else if($this->element === 'payment') {
189  require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
190 
191  $object = new Paiement($this->db);
192  if ($object->fetch($this->fk_object)>0) {
193  return $object->getNomUrl(1);
194  }
195  else{
196  $this->error++;
197  }
198  }
199  else if($this->element === 'payment_supplier') {
200  require_once DOL_DOCUMENT_ROOT.'/fourn/class/paiementfourn.class.php';
201 
202  $object = new PaiementFourn($this->db);
203  if ($object->fetch($this->fk_object)>0) {
204  return $object->getNomUrl(1);
205  }
206  else{
207  $this->error++;
208  }
209  }
210  else if($this->element === 'payment_donation') {
211  require_once DOL_DOCUMENT_ROOT.'/don/class/paymentdonation.class.php';
212 
213  $object = new PaymentDonation($this->db);
214  if ($object->fetch($this->fk_object)>0) {
215  return $object->getNomUrl(1);
216  }
217  else{
218  $this->error++;
219  }
220  }
221  else if($this->element === 'don' || $this->element === 'donation') {
222  require_once DOL_DOCUMENT_ROOT.'/don/class/don.class.php';
223 
224  $object = new Don($this->db);
225  if ($object->fetch($this->fk_object)>0) {
226  return $object->getNomUrl(1);
227  }
228  else{
229  $this->error++;
230  }
231  }
232  else if ($this->action == 'MODULE_SET')
233  {
234  return '<i class="opacitymedium">System to track events into unalterable logs were enabled</i>';
235  }
236  else if ($this->action == 'MODULE_RESET')
237  {
238  if ($this->signature == '0000000000')
239  {
240  return '<i class="opacitymedium">System to track events into unalterable logs were disabled after some recording were done. We saved a special Fingerprint to track the chain as broken.</i>';
241  }
242  else
243  {
244  return '<i class="opacitymedium">System to track events into unalterable logs were disabled. This is possible because no record were done yet.</i>';
245  }
246  }
247 
248  return '<i class="opacitymedium">'.$langs->trans('ImpossibleToReloadObject', $this->element, $this->fk_object).'</i>';
249 
250  }
251 
255  public function getUser()
256  {
257  global $langs, $cachedUser;
258 
259  if(empty($cachedUser))$cachedUser=array();
260 
261  if(empty($cachedUser[$this->fk_user])) {
262  $u=new User($this->db);
263  if($u->fetch($this->fk_user)>0) {
264  $cachedUser[$this->fk_user] = $u;
265  }
266  }
267 
268  if(!empty($cachedUser[$this->fk_user])) {
269  return $cachedUser[$this->fk_user]->getNomUrl(1);
270  }
271 
272  return $langs->trans('ImpossibleToRetrieveUser', $this->fk_user);
273  }
274 
283  public function setObjectData(&$object, $action, $amounts)
284  {
285  global $langs, $user, $mysoc;
286 
287  // Generic fields
288 
289  // action
290  $this->action = $action;
291  // amount
292  $this->amounts= $amounts;
293  // date
294  if ($object->element == 'payment' || $object->element == 'payment_supplier')
295  {
296  $this->date_object = $object->datepaye;
297  }
298  elseif ($object->element=='payment_salary')
299  {
300  $this->date_object = $object->datev;
301  }
302  elseif ($object->element == 'payment_donation')
303  {
304  $this->date_object = $object->datepaid?$object->datepaid:$object->datep;
305  }
306  else {
307  $this->date_object = $object->date;
308  }
309  // ref
310  $this->ref_object = ((! empty($object->newref)) ? $object->newref : $object->ref); // newref is set when validating a draft, ref is set in other cases
311  // type of object
312  $this->element = $object->element;
313  // id of object
314  $this->fk_object = $object->id;
315 
316  $this->object_data=new stdClass();
317 
318  // Add thirdparty info
319  if (empty($object->thirdparty) && method_exists($object, 'fetch_thirdparty')) $object->fetch_thirdparty();
320  if (! empty($object->thirdparty))
321  {
322  $this->object_data->thirdparty = new stdClass();
323 
324  foreach($object->thirdparty as $key=>$value)
325  {
326  if (in_array($key, array('fields'))) continue; // Discard some properties
327  if (! in_array($key, array(
328  'name','name_alias','ref_ext','address','zip','town','state_code','country_code','idprof1','idprof2','idprof3','idprof4','idprof5','idprof6','phone','fax','email','barcode',
329  'tva_intra', 'localtax1_assuj', 'localtax1_value', 'localtax2_assuj', 'localtax2_value', 'managers', 'capital', 'typent_code', 'forme_juridique_code', 'code_client', 'code_fournisseur'
330  ))) continue; // Discard if not into a dedicated list
331  if (!is_object($value)) $this->object_data->thirdparty->{$key} = $value;
332  }
333  }
334 
335  // Add company info
336  if (! empty($mysoc))
337  {
338  $this->object_data->mycompany = new stdClass();
339 
340  foreach($mysoc as $key=>$value)
341  {
342  if (in_array($key, array('fields'))) continue; // Discard some properties
343  if (! in_array($key, array(
344  'name','name_alias','ref_ext','address','zip','town','state_code','country_code','idprof1','idprof2','idprof3','idprof4','idprof5','idprof6','phone','fax','email','barcode',
345  'tva_intra', 'localtax1_assuj', 'localtax1_value', 'localtax2_assuj', 'localtax2_value', 'managers', 'capital', 'typent_code', 'forme_juridique_code', 'code_client', 'code_fournisseur'
346  ))) continue; // Discard if not into a dedicated list
347  if (!is_object($value)) $this->object_data->mycompany->{$key} = $value;
348  }
349  }
350 
351  // Add user info
352  if (! empty($user))
353  {
354  $this->fk_user = $user->id;
355  $this->user_fullname = $user->getFullName($langs);
356  }
357 
358  // Field specific to object
359 
360  if ($this->element == 'facture')
361  {
362  foreach($object as $key=>$value)
363  {
364  if (in_array($key, array('fields'))) continue; // Discard some properties
365  if (! in_array($key, array(
366  'ref','facnumber','ref_client','ref_supplier','datef','type','total_ht','total_tva','total_ttc','localtax1','localtax2','revenuestamp','datepointoftax','note_public'
367  ))) continue; // Discard if not into a dedicated list
368  if (!is_object($value)) $this->object_data->{$key} = $value;
369  }
370 
371  if (! empty($object->newref)) $this->object_data->ref = $object->newref;
372  }
373  elseif ($this->element == 'invoice_supplier')
374  {
375  foreach($object as $key=>$value)
376  {
377  if (in_array($key, array('fields'))) continue; // Discard some properties
378  if (! in_array($key, array(
379  'ref','facnumber','ref_client','ref_supplier','datef','type','total_ht','total_tva','total_ttc','localtax1','localtax2','revenuestamp','datepointoftax','note_public'
380  ))) continue; // Discard if not into a dedicated list
381  if (!is_object($value)) $this->object_data->{$key} = $value;
382  }
383 
384  if (! empty($object->newref)) $this->object_data->ref = $object->newref;
385  }
386  elseif ($this->element == 'payment' || $this->element == 'payment_supplier' || $this->element == 'payment_donation')
387  {
388  $datepayment = $object->datepaye?$object->datepaye:($object->datepaid?$object->datepaid:$object->datep);
389  $paymenttypeid = $object->paiementid?$object->paiementid:$object->paymenttype;
390 
391  $this->object_data->ref = $object->ref;
392  $this->object_data->date = $datepayment;
393  $this->object_data->type_code = dol_getIdFromCode($this->db, $paymenttypeid, 'c_paiement', 'id', 'code');
394  $this->object_data->payment_num = $object->num_paiement;
395  //$this->object_data->fk_account = $object->fk_account;
396  $this->object_data->note = $object->note;
397  //var_dump($this->object_data);exit;
398 
399  $totalamount=0;
400 
401  $paymentpartnumber=0;
402  foreach($object->amounts as $objid => $amount)
403  {
404  if (empty($amount)) continue;
405 
406  $totalamount += $amount;
407 
408  $tmpobject = null;
409  if ($this->element == 'payment_supplier')
410  {
411  include_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
412  $tmpobject = new FactureFournisseur($this->db);
413  }
414  elseif ($this->element == 'payment')
415  {
416  include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
417  $tmpobject = new Facture($this->db);
418  }
419  elseif ($this->element == 'payment_donation')
420  {
421  include_once DOL_DOCUMENT_ROOT.'/don/class/don.class.php';
422  $tmpobject = new Don($this->db);
423  }
424  if (! is_object($tmpobject))
425  {
426  continue;
427  }
428 
429  $result = $tmpobject->fetch($objid);
430  if ($result <= 0)
431  {
432  $this->error = $tmpobject->error;
433  $this->errors = $tmpobject->errors;
434  return -1;
435  }
436 
437  $paymentpart = new stdClass();
438  $paymentpart->amount = $amount;
439 
440  if ($this->element != 'payment_donation')
441  {
442  $result = $tmpobject->fetch_thirdparty();
443  if ($result <= 0)
444  {
445  $this->error = $tmpobject->error;
446  $this->errors = $tmpobject->errors;
447  return -1;
448  }
449 
450  $paymentpart->thirdparty = new stdClass();
451  foreach($tmpobject->thirdparty as $key=>$value)
452  {
453  if (in_array($key, array('fields'))) continue; // Discard some properties
454  if (! in_array($key, array(
455  'name','name_alias','ref_ext','address','zip','town','state_code','country_code','idprof1','idprof2','idprof3','idprof4','idprof5','idprof6','phone','fax','email','barcode',
456  'tva_intra', 'localtax1_assuj', 'localtax1_value', 'localtax2_assuj', 'localtax2_value', 'managers', 'capital', 'typent_code', 'forme_juridique_code', 'code_client', 'code_fournisseur'
457  ))) continue; // Discard if not into a dedicated list
458  if (!is_object($value)) $paymentpart->thirdparty->{$key} = $value;
459  }
460  }
461 
462  // Init object to avoid warnings
463  if ($this->element == 'payment_donation') $paymentpart->donation = new stdClass();
464  else $paymentpart->invoice = new stdClass();
465 
466  foreach($tmpobject as $key=>$value)
467  {
468  if (in_array($key, array('fields'))) continue; // Discard some properties
469  if (! in_array($key, array(
470  'ref','facnumber','ref_client','ref_supplier','datef','type','total_ht','total_tva','total_ttc','localtax1','localtax2','revenuestamp','datepointoftax','note_public'
471  ))) continue; // Discard if not into a dedicated list
472  if (!is_object($value))
473  {
474  if ($this->element == 'payment_donation') $paymentpart->donation->{$key} = $value;
475  else $paymentpart->invoice->{$key} = $value;
476  }
477  }
478 
479  $paymentpartnumber++;
480  $this->object_data->payment_part[$paymentpartnumber] = $paymentpart;
481  }
482 
483  $this->object_data->amount = $totalamount;
484 
485  if (! empty($object->newref)) $this->object_data->ref = $object->newref;
486  }
487  elseif($this->element == 'payment_salary')
488  {
489  $this->object_data->amounts = array($object->amount);
490 
491  if (! empty($object->newref)) $this->object_data->ref = $object->newref;
492  }
493 
494  return 1;
495  }
496 
503  public function fetch($id) {
504 
505  global $langs;
506 
507  dol_syslog(get_class($this)."::fetch id=".$id, LOG_DEBUG);
508 
509  if (empty($id))
510  {
511  $this->error='BadParameter';
512  return -1;
513  }
514 
515  $langs->load("blockedlog");
516 
517  $sql = "SELECT b.rowid, b.date_creation, b.signature, b.signature_line, b.amounts, b.action, b.element, b.fk_object, b.entity,";
518  $sql.= " b.certified, b.tms, b.fk_user, b.user_fullname, b.date_object, b.ref_object, b.object_data";
519  $sql.= " FROM ".MAIN_DB_PREFIX."blockedlog as b";
520  if ($id) $sql.= " WHERE b.rowid = ". $id;
521 
522  $resql=$this->db->query($sql);
523  if ($resql)
524  {
525  if ($this->db->num_rows($resql))
526  {
527  $obj = $this->db->fetch_object($resql);
528 
529  $this->id = $obj->rowid;
530  $this->entity = $obj->entity;
531  $this->ref = $obj->rowid;
532 
533  $this->date_creation = $this->db->jdate($obj->date_creation);
534  $this->tms = $this->db->jdate($obj->tms);
535 
536  $this->amounts = (double) $obj->amounts;
537  $this->action = $obj->action;
538  $this->element = $obj->element;
539 
540  $this->fk_object = $obj->fk_object;
541  $this->date_object = $this->db->jdate($obj->date_object);
542  $this->ref_object = $obj->ref_object;
543 
544  $this->fk_user = $obj->fk_user;
545  $this->user_fullname = $obj->user_fullname;
546 
547  $this->object_data = unserialize($obj->object_data);
548 
549  $this->signature = $obj->signature;
550  $this->signature_line = $obj->signature_line;
551  $this->certified = ($obj->certified == 1);
552 
553  return 1;
554  }
555  else
556  {
557  $this->error=$langs->trans("RecordNotFound");
558  return 0;
559  }
560  }
561  else
562  {
563  $this->error=$this->db->error();
564  return -1;
565  }
566 
567  }
568 
574  public function setCertified() {
575 
576  $res = $this->db->query("UPDATE ".MAIN_DB_PREFIX."blockedlog SET certified=1 WHERE rowid=".$this->id);
577  if($res===false) return false;
578 
579  return true;
580 
581 
582  }
583 
591  public function create($user, $forcesignature='') {
592 
593  global $conf,$langs,$hookmanager;
594 
595  $langs->load('blockedlog');
596 
597  $error=0;
598 
599  // Clean data
600  $this->amounts=(double) $this->amounts;
601 
602  dol_syslog(get_class($this).'::create action='.$this->action.' fk_user='.$this->fk_user.' user_fullname='.$this->user_fullname, LOG_DEBUG);
603 
604  // Check parameters/properties
605  if (! isset($this->amounts)) // amount can be 0 for some events (like when module is disabled)
606  {
607  $this->error=$langs->trans("BlockLogNeedAmountsValue");
608  dol_syslog($this->error, LOG_WARNING);
609  return -1;
610  }
611 
612  if (empty($this->element)) {
613  $this->error=$langs->trans("BlockLogNeedElement");
614  dol_syslog($this->error, LOG_WARNING);
615  return -2;
616  }
617 
618  if (empty($this->action) || empty($this->fk_user) || empty($this->user_fullname)) {
619  $this->error=$langs->trans("BadParameterWhenCallingCreateOfBlockedLog");
620  dol_syslog($this->error, LOG_WARNING);
621  return -3;
622  }
623 
624  $this->date_creation = dol_now();
625 
626  $this->db->begin();
627 
628  $previoushash = $this->getPreviousHash(1, 0); // This get last record and lock database until insert is done
629 
630  $keyforsignature = $this->buildKeyForSignature();
631 
632  $this->signature_line = dol_hash($keyforsignature, '5'); // Not really usefull
633  $this->signature = dol_hash($previoushash . $keyforsignature, '5');
634  if ($forcesignature) $this->signature = $forcesignature;
635  //var_dump($keyforsignature);var_dump($previoushash);var_dump($this->signature_line);var_dump($this->signature);
636 
637  $sql = "INSERT INTO ".MAIN_DB_PREFIX."blockedlog (";
638  $sql.= " date_creation,";
639  $sql.= " action,";
640  $sql.= " amounts,";
641  $sql.= " signature,";
642  $sql.= " signature_line,";
643  $sql.= " element,";
644  $sql.= " fk_object,";
645  $sql.= " date_object,";
646  $sql.= " ref_object,";
647  $sql.= " object_data,";
648  $sql.= " certified,";
649  $sql.= " fk_user,";
650  $sql.= " user_fullname,";
651  $sql.= " entity";
652  $sql.= ") VALUES (";
653  $sql.= "'".$this->db->idate($this->date_creation)."',";
654  $sql.= "'".$this->db->escape($this->action)."',";
655  $sql.= $this->amounts.",";
656  $sql.= "'".$this->db->escape($this->signature)."',";
657  $sql.= "'".$this->db->escape($this->signature_line)."',";
658  $sql.= "'".$this->db->escape($this->element)."',";
659  $sql.= $this->fk_object.",";
660  $sql.= "'".$this->db->idate($this->date_object)."',";
661  $sql.= "'".$this->db->escape($this->ref_object)."',";
662  $sql.= "'".$this->db->escape(serialize($this->object_data))."',";
663  $sql.= "0,";
664  $sql.= $this->fk_user.",";
665  $sql.= "'".$this->db->escape($this->user_fullname)."',";
666  $sql.= ($this->entity ? $this->entity : $conf->entity);
667  $sql.= ")";
668 
669  $res = $this->db->query($sql);
670  if ($res)
671  {
672  $id = $this->db->last_insert_id(MAIN_DB_PREFIX."blockedlog");
673 
674  if ($id > 0)
675  {
676  $this->id = $id;
677 
678  $this->db->commit();
679 
680  return $this->id;
681  }
682  else
683  {
684  $this->db->rollback();
685  return -2;
686  }
687  }
688  else
689  {
690  $this->error=$this->db->error();
691  $this->db->rollback();
692  return -1;
693  }
694 
695  // The commit will release the lock so we can insert nex record
696  }
697 
704  public function checkSignature($previoushash='')
705  {
706  if (empty($previoushash))
707  {
708  $previoushash = $this->getPreviousHash(0, $this->id);
709  }
710  // Recalculate hash
711  $keyforsignature = $this->buildKeyForSignature();
712 
713  $signature_line = dol_hash($keyforsignature, '5'); // Not really usefull
714  $signature = dol_hash($previoushash . $keyforsignature, '5');
715  //var_dump($previoushash); var_dump($keyforsignature); var_dump($signature_line); var_dump($signature);
716 
717  $res = ($signature === $this->signature);
718 
719  if (!$res) {
720  $this->error = 'Signature KO';
721  }
722 
723  return $res;
724  }
725 
731  private function buildKeyForSignature()
732  {
733  //print_r($this->object_data);
734  return $this->date_creation.'|'.$this->action.'|'.$this->amounts.'|'.$this->ref_object.'|'.$this->date_object.'|'.$this->user_fullname.'|'.print_r($this->object_data, true);
735  }
736 
737 
745  public function getPreviousHash($withlock=0, $beforeid=0)
746  {
747  global $conf;
748 
749  $previoussignature='';
750 
751  $sql = "SELECT rowid, signature FROM ".MAIN_DB_PREFIX."blockedlog";
752  $sql.= " WHERE entity=".$conf->entity;
753  if ($beforeid) $sql.= " AND rowid < ".(int) $beforeid;
754  $sql.=" ORDER BY rowid DESC LIMIT 1";
755  $sql.=($withlock ? " FOR UPDATE ": "");
756 
757  $resql = $this->db->query($sql);
758  if ($resql) {
759  $obj = $this->db->fetch_object($resql);
760  if ($obj)
761  {
762  $previoussignature = $obj->signature;
763  }
764  }
765  else
766  {
767  dol_print_error($this->db);
768  exit;
769  }
770 
771  if (empty($previoussignature))
772  {
773  // First signature line (line 0)
774  $previoussignature = $this->getSignature();
775  }
776 
777  return $previoussignature;
778  }
779 
796  public function getLog($element, $fk_object, $limit = 0, $sortfield = '', $sortorder = '', $search_fk_user = -1, $search_start = -1, $search_end = -1, $search_ref='', $search_amount='', $search_code='')
797  {
798  global $conf, $cachedlogs;
799 
800  /* $cachedlogs allow fastest search */
801  if (empty($cachedlogs)) $cachedlogs=array();
802 
803  if ($element=='all') {
804 
805  $sql="SELECT rowid FROM ".MAIN_DB_PREFIX."blockedlog
806  WHERE entity=".$conf->entity;
807 
808  }
809  else if ($element=='not_certified') {
810  $sql="SELECT rowid FROM ".MAIN_DB_PREFIX."blockedlog
811  WHERE entity=".$conf->entity." AND certified = 0";
812 
813  }
814  else if ($element=='just_certified') {
815  $sql="SELECT rowid FROM ".MAIN_DB_PREFIX."blockedlog
816  WHERE entity=".$conf->entity." AND certified = 1";
817 
818  }
819  else{
820  $sql="SELECT rowid FROM ".MAIN_DB_PREFIX."blockedlog
821  WHERE entity=".$conf->entity." AND element='".$element."' AND fk_object=".(int) $fk_object;
822  }
823 
824  if ($search_fk_user > 0) $sql.=natural_search("fk_user", $search_fk_user, 2);
825  if ($search_start > 0) $sql.=" AND date_creation >= '".$this->db->idate($search_start)."'";
826  if ($search_end > 0) $sql.=" AND date_creation <= '".$this->db->idate($search_end)."'";
827  if ($search_ref != '') $sql.=natural_search("ref_object", $search_ref);
828  if ($search_amount != '') $sql.=natural_search("amounts", $search_amount, 1);
829  if ($search_code != '' && $search_code != '-1') $sql.=natural_search("action", $search_code, 3);
830 
831  $sql.=$this->db->order($sortfield, $sortorder);
832  $sql.=$this->db->plimit($limit+1); // We want more, because we will stop into loop later with error if we reach max
833 
834  $res = $this->db->query($sql);
835  if($res) {
836 
837  $results=array();
838 
839  $i = 0;
840  while ($obj = $this->db->fetch_object($res))
841  {
842  $i++;
843  if ($i > $limit)
844  {
845  // Too many record, we will consume too much memory
846  return -2;
847  }
848 
849  if (!isset($cachedlogs[$obj->rowid]))
850  {
851  $b=new BlockedLog($this->db);
852  $b->fetch($obj->rowid);
853 
854  $cachedlogs[$obj->rowid] = $b;
855  }
856 
857  $results[] = $cachedlogs[$obj->rowid];
858  }
859 
860  return $results;
861  }
862 
863  return -1;
864  }
865 
871  public function getSignature()
872  {
873  global $db,$conf,$mysoc;
874 
875  if (empty($conf->global->BLOCKEDLOG_ENTITY_FINGERPRINT)) { // creation of a unique fingerprint
876 
877  require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
878  require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
879 
880  $fingerprint = dol_hash(print_r($mysoc,true).getRandomPassword(1), '5');
881 
882  dolibarr_set_const($db, 'BLOCKEDLOG_ENTITY_FINGERPRINT', $fingerprint, 'chaine',0,'Numeric Unique Fingerprint', $conf->entity);
883 
884  $conf->global->BLOCKEDLOG_ENTITY_FINGERPRINT=$fingerprint;
885  }
886 
887  return $conf->global->BLOCKEDLOG_ENTITY_FINGERPRINT;
888  }
889 
890 
896  function alreadyUsed($ignoresystem=0)
897  {
898  global $conf;
899 
900  $result = false;
901 
902  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."blockedlog";
903  $sql.= " WHERE entity = ".$conf->entity;
904  if ($ignoresystem) $sql.=" AND action not in ('MODULE_SET','MODULE_RESET')";
905  $sql.= $this->db->plimit(1);
906 
907  $res = $this->db->query($sql);
908  if ($res!==false)
909  {
910  $obj = $this->db->fetch_object($res);
911  if ($obj) $result = true;
912  }
913  else dol_print_error($this->db);
914 
915  dol_syslog("Module Blockedlog alreadyUsed with ignoresystem=".$ignoresystem." is ".$result);
916 
917  return $result;
918  }
919 
920 }
921 
buildKeyForSignature()
Return a string for signature.
__construct(DoliDB $db)
Constructor.
alreadyUsed($ignoresystem=0)
Check if module was already used or not for at least one recording.
getPreviousHash($withlock=0, $beforeid=0)
Get previous signature/hash in chain.
getSignature()
Return the signature (hash) of the "genesis-block" (Block 0).
checkSignature($previoushash='')
Check if current signature still correct compared to the value in chain.
getUser()
try to retrieve user author
Class to manage Dolibarr users.
Definition: user.class.php:39
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0)
Return an id or code from a code or id.
Class to manage Dolibarr database access.
dolibarr_set_const($db, $name, $value, $type='chaine', $visible=0, $note='', $entity=1)
Insert a parameter (key,value) into database (delete old key then insert it again).
Definition: admin.lib.php:485
fetch($id)
Get object from database.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
Class to manage suppliers invoices.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
getObjectLink()
Try to retrieve source object (it it still exists)
Class to manage Blocked Log.
setObjectData(&$object, $action, $amounts)
Populate properties of log from object data.
Class to manage payments of customer invoices.
natural_search($fields, $value, $mode=0, $nofirstand=0)
Generate natural SQL search string for a criteria (this criteria can be tested on one or several fiel...
dol_now($mode='gmt')
Return date for now.
getRandomPassword($generic=false)
Return a generated password using default module.
Class to manage payments of donations.
getLog($element, $fk_object, $limit=0, $sortfield= '', $sortorder= '', $search_fk_user=-1, $search_start=-1, $search_end=-1, $search_ref='', $search_amount='', $search_code='')
Return array of log objects (with criterias)
Class to manage donations.
Definition: don.class.php:35
create($user, $forcesignature='')
Create blocked log in database.
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
dol_hash($chain, $type='0')
Returns a hash of a string.
Class to manage invoices.
Class to manage payments for supplier invoices.
setCertified()
Set block certified by authority.