BxBaseEmbedSystem.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. * Iframely integration.
  11. * @see BxDolEmbed
  12. */
  13. class BxBaseEmbedSystem extends BxDolEmbed
  14. {
  15. public function __construct ($aObject, $oTemplate)
  16. {
  17. $this->_sTableName = 'sys_embeded_data';
  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_iframely_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. {
  48. window.iframely && iframely.load(e);
  49. }
  50. </script>";
  51. }
  52. public function getDataFromApi ($sUrl, $sTheme)
  53. {
  54. $a = bx_get_site_info($sUrl, array(
  55. 'thumbnailUrl' => array('tag' => 'link', 'content_attr' => 'href'),
  56. 'OGImage' => array('name_attr' => 'property', 'name' => 'og:image'),
  57. 'icon' => array('tag' => 'link', 'name_attr' => 'rel', 'name' => 'shortcut icon', 'content_attr' => 'href'),
  58. 'icon2' => array('tag' => 'link', 'name_attr' => 'rel', 'name' => 'icon', 'content_attr' => 'href'),
  59. 'icon3' => array('tag' => 'link', 'name_attr' => 'rel', 'name' => 'apple-touch-icon', 'content_attr' => 'href'),
  60. ));
  61. $a['image'] = $a['OGImage'] ? $a['OGImage'] : $a['thumbnailUrl'];
  62. $a['logo'] = $a['icon2'] ? $a['icon2'] : ($a['icon3'] ? $a['icon3'] : $a['icon']);
  63. $a['url'] = $sUrl;
  64. unset($a['OGImage']);
  65. unset($a['thumbnailUrl']);
  66. unset($a['icon2']);
  67. unset($a['icon3']);
  68. unset($a['icon']);
  69. if ($a['image']){
  70. $oStorage = BxDolStorage::getObjectInstance('sys_images');
  71. $iMediaId = $oStorage->storeFileFromUrl($a['image'], false);
  72. if ($iMediaId){
  73. $a['image'] = $oStorage->getFileUrlById($iMediaId);
  74. }
  75. }
  76. if ($a['image'] == ''){
  77. $b = json_decode(bx_file_get_contents("https://api.microlink.io/?url=" . $sUrl), true);
  78. $a = [
  79. 'title' => $b['data']['title'],
  80. 'description' => $b['data']['description'],
  81. 'image' => $b['data']['image']['url'],
  82. 'logo' => $b['data']['logo']['url'],
  83. 'url' => $sUrl,
  84. ];
  85. }
  86. $aU = parse_url($sUrl);
  87. $a['domain'] = $aU['host'];
  88. return json_encode($a);
  89. }
  90. public function getDataHtml ($sUrl, $sTheme)
  91. {
  92. $aData = $this->getData($sUrl, $sTheme);
  93. return $aData;
  94. }
  95. public function getHtml ($sUrl, $sTheme)
  96. {
  97. $aData = $this->getData($sUrl, $sTheme);
  98. return '<a href="'.$aData['url'].'" target="_blank" style="text-decoration: none; color: inherit;">
  99. <div style="display: flex; flex-direction: row; column-gap: height: 128px; border: 1px solid rgba(107, 114, 128, 0.2); border-radius:16px; align-items: stretch;">
  100. <div style="flex: 0 0 128px; height: 128px; border-radius:16px 0 0 16px; background: url('. ($aData['image'] ? $aData['image'] : $aData['logo']) .') center center / cover no-repeat;">
  101. </div>
  102. <div style="flex-grow: 1; display: flex; flex-direction: column; justify-content: space-between; overflow: hidden;margin: 0 1rem">
  103. <div style="padding-top: 0.5rem; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 1; overflow: hidden; text-overflow: ellipsis"><b> '.$aData['title'].'</b></div>
  104. <div style="display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; overflow: hidden; text-overflow: ellipsis;">
  105. '.$aData['description'].'
  106. </div>
  107. <div style="display: flex; flex-direction: row; column-gap: 8px; align-items: center; padding-bottom: 0.5rem;">
  108. <img src="'.$aData['logo'].'" width=24 height=24 >'.$aData['domain'].'
  109. </div>
  110. </div>
  111. </div>
  112. </a>';
  113. }
  114. public function parseLinks(&$aLinks) {
  115. $aResult = [];
  116. if ($aLinks && is_array($aLinks)) {
  117. $oEmbera = $this->getEmberaInstance();
  118. foreach ($aLinks as $sLink) {
  119. $sHtml = $oEmbera->autoEmbed($sLink);
  120. //if nothing to change/embed then return empty string to not replace anything on a frontend
  121. $aResult[] = ['html' => $sHtml != $sLink ? $sHtml : ''];
  122. }
  123. }
  124. return $aResult;
  125. }
  126. }
  127. /** @} */