BxBaseEmbedPeekalink.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. * Peekalink integration.
  11. *
  12. * @see BxDolEmbed
  13. */
  14. class BxBaseEmbedPeekalink extends BxTemplEmbedSystem
  15. {
  16. protected $_sEndpoint;
  17. public function __construct ($aObject, $oTemplate)
  18. {
  19. parent::__construct ($aObject, $oTemplate);
  20. $this->_bAsync = true;
  21. $this->_sEndpoint = 'https://api.peekalink.io/';
  22. }
  23. public function getDataFromApi ($sUrl, $sTheme)
  24. {
  25. $sResponse = '';
  26. if(($sApiKey = getParam('sys_embed_peekalink_api_key')) != '')
  27. $sResponse = bx_file_get_contents($this->_sEndpoint, ['link' => $sUrl], 'post-json', [
  28. 'Authorization: Bearer ' . $sApiKey
  29. ]);
  30. $aResponse = [];
  31. if(!$sResponse || !($aResponse = json_decode($sResponse, true)) || !is_array($aResponse))
  32. return '';
  33. $aResult = [
  34. 'title' => $aResponse['title'],
  35. 'description' => $aResponse['description'],
  36. 'image' => '',
  37. 'logo' => '',
  38. 'url' => $aResponse['url'],
  39. 'domain' => $aResponse['domain']
  40. ];
  41. $aIs = array_flip(['thumbnail', 'medium', 'large', 'original']);
  42. if(($aP = $aResponse['page'] ?? false) && ($aPs = $aP['screenshot'] ?? false) && ($aPs = array_intersect_key($aPs, $aIs)) && (($aImg = $aPs['medium'] ?? false) || ($aImg = reset($aPs))))
  43. $aResult['image'] = $aImg['url'];
  44. if(($aI = $aResponse['image'] ?? false) && ($aI = array_intersect_key($aI, $aIs)) && (($aImg = $aI['thumbnail'] ?? false) || ($aImg = reset($aI))))
  45. $aResult['logo'] = $aImg['url'];
  46. $this->_storeImages($aResult);
  47. return json_encode($aResult);
  48. }
  49. }
  50. /** @} */