$value){
		
			$html =
				str_replace(
					"{%{$key}%}",
					$value,
					$html
				);
		}
		
		return trim($html);
	}
	
	public function getthemeclass($raw = true){
		
		if(
			isset($_COOKIE["theme"]) &&
			$_COOKIE["theme"] == "cream"
		){
			
			$body_class = "theme-white ";
		}else{
			
			$body_class = "";
		}
		
		if(
			$raw &&
			$body_class != ""
		){
			
			return ' class="' . rtrim($body_class) . '"';
		}
		
		return $body_class;
	}
	
	public function loadheader(array $get, array $filters, string $page){
		
		echo
			$this->load("header.html", [
				"title" => trim($get["s"] . " ({$page})"),
				"description" => ucfirst($page) . ' search results for "' . htmlspecialchars($get["s"]) . '"',
				"index" => "no",
				"search" => htmlspecialchars($get["s"]),
				"tabs" => $this->generatehtmltabs($page, $get["s"]),
				"filters" => $this->generatehtmlfilters($filters, $get),
				"body_class" => $this->getthemeclass()
			]);
		
		if(
			preg_match(
				'/bot|wget|curl|python-requests|scrapy|feedfetcher|go-http-client|ruby|universalfeedparser|yahoo\! slurp|spider|rss/i',
				$_SERVER["HTTP_USER_AGENT"]
			)
		){
			
			// bot detected !!
			echo
				$this->drawerror(
					"Tshh, blocked!",
					'You were blocked from viewing this page. If you wish to scrape data from 4get, please consider running your own 4get instance or using the API.',
				);
			die();
		}
	}
	
	public function drawerror($title, $error){
		
		return
			$this->load("search.html", [
				"class" => "",
				"right-left" => "",
				"right-right" => "",
				"left" =>
					'
' .
						'
' . htmlspecialchars($title) . '
' .
						$error .
					''
			]);
	}
	
	public function drawtextresult($site, $greentext = null, $duration = null, $keywords, $tabindex = true, $customhtml = null){
		
		$payload =
			'';
		
		// add favicon, link and archive links
		$payload .= $this->drawlink($site["url"]);
		
		/*
			Draw title + description + filetype
		*/
		$payload .=
			'
';
				
				if($duration !== null){
					
					$payload .=
						'' .
							htmlspecialchars($duration) .
						'
';
				}
				
				$payload .=
					'';
		
		if(
			isset($site["type"]) &&
			$site["type"] != "web"
		){
			
			$payload .= '
' . strtoupper($site["type"]) . '
';
		}
		
		$payload .=
			$this->highlighttext($keywords, $site["title"]) .
		'
' .
					htmlspecialchars($greentext) .
				'
';
		}
		
		if($site["description"] !== null){
			
			$payload .=
				'' .
					$this->highlighttext($keywords, $site["description"]) .
				'
';
		}
		
		$payload .= $customhtml;
		
		$payload .= '';
		
		/*
			Sublinks
		*/
		if(
			isset($site["sublink"]) &&
			!empty($site["sublink"])
		){
			
			usort($site["sublink"], function($a, $b){
				
				return strlen($a["description"]) > strlen($b["description"]);
			});
			
			$payload .=
				'' .
					'
';
			
			$opentr = false;
			for($i=0; $i' .
							'' .
								htmlspecialchars($site["sublink"][$i]["title"]) .
							'
';
				
				if(!empty($site["sublink"][$i]["date"])){
					
					$payload .=
						'' .
							date("jS M y @ g:ia", $site["sublink"][$i]["date"]) .
						'
';
				}
				
				if(!empty($site["sublink"][$i]["description"])){
					
					$payload .=
						'' .
							$this->highlighttext($keywords, $site["sublink"][$i]["description"]) .
						'
';
				}
				
				$payload .= '';
				
				if($opentr === false){
					
					$payload .= '';
				}
			}
			
			if($opentr === true){
				
				$payload .= '';
			}
			
			$payload .= ' | 
';
			
			foreach($site["table"] as $title => $value){
				
				$payload .=
					'' .
						'| ' . htmlspecialchars($title) . '' .
						' | ' . htmlspecialchars($value) . '' .
					' | 
';
			}
			
			$payload .= '
';
		}
		
		return $payload . '';
	}
	
	public function highlighttext($keywords, $text){
		
		$text = htmlspecialchars($text);
		
		$keywords = explode(" ", $keywords);
		$regex = [];
		
		foreach($keywords as $word){
			
			$regex[] = "\b" . preg_quote($word, "/") . "\b";
		}
		
		$regex = "/" . implode("|", $regex) . "/i";
		
		return
			preg_replace(
				$regex,
				'${0}',
				$text
			);
	}
	
	function highlightcode($text){
		
		// https://www.php.net/highlight_string
		ini_set("highlight.comment", "c-comment");
		ini_set("highlight.default", "c-default");
		ini_set("highlight.html", "c-default");
		ini_set("highlight.keyword", "c-keyword");
		ini_set("highlight.string", "c-string");
		
		$text =
			trim(
				preg_replace(
					'/<\/span>$/',
					"", // remove stray ending span because of the ',
							' '
						],
						[
							"\n", // replace 
 with newlines
							" " // replace html entity to space
						],
						str_replace(
							[
								// leading \n<?php ",
								"",
								""
							],
							"",
							highlight_string("', '', $text);
		}
		
		return $text;
	}
	
	public function drawlink($link){
		
		/*
			Add favicon
		*/
		$host = parse_url($link);
		$esc =
			explode(
				".",
				$host["host"],
				2
			);
		
		if(
			count($esc) === 2 &&
			$esc[0] == "www"
		){
			
			$esc = $esc[1];
		}else{
			
			$esc = $esc[0];
		}
		
		$esc = substr($esc, 0, 2);
		
		$urlencode = urlencode($link);
		
		$payload =
			'' .
				'
' .
				'
';
		
		/*
			Draw link
		*/
		$parts = explode("/", $link);
		$clickurl = "";
		
		// remove trailing /
		$c = count($parts) - 1;
		if($parts[$c] == ""){
			
			$parts[$c - 1] = $parts[$c - 1] . "/";
			unset($parts[$c]);
		}
		
		// merge https://site together
		$parts = [
			$parts[0] . $parts[1] . '//' . $parts[2],
			...array_slice($parts, 3, count($parts) - 1)
		];
		
		$c = count($parts);
		for($i=0; $i<$c; $i++){
			
			if($i !== 0){ $clickurl .= "/"; }
			
			$clickurl .= $parts[$i];
			
			if($i === $c - 1){
				
				$parts[$i] = rtrim($parts[$i], "/");
			}
			
			$payload .=
				'
' .
					htmlspecialchars(urldecode($parts[$i])) .
				'';
			
			if($i !== $c - 1){
				
				$payload .= '
';
			}
		}
		
		return $payload . '
' .
					'
' . htmlspecialchars($filter_values["display"]) . '
';
			
			if(is_array($filter_values["option"])){
				
				$tmp .= '
';
			}else{
				
				switch($filter_values["option"]){
					
					case "_DATE":
						$tmp .= '
 $value){
			
			if(
				$value == null ||
				$value == false ||
				$key == "npt" ||
				$key == "extendedsearch" ||
				$value == "any" ||
				$value == "all" ||
				(
					$ommit === true &&
					$key == "s"
				)
			){
				
				continue;
			}
			
			if(
				$key == "older" ||
				$key == "newer"
			){
				
				$value = date("Y-m-d", (int)$value);
			}
			
			$out[$key] = $value;
		}
		
		return http_build_query($out);
	}
	
	public function htmlimage($image, $format){
		
		if(
			preg_match(
				'/^data:/',
				$image
			)
		){
			
			return htmlspecialchars($image);
		}
		
		return "/proxy?i=" . urlencode($image) . "&s=" . $format;
	}
	
	public function htmlnextpage($gets, $npt, $page){
		
		$query = $this->buildquery($gets);
		
		return $page . "?" . $query . "&npt=" . $npt;
	}
}