BxDolEmbed.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 $_sTableData = '';
  42. protected $_bCssJsAdded = false;
  43. public function __construct ($aObject, $oTemplate = null)
  44. {
  45. parent::__construct($aObject, $oTemplate, 'BxDolEmbedQuery');
  46. $this->_oDb->setParams([
  47. 'table_data' => $this->_sTableData
  48. ]);
  49. }
  50. /**
  51. * Get embed provider object instance by object name
  52. * @param $sObject object name
  53. * @return object instance or false on error
  54. */
  55. static public function getObjectInstance($sObject = false, $oTemplate = false)
  56. {
  57. if (!$sObject)
  58. $sObject = getParam('sys_embed_default');
  59. return parent::getObjectInstanceByClassNames($sObject, $oTemplate, 'BxDolEmbed', 'BxDolEmbedQuery');
  60. }
  61. /**
  62. * Print HTML which will be automatically converted into embed upon page load
  63. * @param $sLink - link
  64. * @param $sTitle - title or use link for the title if omitted
  65. * @param $sMaxWidth - try to restrict max width of embed (works in supported embed providers only)
  66. */
  67. public function getLinkHTML ($sLink, $sTitle = '', $sMaxWidth = '')
  68. {
  69. // override this function in particular embed provider class
  70. }
  71. /**
  72. * Execute an initialization JS code
  73. */
  74. public function addProcessLinkMethod ()
  75. {
  76. // override this function in particular embed provider class
  77. return '';
  78. }
  79. /**
  80. * Add css/js files which are needed for embed display and functionality.
  81. */
  82. public function addJsCss ()
  83. {
  84. // override this function in particular embed provider class
  85. }
  86. public function getData ($sUrl, $sTheme)
  87. {
  88. $sUrl = $this->cleanYoutubeUrl($sUrl);
  89. $sData = $this->_oDb->getLocal($sUrl, $sTheme);
  90. if(!$sData)
  91. $sData = $this->{'_getData' . ($this->_bAsync ? 'Async' : '')}($sUrl, $sTheme);
  92. return json_decode($sData, true);
  93. }
  94. public function getDataFromApi ($sUrl, $sTheme)
  95. {
  96. // override this function in particular embed provider class
  97. }
  98. public function process ($sUrl, $sTheme)
  99. {
  100. $sData = $this->getDataFromApi($sUrl, $sTheme);
  101. if($sData) {
  102. $this->_oDb->updateLocal($sUrl, $sTheme, $sData);
  103. bx_alert('embed', 'processed', 0, false, [
  104. 'url' => $sUrl,
  105. 'theme' => $sTheme,
  106. 'data' => $sData
  107. ]);
  108. }
  109. }
  110. public function cleanYoutubeUrl($url)
  111. {
  112. $parsedUrl = parse_url($url);
  113. if (isset($parsedUrl['host']) && $parsedUrl['host'] === 'youtu.be') {
  114. if (isset($parsedUrl['query'])) {
  115. parse_str($parsedUrl['query'], $queryParams);
  116. if (isset($queryParams['si'])) {
  117. unset($queryParams['si']);
  118. }
  119. $queryString = http_build_query($queryParams);
  120. $cleanUrl = $parsedUrl['scheme'] . '://' . $parsedUrl['host'] . $parsedUrl['path'];
  121. if (!empty($queryString)) {
  122. $cleanUrl .= '?' . $queryString;
  123. }
  124. return $cleanUrl;
  125. }
  126. return $url;
  127. }
  128. return $url;
  129. }
  130. protected function _getData($sUrl, $sTheme)
  131. {
  132. $sData = $this->getDataFromApi($sUrl, $sTheme);
  133. if($sData)
  134. $this->_oDb->insertLocal($sUrl, $sTheme, $sData);
  135. return $sData;
  136. }
  137. protected function _getDataAsync($sUrl, $sTheme)
  138. {
  139. $sName = 'embed_for_' . $sUrl . ($sTheme ? '_' . $sTheme : '');
  140. if(($oCronQuery = BxDolCronQuery::getInstance()) && !$oCronQuery->isTransientJobService($sName)) {
  141. $this->_oDb->insertLocal($sUrl, $sTheme);
  142. $oCronQuery->addTransientJobService($sName, ['system', 'process_embed', [$sUrl, $sTheme], 'TemplServices']);
  143. }
  144. return json_encode([
  145. 'url' => $sUrl,
  146. 'title' => $sUrl,
  147. 'domain' => parse_url($sUrl, PHP_URL_HOST)
  148. ]);
  149. }
  150. }
  151. /** @} */