<?
include "mysqlcon.php";

if(isset($_GET['i'])) { $i = $_GET['i']; } else { $i = 0; }
if(isset($_GET['u'])) { $u = $_GET['u']; } else { $u = 0; }

$code = strrev(substr(strrev($u), 0, 5));
$u = strrev(substr(strrev($u), 5));

if(mysql_num_rows(mysql_query("SELECT * FROM ims WHERE u_id='$u' AND code='$code'")) != 1) { exit(); }

$image = @mysql_num_rows(mysql_query("SELECT * FROM ims_image WHERE u_id='$u' AND i_id='$i'"));

if($i == 0 | $u == 0 | $image == 0) {
$filename = "./icons/notavail.gif";
$mime = "image/gif";
$image_name = "notavail.gif";

} else {
$image_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims_image WHERE u_id='$u' AND i_id='$i'"));
$folder_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims_folders WHERE u_id='$u' AND f_id='$image_info[f_id]'"));
$user_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims WHERE u_id='$u'"));

if(date("n", time()) == 2 | date("n", time()) == 4 | date("n", time()) == 6 | date("n", time()) == 8 | date("n", time()) == 9 | date("n", time()) == 11 | date("n", time()) == 1) {
$days = 31;
} elseif(date("n", time()) == 5 | date("n", time()) == 7 | date("n", time()) == 10 | date("n", time()) == 12) {
$days = 30;
} elseif(date("n", time()) == 3) {
$days = 28;
}

if((time() - $days*24*60*60) >= $user_info[reset]) {
$reset = $user_info[reset];
$old_daylight_saving_time = date("I", $reset);
while((time() - $days*24*60*60) >= $reset) {
if(date("n", $reset) == 1 | date("n", $reset) == 3 | date("n", $reset) == 5 | date("n", $reset) == 7 | date("n", $reset) == 8 | date("n", $reset) == 10 | date("n", $reset) == 12) {
$actual_days = 31;
} elseif(date("n", $reset) == 4 | date("n", $reset) == 6 | date("n", $reset) == 9 | date("n", $reset) == 11) {
$actual_days = 30;
} elseif(date("n", $reset) == 2) {
$actual_days = 28;
}

$old_reset = $reset;
$reset = $reset + $actual_days*24*60*60;
if(date("I", $old_reset) != date("I", $reset)) {
  if(date("I", $reset) == 1) {
  $reset = $reset - 60*60;
  } elseif(date("I", $reset) == 0) {
  $reset = $reset + 60*60;
  }
}
}
mysql_query("UPDATE ims SET reset='$reset',bandwidth_used='0' WHERE u_id='$user_info[u_id]'");
$user_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims WHERE u_id='$user_info[u_id]'"));
}

$newviews = $image_info[views] + 1;
mysql_query("UPDATE ims_image SET views='$newviews' WHERE i_id = '$image_info[i_id]'");

  if(($user_info[bandwidth_used]/(1024*1024)) >= $user_info[bandwidth_allotted]) {
  $filename = "./icons/notavail.gif";
  $mime = "image/gif";
  $image_name = "notavail.gif";
  } else {
  $filename = "./u/$u/$folder_info[f_id]/".$image_info[i_id].".".$image_info[ext];
  $image_name = "image_".$image_info[i_id].".".$image_info[ext];
  $bandwidth_used = filesize($filename) + $user_info[bandwidth_used];
  mysql_query("UPDATE ims SET bandwidth_used='$bandwidth_used' WHERE u_id='$u'");
    if($image_info[ext] == "jpeg" | $image_info[ext] == "jpg") {
    $mime = "image/jpeg";
    } elseif($image_info[ext] == "png") {
    $mime = "image/x-png";
    } elseif($image_info[ext] == "bmp") {
    $mime = "image/bmp";
    } elseif($image_info[ext] == "gif") {
    $mime = "image/gif";
    } elseif($image_info[ext] == "pdf") {
    $mime = "application/pdf";
    }
  }
}


$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);

header("Content-Type: $mime");
header("Content-Disposition: inline; filename=$image_name");
header("Pragma: no-cache");
header("Expires: 0");


echo $contents;

?>