BxDataFoxModule.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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->getElementsByTagName('div')->item(0)))) // 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. 'bx_repeat:companies2' => $aCompanies,
  56. ));
  57. return $sHtml;
  58. }
  59. function apiGetCompanies($aLinks)
  60. {
  61. $sAccessToken = $this->apiGetAccessToken();
  62. $aCompanies = array();
  63. $iCounter = 0;
  64. foreach ($aLinks as $sId => &$sHref) {
  65. if (!($a = $this->apiGetCompanyByUrl($sAccessToken, $sHref, $aCompanies)))
  66. continue;
  67. $a['link_id'] = $sId;
  68. $a['item_number'] = ++$iCounter;
  69. unset($a['top_keywords']);
  70. $aCompanies[$a['id']] = $a;
  71. }
  72. return array_values($aCompanies);
  73. }
  74. function apiGetAccessToken()
  75. {
  76. // make request for token
  77. $sHttpCode = null;
  78. $s = bx_file_get_contents($this->_oConfig->sApiUrl . 'oauth2/token', array(
  79. 'grant_type' => 'client_credentials',
  80. 'scope' => 'full',
  81. ), 'post', array(), $sHttpCode, array(
  82. 'user' => getParam('bx_datafox_id'),
  83. 'password' => getParam('bx_datafox_secret'),
  84. ));
  85. if (!$s)
  86. return false;
  87. $o = json_decode($s);
  88. return $o && isset($o->access_token) ? $o->access_token : '';
  89. }
  90. function apiGetCompanyByUrl($sAccessToken, $sHref, $aCompanies)
  91. {
  92. $sHost = parse_url($sHref, PHP_URL_HOST);
  93. if (!$sHref)
  94. return false;
  95. $a = $this->api($sAccessToken, 'companies', array('url' => $sHost));
  96. if (!isset($a['total_count']) || !$a['total_count'] && $a['entries'])
  97. return false;
  98. $aCompany = array_pop($a['entries']);
  99. if (isset($aCompanies[$aCompany['id']]))
  100. return false;
  101. $aCompanyFull = $this->api($sAccessToken, 'companies/' . $aCompany['id'] . '/details');
  102. return $aCompanyFull;
  103. }
  104. function api($sAccessToken, $sUrlAppend, $aData = array())
  105. {
  106. $s = bx_file_get_contents($this->_oConfig->sApiUrl . $sUrlAppend, $aData, 'get', array(
  107. 'Authorization: Bearer ' . $sAccessToken,
  108. ));
  109. if (!$s || !($a = json_decode($s, true)) || (isset($a['object_type']) && 'error' == $a['object_type']))
  110. return false;
  111. return $a;
  112. }
  113. }
  114. /** @} */