BxDataFoxModule.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 DataFox Data Fox API integration
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxDataFoxModule extends BxDolModule
  12. {
  13. function __construct(&$aModule)
  14. {
  15. parent::__construct($aModule);
  16. }
  17. public function serviceGetActions($aWidget)
  18. {
  19. $oInformer = BxDolInformer::getInstance(BxDolStudioTemplate::getInstance());
  20. $oInformer->add('sys-module-discontinued', _t('_adm_txt_modules_discontinued', BX_DOL_URL_STUDIO . 'design.php?name=bx_datafox', _t('_bx_datafox_adm_stg_cpt_type')), BX_INFORMER_ALERT);
  21. return bx_srv('system', 'get_actions', [$aWidget], 'TemplStudioDesigns');
  22. }
  23. function serviceIncludeCssJs()
  24. {
  25. $this->_oTemplate->addCss('datafox.css');
  26. }
  27. function serviceParseText($sHtml)
  28. {
  29. if (false === strstr($sHtml, '<a '))
  30. return $sHtml;
  31. $sId = 'bx-datafox-' . md5(microtime());
  32. // load html with wrapper
  33. $dom = new DOMDocument();
  34. @$dom->loadHTML('<?xml encoding="UTF-8"><div id="' . $sId . '">' . $sHtml . '</div>');
  35. $xpath = new DOMXpath($dom);
  36. // grab links, also add special classes
  37. $aLinks = array();
  38. $oLinks = $xpath->evaluate('//a');
  39. for ($i = 0; $i < $oLinks->length; $i++) {
  40. $oLink = $oLinks->item($i);
  41. $sIdLink = $sId . '_' . $i;
  42. $aLinks[$sIdLink] = $oLink->getAttribute('href');
  43. $sClasses = $oLink->getAttribute('class');
  44. $sClasses = ($sClasses ? preg_replace('/\s*bx-datafox-[a-zA-Z0-9]+_[0-9]+/', '', $sClasses) . ' ' : '') . $sIdLink;
  45. $oLink->removeAttribute('class');
  46. $oLink->setAttribute("class", $sClasses);
  47. }
  48. // remove old code with references
  49. $eCont = $dom->getElementById($sId);
  50. foreach ($xpath->query('//div[contains(attribute::class, "bx-datafox-companies")]', $eCont) as $e)
  51. $e->parentNode->removeChild($e);
  52. // save the result
  53. if (false === ($s = $dom->saveHTML($dom->getElementsByTagName('div')->item(0)))) // in case of error return original string
  54. return $sHtml;
  55. // strip added tags
  56. $sHtml = mb_substr($s, 54, -6);
  57. // add new code with references
  58. $aCompanies = $this->apiGetCompanies($aLinks);
  59. $sHtml .= $this->_oTemplate->parseHtmlByName('companies.html', array(
  60. 'bx_repeat:companies' => $aCompanies,
  61. 'bx_repeat:companies2' => $aCompanies,
  62. ));
  63. return $sHtml;
  64. }
  65. function apiGetCompanies($aLinks)
  66. {
  67. $sAccessToken = $this->apiGetAccessToken();
  68. $aCompanies = array();
  69. $iCounter = 0;
  70. foreach ($aLinks as $sId => &$sHref) {
  71. if (!($a = $this->apiGetCompanyByUrl($sAccessToken, $sHref, $aCompanies)))
  72. continue;
  73. $a['link_id'] = $sId;
  74. $a['item_number'] = ++$iCounter;
  75. unset($a['top_keywords']);
  76. $aCompanies[$a['id']] = $a;
  77. }
  78. return array_values($aCompanies);
  79. }
  80. function apiGetAccessToken()
  81. {
  82. // make request for token
  83. $sHttpCode = null;
  84. $s = bx_file_get_contents($this->_oConfig->sApiUrl . 'oauth2/token', array(
  85. 'grant_type' => 'client_credentials',
  86. 'scope' => 'full',
  87. ), 'post', array(), $sHttpCode, array(
  88. 'user' => getParam('bx_datafox_id'),
  89. 'password' => getParam('bx_datafox_secret'),
  90. ));
  91. if (!$s)
  92. return false;
  93. $o = json_decode($s);
  94. return $o && isset($o->access_token) ? $o->access_token : '';
  95. }
  96. function apiGetCompanyByUrl($sAccessToken, $sHref, $aCompanies)
  97. {
  98. $sHost = parse_url($sHref, PHP_URL_HOST);
  99. if (!$sHref)
  100. return false;
  101. $a = $this->api($sAccessToken, 'companies', array('url' => $sHost));
  102. if (!isset($a['total_count']) || !$a['total_count'] && $a['entries'])
  103. return false;
  104. $aCompany = array_pop($a['entries']);
  105. if (isset($aCompanies[$aCompany['id']]))
  106. return false;
  107. $aCompanyFull = $this->api($sAccessToken, 'companies/' . $aCompany['id'] . '/details');
  108. return $aCompanyFull;
  109. }
  110. function api($sAccessToken, $sUrlAppend, $aData = array())
  111. {
  112. $s = bx_file_get_contents($this->_oConfig->sApiUrl . $sUrlAppend, $aData, 'get', array(
  113. 'Authorization: Bearer ' . $sAccessToken,
  114. ));
  115. if (!$s || !($a = json_decode($s, true)) || (isset($a['object_type']) && 'error' == $a['object_type']))
  116. return false;
  117. return $a;
  118. }
  119. }
  120. /** @} */