Commit 5854970b authored by Arnolds's avatar Arnolds
Browse files

Refactored decryption logic in index.php: removed redundant variable...

Refactored decryption logic in index.php: removed redundant variable `secretResponse`, updated UI handling for passphrase inputs, and streamlined error handling flow.
parent c618d409
Loading
Loading
Loading
Loading
+14 −15
Original line number Diff line number Diff line
@@ -251,7 +251,7 @@ header('X-Robots-Tag: noindex, nofollow');
        }

        const decryptBtn = document.getElementById('decryptBtn');
        let secretResponse = null;
        let result = null;
        decryptBtn.addEventListener('click', async () => {
            const passphrase = document.getElementById('decryptPassphrase').value.trim();

@@ -259,9 +259,7 @@ header('X-Robots-Tag: noindex, nofollow');
            decryptBtn.disabled = true;

            try {
                // IMPORTANT: ideally this should be a POST that only "consumes" on purpose.
                let result;
                if (!secretResponse) {
                if (!result) {
                    const response = await fetch(`/secret-api.php?id=${encodeURIComponent(id)}&retrieve`, {
                        method: 'POST',
                        headers: {'Content-Type': 'application/json'},
@@ -269,9 +267,6 @@ header('X-Robots-Tag: noindex, nofollow');
                    });

                    result = await response.json().catch(() => ({}));
                } else {
                    result = secretResponse;
                }

                    // Generic failure (don’t reveal if id exists)
                    if (!response.ok || !result || !result.secret) {
@@ -281,6 +276,7 @@ header('X-Robots-Tag: noindex, nofollow');
                            '<p class="mb-0">Neizdevās atvērt noslēpumu. Iespējams, tā termiņš ir beidzies vai saite nav pareiza.</p>';
                        return;
                    }
                }

                // If secret is encrypted, require passphrase
                if (result.encrypted && !passphrase) {
@@ -293,6 +289,8 @@ header('X-Robots-Tag: noindex, nofollow');
                        ? await decryptMessage(result.secret, passphrase)
                        : result.secret;
                    document.getElementById('decryptedTextContainer').style.display = 'block';
                    document.getElementById('decryptBtn').style.display = 'none';
                    document.getElementById('passphrase-input-block').style.display = 'none';
                } catch {
                    alert('Nesanāca atkodēt 😬. Pārbaudi, vai burvju vārds ir pareizs 🔐🧙!');
                }
@@ -306,7 +304,8 @@ header('X-Robots-Tag: noindex, nofollow');
                decryptBtn.disabled = false;
            }
        });
    });
    })
    ;
</script>
<div class="modal fade" id="infoModal" tabindex="-1" aria-labelledby="infoModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg modal-dialog-scrollable">