<? /*

File output script for Intellect Board 2 Project

(C) 2004, XXXX Pro, United Open Project
Visit us online: http://intboard.ru

*/

$IBOARD=1;
error_reporting(E_ERROR | E_WARNING | E_PARSE |E_CORE_ERROR | E_CORE_WARNING);

require("config/database.php");
require("config/iboard.php");
require("db/$DBdriver.php");

if ($DBpersist) $link=db_pconnect($DBhost,$DBusername,$DBpassword);
else $link=db_connect($DBhost,$DBusername,$DBpassword);
db_select_db($DBname,$link);

$fid = db_slashes($_GET['fid']);
$action = db_slashes($_GET['a']);
$phid = db_slashes($_GET['ph']);
$key = db_slashes($_GET['key']);

if ($action!="thumb" && $action!="photo") {
  $sql = "SELECT file_id,file_type,file_name FROM ".$GLOBALS['DBprefix']."File WHERE file_id=\"$fid\"";
  $res = db_query($sql,$link);
  if (db_num_rows($res)==0) echo "File not in database!";
  else {
    $file=db_fetch_row($res);
    db_free_result($res);
    if (!$action) {
      if ($_SERVER["HTTP_RANGE"]) {
          $length = filesize($GLOBALS['opt_dir']."/files/".$file[0].".htm");
          $range = $_SERVER["HTTP_RANGE"];
          $range = str_replace("bytes=", "", $range);
          $range = str_replace("-", "", $range);
      }
      if ($range) header("HTTP/1.1 206 Partial Content");
      else header("HTTP/1.1 200 OK");
      if (strpos($file[1],"image")===false) header("Content-Disposition: attachment; filename=".$file[2]);
      header("Accept-Ranges: bytes");
//      header("Content-Length: ".($length-$range));
      header("Content-Range: bytes $range-".($length-1)."/".$length);
      if ($file[1]) header("Content-type: ".$file[1]);
      else header("Content-type: application/octet-stream");
      $fh=fopen($GLOBALS['opt_dir']."/files/".$file[0].".htm","r");
      if ($range) $fseek($fh,$range);
      while ($buffer=fread($fh,16*1024)) echo $buffer;
      fclose($fh);      
    }
    elseif ($action=="preview") {
      $fh=fopen($GLOBALS['opt_dir']."/files/".$file[0].".htm","r");
      $buffer=fread($fh,filesize($GLOBALS['opt_dir']."/files/".$file[0].".htm"));
      fclose($fh);            
      $full = imagecreatefromstring($buffer);
      unset($buffer);
      $sizex = imagesx($full);
      $sizey = imagesy($full);
      if (!$opt_previewx) $opt_previewx=256;
      if (!$opt_previewy) $opt_previewy=64;
      $coeff=1;
      while ($sizex/$coeff>$opt_previewx) $coeff++;
      while ($sizey/$coeff>$opt_previewy) $coeff++;
      if ($coeff>1) {
        $thumb = imagecreate($sizex/$coeff,$sizey/$coeff);
        if ($GLOBALS['opt_GD2']) imagecopyresampled($thumb,$full,0,0,0,0,$sizex/$coeff,$sizey/$coeff,$sizex,$sizey);
        else imagecopyresized($thumb,$full,0,0,0,0,$sizex/$coeff,$sizey/$coeff,$sizex,$sizey);
        imagedestroy($full);
        header("Content-type: image/png");
        imagepng($thumb);
      }
      else {
        header("Content-type: ".$file[1]);
        readfile($GLOBALS['opt_dir']."/files/".$file[0].".htm");        
      }
    }
  }
}
elseif ($action=="thumb") {
  $sql = "SELECT ph_tid,ph_key FROM ".$GLOBALS['DBprefix']."Photo WHERE ph_id=\"$phid\" AND ph_key=\"$key\"";
  $res = db_query($sql,$link);
  list($thumb,$phkey)=db_fetch_row($res);
  if ($key==$phkey) {
    $len=filesize($GLOBALS['opt_dir']."/photos/previews/$thumb.jpg");
    header("Content-type: image/jpeg");
    header("Content-length: ".$len);
    readfile($GLOBALS['opt_dir']."/photos/previews/$thumb.jpg");
  }
  else echo "ERROR: BAD KEY";
}
elseif ($action=="photo") {
  $sql = "SELECT ph_tid,ph_key FROM ".$GLOBALS['DBprefix']."Photo WHERE ph_id=\"$phid\" AND ph_key=\"$key\"";
  $res = db_query($sql,$link);
  list($photo,$phkey)=db_fetch_row($res);
  if ($key==$phkey) {
    $len=filesize($GLOBALS['opt_dir']."/photos/$photo.jpg");
    header("Content-type: image/jpeg");
    header("Content-length: ".$len);
    readfile($GLOBALS['opt_dir']."/photos/$photo.jpg");
  }
  else echo "ERROR: BAD KEY";
}

