<?php

/**
 *
 * @copyright  2020 objectivejs.org
 * @version    1
 * @link       http://www.objectivejs.org
 */

require_once 'filemimetype.php';
require_once 'validatefilename.php';
require_once 'models/wall.inc';

function wallfile($lang, $arglist=false) {
	if (!isset($_SESSION['wall_id'])) {
		goto badrequest;
	}

	$wall_id=$_SESSION['wall_id'];

	$name=false;

	if (is_array($arglist)) {
		if (isset($arglist[0])) {
			$name=$arglist[0];
		}
	}

	if (!$name or !validate_filename($name)) {
		goto badrequest;
	}

	$file=wall_file($wall_id, $name);

	if (!file_exists($file)) {
		goto notfound;
	}

	$filesize=filesize($file);
	$filetype=file_mime_type($file);
	if (!$filetype) {
		$filetype = 'application/octet-stream';
	}

	header("X-Sendfile: $file");
	header("Content-Type: $filetype");
	header("Content-Length: $filesize");

	return false;

badrequest:
	header('HTTP/1.1 400 Bad Request');
	return false;

notfound:
	header('HTTP/1.1 404 Not Found');
	return false;
}
