#!/usr/local/bin/php
<?php

    function parse_redirect_line($line) {
	// Split up lines the look like:
	// https://inside.w.com/x.php 127.0.0.1/localhost linuxbox POST base64buf
	// into url, source_ip, hostname, request_method components
	$result = array();
	$arr_tmp = preg_split('[ ]', $line);
	$result['url'] = $arr_tmp[0];
        $result['source'] = $arr_tmp[1];
	$arr_tmp_2 = preg_match('/([^\/]+)(.*)/', $arr_tmp[1], $m);
	$result['source_ip'] = $m[1];
	$result['auth_user'] =  $arr_tmp[2]; // may be "-"
	$result['request_method']  =  $arr_tmp[3];
	$result['headers'] = parse_headers($arr_tmp[4]);
	return $result;
    }

    function parse_headers($header_buf_base64) {
	// Returns a list (array) of name/value pairs
	$result = array();
        $hdr_buf = base64_decode($header_buf_base64);
	$arr_tmp = preg_split('[\n]', $hdr_buf);
	for($hdr_str = ""; $hdr_str = current($arr_tmp); next($arr_tmp)) {
	    //syslog(LOG_INFO, "hdr_str: $hdr_str");
	    $arr_hdrs = preg_match('/([^:]+):(.*)/', $hdr_str, $m);
	    $atmp = array();
	    $atmp[0] = $m[1];
	    $atmp[1] = $m[2];
	    array_push($result, $atmp);
	}
	return $result;
    }

    function dump_headers($arr_headers) {
	foreach($arr_headers as $header) {
	    syslog(LOG_INFO, "key: {$header[0]} value: {$header[1]}");
	}
    }

    function find_first($hdr_name, $arr_headers) {
	// Returns the value of the first header matching $hdr_name
	$result = "NO_SUCH_HEADER";
	foreach ($arr_headers as $hdr) {
	    if($hdr[0] == $hdr_name) {
		$result = $hdr[1];
		break;
	    }
	}
	return $result;
    }

    function format_url($arr_url) {
        $hdr_str = format_headers($arr_url['headers']);
	$url = "{$arr_url['url']} {$arr_url['source']} ";
	$url .= "{$arr_url['auth_user']}";
	# don't forget the linefeed, or Squid hangs
	$url .= " {$arr_url['request_method']} $hdr_str\n";
	return $url;    
    }

    function format_headers($arr_headers) {
        $r_tmp = "";
	foreach($arr_headers as $hdr) {
	    $r_tmp .=  "{$hdr[0]}:{$hdr[1]}\n";
	}
	$result = base64_encode($r_tmp);
	return $result;
    }

    function main_loop() {

   	define_syslog_variables();
    	openlog("header-redir", LOG_NDELAY, LOG_LOCAL0);

    	syslog(LOG_INFO, "Stating header-redirector.php");

    	$url = "";
    	while(1) {

	    // read urls
	    $url = fgets(STDIN, 65534);
	    if ($url == FALSE) {
		continue;
	    }
	    //syslog(LOG_INFO, ">>$url<<");
	    $arr_url = parse_redirect_line($url);
	    $sip = $arr_url['source_ip'];
	    $headers = $arr_url['headers'];
	    // Append a new, custom header
	    $headers[] = array('Magic-Header', "Magic Header Client is $sip");
	    $arr_url['headers'] = $headers; // NOT a no-op in PHP, unless 
					    // refererencees used

	    $nurl = format_url($arr_url);
	    //syslog(LOG_INFO, $nurl);

	    // NOW--cross check that we can parse our own url:
	    //$arr_url = parse_redirect_line($nurl);
	    //$headers = $arr_url['headers'];
	    //dump_headers($headers);

	    print $nurl; 
    	}

    	// not reached
    	closelog();
    }  //main_loop

    main_loop();

?>






