BxBaseEmbedSystem.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. * System (default) integration.
  11. *
  12. * @see BxDolEmbed
  13. */
  14. class BxBaseEmbedSystem extends BxDolEmbed
  15. {
  16. public function __construct ($aObject, $oTemplate)
  17. {
  18. $this->_sTableName = 'sys_embeded_data';
  19. parent::__construct ($aObject);
  20. if ($oTemplate)
  21. $this->_oTemplate = $oTemplate;
  22. else
  23. $this->_oTemplate = BxDolTemplate::getInstance();
  24. }
  25. public function getLinkHTML ($sLink, $sTitle = '', $sMaxWidth = '')
  26. {
  27. $aData = $this->getData($sLink, '');
  28. if(bx_is_api()) {
  29. if(bx_get('mode') == 'alt')
  30. return json_encode($aData);
  31. return $aData;
  32. }
  33. $aAttrs = [
  34. 'title' => bx_html_attribute($sTitle),
  35. ];
  36. // check for external link
  37. if(strncmp(BX_DOL_URL_ROOT, $sLink, strlen(BX_DOL_URL_ROOT)) !== 0) {
  38. $aAttrs['target'] = '_blank';
  39. if(getParam('sys_add_nofollow') == 'on')
  40. $aAttrs['rel'] = 'nofollow';
  41. }
  42. return $this->_oTemplate->parseHtmlByName('embed_system_link.html', [
  43. 'link' => $aData['url'],
  44. 'attrs' => bx_convert_array2attrs($aAttrs),
  45. 'width' => $sMaxWidth,
  46. 'image' => $aData['image'] ? $aData['image'] : $aData['logo'],
  47. 'logo' => $aData['logo'],
  48. 'title' => $aData['title'],
  49. 'description' => $aData['description'],
  50. 'domain' => $aData['domain'],
  51. ]);
  52. }
  53. public function getDataFromApi ($sUrl, $sTheme)
  54. {
  55. $a = bx_get_site_info($sUrl, [
  56. 'thumbnailUrl' => ['tag' => 'link', 'content_attr' => 'href'],
  57. 'OGImage' => ['name_attr' => 'property', 'name' => 'og:image'],
  58. 'icon' => ['tag' => 'link', 'name_attr' => 'rel', 'name' => 'shortcut icon', 'content_attr' => 'href'],
  59. 'icon2' => ['tag' => 'link', 'name_attr' => 'rel', 'name' => 'icon', 'content_attr' => 'href'],
  60. 'icon3' => ['tag' => 'link', 'name_attr' => 'rel', 'name' => 'apple-touch-icon', 'content_attr' => 'href'],
  61. ]);
  62. $a = array_merge($a, [
  63. 'image' => $a['OGImage'] ? $a['OGImage'] : $a['thumbnailUrl'],
  64. 'logo' => $a['icon2'] ? $a['icon2'] : ($a['icon3'] ? $a['icon3'] : $a['icon']),
  65. 'url' => $sUrl
  66. ]);
  67. unset($a['OGImage'], $a['thumbnailUrl'], $a['icon'], $a['icon2'], $a['icon3']);
  68. if($a['image'] == '') {
  69. $c = [];
  70. if (getParam('sys_embed_microlink_key') != ''){
  71. $c = bx_file_get_contents("https://pro.microlink.io/?url=" . $sUrl, [], 'get', ['x-api-key: ' . getParam('sys_embed_microlink_key')]);
  72. }
  73. else{
  74. $c = bx_file_get_contents("https://api.microlink.io/?url=" . $sUrl);
  75. }
  76. $b = json_decode($c, true);
  77. $a = [
  78. 'title' => $b['data']['title'],
  79. 'description' => $b['data']['description'],
  80. 'image' => $b['data']['image']['url'],
  81. 'logo' => $b['data']['logo']['url'],
  82. 'url' => $sUrl,
  83. ];
  84. }
  85. if($a['image'] && ($oStorage = BxDolStorage::getObjectInstance('sys_images')) !== false) {
  86. $iMediaId = $oStorage->storeFileFromUrl($a['image'], false);
  87. if($iMediaId)
  88. $a['image'] = $oStorage->getFileUrlById($iMediaId);
  89. }
  90. if($a['logo'] && ($oStorage = BxDolStorage::getObjectInstance('sys_images')) !== false) {
  91. $iMediaId = $oStorage->storeFileFromUrl($a['logo'], false);
  92. if($iMediaId)
  93. $a['logo'] = $oStorage->getFileUrlById($iMediaId);
  94. }
  95. $aUrl = parse_url($sUrl);
  96. $a['domain'] = $aUrl['host'];
  97. return json_encode($a);
  98. }
  99. public function parseLinks(&$aLinks)
  100. {
  101. $aResult = [];
  102. if($aLinks && is_array($aLinks)) {
  103. $oEmbera = $this->getEmberaInstance();
  104. foreach ($aLinks as $sLink) {
  105. $sHtml = $oEmbera->autoEmbed($sLink);
  106. //if nothing to change/embed then return empty string to not replace anything on a frontend
  107. $aResult[] = ['html' => $sHtml != $sLink ? $sHtml : ''];
  108. }
  109. }
  110. return $aResult;
  111. }
  112. }
  113. /** @} */