PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/php/mandrill-api-php/src/Mandrill.php

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