BxBaseEmbedSystem.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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->_sTableData = 'sys_embeded_data';
  19. parent::__construct ($aObject, $oTemplate);
  20. }
  21. public function getLinkHTML ($sLink, $sTitle = '', $sMaxWidth = '')
  22. {
  23. $aData = $this->getData($sLink, '');
  24. if(bx_is_api()) {
  25. if(bx_get('mode') == 'alt')
  26. return json_encode($aData);
  27. return $aData;
  28. }
  29. $aAttrs = [
  30. 'title' => bx_html_attribute($sTitle),
  31. ];
  32. // check for external link
  33. if(strncmp(BX_DOL_URL_ROOT, $sLink, strlen(BX_DOL_URL_ROOT)) !== 0) {
  34. $aAttrs['target'] = '_blank';
  35. if(getParam('sys_add_nofollow') == 'on')
  36. $aAttrs['rel'] = 'nofollow';
  37. }
  38. $sImage = $sLogo = '';
  39. if(($oStorage = BxDolStorage::getObjectInstance('sys_images')) !== false) {
  40. if(($iImageId = $aData['image_id'] ?? 0) || (($iImageId = $aData['image'] ?? '') && is_numeric($iImageId)))
  41. $sImage = $oStorage->getFileUrlById($iImageId);
  42. if(($iLogoId = $aData['logo_id'] ?? 0) || (($iLogoId = $aData['logo'] ?? '') && is_numeric($iLogoId)))
  43. $sLogo = $oStorage->getFileUrlById($iLogoId);
  44. }
  45. $sImage = $sImage ?: ($sLogo ?: $this->_oTemplate->getImageUrl('embed.svg'));
  46. return $this->_oTemplate->parseHtmlByName('embed_system_link.html', [
  47. 'link' => $aData['url'],
  48. 'attrs' => bx_convert_array2attrs($aAttrs),
  49. 'width' => $sMaxWidth,
  50. 'bx_if:show_image' => [
  51. 'condition' => (bool)$sImage,
  52. 'content' => [
  53. 'image' => $sImage
  54. ]
  55. ],
  56. 'bx_if:show_logo' => [
  57. 'condition' => (bool)$sLogo,
  58. 'content' => [
  59. 'logo' => $sLogo,
  60. ]
  61. ],
  62. 'title' => $aData['title'] ?? '',
  63. 'description' => $aData['description'] ?? '',
  64. 'domain' => $aData['domain'],
  65. ]);
  66. }
  67. public function getDataFromApi ($sUrl, $sTheme)
  68. {
  69. $a = bx_get_site_info($sUrl, [
  70. 'thumbnailUrl' => ['tag' => 'link', 'content_attr' => 'href'],
  71. 'OGImage' => ['name_attr' => 'property', 'name' => 'og:image'],
  72. 'icon' => ['tag' => 'link', 'name_attr' => 'rel', 'name' => 'shortcut icon', 'content_attr' => 'href'],
  73. 'icon2' => ['tag' => 'link', 'name_attr' => 'rel', 'name' => 'icon', 'content_attr' => 'href'],
  74. 'icon3' => ['tag' => 'link', 'name_attr' => 'rel', 'name' => 'apple-touch-icon', 'content_attr' => 'href'],
  75. ]);
  76. $a = array_merge($a, [
  77. 'image' => $a['OGImage'] ? $a['OGImage'] : $a['thumbnailUrl'],
  78. 'logo' => $a['icon2'] ? $a['icon2'] : ($a['icon3'] ? $a['icon3'] : $a['icon']),
  79. 'url' => $sUrl,
  80. 'domain' => parse_url($sUrl, PHP_URL_HOST)
  81. ]);
  82. unset($a['OGImage'], $a['thumbnailUrl'], $a['icon'], $a['icon2'], $a['icon3']);
  83. $this->_storeImages($a);
  84. return json_encode($a);
  85. }
  86. protected function _storeImages (&$a)
  87. {
  88. $oStorage = BxDolStorage::getObjectInstance('sys_images');
  89. if($oStorage === false)
  90. return;
  91. foreach(['image', 'logo'] as $sKey)
  92. if(($sMediaUrl = $a[$sKey] ?? false) && ($iMediaId = $oStorage->storeFileFromUrl($sMediaUrl, false))) {
  93. $a = array_merge($a, [
  94. $sKey . '_src' => $a[$sKey],
  95. $sKey . '_id' => $iMediaId
  96. ]);
  97. unset($a[$sKey]);
  98. }
  99. }
  100. }
  101. /** @} */