tegarkurniawan

mandrill.php

Jul 12th, 2015
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 KB | None | 0 0
  1. <?php
  2.  
  3. require_once 'Mandrill/Templates.php';
  4. require_once 'Mandrill/Exports.php';
  5. require_once 'Mandrill/Users.php';
  6. require_once 'Mandrill/Rejects.php';
  7. require_once 'Mandrill/Inbound.php';
  8. require_once 'Mandrill/Tags.php';
  9. require_once 'Mandrill/Messages.php';
  10. require_once 'Mandrill/Whitelists.php';
  11. require_once 'Mandrill/Ips.php';
  12. require_once 'Mandrill/Internal.php';
  13. require_once 'Mandrill/Subaccounts.php';
  14. require_once 'Mandrill/Urls.php';
  15. require_once 'Mandrill/Webhooks.php';
  16. require_once 'Mandrill/Senders.php';
  17. require_once 'Mandrill/Metadata.php';
  18. require_once 'Mandrill/Exceptions.php';
  19.  
  20. class Mandrill {
  21.  
  22. public $apikey;
  23. public $ch;
  24. public $root = 'https://mandrillapp.com/api/1.0';
  25. public $debug = false;
  26.  
  27. public static $error_map = array(
  28. "ValidationError" => "Mandrill_ValidationError",
  29. "Invalid_Key" => "Mandrill_Invalid_Key",
  30. "PaymentRequired" => "Mandrill_PaymentRequired",
  31. "Unknown_Subaccount" => "Mandrill_Unknown_Subaccount",
  32. "Unknown_Template" => "Mandrill_Unknown_Template",
  33. "ServiceUnavailable" => "Mandrill_ServiceUnavailable",
  34. "Unknown_Message" => "Mandrill_Unknown_Message",
  35. "Invalid_Tag_Name" => "Mandrill_Invalid_Tag_Name",
  36. "Invalid_Reject" => "Mandrill_Invalid_Reject",
  37. "Unknown_Sender" => "Mandrill_Unknown_Sender",
  38. "Unknown_Url" => "Mandrill_Unknown_Url",
  39. "Unknown_TrackingDomain" => "Mandrill_Unknown_TrackingDomain",
  40. "Invalid_Template" => "Mandrill_Invalid_Template",
  41. "Unknown_Webhook" => "Mandrill_Unknown_Webhook",
  42. "Unknown_InboundDomain" => "Mandrill_Unknown_InboundDomain",
  43. "Unknown_InboundRoute" => "Mandrill_Unknown_InboundRoute",
  44. "Unknown_Export" => "Mandrill_Unknown_Export",
  45. "IP_ProvisionLimit" => "Mandrill_IP_ProvisionLimit",
  46. "Unknown_Pool" => "Mandrill_Unknown_Pool",
  47. "NoSendingHistory" => "Mandrill_NoSendingHistory",
  48. "PoorReputation" => "Mandrill_PoorReputation",
  49. "Unknown_IP" => "Mandrill_Unknown_IP",
  50. "Invalid_EmptyDefaultPool" => "Mandrill_Invalid_EmptyDefaultPool",
  51. "Invalid_DeleteDefaultPool" => "Mandrill_Invalid_DeleteDefaultPool",
  52. "Invalid_DeleteNonEmptyPool" => "Mandrill_Invalid_DeleteNonEmptyPool",
  53. "Invalid_CustomDNS" => "Mandrill_Invalid_CustomDNS",
  54. "Invalid_CustomDNSPending" => "Mandrill_Invalid_CustomDNSPending",
  55. "Metadata_FieldLimit" => "Mandrill_Metadata_FieldLimit",
  56. "Unknown_MetadataField" => "Mandrill_Unknown_MetadataField"
  57. );
  58.  
  59. public function __construct($apikey=null) {
  60. if(!$apikey) $apikey = getenv('MANDRILL_APIKEY');
  61. if(!$apikey) $apikey = $this->readConfigs();
  62. if(!$apikey) throw new Mandrill_Error('You must provide a Mandrill API key');
  63. $this->apikey = $apikey;
  64.  
  65. $this->ch = curl_init();
  66. curl_setopt($this->ch, CURLOPT_USERAGENT, 'Mandrill-PHP/1.0.54');
  67. curl_setopt($this->ch, CURLOPT_POST, true);
  68. curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
  69. curl_setopt($this->ch, CURLOPT_HEADER, false);
  70. curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
  71. curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, 30);
  72. curl_setopt($this->ch, CURLOPT_TIMEOUT, 600);
  73.  
  74. curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, 0);
  75. curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, 0);
  76.  
  77. $this->root = rtrim($this->root, '/') . '/';
  78.  
  79. $this->templates = new Mandrill_Templates($this);
  80. $this->exports = new Mandrill_Exports($this);
  81. $this->users = new Mandrill_Users($this);
  82. $this->rejects = new Mandrill_Rejects($this);
  83. $this->inbound = new Mandrill_Inbound($this);
  84. $this->tags = new Mandrill_Tags($this);
  85. $this->messages = new Mandrill_Messages($this);
  86. $this->whitelists = new Mandrill_Whitelists($this);
  87. $this->ips = new Mandrill_Ips($this);
  88. $this->internal = new Mandrill_Internal($this);
  89. $this->subaccounts = new Mandrill_Subaccounts($this);
  90. $this->urls = new Mandrill_Urls($this);
  91. $this->webhooks = new Mandrill_Webhooks($this);
  92. $this->senders = new Mandrill_Senders($this);
  93. $this->metadata = new Mandrill_Metadata($this);
  94. }
  95.  
  96. public function __destruct() {
  97. curl_close($this->ch);
  98. }
  99.  
  100. public function call($url, $params) {
  101. $params['key'] = $this->apikey;
  102. $params = json_encode($params);
  103. $ch = $this->ch;
  104.  
  105. curl_setopt($ch, CURLOPT_URL, $this->root . $url . '.json');
  106. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  107. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  108. curl_setopt($ch, CURLOPT_VERBOSE, $this->debug);
  109.  
  110. $start = microtime(true);
  111. $this->log('Call to ' . $this->root . $url . '.json: ' . $params);
  112. if($this->debug) {
  113. $curl_buffer = fopen('php://memory', 'w+');
  114. curl_setopt($ch, CURLOPT_STDERR, $curl_buffer);
  115. }
  116.  
  117. $response_body = curl_exec($ch);
  118. $info = curl_getinfo($ch);
  119. $time = microtime(true) - $start;
  120. if($this->debug) {
  121. rewind($curl_buffer);
  122. $this->log(stream_get_contents($curl_buffer));
  123. fclose($curl_buffer);
  124. }
  125. $this->log('Completed in ' . number_format($time * 1000, 2) . 'ms');
  126. $this->log('Got response: ' . $response_body);
  127.  
  128. if(curl_error($ch)) {
  129. throw new Mandrill_HttpError("API call to $url failed: " . curl_error($ch));
  130. }
  131. $result = json_decode($response_body, true);
  132. if($result === null) throw new Mandrill_Error('We were unable to decode the JSON response from the Mandrill API: ' . $response_body);
  133.  
  134. if(floor($info['http_code'] / 100) >= 4) {
  135. throw $this->castError($result);
  136. }
  137.  
  138. return $result;
  139. }
  140.  
  141. public function readConfigs() {
  142. $paths = array('~/.mandrill.key', '/etc/mandrill.key');
  143. foreach($paths as $path) {
  144. if(file_exists($path)) {
  145. $apikey = trim(file_get_contents($path));
  146. if($apikey) return $apikey;
  147. }
  148. }
  149. return false;
  150. }
  151.  
  152. public function castError($result) {
  153. if($result['status'] !== 'error' || !$result['name']) throw new Mandrill_Error('We received an unexpected error: ' . json_encode($result));
  154.  
  155. $class = (isset(self::$error_map[$result['name']])) ? self::$error_map[$result['name']] : 'Mandrill_Error';
  156. return new $class($result['message'], $result['code']);
  157. }
  158.  
  159. public function log($msg) {
  160. if($this->debug) error_log($msg);
  161. }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment