| 1 |
<?php |
| 2 |
/** |
| 3 |
* AtomicEdge Development Mode |
| 4 |
* |
| 5 |
* Provides simulated API responses for local development environments. |
| 6 |
* This allows testing all plugin features without a real AtomicEdge connection. |
| 7 |
* |
| 8 |
* @package AtomicEdge |
| 9 |
* @since 2.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
// Prevent direct access. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Class AtomicEdge_Dev_Mode |
| 19 |
* |
| 20 |
* Development mode utilities for local testing. |
| 21 |
*/ |
| 22 |
class AtomicEdge_Dev_Mode { |
| 23 |
|
| 24 |
/** |
| 25 |
* Local domain patterns that trigger dev mode. |
| 26 |
* |
| 27 |
* @var array |
| 28 |
*/ |
| 29 |
private static $local_patterns = array( |
| 30 |
'.local', |
| 31 |
'.test', |
| 32 |
'.localhost', |
| 33 |
'.dev', |
| 34 |
'.ddev.site', |
| 35 |
'localhost', |
| 36 |
'127.0.0.1', |
| 37 |
'::1', |
| 38 |
); |
| 39 |
|
| 40 |
/** |
| 41 |
* Check if the current site is a local/development environment. |
| 42 |
* |
| 43 |
* @return bool |
| 44 |
*/ |
| 45 |
public static function is_local_environment() { |
| 46 |
$host = wp_parse_url( get_site_url(), PHP_URL_HOST ); |
| 47 |
|
| 48 |
if ( empty( $host ) ) { |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
// Check against known local patterns. |
| 53 |
foreach ( self::$local_patterns as $pattern ) { |
| 54 |
if ( $host === $pattern || substr( $host, -strlen( $pattern ) ) === $pattern ) { |
| 55 |
return true; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
// Check for private IP ranges. |
| 60 |
if ( filter_var( $host, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE ) === false && |
| 61 |
filter_var( $host, FILTER_VALIDATE_IP ) !== false ) { |
| 62 |
return true; |
| 63 |
} |
| 64 |
|
| 65 |
return false; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Check if development mode is enabled. |
| 70 |
* |
| 71 |
* Dev mode is enabled when: |
| 72 |
* 1. Site is in a local environment, AND |
| 73 |
* 2. The dev mode option is enabled (or defaults to enabled for local) |
| 74 |
* |
| 75 |
* @return bool |
| 76 |
*/ |
| 77 |
public static function is_enabled() { |
| 78 |
if ( ! self::is_local_environment() ) { |
| 79 |
return false; |
| 80 |
} |
| 81 |
|
| 82 |
// Default to enabled for local environments, can be disabled via option. |
| 83 |
return get_option( 'atomicedge_dev_mode', true ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Get simulated site data for development. |
| 88 |
* |
| 89 |
* @return array |
| 90 |
*/ |
| 91 |
public static function get_simulated_site_data() { |
| 92 |
$host = wp_parse_url( get_site_url(), PHP_URL_HOST ); |
| 93 |
$prefix = str_replace( array( '.', '-' ), '', $host ); |
| 94 |
|
| 95 |
return array( |
| 96 |
'connected' => true, |
| 97 |
'site_id' => 999, |
| 98 |
'domain' => $host, |
| 99 |
'plan_tier' => 'enterprise', // Give full features in dev mode. |
| 100 |
'waf_enabled' => true, |
| 101 |
'cdn_enabled' => true, |
| 102 |
'cdn_prefix' => $prefix, |
| 103 |
'cdn_url' => $prefix . '.cdn.shift8web.ca', |
| 104 |
'features' => array( |
| 105 |
'waf_protection' => true, |
| 106 |
'analytics' => true, |
| 107 |
'ip_access_control' => true, |
| 108 |
'geo_blocking' => true, |
| 109 |
'cdn' => true, |
| 110 |
'minification' => true, |
| 111 |
), |
| 112 |
'dev_mode' => true, |
| 113 |
); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Get the effective site data (real or simulated). |
| 118 |
* |
| 119 |
* @return array |
| 120 |
*/ |
| 121 |
public static function get_effective_site_data() { |
| 122 |
$stored_data = get_option( 'atomicedge_site_data', array() ); |
| 123 |
|
| 124 |
// If connected with real data, use that. |
| 125 |
if ( ! empty( $stored_data['connected'] ) && empty( $stored_data['dev_mode'] ) ) { |
| 126 |
return $stored_data; |
| 127 |
} |
| 128 |
|
| 129 |
// If dev mode is enabled, return simulated data. |
| 130 |
if ( self::is_enabled() ) { |
| 131 |
return self::get_simulated_site_data(); |
| 132 |
} |
| 133 |
|
| 134 |
return $stored_data; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Check if effectively connected (real or dev mode). |
| 139 |
* |
| 140 |
* @return bool |
| 141 |
*/ |
| 142 |
public static function is_effectively_connected() { |
| 143 |
if ( get_option( 'atomicedge_connected', false ) ) { |
| 144 |
return true; |
| 145 |
} |
| 146 |
|
| 147 |
return self::is_enabled(); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Check if CDN is effectively enabled (real or dev mode). |
| 152 |
* |
| 153 |
* @return bool |
| 154 |
*/ |
| 155 |
public static function is_cdn_effectively_enabled() { |
| 156 |
$site_data = self::get_effective_site_data(); |
| 157 |
return ! empty( $site_data['cdn_enabled'] ); |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Get a dev mode notice for admin display. |
| 162 |
* |
| 163 |
* @return string HTML notice or empty string. |
| 164 |
*/ |
| 165 |
public static function get_admin_notice() { |
| 166 |
if ( ! self::is_enabled() ) { |
| 167 |
return ''; |
| 168 |
} |
| 169 |
|
| 170 |
return sprintf( |
| 171 |
'<div class="atomicedge-notice atomicedge-notice-info" style="margin-bottom: 15px;"> |
| 172 |
<span class="dashicons dashicons-info"></span> |
| 173 |
<div> |
| 174 |
<p><strong>%s</strong></p> |
| 175 |
<p>%s</p> |
| 176 |
</div> |
| 177 |
</div>', |
| 178 |
esc_html__( 'Development Mode Active', 'atomic-edge-security' ), |
| 179 |
esc_html__( 'You are viewing simulated data for local development. All features are available for testing. Connect to AtomicEdge on a production site for real functionality.', 'atomic-edge-security' ) |
| 180 |
); |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Simulate an API response for development. |
| 185 |
* |
| 186 |
* @param string $endpoint The API endpoint. |
| 187 |
* @param array $data Request data. |
| 188 |
* @return array Simulated response. |
| 189 |
*/ |
| 190 |
public static function simulate_api_response( $endpoint, $data = array() ) { |
| 191 |
switch ( $endpoint ) { |
| 192 |
case '/connect': |
| 193 |
return array( |
| 194 |
'success' => true, |
| 195 |
'data' => self::get_simulated_site_data(), |
| 196 |
); |
| 197 |
|
| 198 |
case '/cdn/status': |
| 199 |
return array( |
| 200 |
'success' => true, |
| 201 |
'data' => array( |
| 202 |
'cdn_enabled' => true, |
| 203 |
'cdn_prefix' => self::get_simulated_site_data()['cdn_prefix'], |
| 204 |
'cdn_url' => self::get_simulated_site_data()['cdn_url'], |
| 205 |
'optimization' => array( |
| 206 |
'brotli' => true, |
| 207 |
'image_optimization' => false, |
| 208 |
), |
| 209 |
), |
| 210 |
); |
| 211 |
|
| 212 |
case '/cdn/purge': |
| 213 |
return array( |
| 214 |
'success' => true, |
| 215 |
'data' => array( |
| 216 |
'message' => __( '[Dev Mode] Cache purge simulated.', 'atomic-edge-security' ), |
| 217 |
'purged_at' => gmdate( 'c' ), |
| 218 |
), |
| 219 |
); |
| 220 |
|
| 221 |
case '/analytics': |
| 222 |
return array( |
| 223 |
'success' => true, |
| 224 |
'data' => self::get_simulated_analytics(), |
| 225 |
); |
| 226 |
|
| 227 |
case '/waf/logs': |
| 228 |
return array( |
| 229 |
'success' => true, |
| 230 |
'data' => self::get_simulated_waf_logs(), |
| 231 |
); |
| 232 |
|
| 233 |
default: |
| 234 |
return array( |
| 235 |
'success' => true, |
| 236 |
'data' => array( |
| 237 |
'dev_mode' => true, |
| 238 |
'message' => __( 'Simulated response for development.', 'atomic-edge-security' ), |
| 239 |
), |
| 240 |
); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Get simulated analytics data. |
| 246 |
* |
| 247 |
* @return array |
| 248 |
*/ |
| 249 |
private static function get_simulated_analytics() { |
| 250 |
$data = array( |
| 251 |
'requests' => array(), |
| 252 |
'bandwidth' => array(), |
| 253 |
'blocked' => array(), |
| 254 |
'cache_hit_ratio' => 0.85, |
| 255 |
'total_requests' => 0, |
| 256 |
'total_blocked' => 0, |
| 257 |
); |
| 258 |
|
| 259 |
// Generate 24 hours of fake data. |
| 260 |
$now = time(); |
| 261 |
for ( $i = 23; $i >= 0; $i-- ) { |
| 262 |
$timestamp = gmdate( 'Y-m-d H:00:00', $now - ( $i * 3600 ) ); |
| 263 |
$requests = wp_rand( 100, 500 ); |
| 264 |
$blocked = wp_rand( 5, 30 ); |
| 265 |
$data['requests'][] = array( |
| 266 |
'timestamp' => $timestamp, |
| 267 |
'count' => $requests, |
| 268 |
); |
| 269 |
$data['bandwidth'][] = array( |
| 270 |
'timestamp' => $timestamp, |
| 271 |
'bytes' => $requests * wp_rand( 5000, 20000 ), |
| 272 |
); |
| 273 |
$data['blocked'][] = array( |
| 274 |
'timestamp' => $timestamp, |
| 275 |
'count' => $blocked, |
| 276 |
); |
| 277 |
$data['total_requests'] += $requests; |
| 278 |
$data['total_blocked'] += $blocked; |
| 279 |
} |
| 280 |
|
| 281 |
return $data; |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* Get simulated WAF logs. |
| 286 |
* |
| 287 |
* @return array |
| 288 |
*/ |
| 289 |
private static function get_simulated_waf_logs() { |
| 290 |
$rules = array( |
| 291 |
'SQL Injection Attempt', |
| 292 |
'XSS Attack Detected', |
| 293 |
'Path Traversal Attempt', |
| 294 |
'Remote File Inclusion', |
| 295 |
'Rate Limit Exceeded', |
| 296 |
); |
| 297 |
|
| 298 |
$ips = array( |
| 299 |
'192.168.1.' . wp_rand( 1, 255 ), |
| 300 |
'10.0.0.' . wp_rand( 1, 255 ), |
| 301 |
'172.16.0.' . wp_rand( 1, 255 ), |
| 302 |
); |
| 303 |
|
| 304 |
$logs = array(); |
| 305 |
for ( $i = 0; $i < 10; $i++ ) { |
| 306 |
$logs[] = array( |
| 307 |
'id' => 'dev-' . wp_rand( 10000, 99999 ), |
| 308 |
'timestamp' => gmdate( 'c', time() - wp_rand( 0, 86400 ) ), |
| 309 |
'client_ip' => $ips[ array_rand( $ips ) ], |
| 310 |
'rule' => $rules[ array_rand( $rules ) ], |
| 311 |
'uri' => '/wp-admin/admin-ajax.php', |
| 312 |
'action' => 'blocked', |
| 313 |
'user_agent' => 'Mozilla/5.0 (Dev Mode Simulator)', |
| 314 |
); |
| 315 |
} |
| 316 |
|
| 317 |
return array( |
| 318 |
'logs' => $logs, |
| 319 |
'total' => count( $logs ), |
| 320 |
); |
| 321 |
} |
| 322 |
} |
| 323 |
|