1
0

BxBaseEmbedSystem.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. $oStorage = BxDolStorage::getObjectInstance('sys_images');
  43. $sImage = $aData['image'];
  44. if ($sImage && is_numeric($sImage) && $oStorage) {
  45. $sImage = $oStorage->getFileUrlById($sImage);
  46. }
  47. $sLogo = $aData['logo'];
  48. if ($sLogo && is_numeric($sLogo) && $oStorage) {
  49. $sLogo = $oStorage->getFileUrlById($sLogo);
  50. }
  51. return $this->_oTemplate->parseHtmlByName('embed_system_link.html', [
  52. 'link' => $aData['url'],
  53. 'attrs' => bx_convert_array2attrs($aAttrs),
  54. 'width' => $sMaxWidth,
  55. 'image' => $sImage ? $sImage : $sLogo,
  56. 'logo' => $sLogo,
  57. 'title' => $aData['title'],
  58. 'description' => $aData['description'],
  59. 'domain' => $aData['domain'],
  60. ]);
  61. }
  62. public function getDataFromApi ($sUrl, $sTheme)
  63. {
  64. $a = bx_get_site_info($sUrl, [
  65. 'thumbnailUrl' => ['tag' => 'link', 'content_attr' => 'href'],
  66. 'OGImage' => ['name_attr' => 'property', 'name' => 'og:image'],
  67. 'icon' => ['tag' => 'link', 'name_attr' => 'rel', 'name' => 'shortcut icon', 'content_attr' => 'href'],
  68. 'icon2' => ['tag' => 'link', 'name_attr' => 'rel', 'name' => 'icon', 'content_attr' => 'href'],
  69. 'icon3' => ['tag' => 'link', 'name_attr' => 'rel', 'name' => 'apple-touch-icon', 'content_attr' => 'href'],
  70. ]);
  71. $a = array_merge($a, [
  72. 'image' => $a['OGImage'] ? $a['OGImage'] : $a['thumbnailUrl'],
  73. 'logo' => $a['icon2'] ? $a['icon2'] : ($a['icon3'] ? $a['icon3'] : $a['icon']),
  74. 'url' => $sUrl
  75. ]);
  76. unset($a['OGImage'], $a['thumbnailUrl'], $a['icon'], $a['icon2'], $a['icon3']);
  77. if($a['image'] == '') {
  78. $c = [];
  79. if (getParam('sys_embed_microlink_key') != ''){
  80. $c = bx_file_get_contents("https://pro.microlink.io/?url=" . $sUrl, [], 'get', ['x-api-key: ' . getParam('sys_embed_microlink_key')]);
  81. }
  82. else{
  83. $c = bx_file_get_contents("https://api.microlink.io/?url=" . $sUrl);
  84. }
  85. $b = json_decode($c, true);
  86. $a = [
  87. 'title' => $b['data']['title'],
  88. 'description' => $b['data']['description'],
  89. 'image' => $b['data']['image']['url'],
  90. 'logo' => $b['data']['logo']['url'],
  91. 'url' => $sUrl,
  92. ];
  93. }
  94. if($a['image'] && ($oStorage = BxDolStorage::getObjectInstance('sys_images')) !== false) {
  95. $iMediaId = $oStorage->storeFileFromUrl($a['image'], false);
  96. $a['image'] = $iMediaId;
  97. }
  98. if($a['logo'] && ($oStorage = BxDolStorage::getObjectInstance('sys_images')) !== false) {
  99. $iMediaId = $oStorage->storeFileFromUrl($a['logo'], false);
  100. $a['logo'] = $iMediaId;
  101. }
  102. $aUrl = parse_url($sUrl);
  103. $a['domain'] = $aUrl['host'];
  104. return json_encode($a);
  105. }
  106. }
  107. /** @} */