<?php

  /*

      files.php

      Allows an issue's attached files to be viewed or edited. [Commented]

      ----

      Copyright (C) 2006  Central Manchester CLC
      Copyright (C) 2006  David Thorne (davethorne@gmail.com)

      This program 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 version 2.

      This program 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 this program; if not, write to the Free Software
      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

  */

  // Start session and populate it with data
  session_start();

  // Page security
  require_once 'system/security.php';
  if ($_SESSION['_usertype'] == '') {
    ThrowOut();
  }

  // Page Title
  $ptitle = 'Attached Files';

  // Includes
  require_once 'system/db.php';
  require_once 'system/lang.php';
  require_once 'system/message.php';
  require_once 'system/user_preferences.php';
  require_once 'system/global_preferences.php';
  require_once 'system/functions.php';
  require_once 'system/attachments.php';
  
  // Language selection
  set_text_domain("files");

  // Retrieve Get/Post variables
  $act = $_REQUEST['act'];
  $issue_id = $_REQUEST['issue_id'];
  $filename = urldecode($_REQUEST['filename']);
  $directory = get_attachment_dir($issue_id);
  if ($directory != "" && file_exists($directory."/".$filename)) {
    header("Content-Disposition: attachment; filename=".$filename);
    chdir($directory);
    if (function_exists('finfo_open')) {
      $finfo = finfo_open(FILEINFO_MIME);
      header("Content-type: ".finfo_file($finfo, getcwd()."/".$filename));
      finfo_close($finfo);
    }
    readfile($filename);
    exit;
  } else {
    // Includes
    require 'header.php';
  
    // Language selection
    set_text_domain("issue");

    print "  <div class=\"maintitle\">\n    <h1>".gettext('Error')."</h1>\n  </div>\n
      <div class=\"maindark\">\n    <p>".gettext('Specified attachment does not exist')."</p>\n
      </div>\n  <div class=\"main\">\n</div>";
      
    require 'footer.php';
    exit;
  }
?>

