<?php
/*
script: bandwidth.php
purpose: checks all image requests and either displays the image, or the bandwidth exceeded message.
				logs bandwidth usage for images viewed.
copyright: copyright 2004 Php Image Host. All Rights Reserved. You may modify this file for use on your
					site running a licenced copy of the Php Image Host scripts.
					You must not distribute this file or derivations of it.
Nullified By CrAcKlOvE

*/

// include site configuration

require_once(dirname(__FILE__).'/includes/config.inc.php');


// get the image to view by username and image name
$u = preg_replace('/[^a-z0-9_-]/i', '', $ace->getstr('u', $_REQUEST));
$i = preg_replace('/[^a-z0-9_.-]/i', '',$ace->getstr('i', $_REQUEST));

$iname = $ace->config->image_folder.$u.'/'.$i;

$dot = strpos($i, '.');
$ctype = 'image/pjpeg';

if( $dot != -1 ){
	$type = substr($i, $dot+1);
	if( $type != 'jpg' )
	$ctype = 'image/'.$type;
}

$sql = "SELECT u.bandwidth_exceeded, i.image_id FROM {pa_dbprefix}images i, {pa_dbprefix}users u ";
$sql .="WHERE CONCAT(i.name, '.', i.type)='".addslashes($i)."' ";
$sql .="AND u.username='".addslashes($u)."' AND i.user_id=u.user_id ";
$res = $ace->query($sql, 'Get Image And user');

$img = mysql_fetch_object($res);

header("Status: 200 Ok");
header("Content-Disposition: inline;");
header('Pragma:');
header('Cache-Control:');

if( $img && @file_exists($iname) ){

	$ref = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
	$isadmin = false;
	if( preg_match('/admin\//i', $ref) ){
		if(strpos($ref, $ace->config->siteurl) !== false){
			$isadmin = true;
		}
	}
	if( $img->bandwidth_exceeded && !$isadmin ){

//		header("Content-Type: image/png");
		readfile(dirname(__FILE__).'/bandwidth_exceeded.png');

	}else{
		$lastModified = filemtime($iname);
		header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $lastModified) . ' GMT');
		header("Content-Type: $ctype");
		header('Expires: ' . gmdate("D, d M Y H:i:s", time() + 3600 * 72) . " GMT");
		ob_start();
		@readfile($iname);
		ob_end_flush();
		if( !$isadmin ){
			$sql = "UPDATE images SET bandwidth=bandwidth+filesize WHERE image_id={$img->image_id} ";
			@mysql_query($sql);
		}
	}
}else{
	header("Content-Type: image/png");
	@readfile(dirname(__FILE__).'/not_found.png');
}

exit();

?>
