1
0

BxBaseEmbedSystem.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. $aAttrs = [
  29. 'title' => bx_html_attribute($sTitle),
  30. ];
  31. // check for external link
  32. if (strncmp(BX_DOL_URL_ROOT, $sLink, strlen(BX_DOL_URL_ROOT)) !== 0) {
  33. $aAttrs['target'] = '_blank';
  34. if (getParam('sys_add_nofollow') == 'on')
  35. $aAttrs['rel'] = 'nofollow';
  36. }
  37. return $this->_oTemplate->parseHtmlByName('embed_system_link.html', [
  38. 'link' => $aData['url'],
  39. 'attrs' => bx_convert_array2attrs($aAttrs),
  40. 'width' => $sMaxWidth,
  41. 'image' => $aData['image'] ? $aData['image'] : $aData['logo'],
  42. 'logo' => $aData['logo'],
  43. 'title' => $aData['title'],
  44. 'description' => $aData['description'],
  45. 'domain' => $aData['domain'],
  46. ]);
  47. }
  48. public function getDataFromApi ($sUrl, $sTheme)
  49. {
  50. $a = bx_get_site_info($sUrl, [
  51. 'thumbnailUrl' => ['tag' => 'link', 'content_attr' => 'href'],
  52. 'OGImage' => ['name_attr' => 'property', 'name' => 'og:image'],
  53. 'icon' => ['tag' => 'link', 'name_attr' => 'rel', 'name' => 'shortcut icon', 'content_attr' => 'href'],
  54. 'icon2' => ['tag' => 'link', 'name_attr' => 'rel', 'name' => 'icon', 'content_attr' => 'href'],
  55. 'icon3' => ['tag' => 'link', 'name_attr' => 'rel', 'name' => 'apple-touch-icon', 'content_attr' => 'href'],
  56. ]);
  57. $a = array_merge($a, [
  58. 'image' => $a['OGImage'] ? $a['OGImage'] : $a['thumbnailUrl'],
  59. 'logo' => $a['icon2'] ? $a['icon2'] : ($a['icon3'] ? $a['icon3'] : $a['icon']),
  60. 'url' => $sUrl
  61. ]);
  62. unset($a['OGImage'], $a['thumbnailUrl'], $a['icon'], $a['icon2'], $a['icon3']);
  63. if($a['image'] && ($oStorage = BxDolStorage::getObjectInstance('sys_images')) !== false) {
  64. $iMediaId = $oStorage->storeFileFromUrl($a['image'], false);
  65. if($iMediaId)
  66. $a['image'] = $oStorage->getFileUrlById($iMediaId);
  67. }
  68. if($a['image'] == '') {
  69. $b = json_decode(bx_file_get_contents("https://api.microlink.io/?url=" . $sUrl), true);
  70. $a = [
  71. 'title' => $b['data']['title'],
  72. 'description' => $b['data']['description'],
  73. 'image' => $b['data']['image']['url'],
  74. 'logo' => $b['data']['logo']['url'],
  75. 'url' => $sUrl,
  76. ];
  77. }
  78. $aUrl = parse_url($sUrl);
  79. $a['domain'] = $aUrl['host'];
  80. return json_encode($a);
  81. }
  82. public function parseLinks(&$aLinks)
  83. {
  84. $aResult = [];
  85. if($aLinks && is_array($aLinks)) {
  86. $oEmbera = $this->getEmberaInstance();
  87. foreach ($aLinks as $sLink) {
  88. $sHtml = $oEmbera->autoEmbed($sLink);
  89. //if nothing to change/embed then return empty string to not replace anything on a frontend
  90. $aResult[] = ['html' => $sHtml != $sLink ? $sHtml : ''];
  91. }
  92. }
  93. return $aResult;
  94. }
  95. }
  96. /** @} */