<!DOCTYPE HTML>
<html>
<head>
	<title>VHost File Audits</title>
	<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
	<link href="contrib/contextmenu/dist/jquery.contextMenu.min.css" rel="stylesheet">
	<style type="text/css">
	
		.file-action {
		}
		
		.audit-files {
			margin: 0 0 0.7em 0.7em;
		}
		
		table tr.even td {
			background-color: #F5F5F5;
		}
		
		.audit-files .file {
			cursor: context-menu;
		}
		
		.file_status {
			color: #00AA00;
		}
		
	</style>
</head>
<body>

<div class="container">

	<h1>VHost File Audits</h1>
	
<? IF (count($errors)): ?>
	<div class="alert alert-danger">
		<strong>Error:</strong><br>
	<? FOREACH ($errors as $error): ?>
		<?= HtmlSpecialChars($error); ?><br>
	<? ENDFOREACH; ?>
	</div>
<? ENDIF; ?>

	<div class="alert alert-warning">
		Sites <span class="badge"><?= HtmlSpecialChars($site_count); ?></span> 
		&nbsp;
		File Changes <span class="badge"><?= HtmlSpecialChars($tot_changes); ?></span> 
		&nbsp;
		Script Changes <span class="badge"><?= HtmlSpecialChars($tot_scripts); ?></span> 
		&nbsp;
		Unacknowledged Changes <span class="badge"><?= HtmlSpecialChars($tot_unacked_changes); ?></span> 
		&nbsp;
		<a href="" id="filter_unacked_scripts">Unacknowledged Script Changes <span class="badge"><?= HtmlSpecialChars($tot_unacked_scripts); ?></span></a> 
		&nbsp;
	</div>


	<table class="table">
	<thead>
	<tr>
		<th>Location</th>
		<th>Last Run</th>
	</tr>
	</thead>

	<tbody>
<? FOREACH ($sites AS $i => $site ): ?>
	<tr class="<?= ($i % 2 === 0)?(' even'):(' odd'); ?>">
		<td>
			<span class="site"><?= HtmlSpecialChars($site); ?></span>
		</td>
		<td>
	<? IF ( time() - $audits[$site]['lastrun'] > $warn_age): ?>
			<mark>
			<?= HtmlSpecialChars(date('m/d/y', $audits[$site]['lastrun'])); ?>
			</mark>
	<? ELSE: ?>
			<?= HtmlSpecialChars(date('m/d/y', $audits[$site]['lastrun'])); ?>
	<? ENDIF; ?>
		</td>
	</tr>	
	<tr class="<?= ($i % 2 === 0)?(' even'):(' odd'); ?>">
		<td colspan="2">
	<? IF ( isset($audits[$site]['data']) ): ?>
		<? FOREACH ($audits[$site]['data'] AS $unix_date => $audit ): ?>
			<div>
				<a href="#<?= HtmlSpecialChars($slugs[$site] . '_' . $unix_date); ?>" role="button" data-toggle="collapse">
				<span class="glyphicon glyphicon-list-alt"></span>
				<?= HtmlSpecialChars(date('m/d/y', $unix_date)); ?>
				</a>
				
			<? IF ( count($audit['files']) === 0 ): ?>
				<span class="label label-success">
				<?= HtmlSpecialChars(count($audit['files'])); ?> changes
				</span>
			<? ELSE: ?>
				<span class="label label-default">
				<?= HtmlSpecialChars(count($audit['files'])); ?> changes
				</span>
				
				&nbsp;
				&nbsp;
				
				<? IF ( $audit['added'] > 0 ): ?>
				<span class="label label-info">
				<?= HtmlSpecialChars($audit['added']); ?> added
				</span>
				
				&nbsp;
				<? ENDIF; ?>
				
				<? IF ( $audit['deleted'] > 0 ): ?>
				<span class="label label-primary">
				<?= HtmlSpecialChars($audit['deleted']); ?> deleted
				</span>
				
				&nbsp;
				<? ENDIF; ?>
				
				<? IF ( $audit['changed'] > 0 ): ?>
				<span class="label label-warning">
				<?= HtmlSpecialChars($audit['changed']); ?> changed
				</span>
				
				&nbsp;
				<? ENDIF; ?>
				
				<? IF ( $audit['scripts'] > 0 ): ?>
				<span class="label label-danger">
				<?= HtmlSpecialChars($audit['scripts']); ?> Script
				</span>
			<? ENDIF; ?>
			<? ENDIF; ?>
				
			</div>
			
			<div class="well well-sm collapse" id="<?= HtmlSpecialChars($slugs[$site] . '_' . $unix_date); ?>">
				<p>
				Differences between files in the live site at <?= HtmlSpecialChars(date('h:m a', $audits[$site]['lastrun'])); ?> on <?= HtmlSpecialChars(date('m/d/y', $audits[$site]['lastrun'])); ?> and the backup from <?= HtmlSpecialChars(date('h:m a', $unix_date)); ?> on <?= HtmlSpecialChars(date('m/d/y', $unix_date)); ?>:
				</p>

			<? IF ( count($audit['files']) ): ?>
			<table class="audit-files">
				<? FOREACH ( $audit['files'] AS $file => $info ): ?>
				<tr>
					<td>
					<? IF ($info['action'] == 'added' ): ?>
					<span class="file-action label label-info">
					<? ELSEIF ($info['action'] == 'deleted' ): ?>
					<span class="file-action label label-primary">
					<? ELSEIF ($info['action'] == 'changed' ): ?>
					<span class="file-action label label-warning">
					<? ENDIF; ?>
					<?= HtmlSpecialChars($info['action']); ?>
					</span>
					</td>
					<td>
					<? $is_script = false; ?>
					<? FOREACH ($scripts AS $script): ?>
						<? IF ( preg_match($script, $file) ): ?>
						<? $is_script = true; ?>
						<? break; ?>
						<? ENDIF; ?>
					<? ENDFOREACH; ?>
				
					<? IF ( $is_script ): ?>
						<mark class="file" data-site="<?= HtmlSpecialChars($site); ?>" data-status="<?= HtmlSpecialChars($info['status']); ?>"><?= HtmlSpecialChars($file); ?></mark>
					<? ELSE: ?>
						<span class="file" data-site="<?= HtmlSpecialChars($site); ?>" data-status="<?= HtmlSpecialChars($info['status']); ?>"><?= HtmlSpecialChars($file); ?></span>
					<? ENDIF; ?>

					<? IF ( isset($info['status']) ): ?>
						<? IF ( $info['status'] == 'acknowledge' ): ?>
					<span class="file_status glyphicon glyphicon-ok-sign"></span>
						<? ELSEIF ( $info['status'] == 'ignore_file' ): ?>
					<span class="file_status glyphicon glyphicon-minus-sign"></span>
						<? ELSEIF ( $info['status'] == 'ignore_dir' ): ?>
					<span class="file_status glyphicon glyphicon-minus-sign"></span>
						<? ELSE: ?>
					<span class="file_status glyphicon glyphicon-asterisk"></span>
						<? ENDIF; ?>
					<? ENDIF; ?>
				
					</td>
				</tr>
				
				<? ENDFOREACH; ?>
			
			</table>
			<? ELSE: ?>
			<p>
			No changes.
			</p>
			<? ENDIF; ?>
			</div>
		<? ENDFOREACH; ?>
	<? ELSE: ?>
			<p>
			Never audited.
			</p>
	<? ENDIF; ?>
			
		</td>
	</tr>

