<?php

// 
// Simple php script that uses curl to act as a proxy for any incoming requests.  Make sure requests are properly escaped where applicable.
//
// Forwards the following headers for interaction with Youtube/Google API:
//      Authorization
//      X-GData-Client
//      X-GData-Key
//		X-GData-Device
//

$url = ($_POST['url']) ? $_POST['url'] : $_GET['url'];
$curl_handle=curl_init();

// Handle any POST data
if ($_POST['url']) {
	$postvars = '';
	while ($element = current($_POST)) {
		$postvars .= key($_POST).'='.$element.'&';
		next($_POST);
	}
	curl_setopt ($curl_handle, CURLOPT_POST, true);
	curl_setopt ($curl_handle, CURLOPT_POSTFIELDS, $postvars);
}

// Add special headers if they exist
$headers = getallheaders();
$req_headers = array();
if ($val = $headers['Authorization']) {
    $req_headers[] = 'Authorization: '.$val;
}
if ($val = $headers['X-GData-Client']) {
    $req_headers[] = 'X-GData-Client: '.$val;
}
if ($val = $headers['X-GData-Key']) {
    $req_headers[] = 'X-GData-Key: '.$val;
}
if ($val = $headers['X-GData-Device']) {
	$req_headers[] = 'X-GData-Device: '.$val;
}
curl_setopt($curl_handle,CURLOPT_HTTPHEADER,$req_headers);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);

// Send request
curl_setopt($curl_handle,CURLOPT_URL,$url);
$tmp = curl_exec($curl_handle);
curl_close($curl_handle);

echo "$tmp";

if ($_POST['url']) {
	$fh = fopen('log', 'w') or die("can't write to debug file");
	fwrite($fh, $tmp);
	fclose($fh);
}
?>