<?php
error_reporting(0);

function get($url)
{
	$ch = curl_init($url);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_HEADER, true);
	curl_setopt($ch, CURLOPT_NOBODY, true);
	$data = curl_exec($ch);
	$filesize = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
	curl_close($ch);
	return $filesize;
}

if (file_exists('ovpn.zip')) {
	unlink('ovpn.zip');
}

$rootPath = realpath('./../vpn');
$zip = new ZipArchive();
$zip->open('ovpn.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
$filesToDelete = [];
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($rootPath), RecursiveIteratorIterator::LEAVES_ONLY);

foreach ($files as $name => $file) {
	if (!$file->isDir()) {
		$filePath = $file->getRealPath();
		$relativePath = substr($filePath, strlen($rootPath) + 1);
		$zip->addFile($filePath, $relativePath);
	}
}

$zip->close();
$url = 'ovpn.zip';
$real_size = get($url);
header('Content-Description: File Transfer');
header('Content-Type: application/zip');
header('Content-length: ' . $real_size);
header('Content-Disposition: attachment; filename=' . $url);
readfile($url);
exit();
