<?php
# Mantis - a php based bugtracking system

# Copyright (C) 2000 - 2002  Kenzaburo Ito - kenito@300baud.org
# Copyright (C) 2002 - 2007  Mantis Team   - mantisbt-dev@lists.sourceforge.net

# Mantis is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# Mantis is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Mantis.  If not, see <http://www.gnu.org/licenses/>.

	# --------------------------------------------------------
	# $Id: user_avatar_image.php,v 1.1 2008-02-12 22:06:45 ricardop Exp $
	# --------------------------------------------------------

	require_once( 'core.php' );

	$t_core_path = config_get( 'core_path' );

	require_once( $t_core_path . 'user_api.php' );
	
	$user_id = gpc_get_int( 'id' );
	
	$avatarDef = user_get_avatar( $user_id, false);
	$avatarURL = $avatarDef[0];
	
	if ($avatarURL) {
		ob_end_clean();
		ob_start();
		
		// Request headers. We take these from the request we received, and relay it back to gravatar.com
		$header = array();
		if ($_SERVER['HTTP_PRAGMA']) $header[] = 'Pragma: no-cache';
		if ($_SERVER['HTTP_CACHE_CONTROL']) $header[] = 'Cache-Control: no-cache';
		if ($_SERVER['HTTP_IF_MODIFIED_SINCE']) $header[] = 'If-Modified-Since: '.$_SERVER['HTTP_IF_MODIFIED_SINCE'].'';
		if ($_SERVER['HTTP_IF_NONE_MATCH']) $header[] = 'If-None-Match: '.$_SERVER['HTTP_IF_NONE_MATCH'].'';
		if ($_SERVER['HTTP_USER_AGENT']) $header[] = 'User-Agent: '.$_SERVER['HTTP_USER_AGENT'].'';
		
		if ($_SERVER['HTTP_ACCEPT']) $header[] = 'Accept: '.$_SERVER['HTTP_ACCEPT'].'';
		if ($_SERVER['HTTP_ACCEPT_LANGUAGE']) $header[] = 'Accept-Language: '.$_SERVER['HTTP_ACCEPT_LANGUAGE'].'';
		if ($_SERVER['HTTP_ACCEPT_ENCODING']) $header[] = 'Accept-Encoding: '.$_SERVER['HTTP_ACCEPT_ENCODING'].'';
		if ($_SERVER['HTTP_ACCEPT_CHARSET']) $header[] = 'Accept-Charset: '.$_SERVER['HTTP_ACCEPT_CHARSET'].'';
		
		$ch = curl_init();

		// set URL and other appropriate options
		curl_setopt($ch, CURLOPT_URL, $avatarURL);
		curl_setopt($ch, CURLOPT_HEADER, 1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER , 1 );
		curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

		// grab URL and pass it to the browser
		$response = curl_exec($ch);
		$info = curl_getinfo($ch);

		$code = $info['http_code'];
		$type = $info['content_type'];
		$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
		curl_close($ch);
		$headers = substr($response, 0, $header_size);
		$body = substr($response, $header_size);
				
		// PHP insists in sending some unwanted headers, clean those
		ob_end_clean();
		header("Cache-Control: ");
		header("Pragma: ");
		header("Expires: ");
		header("Date: ");
		header("ETag: ");
		header("Last-Modified: ");
		header("Age: ");
		header("Via: ");
				
		// Proxy the headers, directly to the client. This includes the status code. Clean the response first.
		$headers_array = explode("\n", $headers);
		foreach ($headers_array as $oneHeader) {
			header(trim($oneHeader));
		}
		echo $body;
		die(0);
	}
	
	
