authority.class.php 6.7 KB

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