<commit_before><?php
class CRM_Mailchimp_Page_WebHook extends CRM_Core_Page {
    
  const 
    MC_SETTING_GROUP = 'MailChimp Preferences';
     
  function run() {
    $my_key = CRM_Core_BAO_Setting::getItem(self::MC_SETTING_GROUP,
      'security_key', NULL, FALSE
    );
    
    // Check the key
    try{
      if(!isset($_GET['key']) || $_GET['key'] != $my_key ) {
      throw new Exception("No security key provided or not match");
      }
    }
    catch (Exception $e) {
      CRM_Core_Session::setStatus($e->getMessage());
      return FALSE;
    }      
    // Check the security key to run webhook
    if (isset($_GET['key']) && $_GET['key']==$my_key) {

      if(!empty($_POST['data']['list_id']) && !empty($_POST['type'])) {
        $requestType = $_POST['type'];
        $requestData = $_POST['data'];

        // Mailchimp Subscribe event
        if($requestType == 'subscribe' OR $requestType == 'unsubscribe') {

          // Create/Update contact details in CiviCRM
          $contactID = CRM_Mailchimp_Utils::updateContactDetails($requestData['merges']);
          $contactArray = array($contactID);

          // Subscribe/Unsubscribe to related CiviCRM groups 
          self::manageCiviCRMGroupSubcription($contactArray , $requestData , $requestType);
        }

        // Mailchimp Email Update event
        else if($requestType == 'profile') {
            
          // Create/Update contact details in CiviCRM
          $contactID = CRM_Mailchimp_Utils::updateContactDetails($requestData['merges'], TRUE);
          $contactArray = array($contactID);

          $listID = $requestData['list_id'];
          $mcGroupings = $requestData['merges']['GROUPINGS'];

          // Get the associated CiviCRM Group IDs for the Mailchimp List & Grouping
          $civiGroups = CRM_Mailchimp_Utils::getCiviGroupIdsforMcGroupings($listID, $mcGroupings);
          // Get all CiviCRM groups which are mapped to the Mailchimp List, to which the contact is added to
          $contactGroups = CRM_Mailchimp_Utils::getGroupSubscriptionforMailchimpList($listID , $contactID);

          // Contact is added to any group in Mailchimp, we also need to add the contact to CiviCRM group
          $addedGroups = array_diff($civiGroups , $contactGroups);
          if (!empty($addedGroups)) {
            foreach ($addedGroups as $key => $groupID) {
              CRM_Contact_BAO_GroupContact::addContactsToGroup($contactArray, $groupID, 'Admin', 'Added');
            }
          }

          // Contact is removed from any group in Mailchimp, we also need to remove the contact from CiviCRM group
          $removedGroups = array_diff($contactGroups, $civiGroups);
          if (!empty($removedGroups)) {
            foreach ($removedGroups as $key => $groupID) {
              CRM_Contact_BAO_GroupContact::removeContactsFromGroup($contactArray, $groupID, 'Admin', 'Removed');
            }
          }
        }

        // Mailchimp Email Update event
        else if($requestType == 'upemail') {
          // Try to find the email address
          $email = new CRM_Core_BAO_Email();
          $email->get('email', $requestData['old_email']);

          // If the Email was found.
          if (!empty($email->contact_id)) {
            $email->email = $requestData['new_email'];
            $email->save();
          }
        }

        // Mailchimp Cleaned Email  event
        else if($requestType == 'cleaned') {
          // Try to find the email address
          $email = new CRM_Core_BAO_Email();
          $email->get('email', $requestData['email']);

          // If the Email was found.
          if (!empty($email->contact_id)) {
            $email->on_hold = 1;
            $email->holdEmail($email);
          }
        }
      }
    }

    // Return the JSON output
    header('Content-type: application/json');
    print json_encode($data);
    CRM_Utils_System::civiExit();
  } 
  
