| @@ -56,49 +56,24 @@ | ||
| 56 | 56 | |
| 57 | 57 | /** |
| 58 | 58 | * Collect IP from request. |
| 59 | 59 | * |
| 60 | - * Prefers REMOTE_ADDR since it cannot be spoofed by the client. When it is | |
| 61 | - * a private/reserved address (reverse proxy, Docker bridge gateway like | |
| 62 | - * 192.168.65.1, local dev), the forwarded headers are scanned for the first | |
| 63 | - * public IP. If nothing public is found, the request is local: 127.0.0.1. | |
| 64 | - * | |
| 65 | 60 | * @return string |
| 66 | 61 | */ |
| 67 | 62 | public static function get_ip() { |
| 68 | - $remote_addr = ! empty($_SERVER['REMOTE_ADDR']) ? sanitize_text_field($_SERVER['REMOTE_ADDR']) : ''; | |
| 69 | - | |
| 70 | - if (self::is_public_ip($remote_addr)) { | |
| 71 | - return $remote_addr; | |
| 63 | + $ip = '127.0.0.1'; // Local IP | |
| 64 | + if (! empty($_SERVER['HTTP_CLIENT_IP'])) { | |
| 65 | + $ip = $_SERVER['HTTP_CLIENT_IP']; | |
| 66 | + } elseif (! empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { | |
| 67 | + $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; | |
| 68 | + } else { | |
| 69 | + $ip = ! empty($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : $ip; | |
| 72 | 70 | } |
| 73 | 71 | |
| 74 | - foreach (['HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP'] as $header) { | |
| 75 | - if (empty($_SERVER[$header])) { | |
| 76 | - continue; | |
| 77 | - } | |
| 78 | - $candidates = explode(',', sanitize_text_field($_SERVER[$header])); | |
| 79 | - foreach ($candidates as $candidate) { | |
| 80 | - $candidate = trim($candidate); | |
| 81 | - if (self::is_public_ip($candidate)) { | |
| 82 | - return $candidate; | |
| 83 | - } | |
| 84 | - } | |
| 85 | - } | |
| 86 | - | |
| 87 | - return '127.0.0.1'; | |
| 72 | + return sanitize_text_field($ip); | |
| 88 | 73 | } |
| 89 | 74 | |
| 90 | 75 | /** |
| 91 | - * Check whether a string is a valid public (non-private, non-reserved) IP. | |
| 92 | - * | |
| 93 | - * @param string $ip | |
| 94 | - * @return bool | |
| 95 | - */ | |
| 96 | - private static function is_public_ip($ip): bool { | |
| 97 | - return (bool) filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE); | |
| 98 | - } | |
| 99 | - | |
| 100 | - /** | |
| 101 | 76 | * Get views for front-end display |
| 102 | 77 | * |
| 103 | 78 | * @param string $name it will be file name only from the view's folder. |
| 104 | 79 | * @param array $data |
| @@ -121,17 +96,8 @@ | ||
| 121 | 96 | * @return string Complete API URL |
| 122 | 97 | */ |
| 123 | 98 | public static function get_api_url($endpoint): string { |
| 124 | 99 | $base_url = self::is_dev_api() ? 'https://app.templately.dev' : 'https://app.templately.com'; |
| 125 | - | |
| 126 | - /** | |
| 127 | - * Filter the base URL for development API | |
| 128 | - * | |
| 129 | - * @since 3.5.0 | |
| 130 | - * @param string $base_url The default base URL | |
| 131 | - */ | |
| 132 | - $base_url = apply_filters('templately_dev_api_base_url', $base_url); | |
| 133 | - | |
| 134 | 100 | return "{$base_url}/api/{$endpoint}"; |
| 135 | 101 | } |
| 136 | 102 | |
| 137 | 103 | /** |
| @@ -151,13 +117,8 @@ | ||
| 151 | 117 | 'Authorization' => 'Bearer ' . $api_key, |
| 152 | 118 | 'x-templately-ip' => self::get_ip(), |
| 153 | 119 | 'x-templately-url' => home_url('/'), |
| 154 | 120 | 'x-templately-version' => defined( 'TEMPLATELY_VERSION' ) ? constant( 'TEMPLATELY_VERSION' ) : '1.0.0', |
| 155 | - // Force JSON responses so the cloud returns JSON errors instead of an HTML | |
| 156 | - // error page (which json_decode() cannot parse). Binary/XML downloads | |
| 157 | - // (zip pack, attachment WXR) use their own wp_remote_* calls and bypass | |
| 158 | - // this helper, so they are unaffected. Callers can override via $extra_headers. | |
| 159 | - 'Accept' => 'application/json', | |
| 160 | 121 | ]; |
| 161 | 122 | |
| 162 | 123 | // Add Content-Type for POST requests |
| 163 | 124 | if (strtoupper($method) === 'POST') { |
| @@ -163,15 +124,8 @@ | ||
| 163 | 124 | if (strtoupper($method) === 'POST') { |
| 164 | 125 | $headers['Content-Type'] = 'application/json'; |
| 165 | 126 | } |
| 166 | 127 | |
| 167 | - // Resolve requested platform: $_REQUEST wins (frontend-supplied), then caller's extra_headers, then default. | |
| 168 | - if ( isset( $_REQUEST['requested_platform'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 169 | - $extra_headers['x-templately-requested-platform'] = sanitize_text_field( wp_unslash( $_REQUEST['requested_platform'] ) ); | |
| 170 | - } elseif ( ! isset( $extra_headers['x-templately-requested-platform'] ) ) { | |
| 171 | - $extra_headers['x-templately-requested-platform'] = 'templately'; | |
| 172 | - } | |
| 173 | - | |
| 174 | 128 | // Merge additional headers |
| 175 | 129 | $headers = array_merge($headers, $extra_headers); |
| 176 | 130 | |
| 177 | 131 | $args = [ |
| @@ -196,12 +150,8 @@ | ||
| 196 | 150 | |
| 197 | 151 | // Check for verification header in the response |
| 198 | 152 | self::check_verification_header($response); |
| 199 | 153 | |
| 200 | - | |
| 201 | - // Check for site disconnection in response body | |
| 202 | - self::check_site_disconnection($response); | |
| 203 | - | |
| 204 | 154 | return $response; |
| 205 | 155 | } |
| 206 | 156 | |
| 207 | 157 | /** |
| @@ -259,46 +209,8 @@ | ||
| 259 | 209 | return $sanitized_value; |
| 260 | 210 | } |
| 261 | 211 | |
| 262 | 212 | /** |
| 263 | - * Escape a string for safe embedding inside a GraphQL or JSON string literal. | |
| 264 | - * | |
| 265 | - * GraphQL string escaping rules are identical to JSON string escaping (per the | |
| 266 | - * GraphQL spec), so wp_json_encode() is the authoritative escaper. We strip the | |
| 267 | - * outer quotes it adds and return only the escaped inner content, ready to be | |
| 268 | - * wrapped in your own quote pair. | |
| 269 | - * | |
| 270 | - * Handles pre-encoded JSON: when the caller has already run json_encode() + | |
| 271 | - * wp_slash() on a value (e.g. categories, dependencies in Items.php), the | |
| 272 | - * quotes are already escaped as \" and the string is ready to embed. Calling | |
| 273 | - * wp_json_encode() again would double-escape those backslashes. We detect this | |
| 274 | - * case by checking whether wp_unslash() produces valid JSON, and if so, return | |
| 275 | - * the value directly without further encoding. | |
| 276 | - * | |
| 277 | - * @param string $value Raw string or wp_slash(json_encode()) output. | |
| 278 | - * @return string Escaped string, safe to place between double quotes in GraphQL/JSON. | |
| 279 | - */ | |
| 280 | - public static function esc_json_string( $value ) { | |
| 281 | - $value = (string) $value; | |
| 282 | - | |
| 283 | - // If wp_slash() was applied to a JSON string upstream, the quotes are | |
| 284 | - // already escaped (e.g. {\"key\":\"val\"}). Detect this by unslashing and | |
| 285 | - // checking for valid JSON — if it matches, the value is already suitable | |
| 286 | - // for embedding in a string literal; return it as-is to avoid doubling backslashes. | |
| 287 | - $unslashed = wp_unslash( $value ); | |
| 288 | - if ( $unslashed !== $value ) { | |
| 289 | - $decoded = json_decode( $unslashed, true ); | |
| 290 | - if ( json_last_error() === JSON_ERROR_NONE && null !== $decoded ) { | |
| 291 | - return $value; | |
| 292 | - } | |
| 293 | - } | |
| 294 | - | |
| 295 | - $encoded = wp_json_encode( $value ); | |
| 296 | - // wp_json_encode wraps the value in "...", strip those outer quotes. | |
| 297 | - return substr( $encoded, 1, -1 ); | |
| 298 | - } | |
| 299 | - | |
| 300 | - /** | |
| 301 | 213 | * Check for X-Templately-Verified header and update user verification status |
| 302 | 214 | * |
| 303 | 215 | * @param array|WP_Error $response The HTTP response array from wp_remote_get/wp_remote_post |
| 304 | 216 | * @return void |
| @@ -350,103 +262,8 @@ | ||
| 350 | 262 | return false; |
| 351 | 263 | } |
| 352 | 264 | |
| 353 | 265 | /** |
| 354 | - * Check for site disconnection status in API response body | |
| 355 | - * | |
| 356 | - * Detects SiteNotConnected errors and updates user disconnection status. | |
| 357 | - * Sends X-Templately-Disconnected header for frontend detection. | |
| 358 | - * | |
| 359 | - * | |
| 360 | - * @param array|WP_Error|mixed $response The response object or body array | |
| 361 | - * @return bool True if site is disconnected, false otherwise | |
| 362 | - */ | |
| 363 | - public static function check_site_disconnection($response) { | |
| 364 | - if (is_wp_error($response)) { | |
| 365 | - return false; | |
| 366 | - } | |
| 367 | - | |
| 368 | - $response_body = $response; | |
| 369 | - | |
| 370 | - // If it's a raw WP response array with body, decode it | |
| 371 | - if (is_array($response) && isset($response['body']) && is_string($response['body'])) { | |
| 372 | - $response_body = json_decode(wp_remote_retrieve_body($response), true); | |
| 373 | - } | |
| 374 | - | |
| 375 | - // Check if response body indicates site disconnection | |
| 376 | - if (!is_array($response_body)) { | |
| 377 | - return false; | |
| 378 | - } | |
| 379 | - | |
| 380 | - $status = $response_body['status'] ?? null; | |
| 381 | - $status_text = $response_body['statusText'] ?? null; | |
| 382 | - | |
| 383 | - // Check for SiteNotConnected error | |
| 384 | - if ($status === 'error' && $status_text === 'SiteNotConnected') { | |
| 385 | - try { | |
| 386 | - // Get current user data | |
| 387 | - $options = Options::get_instance(); | |
| 388 | - $user = $options->get('user'); | |
| 389 | - | |
| 390 | - // Only update if user data exists | |
| 391 | - if (!empty($user) && is_array($user)) { | |
| 392 | - // Set disconnection flag | |
| 393 | - $user['is_disconnected'] = true; | |
| 394 | - | |
| 395 | - // Save updated user data | |
| 396 | - $options->set('user', $user); | |
| 397 | - | |
| 398 | - if (defined('TEMPLATELY_DEBUG_LOG') && constant('TEMPLATELY_DEBUG_LOG')) { | |
| 399 | - self::log('Site disconnection detected: SiteNotConnected status'); | |
| 400 | - } | |
| 401 | - } | |
| 402 | - | |
| 403 | - // Send header for frontend detection | |
| 404 | - if (!headers_sent()) { | |
| 405 | - header('X-Templately-Disconnected: true'); | |
| 406 | - } | |
| 407 | - | |
| 408 | - return true; | |
| 409 | - } catch (\Exception $e) { | |
| 410 | - // Log error if debug logging is enabled | |
| 411 | - if (defined('TEMPLATELY_DEBUG_LOG') && constant('TEMPLATELY_DEBUG_LOG')) { | |
| 412 | - self::log('Error updating site disconnection status: ' . $e->getMessage()); | |
| 413 | - } | |
| 414 | - } | |
| 415 | - } | |
| 416 | - | |
| 417 | - return false; | |
| 418 | - } | |
| 419 | - | |
| 420 | - /** | |
| 421 | - * Clear site disconnection status | |
| 422 | - * | |
| 423 | - * Called after successful site migration to reset the disconnection flag. | |
| 424 | - * | |
| 425 | - * @return void | |
| 426 | - */ | |
| 427 | - public static function clear_site_disconnection() { | |
| 428 | - try { | |
| 429 | - $options = Options::get_instance(); | |
| 430 | - $user = $options->get('user'); | |
| 431 | - | |
| 432 | - if (!empty($user) && is_array($user)) { | |
| 433 | - $user['site_url'] = base64_encode( home_url('/') ); | |
| 434 | - $user['is_disconnected'] = false; | |
| 435 | - $options->set('user', $user); | |
| 436 | - | |
| 437 | - if (defined('TEMPLATELY_DEBUG_LOG') && constant('TEMPLATELY_DEBUG_LOG')) { | |
| 438 | - self::log('Site disconnection status cleared and URL updated.'); | |
| 439 | - } | |
| 440 | - } | |
| 441 | - } catch (\Exception $e) { | |
| 442 | - if (defined('TEMPLATELY_DEBUG_LOG') && constant('TEMPLATELY_DEBUG_LOG')) { | |
| 443 | - self::log('Error clearing site disconnection status: ' . $e->getMessage()); | |
| 444 | - } | |
| 445 | - } | |
| 446 | - } | |
| 447 | - | |
| 448 | - /** | |
| 449 | 266 | * API Error Formatter |
| 450 | 267 | * |
| 451 | 268 | * @param int $error_code |
| 452 | 269 | * @param mixed $error_message |
| @@ -747,74 +564,7 @@ | ||
| 747 | 564 | $r[$key] = $value; |
| 748 | 565 | } |
| 749 | 566 | } |
| 750 | 567 | return $r; |
| 751 | - } | |
| 752 | - | |
| 753 | - /** | |
| 754 | - * Creates the plugin's working directory under wp-uploads and blocks direct | |
| 755 | - * web access to it. | |
| 756 | - * | |
| 757 | - * Everything the importer needs on disk lands here: the extracted pack (its | |
| 758 | - * WXR, its template JSON, its attachments), the AI-generated page JSON, and | |
| 759 | - * the FSI logs. wp-uploads is web-served, so these paths are not private just | |
| 760 | - * because their session id is a uuid — the guards are what makes them | |
| 761 | - * unreadable, not the name. | |
| 762 | - * | |
| 763 | - * .htaccess covers Apache and is inherited by everything below this point; | |
| 764 | - * web.config covers IIS; index.php stops a directory listing on any server. | |
| 765 | - * nginx honours none of them, so an nginx site still needs a location rule — | |
| 766 | - * this raises the floor, it does not replace server configuration. | |
| 767 | - * | |
| 768 | - * @param string $dir Absolute path to create and protect. | |
| 769 | - * | |
| 770 | - * @return bool Whether the directory exists and is usable. | |
| 771 | - */ | |
| 772 | - public static function protect_directory( $dir ) { | |
| 773 | - if ( empty( $dir ) ) { | |
| 774 | - return false; | |
| 775 | - } | |
| 776 | - | |
| 777 | - if ( ! is_dir( $dir ) && ! wp_mkdir_p( $dir ) ) { | |
| 778 | - return false; | |
| 779 | - } | |
| 780 | - | |
| 781 | - $guards = [ | |
| 782 | - 'index.php' => "<?php\n// Silence is golden.\n", | |
| 783 | - '.htaccess' => "# Templately working files — not for direct access.\n<IfModule mod_authz_core.c>\n\tRequire all denied\n</IfModule>\n<IfModule !mod_authz_core.c>\n\tOrder allow,deny\n\tDeny from all\n</IfModule>\n", | |
| 784 | - 'web.config' => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<configuration>\n\t<system.webServer>\n\t\t<authorization>\n\t\t\t<deny users=\"*\" />\n\t\t</authorization>\n\t</system.webServer>\n</configuration>\n", | |
| 785 | - ]; | |
| 786 | - | |
| 787 | - foreach ( $guards as $file => $contents ) { | |
| 788 | - $path = trailingslashit( $dir ) . $file; | |
| 789 | - // Never overwrite: a site owner may have relaxed these deliberately. | |
| 790 | - if ( ! file_exists( $path ) ) { | |
| 791 | - @file_put_contents( $path, $contents ); // phpcs:ignore | |
| 792 | - } | |
| 793 | - } | |
| 794 | - | |
| 795 | - return true; | |
| 796 | - } | |
| 797 | - | |
| 798 | - /** | |
| 799 | - * Absolute path to the plugin's protected working directory in wp-uploads. | |
| 800 | - * | |
| 801 | - * @param string $sub Optional subdirectory ('tmp', 'log', 'preview', ...). | |
| 802 | - * | |
| 803 | - * @return string Trailing-slashed path, or '' when uploads is unusable. | |
| 804 | - */ | |
| 805 | - public static function upload_dir( $sub = '' ) { | |
| 806 | - $upload_dir = wp_upload_dir(); | |
| 807 | - | |
| 808 | - if ( ! empty( $upload_dir['error'] ) || empty( $upload_dir['basedir'] ) ) { | |
| 809 | - return ''; | |
| 810 | - } | |
| 811 | - | |
| 812 | - $base = trailingslashit( $upload_dir['basedir'] ) . 'templately' . DIRECTORY_SEPARATOR; | |
| 813 | - | |
| 814 | - // The guards go on the root so every subdirectory inherits them. | |
| 815 | - self::protect_directory( $base ); | |
| 816 | - | |
| 817 | - return '' === $sub ? $base : trailingslashit( $base . $sub ); | |
| 818 | 568 | } |
| 819 | 569 | |
| 820 | 570 | } |