1
0

BxBaseEmbedOembed.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * Oembed integration.
  11. * @see BxDolEmbed
  12. */
  13. class BxBaseEmbedOembed extends BxDolEmbed
  14. {
  15. var $_iCacheTTL = 3600;
  16. public function __construct ($aObject, $oTemplate)
  17. {
  18. parent::__construct ($aObject);
  19. if ($oTemplate)
  20. $this->_oTemplate = $oTemplate;
  21. else
  22. $this->_oTemplate = BxDolTemplate::getInstance();
  23. }
  24. public function getLinkHTML ($sLink, $sTitle = '', $sMaxWidth = '')
  25. {
  26. $aAttrs = array(
  27. 'title' => bx_html_attribute($sTitle),
  28. );
  29. // check for external link
  30. if (strncmp(BX_DOL_URL_ROOT, $sLink, strlen(BX_DOL_URL_ROOT)) !== 0) {
  31. $aAttrs['target'] = '_blank';
  32. if (getParam('sys_add_nofollow') == 'on')
  33. $aAttrs['rel'] = 'nofollow';
  34. }
  35. return $this->_oTemplate->parseHtmlByName('embed_oembed_link.html', array(
  36. 'link' => $sLink,
  37. 'title' => $sTitle,
  38. 'attrs' => bx_convert_array2attrs($aAttrs),
  39. 'width' => $sMaxWidth,
  40. ));
  41. }
  42. public function addProcessLinkMethod ()
  43. {
  44. return "
  45. <script>
  46. function bx_embed_link(e) {
  47. bx_oembed_process_links(e);
  48. }
  49. </script>";
  50. }
  51. public function addJsCss ()
  52. {
  53. if ($this->_bCssJsAdded)
  54. return '';
  55. $this->_bCssJsAdded = true;
  56. $this->_oTemplate->addCss('oembed.css');
  57. return $this->_oTemplate->parseHtmlByName('embed_oembed_integration.html', []);
  58. }
  59. public function parseLinks(&$aLinks) {
  60. $aResult = [];
  61. if ($aLinks && is_array($aLinks)) {
  62. $oEmbera = $this->getEmberaInstance();
  63. foreach ($aLinks as $sLink) {
  64. $sHtml = $oEmbera->autoEmbed($sLink);
  65. //if nothing to change/embed then return empty string to not replace anything on a frontend
  66. $aResult[] = ['html' => $sHtml != $sLink ? $sHtml : ''];
  67. }
  68. }
  69. return $aResult;
  70. }
  71. public function parseLinkHtml($sLink) {
  72. $oEmbera = $this->getEmberaInstance();
  73. return $oEmbera->autoEmbed($sLink);
  74. }
  75. public function getUrlData($sLink) {
  76. $oEmbera = $this->getEmberaInstance(['responsive' => false]);
  77. return $oEmbera->getUrlData([$sLink]);
  78. }
  79. protected function getEmberaInstance($aOptions = []) {
  80. static $oEmbera;
  81. if (!$oEmbera) {
  82. $oHttpCache = new Embera\Http\HttpClientCache(new Embera\Http\HttpClient());
  83. $oHttpCache->setCachingEngine(new Embera\Cache\Filesystem(BX_DIRECTORY_PATH_TMP, $this->_iCacheTTL));
  84. $aDefaultOptions = [
  85. 'responsive' => true,
  86. ];
  87. $oEmbera = new Embera\Embera(array_merge($aDefaultOptions, $aOptions), null, $oHttpCache);
  88. }
  89. return $oEmbera;
  90. }
  91. }
  92. /** @} */