<?php


class MainFunction
{
    function __construct()
    {
        $host = "localhost";
        $user = "root";
        $pw = "";
        $db = "mother_mary";
        $this->db = new PDO("mysql:$host", $user, $pw);
        $this->db->exec("CREATE DATABASE IF NOT EXISTS $db");
        $this->db->exec("USE $db");
    }

    function sendRequest($name, $email, $request, $private, $category)
    {
        $date = date('Y-m-d H:i:s');
        $stmt = $this->db->prepare('INSERT INTO `requests`( `name`, `email`, `request`, `private`, category, `created_at`) VALUES (?,?,?,?,?,?)');
        $stmt->execute([$name, $email, $request, $private, $category, $date]);

        $this->subsribe($email, $name);
        $this->dripSubsribe($email, $name);
        $this->GetResponseSubsribe($email, $name);
        
    }

    function getTesti()
    {
        $data = $this->db->query('select * from requests where private = 1  order by id desc LIMIT 0,40 ');
        $testimonials = $data->fetchAll(PDO::FETCH_ASSOC);
        echo json_encode([
            'data' => $testimonials,
            'total' => number_format(count($testimonials))
        ]);
    }

    function getAllTestimonials()
    {
        $data = $this->db->query('select * from requests order by id desc');
        $testimonials = $data->fetchAll(PDO::FETCH_ASSOC);
        return $testimonials;
    }

    function subsribe($email, $name)
    {
        $url = 'https://api.convertkit.com/v3/forms/2376376/subscribe';
        $data = [
            'api_key' => 'bXx6uwWtbbwG5R148KdSwQ',
            'email' => $email,
            'first_name' => $name,
        ];
	
        $postdata = json_encode($data);
		
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        $result = curl_exec($ch);
        curl_close($ch);
    }

    function dripSubsribe($email, $name)
    {
        $ch = curl_init();
        //YOUR-ACCOUNT-ID replace it with your account id
        curl_setopt($ch, CURLOPT_URL, 'https://api.getdrip.com/v2/2882567/subscribers');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        $data = array(
            'subscribers' => array(
                array( 
                    'email' => $email,
                    'first_name' => $name,
                ),
            ),
        );
        $post = json_encode($data);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        //YOUR-API-TOKEN-HERE replace it with your api-token
        curl_setopt($ch, CURLOPT_USERPWD, '59185bfa447967a740ceccbc2d1c4d01' . ':' . '');
        $headers = array();
        //replace with your app name and registered domain with drip account
        $headers[] = 'User-Agent: Angelic Prayer (www.angelicprayer.com)';
        $headers[] = 'Content-Type: application/json';
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        
        $result = curl_exec($ch);
        echo $result;
        if (curl_errno($ch)) {
            echo 'Error:' . curl_error($ch);
        }
        curl_close($ch);
    }

    function GetResponseSubsribe($email, $name)
    {
        $authorization = "X-Auth-Token: api-key 8shrbmveevzgs5amoqzx5mlzcu3op2lc";
        $ch = curl_init();
        $data = array (
            'name' => $name,
            'email' => $email,
            'campaign' => array('campaignId'=>'zOT0V'),
        );
        $data_string = json_encode($data);
        curl_setopt($ch, CURLOPT_URL,"https://api.getresponse.com/v3/contacts");
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', $authorization));
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        // Receive server response
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $server_output = curl_exec($ch);
        print_r($server_output);
        curl_close ($ch);
    }
}

$use = new MainFunction();


