<?php  
// MARKED FOR REMOVAL IN FUTURE VERSIONS :: OBSOLETE
exit(); 



ob_start(); 


global $wpdb;

if (! $_POST) return;

	$pack_name = stripslashes($_POST['pack_name']);
	$pack_description = stripslashes($_POST['pack_description']);
	$pack_author = stripslashes($_POST['pack_author']);
	$pack_author_url = stripslashes($_POST['pack_author_url']);
	
	// Build the WHERE clause to get the buttons
	$where = '';	
	foreach ($_POST as $key => $value) {
		$buttons = $_POST["maxbutton"]; 
		
	
		
	
	// Get the pack name by replacing spaces with dashes and stripping out any
	// special chars by only accepting letters, numbers, underscores, and dashes
	$pack_name = str_replace(' ', '-', $pack_name);
	$pack_name = preg_replace('/[^a-zA-Z0-9_-]/s', '', $pack_name);
	$pack_name = strtolower($pack_name);
	
	// Create the pack folder in the exports folder
	$pack_folder = MAXBUTTONS_PRO_EXPORTS_DIR . '/' . $pack_name . '/';

	if (! is_dir($pack_folder)) 
		mkdir($pack_folder, 0777, true);
	
	if (! is_dir($pack_folder)) 
	{  echo("Could not read: $pack_folder - Check your file permissions");
		return;
	}
	
	// The root node
	$xml = '<maxbuttons>';
	
	// Pack element and attributes
	$xml .= '<pack ';
	$xml .= 'name="' . stripslashes($_POST['pack_name']) . '" ';
	$xml .= 'description="' . $pack_description . '" ';
	$xml .= 'author="' . $pack_author . '" ';
	$xml .= 'author_url="' . $pack_author_url . '" ';
	$xml .= '/>';
	
	// Build elements and attributes for each button
	foreach ($buttons as $index => $button_id) {
		$button->set($button_id); 
		$data = $button->get(); 
 
		$xml .= '<maxbutton> ';
		$xml .= '<name>' . htmlspecialchars($button->getName()) . '</name>';
		$xml .= '<status>' . htmlspecialchars($button->getStatus()) . '</status>';
		
		
		$blocks = $button->getDefinedBlocks(); 
	 
		foreach($blocks as $block)
		{
			if (isset($data[$block])) 
			{
				if ($block  == 'icon' && $data[$block]['icon_url'] != '')
					$data[$block]["icon_url"] = handle_icon_file($data[$block]["icon_url"], $pack_folder); 
				$xml .= "<$block><![CDATA[" .  json_encode($data[$block]) . "]]></$block>"; 
			}
		}
		$xml .= "</maxbutton>"; 
 
	}
	
	// Close the root node
	$xml .= '</maxbuttons>';
	
  
 
	// Write xml to file
	$file_name = $pack_name . '.xml';
	$file_path = $pack_folder . $file_name;
	$file_handle = fopen($file_path, 'w');
	fwrite($file_handle, $xml);
	fclose($file_handle);
	
	// Start creating the zip file
	$zip = new ZipArchive();
	$zip_overwrite = 1;
	$zip_archive = MAXBUTTONS_PRO_EXPORTS_DIR . '/' . $pack_name . '.zip';
	$zip_archive_url = MAXBUTTONS_PRO_EXPORTS_URL . '/' . $pack_name . '.zip';
	
	if($zip->open($zip_archive, $zip_overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) === true)
	{
		// This is the root folder inside the zip archive
		$zip_root_folder = $pack_name . '/';
		$zip->addEmptyDir($zip_root_folder);
		
		// Get files recursively
		$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($pack_folder));

		// Add each file to the zip archive in the root folder
		foreach ($files as $file) {
			$file = str_replace('\\', '/', realpath($file));
			if (is_file($file) === true) {
				if ($file != '.' && $file != '..') {
					$zip->addFile($file, $zip_root_folder . basename($file));
				}
			}
		}

		$zip->close();
	}
	
	// Now delete the folder
	mbpro_delete_folder($pack_folder);

	// And finally set the zip file for download
	ob_clean(); // delete any previous output;
	header('Content-type: application/zip');
	header('Content-disposition: attachment; filename="' . $pack_name . '.zip"');
	//readfile($zip_archive_url);
	readfile($zip_archive);
	exit();
}

function mbpro_delete_folder($dir) {
	if (is_dir($dir)) {
		$objects = scandir($dir);
		foreach ($objects as $object) {
			if ($object != '.' && $object != '..') {
				if (filetype($dir . '/' . $object) == 'dir') {
					// Recursive call to delete the folder
					mbpro_delete_folder($dir . '/' . $object);
				}
				else {
					// Delete the file
					unlink($dir . '/' . $object);
				}
			}
		}
		
		reset($objects);
		rmdir($dir);
	}
}

function handle_icon_file($icon_url, $pack_folder)
{
	if ($icon_url != '') {
		
		// Download temp file and remove its extension
		$temp_file = download_url($icon_url);
		
		// If the result of download_url() is an object, then we know it's the WP_Error object most likely
		// caused by an HTTP 404 error when trying to download the icon. If that happens, we just ignore it.
		if (!is_object($temp_file)) {
			$temp_file_no_ext = substr($temp_file, 0, -3);
			
			// Rename temp file with extension from icon
			$icon_ext = substr($icon_url, -3, 3);
			$temp_file_new = $temp_file_no_ext . $icon_ext;
			rename($temp_file, $temp_file_new);
			
			// Copy the file into the pack folder and delete the original
			copy($temp_file_new, $pack_folder . basename($temp_file_new));
			unlink($temp_file_new);
			
			// Put the basename of the file into the XML
			return basename($temp_file_new); 
		}
	}
}		
		
?>
