public function update(SS_HTTPRequest $request) { if (!SecurityToken::inst()->checkRequest($request)) { return ''; } $url = $request->postVar('URL'); if (strlen($url)) { $info = Oembed::get_oembed_from_url($url); $info = Embed\Embed::create($url); if ($info) { $object = EmbeddedObject::create(); $object->setFromEmbed($info); $this->object = $object; // needed to make sure the check in FieldHolder works out $object->ID = -1; return $this->FieldHolder(); } else { $this->message = _t('EmbeddedObjectField.ERROR', 'Could not look up provided URL: ' . Convert::raw2xml($url)); return $this->FieldHolder(); } } else { $this->object = null; return $this->FieldHolder(); } }
public function update(SS_HTTPRequest $request) { if (!SecurityToken::inst()->checkRequest($request)) { return ''; } $url = $request->postVar('URL'); if (strlen($url)) { $info = Oembed::get_oembed_from_url($url); if ($info && $info->exists()) { $object = EmbeddedObject::create(); $object->Title = $info->title; $object->SourceURL = $url; $object->Width = $info->width; $object->Height = $info->height; $object->ThumbURL = $info->thumbnail_url; $object->Description = $info->description ? $info->description : $info->title; $object->Type = $info->type; $object->EmbedHTML = $info->forTemplate(); $this->object = $object; // needed to make sure the check in FieldHolder works out $object->ID = -1; return $this->FieldHolder(); } else { $this->message = _t('EmbeddedObjectField.ERROR', 'Could not look up provided URL: ' . Convert::raw2xml($url)); return $this->FieldHolder(); } } else { $this->object = null; return $this->FieldHolder(); } }
public function testRequestProtocolReflectedInGetOembedFromUrl() { Config::inst()->update('Oembed', 'providers', array('http://*.silverstripe.com/watch*' => array('http' => 'http://www.silverstripe.com/oembed/', 'https' => 'https://www.silverstripe.com/oembed/?scheme=https'), 'https://*.silverstripe.com/watch*' => array('http' => 'http://www.silverstripe.com/oembed/', 'https' => 'https://www.silverstripe.com/oembed/?scheme=https'))); Config::inst()->update('Director', 'alternate_protocol', 'http'); foreach (array('http', 'https') as $protocol) { $url = $protocol . '://www.silverstripe.com/watch12345'; $result = Oembed::get_oembed_from_url($url); $this->assertInstanceOf('Oembed_Result', $result); $this->assertEquals($result->getOembedURL(), 'http://www.silverstripe.com/oembed/?format=json&url=' . urlencode($url), 'Returns http based URLs when request is over http, regardless of source URL'); } Config::inst()->update('Director', 'alternate_protocol', 'https'); foreach (array('http', 'https') as $protocol) { $url = $protocol . '://www.silverstripe.com/watch12345'; $result = Oembed::get_oembed_from_url($url); $this->assertInstanceOf('Oembed_Result', $result); $this->assertEquals($result->getOembedURL(), 'https://www.silverstripe.com/oembed/?scheme=https&format=json&url=' . urlencode($url), 'Returns https based URLs when request is over https, regardless of source URL'); } Config::inst()->update('Director', 'alternate_protocol', 'foo'); foreach (array('http', 'https') as $protocol) { $url = $protocol . '://www.silverstripe.com/watch12345'; $result = Oembed::get_oembed_from_url($url); $this->assertInstanceOf('Oembed_Result', $result); $this->assertEquals($result->getOembedURL(), 'http://www.silverstripe.com/oembed/?format=json&url=' . urlencode($url), 'When request protocol doesn\'t have specific handler, fall back to first option'); } }
/** * Convert a single URL, assumes $url has been verified to be * a real URL * * @param string $url */ public function convertUrl($url) { $oembed = Oembed::get_oembed_from_url($url, false, $this->oembedOptions); if ($oembed) { return array('Title' => '', 'Content' => $oembed->forTemplate()); } $graph = OpenGraph::fetch($url); if ($graph) { foreach ($graph as $key => $value) { $data[$key] = Varchar::create_field('Varchar', $value); } if (isset($data['url'])) { return array('Title' => $graph->Title, 'Content' => MicroPost::create()->customise($data)->renderWith('OpenGraphPost')); } } // get the post and take its <title> tag at the very least $service = new RestfulService($url); $response = $service->request(); if ($response && $response->getStatusCode() == 200) { if (preg_match('/<title>(.*?)<\\/title>/is', $response->getBody(), $matches)) { $title = Convert::raw2xml(trim($matches[1])); return array('Title' => $title, 'Content' => "<a href='{$url}'>{$title}</a>"); } } }
public function updateEmbedHTML() { $options = array('width' => $this->Width, 'height' => $this->Height); $info = Oembed::get_oembed_from_url($this->SourceURL, false, $options); if ($info && $info->exists()) { $this->EmbedHTML = $info->forTemplate(); } }
public function GetOembedData(SS_HTTPRequest $request) { $response = "{}"; $this->getResponse()->addHeader("Content-Type", "application/json; charset=utf-8"); $url = $request->postVar('url') ? $request->postVar('url') : $request->getVar("mediaurl"); if (Director::is_site_url($url) && VideoEmbed::GetByURL($url)) { $video = VideoEmbed::GetByURL($url); $response = $video->GetOembedJson(); } else { $oembed = Oembed::get_oembed_from_url($url); if ($oembed && $oembed->exists()) { $response = $oembed->toJson(); } } echo $response; }
function testGetOembedFromUrl() { Config::inst()->update('Oembed', 'providers', array('http://*.silverstripe.com/watch*' => 'http://www.silverstripe.com/oembed/')); $escapedEndpointURL = urlencode("http://www.silverstripe.com/oembed/"); // Test with valid URL $result = Oembed::get_oembed_from_url('http://www.silverstripe.com/watch12345'); $this->assertTrue($result != false); $this->assertEquals($result->getOembedURL(), 'http://www.silverstripe.com/oembed/?format=json&url=' . urlencode('http://www.silverstripe.com/watch12345'), 'Triggers on matching URL'); // Test without www. $result = Oembed::get_oembed_from_url('http://silverstripe.com/watch12345'); $this->assertTrue($result != false); $this->assertEquals($result->getOembedURL(), 'http://www.silverstripe.com/oembed/?format=json&url=' . urlencode('http://silverstripe.com/watch12345'), 'Triggers on matching URL without www'); // Test if options make their way to the URL $result = Oembed::get_oembed_from_url('http://www.silverstripe.com/watch12345', false, array('foo' => 'bar')); $this->assertTrue($result != false); $this->assertEquals($result->getOembedURL(), 'http://www.silverstripe.com/oembed/?format=json&url=' . urlencode('http://www.silverstripe.com/watch12345') . '&foo=bar', 'Includes options'); // Test magic. $result = Oembed::get_oembed_from_url('http://www.silverstripe.com/watch12345', false, array('height' => 'foo', 'width' => 'bar')); $this->assertTrue($result != false); $urlParts = parse_url($result->getOembedURL()); parse_str($urlParts['query'], $query); $this->assertEquals($query['maxheight'], 'foo', 'Magically creates maxheight option'); $this->assertEquals($query['maxwidth'], 'bar', 'Magically creates maxwidth option'); }
public function __construct($url, File $file = null) { parent::__construct($url, $file); $this->oembed = Oembed::get_oembed_from_url($url); if (!$this->oembed) { $controller = Controller::curr(); $response = $controller->getResponse(); $response->addHeader('X-Status', rawurlencode(_t('HtmlEditorField.URLNOTANOEMBEDRESOURCE', "The URL '{url}' could not be turned into a media resource.", "The given URL is not a valid Oembed resource; the embed element couldn't be created.", array('url' => $url)))); $response->setStatusCode(404); throw new SS_HTTPResponse_Exception($response); } }
/** * Get the embedded media * @return false|Oembed_Result */ public function Media() { if ($this->ExternalMedia) { return Oembed::get_oembed_from_url($this->ExternalMedia, false, array('color' => Config::inst()->get('VideoBlock', 'player_color'), 'title' => false, 'portrait' => false, 'byline' => false, 'loop' => false)); } return false; }
protected function setFromEmbed(&$post) { if (!isset($post['Link'])) { return; } $cacheKey = $this->getCacheKey(['embed' => $post['Link']]); if (!($record = unserialize($this->cache()->load($cacheKey)))) { $record = []; if (class_exists('Embed\\Embed')) { $info = \Embed\Embed::create($post['Link']); $record['ObjectName'] = $info->getTitle(); $record['ObjectURL'] = $info->getUrl(); $record['ObjectWidth'] = $info->getWidth(); $record['ObjectHeight'] = $info->getHeight(); $record['ObjectThumbnail'] = $info->getImage(); $record['ObjectDescription'] = $info->getDescription(); $record['ObjectType'] = $info->getType(); $record['ObjectEmbed'] = $info->getCode(); } else { if ($info = \Oembed::get_oembed_from_url($post['Link'])) { if ($info->hasField('title')) { $record['ObjectName'] = $info->getField('title'); } if ($info->hasField('url')) { $record['ObjectURL'] = $info->getField('url'); } if ($info->hasField('width')) { $record['ObjectWidth'] = $info->getField('width'); } if ($info->hasField('height')) { $record['ObjectHeight'] = $info->getField('height'); } if ($info->hasField('thumbnail')) { $record['ObjectThumbnail'] = $info->getField('thumbnail'); } if ($info->hasField('description')) { $record['ObjectDescription'] = $this->textParser()->text($info->getField('description')); } if ($info->hasField('type')) { $record['ObjectType'] = $info->getField('type'); } $record['ObjectEmbed'] = $info->forTemplate(); } } $this->cache()->save(serialize($record), $cacheKey); } foreach ($record as $key => $value) { $post[$key] = $value; } }
/** * * @returns Oembed_Result/bool An Oembed descriptor, or false */ public function GetOembed() { $res = false; if ($this->Type !== 'HTML 5/Flash' && $this->GetUrl()) { $options = array(); if ($this->getWidth()) { $options['width'] = $this->getWidth(); } if ($this->getHeight()) { $options['height'] = $this->getHeight(); } $options['autoplay'] = $this->getAutoPlay() ? 1 : 0; $res = Oembed::get_oembed_from_url($this->GetSettings()->url, false, $options); } else { $res = new Oembed_Result($this->GetUrl()); $refObject = new ReflectionObject($res); $refProperty = $refObject->getProperty('data'); $refProperty->setAccessible(true); $refProperty->setValue($res, Convert::json2array($this->GetOembedJson())); // } } return $res; }