1
0

BxBaseEmbedOembed.php 2.9 KB

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