Alexander 5 лет назад
Родитель
Сommit
253f4e043c

+ 2 - 0
modules/boonex/videos/classes/BxVideosConfig.php

@@ -183,6 +183,8 @@ class BxVideosConfig extends BxBaseModTextConfig
                 'txt_all_entries_by_context' => '_bx_videos_page_title_browse_by_context',
                 'txt_all_entries_by_context' => '_bx_videos_page_title_browse_by_context',
                 'txt_pict_use_as_poster' => '_bx_videos_form_entry_input_picture_use_as_poster',
                 'txt_pict_use_as_poster' => '_bx_videos_form_entry_input_picture_use_as_poster',
             ),
             ),
+
+            'OEMBED_PROVIDERS_IMPORT_URL' => 'https://oembed.com/providers.json',
         );
         );
 
 
         $this->_aJsClasses = array(
         $this->_aJsClasses = array(

+ 40 - 0
modules/boonex/videos/classes/BxVideosDb.php

@@ -33,6 +33,46 @@ class BxVideosDb extends BxBaseModTextDb
     public function getEmbedProviders() {
     public function getEmbedProviders() {
         return $this->getAll("SELECT * FROM `bx_videos_embeds_providers`");
         return $this->getAll("SELECT * FROM `bx_videos_embeds_providers`");
     }
     }
+
+    public function updateOEmbedProviders() {
+        $CNF = &$this->_oConfig->CNF;
+
+        $aProviders = @json_decode(bx_file_get_contents($CNF['OEMBED_PROVIDERS_IMPORT_URL']), true);
+        if (!$aProviders) return;
+
+        $this->query('TRUNCATE TABLE `bx_videos_oembed_endpoints`');
+        $this->query('TRUNCATE TABLE `bx_videos_oembed_schemes`');
+
+        foreach ($aProviders as $aProvider) {
+            foreach ($aProvider['endpoints'] as $aEndpoint) {
+                if (!isset($aEndpoint['url']) || empty($aEndpoint['url']) || !isset($aEndpoint['schemes']) || empty($aEndpoint['schemes'])) continue;
+
+                $aEndpoint['url'] = str_replace('{format}', 'json', $aEndpoint['url']);
+                $this->query('INSERT INTO `bx_videos_oembed_endpoints` SET `url` = :url', ['url' => $aEndpoint['url']]);
+                $iEndpointId = $this->lastId();
+
+                $aRows = [];
+                foreach ($aEndpoint['schemes'] as $sScheme) {
+                    $aRows[] = "({$iEndpointId}, ".$this->escape(str_replace('*', '%', $sScheme)).")";
+                }
+                if ($aRows)
+                    $this->query('INSERT INTO `bx_videos_oembed_schemes` (`endpoint_id`, `url`) VALUES '.implode(', ', $aRows));
+            }
+        }
+    }
+
+    public function getOEmbedEndpoint($sLink) {
+        if (!$this->getOne("SELECT COUNT(*) FROM `bx_videos_oembed_endpoints`"))
+            $this->updateOEmbedProviders();
+
+        return $this->getOne("
+            SELECT `bx_videos_oembed_endpoints`.`url` 
+            FROM `bx_videos_oembed_endpoints`
+            JOIN `bx_videos_oembed_schemes` ON `bx_videos_oembed_endpoints`.`id` = `bx_videos_oembed_schemes`.`endpoint_id`
+            WHERE :link LIKE `bx_videos_oembed_schemes`.`url`
+            LIMIT 1
+        ", ['link' => $sLink]);
+    }
 }
 }
 
 
 /** @} */
 /** @} */

+ 9 - 7
modules/boonex/videos/classes/BxVideosEmbedProviderOEmbed.php

@@ -14,18 +14,20 @@
  */
  */
 class BxVideosEmbedProviderOEmbed
 class BxVideosEmbedProviderOEmbed
 {
 {
-    var $_sOEmbedUrl;
-    var $_sQuickCheck;
+    var $_oModule;
+    public function __construct(&$oModule) {
+        $this->_oModule = $oModule;
+    }
 
 
-    public function __construct($aParams) {
-        $this->_sOEmbedUrl = $aParams['oembed_url'];
-        $this->_sQuickCheck = $aParams['quick_check'];
+    public function updateEndpoints() {
+        $this->_oModule->_oDb->updateOEmbedProviders();
     }
     }
 
 
     public function parseLink($sLink) {
     public function parseLink($sLink) {
-        if ($this->_sQuickCheck && stripos($sLink, $this->_sQuickCheck) === false) return false;
+        $sEndpointUrl = $this->_oModule->_oDb->getOEmbedEndpoint($sLink);
+        if (!$sEndpointUrl) return false;
 
 
-        $aResponse = @json_decode(file_get_contents(bx_append_url_params($this->_sOEmbedUrl, ['url' => $sLink])), true);
+        $aResponse = @json_decode(bx_file_get_contents(bx_append_url_params($sEndpointUrl, ['url' => $sLink])), true);
         if ($aResponse) {
         if ($aResponse) {
             if (isset($aResponse['type']) && $aResponse['type'] == 'video' && isset($aResponse['html']) && !empty($aResponse['html']))
             if (isset($aResponse['type']) && $aResponse['type'] == 'video' && isset($aResponse['html']) && !empty($aResponse['html']))
                 return [
                 return [

+ 7 - 1
modules/boonex/videos/classes/BxVideosModule.php

@@ -33,7 +33,7 @@ class BxVideosModule extends BxBaseModTextModule
             if ($aEmbedProvider['class_file'])
             if ($aEmbedProvider['class_file'])
                 require_once(BX_DIRECTORY_PATH_ROOT . $aEmbedProvider['class_file']);
                 require_once(BX_DIRECTORY_PATH_ROOT . $aEmbedProvider['class_file']);
 
 
-            $oLinkParser = new $aEmbedProvider['class_name']($aEmbedProvider['params'] ? unserialize($aEmbedProvider['params']) : []);
+            $oLinkParser = new $aEmbedProvider['class_name']($this);
             if ($mResult = $oLinkParser->parseLink($sLink))
             if ($mResult = $oLinkParser->parseLink($sLink))
                 return $mResult;
                 return $mResult;
         }
         }
@@ -168,6 +168,12 @@ class BxVideosModule extends BxBaseModTextModule
             )
             )
         );
         );
     }
     }
+
+    public function serviceUpdateOEmbedProviders() {
+        bx_import('EmbedProviderOEmbed', $this->_aModule);
+        $oLinkParser = new BxVideosEmbedProviderOEmbed($this);
+        $oLinkParser->updateEndpoints();
+    }
 }
 }
 
 
 /** @} */
 /** @} */

