BxDolEmbed.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 $_bCssJsAdded = false;
  41. protected $_sTableName = '';
  42. /**
  43. * Get embed provider object instance by object name
  44. * @param $sObject object name
  45. * @return object instance or false on error
  46. */
  47. static public function getObjectInstance($sObject = false, $oTemplate = false)
  48. {
  49. if (!$sObject)
  50. $sObject = getParam('sys_embed_default');
  51. return parent::getObjectInstanceByClassNames($sObject, $oTemplate, 'BxDolEmbed', 'BxDolEmbedQuery');
  52. }
  53. /**
  54. * Print HTML which will be automatically converted into embed upon page load
  55. * @param $sLink - link
  56. * @param $sTitle - title or use link for the title if omitted
  57. * @param $sMaxWidth - try to restrict max width of embed (works in supported embed providers only)
  58. */
  59. public function getLinkHTML ($sLink, $sTitle = '', $sMaxWidth = '')
  60. {
  61. // override this function in particular embed provider class
  62. }
  63. /**
  64. * Execute an initialization JS code
  65. */
  66. public function addProcessLinkMethod ()
  67. {
  68. // override this function in particular embed provider class
  69. return '';
  70. }
  71. /**
  72. * Add css/js files which are needed for embed display and functionality.
  73. */
  74. public function addJsCss ()
  75. {
  76. // override this function in particular embed provider class
  77. }
  78. public function getData ($sUrl, $sTheme)
  79. {
  80. $sUrl = $this->cleanYoutubeUrl($sUrl);
  81. $sData = BxDolEmbedQuery::getLocal($sUrl, $sTheme, $this->_sTableName);
  82. if(!$sData) {
  83. $sData = $this->getDataFromApi($sUrl, $sTheme);
  84. if($sData)
  85. BxDolEmbedQuery::setLocal($sUrl, $sTheme, $this->_sTableName, $sData);
  86. }
  87. return json_decode($sData, true);
  88. }
  89. public function getDataFromApi ($sUrl, $sTheme)
  90. {
  91. // override this function in particular embed provider class
  92. }
  93. function cleanYoutubeUrl($url) {
  94. $parsedUrl = parse_url($url);
  95. if (isset($parsedUrl['host']) && $parsedUrl['host'] === 'youtu.be') {
  96. if (isset($parsedUrl['query'])) {
  97. parse_str($parsedUrl['query'], $queryParams);
  98. if (isset($queryParams['si'])) {
  99. unset($queryParams['si']);
  100. }
  101. $queryString = http_build_query($queryParams);
  102. $cleanUrl = $parsedUrl['scheme'] . '://' . $parsedUrl['host'] . $parsedUrl['path'];
  103. if (!empty($queryString)) {
  104. $cleanUrl .= '?' . $queryString;
  105. }
  106. return $cleanUrl;
  107. }
  108. return $url;
  109. }
  110. return $url;
  111. }
  112. }
  113. /** @} */