From 51d7eb4f4c8a22d5027e1de32b30483093a300a3 Mon Sep 17 00:00:00 2001 From: d47081 Date: Sat, 16 Jul 2022 12:14:46 +0300 Subject: [PATCH] upgrade to PHP 8 --- crawler.php | 2 +- library/api.php | 27 +++++++++++++++++++++++---- library/sqlite.php | 26 +++++++++++++------------- 3 files changed, 37 insertions(+), 18 deletions(-) diff --git a/crawler.php b/crawler.php index 6b2575c..89bee13 100644 --- a/crawler.php +++ b/crawler.php @@ -13,7 +13,7 @@ if (false !== sem_acquire($semaphore, 1)) { require_once('library/crypto.php'); require_once('library/helper.php'); - $db = new SQLite(); + $db = new SQLite(DB_NAME, DB_USERNAME, DB_PASSWORD); $api = new API(); $blockLast = $db->getLastBlock(); diff --git a/library/api.php b/library/api.php index 0d7f5c1..3496b6f 100644 --- a/library/api.php +++ b/library/api.php @@ -4,10 +4,29 @@ class API { private $_url = 'https://explorer.kevacoin.org/api/'; + private function _file_get_contents($url) { + + $ch = curl_init(); + + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); + curl_setopt($ch, CURLOPT_HEADER, false); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_REFERER, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3000); + curl_setopt($ch, CURLOPT_TIMEOUT, 10000); + + $result = curl_exec($ch); + curl_close($ch); + + return $result; +} + public function getblockcount() { // API return: int - if (false !== $response = file_get_contents($this->_url . 'getblockcount')) { + if (false !== $response = $this->_file_get_contents($this->_url . 'getblockcount')) { return (int) $response; } @@ -17,7 +36,7 @@ class API { public function getblockhash($block) { - if (false !== $response = file_get_contents($this->_url . 'getblockhash?index=' . $block)) { + if (false !== $response = $this->_file_get_contents($this->_url . 'getblockhash?index=' . $block)) { // API return: string if (false !== json_decode($response)) { @@ -31,7 +50,7 @@ class API { public function getblock($hash) { // API return: json - if (false !== $response = json_decode(file_get_contents($this->_url . 'getblock?hash=' . $hash), true)) { + if (false !== $response = json_decode($this->_file_get_contents($this->_url . 'getblock?hash=' . $hash), true)) { if (isset($response['hash'])) { return $response; } @@ -42,7 +61,7 @@ class API { public function getrawtransaction($txid) { - if (false !== $response = json_decode(file_get_contents($this->_url . 'getrawtransaction?txid=' . $txid . '&decrypt=1'), true)) { + if (false !== $response = json_decode($this->_file_get_contents($this->_url . 'getrawtransaction?txid=' . $txid . '&decrypt=1'), true)) { if (isset($response['txid'])) { return $response; } diff --git a/library/sqlite.php b/library/sqlite.php index 1b7c5de..6b62ae9 100644 --- a/library/sqlite.php +++ b/library/sqlite.php @@ -4,11 +4,11 @@ class SQLite { private $_db; - public function __construct($database, $username, $password) { + public function __construct(string $database, string $username, string $password) { try { - $this->_db = new PDO('sqlite:' . $database, $username, $password, [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8']); + $this->_db = new PDO('sqlite:' . $database, $username, $password); $this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->_db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); $this->_db->setAttribute(PDO::ATTR_TIMEOUT, 600); @@ -75,7 +75,7 @@ class SQLite { } } - public function getBlock($blockId) { + public function getBlock(int $blockId) { try { @@ -85,7 +85,7 @@ class SQLite { $result = $query->fetch(); - return $result ? $result['blockId'] : false; + return $result ? $result['blockId'] : 0; } catch(PDOException $e) { trigger_error($e->getMessage()); @@ -93,7 +93,7 @@ class SQLite { } } - public function getNameSpace($hash) { + public function getNameSpace(string $hash) { try { @@ -103,7 +103,7 @@ class SQLite { $result = $query->fetch(); - return $result ? $result['nameSpaceId'] : false; + return $result ? $result['nameSpaceId'] : ''; } catch(PDOException $e) { trigger_error($e->getMessage()); @@ -111,7 +111,7 @@ class SQLite { } } - public function getData($txId) { + public function getData(string $txId) { try { @@ -121,7 +121,7 @@ class SQLite { $result = $query->fetch(); - return $result ? $result['dataId'] : false; + return $result ? $result['dataId'] : 0; } catch(PDOException $e) { trigger_error($e->getMessage()); @@ -129,7 +129,7 @@ class SQLite { } } - public function addBlock($blockId) { + public function addBlock(int $blockId) { try { @@ -146,7 +146,7 @@ class SQLite { } } - public function setLostTransactions($blockId, $lostTransactions) { + public function setLostTransactions(int $blockId, int $lostTransactions) { try { @@ -162,7 +162,7 @@ class SQLite { } } - public function addNameSpace($hash) { + public function addNameSpace(string $hash) { try { @@ -179,7 +179,7 @@ class SQLite { } } - public function addData($blockId, $nameSpaceId, $time, $size, $txid, $key, $value, $ns, $deleted = false) { + public function addData(int $blockId, int $nameSpaceId, int $time, int $size, string $txid, string $key, string $value, string $ns, bool $deleted = false) { try { @@ -234,7 +234,7 @@ class SQLite { } } - public function setDataKeyDeleted($nameSpaceId, $key, $deleted) { + public function setDataKeyDeleted(int $nameSpaceId, string $key, bool $deleted) { try {