<?php
$locale->setSection("Admin: Logs");

if (!$users->userPerms->isSuperUser()) {
	jsonFeedback(false, "You are not allowed to use this function.");
}


/*
Data processing
*/
if ($_GET['action'] == "archive") {
	if ($config['misc']['demoMode']) {
		jsonFeedback(false, "Action unavailable in this demo version of the software!");
	} else {
		$rs = $log->select("*", false, array("id" => "DESC"));
		$countDBRecords = sizeof($rs);
		if (!$rs) {
			jsonFeedback(false, "Failed to retrieve log data!");
		}
		$path = $config['path']['data']."/activity_logs_".date("d-m-y")."_".time().".txt";
		$fp = fopen($path, "x");
		if (!$fp) {
			jsonFeedback(false, "Failed to create archive file!");
		} else {
			$count = 0;
			foreach ($rs as $key => $val) {
				$val['username'] = $users->getUsernameById($val['uid']);
				$rs2 = fputcsv($fp, $val, ";");
				if ($rs2) {
					$count++;
				}
			}
			fclose($fp);
			if ($countDBRecords == $count) {
				$rs = $log->delete(array("1", "=", "1"));
				if (!$rs) {
					jsonFeedback(false, "Failed to clear logs!");
				} else {
					jsonFeedback(true, "Activity log has been successfuly archived!");
				}
			} else {
				jsonFeedback(false, "Database records ".$countDBRecords.", writen ".$count);
			}
		}
	}
}

/*
Data gathering and setup
*/

/*
Display
*/
