123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- <?php
- /**
- * Service
- *
- * This file is part of Grav MediaEmbed plugin.
- *
- * Dual licensed under the MIT or GPL Version 3 licenses, see LICENSE.
- * http://benjamin-regler.de/license/
- */
- namespace Grav\Plugin\MediaEmbed;
- use Grav\Common\Grav;
- use Grav\Common\GravTrait;
- use RocketTheme\Toolbox\Event\Event;
- /**
- * Service
- */
- class Service
- {
- /**
- * @var Service
- */
- use GravTrait;
- /** ---------------------------
- * Private/protected properties
- * ----------------------------
- */
- /**
- * @var Grav\Plugin\MediaEmbed\ServiceProvider
- */
- protected $services = [];
- /**
- * @var array
- */
- protected $domains;
- /** -------------
- * Public methods
- * --------------
- */
- public function __construct()
- {
- // Fire event
- self::getGrav()->fireEvent('onMediaEmbed', new Event(['service' => $this]));
- }
- public function call($method, $params = [])
- {
- $result = [];
- foreach ($this->services as $key => $service) {
- if (method_exists($service['provider'], $method)) {
- $data = call_user_method_array([$service['provider'], $method], $params);
- if ($data) {
- $result[] = $data;
- }
- }
- }
- return $result;
- }
- public function match($url, $embedCode = null)
- {
- $embed_key = 'embed';
- if ($embedCode && (strtolower($embedCode) !== $embed_key)) {
- return false;
- }
- $parts = $this->parseUrl($url);
- if ($parts['host'] == $embed_key) {
- return false;
- }
- return isset($this->domains[$parts['host']]);
- }
- public function embed($embedCode, $options = [])
- {
- if (!$this->match($embedCode)) {
- throw new \Exception('Unknown embed code "' . htmlspecialchars($embedCode) . '".');
- }
- // Extract domain from embed code
- $domain = strtolower($this->parseUrl($embedCode)['host']);
- $key = $this->domains[$domain];
- // Call OEmbed service provider
- $provider = $this->services[$key]['provider'];
- $provider->init($embedCode, $options);
- // Return initialized provider
- return $provider;
- // // Get type of media
- // $type = ucfirst($provider->type());
- // // Get the default properties of the response class
- // $class = __NAMESPACE__ . "\\Response\\{$type}Response";
- // // Repopulate properties
- // $vars = [
- // 'raw' => $embedCode,
- // 'options' => $provider->attributes(),
- // 'url' => $provider->getEmbedCode(,
- // ] + get_class_vars($class);
- // // Enrich properties with media resource informations
- // foreach ($vars as $key => $value) {
- // if (!$value) {
- // $vars[$key] = $provider->{$key}();
- // }
- // }
- // // Create response
- // $response = new $class($vars);
- // return [$provider, $response];
- // try {
- // // Call ServiceProvider
- // $provider->init($embedCode);
- // $embed += array(
- // // Get unique id of video
- // 'id' => $provider->canonicalize($embedCode),
- // // Templates are stored in the templates/partials folder
- // 'assets' => $provider->getAssets(),
- // 'template' => $provider->getTemplatePaths(),
- // 'variables' => $provider->getTemplateVariables(),
- // // Store embed status of ServiceProvider call
- // 'success' => true,
- // 'message' => '',
- // );
- // } catch (\Exception $e) {
- // $embed['success'] = false;
- // $embed['message'] = $e->getMessage();
- // }
- // }
- // return $embed;
- }
- public function getDomains()
- {
- $allDomains = [];
- foreach ($this->services as $key => $service) {
- $allDomains[] = $service['domains'];
- }
- // Flatten multidimensional array of domains
- $domains = [];
- array_walk($allDomains, function($domain) use (&$domains) {
- $domains[] = $domain;
- });
- // Return unique array of domains
- return array_unique($domains);
- }
- public function getProviders()
- {
- // Get providers sorted by priority
- $classes = $this->collectProviders(function($key, $service) {
- return true;
- }, true, 'class');
- // Replace names with provider classes
- $providers = [];
- foreach ($classes as $class) {
- $name = substr($class, strrpos($class, '\\') + 1);
- $provider = $this->services[$class]['provider'];
- if ( isset($providers[$name]) ) {
- $providers[$name] = array($providers[$name], $provider);
- } else {
- $providers[$name][] = $provider;
- }
- }
- return $providers;
- }
- public function getProviderByName($name, $all = false)
- {
- $providers = $this->collectProviders(
- function($key, $service) use ($name) {
- return preg_match("~^$name$~i", $service['name']);
- }, $all);
- return $providers;
- }
- public function getProviderByDomain($domain, $all = false)
- {
- $providers = $this->collectProviders(
- function($key, $service) use ($domain) {
- return in_array($domain, $service['domains']);
- }, $all);
- return $providers;
- }
- public function register($provider, $priority = 0)
- {
- if ($provider instanceof \Grav\Plugin\MediaEmbed\OEmbed\OEmbed) {
- $key = md5(spl_object_hash($provider));
- $domains = $provider->getDomains();
- $this->services[$key] = array(
- 'priority' => $priority,
- 'domains' => $domains,
- 'provider' => $provider,
- 'name' => $provider->name(),
- 'class' => $key,
- );
- foreach ($domains as $domain) {
- if (!isset($this->domains[$domain]) || ($priority > $this->services[$domain]['priority'])) {
- $this->domains[$domain] = $key;
- }
- }
- }
- }
- public function unregister($provider)
- {
- if ($provider instanceof \Grav\Plugin\MediaEmbed\OEmbed\OEmbed) {
- $key = md5(spl_object_hash($provider));
- if (isset($this->services[$key])) {
- $domains = $this->services[$key]['domains'];
- unset($this->services[$key]);
- // Unset and repopulate domain keys, if possible
- foreach ($domains as $domain) {
- if ($provider = $this->getProviderByDomain($domain)) {
- $this->domains[$domain] = get_class($provider);
- } else {
- unset($this->domains[$domain]);
- }
- }
- }
- }
- }
- protected function collectProviders($callback, $all, $id = 'provider')
- {
- $services = [];
- foreach ($this->services as $key => $service) {
- if ($callback($key, $service)) {
- $services[] = $service;
- }
- }
- // Sort providers based on priority
- uasort($services, function ($a, $b) {
- // Priority is first sort criterion
- $cmp = $a['priority'] - $b['priority'];
- // Text string is second criterion (in case of two equal priorities)
- if ( $cmp == 0 ) {
- $cmp = strnatcmp($a['name'], $b['name']);
- }
- return $cmp;
- });
- // Strip additional service informations
- $providers = array_map(function($service) use ($id) {
- return $service[$id];
- }, $services);
- if (count($providers)) {
- // Return providers
- return ($all ? $providers : $providers[0]);
- }
- return [];
- }
- /** -------------------------------
- * Private/protected helper methods
- * --------------------------------
- */
- protected function parseUrl($url)
- {
- if (!filter_var($url, FILTER_VALIDATE_URL)) {
- return [];
- }
- // Parse URL
- $url = html_entity_decode($url, ENT_COMPAT | ENT_HTML401, 'UTF-8');
- $parts = parse_url($url);
- $parts['url'] = $url;
- $parts['host'] = preg_replace("/^www\./", '', $parts['host']);
- // Get top-level domain from URL
- $parts['domain'] = isset($parts['host']) ? $parts['host'] : '';
- if ( preg_match('~(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$~i', $parts['domain'], $match) ) {
- $parts['domain'] = $match['domain'];
- }
- if (isset($parts['query'])) {
- parse_str(urldecode($parts['query']), $parts['query']);
- }
- $parts['query'] = [];
- return $parts;
- }
- }
|