/** * Function to actually build the form * * @return None * @access public */ public function buildQuickForm() { $this->addFormRule(array('CRM_Mailchimp_Form_Setting', 'formRule'), $this); CRM_Core_Resources::singleton()->addStyleFile('uk.co.vedaconsulting.mailchimp', 'css/mailchimp.css'); $webhook_url = CRM_Utils_System::url('civicrm/mailchimp/webhook', 'reset=1', TRUE, NULL, FALSE, TRUE); $this->assign('webhook_url', 'Webhook URL - ' . $webhook_url); // Add the API Key Element $this->addElement('text', 'api_key', ts('API Key'), array('size' => 48)); // Add the User Security Key Element $this->addElement('text', 'security_key', ts('Security Key'), array('size' => 24)); // Add Enable or Disable Debugging $enableOptions = array(1 => ts('Yes'), 0 => ts('No')); $this->addRadio('enable_debugging', ts('Enable Debugging'), $enableOptions, NULL); // Create the Submit Button. $buttons = array(array('type' => 'submit', 'name' => ts('Save & Test'))); $groups = CRM_Mailchimp_Utils::getGroupsToSync(array(), null, $membership_only = TRUE); foreach ($groups as $group_id => $details) { $list = new Mailchimp_Lists(CRM_Mailchimp_Utils::mailchimp()); $webhookoutput = $list->webhooks($details['list_id']); if ($webhookoutput[0]['sources']['api'] == 1) { CRM_Mailchimp_Utils::checkDebug('CRM_Mailchimp_Form_Setting - API is set in Webhook setting for listID', $details['list_id']); $listID = $details['list_id']; CRM_Core_Session::setStatus(ts('API is set in Webhook setting for listID %1', array(1 => $listID)), ts('Error'), 'error'); break; } } // Add the Buttons. $this->addButtons($buttons); }
function run() { $my_key = CRM_Core_BAO_Setting::getItem(self::MC_SETTING_GROUP, 'security_key', NULL, FALSE); CRM_Mailchimp_Utils::checkDebug('CRM_Mailchimp_Page_WebHook run $my_key= ', $my_key); if (CRM_Core_Config::singleton()->userPermissionClass->isModulePermissionSupported() && !CRM_Mailchimp_Permission::check('allow webhook posts')) { CRM_Core_Error::fatal(); } // Check the key // @todo is this a DOS attack vector? seems a lot of work for saying 403, go away, to a robot! if (!isset($_GET['key']) || $_GET['key'] != $my_key) { CRM_Core_Error::fatal(); } if (!empty($_POST['data']['list_id']) && !empty($_POST['type'])) { $requestType = $_POST['type']; $requestData = $_POST['data']; // Return if API is set in webhook setting for lists $list = new Mailchimp_Lists(CRM_Mailchimp_Utils::mailchimp()); $webhookoutput = $list->webhooks($requestData['list_id']); if ($webhookoutput[0]['sources']['api'] == 1) { CRM_Mailchimp_Utils::checkDebug('CRM_Mailchimp_Page_WebHook run API is set in Webhook setting for listID', $requestData['list_id']); return; } switch ($requestType) { case 'subscribe': case 'unsubscribe': case 'profile': // Create/Update contact details in CiviCRM $delay = $requestType == 'profile'; $contactID = CRM_Mailchimp_Utils::updateContactDetails($requestData['merges'], $delay); $contactArray = array($contactID); // Subscribe/Unsubscribe to related CiviCRM groups self::manageCiviCRMGroupSubcription($contactID, $requestData, $requestType); CRM_Mailchimp_Utils::checkDebug('Start - CRM_Mailchimp_Page_WebHook run $_POST= ', $_POST); CRM_Mailchimp_Utils::checkDebug('Start - CRM_Mailchimp_Page_WebHook run $contactID= ', $contactID); CRM_Mailchimp_Utils::checkDebug('Start - CRM_Mailchimp_Page_WebHook run $requestData= ', $requestData); CRM_Mailchimp_Utils::checkDebug('Start - CRM_Mailchimp_Page_WebHook run $requestType= ', $requestType); break; case 'upemail': // Mailchimp Email Update event // Try to find the email address $email = new CRM_Core_BAO_Email(); $email->get('email', $requestData['old_email']); CRM_Mailchimp_Utils::checkDebug('CRM_Mailchimp_Page_WebHook run- case upemail $requestData[old_email]= ', $requestData['old_email']); // If the Email was found. if (!empty($email->contact_id)) { $email->email = $requestData['new_email']; $email->save(); CRM_Mailchimp_Utils::checkDebug('CRM_Mailchimp_Page_WebHook run- case upemail inside condition $requestData[new_email]= ', $requestData['new_email']); } break; case 'cleaned': // Try to find the email address $email = new CRM_Core_BAO_Email(); $email->get('email', $requestData['email']); CRM_Mailchimp_Utils::checkDebug('CRM_Mailchimp_Page_WebHook run - case cleaned $requestData[new_email]= ', $requestData['email']); // If the Email was found. if (!empty($email->contact_id)) { $email->on_hold = 1; $email->holdEmail($email); $email->save(); CRM_Mailchimp_Utils::checkDebug('CRM_Mailchimp_Page_WebHook run - case cleaned inside condition $email= ', $email); CRM_Mailchimp_Utils::checkDebug('CRM_Mailchimp_Page_WebHook run - case cleaned inside condition $requestData[new_email]= ', $requestData['email']); } break; default: // unhandled webhook CRM_Mailchimp_Utils::checkDebug('End- CRM_Mailchimp_Page_WebHook run $contactID= ', $contactID); CRM_Mailchimp_Utils::checkDebug('End- CRM_Mailchimp_Page_WebHook run $requestData= ', $requestData); CRM_Mailchimp_Utils::checkDebug('End- CRM_Mailchimp_Page_WebHook run $requestType= ', $requestType); CRM_Mailchimp_Utils::checkDebug('End - CRM_Mailchimp_Page_WebHook run $email= ', $email); } } // Return the JSON output header('Content-type: application/json'); $data = NULL; // We should ideally throw some status print json_encode($data); CRM_Utils_System::civiExit(); }
/** * CiviCRM to Mailchimp Sync * * @param array $params * @return array API result descriptor * @see civicrm_api3_create_success * @see civicrm_api3_create_error * @throws API_Exception */ function civicrm_api3_mailchimp_sync($params) { $groups = CRM_Mailchimp_Utils::getGroupsToSync(array(), null, $membership_only = TRUE); foreach ($groups as $group_id => $details) { $list = new Mailchimp_Lists(CRM_Mailchimp_Utils::mailchimp()); $webhookoutput = $list->webhooks($details['list_id']); if ($webhookoutput[0]['sources']['api'] == 1) { return civicrm_api3_create_error('civicrm_api3_mailchimp_sync - API is set in Webhook setting for listID ' . $details['list_id'] . ' Please uncheck API'); } } $result = $pullResult = array(); // Do pull first from mailchimp to CiviCRM $pullRunner = CRM_Mailchimp_Form_Pull::getRunner($skipEndUrl = TRUE); if ($pullRunner) { $pullResult = $pullRunner->runAll(); } // Do push from CiviCRM to mailchimp $runner = CRM_Mailchimp_Form_Sync::getRunner($skipEndUrl = TRUE); if ($runner) { $result = $runner->runAll(); } if ($pullResult['is_error'] == 0 && $result['is_error'] == 0) { return civicrm_api3_create_success(); } else { return civicrm_api3_create_error(); } }