<?php

/* Magic superglobals */
$__PH7_cgilib_Header_array = array('type'=>'text/html','head'=>'','others'=>array());
$__PH7_cgilib_Header = 'Content-Type: '.$__PH7_cgilib_Header_array['type']."\r\n\r\n";

function __ph7_cgilib_properNaming($name,$value){
	return str_replace(' ','-',ucwords(str_replace('_',' ',strtolower(str_replace('HTTP_','',$name))))).': '.$value;
}

function __ph7_cgilib_envToHTTPRequest(){
	$req='';
	if(isset($_ENV['REQUEST_METHOD'])){

		$req=$_ENV['REQUEST_METHOD'].' '.$_ENV['SCRIPT_NAME'].(strlen($_ENV['QUERY_STRING'])?'?'.$_ENV['QUERY_STRING']:'').' '.$_ENV['SERVER_PROTOCOL']."\r\n";

		foreach($_ENV as $k => $v) {
			if(substr($k,0,5)=='HTTP_') {
				$req.=__ph7_cgilib_properNaming($k,$v)."\r\n";
			}
		}
		if(intval($_ENV['CONTENT_LENGTH']) > 0) {
			$req.=__ph7_cgilib_properNaming('CONTENT_LENGTH',$_ENV['CONTENT_LENGTH'])."\r\n";
			$req.=__ph7_cgilib_properNaming('CONTENT_TYPE',$_ENV['CONTENT_TYPE'])."\r\n";
		}
		if($_ENV['REQUEST_METHOD']!='GET') {
			$req.="\r\n";
			$stdin=fopen('php://stdin', 'r');
			while( $line = fgets( $stdin ) ) {
				$req.=$line;
			}
			fclose( $stdin );
			$req.="\r\n";
		}
	}
	return $req;
}

function __ph7_cgilib_header_update() {
	global $__PH7_cgilib_Header,$__PH7_cgilib_Header_array;
	$otherheaders=implode("\r\n",$__PH7_cgilib_Header_array['others']);
	if(!empty($otherheaders)) $otherheaders.="\r\n";
	$__PH7_cgilib_Header = ($__PH7_cgilib_Header_array['head']?$__PH7_cgilib_Header_array['head']."\r\n":'').'Content-Type: '.$__PH7_cgilib_Header_array['type']."\r\n".$otherheaders."\r\n";
}

function __ph7_cgilib_header($str) {
	global $__PH7_cgilib_Header_array;
	if(substr($str,0,5)=='HTTP/') {
		$__PH7_cgilib_Header_array['head'] = $str;
	}else if(substr(strtolower($str),0,13)=='content-type:'){
		$__PH7_cgilib_Header_array['type'] = trim(substr($str,13));
	}else{
		$__PH7_cgilib_Header_array['others'][] = $str;
	}

	__ph7_cgilib_header_update();
}

function __ph7_cgilib_header_remove($header='') {
	global $__PH7_cgilib_Header_array;
	if(!empty($header)){
		$o_cnt =  count($__PH7_cgilib_Header_array['others']);
		$headerlower=trim(strtolower($header));
		$headerlen=strlen($headerlower);
		for($i=0; $i<$o_cnt; $i++) {
			if(substr(strtolower($__PH7_cgilib_Header_array['others'][$i]),0,$headerlen)==$headerlower) {
				unset($__PH7_cgilib_Header_array['others'][$i]);
			}
		}
	} else {
		$__PH7_cgilib_Header_array['others']=array(); // clear all other headers
	}

	__ph7_cgilib_header_update();
}

function __ph7_cgilib_setcookie($name, $value='', $expire=0, $path='', $domain='', $secure=false, $HTTPOnly=false, $raw=false) {
	if ( !empty($domain) ){
		// Fix the domain to accept domains with and without 'www.'.
		if ( strtolower( substr($domain, 0, 4) ) == 'www.' ) $domain = substr($domain, 4);
		// Add the dot prefix to ensure compatibility with subdomains
		if ( substr($domain, 0, 1) != '.' ) $domain = '.'.$domain;

		// Remove port information.
		$port = strpos($domain, ':');

		if ( $port !== false ) $domain = substr($domain, 0, $port);
	}

	header('Set-Cookie: '.rawurlencode($name).'='.($raw?$value:rawurlencode($value))
		.(empty($domain) ? '' : '; Domain='.$domain)
		.(empty($expire) ? '' : '; expires='.gmdate('l, d-M-y H:i:s', $expire).' GMT')
		.(empty($path) ? '' : '; Path='.$path)
		.(!$secure ? '' : '; Secure')
		.(!$HTTPOnly ? '' : '; HttpOnly'), false);
	return true;
}

function __ph7_cgilib_ip2long($ip){
	$ipparts=explode('.',$ip);
	return intval($ipparts[0])<<24 | intval($ipparts[1])<<16 | intval($ipparts[2])<<8 | intval($ipparts[3]);
}

function __ph7_cgilib_long2ip($iplong){
	return (intval($iplong) >> 24).'.'.((intval($iplong)<<8)>>24).'.'.((intval($iplong)<<16)>>24).'.'.((intval($iplong)<<24)>>24);
}

/* Wrappers */
function header($str) { __ph7_cgilib_header($str); }
function setcookie($name, $value='', $expire=0, $path='', $domain='', $secure=false, $HTTPOnly=false) { return __ph7_cgilib_setcookie($name, $value, $expire, $path, $domain, $secure, $HTTPOnly, false); }
function setrawcookie($name, $value='', $expire=0, $path='', $domain='', $secure=false, $HTTPOnly=false) { return __ph7_cgilib_setcookie($name, $value, $expire, $path, $domain, $secure, $HTTPOnly, true); }
function ip2long($ip){ return __ph7_cgilib_ip2long($ip); }
function long2ip($iplong){ return __ph7_cgilib_long2ip($iplong); }
function header_remove($header='') { __ph7_cgilib_header_remove($header); }

/* Autorun $_ENV/STDIN importing */
__ph7_cgi_parseHTTPHeader(__ph7_cgilib_envToHTTPRequest());
?>