<?php
/**
 * Exposure Audit screen.
 *
 * @package Upload_Vault
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

$uvgf_audit    = get_option( 'uvgf_audit', array() );
$uvgf_state    = get_option( 'uvgf_state', array() );
$uvgf_labels   = Uvgf_Auditor::verdict_labels();
$uvgf_verdict  = isset( $uvgf_audit['verdict'] ) ? (string) $uvgf_audit['verdict'] : '';
$uvgf_titles   = Uvgf_Locator::form_titles();
$uvgf_server   = Uvgf_Locator::server_type();
$uvgf_htaccess = Uvgf_Locator::htaccess_applies();
$uvgf_folders  = isset( $uvgf_audit['folders'] ) && is_array( $uvgf_audit['folders'] ) ? $uvgf_audit['folders'] : array();
$uvgf_rules_on = ! empty( $uvgf_audit['installed'] );
?>
<div class="wrap uvgf-wrap">
	<h1><?php esc_html_e( 'Upload Vault — Exposure Audit', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?></h1>

	<?php Uvgf_Admin::render_notice(); ?>

	<?php if ( ! Uvgf_Locator::gf_active() ) : ?>
		<div class="notice notice-info inline">
			<p><?php esc_html_e( 'Gravity Forms is not active. Upload Vault still audits and protects the gravity_forms upload directory (files outlive the form plugin); form titles and entry-link rewriting switch on when Gravity Forms is active.', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?></p>
		</div>
	<?php endif; ?>

	<?php if ( '' === $uvgf_verdict ) : ?>
		<p><?php esc_html_e( 'No audit has run yet. Run the first one to see, as a served fact, what a stranger with a leaked URL would get.', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?></p>
	<?php else : ?>
		<div class="uvgf-verdict uvgf-verdict--<?php echo esc_attr( $uvgf_verdict ); ?>">
			<strong><?php echo isset( $uvgf_labels[ $uvgf_verdict ] ) ? esc_html( $uvgf_labels[ $uvgf_verdict ] ) : esc_html( $uvgf_verdict ); ?></strong>
			<?php if ( ! empty( $uvgf_audit['checked_at'] ) ) : ?>
				<span class="uvgf-muted">
					<?php
					/* translators: %s: human time diff. */
					printf( esc_html__( 'Last audit: %s ago', 'buildsbyluke-upload-vault-for-gravity-forms' ), esc_html( human_time_diff( (int) $uvgf_audit['checked_at'], time() ) ) );
					?>
				</span>
			<?php endif; ?>
		</div>

		<?php if ( 'impaired' === $uvgf_verdict && ! empty( $uvgf_state['loopback_error'] ) ) : ?>
			<div class="notice notice-warning inline">
				<p>
					<?php esc_html_e( 'This site cannot request itself over HTTP (loopback), so Upload Vault cannot verify anything and will not guess. Ask your host about loopback requests.', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?>
					<code><?php echo esc_html( (string) $uvgf_state['loopback_error'] ); ?></code>
				</p>
			</div>
		<?php endif; ?>

		<?php if ( 'installed_unverified' === $uvgf_verdict && ! $uvgf_htaccess ) : ?>
			<div class="notice notice-warning inline">
				<p>
					<?php
					/* translators: %s: server type. */
					printf( esc_html__( 'Your server (%s) ignores .htaccess files, so the deny rules Upload Vault wrote cannot take effect on their own. Add the matching server snippet below, then re-run the audit — the verdict flips to Protected only when the server actually refuses direct requests.', 'buildsbyluke-upload-vault-for-gravity-forms' ), esc_html( $uvgf_server ) );
					?>
				</p>
			</div>
			<h2><?php esc_html_e( 'Server snippet', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?></h2>
			<?php if ( 'iis' === $uvgf_server ) : ?>
				<p><?php esc_html_e( 'Save as web.config inside the gravity_forms upload folder:', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?></p>
				<pre class="uvgf-snippet"><?php echo esc_html( Uvgf_Locator::iis_snippet() ); ?></pre>
			<?php else : ?>
				<p><?php esc_html_e( 'Add inside your site\'s server block, then reload nginx:', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?></p>
				<pre class="uvgf-snippet"><?php echo esc_html( Uvgf_Locator::nginx_snippet() ); ?></pre>
			<?php endif; ?>
		<?php endif; ?>

		<h2><?php esc_html_e( 'Form upload folders', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?></h2>
		<?php if ( empty( $uvgf_folders ) ) : ?>
			<p><?php esc_html_e( 'No Gravity Forms upload folders found.', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?></p>
		<?php else : ?>
			<table class="widefat striped uvgf-table">
				<thead>
					<tr>
						<th><?php esc_html_e( 'Form', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?></th>
						<th><?php esc_html_e( 'Folder', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?></th>
						<th><?php esc_html_e( 'Files', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?></th>
						<th><?php esc_html_e( 'Size', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?></th>
						<th><?php esc_html_e( 'Direct access', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?></th>
						<th><?php esc_html_e( 'Listing', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php foreach ( $uvgf_folders as $uvgf_row ) : ?>
						<tr>
							<td>
								<?php
								$uvgf_fid = (int) $uvgf_row['form_id'];
								if ( isset( $uvgf_titles[ $uvgf_fid ] ) && '' !== $uvgf_titles[ $uvgf_fid ] ) {
									echo esc_html( $uvgf_titles[ $uvgf_fid ] );
								} elseif ( $uvgf_fid > 0 ) {
									/* translators: %d: form id. */
									printf( esc_html__( 'Form #%d', 'buildsbyluke-upload-vault-for-gravity-forms' ), (int) $uvgf_fid );
								} else {
									esc_html_e( '(unrecognised folder)', 'buildsbyluke-upload-vault-for-gravity-forms' );
								}
								?>
							</td>
							<td><code><?php echo esc_html( $uvgf_row['folder'] ); ?></code></td>
							<td><?php echo esc_html( number_format_i18n( (int) $uvgf_row['files'] ) ); ?></td>
							<td><?php echo esc_html( size_format( (int) $uvgf_row['bytes'] ) ); ?></td>
							<td>
								<?php if ( ! empty( $uvgf_row['excluded'] ) ) : ?>
									<span class="uvgf-pill uvgf-pill--open"><?php esc_html_e( 'Left open (your setting)', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?></span>
								<?php elseif ( 'exposed' === $uvgf_row['status'] ) : ?>
									<span class="uvgf-pill uvgf-pill--exposed"><?php esc_html_e( 'EXPOSED', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?></span>
								<?php elseif ( 'protected' === $uvgf_row['status'] ) : ?>
									<span class="uvgf-pill uvgf-pill--ok"><?php esc_html_e( 'Refused', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?></span>
								<?php else : ?>
									<span class="uvgf-pill"><?php echo esc_html( $uvgf_row['status'] ); ?></span>
								<?php endif; ?>
								<?php if ( ! empty( $uvgf_row['http'] ) ) : ?>
									<span class="uvgf-muted">HTTP <?php echo esc_html( (string) (int) $uvgf_row['http'] ); ?></span>
								<?php endif; ?>
							</td>
							<td>
								<?php if ( ! empty( $uvgf_row['listing'] ) ) : ?>
									<span class="uvgf-pill uvgf-pill--exposed"><?php esc_html_e( 'Browsable!', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?></span>
								<?php else : ?>
									<span class="uvgf-muted">—</span>
								<?php endif; ?>
							</td>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
		<?php endif; ?>
	<?php endif; ?>

	<div class="uvgf-actions">
		<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="uvgf-inline-form">
			<input type="hidden" name="action" value="uvgf_run_audit" />
			<?php wp_nonce_field( 'uvgf_run_audit' ); ?>
			<button type="submit" class="button button-primary"><?php esc_html_e( 'Audit now', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?></button>
		</form>
		<?php if ( $uvgf_rules_on || Uvgf_Protector::rules_installed() ) : ?>
			<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="uvgf-inline-form">
				<input type="hidden" name="action" value="uvgf_unprotect" />
				<?php wp_nonce_field( 'uvgf_unprotect' ); ?>
				<button type="submit" class="button"><?php esc_html_e( 'Remove protection', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?></button>
			</form>
		<?php else : ?>
			<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="uvgf-inline-form">
				<input type="hidden" name="action" value="uvgf_protect" />
				<?php wp_nonce_field( 'uvgf_protect' ); ?>
				<button type="submit" class="button button-primary"><?php esc_html_e( 'Protect uploads', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?></button>
			</form>
		<?php endif; ?>
	</div>

	<p class="uvgf-muted">
		<?php esc_html_e( 'Protection = deny rules for direct requests + downloads through a capability-checked proxy. Entry links and notification emails are rewritten to proxy links automatically. No plugin can guarantee security; Upload Vault shows you what your server actually serves and helps you restrict it.', 'buildsbyluke-upload-vault-for-gravity-forms' ); ?>
	</p>

	<?php Uvgf_Admin::render_upsell(); ?>
</div>
