<?php
/* Copyright (C) 2016      Garcia MICHEL <garcia@soamichel.fr>
 *
 * 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; either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */

header('Content-type: text/plain; charset=utf-8');

if(false === (@include '../../main.inc.php')){ // From htdocs directory
  if(false === (@include '../../../main.inc.php')){ // From "custom" directory
		http_response_code(500);
		die('Missing main.inc.php file');
	}
}

if(!$conf->autoupgrade->enabled or !$user->admin){
	http_response_code(401);
	die('Unauthorized');
}

$filename = GETPOST('file');
if($filename == ''){
	http_response_code(400);
	die('Missing filename');
}

$file = DOL_DATA_ROOT.'/autoupgrade/logs/'.$filename;
if(!file_exists($file)){
	http_response_code(404);
	die('Not found log file : '.$filename);
}

header('Content-length: '.filesize($file));
header('Content-disposition: attachment; filename="'.$filename.'"');
ob_clean();
flush();
readfile($file);
?>
