<?php
///////////////////////////////////////////////////////////////////////////////////////////
///Script PHP/MYSQL of management of classifieds ads developed by Script PAG
///Script PAG all rights reserved. Use under license. http://www.script-pag.com
///////////////////////////////////////////////////////////////////////////////////////////

############################################################

use Spipu\Html2Pdf\Html2Pdf;
require_once __DIR__.'/includes_adm/all_fns_adm.php';

///////////////////////////////////
//Check if the super admin is connected
//////////////////////////////////
	
if(!empty($_SESSION['valid_admin']))
$valid_admin = $_SESSION['valid_admin'];

if(!check_super_admin())
redirect('index.php');

///////////////////////////////////
//Initializing variable $error
//////////////////////////////////

$error = '';

///////////////////////////////////
//Search user data
//////////////////////////////////
if(isset($_GET['search_user']) && isset($_GET['year']) && !empty($_GET['year'])) {
	$path = __DIR__.'/../upload/consents/'.$_GET['year'];
	$topath =  __DIR__.'/temporary_files_adm/consents_'.$_GET['year'].'.zip';
	if(is_dir($path)) {
		$zip = new ZipArchive;
		if($zip->open($topath, ZipArchive::CREATE) == true) {
			$handler = opendir($path);
			while($file = readdir($handler)) {
				if($file != '.' && $file != '..' && $file != '.DS_Store') {
					$file_name = explode(DIRECTORY_SEPARATOR, $file);
					$zip->addFile($path.DIRECTORY_SEPARATOR.$file, 'consents_'.$_GET['year'].'/'.end($file_name));
				}
			}
			$zip->close();
		}
		
		header('Content-disposition: attachment; filename=consents_'.$_GET['year'].'.zip'); 
		header('Content-Type: application/force-download'); 
		header('Content-Transfer-Encoding: fichier');  
		header('Content-Length: '.filesize($topath)); 
		header('Pragma: no-cache'); 
		header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0'); 
		header('Expires: 0'); 
		readfile($topath);
	}
	else $error = 1;
}

///////////////////////////////////
//Close database connexion
//////////////////////////////////

$conn = null;

///////////////////////////////////
//Display view
//////////////////////////////////

$view = array('template_adm/tpl_user_cookies_adm.php');
require_once __DIR__.'/template_adm/tpl_layout_adm.php';
