<?php
class mailRu_verify
{

  public $email;
  public $verifier_email;
  public $port;
  private $errors;
  private $debug;
  private $debug_raw;

  private $_hotmail_username_check_url = 'https://account.mail.ru/api/v1/user/password/restore?';
  private $page_content;
  private $page_headers;

  public function __construct($email = null, $verifier_email = null, $port = 25)
  {
    $this->debug = array();
    $this->debug_raw = array();
    if (!is_null($email) && !is_null($verifier_email)) {
      $this->debug[] = 'Initialized with Email: ' . $email . ', Verifier Email: ' . $verifier_email . ', Port: ' . $port;
      $this->set_email($email);
      $this->set_verifier_email($verifier_email);
    } else {
      $this->debug[] = 'Initialized with no email or verifier email values';
    }
    $this->set_port($port);
  }

  public function set_verifier_email($email)
  {
    $this->verifier_email = $email;
    $this->debug[] = 'Verifier Email was set to ' . $email;
  }

  public function get_verifier_email()
  {
    return $this->verifier_email;
  }


  public function set_email($email)
  {
    $this->email = $email;
    $this->debug[] = 'Email was set to ' . $email;
  }

  public function get_email()
  {
    return $this->email;
  }

  public function set_port($port)
  {
    $this->port = $port;
    $this->debug[] = 'Port was set to ' . $port;
  }

  public function get_port()
  {
    return $this->port;
  }

  public function get_errors()
  {
    return array('errors' => $this->errors);
  }

  public function get_debug($raw = false)
  {
    if ($raw) {
      return $this->debug_raw;
    } else {
      return $this->debug;
    }
  }

  public function verify()
  {
    $this->debug[] = 'Verify function was called.';
    $is_valid = false;
    $is_valid = $this->validate_hotmail();
    return $is_valid;
  }

  private function getEmailAccFromEmail($email)
  {
    $email_address = explode('@', $email);

    $domain = array_shift($email_address);

    return $domain;
  }

  private function add_error($code, $msg)
  {
    $this->errors[] = array('code' => $code, 'message' => $msg);
  }

  private function clear_errors()
  {
    $this->errors = array();
  }

  private function validate_hotmail()
  {
    $this->debug[] = 'Validating email address...';
    $this->debug[] = 'Getting output content...';
    $this->debug[] = 'Preparing d...';
    $post_email = strtolower($this->email);
    $this->debug[] = 'Ready to submit the POST request to validate the email.';
    $response = $this->request_validation($post_email);
    $this->debug[] = 'Searching username error...';
    $json_response = json_decode($response, true);
    //	  var_dump($json_response['status']);
//	  var_dump($json_response);
    if ($json_response['status'] == 200) {
      $this->debug[] = 'This Email Address Exist';
      return true;
    } else {
      $this->debug[] = 'This Email Address Not Found';
    }
    return false;
  }

  private function request_validation($post_email)
  {
    $headers = array();
    $headers[] = 'Origin: https://account.mail.ru/api/v1/user/password/restore';
    $headers[] = 'hpgid: 33';
    $headers[] = 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36';
    $headers[] = 'Content-type: application/json; charset=UTF-8';
    $headers[] = 'Accept: application/json';
    $headers[] = 'Referer: https://account.mail.ru/api/v1/user/password/restore';
    $headers[] = 'Accept-Encoding: gzip, deflate, br';
    $headers[] = 'Accept-Language: en-US,en;q=0.9,bn;q=0.8,ru;q=0.7,es;q=0.6,fr;q=0.5';
    $headers[] = 'cache-control: no-cache';

    $url = $this->_hotmail_username_check_url . "email=$post_email&htmlencoded=false";

    // set post fields
    // $post = $fields;

    // var_dump($post);

    $ch = curl_init($url);

    curl_setopt($ch, CURLOPT_URL, $url);
    //	curl_setopt($ch, CURLOPT_ENCODING, "");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    //	curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    //	curl_setopt($ch, CURLOPT_VERBOSE, true);
    $result = curl_exec($ch);


    //  var_dump($result);



    return $result;
  }

}