<commit_before><?php

/*
 * Azada 0.1b
 * php web-based proxy
 * https://github.com/hamidsamak/azada
 */

error_reporting(0);

if (isset($_GET['url']) && empty($_GET['url']) === false) {
	$url = urldecode($_GET['url']);
	$rot13 = isset($_GET['rot13']);
	$base64 = isset($_GET['base64']);

	if ($rot13)
		$url = str_rot13($url);

	if ($base64)
		$url = base64_decode($url);

	if ($result = get_contents($url, true)) {
		$data = trim($result[0]);
		$info = $result[1];

		$parse = parse_url($url);

		if (isset($info['content_type']) && empty($info['content_type']) === false)
			header('Content-type: ' . $info['content_type']);
		
		if (strtolower(substr($data, 0, 9)) === '<!doctype') {
			if (isset($parse['scheme']) === false)
				$parse['scheme'] = 'http';
			if (isset($parse['host']) === false)
				$parse['host'] = $parse['path'];

			$base_url = $parse['scheme'] . '://' . $parse['host'];

			$doc = new DOMDocument();
			
			$doc->loadHTML($data);

			foreach (array('a' => 'href', 'link' => 'href', 'img' => 'src', 'script' => 'src', 'form' => 'action') as $name => $attribute)
				foreach ($doc->getElementsByTagName($name) as $link) {
					$value = $link->getAttribute($attribute);
					
					$parse = parse_url($value);
					if (isset($parse['scheme']) === false)
						$value = $base_url . (substr($value, 0, 1) === '/' ? null : '/') . $value;

					if ($base64)
						$value = base64_encode($value);

					if ($rot13)
						$value = str_rot13($value);

					$link->setAttribute($attribute, $_SERVER['PHP_SELF'] . '?url=' . $value . ($base64 ? '&base64=1' : null) . ($rot13 ? '&rot13=1' : null));
				}

			echo $doc->saveHTML();
		} else
			die($data);
	} else
		die('<strong>Azada error:</strong> Unreachable host.');

	exit;
}

if (file_exists(__DIR__ . '/cookie.txt'))
	unlink('cookie.txt');

echo '<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
<title>Azada</title>
<script type="text/javascript">
function $(id) {
	return document.getElementById(id);
}
function options() {
	var url = $("url").value;

	if ($("base64").checked == true)
		url = window.btoa(url);
	else
		$("base64").removeAttribute("name");

	if ($("rot13").checked == true)
		url = url.replace(/[a-zA-Z]/g,function(c){return String.fromCharCode((c<="Z"?90:122)>=(c=c.charCodeAt(0)+13)?c:c-26);});
	else
		$("rot13").removeAttribute("name");

	$("form").innerHTML = "<input name=\"url\" type=\"hidden\" value=\"" + url + "\">" + $("form").innerHTML;
	$("url").removeAttribute("name");

	return true;
}
</script>
</head>
<body>
<header><h1>Azada</h1></header>
<section>
	<form id="form" method="get" action="' . $_SERVER['PHP_SELF'] . '" onsubmit="return options();">
	URL: <input id="url" name="url" type="text" value=""> <button type="submit">Browse</button><br>
	<label><input id="base64" name="base64" type="checkbox" value="1" checked> Base64 encode</label><br><commit_msg>Add javascript enabled dependency<commit_after><?php

/*
 * Azada 0.1b
 * php web-based proxy
 * https://github.com/hamidsamak/azada
 */

error_reporting(0);

if (isset($_GET['url']) && empty($_GET['url']) === false) {
	$url = urldecode($_GET['url']);
	$rot13 = isset($_GET['rot13']);
	$base64 = isset($_GET['base64']);

	if ($rot13)
		$url = str_rot13($url);

	if ($base64)
		$url = base64_decode($url);

	if ($result = get_contents($url, true)) {
		$data = trim($result[0]);
		$info = $result[1];

		$parse = parse_url($url);

		if (isset($info['content_type']) && empty($info['content_type']) === false)
			header('Content-type: ' . $info['content_type']);
		
		if (strtolower(substr($data, 0, 9)) === '<!doctype') {
			if (isset($parse['scheme']) === false)
				$parse['scheme'] = 'http';
			if (isset($parse['host']) === false)
				$parse['host'] = $parse['path'];

			$base_url = $parse['scheme'] . '://' . $parse['host'];

			$doc = new DOMDocument();
			
			$doc->loadHTML($data);

			foreach (array('a' => 'href', 'link' => 'href', 'img' => 'src', 'script' => 'src', 'form' => 'action') as $name => $attribute)
				foreach ($doc->getElementsByTagName($name) as $link) {
					$value = $link->getAttribute($attribute);
					
					$parse = parse_url($value);
					if (isset($parse['scheme']) === false)
						$value = $base_url . (substr($value, 0, 1) === '/' ? null : '/') . $value;

					if ($base64)
						$value = base64_encode($value);

					if ($rot13)
						$value = str_rot13($value);

					$link->setAttribute($attribute, $_SERVER['PHP_SELF'] . '?url=' . $value . ($base64 ? '&base64=1' : null) . ($rot13 ? '&rot13=1' : null));
				}

			echo $doc->saveHTML();
		} else
			die($data);
	} else
		die('<strong>Azada error:</strong> Unreachable host.');

	exit;
}

if (file_exists(__DIR__ . '/cookie.txt'))
	unlink('cookie.txt');

echo '<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
<title>Azada</title>
<script type="text/javascript">
function $(id) {
	return document.getElementById(id);
}
function options() {
	var url = $("url").value;

	if ($("base64").checked == true)
		url = window.btoa(url);
	else
		$("base64").removeAttribute("name");

	if ($("rot13").checked == true)
		url = url.replace(/[a-zA-Z]/g,function(c){return String.fromCharCode((c<="Z"?90:122)>=(c=c.charCodeAt(0)+13)?c:c-26);});
	else
		$("rot13").removeAttribute("name");

	$("form").innerHTML = "<input name=\"url\" type=\"hidden\" value=\"" + url + "\">" + $("form").innerHTML;
	$("url").removeAttribute("name");

	return true;
}

window.onload = function(){
	$("browse").removeAttribute("disabled");
}
</script>
</head>
<body>
<header><h1>Azada</h1></header>
<section>
	<form id="form" method="get" action="' . $_SERVER['PHP_SELF'] . '" onsubmit="return options();">
	URL: <input id="url" name="url" type="text" value=""> <button id="browse" type="submit" disabled>Browse</button><noscript> Please enable javascript</noscript><br>
	<label><input id="base64" name="base64" type="checkbox" value="1" checked> Base64 encode</label><br>
{'fraction_non_alphanumeric': 0.2504889178617992, 'fraction_numerical': 0.025423728813559324, 'mean_word_length': 4.5438121047877145, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 85, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1288110', 'n_tokens_mistral': 2523, 'n_tokens_neox': 2212, 'n_words': 422}