BxBaseEmbedSystem.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 = $aData['image'] ?? '';
  39. $sLogo = $aData['logo'] ?? '';
  40. if(($oStorage = BxDolStorage::getObjectInstance('sys_images')) !== false) {
  41. if($sImage && is_numeric($sImage))
  42. $sImage = $oStorage->getFileUrlById($sImage);
  43. if($sLogo && is_numeric($sLogo))
  44. $sLogo = $oStorage->getFileUrlById($sLogo);
  45. }
  46. $sImage = $sImage ?: $sLogo;
  47. return $this->_oTemplate->parseHtmlByName('embed_system_link.html', [
  48. 'link' => $aData['url'],
  49. 'attrs' => bx_convert_array2attrs($aAttrs),
  50. 'width' => $sMaxWidth,
  51. 'bx_if:show_image' => [
  52. 'condition' => (bool)$sImage,
  53. 'content' => [
  54. 'image' => $sImage
  55. ]
  56. ],
  57. 'bx_if:show_logo' => [
  58. 'condition' => (bool)$sLogo,
  59. 'content' => [
  60. 'logo' => $sLogo,
  61. ]
  62. ],
  63. 'title' => $aData['title'] ?? '',
  64. 'description' => $aData['description'] ?? '',
  65. 'domain' => $aData['domain'],
  66. ]);
  67. }
  68. public function getDataFromApi ($sUrl, $sTheme)
  69. {
  70. $a = bx_get_site_info($sUrl, [
  71. 'thumbnailUrl' => ['tag' => 'link', 'content_attr' => 'href'],
  72. 'OGImage' => ['name_attr' => 'property', 'name' => 'og:image'],
  73. 'icon' => ['tag' => 'link', 'name_attr' => 'rel', 'name' => 'shortcut icon', 'content_attr' => 'href'],
  74. 'icon2' => ['tag' => 'link', 'name_attr' => 'rel', 'name' => 'icon', 'content_attr' => 'href'],
  75. 'icon3' => ['tag' => 'link', 'name_attr' => 'rel', 'name' => 'apple-touch-icon', 'content_attr' => 'href'],
  76. ]);
  77. $a = array_merge($a, [
  78. 'image' => $a['OGImage'] ? $a['OGImage'] : $a['thumbnailUrl'],
  79. 'logo' => $a['icon2'] ? $a['icon2'] : ($a['icon3'] ? $a['icon3'] : $a['icon']),
  80. 'url' => $sUrl,
  81. 'domain' => parse_url($sUrl, PHP_URL_HOST)
  82. ]);
  83. unset($a['OGImage'], $a['thumbnailUrl'], $a['icon'], $a['icon2'], $a['icon3']);
  84. $this->_storeImages($a);
  85. return json_encode($a);
  86. }
  87. protected function _storeImages (&$a)
  88. {
  89. $oStorage = BxDolStorage::getObjectInstance('sys_images');
  90. if($oStorage === false)
  91. return;
  92. foreach(['image', 'logo'] as $sKey)
  93. if(($sMediaUrl = $a[$sKey] ?? false) && ($iMediaId = $oStorage->storeFileFromUrl($sMediaUrl, false)))
  94. $a = array_merge($a, [
  95. $sKey . '_id' => $iMediaId,
  96. $sKey => $oStorage->getFileUrlById($iMediaId)
  97. ]);
  98. }
  99. }
  100. /** @} */