add_action('rest_api_init', function () { register_rest_route('gamedooble/v1', '/config', array( 'methods' => array('POST', 'OPTIONS'), 'callback' => 'gamedooble_handle_config', 'permission_callback' => '__return_true', )); }); function gamedooble_handle_config($request) { // پاسخ به preflight (اگر جایی CORS/OPTIONS مطرح شد) if ($request->get_method() === 'OPTIONS') { return new WP_REST_Response(null, 200); } global $wpdb; $params = $request->get_json_params(); if (!is_array($params)) $params = []; $license_key = sanitize_text_field($params['license_key'] ?? ''); $hwid = sanitize_text_field($params['hwid'] ?? ''); if ($license_key === '') { return new WP_REST_Response([ 'success' => false, 'message' => 'کد لایسنس ارسال نشده است.' ], 400); } // HWID را خالی قبول نکن؛ چون در این حالت لایسنس ممکن است با مقدار خالی قفل شود if ($hwid === '') { return new WP_REST_Response([ 'success' => false, 'message' => 'HWID ارسال نشده است.' ], 400); } // تشخیص جدول صحیح لایسنس (با توجه به دیتابیس شما) $t1 = $wpdb->prefix . 'game_licenses'; $t2 = $wpdb->prefix . 'gamedooble_licenses'; $table_name = $wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $t1)) ? $t1 : null; if (!$table_name && $wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $t2))) { $table_name = $t2; } if (!$table_name) { return new WP_REST_Response([ 'success' => false, 'message' => 'جدول لایسنس در دیتابیس یافت نشد.' ], 500); } // جستجوی لایسنس $license = $wpdb->get_row( $wpdb->prepare("SELECT * FROM {$table_name} WHERE license_key = %s LIMIT 1", $license_key) ); if (!$license) { return new WP_REST_Response([ 'success' => false, 'message' => 'لایسنس یافت نشد.' ], 404); } // بررسی game_id $game_id = isset($license->game_id) ? intval($license->game_id) : 0; if ($game_id <= 0) { // اگر شما AES را همیشه در جدول ذخیره می‌کنید، می‌توانید این را آزادتر کنید // ولی برای حالت ترکیبی بهتر است شفاف خطا بدهیم // (یا اینکه فقط از aes_key جدول استفاده کنیم) } // دریافت AES از متاباکس محصول یا از رکورد $aes_key = ''; if ($game_id > 0) { $aes_key = (string) get_post_meta($game_id, '_game_aes_key', true); } if ($aes_key === '' && !empty($license->aes_key)) { $aes_key = (string) $license->aes_key; } if ($aes_key === '') { return new WP_REST_Response([ 'success' => false, 'message' => 'کلید رمزگشایی برای این بازی تعریف نشده است.' ], 400); } // HWID ثبت/بررسی $db_hwid = (string) ($license->hwid ?? ''); if ($db_hwid === '') { $wpdb->update( $table_name, ['hwid' => $hwid, 'status' => 'active'], ['id' => intval($license->id)], ['%s', '%s'], ['%d'] ); } elseif (!hash_equals($db_hwid, $hwid)) { return new WP_REST_Response([ 'success' => false, 'message' => 'این لایسنس روی سیستم دیگری فعال شده است.' ], 403); } return new WP_REST_Response([ 'success' => true, 'message' => 'لایسنس معتبر است.', 'aes_key' => $aes_key ], 200); }