<?php
set_time_limit(60*10);
require_once('local_auth.inc.php');
$db = Database::getDatabase();

/* resulting csv data */
$formattedCSVData = array();

/* header */
$lArr = array();
$lArr[] = "Id";
$lArr[] = "Short Url";
$lArr[] = "Original Url";
$lArr[] = "Created Date";
$lArr[] = "Created IP";
$lArr[] = "Visits";
$lArr[] = "Access Password";
$lArr[] = "Expires";
$lArr[] = "Available Uses";
$lArr[] = "Last Accessed";
$lArr[] = "Status";
$formattedCSVData[] = "\"" . implode("\",\"", $lArr) . "\"";
	
/* get all url data */
$urlData = $db->getRows("SELECT * FROM shorturl ORDER BY dateCreated asc");
foreach($urlData AS $row)
{
	$lArr = array();
	$lArr[] = $row['id'];
	$lArr[] = WEB_ROOT."/".$row['shortUrl'];
	$lArr[] = $row['originalUrl'];
	$lArr[] = dater($row['dateCreated']);
	$lArr[] = $row['createdIP'];
	$lArr[] = $row['visits'];
	$lArr[] = $row['password'];
	$lArr[] = ($row['expiryDate']!="0000-00-00 00:00:00")?dater($row['expiryDate']):"";
	$lArr[] = $row['totalUses'];
	$lArr[] = ($row['lastAccessed']!="0000-00-00 00:00:00")?dater($row['lastAccessed']):"";
	$lArr[] = $row['status'];
	
	$formattedCSVData[] = "\"" . implode("\",\"", $lArr) . "\"";
}

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT\n");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Content-type: application/zip;\n"); //or yours?
header("Content-Transfer-Encoding: binary");
//$len = strlen($formattedCSVData);
//header("Content-Length: $len;\n");
$outname = "url_data.csv";
header("Content-Disposition: attachment; filename=\"$outname\";\n\n");

echo implode("\n", $formattedCSVData);