Browse Source

upgrade to PHP 8

sqlite
d47081 2 years ago
parent
commit
51d7eb4f4c
  1. 2
      crawler.php
  2. 27
      library/api.php
  3. 26
      library/sqlite.php

2
crawler.php

@ -13,7 +13,7 @@ if (false !== sem_acquire($semaphore, 1)) {
require_once('library/crypto.php'); require_once('library/crypto.php');
require_once('library/helper.php'); require_once('library/helper.php');
$db = new SQLite(); $db = new SQLite(DB_NAME, DB_USERNAME, DB_PASSWORD);
$api = new API(); $api = new API();
$blockLast = $db->getLastBlock(); $blockLast = $db->getLastBlock();

27
library/api.php

@ -4,10 +4,29 @@ class API {
private $_url = 'https://explorer.kevacoin.org/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() { public function getblockcount() {
// API return: int // 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; return (int) $response;
} }
@ -17,7 +36,7 @@ class API {
public function getblockhash($block) { 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 // API return: string
if (false !== json_decode($response)) { if (false !== json_decode($response)) {
@ -31,7 +50,7 @@ class API {
public function getblock($hash) { public function getblock($hash) {
// API return: json // 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'])) { if (isset($response['hash'])) {
return $response; return $response;
} }
@ -42,7 +61,7 @@ class API {
public function getrawtransaction($txid) { 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'])) { if (isset($response['txid'])) {
return $response; return $response;
} }

26
library/sqlite.php

@ -4,11 +4,11 @@ class SQLite {
private $_db; private $_db;
public function __construct($database, $username, $password) { public function __construct(string $database, string $username, string $password) {
try { 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_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->_db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); $this->_db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$this->_db->setAttribute(PDO::ATTR_TIMEOUT, 600); $this->_db->setAttribute(PDO::ATTR_TIMEOUT, 600);
@ -75,7 +75,7 @@ class SQLite {
} }
} }
public function getBlock($blockId) { public function getBlock(int $blockId) {
try { try {
@ -85,7 +85,7 @@ class SQLite {
$result = $query->fetch(); $result = $query->fetch();
return $result ? $result['blockId'] : false; return $result ? $result['blockId'] : 0;
} catch(PDOException $e) { } catch(PDOException $e) {
trigger_error($e->getMessage()); trigger_error($e->getMessage());
@ -93,7 +93,7 @@ class SQLite {
} }
} }
public function getNameSpace($hash) { public function getNameSpace(string $hash) {
try { try {
@ -103,7 +103,7 @@ class SQLite {
$result = $query->fetch(); $result = $query->fetch();
return $result ? $result['nameSpaceId'] : false; return $result ? $result['nameSpaceId'] : '';
} catch(PDOException $e) { } catch(PDOException $e) {
trigger_error($e->getMessage()); trigger_error($e->getMessage());
@ -111,7 +111,7 @@ class SQLite {
} }
} }
public function getData($txId) { public function getData(string $txId) {
try { try {
@ -121,7 +121,7 @@ class SQLite {
$result = $query->fetch(); $result = $query->fetch();
return $result ? $result['dataId'] : false; return $result ? $result['dataId'] : 0;
} catch(PDOException $e) { } catch(PDOException $e) {
trigger_error($e->getMessage()); trigger_error($e->getMessage());
@ -129,7 +129,7 @@ class SQLite {
} }
} }
public function addBlock($blockId) { public function addBlock(int $blockId) {
try { try {
@ -146,7 +146,7 @@ class SQLite {
} }
} }
public function setLostTransactions($blockId, $lostTransactions) { public function setLostTransactions(int $blockId, int $lostTransactions) {
try { try {
@ -162,7 +162,7 @@ class SQLite {
} }
} }
public function addNameSpace($hash) { public function addNameSpace(string $hash) {
try { 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 { try {
@ -234,7 +234,7 @@ class SQLite {
} }
} }
public function setDataKeyDeleted($nameSpaceId, $key, $deleted) { public function setDataKeyDeleted(int $nameSpaceId, string $key, bool $deleted) {
try { try {

Loading…
Cancel
Save