BxDataFoxModule.php 4.3 KB

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