<h1>Contact Form 7 Integrations</h1>

    <?php 
// get option if plugin is already activated
$cUsComAPI_Cloud = new cUsComAPI_Cloud();
if ($cf7_cloud_activated == 1) {
    // get the credentials
    $credentials = get_option('cUsCloud_settings_userCredentials');
    $cUs_API_Account = $credentials['API_Account'];
    $cUs_API_Key = $credentials['API_Key'];
    $cus_par_url = 'https://admin.contactus.com/partners';
    $default_deep_link = get_option('cUsCloud_settings_default_deep_link_view');
    $defaultFormId = get_option('cUsCloud_settings_form_id');
    if (!strlen($default_deep_link)) {
        $cUsAPI_getFormKeys = $cUsComAPI_Cloud->getFormKeysAPI($cUs_API_Account, $cUs_API_Key);
        //api hook;
        $default_deep_link = $cUsComAPI_Cloud->getDefaultDeepLink($cUsAPI_getFormKeys);
        update_option('cUsCloud_settings_default_deep_link_view', $default_deep_link);
        // DEFAULT FORM KEYS
    }
    if (!strlen($defaultFormId)) {
        $cUsAPI_getFormKeys = $cUsComAPI_Cloud->getFormKeysAPI($cUs_API_Account, $cUs_API_Key);
        //api hook;
        $defaultFormId = $cUsComAPI_Cloud->getDefaultFormID($cUsAPI_getFormKeys);
        update_option('cUsCloud_settings_form_id', $defaultFormId);
        // DEFAULT FORM KEYS
    }
    $partnerID = $cUsComAPI_Cloud->get_partner_id($default_deep_link);
    $cus_CRED_url = $cus_par_url . '/index.php?loginName=' . $cUs_API_Account . '&userPsswd=' . urlencode($cUs_API_Key);
    ?>
function cUsCloud_loginAlreadyUser_callback()
{
    $cUsCF_api = new cUsComAPI_Cloud();
    $cUs_email = $_REQUEST['email'];
    $cUs_pass = $_REQUEST['pass'];
    $cUsCloud_API_credentials = $cUsCF_api->getAPICredentials($cUs_email, $cUs_pass);
    //api hook;
    //print_r($cUsCloud_API_credentials);
    if ($cUsCloud_API_credentials) {
        $cUs_json = json_decode($cUsCloud_API_credentials);
        switch ($cUs_json->status) {
            case 'success':
                $cUs_API_Account = $cUs_json->api_account;
                $cUs_API_Key = $cUs_json->api_key;
                if (strlen(trim($cUs_API_Account)) && strlen(trim($cUs_API_Key))) {
                    $aryUserCredentials = array('API_Account' => $cUs_API_Account, 'API_Key' => $cUs_API_Key);
                    //update_option('cUsCloud_settings_userCredentials', $aryUserCredentials);
                    $cUsCF_API_getKeysResult = $cUsCF_api->getFormKeysAPI($cUs_API_Account, $cUs_API_Key);
                    //api hook;
                    $cUs_jsonKeys = json_decode($cUsCF_API_getKeysResult);
                    //print_r( $cUs_jsonKeys ); exit;
                    if ($cUs_jsonKeys->status == 'success') {
                        $postData = array('email' => $cUs_email, 'credential' => $cUs_pass);
                        update_option('cUsCloud_settings_userData', $postData);
                        //print_r($cUs_jsonKeys->data);
                        //exit;
                        foreach ($cUs_jsonKeys->data as $oForms => $oForm) {
                            if ($oForm->form_type == 'post' && $oForm->default == 1) {
                                //GET DEFAULT POST FORM KEY
                                $defaultFormKey = $oForm->form_key;
                                $form_type = $oForm->form_type;
                                $deeplinkview = $oForm->deep_link_view;
                                //exit;
                                $defaultFormId = $oForm->form_id;
                                update_option('cUsCloud_settings_default_deep_link_view', $deeplinkview);
                                // DEFAULT FORM KEYS
                                //exit;
                            } else {
                                $deeplinkview = $oForm->deep_link_view;
                            }
                        }
                        // check if form with Type 7 is available
                        if (!isset($defaultFormKey) || !strlen($defaultFormKey)) {
                            //echo 2; // no form of type POST/7 is available
                            $aryResponse = array('status' => 2, 'cUs_API_Account' => $cUs_API_Account, 'cUs_API_Key' => $cUs_API_Key, 'deep_link_view' => $cUsCF_api->parse_deeplink($deeplinkview));
                        } else {
                            $aryFormOptions = array('tab_user' => 1, 'cus_version' => 'tab');
                            //DEFAULT SETTINGS / FIRST TIME
                            //update_option('cUsCloud_FORM_settings', $aryFormOptions );//UPDATE FORM SETTINGS
                            update_option('cUsCloud_settings_form_key', $defaultFormKey);
                            //DEFAULT FORM KEYS
                            update_option('cUsCloud_settings_form_id', $defaultFormId);
                            //DEFAULT FORM ID
                            update_option('cUsCloud_settings_form_keys', $cUs_jsonKeys);
                            // ALL FORM KEYS
                            update_option('cf7_cloud_database_active', 1);
                            update_option('cUsCloud_settings_userCredentials', $aryUserCredentials);
                            //update_option('cUsCloud_settings_default_deep_link_view', $deeplinkview); // DEFAULT FORM KEYS
                            $aryResponse = array('status' => 1);
                        }
                        //echo 1;
                    } elseif ($cUs_jsonKeys->error === 'No valid form keys') {
                        $aryResponse = array('status' => 3, 'message' => 'No valid form keys');
                    } else {
                        echo 'Error. . . ';
                    }
                } else {
                    $aryResponse = array('status' => 3, 'message' => $cUs_json->error);
                }
                break;
            case 'error':
                $aryResponse = array('status' => 3, 'message' => $cUs_json->error);
                break;
        }
    }
    echo json_encode($aryResponse);
    die;
}