PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/class.eloquaLookup.php

https://github.com/j-lee/Eloqua-Lookup
PHP | 147 lines | 103 code | 14 blank | 30 comment | 10 complexity | 3b47176abad0554cdfe3ffe9047caa4a MD5 | raw file
  1. <?php
  2. /***************************************************************************************************
  3. *
  4. * Eloqua Server-Side Data Lookup v1.1 (August 23, 2010)
  5. * http://github.com/j-lee/Eloqua-Lookup
  6. *
  7. * Copyright 2010 James Lee
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License");
  10. * you may not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. **************************************************************************************************/
  21. class EloquaLookup {
  22. protected $lookupURL = 'http://now.eloqua.com/visitor/v200/svrGP.aspx?pps=50'; // eloqua's lookup url
  23. protected $jsdata = NULL; // javascript content returned from Eloqua
  24. public $data = NULL; // parse eloqua javascript data placed into array
  25. public $siteID = NULL; // int: site instance id on Eloqua
  26. public $key = NULL; // string: data lookup key
  27. public $criteria = NULL; // string|array: criteria for data lookup
  28. public $GUID = NULL; // string: GUID for visitor lookup
  29. public function __construct() {
  30. // check if curl extension is enabled
  31. if (function_exists('curl_init') === FALSE)
  32. die('The CURL Extension needs to be enabled to use Eloqua Lookup');
  33. }
  34. // returns data safely from data array by name (only valid for contacts/prospects/visitors lookup)
  35. public function getField($name) {
  36. return isset($this->data[$name]) ? $this->data[$name] : '';
  37. }
  38. // perform eloqua data lookup
  39. protected function eloquaLookup() {
  40. $url = $this->lookupURL;
  41. if (gettype($this->criteria) === 'string') {
  42. $url .= '&DLLookup='.urlencode($this->criteria);
  43. }
  44. elseif (gettype($this->criteria) === 'array') {
  45. $criteria = '';
  46. foreach ($this->criteria as $key=>$value) {
  47. $criteria .= '<'.$key.'>'.$value.'</'.$key.'>';
  48. }
  49. $url .= '&DLLookup='.urlencode($criteria);
  50. }
  51. $ch = curl_init();
  52. if ($this->GUID) {
  53. $header[] = 'Host: now.eloqua.com'; // necessary for Eloqua to read the cookie
  54. $header[] = 'Cookie: ELOQUA=GUID='.$this->GUID.'; ELQSTATUS=OK';
  55. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  56. }
  57. curl_setopt($ch, CURLOPT_URL, $url);
  58. curl_setopt($ch, CURLOPT_FAILONERROR, FALSE);
  59. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE); // disallow redirects
  60. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  61. $this->jsdata = curl_exec($ch);
  62. curl_close($ch);
  63. return $this->parseData($this->jsdata);
  64. }
  65. // parse javascript data from Eloqua
  66. protected function parseData($data) {
  67. // check if certain keywords exists in returned data. if so, valid data was return
  68. if (strpos($data, 'strTemp') > 0) { // indicates contacts/prospects/visitor lookup
  69. preg_match_all("/if\(strDataField == '(\w+)' \|\| strDataField == '(\w+)'\){strTemp = '(.*)';}/", $data, $matches, PREG_SET_ORDER);
  70. // put data into array
  71. if (count($matches) > 0) {
  72. $data = array();
  73. foreach ($matches as $match) {
  74. $value = preg_replace("#(\\\x[0-9A-F]{2})#e", "chr(hexdec('\\1'))", $match[3]); // converts utf-8 back to its printable character
  75. $data[$match[1]] = $value;
  76. $data[$match[2]] = $value;
  77. }
  78. $this->data = $data;
  79. return TRUE;
  80. }
  81. }
  82. elseif (strpos($data, 'blnTemp') > 0) { // indicates contact group memberships lookup
  83. preg_match_all("/if\(strContactGroupGUID.replace\('{', ''\).replace\('}', ''\).toLowerCase\(\) == '(.*)'\){blnTemp = true;}/", $data, $matches, PREG_SET_ORDER);
  84. if (count($matches) > 0) {
  85. $data = array();
  86. foreach ($matches as $match)
  87. $data[] = $match[1];
  88. $this->data = $data;
  89. return TRUE;
  90. }
  91. }
  92. else $this->jsdata = NULL;
  93. return FALSE;
  94. }
  95. }
  96. // class to initialize contact lookup
  97. class EloquaContactLookup extends EloquaLookup {
  98. public function __construct($siteID, $key, $criteria=NULL) {
  99. parent::__construct();
  100. $this->criteria = $criteria;
  101. $this->lookupURL = $this->lookupURL.'&siteid='.urlencode(intval($siteID)).'&DLKey='.urlencode($key).'&elqCookie=1';
  102. $this->eloquaLookup();
  103. }
  104. }
  105. // class to initialize prospect lookup
  106. class EloquaProspectLookup extends EloquaLookup {
  107. public function __construct($siteID, $key, $criteria=NULL) {
  108. parent::__construct();
  109. $this->criteria = $criteria;
  110. $this->lookupURL = $this->lookupURL.'&siteid='.urlencode(intval($siteID)).'&DLKey='.urlencode($key).'&elqCookie=1';
  111. $this->eloquaLookup();
  112. }
  113. }
  114. // class to initialize visitor lookup
  115. class EloquaVisitorLookup extends EloquaLookup {
  116. public function __construct($siteID, $key, $GUID=NULL, $criteria=NULL) {
  117. parent::__construct();
  118. $this->GUID = strtoupper( preg_replace('/[^a-fA-f0-9\s]/', '', $GUID) );
  119. $this->criteria = $criteria;
  120. $this->lookupURL = $this->lookupURL.'&siteid='.urlencode(intval($siteID)).'&DLKey='.urlencode($key);
  121. $this->eloquaLookup();
  122. }
  123. }
  124. // class to initialize contact group memberships lookup
  125. class EloquaMembershipsLookup extends EloquaLookup {
  126. public function __construct($siteID, $key, $criteria=NULL) {
  127. parent::__construct();
  128. $this->criteria = $criteria;
  129. $this->lookupURL = $this->lookupURL.'&siteid='.urlencode(intval($siteID)).'&DLKey='.urlencode($key).'&elqCookie=1';
  130. $this->eloquaLookup();
  131. }
  132. }