1
0

BxBaseEmbedSystem.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. protected $_sStorage;
  17. protected $_iProfileId;
  18. public function __construct ($aObject, $oTemplate)
  19. {
  20. $this->_sTableData = 'sys_embeded_data';
  21. parent::__construct ($aObject, $oTemplate);
  22. $this->_sStorage = 'sys_images';
  23. $this->_iProfileId = 0;
  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. $sImage = $sLogo = '';
  43. if(($oStorage = BxDolStorage::getObjectInstance($this->_sStorage)) !== false) {
  44. if(($iImageId = $aData['image_id'] ?? 0) || (($iImageId = $aData['image'] ?? '') && is_numeric($iImageId)))
  45. $sImage = $oStorage->getFileUrlById($iImageId);
  46. if(($iLogoId = $aData['logo_id'] ?? 0) || (($iLogoId = $aData['logo'] ?? '') && is_numeric($iLogoId)))
  47. $sLogo = $oStorage->getFileUrlById($iLogoId);
  48. }
  49. $sImage = $sImage ?: ($sLogo ?: $this->_oTemplate->getImageUrl('embed.svg'));
  50. return $this->_oTemplate->parseHtmlByName('embed_system_link.html', [
  51. 'link' => $aData['url'],
  52. 'attrs' => bx_convert_array2attrs($aAttrs),
  53. 'width' => $sMaxWidth,
  54. 'bx_if:show_image' => [
  55. 'condition' => (bool)$sImage,
  56. 'content' => [
  57. 'image' => $sImage
  58. ]
  59. ],
  60. 'bx_if:show_logo' => [
  61. 'condition' => (bool)$sLogo,
  62. 'content' => [
  63. 'logo' => $sLogo,
  64. ]
  65. ],
  66. 'title' => $aData['title'] ?? '',
  67. 'description' => $aData['description'] ?? '',
  68. 'domain' => $aData['domain'],
  69. ]);
  70. }
  71. public function getDataFromApi ($sUrl, $sTheme)
  72. {
  73. $a = bx_get_site_info($sUrl, [
  74. 'thumbnailUrl' => ['tag' => 'link', 'content_attr' => 'href'],
  75. 'OGImage' => ['name_attr' => 'property', 'name' => 'og:image'],
  76. 'icon' => ['tag' => 'link', 'name_attr' => 'rel', 'name' => 'shortcut icon', 'content_attr' => 'href'],
  77. 'icon2' => ['tag' => 'link', 'name_attr' => 'rel', 'name' => 'icon', 'content_attr' => 'href'],
  78. 'icon3' => ['tag' => 'link', 'name_attr' => 'rel', 'name' => 'apple-touch-icon', 'content_attr' => 'href'],
  79. ]);
  80. $a = array_merge($a, [
  81. 'image' => $a['OGImage'] ? $a['OGImage'] : $a['thumbnailUrl'],
  82. 'logo' => $a['icon2'] ? $a['icon2'] : ($a['icon3'] ? $a['icon3'] : $a['icon']),
  83. 'url' => $sUrl,
  84. 'domain' => parse_url($sUrl, PHP_URL_HOST)
  85. ]);
  86. unset($a['OGImage'], $a['thumbnailUrl'], $a['icon'], $a['icon2'], $a['icon3']);
  87. $this->_storeImages($a);
  88. return json_encode($a);
  89. }
  90. public function onGetDataFromApi ($iId)
  91. {
  92. if(!$this->_sTableData)
  93. return;
  94. $aLocal = $this->_oDb->getLocalInfoById($iId);
  95. if(!$aLocal || !is_array($aLocal))
  96. return;
  97. if(($aData = json_decode($aLocal['data'], true)) && is_array($aData)) {
  98. $oStorage = BxDolStorage::getObjectInstance($this->_sStorage);
  99. if($oStorage === false)
  100. return;
  101. foreach(['image_id', 'logo_id'] as $sKey)
  102. if(($iMediaId = $aData[$sKey] ?? false))
  103. $oStorage->updateGhostsContentId($iMediaId, $this->_iProfileId, $iId, true);
  104. }
  105. }
  106. protected function _storeImages (&$a)
  107. {
  108. $oStorage = BxDolStorage::getObjectInstance($this->_sStorage);
  109. if($oStorage === false)
  110. return;
  111. foreach(['image', 'logo'] as $sKey)
  112. if(($sMediaUrl = $a[$sKey] ?? false) && ($iMediaId = $oStorage->storeFileFromUrl($sMediaUrl, false, $this->_iProfileId))) {
  113. $a = array_merge($a, [
  114. $sKey . '_src' => $a[$sKey],
  115. $sKey . '_id' => $iMediaId
  116. ]);
  117. unset($a[$sKey]);
  118. }
  119. }
  120. }
  121. /** @} */