authority.class.php 6.9 KB

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