+ 1 - 0
modules/boonex/videos/install/config.php

@@ -112,6 +112,7 @@ $aConfig = array(
     	'process_esearches' => 1,
     	'process_esearches' => 1,
         'register_transcoders' => 1,
         'register_transcoders' => 1,
         'clear_db_cache' => 1,
         'clear_db_cache' => 1,
+        'initialize_oembed_endpoints' => 1,
     ),
     ),
     'disable' => array (
     'disable' => array (
         'execute_sql' => 1,
         'execute_sql' => 1,

+ 5 - 0
modules/boonex/videos/install/installer.php

@@ -15,6 +15,11 @@ class BxVideosInstaller extends BxBaseModTextInstaller
     {
     {
         parent::__construct($aConfig);
         parent::__construct($aConfig);
     }
     }
+
+    protected function actionInitializeOEmbedEndpoints($sOperation) {
+        bx_srv('bx_videos', 'update_oembed_providers', array());
+        return BX_DOL_STUDIO_INSTALLER_SUCCESS;
+    }
 }
 }
 
 
 /** @} */
 /** @} */

+ 3 - 0
modules/boonex/videos/install/sql/disable.sql

@@ -61,3 +61,6 @@ DELETE FROM `sys_objects_uploader` WHERE `object` IN('bx_videos_simple', 'bx_vid
 SET @iHandler := (SELECT `id` FROM `sys_alerts_handlers` WHERE `name` = 'bx_videos' LIMIT 1);
 SET @iHandler := (SELECT `id` FROM `sys_alerts_handlers` WHERE `name` = 'bx_videos' LIMIT 1);
 DELETE FROM `sys_alerts` WHERE `handler_id` = @iHandler;
 DELETE FROM `sys_alerts` WHERE `handler_id` = @iHandler;
 DELETE FROM `sys_alerts_handlers` WHERE `id` = @iHandler;
 DELETE FROM `sys_alerts_handlers` WHERE `id` = @iHandler;
+
+-- CRON
+DELETE FROM `sys_cron_jobs` WHERE `name` = 'bx_videos_oembed_update';

+ 5 - 0
modules/boonex/videos/install/sql/enable.sql

@@ -470,3 +470,8 @@ INSERT INTO `sys_alerts` (`unit`, `action`, `handler_id`) VALUES
 ('profile', 'delete', @iHandler),
 ('profile', 'delete', @iHandler),
 
 
 ('bx_videos_video_mp4', 'transcoded', @iHandler);
 ('bx_videos_video_mp4', 'transcoded', @iHandler);
+
+
+-- CRON
+INSERT INTO `sys_cron_jobs` (`name`, `time`, `class`, `file`, `service_call`) VALUES
+('bx_videos_oembed_update', '0 0 * * 0', '', '', 'a:2:{s:6:"module";s:9:"bx_videos";s:6:"method";s:23:"update_oembed_providers";}');

+ 14 - 2
modules/boonex/videos/install/sql/install.sql

@@ -289,9 +289,21 @@ CREATE TABLE IF NOT EXISTS `bx_videos_embeds_providers` (
   `class_file` varchar(255) NOT NULL,
   `class_file` varchar(255) NOT NULL,
   PRIMARY KEY (`id`)
   PRIMARY KEY (`id`)
 );
 );
