b0c245e434
* Nearly 30 services supported * Some output of them fixed using smarty templating * s9y 1.6 and 1.7 compatibility (different smarty versions) * oEmbed result fixing while saving to DB.
115 lines
4.3 KiB
PHP
115 lines
4.3 KiB
PHP
<?php
|
|
class OEmbedProvider extends EmbedProvider{
|
|
private $urlRegExp;
|
|
private $jsonEndpoint;
|
|
private $xmlEndpoint;
|
|
private $dimensionsSupported = true;
|
|
|
|
private $onlyJson = false;
|
|
|
|
public function __construct($url,$endpoint, $onlyJson=false, $maxwidth=null, $maxheight=null, $dimensionsSupported=true){
|
|
parent::__construct($url,$endpoint,$maxwidth,$maxheight);
|
|
$this->onlyJson = $onlyJson;
|
|
$this->dimensionsSupported = $dimensionsSupported;
|
|
$this->urlRegExp=preg_replace(array("/\*/","/\//","/\.\*\./"),array(".*","\/",".*"),$url);
|
|
$this->urlRegExp="/".$this->urlRegExp."/";
|
|
if (preg_match("/\{format\}/",$endpoint)){
|
|
$this->jsonEndpoint=preg_replace("/\{format\}/","json",$endpoint);
|
|
$this->jsonEndpoint.="?url={url}";
|
|
$this->xmlEndpoint=preg_replace("/\{format\}/","xml",$endpoint);
|
|
$this->xmlEndpoint.="?url={url}";
|
|
} else {
|
|
$this->jsonEndpoint=$endpoint."?url={url}&format=json";
|
|
$this->xmlEndpoint=$endpoint."?url={url}&format=xml";
|
|
}
|
|
if ($this->dimensionsSupported) {
|
|
if (!empty($this->maxwidth)) {
|
|
$this->jsonEndpoint.= '&maxwidth=' . $this->maxwidth;
|
|
$this->xmlEndpoint.= '&maxwidth=' . $this->maxwidth;
|
|
}
|
|
if (!empty($this->maxheight)) {
|
|
$this->jsonEndpoint.= '&maxwidth=' . $this->maxheight;
|
|
$this->xmlEndpoint.= '&maxwidth=' . $this->maxheight;
|
|
}
|
|
}
|
|
}
|
|
|
|
public function getUrlRegExp(){ return $this->urlRegExp; }
|
|
public function getJsonEndpoint(){ return $this->jsonEndpoint; }
|
|
public function getXmlEndpoint(){ return $this->xmlEndpoint; }
|
|
|
|
public function match($url){
|
|
return preg_match($this->urlRegExp,$url);
|
|
}
|
|
private function file_get_contents($fileurl) {
|
|
if (defined('OEMBED_USE_CURL') && OEMBED_USE_CURL && defined('CURLOPT_URL')) {
|
|
$ch = curl_init();
|
|
$timeout = 5; // 0 wenn kein Timeout
|
|
curl_setopt($ch, CURLOPT_URL, $fileurl);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
|
|
curl_setopt($ch, CURLOPT_MAXREDIRS, 3 );
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
|
|
$file_content = curl_exec($ch);
|
|
curl_close($ch);
|
|
}
|
|
else {
|
|
$context = array ( 'http' => array ( 'method' => 'GET', 'max_redirects' => 3, ),);
|
|
$file_content = file_get_contents($fileurl, null, stream_context_create($context));
|
|
}
|
|
return $file_content;
|
|
}
|
|
private function provideXML($url){
|
|
return $this->file_get_contents(preg_replace("/\{url\}/",urlencode($url),$this->xmlEndpoint));
|
|
}
|
|
private function getTypeObj($type){
|
|
switch($type){
|
|
case "image":
|
|
case "photo":
|
|
return new PhotoEmbed();
|
|
break;
|
|
case "video":
|
|
return new VideoEmbed();
|
|
break;
|
|
case "link":
|
|
return new LinkEmbed();
|
|
break;
|
|
case "rich":
|
|
return new RichEmbed();
|
|
break;
|
|
default:
|
|
return new OEmbed();
|
|
}
|
|
}
|
|
private function provideObject($url){
|
|
if (!$this->onlyJson) {
|
|
$xml=simplexml_load_string($this->provideXML($url));
|
|
}
|
|
else {
|
|
$data=$this->provide($url);
|
|
if (!empty($data)) $xml = json_decode($data);
|
|
}
|
|
//TODO $xml->type alapjan assigner
|
|
$obj = $this->getTypeObj((string)$xml->type);
|
|
$obj->cloneObj($xml);
|
|
$obj->resource_url=$url;
|
|
return $obj;
|
|
}
|
|
private function provideSerialized($url){
|
|
$serialized=serialize($this->provideObject($url));
|
|
return $serialized;
|
|
}
|
|
public function provide($url,$format="json"){
|
|
if($format=="xml"){
|
|
return $this->provideXML($url);
|
|
} else if ($format=="object"){
|
|
return $this->provideObject($url);
|
|
} else if ($format=="serialized"){
|
|
return $this->provideSerialized($url);
|
|
} else {
|
|
return $this->file_get_contents(preg_replace("/\{url\}/",urlencode($url),$this->jsonEndpoint));
|
|
}
|
|
}
|
|
public function register(){}
|
|
}
|
|
|