commit
71809eabaa
10 changed files with 797 additions and 72 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -6,5 +6,9 @@
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
|
||||||
tmp/*
|
tmp/*
|
||||||
|
logs/*
|
||||||
|
|
||||||
!*.gitkeep
|
!*.gitkeep
|
||||||
|
|
||||||
|
#Ignore config file
|
||||||
|
lbry_config.php
|
||||||
|
|
|
@ -9,10 +9,18 @@ class LBRY_Daemon
|
||||||
{
|
{
|
||||||
private $address = 'localhost:5279';
|
private $address = 'localhost:5279';
|
||||||
|
|
||||||
|
private $logger = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* LBRY Daemon Object constructor
|
* LBRY Daemon Object constructor
|
||||||
*/
|
*/
|
||||||
public function __construct()
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->logger = new LBRY_Daemon_Logger();
|
||||||
|
$this->init();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function init()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,8 +31,14 @@ class LBRY_Daemon
|
||||||
*/
|
*/
|
||||||
public function wallet_unused_address()
|
public function wallet_unused_address()
|
||||||
{
|
{
|
||||||
|
try {
|
||||||
$result = $this->request('wallet_unused_address');
|
$result = $this->request('wallet_unused_address');
|
||||||
return $result->result;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -35,8 +49,14 @@ class LBRY_Daemon
|
||||||
*/
|
*/
|
||||||
public function wallet_balance()
|
public function wallet_balance()
|
||||||
{
|
{
|
||||||
|
try {
|
||||||
$result = $this->request('wallet_balance');
|
$result = $this->request('wallet_balance');
|
||||||
return $result->result;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -45,8 +65,14 @@ class LBRY_Daemon
|
||||||
*/
|
*/
|
||||||
public function channel_list()
|
public function channel_list()
|
||||||
{
|
{
|
||||||
|
try {
|
||||||
$result = $this->request('channel_list')->result;
|
$result = $this->request('channel_list')->result;
|
||||||
return empty($result) ? null : $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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -69,6 +95,7 @@ class LBRY_Daemon
|
||||||
|
|
||||||
$channel_name = '@' . $channel_name;
|
$channel_name = '@' . $channel_name;
|
||||||
|
|
||||||
|
try {
|
||||||
$result = $this->request(
|
$result = $this->request(
|
||||||
'channel_new',
|
'channel_new',
|
||||||
array(
|
array(
|
||||||
|
@ -76,8 +103,12 @@ class LBRY_Daemon
|
||||||
'amount' => floatval($bid_amount)
|
'amount' => floatval($bid_amount)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
$this->check_for_errors($result);
|
|
||||||
return $result->result;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -107,13 +138,17 @@ class LBRY_Daemon
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Bring thumbnails into the mix
|
// TODO: Bring thumbnails into the mix
|
||||||
|
try {
|
||||||
$result = $this->request(
|
$result = $this->request(
|
||||||
'publish',
|
'publish',
|
||||||
$args
|
$args
|
||||||
);
|
);
|
||||||
|
return $result->result;
|
||||||
$this->check_for_errors($result);
|
} catch (LBRYDaemonException $e) {
|
||||||
return $result;
|
$this->logger->log('wallet_unused_address error', $e->getMessage() . ' | Code: ' . $e->getCode());
|
||||||
|
LBRY()->notice->set_notice('error', 'Issue publishing / updating post to LBRY Network.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -143,8 +178,18 @@ class LBRY_Daemon
|
||||||
curl_setopt($ch, CURLOPT_HEADER, 0);
|
curl_setopt($ch, CURLOPT_HEADER, 0);
|
||||||
|
|
||||||
$result = curl_exec($ch);
|
$result = curl_exec($ch);
|
||||||
|
$response_code = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
|
||||||
curl_close($ch);
|
curl_close($ch);
|
||||||
return json_decode($result);
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -154,7 +199,10 @@ class LBRY_Daemon
|
||||||
private function check_for_errors($response)
|
private function check_for_errors($response)
|
||||||
{
|
{
|
||||||
if (property_exists($response, 'error')) {
|
if (property_exists($response, 'error')) {
|
||||||
throw new \Exception($response->error->message, $response->error->code);
|
$message = $response->error->message;
|
||||||
|
$code = $response->error->code;
|
||||||
|
$this->logger->log("Daemon error code $code", $message);
|
||||||
|
throw new LBRYDaemonException($message, $code);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -162,31 +210,39 @@ class LBRY_Daemon
|
||||||
* Temporary placeholder function for daemon. Not currently in use.
|
* Temporary placeholder function for daemon. Not currently in use.
|
||||||
* @return [type] [description]
|
* @return [type] [description]
|
||||||
*/
|
*/
|
||||||
private function download_daemon()
|
// 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 &`;
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
class LBRYDaemonException extends Exception
|
||||||
{
|
{
|
||||||
$output_filename = "lbrydaemon";
|
public function __contstruct($message = '', $code = 0, Exception $previous = null)
|
||||||
|
{
|
||||||
// HACK: Shouldn't just directly download, need to know OS, etc
|
parent::__construct($message, $code, $previous);
|
||||||
// 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 &`;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,9 +8,82 @@
|
||||||
class LBRY_Daemon_Logger
|
class LBRY_Daemon_Logger
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* [__construct description]
|
* The directory to log to
|
||||||
|
* @var string
|
||||||
*/
|
*/
|
||||||
public function __construct()
|
private $dir = LBRY_ABSPATH . 'logs/';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The filename to log to
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $filename;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The file ext
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $ext = '.log';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Max size of the file before rotating
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
private $maxsize = 15728640; // 15MB;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $filename unique name for this logger to log to
|
||||||
|
*/
|
||||||
|
public function __construct($filename = 'daemon')
|
||||||
{
|
{
|
||||||
|
$this->filename = $filename;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Log to a file, with file size maximum. Adds hashs to old files
|
||||||
|
*
|
||||||
|
* @param string $event
|
||||||
|
* @param string $text
|
||||||
|
*/
|
||||||
|
public function log($event, $message = null)
|
||||||
|
{
|
||||||
|
$filepath = $this->dir . $this->filename . $this->ext;
|
||||||
|
|
||||||
|
// If our file is past our size limit, stash it
|
||||||
|
if (file_exists($filepath) && filesize($filepath) > $this->maxsize) {
|
||||||
|
rename($filepath, $this->dir . $this->filename . time() . $this->ext);
|
||||||
|
|
||||||
|
$logfiles = scandir($this->dir);
|
||||||
|
|
||||||
|
// If we have to many files, delete the oldest one
|
||||||
|
if (count($logfiles) > 7) {
|
||||||
|
$oldest = PHP_INT_MAX;
|
||||||
|
foreach ($logfiles as $file) {
|
||||||
|
if (!strstr($file, $this->filename)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$stamp = substr($file, strlen($this->filename));
|
||||||
|
$stamp = str_replace($this->ext, '', $stamp);
|
||||||
|
if ($stamp && $stamp < $oldest) {
|
||||||
|
$oldest = $stamp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($logfiles as $file) {
|
||||||
|
if (strstr($file, $this->filename) && strstr($file, $oldest)) {
|
||||||
|
unlink($this->dir . $file);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$date = date('Y-m-d H:i:s');
|
||||||
|
$space = str_repeat(' ', strlen($date) + 1);
|
||||||
|
$data = date('Y-m-d H:i:s') . " ";
|
||||||
|
$data .= "EVENT: " . $event . PHP_EOL;
|
||||||
|
$data .= $space . "MESSAGE: " . $message . PHP_EOL;
|
||||||
|
file_put_contents($filepath, $data, FILE_APPEND);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,33 +18,244 @@ class LBRY_Speech
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->parser = new LBRY_Speech_Parser();
|
$this->parser = new LBRY_Speech_Parser();
|
||||||
add_action('save_post', array($this, 'upload_assets'));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
if (is_admin()) {
|
||||||
* Checks to see if we need to rewrite URLS, does if necessary
|
add_action('save_post', array($this, 'upload_media'), 10, 2);
|
||||||
*/
|
} else {
|
||||||
public function maybe_rewrite_urls()
|
// Replace the image srcsets
|
||||||
{
|
add_filter('wp_calculate_image_srcset', array($this->parser, 'replace_image_srcset'), 10, 5);
|
||||||
// See if we have a Spee.ch URL and if we are on the front-end
|
// Core filter for lots of image source calls
|
||||||
$speech_url = get_option(LBRY_SETTINGS)[LBRY_SPEECH];
|
add_filter('wp_get_attachment_image_src', array($this->parser, 'replace_attachment_image_src'), 10, 3);
|
||||||
if ($speech_url != '' && !is_admin()) {
|
// Replace any left over urls with speech urls
|
||||||
ob_start(array($this->parser, 'rewrite'));
|
add_filter('the_content', array($this->parser, 'replace_urls_with_speech'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Uploads assets to the speech server
|
* Uploads assets to the speech server
|
||||||
|
* @param int $post_id The ID of the post to
|
||||||
|
* @return bool True if successful, false if not or if no Speech URL available
|
||||||
*/
|
*/
|
||||||
public function upload_assets()
|
public function upload_media($post_id, $post)
|
||||||
{
|
{
|
||||||
|
// Only check post_type of Post
|
||||||
|
if ('post' !== $post->post_type) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
$speech_url = get_option(LBRY_SETTINGS)[LBRY_SPEECH];
|
$speech_url = get_option(LBRY_SETTINGS)[LBRY_SPEECH];
|
||||||
|
|
||||||
// Die if we don't have a spee.ch url
|
// Die if we don't have a spee.ch url
|
||||||
if (!$speech_url || $speech_url === '') {
|
if (!$speech_url || $speech_url === '') {
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Find assets, upload them to the Spee.ch Server
|
$all_media = $this->find_media($post_id);
|
||||||
|
|
||||||
|
// IDEA: Notify user if post save time will take a while, may be a concern for request timeouts
|
||||||
|
if ($all_media) {
|
||||||
|
$requests = array();
|
||||||
|
|
||||||
|
// Build all the Curl Requests
|
||||||
|
foreach ($all_media as $media) {
|
||||||
|
$params = array(
|
||||||
|
'name' => $media->name,
|
||||||
|
'file' => $media->file,
|
||||||
|
'title' => $media->title,
|
||||||
|
'type' => $media->type
|
||||||
|
);
|
||||||
|
|
||||||
|
// Pull Channel and Password from config file for now
|
||||||
|
// COMBAK: This will change in the future
|
||||||
|
if (LBRY_SPEECH_CHANNEL && LBRY_SPEECH_CHANNEL_PASSWORD) {
|
||||||
|
$params['channelName'] = LBRY_SPEECH_CHANNEL;
|
||||||
|
$params['channelPassword'] = LBRY_SPEECH_CHANNEL_PASSWORD;
|
||||||
|
}
|
||||||
|
|
||||||
|
$ch = $this->build_request('publish', $params);
|
||||||
|
$requests[] = array(
|
||||||
|
'request' => $ch,
|
||||||
|
'media' => $media
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init the curl multi handle
|
||||||
|
$mh = curl_multi_init();
|
||||||
|
|
||||||
|
// Add each request to the multi handle
|
||||||
|
foreach ($requests as $request) {
|
||||||
|
curl_multi_add_handle($mh, $request['request']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Execute all requests simultaneously
|
||||||
|
$running = null;
|
||||||
|
do {
|
||||||
|
curl_multi_exec($mh, $running);
|
||||||
|
} while ($running);
|
||||||
|
|
||||||
|
// Close the handles
|
||||||
|
foreach ($requests as $request) {
|
||||||
|
curl_multi_remove_handle($mh, $request['request']);
|
||||||
|
}
|
||||||
|
curl_multi_close($mh);
|
||||||
|
|
||||||
|
// Run through responses, and upload meta as necessary
|
||||||
|
foreach ($requests as $request) {
|
||||||
|
$result = json_decode(curl_multi_getcontent($request['request']));
|
||||||
|
$media = $request['media'];
|
||||||
|
$response_code = curl_getinfo($request['request'], CURLINFO_RESPONSE_CODE);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// check we got a success code
|
||||||
|
if ($response_code != '200') {
|
||||||
|
if (!empty($result) && !$result->success && $result->message) {
|
||||||
|
throw new \Exception("API Issue with message: " . $result->message);
|
||||||
|
} else {
|
||||||
|
throw new \Exception("Speech URL Connection Issue | Code: " . $response_code, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update image meta
|
||||||
|
if ($result && $result->success) {
|
||||||
|
$meta = wp_get_attachment_metadata($media->id);
|
||||||
|
if ($media->image_size) {
|
||||||
|
$meta['sizes'][$media->image_size]['speech_asset_url'] = $result->data->serveUrl;
|
||||||
|
} else {
|
||||||
|
$meta['speech_asset_url'] = $result->data->serveUrl;
|
||||||
|
}
|
||||||
|
wp_update_attachment_metadata($media->id, $meta);
|
||||||
|
} else { // Something unhandled happened here
|
||||||
|
throw new \Exception("Unknown Speech Upload issue for asset");
|
||||||
|
}
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$image_size = $media->image_size ? $media->image_size : 'full';
|
||||||
|
error_log('Failed to upload asset with ID ' . $media->id . ' for size ' . $image_size . ' to supplied speech URL.');
|
||||||
|
error_log($e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds all media attached to a post
|
||||||
|
* @param int $post_id The post to search
|
||||||
|
* @return array An array of Speech Media Objects
|
||||||
|
*/
|
||||||
|
private function find_media($post_id)
|
||||||
|
{
|
||||||
|
$all_media = array();
|
||||||
|
|
||||||
|
// Get content and put into a DOMDocument
|
||||||
|
$content = get_post_field('post_content', $post_id);
|
||||||
|
if (!$content) {
|
||||||
|
return $all_media;
|
||||||
|
}
|
||||||
|
|
||||||
|
$images = $this->parser->scrape_images($content);
|
||||||
|
|
||||||
|
// Get all the image ID's
|
||||||
|
$image_ids = array();
|
||||||
|
foreach ($images as $image) {
|
||||||
|
$new_id = $this->parser->get_attachment_id_from_tag('image', $image);
|
||||||
|
if ($new_id) {
|
||||||
|
$image_ids[] = $new_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Don't forget the featured image
|
||||||
|
if ($featured_id = get_post_thumbnail_id($post_id)) {
|
||||||
|
$image_ids = array_merge($image_ids, array($featured_id));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Throw each image into a media object
|
||||||
|
foreach ($image_ids as $attachment_id) {
|
||||||
|
|
||||||
|
// Create main image media object
|
||||||
|
$meta = wp_get_attachment_metadata($attachment_id);
|
||||||
|
|
||||||
|
// If we don't have meta, get out because none of this will work
|
||||||
|
if (!$meta) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$this->is_published($meta)) {
|
||||||
|
$all_media[] = new LBRY_Speech_Media($attachment_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
// COMBAK: find a way to make this more efficient?
|
||||||
|
// Create a media object for each image size
|
||||||
|
// Get images sizes for this attachment, as not all image sizes implemented
|
||||||
|
$image_sizes = wp_get_attachment_metadata($attachment_id)['sizes'];
|
||||||
|
|
||||||
|
foreach ($image_sizes as $size => $meta) {
|
||||||
|
if (!$this->is_published($meta)) {
|
||||||
|
$all_media[] = new LBRY_Speech_Media($attachment_id, array('image_size' => $size));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$videos = $this->parser->scrape_videos($content);
|
||||||
|
|
||||||
|
$video_ids = array();
|
||||||
|
foreach ($videos as $video) {
|
||||||
|
$new_id = $this->parser->get_attachment_id_from_tag('mp4', $video);
|
||||||
|
if ($new_id) {
|
||||||
|
$video_ids[] = $new_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Parse video tags based on wordpress shortcode for local embedds
|
||||||
|
foreach ($video_ids as $attachment_id) {
|
||||||
|
$meta = wp_get_attachment_metadata($attachment_id);
|
||||||
|
|
||||||
|
if (!$this->is_published($meta)) {
|
||||||
|
$all_media[] = new LBRY_Speech_Media($attachment_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $all_media;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks meta to see if a spee.ch url exists
|
||||||
|
* @param array $meta An array of meta which would possibly contain a speech_asset_url
|
||||||
|
* @return boolean Whether or not its published to speech
|
||||||
|
*/
|
||||||
|
public function is_published($meta)
|
||||||
|
{
|
||||||
|
if (key_exists('speech_asset_url', $meta) && $meta['speech_asset_url'] !== '') {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds a cURL request to the Speech URL
|
||||||
|
* @param string $method The method to call on the Speech API
|
||||||
|
* @param array $params The Parameters to send the Speech API Call
|
||||||
|
* @return string The cURL object pointer
|
||||||
|
*/
|
||||||
|
private function build_request($method, $params = array())
|
||||||
|
{
|
||||||
|
$speech_url = get_option(LBRY_SETTINGS)[LBRY_SPEECH];
|
||||||
|
|
||||||
|
// Die if no URL
|
||||||
|
if (!$speech_url) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$address = $speech_url . '/api/claim/' . $method;
|
||||||
|
|
||||||
|
// Send it via curl
|
||||||
|
$ch = curl_init();
|
||||||
|
curl_setopt($ch, CURLOPT_URL, $address);
|
||||||
|
curl_setopt($ch, CURLOPT_POST, true);
|
||||||
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
|
||||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
|
curl_setopt($ch, CURLOPT_AUTOREFERER, false);
|
||||||
|
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
|
||||||
|
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
|
||||||
|
curl_setopt($ch, CURLOPT_HEADER, false);
|
||||||
|
|
||||||
|
return $ch;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
79
classes/LBRY_Speech_Media.php
Normal file
79
classes/LBRY_Speech_Media.php
Normal file
|
@ -0,0 +1,79 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Data container for necessary Speech api media uploaduploads
|
||||||
|
*
|
||||||
|
* Visit https://github.com/lbryio/spee.ch for more info
|
||||||
|
*
|
||||||
|
* @package LBRYPress
|
||||||
|
*/
|
||||||
|
|
||||||
|
class LBRY_Speech_Media
|
||||||
|
{
|
||||||
|
public $id;
|
||||||
|
|
||||||
|
public $name;
|
||||||
|
|
||||||
|
public $file;
|
||||||
|
|
||||||
|
public $type;
|
||||||
|
|
||||||
|
public $nsfw;
|
||||||
|
|
||||||
|
public $license;
|
||||||
|
|
||||||
|
public $title;
|
||||||
|
|
||||||
|
public $description;
|
||||||
|
|
||||||
|
public $thumbnail;
|
||||||
|
|
||||||
|
public $image_size = false;
|
||||||
|
|
||||||
|
public function __construct(int $attachment_id, $args = array())
|
||||||
|
{
|
||||||
|
|
||||||
|
// Set supplied arguments
|
||||||
|
$default = array(
|
||||||
|
'nsfw' => null,
|
||||||
|
'license' => null,
|
||||||
|
'description' => null,
|
||||||
|
'thumbnail' => null,
|
||||||
|
'image_size' => false,
|
||||||
|
);
|
||||||
|
|
||||||
|
$settings = array_merge($default, array_intersect_key($args, $default));
|
||||||
|
|
||||||
|
foreach ($settings as $key => $value) {
|
||||||
|
$this->{$key} = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Get attachment ID, name, file, and type from the URL
|
||||||
|
$this->id = $attachment_id;
|
||||||
|
|
||||||
|
|
||||||
|
$path = get_attached_file($this->id);
|
||||||
|
|
||||||
|
// Apply data dependent on whether this is an image 'size' or not
|
||||||
|
if ($this->image_size) {
|
||||||
|
$meta = wp_get_attachment_metadata($this->id)['sizes'][$this->image_size];
|
||||||
|
$pathinfo = pathinfo($meta['file']);
|
||||||
|
$ext = '.' . $pathinfo['extension'];
|
||||||
|
$new_ext = '-' . $meta['width'] . 'x' . $meta['height'] . $ext;
|
||||||
|
$path = str_replace($ext, $new_ext, $path);
|
||||||
|
$filename = $pathinfo['basename'];
|
||||||
|
// COMBAK: Probably wont need this underscore check with Daemon V3
|
||||||
|
$this->name = str_replace('_', '-', $pathinfo['filename']);
|
||||||
|
$this->type = $meta['mime-type'];
|
||||||
|
$this->title = $pathinfo['filename'];
|
||||||
|
} else {
|
||||||
|
$attachment = get_post($this->id);
|
||||||
|
$filename = wp_basename($path);
|
||||||
|
$this->name = str_replace('_', '-', $attachment->post_name);
|
||||||
|
$this->type = $attachment->post_mime_type;
|
||||||
|
$this->title = $attachment->post_title;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->file = new CURLFile($path, $this->type, $filename);
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,13 +7,308 @@
|
||||||
|
|
||||||
class LBRY_Speech_Parser
|
class LBRY_Speech_Parser
|
||||||
{
|
{
|
||||||
public function rewrite($html)
|
/**
|
||||||
|
* Replace img srcset attributes with Spee.ch urls
|
||||||
|
* Check https://developer.wordpress.org/reference/functions/wp_calculate_image_srcset/ hook for details
|
||||||
|
*/
|
||||||
|
public function replace_image_srcset($sources, $size_array, $image_src, $image_meta, $attachment_id)
|
||||||
{
|
{
|
||||||
// TODO: Completely fix this, as its super slow. Looking at cdn_enabler for ideas
|
// $time_start = microtime(true);
|
||||||
$speech_url = get_option(LBRY_SETTINGS)[LBRY_SPEECH];
|
|
||||||
|
|
||||||
$html = str_replace(site_url(), $speech_url, $html);
|
$new_sources = $sources;
|
||||||
|
$sizes = $image_meta['sizes'];
|
||||||
|
|
||||||
return $html;
|
foreach ($sources as $width => $source) {
|
||||||
|
$speech_url = $this->find_speech_url_by_width($sizes, $width);
|
||||||
|
|
||||||
|
if ($speech_url) {
|
||||||
|
$new_sources[$width]['url'] = $speech_url;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// $time_end = microtime(true);
|
||||||
|
// $time = ($time_end - $time_start) * 1000;
|
||||||
|
// error_log("srcset in $time milliseconds");
|
||||||
|
return $new_sources;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replaces the output of the 'wp_get_attachment_image_src' function
|
||||||
|
* Check https://developer.wordpress.org/reference/functions/wp_get_attachment_image_src/ for details
|
||||||
|
*/
|
||||||
|
public function replace_attachment_image_src($image, $attachment_id, $size)
|
||||||
|
{
|
||||||
|
// $time_start = microtime(true);
|
||||||
|
if (!$image) {
|
||||||
|
return $image;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Die if we don't have a speech_asset_url
|
||||||
|
$image_meta = wp_get_attachment_metadata($attachment_id);
|
||||||
|
if (!LBRY()->speech->is_published($image_meta)) {
|
||||||
|
return $image;
|
||||||
|
}
|
||||||
|
|
||||||
|
$new_image = $image;
|
||||||
|
$sizes = $image_meta['sizes'];
|
||||||
|
|
||||||
|
// If we have a given size, then use that immediately
|
||||||
|
if (is_string($size)) {
|
||||||
|
switch ($size) {
|
||||||
|
case 'full':
|
||||||
|
$new_image[0] = $image_meta['speech_asset_url'];
|
||||||
|
break;
|
||||||
|
case 'post-thumbnail':
|
||||||
|
if (LBRY()->speech->is_published($sizes['thumbnail'])) {
|
||||||
|
$new_image[0] = $sizes['thumbnail']['speech_asset_url'];
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
if (key_exists($size, $sizes) && LBRY()->speech->is_published($sizes[$size])) {
|
||||||
|
$new_image[0] = $sizes[$size]['speech_asset_url'];
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// $time_end = microtime(true);
|
||||||
|
// $time = ($time_end - $time_start) * 1000;
|
||||||
|
// error_log("attachment image source in $time milliseconds");
|
||||||
|
return $new_image;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise, we can find it by the url provided
|
||||||
|
$speech_url = $this->find_speech_url_by_file_url($image_meta, $image[0]);
|
||||||
|
if ($speech_url) {
|
||||||
|
$new_image[0] = $speech_url;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $new_image;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scrape content for urls that have a speech equivalent and replace
|
||||||
|
* @param string $content The content to scrape
|
||||||
|
* @return string The content with Spee.ch urls replaced
|
||||||
|
*/
|
||||||
|
// COMBAK: Need to make this a bit faster. Sitting between 100 and 300ms currently
|
||||||
|
public function replace_urls_with_speech($content)
|
||||||
|
{
|
||||||
|
// $time_start = microtime(true);
|
||||||
|
$new_content = $content;
|
||||||
|
|
||||||
|
$assets = array();
|
||||||
|
|
||||||
|
// Get Images
|
||||||
|
$images = $this->scrape_images($content);
|
||||||
|
foreach ($images as $image) {
|
||||||
|
if ($src = $this->get_src_from_image_tag($image)) {
|
||||||
|
$assets[] = $src;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get Videos
|
||||||
|
$videos = $this->scrape_videos($content);
|
||||||
|
foreach ($videos as $video) {
|
||||||
|
if ($src = $this->get_src_from_video_tag($video)) {
|
||||||
|
$assets[] = $src;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Replace with Speech Urls
|
||||||
|
foreach ($assets as $asset) {
|
||||||
|
$id = $this->rigid_attachment_url_to_postid($asset);
|
||||||
|
$meta = wp_get_attachment_metadata($id);
|
||||||
|
|
||||||
|
if ($meta && LBRY()->speech->is_published($meta) && $speech_url = $this->find_speech_url_by_file_url($meta, $asset)) {
|
||||||
|
$new_content = str_replace($asset, $speech_url, $new_content);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// $time_end = microtime(true);
|
||||||
|
// $time = ($time_end - $time_start) * 1000;
|
||||||
|
// error_log("replace content urls in $time milliseconds");
|
||||||
|
return $new_content;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scrapes all image tags from content
|
||||||
|
* @param string $content The content to scrape
|
||||||
|
* @return array Array of image tags, empty if none found
|
||||||
|
*/
|
||||||
|
public function scrape_images($content)
|
||||||
|
{
|
||||||
|
// Find all images
|
||||||
|
preg_match_all('/<img [^>]+>/', $content, $images);
|
||||||
|
|
||||||
|
// Check to make sure we have results
|
||||||
|
$images = empty($images[0]) ? array() : $images[0];
|
||||||
|
|
||||||
|
return array_unique($images);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scrapes all video shortcodes from content
|
||||||
|
* @param string $content The content to scrape
|
||||||
|
* @return array Array of video tags, empty if none found
|
||||||
|
*/
|
||||||
|
public function scrape_videos($content)
|
||||||
|
{
|
||||||
|
// Only MP4 videos for now
|
||||||
|
preg_match_all('/\[video.*mp4=".*".*\]/', $content, $videos);
|
||||||
|
$videos = empty($videos[0]) ? array() : $videos[0];
|
||||||
|
|
||||||
|
return array_unique($videos);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrives an attachment ID via an html tag
|
||||||
|
* @param string $type Can be 'image' or 'mp4'
|
||||||
|
* @param string $tag The Tag / shortcode to scrub for an ID
|
||||||
|
* @return int|bool An ID if one is found, false otherwise
|
||||||
|
*/
|
||||||
|
public function get_attachment_id_from_tag($type, $tag)
|
||||||
|
{
|
||||||
|
$attachment_id = false;
|
||||||
|
switch ($type) {
|
||||||
|
case 'image':
|
||||||
|
// Looks for wp image class first, if not, pull id from source
|
||||||
|
if (preg_match('/wp-image-([0-9]+)/i', $tag, $class_id)) {
|
||||||
|
$attachment_id = absint($class_id[1]);
|
||||||
|
} elseif ($src = $this->get_src_from_image_tag($tag)) {
|
||||||
|
$attachment_id = $this->rigid_attachment_url_to_postid($src);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'mp4':
|
||||||
|
if ($src = $this->get_src_from_video_tag($tag)) {
|
||||||
|
$attachment_id = $this->rigid_attachment_url_to_postid($src);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return $attachment_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pulls src from image tag
|
||||||
|
* @param string $image HTML Image tag
|
||||||
|
* @return string The source if found and is a local source, otherwise false
|
||||||
|
*/
|
||||||
|
private function get_src_from_image_tag($image)
|
||||||
|
{
|
||||||
|
if (preg_match('/src="((?:https?:)?\/\/[^"]+)"/', $image, $src) && $src[1]) {
|
||||||
|
if ($this->is_local($src[1])) {
|
||||||
|
return $src[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pulls src from video tag
|
||||||
|
* @param string $video The video shortcode
|
||||||
|
* @return string The source if found and is a local source, otherwise false
|
||||||
|
*/
|
||||||
|
private function get_src_from_video_tag($video)
|
||||||
|
{
|
||||||
|
if (preg_match('/mp4="((?:https?:)?\/\/[^"]+)"/', $video, $src) && $src[1]) {
|
||||||
|
if ($this->is_local($src[1])) {
|
||||||
|
return $src[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a speech_asset_url based sizes meta provided
|
||||||
|
* @param array $sizes Image sizes meta array
|
||||||
|
* @param string $width The width to look for
|
||||||
|
* @return string An asset url if found, false if not
|
||||||
|
*/
|
||||||
|
private function find_speech_url_by_width($sizes, $width)
|
||||||
|
{
|
||||||
|
foreach ($sizes as $key => $size) {
|
||||||
|
if ($size['width'] == $width && key_exists('speech_asset_url', $size)) {
|
||||||
|
return $size['speech_asset_url'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a speech_asset_url based on a passed url
|
||||||
|
* @param array $meta The attachment meta data for the asset
|
||||||
|
* @param string $url The URL of the asset being exchanged
|
||||||
|
* @return string The URL of the Speech Asset
|
||||||
|
*/
|
||||||
|
private function find_speech_url_by_file_url($meta, $url)
|
||||||
|
{
|
||||||
|
// See if this looks like video meta
|
||||||
|
if (!key_exists('file', $meta) && key_exists('mime_type', $meta) && $meta['mime_type'] == 'video/mp4') {
|
||||||
|
return $meta['speech_asset_url'];
|
||||||
|
}
|
||||||
|
|
||||||
|
$pathinfo = pathinfo($url);
|
||||||
|
$basename = $pathinfo['basename'];
|
||||||
|
|
||||||
|
// Check main file or if $meta is just a url (video) first
|
||||||
|
if (key_exists('file', $meta) && $basename == wp_basename($meta['file'])) {
|
||||||
|
return $meta['speech_asset_url'];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check to see if we have a meta option here
|
||||||
|
if (key_exists('sizes', $meta)) {
|
||||||
|
// Then check sizes
|
||||||
|
foreach ($meta['sizes'] as $size => $meta) {
|
||||||
|
if ($basename == $meta['file'] && key_exists('speech_asset_url', $meta)) {
|
||||||
|
return $meta['speech_asset_url'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Couldn't make a match
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks to see if a url is local to this installation
|
||||||
|
* @param string $url
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
private function is_local($url)
|
||||||
|
{
|
||||||
|
if (strpos($url, home_url()) !== false) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks for image crop sizes and filters out query params
|
||||||
|
* Courtesy of this post: http://bordoni.me/get-attachment-id-by-image-url/
|
||||||
|
* @param string $url The url of the attachment you want an ID for
|
||||||
|
* @return int The found post_id
|
||||||
|
*/
|
||||||
|
private function rigid_attachment_url_to_postid($url)
|
||||||
|
{
|
||||||
|
$scrubbed_url = strtok($url, '?'); // Clean up query params first
|
||||||
|
$post_id = attachment_url_to_postid($scrubbed_url);
|
||||||
|
|
||||||
|
if (! $post_id) {
|
||||||
|
$dir = wp_upload_dir();
|
||||||
|
$path = $scrubbed_url;
|
||||||
|
|
||||||
|
if (0 === strpos($path, $dir['baseurl'] . '/')) {
|
||||||
|
$path = substr($path, strlen($dir['baseurl'] . '/'));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (preg_match('/^(.*)(\-\d*x\d*)(\.\w{1,})/i', $path, $matches)) {
|
||||||
|
$url = $dir['baseurl'] . '/' . $matches[1] . $matches[3];
|
||||||
|
$post_id = attachment_url_to_postid($url);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (int) $post_id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -108,7 +108,7 @@ class LBRYPress
|
||||||
'license2' => 'License 2',
|
'license2' => 'License 2',
|
||||||
'license3' => 'License 3'
|
'license3' => 'License 3'
|
||||||
));
|
));
|
||||||
$this->define('LBRY_MIN_BALANCE', 20);
|
$this->define('LBRY_MIN_BALANCE', 2000);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -138,7 +138,7 @@ class LBRYPress
|
||||||
$this->notice = new LBRY_Admin_Notice();
|
$this->notice = new LBRY_Admin_Notice();
|
||||||
$this->network = new LBRY_Network();
|
$this->network = new LBRY_Network();
|
||||||
} else {
|
} else {
|
||||||
$this->speech->maybe_rewrite_urls();
|
// $this->speech->maybe_rewrite_urls();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
4
lbry_config.example.php
Normal file
4
lbry_config.example.php
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
<?php
|
||||||
|
// Define Speech Channel and password
|
||||||
|
// define('LBRY_SPEECH_CHANNEL', '');
|
||||||
|
// define('LBRY_SPEECH_CHANNEL_PASSWORD', '');
|
|
@ -70,6 +70,9 @@ function LBRY()
|
||||||
if (! class_exists('LBRYPress')) {
|
if (! class_exists('LBRYPress')) {
|
||||||
require_once(dirname(__FILE__) . '/classes/LBRYPress.php');
|
require_once(dirname(__FILE__) . '/classes/LBRYPress.php');
|
||||||
}
|
}
|
||||||
|
// Bring in configuration requirements
|
||||||
|
// HACK: Will probably be getting rid of configuration once we sort out Spee.ch Implementation
|
||||||
|
require_once(dirname(__FILE__) . '/lbry_config.php');
|
||||||
return LBRYPress::instance();
|
return LBRYPress::instance();
|
||||||
} else {
|
} else {
|
||||||
add_action('admin_notices', 'lbry_requirements_error');
|
add_action('admin_notices', 'lbry_requirements_error');
|
||||||
|
|
0
logs/.gitkeep
Normal file
0
logs/.gitkeep
Normal file
Loading…
Reference in a new issue