      1: <?php
      2:
      3: require_once 'Mailchimp/Folders.php';
      4: require_once 'Mailchimp/Templates.php';
      5: require_once 'Mailchimp/Users.php';
      6: require_once 'Mailchimp/Helper.php';
      7: require_once 'Mailchimp/Mobile.php';
      8: require_once 'Mailchimp/Ecomm.php';
      9: require_once 'Mailchimp/Neapolitan.php';
     10: require_once 'Mailchimp/Lists.php';
     11: require_once 'Mailchimp/Campaigns.php';
     12: require_once 'Mailchimp/Vip.php';
     13: require_once 'Mailchimp/Reports.php';
     14: require_once 'Mailchimp/Gallery.php';
     15: require_once 'Mailchimp/Exceptions.php';
     16:
     17: class Mailchimp {
     18:
     19:     /**
     20:      * Placeholder attribute for Mailchimp_Folders class
     21:      *
     22:      * @var Mailchimp_Folders
     23:      * @access public
     24:      */
     25:     var $folders;
     26:     /**
     27:      * Placeholder attribute for Mailchimp_Templates class
     28:      *
     29:      * @var Mailchimp_Templates
     30:      * @access public
     31:      */
     32:     var $templates;
     33:     /**
     34:      * Placeholder attribute for Mailchimp_Users class
     35:      *
     36:      * @var Mailchimp_Users
     37:      * @access public
     38:      */
     39:     var $users;
     40:     /**
     41:      * Placeholder attribute for Mailchimp_Helper class
     42:      *
     43:      * @var Mailchimp_Helper
     44:      * @access public
     45:      */
     46:     var $helper;
     47:     /**
     48:      * Placeholder attribute for Mailchimp_Mobile class
     49:      *
     50:      * @var Mailchimp_Mobile
     51:      * @access public
     52:      */
     53:     var $mobile;
     54:     /**
     55:      * Placeholder attribute for Mailchimp_Ecomm class
     56:      *
     57:      * @var Mailchimp_Ecomm
     58:      * @access public
     59:      */
     60:     var $ecomm;
     61:     /**
     62:      * Placeholder attribute for Mailchimp_Neapolitan class
     63:      *
     64:      * @var Mailchimp_Neapolitan
     65:      * @access public
     66:      */
     67:     var $neapolitan;
     68:     /**
     69:      * Placeholder attribute for Mailchimp_Lists class
     70:      *
     71:      * @var Mailchimp_Lists
     72:      * @access public
     73:      */
     74:     var $lists;
     75:     /**
     76:      * Placeholder attribute for Mailchimp_Campaigns class
     77:      *
     78:      * @var Mailchimp_Campaigns
     79:      * @access public
     80:      */
     81:     var $campaigns;
     82:     /**
     83:      * Placeholder attribute for Mailchimp_Vip class
     84:      *
     85:      * @var Mailchimp_Vip
     86:      * @access public
     87:      */
     88:     var $vip;
     89:     /**
     90:      * Placeholder attribute for Mailchimp_Reports class
     91:      *
     92:      * @var Mailchimp_Reports
     93:      * @access public
     94:      */
     95:     var $reports;
     96:     /**
     97:      * Placeholder attribute for Mailchimp_Gallery class
     98:      *
     99:      * @var Mailchimp_Gallery
    100:      * @access public
    101:      */
    102:     var $gallery;
    103:
    104:     /**
    105:      * CURLOPT_SSL_VERIFYPEER setting
    106:      * @var  bool
    107:      */
    108:     public $ssl_verifypeer = true;
    109:     /**
    110:      * CURLOPT_SSL_VERIFYHOST setting
    111:      * @var  bool
    112:      */
    113:     public $ssl_verifyhost = 2;
    114:     /**
    115:      * CURLOPT_CAINFO
    116:      * @var  string
    117:      */
    118:     public $ssl_cainfo = null;
    119:
    120:     /**
    121:      * the api key in use
    122:      * @var  string
    123:      */
    124:     public $apikey;
    125:     public $ch;
    126:     public $root = 'https://api.mailchimp.com/2.0';
    127:     /**
    128:      * whether debug mode is enabled
    129:      * @var  bool
    130:      */
    131:     public $debug = false;
    132:
    133:     public static $error_map = array(
    134:         "ValidationError" => "Mailchimp_ValidationError",
    135:         "ServerError_MethodUnknown" => "Mailchimp_ServerError_MethodUnknown",
    136:         "ServerError_InvalidParameters" => "Mailchimp_ServerError_InvalidParameters",
    137:         "Unknown_Exception" => "Mailchimp_Unknown_Exception",
    138:         "Request_TimedOut" => "Mailchimp_Request_TimedOut",
    139:         "Zend_Uri_Exception" => "Mailchimp_Zend_Uri_Exception",
    140:         "PDOException" => "Mailchimp_PDOException",
    141:         "Avesta_Db_Exception" => "Mailchimp_Avesta_Db_Exception",
    142:         "XML_RPC2_Exception" => "Mailchimp_XML_RPC2_Exception",
    143:         "XML_RPC2_FaultException" => "Mailchimp_XML_RPC2_FaultException",
    144:         "Too_Many_Connections" => "Mailchimp_Too_Many_Connections",
    145:         "Parse_Exception" => "Mailchimp_Parse_Exception",
    146:         "User_Unknown" => "Mailchimp_User_Unknown",
    147:         "User_Disabled" => "Mailchimp_User_Disabled",
    148:         "User_DoesNotExist" => "Mailchimp_User_DoesNotExist",
    149:         "User_NotApproved" => "Mailchimp_User_NotApproved",
    150:         "Invalid_ApiKey" => "Mailchimp_Invalid_ApiKey",
    151:         "User_UnderMaintenance" => "Mailchimp_User_UnderMaintenance",
    152:         "Invalid_AppKey" => "Mailchimp_Invalid_AppKey",
    153:         "Invalid_IP" => "Mailchimp_Invalid_IP",
    154:         "User_DoesExist" => "Mailchimp_User_DoesExist",
    155:         "User_InvalidRole" => "Mailchimp_User_InvalidRole",
    156:         "User_InvalidAction" => "Mailchimp_User_InvalidAction",
    157:         "User_MissingEmail" => "Mailchimp_User_MissingEmail",
    158:         "User_CannotSendCampaign" => "Mailchimp_User_CannotSendCampaign",
    159:         "User_MissingModuleOutbox" => "Mailchimp_User_MissingModuleOutbox",
    160:         "User_ModuleAlreadyPurchased" => "Mailchimp_User_ModuleAlreadyPurchased",
    161:         "User_ModuleNotPurchased" => "Mailchimp_User_ModuleNotPurchased",
    162:         "User_NotEnoughCredit" => "Mailchimp_User_NotEnoughCredit",
    163:         "MC_InvalidPayment" => "Mailchimp_MC_InvalidPayment",
    164:         "List_DoesNotExist" => "Mailchimp_List_DoesNotExist",
    165:         "List_InvalidInterestFieldType" => "Mailchimp_List_InvalidInterestFieldType",
    166:         "List_InvalidOption" => "Mailchimp_List_InvalidOption",
    167:         "List_InvalidUnsubMember" => "Mailchimp_List_InvalidUnsubMember",
    168:         "List_InvalidBounceMember" => "Mailchimp_List_InvalidBounceMember",
    169:         "List_AlreadySubscribed" => "Mailchimp_List_AlreadySubscribed",
    170:         "List_NotSubscribed" => "Mailchimp_List_NotSubscribed",
    171:         "List_InvalidImport" => "Mailchimp_List_InvalidImport",
    172:         "MC_PastedList_Duplicate" => "Mailchimp_MC_PastedList_Duplicate",
    173:         "MC_PastedList_InvalidImport" => "Mailchimp_MC_PastedList_InvalidImport",
    174:         "Email_AlreadySubscribed" => "Mailchimp_Email_AlreadySubscribed",
    175:         "Email_AlreadyUnsubscribed" => "Mailchimp_Email_AlreadyUnsubscribed",
    176:         "Email_NotExists" => "Mailchimp_Email_NotExists",
    177:         "Email_NotSubscribed" => "Mailchimp_Email_NotSubscribed",
    178:         "List_MergeFieldRequired" => "Mailchimp_List_MergeFieldRequired",
    179:         "List_CannotRemoveEmailMerge" => "Mailchimp_List_CannotRemoveEmailMerge",
    180:         "List_Merge_InvalidMergeID" => "Mailchimp_List_Merge_InvalidMergeID",
    181:         "List_TooManyMergeFields" => "Mailchimp_List_TooManyMergeFields",
    182:         "List_InvalidMergeField" => "Mailchimp_List_InvalidMergeField",
    183:         "List_InvalidInterestGroup" => "Mailchimp_List_InvalidInterestGroup",
    184:         "List_TooManyInterestGroups" => "Mailchimp_List_TooManyInterestGroups",
    185:         "Campaign_DoesNotExist" => "Mailchimp_Campaign_DoesNotExist",
    186:         "Campaign_StatsNotAvailable" => "Mailchimp_Campaign_StatsNotAvailable",
    187:         "Campaign_InvalidAbsplit" => "Mailchimp_Campaign_InvalidAbsplit",
    188:         "Campaign_InvalidContent" => "Mailchimp_Campaign_InvalidContent",
    189:         "Campaign_InvalidOption" => "Mailchimp_Campaign_InvalidOption",
    190:         "Campaign_InvalidStatus" => "Mailchimp_Campaign_InvalidStatus",
    191:         "Campaign_NotSaved" => "Mailchimp_Campaign_NotSaved",
    192:         "Campaign_InvalidSegment" => "Mailchimp_Campaign_InvalidSegment",
    193:         "Campaign_InvalidRss" => "Mailchimp_Campaign_InvalidRss",
    194:         "Campaign_InvalidAuto" => "Mailchimp_Campaign_InvalidAuto",
    195:         "MC_ContentImport_InvalidArchive" => "Mailchimp_MC_ContentImport_InvalidArchive",
    196:         "Campaign_BounceMissing" => "Mailchimp_Campaign_BounceMissing",
    197:         "Campaign_InvalidTemplate" => "Mailchimp_Campaign_InvalidTemplate",
    198:         "Invalid_EcommOrder" => "Mailchimp_Invalid_EcommOrder",
    199:         "Absplit_UnknownError" => "Mailchimp_Absplit_UnknownError",
    200:         "Absplit_UnknownSplitTest" => "Mailchimp_Absplit_UnknownSplitTest",
    201:         "Absplit_UnknownTestType" => "Mailchimp_Absplit_UnknownTestType",
    202:         "Absplit_UnknownWaitUnit" => "Mailchimp_Absplit_UnknownWaitUnit",
    203:         "Absplit_UnknownWinnerType" => "Mailchimp_Absplit_UnknownWinnerType",
    204:         "Absplit_WinnerNotSelected" => "Mailchimp_Absplit_WinnerNotSelected",
    205:         "Invalid_Analytics" => "Mailchimp_Invalid_Analytics",
    206:         "Invalid_DateTime" => "Mailchimp_Invalid_DateTime",
    207:         "Invalid_Email" => "Mailchimp_Invalid_Email",
    208:         "Invalid_SendType" => "Mailchimp_Invalid_SendType",
    209:         "Invalid_Template" => "Mailchimp_Invalid_Template",
    210:         "Invalid_TrackingOptions" => "Mailchimp_Invalid_TrackingOptions",
    211:         "Invalid_Options" => "Mailchimp_Invalid_Options",
    212:         "Invalid_Folder" => "Mailchimp_Invalid_Folder",
    213:         "Invalid_URL" => "Mailchimp_Invalid_URL",
    214:         "Module_Unknown" => "Mailchimp_Module_Unknown",
    215:         "MonthlyPlan_Unknown" => "Mailchimp_MonthlyPlan_Unknown",
    216:         "Order_TypeUnknown" => "Mailchimp_Order_TypeUnknown",
    217:         "Invalid_PagingLimit" => "Mailchimp_Invalid_PagingLimit",
    218:         "Invalid_PagingStart" => "Mailchimp_Invalid_PagingStart",
    219:         "Max_Size_Reached" => "Mailchimp_Max_Size_Reached",
    220:         "MC_SearchException" => "Mailchimp_MC_SearchException"
    221:     );
    222:
    223:     public function __construct($apikey=null, $opts=array()) {
    224:         if(!$apikey) $apikey = getenv('MAILCHIMP_APIKEY');
    225:         if(!$apikey) $apikey = $this->readConfigs();
    226:         if(!$apikey) throw new Mailchimp_Error('You must provide a MailChimp API key');
    227:         $this->apikey = $apikey;
    228:         $dc = "us1";
    229:         if (strstr($this->apikey,"-")){
    230:             list($key, $dc) = explode("-",$this->apikey,2);
    231:             if (!$dc) $dc = "us1";
    232:         }
    233:         $this->root = str_replace('https://api', 'https://'.$dc.'.api', $this->root);
    234:         $this->root = rtrim($this->root, '/') . '/';
    235:
    236:         if (!isset($opts['timeout']) || !is_int($opts['timeout'])){
    237:             $opts['timeout']=600;
    238:         }
    239:         if (isset($opts['debug'])){
    240:             $this->debug = true;
    241:         }
    242:         if (isset($opts['ssl_verifypeer'])){
    243:             $this->ssl_verifypeer = $opts['ssl_verifypeer'];
    244:         }
    245:         if (isset($opts['ssl_verifyhost'])){
    246:             $this->ssl_verifyhost = $opts['ssl_verifyhost'];
    247:         }
    248:         if (isset($opts['ssl_cainfo'])){
    249:             $this->ssl_cainfo = $opts['ssl_cainfo'];
    250:         }
    251:
    252:
    253:         $this->ch = curl_init();
    254:         curl_setopt($this->ch, CURLOPT_USERAGENT, 'MailChimp-PHP/2.0.4');
    255:         curl_setopt($this->ch, CURLOPT_POST, true);
    256:         curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
    257:         curl_setopt($this->ch, CURLOPT_HEADER, false);
    258:         curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
    259:         curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, 30);
    260:         curl_setopt($this->ch, CURLOPT_TIMEOUT, $opts['timeout']);
    261:
    262:
    263:         $this->folders = new Mailchimp_Folders($this);
    264:         $this->templates = new Mailchimp_Templates($this);
    265:         $this->users = new Mailchimp_Users($this);
    266:         $this->helper = new Mailchimp_Helper($this);
    267:         $this->mobile = new Mailchimp_Mobile($this);
    268:         $this->ecomm = new Mailchimp_Ecomm($this);
    269:         $this->neapolitan = new Mailchimp_Neapolitan($this);
    270:         $this->lists = new Mailchimp_Lists($this);
    271:         $this->campaigns = new Mailchimp_Campaigns($this);
    272:         $this->vip = new Mailchimp_Vip($this);
    273:         $this->reports = new Mailchimp_Reports($this);
    274:         $this->gallery = new Mailchimp_Gallery($this);
    275:     }
    276:
    277:     public function __destruct() {
    278:         curl_close($this->ch);
    279:     }
    280:
    281:     public function call($url, $params) {
    282:         $params['apikey'] = $this->apikey;
    283:         $params = json_encode($params);
    284:         $ch = $this->ch;
    285:
    286:         curl_setopt($ch, CURLOPT_URL, $this->root . $url . '.json');
    287:         curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    288:         curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    289:         curl_setopt($ch, CURLOPT_VERBOSE, $this->debug);
    290:         // SSL Options
    291:         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
    292:         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $this->ssl_verifyhost);
    293:         if ($this->ssl_cainfo) curl_setopt($ch, CURLOPT_CAINFO, $this->ssl_cainfo);
    294:
    295:         $start = microtime(true);
    296:         $this->log('Call to ' . $this->root . $url . '.json: ' . $params);
    297:         if($this->debug) {
    298:             $curl_buffer = fopen('php://memory', 'w+');
    299:             curl_setopt($ch, CURLOPT_STDERR, $curl_buffer);
    300:         }
    301:
    302:         $response_body = curl_exec($ch);
    303:         $info = curl_getinfo($ch);
    304:         $time = microtime(true) - $start;
    305:         if($this->debug) {
    306:             rewind($curl_buffer);
    307:             $this->log(stream_get_contents($curl_buffer));
    308:             fclose($curl_buffer);
    309:         }
    310:         $this->log('Completed in ' . number_format($time * 1000, 2) . 'ms');
    311:         $this->log('Got response: ' . $response_body);
    312:
    313:         if(curl_error($ch)) {
    314:             throw new Mailchimp_HttpError("API call to $url failed: " . curl_error($ch));
    315:         }
    316:         $result = json_decode($response_body, true);
    317:
    318:         if(floor($info['http_code'] / 100) >= 4) {
    319:             throw $this->castError($result);
    320:         }
    321:
    322:         return $result;
    323:     }
    324:
    325:     public function readConfigs() {
    326:         $paths = array('~/.mailchimp.key', '/etc/mailchimp.key');
    327:         foreach($paths as $path) {
    328:             if(file_exists($path)) {
    329:                 $apikey = trim(file_get_contents($path));
    330:                 if($apikey) return $apikey;
    331:             }
    332:         }
    333:         return false;
    334:     }
    335:
    336:     public function castError($result) {
    337:         if($result['status'] !== 'error' || !$result['name']) throw new Mailchimp_Error('We received an unexpected error: ' . json_encode($result));
    338:
    339:         $class = (isset(self::$error_map[$result['name']])) ? self::$error_map[$result['name']] : 'Mailchimp_Error';
    340:         return new $class($result['error'], $result['code']);
    341:     }
    342:
    343:     public function log($msg) {
    344:         if($this->debug) error_log($msg);
    345:     }
    346: }
    347:
    348:
    349: