PageRenderTime 58ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/lib/mandrill/Mandrill.php

https://gitlab.com/x33n/platform
PHP | 118 lines | 88 code | 21 blank | 9 comment | 11 complexity | 671c74ff7534f430f8fad7f9502a1a00 MD5 | raw file
  1. <?php
  2. class Mandrill {
  3. public $apikey;
  4. public $ch;
  5. public $root = 'https://mandrillapp.com/api/1.0';
  6. public $debug = false;
  7. public static $error_map = array(
  8. "ValidationError" => "Mandrill_ValidationError",
  9. "Invalid_Key" => "Mandrill_Invalid_Key",
  10. "Unknown_Template" => "Mandrill_Unknown_Template",
  11. "Invalid_Tag_Name" => "Mandrill_Invalid_Tag_Name",
  12. "Invalid_Reject" => "Mandrill_Invalid_Reject",
  13. "Unknown_Sender" => "Mandrill_Unknown_Sender",
  14. "Unknown_Url" => "Mandrill_Unknown_Url",
  15. "Invalid_Template" => "Mandrill_Invalid_Template",
  16. "Unknown_Webhook" => "Mandrill_Unknown_Webhook",
  17. "Unknown_InboundDomain" => "Mandrill_Unknown_InboundDomain"
  18. );
  19. public function __construct($apikey=null) {
  20. if(!$apikey) $apikey = getenv('MANDRILL_APIKEY');
  21. if(!$apikey) $apikey = $this->readConfigs();
  22. if(!$apikey) throw new Mandrill_Error('You must provide a Mandrill API key');
  23. $this->apikey = $apikey;
  24. $this->ch = curl_init();
  25. curl_setopt($this->ch, CURLOPT_USERAGENT, 'like Mandrill-PHP/1.0.18');
  26. curl_setopt($this->ch, CURLOPT_POST, true);
  27. curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
  28. curl_setopt($this->ch, CURLOPT_HEADER, false);
  29. curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
  30. curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, 30);
  31. curl_setopt($this->ch, CURLOPT_TIMEOUT, 600);
  32. $this->root = rtrim($this->root, '/') . '/';
  33. }
  34. public function __destruct() {
  35. curl_close($this->ch);
  36. }
  37. public function call($url, $params) {
  38. $params['key'] = $this->apikey;
  39. $params = json_encode($params);
  40. $ch = $this->ch;
  41. curl_setopt($ch, CURLOPT_URL, $this->root . $url . '.json');
  42. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  43. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  44. curl_setopt($ch, CURLOPT_VERBOSE, $this->debug);
  45. $start = microtime(true);
  46. $this->log('Call to ' . $this->root . $url . '.json: ' . $params);
  47. if($this->debug) {
  48. $curl_buffer = fopen('php://memory', 'w+');
  49. curl_setopt($ch, CURLOPT_STDERR, $curl_buffer);
  50. }
  51. $response_body = curl_exec($ch);
  52. $info = curl_getinfo($ch);
  53. $time = microtime(true) - $start;
  54. if($this->debug) {
  55. rewind($curl_buffer);
  56. $this->log(stream_get_contents($curl_buffer));
  57. fclose($curl_buffer);
  58. }
  59. $this->log('Completed in ' . number_format($time * 1000, 2) . 'ms');
  60. $this->log('Got response: ' . $response_body);
  61. if(curl_error($ch)) {
  62. throw new Mandrill_HttpError("API call to $url failed: " . curl_error($ch));
  63. }
  64. $result = json_decode($response_body, true);
  65. if($result === null) throw new Mandrill_Error('We were unable to decode the JSON response from the Mandrill API: ' . $response_body);
  66. if(floor($info['http_code'] / 100) >= 4) {
  67. //throw $this->castError($result);
  68. }
  69. return $result;
  70. }
  71. public function readConfigs() {
  72. $paths = array('~/.mandrill.key', '/etc/mandrill.key');
  73. foreach($paths as $path) {
  74. if(file_exists($path)) {
  75. $apikey = trim(file_get_contents($path));
  76. if($apikey) return $apikey;
  77. }
  78. }
  79. return false;
  80. }
  81. public function castError($result) {
  82. //if($result['status'] !== 'error' || !$result['name']) throw new Mandrill_Error('We received an unexpected error: ' . json_encode($result));
  83. //$class = (isset(self::$error_map[$result['name']])) ? self::$error_map[$result['name']] : 'Mandrill_Error';
  84. //return new $class($result['message'], $result['code']);
  85. }
  86. public function log($msg) {
  87. if($this->debug) error_log($msg);
  88. }
  89. /***************************************************************************
  90. *
  91. * API ENDPOINT MAPPINGS
  92. *
  93. ***************************************************************************/
  94. public function getUserInfo() {
  95. return $this->call('users/info', array());
  96. }
  97. }