<? ENDFOREACH; ?>
	<tbody>
	</table>

</div>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<script src="contrib/contextmenu/dist/jquery.contextMenu.min.js"></script>
<script type="text/javascript">

	$(function () {
		$('[data-toggle="tooltip"]').tooltip();
		$('[data-toggle="popover"]').popover();
		
		$('.nogo').click(function(e){
			e.preventDefault();
		});
		
		$('.collapse').collapse('hide')
		
		$('.audit-files mark').click(function(){
		
		});
		
		$.contextMenu({
        	selector: '.file', 
            callback: file_action,
            items: {
				acknowledge : {name: "Acknowledge File", icon : get_file_action_icon},
				ignore_file : {name: "Ignore File", icon : get_file_action_icon},
				ignore_dir  : {name: "Ignore Directory", icon : get_file_action_icon},
            }
        });
        
        $('#unacked_scripts').click(function(e){
        	e.preventDefault();
        	$('.audit-files .warning').each(function(){
        		if ( $(this).data('status') == '' ) {
        			console.log( $(this).text() + ' is  Unacknowledged');
        		}
        	});
        });
        
	});
	
	function get_file_action_icon (opt, $itemElement, itemKey, item) {
		var aout = '';
		if (opt.context) {
			var $file = $(opt.context);
			if ($file.data('status') == itemKey) {
				aout = 'context-menu-icon context-menu-icon-quit';
			}
		}
		return aout;
	}
	
	function file_action (key, options) {
		var site = $(this).data('site');
		var file = $(this).text();
		if ( key == 'ignore_dir' ) {
			file = dirname(file);
		}
		
		var data = {
			site   : site, 
			file   : file, 
			action : key
		};
		
		$.ajax({
			url     : "file.php", 
			method  : 'post', 
			data    : data, 
			success : show_file_result, 
			error   : show_file_error, 
		});
	}
	
	function show_file_result (response) {
		console.log('ok: ' );
		console.log(response);
	}

	function show_file_error (jqXHR, textStatus, errorThrown) {
		console.log('Failwhale :(');
	}

	function dirname(path) {
		return path.match( /.*\// )[0];
	}

</script>
</body>
</html>
