authority.class.php 6.6 KB

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