BxDolEmbed.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. /**
  10. * Embed provider.
  11. *
  12. * Create an embed from a link.
  13. *
  14. * @section embed_provider_create Creating the Editor object:
  15. *
  16. *
  17. * Add record to 'sys_objects_embeds' table:
  18. *
  19. * - object: name of the embed object, in the format: vendor prefix, underscore, module prefix, underscore, internal identifier or nothing
  20. * - title: title of the embed provider, dmay be isplayed in the Studio.
  21. * - override_class_name: user defined class name which is derived from one of base embed provider classes.
  22. * - override_class_file: the location of the user defined class, leave it empty if class is located in system folders.
  23. *
  24. *
  25. * @section example Example of usage
  26. *
  27. * Generate HTML for a link which will be converted to an embed later:
  28. *
  29. * @code
  30. * $oEmbed = BxDolEmbed::getObjectInstance(); // get default embed object instance
  31. * if ($oEmbed) // check if embed provider is available for using
  32. * echo $oEmbed->getLinkHTML ('https://una.io', 'UNA.IO'); // output HTML which will be automatically converted into embed upon page load
  33. * else
  34. * echo '<a href="https://una.io">UNA.IO</a>';
  35. * @endcode
  36. *
  37. */
  38. class BxDolEmbed extends BxDolFactoryObject
  39. {
  40. protected $_bAsync = false;
  41. protected $_sSocketName = 'sys_embed';
  42. protected $_sTableData = '';
  43. protected $_bCssJsAdded = false;
  44. public function __construct ($aObject, $oTemplate = null)
  45. {
  46. parent::__construct($aObject, $oTemplate, 'BxDolEmbedQuery');
  47. $this->_oDb->setParams([
  48. 'table_data' => $this->_sTableData
  49. ]);
  50. }
  51. /**
  52. * Get embed provider object instance by object name
  53. * @param $sObject object name
  54. * @return object instance or false on error
  55. */
  56. static public function getObjectInstance($sObject = false, $oTemplate = false)
  57. {
  58. if (!$sObject)
  59. $sObject = getParam('sys_embed_default');
  60. return parent::getObjectInstanceByClassNames($sObject, $oTemplate, 'BxDolEmbed', 'BxDolEmbedQuery');
  61. }
  62. /**
  63. * Print HTML which will be automatically converted into embed upon page load
  64. * @param $sLink - link
  65. * @param $sTitle - title or use link for the title if omitted
  66. * @param $sMaxWidth - try to restrict max width of embed (works in supported embed providers only)
  67. */
  68. public function getLinkHTML ($sLink, $sTitle = '', $sMaxWidth = '')
  69. {
  70. // override this function in particular embed provider class
  71. }
  72. /**
  73. * Execute an initialization JS code
  74. */
  75. public function addProcessLinkMethod ()
  76. {
  77. // override this function in particular embed provider class
  78. return '';
  79. }
  80. /**
  81. * Add css/js files which are needed for embed display and functionality.
  82. */
  83. public function addJsCss ()
  84. {
  85. // override this function in particular embed provider class
  86. }
  87. public function getData ($sUrl, $sTheme)
  88. {
  89. $sUrl = $this->cleanYoutubeUrl($sUrl);
  90. $sData = $this->_oDb->getLocal($sUrl, $sTheme);
  91. if(!$sData)
  92. $sData = $this->{'_getData' . ($sData !== false ? 'Empty' : ($this->_bAsync ? 'Async' : ''))}($sUrl, $sTheme);
  93. return json_decode($sData, true);
  94. }
  95. public function getDataFromApi ($sUrl, $sTheme)
  96. {
  97. // override this function in particular embed provider class
  98. }
  99. public function process ()
  100. {
  101. $aEmbeds = $this->_oDb->getLocalUnprocessed();
  102. if(empty($aEmbeds) || !is_array($aEmbeds))
  103. return;
  104. $oSockets = BxDolSockets::getInstance();
  105. $bSockets = $oSockets->isEnabled();
  106. foreach($aEmbeds as $aEmbed)
  107. if(($sData = $this->getDataFromApi($aEmbed['url'], $aEmbed['theme']))) {
  108. $this->_oDb->updateLocal(['data' => $sData], ['id' => $aEmbed['id']]);
  109. if($bSockets)
  110. $oSockets->sendEvent($this->_sSocketName, $aEmbed['id'], 'processed', $sData);
  111. }
  112. }
  113. public function cleanYoutubeUrl($url)
  114. {
  115. $parsedUrl = parse_url($url);
  116. if (isset($parsedUrl['host']) && $parsedUrl['host'] === 'youtu.be') {
  117. if (isset($parsedUrl['query'])) {
  118. parse_str($parsedUrl['query'], $queryParams);
  119. if (isset($queryParams['si'])) {
  120. unset($queryParams['si']);
  121. }
  122. $queryString = http_build_query($queryParams);
  123. $cleanUrl = $parsedUrl['scheme'] . '://' . $parsedUrl['host'] . $parsedUrl['path'];
  124. if (!empty($queryString)) {
  125. $cleanUrl .= '?' . $queryString;
  126. }
  127. return $cleanUrl;
  128. }
  129. return $url;
  130. }
  131. return $url;
  132. }
  133. protected function _getData($sUrl, $sTheme)
  134. {
  135. $sData = $this->getDataFromApi($sUrl, $sTheme);
  136. if($sData)
  137. $this->_oDb->insertLocal($sUrl, $sTheme, $sData);
  138. return $sData;
  139. }
  140. protected function _getDataAsync($sUrl, $sTheme)
  141. {
  142. $iId = $this->_oDb->insertLocal($sUrl, $sTheme);
  143. if(($sName = 'bx_embed_processing') && ($oCronQuery = BxDolCronQuery::getInstance()) && !$oCronQuery->isTransientJobService($sName))
  144. $oCronQuery->addTransientJobService($sName, ['system', 'process_embed', [], 'TemplServices']);
  145. return $this->_getDataEmpty($sUrl, $sTheme, [
  146. 'id' => $iId,
  147. 'processing' => true
  148. ]);
  149. }
  150. protected function _getDataEmpty($sUrl, $sTheme, $aAddons = [])
  151. {
  152. return json_encode(array_merge([
  153. 'url' => $sUrl,
  154. 'domain' => parse_url($sUrl, PHP_URL_HOST),
  155. ], $aAddons));
  156. }
  157. }
  158. /** @} */