<?php
/**
 +===========================================================================+
 |    ____         __            ____ _               _               _      |
 |   / ___|  __ _ / _| ___ _ __ / ___| |__   ___  ___| | _____  _   _| |_    |
 |   \___ \ / _` | |_ / _ \ '__| |   | '_ \ / _ \/ __| |/ / _ \| | | | __|   |
 |    ___) | (_| |  _|  __/ |  | |___| | | |  __/ (__|   < (_) | |_| | |_    |
 |   |____/ \__,_|_|  \___|_|   \____|_| |_|\___|\___|_|\_\___/ \__,_|\__|   |
 |                                                                           |
 | (c) NinTechNet Limited ~ https://nintechnet.com/safercheckout/            |
 +===========================================================================+ 2024-06-11
*/

if (! defined('ABSPATH') ) {
	die('Forbidden');
}

// ===========================================================================
// Display the "IP Address" page.

wp_nonce_field('safercheckout-nonce', 'safercheckout-nonce', 0);
?>
<input type="hidden" id="safercheckout-settings" name="safercheckout-settings" value="settings-ip" />

<h2><?php esc_html_e('IP Address', 'safercheckout-lite') ?></h2>
<table class="form-table sco-border-grey">
<tbody>
	<?php
	// User IP address
	if ( empty( $this->sc_options['source_ip'] ) ) {
		$this->sc_options['source_ip'] = $this->sc_default['source_ip'];
	}
	if (! isset( $this->sc_options['ip_blacklist'] ) ) {
		$this->sc_options['ip_blacklist'] = $this->sc_default['ip_blacklist'];
	}
	if (! isset( $this->sc_options['ip_whitelist'] ) ) {
		$this->sc_options['ip_whitelist'] = $this->sc_default['ip_whitelist'];
	}
	?>
	<tr valign="top">
		<th scope="row">
			<label>
				<?php esc_html_e("Retrieve IP address from", 'safercheckout-lite') ?> <span class="woocommerce-help-tip" tabindex="0" aria-label="<?php esc_attr_e('This option should be used if you are behind a reverse proxy, a load balancer or using a CDN (e.g., Clouflare), in order to tell SaferCheckout which IP address it should use. By default, it will rely on REMOTE_ADDR.', 'safercheckout-lite') ?>"></span>
			</label>
		</th>
		<td>
		<?php
		$ip_addresses = SaferCheckoutLite_helpers::get_ip_list();
		foreach( $ip_addresses as $var => $ip ) {
			// Warn if it's a private address
			if ( true === SaferCheckoutLite_helpers::is_private( $ip ) ) {
				echo '<p><label><input type="radio" name="safercheckout_source_ip" value ="'. esc_attr( $var ) .'"'. checked( $this->sc_options['source_ip'], $var, 0 ) .' /> <code>'. esc_html( $var) .'</code>: '. esc_html( $ip ) . ' <span style="color: rgb(214, 54, 56)">⚠️ ️️' . esc_html__('This is a private IP address. Make sure to select a public address that shows your real IP.', 'safercheckout-lite') .'</span></label></p>';

			} else {
				echo '<p><label><input type="radio" name="safercheckout_source_ip" value ="'. esc_attr( $var ) .'"'. checked( $this->sc_options['source_ip'], $var, 0 ) .' /> <code>'. esc_html( $var) .'</code>: '. esc_html( $ip ) .'</label></p>';
			}
		}
		// Warn if the server is behind Cloudflare
		if ( true === SaferCheckoutLite_helpers::is_cloudflare( $this->sc_options['source_ip'] ) ) {
			echo '<p><label><span style="color: rgb(214, 54, 56)">⚠️ ️️' .
			sprintf(
				/* Translators: IP address */
				esc_html__('IP address %s belongs to Cloudflare. Make sure to select an address that shows your real IP, not Cloudflare server\'s.', 'safercheckout-lite'),
				// phpcs:ignore
				esc_html( $_SERVER[ $this->sc_options['source_ip'] ] )
			) . '</span></label></p>';
		}
		?>
		</td>
		<td>&nbsp;</td>
	</tr>
	<tr valign="top">
		<th scope="row">
			<label>
				<?php esc_html_e('Allow the following IP addresses, CIDR or AS number', 'safercheckout-lite') ?> <span class="woocommerce-help-tip" tabindex="0" aria-label="<?php esc_attr_e('You can permanently allow an IP address, a whole range of IP addresses or even an AS number (Autonomous System number). If an IP address matched that list, the order would be immediately accepted.', 'safercheckout-lite') ?>"></span>
			</label>
		</th>
		<td>
			<p><?php esc_html_e('Whitelist:', 'safercheckout-lite') ?></p>
			<textarea style="min-width: 50%; height: 100px;" name="safercheckout_ip_whitelist"><?php
				foreach ( $this->sc_options['ip_whitelist'] as $ip => $null ) {
					echo esc_textarea( "$ip\n" );
				}
			?></textarea>
			<p class="description">
				<?php esc_html_e('One item per line.', 'safercheckout-lite') ?> <span><a href="javascript:" style="text-decoration:none;" onClick="safercheckout_toggle('view-ip-allow');"> <?php esc_html_e('View allowed syntax', 'safercheckout-lite') ?></a></span>
			</p>
			<div id="view-ip-allow" style="display:none">
				<ul class="view">
					<li><?php printf(
						/* Translators: IP address */
						esc_html__('IPv4 address: %s', 'safercheckout-lite') , '<code>66.155.10.20</code>' )
					?></li>
					<li><?php
						/* Translators: IP address */
						printf( esc_html__('IPv4 CIDR: %s', 'safercheckout-lite') , '<code>66.155.0.0/17</code>' )
					?></li>
					<li><?php printf(
						/* Translators: IP address */
						esc_html__('IPv6 address: %s', 'safercheckout-lite') , '<code>2001:db8:85a3::8a2e</code>' )
					?></li>
					<li><?php printf(
						/* Translators: IP address */
						esc_html__('IPv6 CIDR: %s', 'safercheckout-lite') , '<code>2c0f:f248::/32</code>' )
					?></li>
					<li><?php printf(
						/* Translators: IP address */
						esc_html__('Autonomous System number: %s', 'safercheckout-lite') , '<code>AS15169</code>' )
					?></li>
				</ul>
			</div>
		</td>
		<td>&nbsp;</td>
	</tr>
	<tr valign="top">
		<th scope="row">
			<label>
				<?php esc_html_e('Block the following IP addresses, CIDR or AS number', 'safercheckout-lite') ?> <span class="woocommerce-help-tip" tabindex="0" aria-label="<?php esc_attr_e('You can permanently block an IP address, a whole range of IP addresses or even an AS number (Autonomous System number). If an IP address matched that list, the order would be immediately rejected.', 'safercheckout-lite') ?>"></span>
			</label>
		</th>

		<td>
			<p><?php esc_html_e('Blacklist:', 'safercheckout-lite') ?></p>
			<textarea style="min-width: 50%; height: 100px;" name="safercheckout_ip_blacklist"><?php
				foreach ( $this->sc_options['ip_blacklist'] as $ip => $null ) {
					echo esc_textarea( "$ip\n" );
				}
			?></textarea>
			<p class="description">
				<?php esc_html_e('One item per line.', 'safercheckout-lite') ?> <span><a href="javascript:" style="text-decoration:none;" onClick="safercheckout_toggle('view-ip-block');"> <?php esc_html_e('View allowed syntax', 'safercheckout-lite') ?></a></span>
			</p>
			<div id="view-ip-block" style="display:none">
				<ul class="view">
					<li><?php printf(
						/* Translators: IP address */
						esc_html__('IPv4 address: %s', 'safercheckout-lite') , '<code>66.155.10.20</code>' )
					?></li>
					<li><?php printf(
						/* Translators: IP address */
						esc_html__('IPv4 CIDR: %s', 'safercheckout-lite') , '<code>66.155.0.0/17</code>' )
					?></li>
					<li><?php printf(
						/* Translators: IP address */
						esc_html__('IPv6 address: %s', 'safercheckout-lite') , '<code>2001:db8:85a3::8a2e</code>' )
					?></li>
					<li><?php printf(
						/* Translators: IP address */
						esc_html__('IPv6 CIDR: %s', 'safercheckout-lite') , '<code>2c0f:f248::/32</code>' )
					?></li>
					<li><?php printf(
						/* Translators: IP address */
						esc_html__('Autonomous System number: %s', 'safercheckout-lite') , '<code>AS15169</code>' )
					?></li>
				</ul>
			</div>
		</td>
		<td>&nbsp;</td>
	</tr>

	<tr valign="top">
		<th scope="row">
			<label>
				<?php esc_html_e('Reverse DNS lookup', 'safercheckout-lite') ?> <sup class="sco-pro-only">Pro</sup> <span class="woocommerce-help-tip" tabindex="0" aria-label="<?php esc_attr_e('This option will run a reverse DNS lookup on the customer IP address, then will run a forward DNS lookup on the domain name returned to make sure that it matches the IP address. You can also define the risk score to apply. Note that if the IP doesn\'t have any rDNS at all, the check will be skipped.', 'safercheckout-lite') ?>"></span>
			</label>
		</th>
		<td>
			<label>
				<input type="checkbox" disabled /> <?php esc_html_e('IP address must have a valid reverse DNS.', 'safercheckout-lite' ) ?>
			</label>
		</td>
		<td>
			<label>
				<?php esc_html_e('Risk score:', 'safercheckout-lite') ?><br /><input type="number" value="20" style="width:100px;" disabled />
			</label>
		</td>
	</tr>
	<tr valign="top">
		<th scope="row">
			<label>
				<?php esc_html_e('Block the following rDNS', 'safercheckout-lite') ?> <sup class="sco-pro-only">Pro</sup> <span class="woocommerce-help-tip" tabindex="0" aria-label="<?php esc_attr_e('You can permanently block the reverse DNS (domain name) of an IP address, or any part of it. If an rDNS matched that list, the order would be immediately rejected.', 'safercheckout-lite') ?>"></span>
			</label>
		</th>
		<td>
			<p><?php esc_html_e('Blacklist:', 'safercheckout-lite') ?></p>
			<textarea style="min-width: 50%; height: 100px;" disabled></textarea>
			<p class="description"><?php esc_html_e('Full or partial case-insensitive string, one per line.', 'safercheckout-lite') ?>
			<span><a href="javascript:" style="text-decoration:none;" onClick="safercheckout_toggle('view-rdns-block');"> <?php esc_html_e('View examples', 'safercheckout-lite') ?></a></span></p>
			<div id="view-rdns-block" style="display:none">
				<ul class="view">
					<li><code>218-173-1-18.dynamic-ip.hinet.net</code></li>
					<li><code>dynamic-ip.hinet.net</code></li>
					<li><code>tor-exit.</code></li>
					<li><code>tor.node</code></li>
				</ul>
			</div>
		</td>
		<td>&nbsp;</td>
	</tr>
	<tr valign="top">
		<th scope="row">
			<label>
				<?php esc_html_e('DNSBL', 'safercheckout-lite') ?> <sup class="sco-pro-only">Pro</sup> <span class="woocommerce-help-tip" tabindex="0" aria-label="<?php esc_attr_e('A DNSBL (Domain Name System Blacklist) is a service that contains IP addresses identified as sending spam, hosting malicious content, hijacking IP space, or acting like a bulletproof hosting company. This option lets you configure which DNSBL you want to use to check your customer\'s IP address as well as the risk score. To get more info about each service, click on the corresponding link.', 'safercheckout-lite') ?>"></span>
			</label>
		</th>
		<td>
			<p><?php esc_html_e('Check if the IP address is blacklisted by the following services:', 'safercheckout-lite') ?></p>
			<p><label>
				<input type="checkbox" checked disabled /><a href="https://en.wikipedia.org/wiki/The_Spamhaus_Project" target="_blank" rel="noreferrer noopener" title="<?php esc_attr_e('Click for more details about this service.', 'safercheckout-lite') ?>">Spamhaus</a>&nbsp;&nbsp;<input type="button" class="button button-small" value="<?php esc_attr_e('Test', 'safercheckout-lite') ?>" title="<?php esc_attr_e('Click to test it.', 'safercheckout-lite') ?>" disabled>
			</label>
			</p>
			<p><label>
				<input type="checkbox" disabled /><a href="https://en.wikipedia.org/wiki/SpamCop" target="_blank" rel="noreferrer noopener" title="<?php esc_attr_e('Click for more details about this service.', 'safercheckout-lite') ?>">SpamCop</a>&nbsp;&nbsp;<input type="button" class="button button-small" value="<?php esc_attr_e('Test', 'safercheckout-lite') ?>" title="<?php esc_attr_e('Click to test it.', 'safercheckout-lite') ?>" disabled>
			</label></p>
		</td>
		<td>
			<label>
				<?php esc_html_e('Risk score:', 'safercheckout-lite') ?><br /><input type="number" value="50" style="width:100px;"disabled />
			</label>
		</td>
	</tr>


</tbody>
</table>
<?php

// ===========================================================================
// EOF
