Connector.class.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. class AC_Connector {
  3. public $url;
  4. public $api_key;
  5. public $output = "json";
  6. function __construct($url, $api_key, $api_user = "", $api_pass = "") {
  7. // $api_pass should be md5() already
  8. $base = "";
  9. if (!preg_match("/https:\/\/www.activecampaign.com/", $url)) {
  10. // not a reseller
  11. $base = "/admin";
  12. }
  13. if (preg_match("/\/$/", $url)) {
  14. // remove trailing slash
  15. $url = substr($url, 0, strlen($url) - 1);
  16. }
  17. if ($api_key) {
  18. $this->url = "{$url}{$base}/api.php?api_key={$api_key}";
  19. }
  20. elseif ($api_user && $api_pass) {
  21. $this->url = "{$url}{$base}/api.php?api_user={$api_user}&api_pass={$api_pass}";
  22. }
  23. $this->api_key = $api_key;
  24. }
  25. public function credentials_test() {
  26. $test_url = "{$this->url}&api_action=user_me&api_output={$this->output}";
  27. $r = $this->curl($test_url);
  28. if (is_object($r) && (int)$r->result_code) {
  29. // successful
  30. $r = true;
  31. }
  32. else {
  33. // failed
  34. $r = false;
  35. }
  36. return $r;
  37. }
  38. // debug function (nicely outputs variables)
  39. public function dbg($var, $continue = 0, $element = "pre", $extra = "") {
  40. echo "<" . $element . ">";
  41. echo "Vartype: " . gettype($var) . "\n";
  42. if ( is_array($var) ) echo "Elements: " . count($var) . "\n";
  43. elseif ( is_string($var) ) echo "Length: " . strlen($var) . "\n";
  44. if ($extra) {
  45. echo $extra . "\n";
  46. }
  47. echo "\n";
  48. print_r($var);
  49. echo "</" . $element . ">";
  50. if (!$continue) exit();
  51. }
  52. public function curl($url, $params_data = array(), $verb = "", $custom_method = "") {
  53. if ($this->version == 1) {
  54. // find the method from the URL.
  55. $method = preg_match("/api_action=[^&]*/i", $url, $matches);
  56. if ($matches) {
  57. $method = preg_match("/[^=]*$/i", $matches[0], $matches2);
  58. $method = $matches2[0];
  59. } elseif ($custom_method) {
  60. $method = $custom_method;
  61. }
  62. } elseif ($this->version == 2) {
  63. $method = $custom_method;
  64. $url .= "?api_key=" . $this->api_key;
  65. }
  66. $debug_str1 = "";
  67. $request = curl_init();
  68. $debug_str1 .= "\$ch = curl_init();\n";
  69. curl_setopt($request, CURLOPT_HEADER, 0);
  70. curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
  71. $debug_str1 .= "curl_setopt(\$ch, CURLOPT_HEADER, 0);\n";
  72. $debug_str1 .= "curl_setopt(\$ch, CURLOPT_RETURNTRANSFER, true);\n";
  73. if ($params_data && $verb == "GET") {
  74. if ($this->version == 2) {
  75. $url .= "&" . $params_data;
  76. curl_setopt($request, CURLOPT_URL, $url);
  77. }
  78. }
  79. else {
  80. curl_setopt($request, CURLOPT_URL, $url);
  81. if ($params_data && !$verb) {
  82. // if no verb passed but there IS params data, it's likely POST.
  83. $verb = "POST";
  84. } elseif ($params_data && $verb) {
  85. // $verb is likely "POST" or "PUT".
  86. } else {
  87. $verb = "GET";
  88. }
  89. }
  90. $debug_str1 .= "curl_setopt(\$ch, CURLOPT_URL, \"" . $url . "\");\n";
  91. if ($this->debug) {
  92. $this->dbg($url, 1, "pre", "Description: Request URL");
  93. }
  94. if ($verb == "POST" || $verb == "PUT" || $verb == "DELETE") {
  95. if ($verb == "PUT") {
  96. curl_setopt($request, CURLOPT_CUSTOMREQUEST, "PUT");
  97. $debug_str1 .= "curl_setopt(\$ch, CURLOPT_CUSTOMREQUEST, \"PUT\");\n";
  98. } elseif ($verb == "DELETE") {
  99. curl_setopt($request, CURLOPT_CUSTOMREQUEST, "DELETE");
  100. $debug_str1 .= "curl_setopt(\$ch, CURLOPT_CUSTOMREQUEST, \"DELETE\");\n";
  101. } else {
  102. $verb = "POST";
  103. curl_setopt($request, CURLOPT_POST, 1);
  104. $debug_str1 .= "curl_setopt(\$ch, CURLOPT_POST, 1);\n";
  105. }
  106. $data = "";
  107. if (is_array($params_data)) {
  108. foreach($params_data as $key => $value) {
  109. if (is_array($value)) {
  110. if (is_int($key)) {
  111. // array two levels deep
  112. foreach ($value as $key_ => $value_) {
  113. if (is_array($value_)) {
  114. foreach ($value_ as $k => $v) {
  115. $k = urlencode($k);
  116. $data .= "{$key_}[{$key}][{$k}]=" . urlencode($v) . "&";
  117. }
  118. }
  119. else {
  120. $data .= "{$key_}[{$key}]=" . urlencode($value_) . "&";
  121. }
  122. }
  123. }
  124. else {
  125. // IE: [group] => array(2 => 2, 3 => 3)
  126. // normally we just want the key to be a string, IE: ["group[2]"] => 2
  127. // but we want to allow passing both formats
  128. foreach ($value as $k => $v) {
  129. if (!is_array($v)) {
  130. $k = urlencode($k);
  131. $data .= "{$key}[{$k}]=" . urlencode($v) . "&";
  132. }
  133. }
  134. }
  135. }
  136. else {
  137. $data .= "{$key}=" . urlencode($value) . "&";
  138. }
  139. }
  140. }
  141. else {
  142. // not an array - perhaps serialized or JSON string?
  143. // just pass it as data
  144. $data = "data={$params_data}";
  145. }
  146. $data = rtrim($data, "& ");
  147. curl_setopt($request, CURLOPT_HTTPHEADER, array("Expect:"));
  148. $debug_str1 .= "curl_setopt(\$ch, CURLOPT_HTTPHEADER, array(\"Expect:\"));\n";
  149. if ($this->debug) {
  150. curl_setopt($request, CURLINFO_HEADER_OUT, 1);
  151. $debug_str1 .= "curl_setopt(\$ch, CURLINFO_HEADER_OUT, 1);\n";
  152. $this->dbg($data, 1, "pre", "Description: POST data");
  153. }
  154. curl_setopt($request, CURLOPT_POSTFIELDS, $data);
  155. $debug_str1 .= "curl_setopt(\$ch, CURLOPT_POSTFIELDS, \"" . $data . "\");\n";
  156. }
  157. curl_setopt($request, CURLOPT_SSL_VERIFYPEER, false);
  158. curl_setopt($request, CURLOPT_SSL_VERIFYHOST, 0);
  159. curl_setopt($request, CURLOPT_FOLLOWLOCATION, true);
  160. $debug_str1 .= "curl_setopt(\$ch, CURLOPT_SSL_VERIFYPEER, false);\n";
  161. $debug_str1 .= "curl_setopt(\$ch, CURLOPT_SSL_VERIFYHOST, 0);\n";
  162. $debug_str1 .= "curl_setopt(\$ch, CURLOPT_FOLLOWLOCATION, true);\n";
  163. $response = curl_exec($request);
  164. $debug_str1 .= "curl_exec(\$ch);\n";
  165. if ($this->debug) {
  166. $this->dbg($response, 1, "pre", "Description: Raw response");
  167. }
  168. $http_code = curl_getinfo($request, CURLINFO_HTTP_CODE);
  169. $debug_str1 .= "\$http_code = curl_getinfo(\$ch, CURLINFO_HTTP_CODE);\n";
  170. if ($this->debug) {
  171. $this->dbg($http_code, 1, "pre", "Description: Response HTTP code");
  172. $request_headers = curl_getinfo($request, CURLINFO_HEADER_OUT);
  173. $debug_str1 .= "\$request_headers = curl_getinfo(\$ch, CURLINFO_HEADER_OUT);\n";
  174. $this->dbg($request_headers, 1, "pre", "Description: Request headers");
  175. }
  176. curl_close($request);
  177. $debug_str1 .= "curl_close(\$ch);\n";
  178. $object = json_decode($response);
  179. if ($this->debug) {
  180. $this->dbg($object, 1, "pre", "Description: Response object (json_decode)");
  181. }
  182. if ( !is_object($object) || (!isset($object->result_code) && !isset($object->succeeded) && !isset($object->success)) ) {
  183. // add methods that only return a string
  184. $string_responses = array("tracking_event_remove", "contact_list", "form_html", "tracking_site_status", "tracking_event_status", "tracking_whitelist", "tracking_log", "tracking_site_list", "tracking_event_list");
  185. if (in_array($method, $string_responses)) {
  186. return $response;
  187. }
  188. // something went wrong
  189. return "An unexpected problem occurred with the API request. Some causes include: invalid JSON or XML returned. Here is the actual response from the server: ---- " . $response;
  190. }
  191. if ($this->debug) {
  192. echo "<textarea style='height: 300px; width: 600px;'>" . $debug_str1 . "</textarea>";
  193. }
  194. header("HTTP/1.1 " . $http_code);
  195. $object->http_code = $http_code;
  196. if (isset($object->result_code)) {
  197. $object->success = $object->result_code;
  198. if (!(int)$object->result_code) {
  199. $object->error = $object->result_message;
  200. }
  201. }
  202. elseif (isset($object->succeeded)) {
  203. // some calls return "succeeded" only
  204. $object->success = $object->succeeded;
  205. if (!(int)$object->succeeded) {
  206. $object->error = $object->message;
  207. }
  208. }
  209. return $object;
  210. }
  211. }
  212. ?>