authority.class.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. /**
  18. * Class to manage certif authority
  19. */
  20. class BlockedLogAuthority
  21. {
  22. /**
  23. * Id of the log
  24. * @var int
  25. */
  26. public $id;
  27. /**
  28. * Unique fingerprint of the blockchain to store
  29. * @var string
  30. */
  31. public $signature = '';
  32. /**
  33. * Entire fingerprints blockchain
  34. * @var string
  35. */
  36. public $blockchain = '';
  37. /**
  38. * timestamp
  39. * @var int
  40. */
  41. public $tms = 0;
  42. /**
  43. * Constructor
  44. *
  45. * @param DoliDB $db Database handler
  46. */
  47. public function __construct($db)
  48. {
  49. $this->db = $db;
  50. }
  51. /**
  52. * Get the blockchain
  53. *
  54. * @return string blockchain
  55. */
  56. public function getLocalBlockChain()
  57. {
  58. $block_static = new BlockedLog($this->db);
  59. $this->signature = $block_static->getSignature();
  60. $blocks = $block_static->getLog('all', 0, 0, 'rowid', 'ASC') ;
  61. $this->blockchain = '';
  62. foreach($blocks as &$b) {
  63. $this->blockchain.=$b->signature;
  64. }
  65. return $this->blockchain;
  66. }
  67. /**
  68. * Get hash of the block chain to check
  69. *
  70. * @return string hash md5 of blockchain
  71. */
  72. public function getBlockchainHash()
  73. {
  74. return md5($this->signature.$this->blockchain);
  75. }
  76. /**
  77. * Get hash of the block chain to check
  78. *
  79. * @param string $hash hash md5 of blockchain to test
  80. * @return boolean
  81. */
  82. public function checkBlockchain($hash)
  83. {
  84. return ($hash === $this->getBlockchainHash() );
  85. }
  86. /**
  87. * Add a new block to the chain
  88. *
  89. * @param string $block new block to chain
  90. * @return void
  91. */
  92. public function addBlock($block)
  93. {
  94. $this->blockchain.=$block;
  95. }
  96. /**
  97. * hash already exist into chain ?
  98. *
  99. * @param string $block new block to chain
  100. * @return boolean
  101. */
  102. public function checkBlock($block)
  103. {
  104. if(strlen($block)!=64) return false;
  105. $blocks = str_split($this->blockchain,64);
  106. if(!in_array($block,$blocks)) {
  107. return true;
  108. }
  109. else{
  110. return false;
  111. }
  112. }
  113. /**
  114. * Get object from database
  115. *
  116. * @param int $id Id of object to load
  117. * @param string $signature Signature of object to load
  118. * @return int >0 if OK, <0 if KO, 0 if not found
  119. */
  120. public function fetch($id, $signature='')
  121. {
  122. global $langs;
  123. dol_syslog(get_class($this)."::fetch id=".$id, LOG_DEBUG);
  124. if (empty($id) && empty($signature))
  125. {
  126. $this->error='BadParameter';
  127. return -1;
  128. }
  129. $langs->load("blockedlog");
  130. $sql = "SELECT b.rowid, b.signature, b.blockchain, b.tms";
  131. $sql.= " FROM ".MAIN_DB_PREFIX."blockedlog_authority as b";
  132. if ($id) $sql.= " WHERE b.rowid = ". $id;
  133. else if($signature)$sql.= " WHERE b.signature = '". $this->db->escape( $signature ) ."'" ;
  134. $resql=$this->db->query($sql);
  135. if ($resql)
  136. {
  137. if ($this->db->num_rows($resql))
  138. {
  139. $obj = $this->db->fetch_object($resql);
  140. $this->id = $obj->rowid;
  141. $this->ref = $obj->rowid;
  142. $this->signature = $obj->signature;
  143. $this->blockchain = $obj->blockchain;
  144. $this->tms = $this->db->jdate($obj->tms);
  145. return 1;
  146. }
  147. else
  148. {
  149. $this->error=$langs->trans("RecordNotFound");
  150. return 0;
  151. }
  152. }
  153. else
  154. {
  155. $this->error=$this->db->error();
  156. return -1;
  157. }
  158. }
  159. /**
  160. * Create authority in database.
  161. *
  162. * @param User $user Object user that create
  163. * @return int <0 if KO, >0 if OK
  164. */
  165. public function create($user)
  166. {
  167. global $conf,$langs,$hookmanager;
  168. $langs->load('blockedlog');
  169. $error=0;
  170. dol_syslog(get_class($this).'::create', LOG_DEBUG);
  171. $this->db->begin();
  172. $sql = "INSERT INTO ".MAIN_DB_PREFIX."blockedlog_authority (";
  173. $sql.= " signature,";
  174. $sql.= " blockchain";
  175. $sql.= ") VALUES (";
  176. $sql.= "'".$this->db->escape($this->signature)."',";
  177. $sql.= "'".$this->db->escape($this->blockchain)."'";
  178. $sql.= ")";
  179. $res = $this->db->query($sql);
  180. if ($res)
  181. {
  182. $id = $this->db->last_insert_id(MAIN_DB_PREFIX."blockedlog_authority");
  183. if ($id > 0)
  184. {
  185. $this->id = $id;
  186. $this->db->commit();
  187. return $this->id;
  188. }
  189. else
  190. {
  191. $this->db->rollback();
  192. return -2;
  193. }
  194. }
  195. else
  196. {
  197. $this->error=$this->db->error();
  198. $this->db->rollback();
  199. return -1;
  200. }
  201. }
  202. /**
  203. * Create authority in database.
  204. *
  205. * @param User $user Object user that create
  206. * @return int <0 if KO, >0 if OK
  207. */
  208. public function update($user)
  209. {
  210. global $conf,$langs,$hookmanager;
  211. $langs->load('blockedlog');
  212. $error=0;
  213. dol_syslog(get_class($this).'::create', LOG_DEBUG);
  214. $this->db->begin();
  215. $sql = "UPDATE ".MAIN_DB_PREFIX."blockedlog_authority SET ";
  216. $sql.= " blockchain='".$this->db->escape($this->blockchain)."'";
  217. $sql.= " WHERE rowid=".$this->id;
  218. $res = $this->db->query($sql);
  219. if ($res)
  220. {
  221. $this->db->commit();
  222. return 1;
  223. }
  224. else
  225. {
  226. $this->error=$this->db->error();
  227. $this->db->rollback();
  228. return -1;
  229. }
  230. }
  231. /**
  232. * For cron to sync to authority.
  233. *
  234. * @return int <0 if KO, >0 if OK
  235. */
  236. public function syncSignatureWithAuthority()
  237. {
  238. global $conf, $langs;
  239. //TODO create cron task on activation
  240. if(empty($conf->global->BLOCKEDLOG_AUTHORITY_URL) || empty($conf->global->BLOCKEDLOG_USE_REMOTE_AUTHORITY)) {
  241. $this->error = $langs->trans('NoAuthorityURLDefined');
  242. return -2;
  243. }
  244. require_once DOL_DOCUMENT_ROOT.'/blockedlog/class/blockedlog.class.php';
  245. $block_static = new BlockedLog($this->db);
  246. $blocks = $block_static->getLog('not_certified', 0, 0, 'rowid', 'ASC');
  247. $signature=$block_static->getSignature();
  248. foreach($blocks as &$block) {
  249. $url = $conf->global->BLOCKEDLOG_AUTHORITY_URL.'/blockedlog/ajax/authority.php?s='.$signature.'&b='.$block->signature;
  250. $res = file_get_contents($url);
  251. echo $block->signature.' '.$url. ' '.$res.'<br>';
  252. if($res === 'blockalreadyadded' || $res === 'blockadded') {
  253. $block->setCertified();
  254. }
  255. else {
  256. $this->error = $langs->trans('ImpossibleToContactAuthority ',$url);
  257. return -1;
  258. }
  259. }
  260. return 1;
  261. }
  262. }