<?php

class Eurostocks_Robot_Txt_Parser {

  public $url;
  public $user_agent = false;
 

  public function __construct($url, $user_agent = false){
    $this->url = $url;
    $this->user_agent = $user_agent;
  }

  function is_robots_allowed()
  {

    $agents = [
      preg_quote('*')
    ];
    if($this->user_agent) {
      $agents[] = preg_quote($this->user_agent);
    }
    $agents = implode('|', $agents);

    $parsed = parse_url($this->url);

    $curl_target = "{$parsed['scheme']}://{$parsed['host']}/robots.txt";

    $curl_opts = [
      CURLOPT_FOLLOWLOCATION => TRUE,
      CURLOPT_USERAGENT => $this->user_agent,
    ];

    $response = wp_remote_get($curl_target, $curl_opts);
    if (is_wp_error($response)) {
      $error_message = $response->get_error_message();
      error_log("Error: $error_message");
  } else {
      $body = wp_remote_retrieve_body($response);
      $robotstxt = $body;
  }

    if(empty($robotstxt)) {
      error_log("robots.txt not found");
      return FALSE;
    }

    $rules = [];

    $line = strtok($robotstxt, "\r\n");
    $current_user_agent_applies = FALSE;
    while(FALSE !== $line) {

      if(!$line = trim($line)) continue;

      if(preg_match('/^\s*User-agent: (.*)/i', $line, $match)) {
        $current_user_agent_applies = preg_match("/($agents)/i", $match[1]);
      }

      if ($current_user_agent_applies) {
        if (preg_match('/^\s*Disallow:\s*(\S.*)/i', $line, $regs)) {
            $rules[] = preg_quote(trim($regs[1]), "/");
        } 
      }
    

      $line = strtok("\r\n");

    }

    foreach($rules as $rule) {
      if(preg_match("/^$rule/", $parsed['path'])) {
        return true;
      }
    }

    return false;

  }


}
