<?php
/**
 *  Copyright (C) 2021  Cover Tower LLC
 *
 *  This file is part of Roundpin, which is licensed under the
 *  GNU Affero General Public License Version 3.0. The license terms
 *  are detailed in the "LICENSE.txt" file located in the root directory.
 */

session_start();

if (isset($_GET['s_ajax_call']) && ($_GET['s_ajax_call'] == $_SESSION['validate_s_access']) && isset($_GET['destSipUser']) &&
    $_GET['destSipUser'] != '' && isset($_GET['sentFlNm']) && $_GET['sentFlNm'] != '') {

    $destSipUser = $_GET['destSipUser'];
    $sentFileName = $_GET['sentFlNm'];
    $fileToDownload = 'textchat/' . $destSipUser . '/uploads/' . $sentFileName;

    // Process the download
    if (is_file($fileToDownload)) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename="' . basename($fileToDownload) . '"');
            header('Expires: 0');
            header('Cache-Control: no-cache, must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($fileToDownload));
            flush();
            readfile($fileToDownload);
            exit();
    } else {
            http_response_code(404);
	    exit();
    }

} else {
        header("Location: roundpin-login.php");
}

?>