<?php ob_start(); ?>
<?php

define('PS_MBE_DIR', getcwd());
echo PS_MBE_DIR;

if (!include(PS_MBE_DIR . '/../../config/config.inc.php')) {
    die("Failed to include '/../../config/config.inc.php' (Check permissions)");
}
if (!include(PS_MBE_DIR . '/lib/pclzip.lib.php')) {
    die("Failed to include '/lib/pclzip.lib.php' (Check permissions)");
}


if (isset($_GET['files'])) {
    $files = $_GET['files'];
    if (count($files) > 1) {
        $dir = _PS_ROOT_DIR_ . '/media/mbe/';
        $path = $dir . 'compressed.zip';
        if (file_exists($path)) {
            unlink($path);
        }
        $values = array();
        foreach ($files as $f) {
            $paths = explode('/', $f);
            $values[] = end($paths);
        }
        chdir($dir);
        $archive = new PclZip($path);
        $response = $archive->create($values);
        if ($response == 0) {
            die("[ERROR] PclZip : " . $archive->errorInfo(true));
        }

        if (headers_sent()) {
            echo 'HTTP header already sent';
        }
        else {
            if (!is_file($path)) {
                header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
                echo 'File not found';
            }
            else {
                if (!is_readable($path)) {
                    header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');
                    echo 'File not readable';
                }
                else {
                    header($_SERVER['SERVER_PROTOCOL'] . ' 200 OK');
                    header("Content-Type: application/zip");
                    header("Content-Transfer-Encoding: Binary");
                    header("Content-Length: " . filesize($path));
                    header("Content-Disposition: attachment; filename=\"" . basename($path) . "\"");
                    ob_clean();
                    flush();
                    readfile($path);
                    exit;
                }
            }
        }
        die;
    }
    else {
        $file = $files[0];
        $path = explode('/', $file);
        $name = end($path);
        $names = explode('.', $name);
        $ext = end($names);
        if ($ext == 'html') {
            header("Content-Disposition:inline;filename='" . $name . "'");
            header("Content-length: " . filesize($file));
            if(ob_get_length() > 0) {
				ob_clean();
			}
            flush();
            readfile($file);
            exit;
        }
		else  if ($ext == 'gif') {
            header("Content-type:image/gif");
            header("Content-Disposition:inline;filename='" . $name . "'");
            header("Content-length: " . filesize($file));
            if(ob_get_length() > 0) {
				ob_clean();
			}
            flush();
            readfile($file);
            exit;
        }
        else {
            header("Content-type:application/pdf");
            header("Content-Disposition: inline;filename='" . $name . "'");
            header("Content-length: " . filesize($file));
            if(ob_get_length() > 0) {
				ob_clean();
			}
            flush();
            readfile($file);
            exit;
        }
        die;
    }
}
else {
    die('Missing params');
}