2018-09-01 01:50:29 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Connects to an spee.ch style server to host assets via the LBRY Protocol
|
|
|
|
*
|
|
|
|
* Visit https://github.com/lbryio/spee.ch for more info
|
|
|
|
*
|
|
|
|
* @package LBRYPress
|
|
|
|
*/
|
|
|
|
|
2018-09-01 03:14:12 +02:00
|
|
|
class LBRY_Speech
|
2018-09-01 01:50:29 +02:00
|
|
|
{
|
2018-09-11 22:10:15 +02:00
|
|
|
/**
|
2018-09-14 02:34:11 +02:00
|
|
|
* HTML Parser
|
|
|
|
* @var LBRY_Speech_Parser
|
2018-09-11 22:10:15 +02:00
|
|
|
*/
|
2018-09-14 02:34:11 +02:00
|
|
|
private $parser = null;
|
2018-09-01 03:14:12 +02:00
|
|
|
|
2018-09-14 02:34:11 +02:00
|
|
|
public function __construct()
|
2018-09-01 03:14:12 +02:00
|
|
|
{
|
2018-09-14 02:34:11 +02:00
|
|
|
$this->parser = new LBRY_Speech_Parser();
|
2018-10-26 18:46:28 +02:00
|
|
|
add_action('save_post', array($this, 'upload_media'), 10, 2);
|
2018-09-01 03:14:12 +02:00
|
|
|
}
|
|
|
|
|
2018-09-14 02:34:11 +02:00
|
|
|
/**
|
|
|
|
* Checks to see if we need to rewrite URLS, does if necessary
|
|
|
|
*/
|
2018-10-26 01:30:24 +02:00
|
|
|
|
2018-09-14 02:34:11 +02:00
|
|
|
public function maybe_rewrite_urls()
|
2018-09-01 03:14:12 +02:00
|
|
|
{
|
2018-09-14 02:34:11 +02:00
|
|
|
// See if we have a Spee.ch URL and if we are on the front-end
|
|
|
|
$speech_url = get_option(LBRY_SETTINGS)[LBRY_SPEECH];
|
|
|
|
if ($speech_url != '' && !is_admin()) {
|
|
|
|
ob_start(array($this->parser, 'rewrite'));
|
|
|
|
}
|
2018-09-01 03:14:12 +02:00
|
|
|
}
|
2018-09-14 02:42:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Uploads assets to the speech server
|
2018-10-26 01:30:24 +02:00
|
|
|
* @param int $post_id The ID of the post to
|
|
|
|
* @return bool True if successful, false if not or if no Speech URL available
|
2018-09-14 02:42:38 +02:00
|
|
|
*/
|
2018-10-26 01:30:24 +02:00
|
|
|
// TODO: set up error reporting
|
2018-10-26 18:46:28 +02:00
|
|
|
public function upload_media($post_id, $post)
|
2018-09-14 02:42:38 +02:00
|
|
|
{
|
2018-10-26 18:46:28 +02:00
|
|
|
// Only check post_type of Post
|
|
|
|
if ('post' !== $post->post_type) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-14 02:42:38 +02:00
|
|
|
$speech_url = get_option(LBRY_SETTINGS)[LBRY_SPEECH];
|
|
|
|
|
|
|
|
// Die if we don't have a spee.ch url
|
|
|
|
if (!$speech_url || $speech_url === '') {
|
2018-10-26 01:30:24 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-26 08:34:13 +02:00
|
|
|
$all_media = $this->find_media($post_id);
|
2018-10-26 01:30:24 +02:00
|
|
|
|
2018-10-26 02:59:34 +02:00
|
|
|
// IDEA: Notify user if post save time will take a while, may be a concern for request timeouts
|
2018-10-26 08:34:13 +02:00
|
|
|
if ($all_media) {
|
|
|
|
foreach ($all_media as $media) {
|
2018-10-26 18:46:28 +02:00
|
|
|
if (! get_post_meta($media->id, 'lbry_speech_uploaded')) {
|
|
|
|
$params = array(
|
|
|
|
'name' => $media->name,
|
|
|
|
'file' => $media->file,
|
|
|
|
'title' => $media->title,
|
|
|
|
'type' => $media->type
|
|
|
|
);
|
|
|
|
|
|
|
|
if (LBRY_SPEECH_CHANNEL && LBRY_SPEECH_CHANNEL_PASSWORD) {
|
|
|
|
$params['channelName'] = LBRY_SPEECH_CHANNEL;
|
|
|
|
$params['channelPassword'] = LBRY_SPEECH_CHANNEL_PASSWORD;
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = $this->request('publish', $params);
|
|
|
|
error_log(print_r($result, true));
|
|
|
|
|
|
|
|
if ($result->success) {
|
|
|
|
update_post_meta($media->id, 'lbry_speech_uploaded', true);
|
|
|
|
update_post_meta($media->id, 'lbry_speech_url', $result->data->serveUrl);
|
|
|
|
}
|
2018-10-26 18:13:24 +02:00
|
|
|
}
|
|
|
|
|
2018-10-26 02:59:34 +02:00
|
|
|
// TODO: Make sure to warn if image name is already taken on channel
|
2018-10-26 01:30:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds all media attached to a post
|
|
|
|
* @param int $post_id The post to search
|
2018-10-26 08:34:13 +02:00
|
|
|
* @return array An array of Speech Media Objects
|
2018-10-26 01:30:24 +02:00
|
|
|
*/
|
2018-10-26 08:34:13 +02:00
|
|
|
protected function find_media($post_id)
|
2018-10-26 01:30:24 +02:00
|
|
|
{
|
2018-10-26 09:43:34 +02:00
|
|
|
$all_media = array();
|
|
|
|
|
2018-10-26 08:34:13 +02:00
|
|
|
// Get content and put into a DOMDocument
|
|
|
|
$content = apply_filters('the_content', get_post_field('post_content', $post_id));
|
2018-10-26 18:46:28 +02:00
|
|
|
if (!$content) {
|
|
|
|
return $all_media;
|
|
|
|
}
|
2018-10-26 08:34:13 +02:00
|
|
|
$DOM = new DOMDocument();
|
|
|
|
// Hide HTML5 Tag warnings
|
|
|
|
libxml_use_internal_errors(true);
|
|
|
|
$DOM->loadHTML($content);
|
|
|
|
|
|
|
|
$images = $DOM->getElementsByTagName('img');
|
|
|
|
$videos = $DOM->getElementsByTagName('video');
|
|
|
|
|
|
|
|
// Get each image attribute
|
|
|
|
foreach ($images as $image) {
|
2018-10-26 09:43:34 +02:00
|
|
|
$src = $image->getAttribute('src');
|
|
|
|
if ($this->is_local($src)) {
|
|
|
|
$all_media[] = new LBRY_Speech_Media($src);
|
|
|
|
}
|
2018-10-26 01:30:24 +02:00
|
|
|
}
|
2018-10-26 08:34:13 +02:00
|
|
|
|
|
|
|
// Parse video tags based on wordpress output for local embedds
|
|
|
|
// Because video tag is HTML5, treat it like an XML node
|
|
|
|
foreach ($videos as $video) {
|
|
|
|
$source = $video->getElementsByTagName('source');
|
|
|
|
$src = $source[0]->attributes->getNamedItem('src')->value;
|
2018-10-26 09:43:34 +02:00
|
|
|
if ($this->is_local($src)) {
|
|
|
|
$all_media[] = new LBRY_Speech_Media($src);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $all_media;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2018-10-26 08:34:13 +02:00
|
|
|
}
|
|
|
|
|
2018-10-26 09:43:34 +02:00
|
|
|
return false;
|
2018-10-26 01:30:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a cURL request to the Speech URL
|
2018-10-26 02:44:08 +02:00
|
|
|
* @param string $method The method to call on the Speech API
|
|
|
|
* @param array $params The Parameters to send the Speech API Call
|
2018-10-26 01:30:24 +02:00
|
|
|
* @return string The cURL response
|
|
|
|
*/
|
|
|
|
private function request($method, $params = array())
|
|
|
|
{
|
|
|
|
$speech_url = get_option(LBRY_SETTINGS)[LBRY_SPEECH];
|
|
|
|
|
|
|
|
// Die if no URL
|
|
|
|
if (!$speech_url) {
|
2018-09-14 02:42:38 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-26 01:30:24 +02:00
|
|
|
$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);
|
2018-10-24 20:49:46 +02:00
|
|
|
|
2018-10-26 01:30:24 +02:00
|
|
|
$result = curl_exec($ch);
|
|
|
|
curl_close($ch);
|
|
|
|
return json_decode($result);
|
2018-09-14 02:42:38 +02:00
|
|
|
}
|
2018-09-01 01:50:29 +02:00
|
|
|
}
|