+
+CREATE TABLE IF NOT EXISTS `bx_videos_oembed_endpoints` (
+  `id` int(11) NOT NULL AUTO_INCREMENT,
+  `url` varchar(255) NOT NULL,
+  PRIMARY KEY (`id`)
+);
+
+CREATE TABLE IF NOT EXISTS `bx_videos_oembed_schemes` (
+  `id` int(11) NOT NULL AUTO_INCREMENT,
+  `endpoint_id` int(11) NOT NULL,
+  `url` varchar(255) NOT NULL,
+  PRIMARY KEY (`id`)
+);
 INSERT INTO `bx_videos_embeds_providers` (`object`, `module`, `params`, `class_name`, `class_file`) VALUES
 INSERT INTO `bx_videos_embeds_providers` (`object`, `module`, `params`, `class_name`, `class_file`) VALUES
-('youtube', 'bx_videos', 'a:2:{s:10:"oembed_url";s:42:"https://www.youtube.com/oembed?format=json";s:11:"quick_check";s:5:"youtu";}', 'BxVideosEmbedProviderOEmbed', 'modules/boonex/videos/classes/BxVideosEmbedProviderOEmbed.php'),
-('vimeo', 'bx_videos', 'a:2:{s:10:"oembed_url";s:33:"https://vimeo.com/api/oembed.json";s:11:"quick_check";s:5:"vimeo";}', 'BxVideosEmbedProviderOEmbed', 'modules/boonex/videos/classes/BxVideosEmbedProviderOEmbed.php');
+('oembed', 'bx_videos', '', 'BxVideosEmbedProviderOEmbed', 'modules/boonex/videos/classes/BxVideosEmbedProviderOEmbed.php');
 
 
 
 
 -- STORAGES & TRANSCODERS
 -- STORAGES & TRANSCODERS

+ 1 - 1
modules/boonex/videos/install/sql/uninstall.sql

@@ -1,6 +1,6 @@
 
 
 -- TABLES
 -- TABLES
-DROP TABLE IF EXISTS `bx_videos_entries`, `bx_videos_photos`, `bx_videos_videos`, `bx_videos_media_resized`, `bx_videos_cmts`, `bx_videos_cmts_notes`, `bx_videos_votes`, `bx_videos_votes_track`, `bx_videos_svotes`, `bx_videos_svotes_track`, `bx_videos_reactions`, `bx_videos_reactions_track`, `bx_videos_views_track`, `bx_videos_meta_keywords`, `bx_videos_meta_locations`, `bx_videos_meta_mentions`, `bx_videos_reports`, `bx_videos_reports_track`, `bx_videos_favorites_track`, `bx_videos_favorites_lists`, `bx_videos_scores`, `bx_videos_scores_track`, `bx_videos_embeds_providers`;
+DROP TABLE IF EXISTS `bx_videos_entries`, `bx_videos_photos`, `bx_videos_videos`, `bx_videos_media_resized`, `bx_videos_cmts`, `bx_videos_cmts_notes`, `bx_videos_votes`, `bx_videos_votes_track`, `bx_videos_svotes`, `bx_videos_svotes_track`, `bx_videos_reactions`, `bx_videos_reactions_track`, `bx_videos_views_track`, `bx_videos_meta_keywords`, `bx_videos_meta_locations`, `bx_videos_meta_mentions`, `bx_videos_reports`, `bx_videos_reports_track`, `bx_videos_favorites_track`, `bx_videos_favorites_lists`, `bx_videos_scores`, `bx_videos_scores_track`, `bx_videos_embeds_providers`, `bx_videos_oembed_endpoints`, `bx_videos_oembed_schemes`;
 
 
 
 
 -- STORAGES & TRANSCODERS
 -- STORAGES & TRANSCODERS