/modules/mailchimp/modules/mailchimp_sts/mailchimp_sts.class.php

https://github.com/GiveCampUK/SIS · PHP · 185 lines · 132 code · 27 blank · 26 comment · 19 complexity · a0bb5d941fa62fdcad0439131c350e86 MD5 · raw file

  1. <?php
  2. class MailChimpSTS {
  3. var $version = "1.0";
  4. var $errorMessage;
  5. var $errorCode;
  6. /**
  7. * Cache the information on the API location on the server
  8. */
  9. var $apiUrl;
  10. /**
  11. * Default to a 300 second timeout on server calls
  12. */
  13. var $timeout = 300;
  14. /**
  15. * Default to a 8K chunk size
  16. */
  17. var $chunkSize = 8192;
  18. /**
  19. * Cache the user api_key so we only have to log in once per client instantiation
  20. */
  21. var $api_key;
  22. /**
  23. * Cache the user api_key so we only have to log in once per client instantiation
  24. */
  25. var $secure = FALSE;
  26. /**
  27. * Connect to the MailChimp API for a given list.
  28. *
  29. * @param string $apikey Your MailChimp apikey
  30. * @param string $secure Whether or not this should use a secure connection
  31. */
  32. function MailChimpSTS($api_key, $secure = FALSE) {
  33. $this->secure = $secure;
  34. $this->apiUrl = parse_url("http://sts.mailchimp.com/" . $this->version . "/");
  35. $this->api_key = $api_key;
  36. }
  37. function set_timeout($seconds){
  38. if (is_int($seconds)){
  39. $this->timeout = $seconds;
  40. return true;
  41. }
  42. }
  43. function verify_email_address($email) {
  44. return $this->callServer("VerifyEmailAddress", array('email' => $email));
  45. }
  46. function list_verified_email_addresses() {
  47. return $this->callServer("ListVerifiedEmailAddresses");
  48. }
  49. function get_send_quota() {
  50. return $this->callServer("GetSendQuota");
  51. }
  52. function get_send_statistics() {
  53. return $this->callServer("GetSendStatistics");
  54. }
  55. function get_mc_send_stats($tag_id = 1, $since = NULL) {
  56. return $this->callServer("GetSendStats",
  57. array('tag_id' => $tag_id, 'since' => $since));
  58. }
  59. function get_tags() {
  60. return $this->callServer('GetTags');
  61. }
  62. function get_url_stats($url_id = NULL, $since = NULL) {
  63. return $this->callServer("GetUrlStats",
  64. array('url_id' => $url_id, 'since' => $since));
  65. }
  66. function get_urls() {
  67. return $this->callServer('GetUrls');
  68. }
  69. function send_email(array $message, $track_opens = TRUE, $track_clicks = TRUE, $tags = array()) {
  70. return $this->callServer("SendEmail", array(
  71. 'message' => $message,
  72. 'track_opens' => $track_opens,
  73. 'track_clicks' => $track_clicks,
  74. 'tags' => $tags
  75. ));
  76. }
  77. /**
  78. * Actually connect to the server and call the requested methods, parsing the result
  79. * You should never have to call this function manually
  80. */
  81. function callServer($method, $params = array()) {
  82. $dc = "us1";
  83. if (strstr($this->api_key, "-")) {
  84. list($key, $dc) = explode("-", $this->api_key, 2);
  85. if (!$dc) {
  86. $dc = "us1";
  87. }
  88. }
  89. $host = $dc . "." . $this->apiUrl["host"];
  90. $params["apikey"] = $this->api_key;
  91. $this->errorMessage = "";
  92. $this->errorCode = "";
  93. $sep_changed = FALSE;
  94. //sigh, apparently some distribs change this to &amp; by default
  95. if (ini_get("arg_separator.output") != "&") {
  96. $sep_changed = TRUE;
  97. $orig_sep = ini_get("arg_separator.output");
  98. ini_set("arg_separator.output", "&");
  99. }
  100. $post_vars = http_build_query($params);
  101. if ($sep_changed) {
  102. ini_set("arg_separator.output", $orig_sep);
  103. }
  104. $payload = "POST " . $this->apiUrl["path"] . '/' . $method . ".php HTTP/1.0\r\n";
  105. $payload .= "Host: " . $host . "\r\n";
  106. $payload .= "User-Agent: MCAPI/" . $this->version . "\r\n";
  107. $payload .= "Content-type: application/x-www-form-urlencoded\r\n";
  108. $payload .= "Content-length: " . strlen($post_vars) . "\r\n";
  109. $payload .= "Connection: close \r\n\r\n";
  110. $payload .= $post_vars;
  111. ob_start();
  112. if ($this->secure) {
  113. $sock = fsockopen("ssl://" . $host, 443, $errno, $errstr, 30);
  114. }
  115. else {
  116. $sock = fsockopen($host, 80, $errno, $errstr, 30);
  117. }
  118. if (!$sock) {
  119. $this->errorMessage = "Could not connect (ERR $errno: $errstr)";
  120. $this->errorCode = "-99";
  121. ob_end_clean();
  122. return FALSE;
  123. }
  124. $response = "";
  125. fwrite($sock, $payload);
  126. stream_set_timeout($sock, $this->timeout);
  127. $info = stream_get_meta_data($sock);
  128. while ((!feof($sock)) && (!$info["timed_out"])) {
  129. $response .= fread($sock, $this->chunkSize);
  130. $info = stream_get_meta_data($sock);
  131. }
  132. fclose($sock);
  133. ob_end_clean();
  134. if ($info["timed_out"]) {
  135. $this->errorMessage = "Could not read response (timed out)";
  136. $this->errorCode = -98;
  137. return FALSE;
  138. }
  139. list($headers, $response) = explode("\r\n\r\n", $response, 2);
  140. $headers = explode("\r\n", $headers);
  141. if (ini_get("magic_quotes_runtime")) {
  142. $response = stripslashes($response);
  143. }
  144. $serial = unserialize($response);
  145. if ($response && $serial === FALSE) {
  146. $response = array("error" => "Bad Response. Got This: " . $response, "code" => "-99");
  147. }
  148. else {
  149. $response = $serial;
  150. }
  151. if (is_array($response) && (isset($response["error"]) || isset($response["http_code"]))) {
  152. $this->errorCode = @$response["code"] | @$response["http_code"];
  153. $this->errorMessage = @$response["error"] . @$response["message"];
  154. return FALSE;
  155. }
  156. return $response;
  157. }
  158. }