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