<?php

class HTTPRequest {

	public $body = "";
	public $headers = [];
	public $status = 0;
	public $version = "1.0";

	public $connect_timeout = 2;
	public $transfer_timeout = 300;

	public $ch = null;

	public function __construct() {
		$this->ch = curl_init();
	}

	public function get($url, $headers = [], $headersonly = false) {
		curl_reset($this->ch);
	    curl_setopt($this->ch, CURLOPT_URL, $url);
		curl_setopt($this->ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:138.0) Gecko/20100101 Firefox/138.0");
		curl_setopt($this->ch, CURLOPT_FILETIME, true);
		curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, $this->connect_timeout);
		curl_setopt($this->ch, CURLOPT_TIMEOUT, $this->transfer_timeout);
        curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers);
		curl_setopt($this->ch, CURLOPT_NOBODY, $headersonly);
		curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($this->ch, CURLOPT_HEADER, true);
		curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);

		$this->status = 0;
		$response = curl_exec($this->ch);

		$header_size = curl_getinfo($this->ch, CURLINFO_HEADER_SIZE);
		$header = substr($response, 0, $header_size);
		$this->body = substr($response, $header_size);
		$this->headers = [];

		foreach ($hl as $h) {
			$h = trim($h);
			if (preg_match('/^HTTP\/([^\s]+)\s+(\d+)\s*/', $h, $m)) {
				$this->version = $m[1];
				$this->status = $m[2];
				continue;
			}
			if (str_starts_with($h, " ")) {
				$this->headers[$curr] .= " " . trim($h);
				continue;
			}
			if (preg_match('/^([^:]+):\s+(.*)$/', $h, $m)) {
				$curr = strtolower($m[1]);
				$this->headers[$curr] = $m[2];
			}
		}

		return $this->status;

	}


	public function post($url, $data, $headers = [], $headersonly = false) {


		curl_reset($this->ch);
	    curl_setopt($this->ch, CURLOPT_URL, $url);
		curl_setopt($this->ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:138.0) Gecko/20100101 Firefox/138.0");
		curl_setopt($this->ch, CURLOPT_POST, true);
		curl_setopt($this->ch, CURLOPT_FILETIME, true);
		curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, $this->connect_timeout);
		curl_setopt($this->ch, CURLOPT_TIMEOUT, $this->transfer_timeout);
        curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers);
		curl_setopt($this->ch, CURLOPT_NOBODY, $headersonly);
		curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($this->ch, CURLOPT_HEADER, true);
		curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($this->ch, CURLOPT_POSTFIELDS, $data);

		$this->status = 0;

		$response = curl_exec($this->ch);

		$header_size = curl_getinfo($this->ch, CURLINFO_HEADER_SIZE);
		$header = substr($response, 0, $header_size);
		$this->body = substr($response, $header_size);
		$this->headers = [];

		$hl = explode("\n", $header);

		foreach ($hl as $h) {
			$h = trim($h);
			if (preg_match('/^HTTP\/([^\s]+)\s+(\d+)\s*/', $h, $m)) {
				$this->version = $m[1];
				$this->status = $m[2];
				continue;
			}
			if (str_starts_with($h, " ")) {
				$this->headers[$curr] .= " " . trim($h);
				continue;
			}
			if (preg_match('/^([^:]+):\s+(.*)$/', $h, $m)) {
				$curr = strtolower($m[1]);
				$this->headers[$curr] = $m[2];
			}
		}

		return $this->status;

	}


}
