<?php

class Piotnetforms_Helper {
	protected $B = 0;
	protected $I = 0;
	protected $U = 0;
	protected $HREF = '';

	public function mailchimp_curl_get_connect( $url, $request_type, $api_key, $data = [] ) {
		if ( $request_type == 'GET' ) {
			$url .= '?' . http_build_query( $data );
		}

		$mch = curl_init();
		$headers = [
			'Content-Type: application/json',
			'Authorization: Basic '.base64_encode( 'user:'. $api_key )
		];
		curl_setopt( $mch, CURLOPT_URL, $url );
		curl_setopt( $mch, CURLOPT_HTTPHEADER, $headers );
		//curl_setopt($mch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
		curl_setopt( $mch, CURLOPT_RETURNTRANSFER, true );
		curl_setopt( $mch, CURLOPT_CUSTOMREQUEST, $request_type );
		curl_setopt( $mch, CURLOPT_TIMEOUT, 10 );
		curl_setopt( $mch, CURLOPT_SSL_VERIFYPEER, false );
		if ( $request_type != 'GET' ) {
			curl_setopt( $mch, CURLOPT_POST, true );
			curl_setopt( $mch, CURLOPT_POSTFIELDS, json_encode( $data ) );
		}
		return curl_exec( $mch );
	}
	public function mailchimp_curl_put_member( $url, $api_key, $data ) {
		$ch = curl_init( $url );
		$header = [
			'Content-Type: application/json',
			'Authorization: Basic '.base64_encode( 'user:'. $api_key )
		];
		curl_setopt( $ch, CURLOPT_HTTPHEADER, $header );
		curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
		curl_setopt( $ch, CURLOPT_TIMEOUT, 10 );
		curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'PUT' );
		curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
		curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $data ) );

		$result   = curl_exec( $ch );
		$httpCode = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
		curl_close( $ch );
		return $httpCode;
	}
	public function zohocrm_post_record( $data, $url, $token ) {
		$data = json_encode( $data );
		$data = '{"data":['.$data.']}';
		$curl = curl_init();
		curl_setopt_array( $curl, [
		CURLOPT_URL => $url,
		CURLOPT_RETURNTRANSFER => true,
		CURLOPT_ENCODING => '',
		CURLOPT_MAXREDIRS => 10,
		CURLOPT_TIMEOUT => 0,
		CURLOPT_FOLLOWLOCATION => true,
		CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
		CURLOPT_CUSTOMREQUEST => 'POST',
		CURLOPT_POSTFIELDS => $data,
		CURLOPT_HTTPHEADER => [
				'Authorization: Zoho-oauthtoken '.$token.'',
				'Content-Type: application/x-www-form-urlencoded',
			],
		] );
		$response = curl_exec( $curl );
		curl_close( $curl );
		return $response;
	}
	public function zohocrm_get_record( $url, $token ) {
		$curl = curl_init();
		curl_setopt_array( $curl, [
			CURLOPT_URL => $url,
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_ENCODING => '',
			CURLOPT_MAXREDIRS => 10,
			CURLOPT_TIMEOUT => 0,
			CURLOPT_FOLLOWLOCATION => true,
			CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
			CURLOPT_CUSTOMREQUEST => 'GET',
			CURLOPT_HTTPHEADER => [
				'Authorization: Zoho-oauthtoken '.$token.'',
				'Content-Type: application/x-www-form-urlencoded',
			],
		] );
		$response = curl_exec( $curl );
		curl_close( $curl );
		return $response;
	}
	public function zoho_refresh_token() {
		$zoho_client_id = get_option( 'piotnetforms-zoho-client-id' );
		$zoho_client_secret = get_option( 'piotnetforms-zoho-client-secret' );
		$zoho_domain_refresh_token = get_option( 'piotnetforms-zoho-domain' );
		$refresh_token = get_option( 'piotnetforms_zoho_refresh_token' );

		$url_refresh_token = 'https://'.$zoho_domain_refresh_token.'/oauth/v2/token?refresh_token='.$refresh_token.'&client_id='.$zoho_client_id.'&client_secret='.$zoho_client_secret.'&grant_type=refresh_token';
		$zoho_access_token = wp_remote_post( $url_refresh_token );
		if ( empty( $zoho_access_token->error ) ) {
			$zoho_access_token = json_decode( $zoho_access_token['body'] );
			update_option( 'piotnetforms_zoho_access_token', $zoho_access_token->access_token );
			update_option( 'piotnetforms_zoho_api_domain', $zoho_access_token->api_domain );
		} else {
			echo $zoho_access_token->error;
		}
	}

	public function piotnetforms_sendinblue_get_list( $api_key ) {
		$curl = curl_init();
		curl_setopt_array( $curl, [
			CURLOPT_URL => 'https://api.brevo.com/v3/contacts/lists?limit=50&offset=0&sort=desc',
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_ENCODING => '',
			CURLOPT_MAXREDIRS => 10,
			CURLOPT_TIMEOUT => 0,
			CURLOPT_FOLLOWLOCATION => true,
			CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
			CURLOPT_CUSTOMREQUEST => 'GET',
			CURLOPT_HTTPHEADER => [
				'Accept: application/json',
				'api-key: '.$api_key.'',
			],
		] );
		$response = curl_exec( $curl );
		curl_close( $curl );
		return $response;
	}
	public function piotnetforms_sendinblue_get_attribute( $api_key ) {
		$curl = curl_init();
		curl_setopt_array( $curl, [
			CURLOPT_URL => 'https://api.brevo.com/v3/contacts/attributes',
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_ENCODING => '',
			CURLOPT_MAXREDIRS => 10,
			CURLOPT_TIMEOUT => 0,
			CURLOPT_FOLLOWLOCATION => true,
			CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
			CURLOPT_CUSTOMREQUEST => 'GET',
			CURLOPT_HTTPHEADER => [
				'Accept: application/json',
				'api-key: '.$api_key.'',
			],
		] );
		$response = curl_exec( $curl );
		curl_close( $curl );
		return $response;
	}
	public function piotnetforms_sendinblue_create_contact( $api_key, $data ) {
		$curl = curl_init();
		curl_setopt_array( $curl, [
			CURLOPT_URL => 'https://api.brevo.com/v3/contacts',
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_ENCODING => '',
			CURLOPT_MAXREDIRS => 10,
			CURLOPT_TIMEOUT => 0,
			CURLOPT_FOLLOWLOCATION => true,
			CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
			CURLOPT_CUSTOMREQUEST => 'POST',
			CURLOPT_POSTFIELDS =>$data,
			CURLOPT_HTTPHEADER => [
				'Accept: application/json',
				'Content-Type: application/json',
				'api-key: '.$api_key.'',
			],
		] );
		$response = curl_exec( $curl );
		curl_close( $curl );
		return $response;
	}

	public function activecampaign_add_contact( $api, $data ) {
		$activecampaign_request = curl_init( $api );
		curl_setopt( $activecampaign_request, CURLOPT_HEADER, 0 );
		curl_setopt( $activecampaign_request, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt( $activecampaign_request, CURLOPT_POSTFIELDS, $data );
		curl_setopt( $activecampaign_request, CURLOPT_FOLLOWLOCATION, true );

		$activecampaign_response = (string)curl_exec( $activecampaign_request );

		curl_close( $activecampaign_request );
		return unserialize( $activecampaign_response );
	}
	public function activecampaign_edit_contact( $api, $data ) {
		$request = curl_init( $api );
		curl_setopt( $request, CURLOPT_HEADER, 0 );
		curl_setopt( $request, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt( $request, CURLOPT_POSTFIELDS, $data );
		curl_setopt( $request, CURLOPT_FOLLOWLOCATION, true );
		$response = (string)curl_exec( $request );
		curl_close( $request );
		return unserialize( $response );
	}
	public function piotnetforms_convertkit_add_subscriber( $data, $form_id ) {
		$curl = curl_init();
		curl_setopt_array( $curl, [
			CURLOPT_URL => 'https://api.convertkit.com/v3/forms/'.$form_id.'/subscribe',
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_ENCODING => '',
			CURLOPT_MAXREDIRS => 10,
			CURLOPT_TIMEOUT => 0,
			CURLOPT_FOLLOWLOCATION => true,
			CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
			CURLOPT_CUSTOMREQUEST => 'POST',
			CURLOPT_POSTFIELDS => json_encode( $data ),
			CURLOPT_HTTPHEADER => [
				'Content-Type: application/json; charset=utf-8',
			],
		] );
		$response = curl_exec( $curl );
		curl_close( $curl );
		return $response;
	}
	public function piotnetforms_constant_contact_create_contact( $token, $data ) {
		$curl = curl_init();
		curl_setopt_array( $curl, [
		CURLOPT_URL => 'https://api.cc.email/v3/contacts',
		CURLOPT_RETURNTRANSFER => true,
		CURLOPT_ENCODING => '',
		CURLOPT_MAXREDIRS => 10,
		CURLOPT_TIMEOUT => 0,
		CURLOPT_FOLLOWLOCATION => true,
		CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
		CURLOPT_CUSTOMREQUEST => 'POST',
		CURLOPT_POSTFIELDS => $data,
		CURLOPT_HTTPHEADER => [
			'accept: application/json',
			'authorization: Bearer '.$token,
			'cache-control: no-cache',
			'content-type: application/json'
		],
		] );

		$response = curl_exec( $curl );

		curl_close( $curl );
		return $response;
	}
	public function piotnetforms_constant_contact_refresh_token( $key, $secret, $refresh_token ) {
		$curl = curl_init();
		curl_setopt_array( $curl, [
		CURLOPT_URL => 'https://authz.constantcontact.com/oauth2/default/v1/token?refresh_token='.$refresh_token.'&grant_type=refresh_token',
		CURLOPT_RETURNTRANSFER => true,
		CURLOPT_ENCODING => '',
		CURLOPT_MAXREDIRS => 10,
		CURLOPT_TIMEOUT => 0,
		CURLOPT_FOLLOWLOCATION => true,
		CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
		CURLOPT_CUSTOMREQUEST => 'POST',
		CURLOPT_HTTPHEADER => [
			'Content-Type: application/x-www-form-urlencoded',
			'Authorization: Basic '. base64_encode( $key.':'.$secret ),
		],
		] );
		$response = curl_exec( $curl );
		curl_close( $curl );
		$response = json_decode( $response );
		if ( !empty( $response ) && !empty( $response->access_token ) ) {
			update_option( 'piotnetforms-constant-contact-access-token', $response->access_token );
			update_option( 'piotnetforms-constant-contact-refresh-token', $response->refresh_token );
			update_option( 'piotnetforms-constant-contact-time-get-token', time() );
		} else {
			echo 'An error occurred';
			wp_die();
		}
		return $response->access_token;
	}

	public function piotnetforms_paypal_get_token( $client_id, $client_secret, $paypal_url ) {
		$curl = curl_init();
		curl_setopt_array( $curl, [
			CURLOPT_URL => $paypal_url.'v1/oauth2/token',
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_ENCODING => '',
			CURLOPT_MAXREDIRS => 10,
			CURLOPT_TIMEOUT => 0,
			CURLOPT_FOLLOWLOCATION => true,
			CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
			CURLOPT_CUSTOMREQUEST => 'POST',
			CURLOPT_POSTFIELDS => 'grant_type=client_credentials',
			CURLOPT_HTTPHEADER => [
				'Accept: application/json',
				'Accept-Language: en_US',
				'Authorization: Basic '.base64_encode( $client_id . ':' . $client_secret ),
				'Content-Type: application/x-www-form-urlencoded'
			],
		] );
		$response = json_decode( curl_exec( $curl ) )->access_token;
		curl_close( $curl );
		return $response;
	}

    public function piotnetforms_check_email_exists($form_id, $name, $email, $check_all){
        $exists = false;
        $args = [
            'post_type' => 'piotnetforms-data',
            'post_status' => 'publish',
            'posts_per_page' => -1
        ];
        $posts = get_posts($args);
        foreach($posts as $post){
            $post_id = $post->ID;
            $database = get_post_meta($post_id, '_piotnetforms_fields_database', true);
            $database = json_decode($database);
            if($check_all){
                if($database->$name->value == $email){
                    $exists = true;
                    break;
                }
            }else{
                $id = !empty($database->form_id->value) ; $database->form_id->value = '';
                if($id == $form_id && $database->$name->value == $email){
                    $exists = true;
                    break;
                }
            }
        }
        return $exists;
    }
    public function piotnetforms_twilio_whatsapp_send_msg($data, $url, $header){
        $whatsapp_body = http_build_query( $data );
        $whatsapp_curl = curl_init();
        curl_setopt_array( $whatsapp_curl, [
                CURLOPT_URL => $url,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'POST',
                CURLOPT_POSTFIELDS => $whatsapp_body,
                CURLOPT_HTTPHEADER => [
                    'Authorization:  Basic '.$header.'',
                    'Content-Type: application/x-www-form-urlencoded',
                ],
            ] );
        $whatsapp_response = curl_exec( $whatsapp_curl );
        curl_close( $whatsapp_curl );
        return $whatsapp_response;
    }
}
