WMarkDown/PHP/Modules/WMarkDown.Modules.Multimedia.php

293 lines
15 KiB
PHP
Raw Normal View History

<?php
namespace WMarkDown\Modules;
class Multimedia extends \WMarkDown\Abstracts\Modules{
public function get_next_position($string){
if(
$this->more &&
$this->from < 0 &&
$this->more = preg_match('/^\[\[(images?|slider|videos?|audios?|embed|thumbnails?) ?(([^\]"\'\n\r]+|[\r\n]+|"(([^\\\\"]+|\\\\(.|[\r\n]))*)"|\'(([^\\\\\']+|\\\\(.|[\r\n]))*)\'|\][^\]])+)\]\]/m', $string, $this->matches, PREG_OFFSET_CAPTURE)
)
$this->from = $this->matches[0][1];
}
private static function multimedia_box($html, $multiple){
return '<div class="multimedia-box" data-multiple="' . ($multiple ? "true" : "false") . '">' . $html . '</div>';
}
public function image($data){
if(!$data)
return '';
$images;
$text;
if(is_string($data)){
if(!preg_match('/^(\[(([^\\\\\]]+|\\\\(.|[\r\n]))*)\]|[^ ]+)( (.+))?$/', trim($data), $matches))
return '';
$images = $matches[1][0] == "[" ? json_decode($matches[1], true) : [$matches[1]];
$text = isset($matches[6]) ? $matches[6] : null;
}else{
if(isset($data[0]))
$data = ["images" => $data];
$images = $data["images"];
$text = isset($data["text"]) && $data["text"] ? $data["text"] : null;
};
$html = '';
$image_text = $text ? $text : "image";
foreach($images as $image)
$html .= '<img src="' . $image . '" alt="' . $image_text . '" />';
return ('
<span data-type="image"' . ($text !== null ? ' title="' . $text . '"' : '') . ' data-status="unloaded" data-error-type="ok" onclick="wmarkdown.multimedia.go(this, event);">
<img data-src="' . base64_encode(json_encode($images)) . '" data-i="0"' . ($text !== null ? ' alt="' . $text . '"' : '') . ' onerror="wmarkdown.multimedia.image_try_next(this, event);" />
<span class="image-style"></span>
<noscript>' . $html . '</noscript>
<a href="#" target="_blank"></a>
' . ($text !== null ? '<span class="text">' . $text . '</span>' : '') . '
</span>
');
}
public function images($data){
return preg_replace_callback('/^([\s\t]+)?(\[(([^\\\\\[\]"\'\r\n]+|"(([^\\\\"\r\n]+|\\\\(.|[\r\n]))*)"|\'(([^\\\\\'\r\n]+|\\\\(.|[\r\n]))*)\'|\\\\(.|[\n\r])|[\r\n])*)\]|[^\r\n]+)( ([^\r\n]+)?)?[\r\n]?$/m', function($values){
return ($string = trim($values[0])) ? $this->image($string) : "";
}, $data);
}
public function video($data){
if(!$data)
return '';
$videos;
$posters;
$text;
if(is_string($data)){
if(!preg_match('/^(\[(([^\\\\\]]+|\\\\(.|[\r\n]))*)\]|[^ ]+)( (\[(([^\\\\\[]+|\\\\(.|[\r\n]))*)\]|[a-z][a-z\d]{2,}\:\/{2}[^ ]+))?( (.+))?$/im', trim($data), $matches))
return '';
$videos = $matches[1][0] == "[" ? json_decode($matches[1], true) : [$matches[1]];
$posters = isset($matches[6]) && $matches[6] ? ($matches[6][0] == "[" ? json_decode($matches[6], true) : [$matches[6]]) : null;
$text = isset($matches[11]) && $matches[11] ? $matches[11] : null;
}else{
if(isset($data[0]))
$data = ["videos" => $data];
$videos = $data["videos"];
$posters = isset($data["posters"]) ? $data["posters"] : null;
$text = isset($data["text"]) ? $data["text"] : null;
};
$html = '';
$video_text = $text ? $text : "video";
foreach($videos as $video)
$html .= ('
<video title="' . $video_text . '">
<source src="' . $video . '" type="' . $this->wmarkdown->get_mime($video) . '" />
<span data-i18n="multimedia_no_recognized">Formato no reconocido por el navegador.</span>
</video>
');
return ('
<span data-type="video" data-status="unloaded" data-error-type="ok" onclick="wmarkdown.multimedia.go(this, event);">
<video controls data-src="' . base64_encode(json_encode($videos)) . '" data-i="0"' . ($posters ? ' data-posters="' . base64_encode(json_encode($posters)) . '" data-posters-i="0"' : '') . ' onerror="wmarkdown.multimedia.video_try_next(this, event);">
<span data-i18n="multimedia_no_recognized">Formato no reconocido por el navegador.</span>
</video>
<noscript>' . $html . '</noscript>
<a href="#" target="_blank"></a>
' . ($text !== null ? '<span class="text">' . $text . '</span>' : null) . '
</span>
');
}
public function videos($data){
return preg_replace_callback('/^([\s\t]+)?(\[(([^\\\\\[]+|\\\\(.|[\n\r]))*)\]|[^ ]+)( (\[(([^\\\\\[]+|\\\\(.|[\n\r]))*)\]|[a-z][a-z\d]{2,}\:\/{2}[^ ]+))?( (.+)?)?/im', function($values){
return $this->video($values[0]);
}, $data);
}
private function audio_poster($data){
return self::multimedia_box($this->image($data), false);
}
public function audio($data, $multiple = null){
if(!$data)
return '';
$audios;
$posters;
$text;
if(is_string($data)){
if(!preg_match('/^(\[(([^\\\\\]]+|\\\\(.|[\r\n]))*)\]|[^ ]+)( (\[(([^\\\\\[]+|\\\\(.|[\r\n]))*)\]|[a-z][a-z\d]{2,}\:\/{2}[^ ]+))?( (.+))?$/im', trim($data), $matches))
return '';
$audios = $matches[1][0] == "[" ? json_decode($matches[1], true) : [$matches[1]];
$posters = isset($matches[6]) && $matches[6] ? ($matches[6][0] == "[" ? json_decode($matches[6], true) : [$matches[6]]) : null;
$text = isset($matches[11]) && $matches[11] ? $matches[11] : null;
}else{
if(isset($data[0]))
$data = ["audios" => $data];
$audios = $data["audios"];
$posters = isset($data["posters"]) ? $data["posters"] : null;
$text = isset($data["text"]) ? $data["text"] : null;
};
$html = '';
$audio_text = $text ? $text : "audio";
foreach($audios as $audio)
$html .= ('
<audio title="' . $audio_text . '">
<source src="' . $audio . '" type="' . $this->wmarkdown->get_mime($audio) . '" />
<span data-i18n="multimedia_no_recognized">Formato no reconocido por el navegador.</span>
</audio>
');
return ('
<span data-type="audio" data-status="unloaded" data-error-type="ok" onclick="wmarkdown.multimedia.go(this, event);">
<audio controls data-src="' . base64_encode(json_encode($audios)) . '" data-i="0" onerror="wmarkdown.multimedia.audio_try_next(this, event);">
<span data-i18n="multimedia_no_recognized">Formato no reconocido por el navegador.</span>
</audio>
<noscript>' . $html . '</noscript>
<a href="#" target="_blank"></a>
' . (!empty($posters) ? $this->audio_poster([
"images" => $posters,
"text" => $text
], $multiple) : ($text !== null ? '<span class="text">' . $text . '</span>' : null)) . '
</span>
');
}
public function audios($data){
return preg_replace_callback('/^([\s\t]+)?(\[(([^\\\\\[]+|\\\\(.|[\n\r]))*)\]|[^ ]+)( (\[(([^\\\\\[]+|\\\\(.|[\n\r]))*)\]|[a-z][a-z\d]{2,}\:\/{2}[^ ]+))?( (.+)?)?/im', function($values){
return $this->audio($values[0], true);
}, $data);
}
private static function check($matches, $is){
if(!$matches || count($matches) < 2)
return null;
foreach(is_array($is) ? $is : [$is] as $i)
if(isset($matches[$i]) && $matches[$i])
return $i;
return null;
}
public function embed($links){
if(!$links)
return '';
if(!is_array($links))
$links = preg_split('/[\r\n]+/', "" . $links);
$html = '';
$j = 0;
foreach($links as $link){
if(!($link = trim($link)))
continue;
$j ++;
if(preg_match('/^https?\:\/{2}(www\.)?(youtube\.com\/(embed\/|(((?![\?\&]v\=).)+)?[\?\&]v\=)([^\/\&]+)|youtu\.be\/([^\/\&\?]+))/', $link, $matches) && ($i = self::check($matches, [6, 7])))
$html .= ('
<span data-platform="youtube" data-id="' . $matches[$i] . '" data-status="unmounted">
<noscript>
<iframe
src="https://www.youtube.com/embed/' . $matches[$i] . '"
style="position:absolute;top:0em;left:0em;width:100%;height:100%;"
title="YouTube video player"
frameborder="0"
allow="fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen="allowfullscreen"
mozallowfullscreen="mozallowfullscreen"
msallowfullscreen="msallowfullscreen"
oallowfullscreen="oallowfullscreen"
webkitallowfullscreen="webkitallowfullscreen"
allowfullscreen></iframe>
</noscript>
</span>
');
elseif(preg_match('/^https?\:\/{2}((www|player)\.)?vimeo\.com\/(video\/)?(\d+)/', $link, $matches) && ($i = self::check($matches, [4])))
$html .= ('
<span data-platform="vimeo" data-id="' . $matches[$i] . '" data-status="unmounted">
<noscript>
<iframe
src="https://player.vimeo.com/video/' . $matches[$i] . '"
style="position:absolute;top:0em;left:0em;width:100%;height:100%;"
frameborder="0"
allow="fullscreen; picture-in-picture"
allowfullscreen></iframe>
</noscript>
</span>
');
elseif(preg_match('/^https?\:\/{2}(www\.)?facebook\.com\/((?!\/videos\/).)+\/videos\/(\d+)/', $link, $matches) && ($i = self::check($matches, [3])))
$html .= ('
<span data-platform="facebook" data-id="' . $matches[$i] . '" data-status="unmounted">
<noscript>
<iframe
src="https://www.facebook.com/video.php?v=' . $matches[$i] . '"
style="position:absolute;top:0em;left:0em;width:100%;height:100%;border:none;overflow:hidden;"
scrolling="no"
frameborder="0"
allow="autoplay; clipboard-write; encrypted-media; picture-in-picture; web-share"
allowfullscreen></iframe>
</noscript>
</span>
');
elseif(preg_match('/^https?\:\/{2}((www|player)\.)?twitch\.(com|tv)\/(videos\/|\?video\=)(\d+)/', $link, $matches) && ($i = self::check($matches, [5])))
$html .= ('
<span data-platform="twitch" data-id="' . $matches[$i] . '" data-status="unmounted">
<noscript>
<iframe
src="https://player.twitch.tv/?video=' . $matches[$i] . '&parent=wmarkdown.k3y.pw"
style="position:absolute;top:0em;left:0em;width:100%;height:100%;"
scrolling="no"
frameborder="0"
allow="autoplay; clipboard-write; encrypted-media; picture-in-picture; web-share"
allowfullscreen></iframe>
</noscript>
</span>
');
else
$j --;
};
return '<span data-type="embed" data-links-count="' . count($links) . '">' . $html . '</span>';
}
/*public static function process($wmarkdown, $key, $data, $multiple = null){
return '<div class="multimedia-box" data-multiple="' . (($multiple !== null ? $multiple : $key[strlen($key) - 1] == "s") ? "true" : "false") . '">' . (
$data && method_exists('\WMarkDown\Modules\Multimedia', $key) ?
\call_user_func('\WMarkDown\Modules\Multimedia::' . $key, $wmarkdown, $data) :
''
) . '</div>';
}*/
public function thumbnails($data){
// print_r($data);
return "";
}
public function process($string, $modules, $level){
$method = $this->matches[1][0];
// if($method == "images")
// print_r($this->matches);
// $this->html = self::process($this->wmarkdown, $this->matches[1][0], $this->matches[2][0]);
$this->html = self::multimedia_box($this->matches[2][0] && method_exists($this, $method) ? $this->$method($this->matches[2][0]) : '', $method[strlen($method) - 1] == "s");
$this->length = strlen($this->matches[0][0]);
$this->to = $this->from + $this->length;
$this->content = $this->matches[2][0];
}
};