2018-09-01 01:50:29 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Main class for the Daemon setup
|
|
|
|
*
|
|
|
|
* @package LBRYPress
|
|
|
|
*/
|
|
|
|
|
|
|
|
class LBRY_Daemon
|
|
|
|
{
|
|
|
|
private $address = 'localhost:5279';
|
|
|
|
|
2018-10-29 22:55:26 +01:00
|
|
|
private $logger = null;
|
|
|
|
|
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();
|
|
|
|
$this->init();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function init()
|
2018-09-01 01:50:29 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an unused wallet address
|
2018-09-13 21:02:32 +02:00
|
|
|
* https://lbryio.github.io/lbry/#wallet_unused_address
|
2018-09-01 01:50:29 +02:00
|
|
|
* @return string Unused wallet address in base58
|
|
|
|
*/
|
|
|
|
public function wallet_unused_address()
|
|
|
|
{
|
2018-10-29 22:55:26 +01:00
|
|
|
try {
|
|
|
|
$result = $this->request('wallet_unused_address');
|
|
|
|
return $result->result;
|
|
|
|
} catch (LBRYDaemonException $e) {
|
|
|
|
$this->logger->log('wallet_unused_address error', $e->getMessage() . ' | Code: ' . $e->getCode());
|
|
|
|
LBRY()->notice->set_notice('error', 'Issue getting unused wallet address.');
|
|
|
|
return;
|
|
|
|
}
|
2018-09-01 01:50:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the balance of a current LBRY wallet
|
2018-09-13 21:02:32 +02:00
|
|
|
* https://lbryio.github.io/lbry/cli/#wallet_balance
|
2018-09-01 01:50:29 +02:00
|
|
|
* @param string $address Wallet Address
|
|
|
|
* @return float Wallet Balance
|
|
|
|
*/
|
2018-10-05 01:47:32 +02:00
|
|
|
public function wallet_balance()
|
2018-09-01 01:50:29 +02:00
|
|
|
{
|
2018-10-29 22:55:26 +01:00
|
|
|
try {
|
|
|
|
$result = $this->request('wallet_balance');
|
|
|
|
return $result->result;
|
|
|
|
} catch (LBRYDaemonException $e) {
|
|
|
|
$this->logger->log('wallet_balance error', $e->getMessage() . ' | Code: ' . $e->getCode());
|
|
|
|
LBRY()->notice->set_notice('error', 'Issue getting wallet address.');
|
|
|
|
return;
|
|
|
|
}
|
2018-09-01 03:14:12 +02:00
|
|
|
}
|
|
|
|
|
2018-09-13 01:39:13 +02:00
|
|
|
/**
|
|
|
|
* https://lbryio.github.io/lbry/#channel_list
|
2018-10-05 02:19:06 +02:00
|
|
|
* @return array claim dictionary or null if empty
|
2018-09-13 01:39:13 +02:00
|
|
|
*/
|
2018-09-01 03:14:12 +02:00
|
|
|
public function channel_list()
|
|
|
|
{
|
2018-10-29 22:55:26 +01:00
|
|
|
try {
|
|
|
|
$result = $this->request('channel_list')->result;
|
|
|
|
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.');
|
|
|
|
return;
|
|
|
|
}
|
2018-09-01 01:50:29 +02:00
|
|
|
}
|
|
|
|
|
2018-09-13 01:39:13 +02:00
|
|
|
/**
|
|
|
|
* https://lbryio.github.io/lbry/#channel_new
|
|
|
|
* @return array dictionary containing result of the request
|
|
|
|
*/
|
2018-09-14 02:34:11 +02:00
|
|
|
public function channel_new($channel_name, $bid_amount)
|
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
|
|
|
|
if (strpos($channel_name, '@')) {
|
|
|
|
throw new \Exception('Illegal character "@" in channel name', 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// No white space allowed
|
|
|
|
if (strpos($channel_name, ' ')) {
|
|
|
|
throw new \Exception("No spaces allowed in channel name", 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
$channel_name = '@' . $channel_name;
|
|
|
|
|
2018-10-29 22:55:26 +01:00
|
|
|
try {
|
|
|
|
$result = $this->request(
|
|
|
|
'channel_new',
|
|
|
|
array(
|
|
|
|
'channel_name' => $channel_name,
|
|
|
|
'amount' => floatval($bid_amount)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
return $result->result;
|
|
|
|
} catch (LBRYDaemonException $e) {
|
|
|
|
$this->logger->log('channel_new error', $e->getMessage() . ' | Code: ' . $e->getCode());
|
|
|
|
LBRY()->notice->set_notice('error', 'Issue creating new channel.');
|
|
|
|
return;
|
|
|
|
}
|
2018-09-13 01:39:13 +02:00
|
|
|
}
|
|
|
|
|
2018-10-12 09:42:40 +02:00
|
|
|
/**
|
|
|
|
* Publishes a post to the LBRY Network
|
2018-10-12 22:31:23 +02:00
|
|
|
* @param string $name The slug for the post
|
|
|
|
* @param float $bid The amount of LBC to bid
|
|
|
|
* @param string $filepath The path of the temporary content file
|
|
|
|
* @param string $title The Title of the post
|
|
|
|
* @param string $description The Description of the Post
|
|
|
|
* @param string $language Two letter ISO Code of the language
|
|
|
|
* @return string $channel The Claim ID of the Channel
|
2018-10-12 09:42:40 +02:00
|
|
|
*/
|
2018-10-31 20:48:58 +01:00
|
|
|
public function publish($name, $bid, $filepath, $title, $description, $language, $channel, $thumbnail = false)
|
2018-10-12 09:42:40 +02:00
|
|
|
{
|
2018-10-12 22:31:23 +02:00
|
|
|
$args = array(
|
|
|
|
'name' => $name,
|
|
|
|
'bid' => $bid,
|
|
|
|
'file_path' => $filepath,
|
|
|
|
'title' => $title,
|
|
|
|
'description' => $description,
|
|
|
|
'language' => $language,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Make sure we aren't publishing to unattributed
|
|
|
|
if ($channel != 'null') {
|
|
|
|
$args['channel_id'] = $channel;
|
|
|
|
}
|
|
|
|
|
2018-10-31 20:48:58 +01:00
|
|
|
if ($thumbnail) {
|
|
|
|
$args['thumbnail'] = $thumbnail;
|
|
|
|
}
|
2018-11-01 18:13:55 +01:00
|
|
|
|
2018-10-29 22:55:26 +01:00
|
|
|
try {
|
|
|
|
$result = $this->request(
|
|
|
|
'publish',
|
|
|
|
$args
|
|
|
|
);
|
|
|
|
return $result->result;
|
|
|
|
} catch (LBRYDaemonException $e) {
|
2018-11-01 18:13:55 +01:00
|
|
|
$this->logger->log('publish error', $e->getMessage() . ' | Code: ' . $e->getCode());
|
2018-10-29 22:55:26 +01:00
|
|
|
LBRY()->notice->set_notice('error', 'Issue publishing / updating post to LBRY Network.');
|
|
|
|
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
|
|
|
|
* @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-01 01:50:29 +02:00
|
|
|
private function request($method, $params = array())
|
|
|
|
{
|
|
|
|
// JSONify our request data
|
|
|
|
$data = array(
|
|
|
|
'method' => $method,
|
|
|
|
'params' => $params
|
|
|
|
);
|
|
|
|
$data = json_encode($data);
|
|
|
|
|
|
|
|
// Send it via curl
|
|
|
|
$ch = curl_init();
|
|
|
|
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);
|
|
|
|
|
|
|
|
$result = curl_exec($ch);
|
2018-10-29 22:55:26 +01:00
|
|
|
$response_code = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
|
2018-09-01 01:50:29 +02:00
|
|
|
curl_close($ch);
|
2018-10-29 22:55:26 +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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$result = json_decode($result);
|
|
|
|
$this->check_for_errors($result);
|
|
|
|
return $result;
|
2018-10-05 19:22:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks for erros in decoded daemon response and throws an exception if it finds one
|
|
|
|
* @param $response
|
|
|
|
*/
|
|
|
|
private function check_for_errors($response)
|
|
|
|
{
|
|
|
|
if (property_exists($response, 'error')) {
|
2018-10-29 22:55:26 +01:00
|
|
|
$message = $response->error->message;
|
|
|
|
$code = $response->error->code;
|
|
|
|
$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
|
|
|
}
|
|
|
|
}
|