2018-09-01 01:50:29 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Main class for the Daemon setup
|
|
|
|
*
|
|
|
|
* @package LBRYPress
|
|
|
|
*/
|
2022-02-14 02:16:29 +01:00
|
|
|
defined('ABSPATH') || die(); // Exit if accessed directly
|
2018-09-01 01:50:29 +02:00
|
|
|
|
|
|
|
class LBRY_Daemon
|
|
|
|
{
|
2018-11-09 19:34:12 +01:00
|
|
|
/**
|
|
|
|
* The address of the Lbry Daemon
|
|
|
|
* @var string
|
|
|
|
*/
|
2018-09-01 01:50:29 +02:00
|
|
|
private $address = 'localhost:5279';
|
|
|
|
|
2018-11-09 19:34:12 +01:00
|
|
|
/**
|
|
|
|
* The Daemon Logger
|
|
|
|
* @var LBRY_Daemon_Logger
|
|
|
|
*/
|
2018-11-12 21:38:16 +01:00
|
|
|
public $logger = null;
|
2018-10-29 22:55:26 +01:00
|
|
|
|
2018-09-11 22:10:15 +02:00
|
|
|
/**
|
|
|
|
* LBRY Daemon Object constructor
|
|
|
|
*/
|
|
|
|
public function __construct()
|
2018-10-29 22:55:26 +01:00
|
|
|
{
|
|
|
|
$this->logger = new LBRY_Daemon_Logger();
|
2018-09-01 01:50:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-04 00:37:32 +01:00
|
|
|
* Returns an unused address
|
|
|
|
* https://lbry.tech/api/sdk#address_unused
|
2018-11-09 19:34:12 +01:00
|
|
|
* @return string Unused wallet address in base58
|
2018-09-01 01:50:29 +02:00
|
|
|
*/
|
|
|
|
public function wallet_unused_address()
|
|
|
|
{
|
2018-10-29 22:55:26 +01:00
|
|
|
try {
|
2022-02-12 22:28:25 +01:00
|
|
|
$result = $this->request( 'address_unused' );
|
2018-10-29 22:55:26 +01:00
|
|
|
return $result->result;
|
2022-02-12 22:28:25 +01:00
|
|
|
} catch ( LBRYDaemonException $e ) {
|
|
|
|
$this->logger->log( 'address_unused error', $e->getMessage() . ' | Code: ' . $e->getCode() );
|
|
|
|
LBRY()->notice->set_notice( 'error', 'Issue getting unused address.' );
|
2018-10-29 22:55:26 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-09-01 01:50:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-27 23:42:20 +01:00
|
|
|
* Returns an paginated array of Address lists
|
2018-11-04 00:37:32 +01:00
|
|
|
* https://lbry.tech/api/sdk#address_list
|
2020-01-28 00:07:32 +01:00
|
|
|
* @param int $page Pagination page number
|
2018-11-09 19:34:12 +01:00
|
|
|
* @return array Array of address lists linked to this account
|
2018-11-04 00:37:32 +01:00
|
|
|
*/
|
2022-02-12 22:28:25 +01:00
|
|
|
public function address_list( $page = 1 ) {
|
2020-01-27 23:42:20 +01:00
|
|
|
// Get 20 per page
|
|
|
|
$params = array(
|
|
|
|
'page' => $page,
|
|
|
|
'page_size' => 20
|
|
|
|
);
|
2018-11-04 00:37:32 +01:00
|
|
|
try {
|
2022-02-12 22:28:25 +01:00
|
|
|
$result = $this->request( 'address_list', $params );
|
2020-01-28 00:04:37 +01:00
|
|
|
return $result->result->items;
|
2022-02-12 22:28:25 +01:00
|
|
|
} catch ( LBRYDaemonException $e ) {
|
|
|
|
$this->logger->log( 'address_list error', $e->getMessage() . ' | Code: ' . $e->getCode() );
|
|
|
|
LBRY()->notice->set_notice( 'error', 'Issue getting address list.' );
|
2018-11-04 00:37:32 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-27 23:53:01 +01:00
|
|
|
* Returns the available balance of a current LBRY account
|
2022-01-25 21:27:12 +01:00
|
|
|
* https://lbry.tech/api/sdk#wallet_balance
|
|
|
|
* @param string $address Wallet Address
|
2022-02-12 22:28:25 +01:00
|
|
|
* @return object $wallet_balance Wallet Balance
|
2022-01-25 21:27:12 +01:00
|
|
|
*
|
2018-09-01 01:50:29 +02:00
|
|
|
*/
|
2018-10-05 01:47:32 +02:00
|
|
|
public function wallet_balance()
|
2022-02-12 22:28:25 +01:00
|
|
|
{
|
2022-01-25 21:27:12 +01:00
|
|
|
try { // Convert JSON string to an object
|
2022-02-12 22:28:25 +01:00
|
|
|
$result = $this->request( 'wallet_balance' );
|
2022-01-25 21:27:12 +01:00
|
|
|
return $result;
|
2018-10-29 22:55:26 +01:00
|
|
|
} catch (LBRYDaemonException $e) {
|
2022-01-29 00:05:24 +01:00
|
|
|
$this->logger->log('wallet_balance error', $e->getMessage() . ' | Code: ' . $e->getCode());
|
|
|
|
LBRY()->notice->set_notice('error', 'Issue getting wallet balance.');
|
2018-10-29 22:55:26 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-09-01 03:14:12 +02:00
|
|
|
}
|
|
|
|
|
2018-09-13 01:39:13 +02:00
|
|
|
/**
|
2018-11-04 00:37:32 +01:00
|
|
|
* Returns a list of channels for this account
|
|
|
|
* https://lbry.tech/api/sdk#channel_list
|
2020-01-28 00:00:13 +01:00
|
|
|
* @param int $page Pagination page number
|
2018-11-09 19:34:12 +01:00
|
|
|
* @return array claim dictionary or null if empty
|
2018-09-13 01:39:13 +02:00
|
|
|
*/
|
2022-02-12 22:28:25 +01:00
|
|
|
public function channel_list( $page = 1 )
|
2018-09-01 03:14:12 +02:00
|
|
|
{
|
2020-01-28 00:00:13 +01:00
|
|
|
$params = array(
|
|
|
|
'page' => $page,
|
|
|
|
'page_size' => 20
|
|
|
|
);
|
|
|
|
|
2018-10-29 22:55:26 +01:00
|
|
|
try {
|
2022-02-12 22:28:25 +01:00
|
|
|
$result = $this->request( 'channel_list', $params )->result->items;
|
|
|
|
return empty( $result ) ? null : $result;
|
|
|
|
} catch ( LBRYDaemonException $e ) {
|
|
|
|
$this->logger->log( 'channel_list error', $e->getMessage() . ' | Code: ' . $e->getCode() );
|
|
|
|
LBRY()->notice->set_notice( 'error', 'Issue retrieving channel list.' );
|
2018-10-29 22:55:26 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-09-01 01:50:29 +02:00
|
|
|
}
|
|
|
|
|
2018-09-13 01:39:13 +02:00
|
|
|
/**
|
2018-11-04 00:37:32 +01:00
|
|
|
* Create a claim for a new channel
|
2020-01-28 00:23:43 +01:00
|
|
|
* https://lbry.tech/api/sdk#channel_create
|
2018-11-09 19:34:12 +01:00
|
|
|
* @return array dictionary containing result of the request
|
2018-09-13 01:39:13 +02:00
|
|
|
*/
|
2022-02-12 22:28:25 +01:00
|
|
|
|
|
|
|
public function channel_new( $channel_name, $channel_bid )
|
2018-09-13 01:39:13 +02:00
|
|
|
{
|
2018-10-05 19:22:47 +02:00
|
|
|
// TODO: Sanitize channel name and bid
|
|
|
|
// Make sure no @ sign, as we will add that
|
2022-02-12 22:28:25 +01:00
|
|
|
if ( strpos( $channel_name, '@' ) ) {
|
|
|
|
throw new \Exception( 'Illegal character "@" in channel name', 1 );
|
2018-10-05 19:22:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// No white space allowed
|
2022-02-12 22:28:25 +01:00
|
|
|
if ( strpos( $channel_name, ' ' ) ) {
|
|
|
|
throw new \Exception( "No spaces allowed in channel name", 1 );
|
2018-10-05 19:22:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$channel_name = '@' . $channel_name;
|
|
|
|
|
2018-10-29 22:55:26 +01:00
|
|
|
try {
|
|
|
|
$result = $this->request(
|
2020-01-28 00:23:43 +01:00
|
|
|
'channel_create',
|
2018-10-29 22:55:26 +01:00
|
|
|
array(
|
2020-01-28 00:23:43 +01:00
|
|
|
'name' => $channel_name,
|
2022-02-09 17:17:45 +01:00
|
|
|
'bid' => $channel_bid
|
2018-10-29 22:55:26 +01:00
|
|
|
)
|
|
|
|
);
|
2022-02-09 17:17:45 +01:00
|
|
|
|
|
|
|
$this->logger->log( 'channel_create success!', 'Successfully created channel with result: ' . print_r( $result->result, true ) );
|
2018-10-29 22:55:26 +01:00
|
|
|
return $result->result;
|
2022-02-09 17:17:45 +01:00
|
|
|
|
2018-10-29 22:55:26 +01:00
|
|
|
} catch (LBRYDaemonException $e) {
|
2022-02-12 22:28:25 +01:00
|
|
|
$this->logger->log( 'channel_new error', $e->getMessage() . ' | Code: ' . $e->getCode() );
|
|
|
|
throw new \Exception( 'Issue creating new channel.', 1 );
|
2018-10-29 22:55:26 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-09-13 01:39:13 +02:00
|
|
|
}
|
|
|
|
|
2020-01-28 05:33:59 +01:00
|
|
|
/**
|
|
|
|
* Returns the canonical URL for the supplied claim ID, null otherwise
|
|
|
|
* @param string $claim_id
|
|
|
|
* @return string Canonical URL, null if not found
|
|
|
|
*/
|
2022-02-12 22:28:25 +01:00
|
|
|
public function canonical_url( $claim_id = null )
|
2020-01-28 05:33:59 +01:00
|
|
|
{
|
2022-02-12 22:28:25 +01:00
|
|
|
if ( ! $claim_id ) {
|
2020-01-28 05:54:40 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-01-28 05:33:59 +01:00
|
|
|
try {
|
|
|
|
$result = $this->request(
|
|
|
|
'claim_search',
|
|
|
|
array(
|
|
|
|
'claim_id' => $claim_id,
|
|
|
|
'no_totals' => true
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$items = $result->result->items;
|
2022-02-12 22:28:25 +01:00
|
|
|
if ( ! $items ) {
|
2020-01-28 05:33:59 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $items[0]->canonical_url;
|
2022-02-12 22:28:25 +01:00
|
|
|
} catch ( LBRYDaemonException $e ) {
|
|
|
|
$this->logger->log( 'canonical_url error', $e->getMessage() . ' | Code: ' . $e->getCode() );
|
2020-01-28 05:33:59 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-12 09:42:40 +02:00
|
|
|
/**
|
|
|
|
* Publishes a post to the LBRY Network
|
2018-11-04 00:37:32 +01:00
|
|
|
* https://lbry.tech/api/sdk#publish
|
2018-11-09 19:34:12 +01:00
|
|
|
* @param array $args An array containing necessary data for publish post
|
|
|
|
*
|
|
|
|
* Available params:
|
|
|
|
* ['name', 'bid', 'file_path', 'title', 'description', 'language', 'license', 'channel_id', 'thumbnail']
|
|
|
|
*
|
|
|
|
* @return object $result
|
2018-10-12 09:42:40 +02:00
|
|
|
*/
|
2022-02-12 22:28:25 +01:00
|
|
|
public function publish( $args )
|
2018-10-12 09:42:40 +02:00
|
|
|
{
|
2018-10-29 22:55:26 +01:00
|
|
|
try {
|
|
|
|
$result = $this->request(
|
|
|
|
'publish',
|
|
|
|
$args
|
|
|
|
);
|
2022-02-12 22:28:25 +01:00
|
|
|
$this->logger->log( 'publish success!', 'Successfully published post with result: ' . print_r( $result->result, true ) );
|
2018-10-29 22:55:26 +01:00
|
|
|
return $result->result;
|
2022-02-12 22:28:25 +01:00
|
|
|
} catch ( LBRYDaemonException $e ) {
|
|
|
|
$this->logger->log('publish error', $e->getMessage() . ' | Code: ' . $e->getCode() );
|
|
|
|
LBRY()->notice->set_notice( 'error', 'Issue publishing / updating post to LBRY Network.' );
|
2018-10-29 22:55:26 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-10-12 09:42:40 +02:00
|
|
|
}
|
2018-10-29 22:55:26 +01:00
|
|
|
|
2018-09-13 01:39:13 +02:00
|
|
|
/**
|
|
|
|
* Sends a cURL request to the LBRY Daemon
|
2018-11-09 19:34:12 +01:00
|
|
|
* @param string $method The method to call on the LBRY API
|
|
|
|
* @param array $params The Parameters to send the LBRY API Call
|
|
|
|
* @return string The cURL response
|
2018-09-13 01:39:13 +02:00
|
|
|
*/
|
2022-02-12 22:28:25 +01:00
|
|
|
private function request( $method, $params = array() )
|
2018-09-01 01:50:29 +02:00
|
|
|
{
|
|
|
|
// JSONify our request data
|
|
|
|
$data = array(
|
|
|
|
'method' => $method,
|
|
|
|
'params' => $params
|
|
|
|
);
|
2022-02-12 22:28:25 +01:00
|
|
|
$data = json_encode( $data );
|
2018-09-01 01:50:29 +02:00
|
|
|
|
|
|
|
// Send it via curl
|
|
|
|
$ch = curl_init();
|
2022-02-12 22:28:25 +01:00
|
|
|
curl_setopt( $ch, CURLOPT_URL, $this->address );
|
|
|
|
curl_setopt( $ch, CURLOPT_POST, true );
|
|
|
|
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );
|
|
|
|
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
|
|
|
|
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json' ) );
|
|
|
|
curl_setopt( $ch, CURLOPT_AUTOREFERER, false );
|
|
|
|
curl_setopt( $ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
|
|
|
|
curl_setopt( $ch, CURLOPT_HEADER, 0 );
|
2018-09-01 01:50:29 +02:00
|
|
|
|
2022-02-12 22:28:25 +01:00
|
|
|
$result = curl_exec( $ch );
|
|
|
|
$response_code = curl_getinfo( $ch, CURLINFO_RESPONSE_CODE );
|
|
|
|
curl_close( $ch );
|
2018-10-29 22:55:26 +01:00
|
|
|
|
2022-02-12 22:28:25 +01:00
|
|
|
if ( $response_code != '200' ) {
|
|
|
|
$this->logger->log( "Damon Connection Issue", "Daemon connection returned response code $response_code" );
|
|
|
|
throw new LBRYDaemonException( "Daemon Connection Issue", $response_code );
|
2018-10-29 22:55:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-12 22:28:25 +01:00
|
|
|
$result = json_decode( $result );
|
|
|
|
$this->check_for_errors( $result );
|
2018-10-29 22:55:26 +01:00
|
|
|
return $result;
|
2018-10-05 19:22:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-02-12 22:28:25 +01:00
|
|
|
* Checks for errors in decoded daemon response and throws an exception if it finds one
|
2018-10-05 19:22:47 +02:00
|
|
|
* @param $response
|
|
|
|
*/
|
2022-02-12 22:28:25 +01:00
|
|
|
private function check_for_errors( $response )
|
2018-10-05 19:22:47 +02:00
|
|
|
{
|
2022-02-12 22:28:25 +01:00
|
|
|
if ( property_exists( $response, 'error' ) ) {
|
2018-10-29 22:55:26 +01:00
|
|
|
$message = $response->error->message;
|
|
|
|
$code = $response->error->code;
|
2022-02-12 22:28:25 +01:00
|
|
|
$this->logger->log( "Daemon error code $code", $message );
|
|
|
|
throw new LBRYDaemonException( $message, $code );
|
2018-10-05 19:22:47 +02:00
|
|
|
}
|
2018-09-01 01:50:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Temporary placeholder function for daemon. Not currently in use.
|
|
|
|
* @return [type] [description]
|
|
|
|
*/
|
2018-10-29 22:55:26 +01:00
|
|
|
// private function download_daemon()
|
|
|
|
// {
|
|
|
|
// $output_filename = "lbrydaemon";
|
|
|
|
//
|
|
|
|
// // HACK: Shouldn't just directly download, need to know OS, etc
|
|
|
|
// // TODO: Make sure we are only installing if not there or corrupted
|
|
|
|
// $host = "http://build.lbry.io/daemon/build-6788_commit-5099e19_branch-lbryum-refactor/mac/lbrynet";
|
|
|
|
// $fp = fopen(LBRY_URI . '/' . $output_filename, 'w+');
|
|
|
|
// $ch = curl_init();
|
|
|
|
// curl_setopt($ch, CURLOPT_URL, $host);
|
|
|
|
// curl_setopt($ch, CURLOPT_VERBOSE, 1);
|
|
|
|
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
// curl_setopt($ch, CURLOPT_FILE, $fp);
|
|
|
|
// curl_setopt($ch, CURLOPT_AUTOREFERER, false);
|
|
|
|
// curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
|
|
|
|
// curl_setopt($ch, CURLOPT_HEADER, 0);
|
|
|
|
//
|
|
|
|
// $result = curl_exec($ch);
|
|
|
|
// curl_close($ch);
|
|
|
|
// fclose($fp);
|
|
|
|
//
|
|
|
|
// $filepath = LBRY_URI . '/' . $output_filename;
|
|
|
|
//
|
|
|
|
// `chmod +x {$filepath}`;
|
|
|
|
// error_log(`{$filepath} status`);
|
|
|
|
// `{$filepath} start &`;
|
|
|
|
// }
|
|
|
|
}
|
2018-09-01 01:50:29 +02:00
|
|
|
|
2018-10-29 22:55:26 +01:00
|
|
|
class LBRYDaemonException extends Exception
|
|
|
|
{
|
|
|
|
public function __contstruct($message = '', $code = 0, Exception $previous = null)
|
|
|
|
{
|
|
|
|
parent::__construct($message, $code, $previous);
|
2018-09-01 01:50:29 +02:00
|
|
|
}
|
|
|
|
}
|