# File Mandrill.php

      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.52');
     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:         $this->root = rtrim($this->root, '/') . '/';
     75:
     76:         $this->templates = new Mandrill_Templates($this);
     77:         $this->exports = new Mandrill_Exports($this);
     78:         $this->users = new Mandrill_Users($this);
     79:         $this->rejects = new Mandrill_Rejects($this);
     80:         $this->inbound = new Mandrill_Inbound($this);
     81:         $this->tags = new Mandrill_Tags($this);
     82:         $this->messages = new Mandrill_Messages($this);
     83:         $this->whitelists = new Mandrill_Whitelists($this);
     84:         $this->ips = new Mandrill_Ips($this);
     85:         $this->internal = new Mandrill_Internal($this);
     86:         $this->subaccounts = new Mandrill_Subaccounts($this);
     87:         $this->urls = new Mandrill_Urls($this);
     88:         $this->webhooks = new Mandrill_Webhooks($this);
     89:         $this->senders = new Mandrill_Senders($this);
     90:         $this->metadata = new Mandrill_Metadata($this);
     91:     }
     92:
     93:     public function __destruct() {
     94:         curl_close($this->ch);
     95:     }
     96:
     97:     public function call($url, $params) {
     98:         $params['key'] = $this->apikey;
     99:         $params = json_encode($params);
    100:         $ch = $this->ch;
    101:
    102:         curl_setopt($ch, CURLOPT_URL, $this->root . $url . '.json');
    103:         curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    104:         curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    105:         curl_setopt($ch, CURLOPT_VERBOSE, $this->debug);
    106:
    107:         $start = microtime(true);
    108:         $this->log('Call to ' . $this->root . $url . '.json: ' . $params);
    109:         if($this->debug) {
    110:             $curl_buffer = fopen('php://memory', 'w+');
    111:             curl_setopt($ch, CURLOPT_STDERR, $curl_buffer);
    112:         }
    113:
    114:         $response_body = curl_exec($ch);
    115:         $info = curl_getinfo($ch);
    116:         $time = microtime(true) - $start;
    117:         if($this->debug) {
    118:             rewind($curl_buffer);
    119:             $this->log(stream_get_contents($curl_buffer));
    120:             fclose($curl_buffer);
    121:         }
    122:         $this->log('Completed in ' . number_format($time * 1000, 2) . 'ms');
    123:         $this->log('Got response: ' . $response_body);
    124:
    125:         if(curl_error($ch)) {
    126:             throw new Mandrill_HttpError("API call to $url failed: " . curl_error($ch));
    127:         }
    128:         $result = json_decode($response_body, true);
    129:         if($result === null) throw new Mandrill_Error('We were unable to decode the JSON response from the Mandrill API: ' . $response_body);
    130:
    131:         if(floor($info['http_code'] / 100) >= 4) {
    132:             throw $this->castError($result);
    133:         }
    134:
    135:         return $result;
    136:     }
    137:
    138:     public function readConfigs() {
    139:         $paths = array('~/.mandrill.key', '/etc/mandrill.key');
    140:         foreach($paths as $path) {
    141:             if(file_exists($path)) {
    142:                 $apikey = trim(file_get_contents($path));
    143:                 if($apikey) return $apikey;
    144:             }
    145:         }
    146:         return false;
    147:     }
    148:
    149:     public function castError($result) {
    150:         if($result['status'] !== 'error' || !$result['name']) throw new Mandrill_Error('We received an unexpected error: ' . json_encode($result));
    151:
    152:         $class = (isset(self::$error_map[$result['name']])) ? self::$error_map[$result['name']] : 'Mandrill_Error';
    153:         return new $class($result['message'], $result['code']);
    154:     }
    155:
    156:     public function log($msg) {
    157:         if($this->debug) error_log($msg);
    158:     }
    159: }
    160:
    161:
    162: