<?php

session_start();

require_once __DIR__ . '/inc/flash.php';
require_once __DIR__ . '/inc/functions.php';

$is_post_request = strtolower($_SERVER['REQUEST_METHOD']) === 'post';

if (!$is_post_request) {
    redirect_with_message('Invalid file download operation', FLASH_ERROR, 'download');
}

$code = $_POST['fm_dlcode'];

if($code < 1000) {
    redirect_with_message('Invalid Code', FLASH_ERROR, 'download');

}

$msg = 'Download code ' . $code . '<br/>';
$cmd = 'sudo -u admin /usr/sbin/diag/filemanage -u ' . $code;
//$cmd = '/usr/sbin/diag/filemanage -u ' . $code;
exec($cmd, $upd_buff, $return_val);
foreach($upd_buff as $line) {
    $msg .= $line . '<br/>';
}
if($return_val == 0) {
    $msgstyle = FLASH_SUCCESS;
} else {
    $msgstyle = FLASH_ERROR;
}

redirect_with_message($msg, $msgstyle, 'download');