  /*
   * Add/Remove contact from CiviCRM Groups mapped with Mailchimp List & Groups 
   */
  static function manageCiviCRMGroupSubcription($contactID = array(), $requestData , $action) {
    if (empty($contactID) || empty($requestData['merges']['GROUPINGS']) || empty($requestData['list_id']) || empty($action)) {
      return NULL;
    }
    
    $listID = $requestData['list_id'];<commit_msg>Undefined variable: data in webhook page<commit_after><?php
class CRM_Mailchimp_Page_WebHook extends CRM_Core_Page {
    
  const 
    MC_SETTING_GROUP = 'MailChimp Preferences';
     
  function run() {
    $my_key = CRM_Core_BAO_Setting::getItem(self::MC_SETTING_GROUP,
      'security_key', NULL, FALSE
    );
    
    // Check the key
    try{
      if(!isset($_GET['key']) || $_GET['key'] != $my_key ) {
      throw new Exception("No security key provided or not match");
      }
    }
    catch (Exception $e) {
      CRM_Core_Session::setStatus($e->getMessage());
      return FALSE;
    }      
    // Check the security key to run webhook
    if (isset($_GET['key']) && $_GET['key']==$my_key) {

      if(!empty($_POST['data']['list_id']) && !empty($_POST['type'])) {
        $requestType = $_POST['type'];
        $requestData = $_POST['data'];

        // Mailchimp Subscribe event
        if($requestType == 'subscribe' OR $requestType == 'unsubscribe') {

          // Create/Update contact details in CiviCRM
          $contactID = CRM_Mailchimp_Utils::updateContactDetails($requestData['merges']);
          $contactArray = array($contactID);

          // Subscribe/Unsubscribe to related CiviCRM groups 
          self::manageCiviCRMGroupSubcription($contactArray , $requestData , $requestType);
        }

        // Mailchimp Email Update event
        else if($requestType == 'profile') {
            
          // Create/Update contact details in CiviCRM
          $contactID = CRM_Mailchimp_Utils::updateContactDetails($requestData['merges'], TRUE);
          $contactArray = array($contactID);

          $listID = $requestData['list_id'];
          $mcGroupings = $requestData['merges']['GROUPINGS'];

          // Get the associated CiviCRM Group IDs for the Mailchimp List & Grouping
          $civiGroups = CRM_Mailchimp_Utils::getCiviGroupIdsforMcGroupings($listID, $mcGroupings);
          // Get all CiviCRM groups which are mapped to the Mailchimp List, to which the contact is added to
          $contactGroups = CRM_Mailchimp_Utils::getGroupSubscriptionforMailchimpList($listID , $contactID);

          // Contact is added to any group in Mailchimp, we also need to add the contact to CiviCRM group
          $addedGroups = array_diff($civiGroups , $contactGroups);
          if (!empty($addedGroups)) {
            foreach ($addedGroups as $key => $groupID) {
              CRM_Contact_BAO_GroupContact::addContactsToGroup($contactArray, $groupID, 'Admin', 'Added');
            }
          }

          // Contact is removed from any group in Mailchimp, we also need to remove the contact from CiviCRM group
          $removedGroups = array_diff($contactGroups, $civiGroups);
          if (!empty($removedGroups)) {
            foreach ($removedGroups as $key => $groupID) {
              CRM_Contact_BAO_GroupContact::removeContactsFromGroup($contactArray, $groupID, 'Admin', 'Removed');
            }
          }
        }

        // Mailchimp Email Update event
        else if($requestType == 'upemail') {
          // Try to find the email address
          $email = new CRM_Core_BAO_Email();
          $email->get('email', $requestData['old_email']);

          // If the Email was found.
          if (!empty($email->contact_id)) {
            $email->email = $requestData['new_email'];
            $email->save();
          }
        }

        // Mailchimp Cleaned Email  event
        else if($requestType == 'cleaned') {
          // Try to find the email address
          $email = new CRM_Core_BAO_Email();
          $email->get('email', $requestData['email']);

          // If the Email was found.
          if (!empty($email->contact_id)) {
            $email->on_hold = 1;
            $email->holdEmail($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();
  } 
  
  /*
   * Add/Remove contact from CiviCRM Groups mapped with Mailchimp List & Groups 
   */
  static function manageCiviCRMGroupSubcription($contactID = array(), $requestData , $action) {
    if (empty($contactID) || empty($requestData['merges']['GROUPINGS']) || empty($requestData['list_id']) || empty($action)) {
      return NULL;
    }
    
    $listID = $requestData['list_id'];
{'fraction_non_alphanumeric': 0.12740025296079108, 'fraction_numerical': 0.00022996435552489363, 'mean_word_length': 2.528600405679513, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1659450', 'n_tokens_mistral': 2502, 'n_tokens_neox': 2460, 'n_words': 590}