I have a website called [Pastebin}(https://paste-bin.us), that is a minimalistic version of the original Pastebin, but I am having trouble pasting code. My server won’t accept it.
I tried encoding it in base64 which worked, and stored the data as base 64, and when I went to decode it through a tool it decoded perfectly, but when I view it at the generated pastebin link it displays like �������Y� ��T��T��ԑTUQT
and I can’t figure it out. I has a mostly working version that encoded and decoded perfectly but it wasn’t just displaying the code, but displaying it with the html in it. So if I had <h1>Test</h1>
in my code, it would display it as a header and not the plain code. I tried to find this again but it got lose in my revisions. Below I have my current (not working) code that has base64 encoding/decoding/displaying errors, I cant figure out which.
JS
<code> window.onload = function() {
document.querySelector('#pasteForm').addEventListener('submit', function(e) {
var textArea = document.querySelector('#textToPaste');
var formData = new FormData(e.target);
var text = formData.get('text').replace(/n/g, '<br>').replace(/^ /gm, ' ').replace(/^t/gm, ' ');
var encodedText = btoa(text); // Encode the text in base64
formData.set('text', '<pre>' + encodedText + '</pre>'); // Use the encoded text
var xhr = new XMLHttpRequest();
xhr.open('POST', 'create.php', true);
xhr.onload = function() {
if (xhr.status === 200) {
var password = formData.get('password');
document.querySelector('#linkBox').value = xhr.responseURL + '?password=' + encodeURIComponent(password);
document.querySelector('#linkBox').value = xhr.responseURL;
document.querySelector('#overlay').style.display = 'block';
document.querySelector('#popup').style.display = 'block';
console.error('An error occurred');
document.querySelector('#closeButton').addEventListener('click', function() {
document.querySelector('#overlay').style.display = 'none';
document.querySelector('#popup').style.display = 'none';
document.querySelector('#copyButton').addEventListener('click', function() {
var linkBox = document.querySelector('#linkBox');
document.execCommand('copy');
var copyButtonIcon = document.querySelector('#copyButton img');
var originalIconSrc = copyButtonIcon.src;
copyButtonIcon.src = 'https://cdn.cirrus.center/main/icons/check.png';
copyButtonIcon.src = originalIconSrc;
<code> window.onload = function() {
document.querySelector('#pasteForm').addEventListener('submit', function(e) {
e.preventDefault();
var textArea = document.querySelector('#textToPaste');
var formData = new FormData(e.target);
var text = formData.get('text').replace(/n/g, '<br>').replace(/^ /gm, ' ').replace(/^t/gm, ' ');
var encodedText = btoa(text); // Encode the text in base64
formData.set('text', '<pre>' + encodedText + '</pre>'); // Use the encoded text
var xhr = new XMLHttpRequest();
xhr.open('POST', 'create.php', true);
xhr.onload = function() {
if (xhr.status === 200) {
var password = formData.get('password');
if (password) {
document.querySelector('#linkBox').value = xhr.responseURL + '?password=' + encodeURIComponent(password);
} else {
document.querySelector('#linkBox').value = xhr.responseURL;
}
document.querySelector('#overlay').style.display = 'block';
document.querySelector('#popup').style.display = 'block';
} else {
console.error('An error occurred');
}
};
xhr.send(formData);
});
document.querySelector('#closeButton').addEventListener('click', function() {
document.querySelector('#overlay').style.display = 'none';
document.querySelector('#popup').style.display = 'none';
});
document.querySelector('#copyButton').addEventListener('click', function() {
var linkBox = document.querySelector('#linkBox');
linkBox.select();
document.execCommand('copy');
var copyButtonIcon = document.querySelector('#copyButton img');
var originalIconSrc = copyButtonIcon.src;
copyButtonIcon.src = 'https://cdn.cirrus.center/main/icons/check.png';
setTimeout(function() {
copyButtonIcon.src = originalIconSrc;
}, 2000);
});
};
</code>
window.onload = function() {
document.querySelector('#pasteForm').addEventListener('submit', function(e) {
e.preventDefault();
var textArea = document.querySelector('#textToPaste');
var formData = new FormData(e.target);
var text = formData.get('text').replace(/n/g, '<br>').replace(/^ /gm, ' ').replace(/^t/gm, ' ');
var encodedText = btoa(text); // Encode the text in base64
formData.set('text', '<pre>' + encodedText + '</pre>'); // Use the encoded text
var xhr = new XMLHttpRequest();
xhr.open('POST', 'create.php', true);
xhr.onload = function() {
if (xhr.status === 200) {
var password = formData.get('password');
if (password) {
document.querySelector('#linkBox').value = xhr.responseURL + '?password=' + encodeURIComponent(password);
} else {
document.querySelector('#linkBox').value = xhr.responseURL;
}
document.querySelector('#overlay').style.display = 'block';
document.querySelector('#popup').style.display = 'block';
} else {
console.error('An error occurred');
}
};
xhr.send(formData);
});
document.querySelector('#closeButton').addEventListener('click', function() {
document.querySelector('#overlay').style.display = 'none';
document.querySelector('#popup').style.display = 'none';
});
document.querySelector('#copyButton').addEventListener('click', function() {
var linkBox = document.querySelector('#linkBox');
linkBox.select();
document.execCommand('copy');
var copyButtonIcon = document.querySelector('#copyButton img');
var originalIconSrc = copyButtonIcon.src;
copyButtonIcon.src = 'https://cdn.cirrus.center/main/icons/check.png';
setTimeout(function() {
copyButtonIcon.src = originalIconSrc;
}, 2000);
});
};
PHP
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (!file_exists('paste')) {
$redirectIndex = '<?php' . PHP_EOL;
$redirectIndex .= 'header("Location: ..");' . PHP_EOL;
$redirectIndex .= 'exit;' . PHP_EOL;
file_put_contents('paste/index.php', $redirectIndex);
if (isset($_POST['text']) && $_POST['text'] !== '') {
$decodedText = base64_decode($_POST['text']); // Decode the base64 text
$decodedText = mb_convert_encoding($decodedText, 'UTF-8', 'auto'); // Convert the encoding to UTF-8
$index = '<?php' . PHP_EOL;
if (isset($_POST['password']) && $_POST['password'] !== '') {
$index .= 'if (!isset($_GET["password"]) || $_GET["password"] !== "' . $_POST['password'] . '") {' . PHP_EOL;
$index .= ' echo "<h1>Incorrect password</h1>";' . PHP_EOL;
$index .= ' exit;' . PHP_EOL;
$index .= 'echo <<<EOT' . PHP_EOL; // Use the decoded text
$index .= $decodedText . PHP_EOL;
$index .= 'EOT;' . PHP_EOL;
if (isset($_POST['burn'])) {
$index .= 'file_put_contents(__FILE__, "<h1>This paste has been deleted</h1>");' . PHP_EOL;
$index .= 'exit;' . PHP_EOL;
$index .= '?>' . PHP_EOL;
$index .= '<!DOCTYPE html>' . PHP_EOL;
$index .= '<html>' . PHP_EOL;
$index .= '<head>' . PHP_EOL;
$index .= ' <title>' . ($_POST['title'] ?? 'Pastebin') . '</title>' . PHP_EOL;
$index .= '</head>' . PHP_EOL;
$index .= '<body>' . PHP_EOL;
$index .= '</body>' . PHP_EOL;
file_put_contents("paste/$dir/index.php", $index);
header("Location: /paste/$dir");
<code><?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (!file_exists('paste')) {
mkdir('paste');
$redirectIndex = '<?php' . PHP_EOL;
$redirectIndex .= 'header("Location: ..");' . PHP_EOL;
$redirectIndex .= 'exit;' . PHP_EOL;
file_put_contents('paste/index.php', $redirectIndex);
}
$dir = uniqid();
mkdir("paste/$dir");
$decodedText = '';
if (isset($_POST['text']) && $_POST['text'] !== '') {
$decodedText = base64_decode($_POST['text']); // Decode the base64 text
$decodedText = mb_convert_encoding($decodedText, 'UTF-8', 'auto'); // Convert the encoding to UTF-8
}
$index = '<?php' . PHP_EOL;
if (isset($_POST['password']) && $_POST['password'] !== '') {
$index .= 'if (!isset($_GET["password"]) || $_GET["password"] !== "' . $_POST['password'] . '") {' . PHP_EOL;
$index .= ' echo "<h1>Incorrect password</h1>";' . PHP_EOL;
$index .= ' exit;' . PHP_EOL;
$index .= '}' . PHP_EOL;
}
$index .= 'echo <<<EOT' . PHP_EOL; // Use the decoded text
$index .= $decodedText . PHP_EOL;
$index .= 'EOT;' . PHP_EOL;
if (isset($_POST['burn'])) {
$index .= 'file_put_contents(__FILE__, "<h1>This paste has been deleted</h1>");' . PHP_EOL;
$index .= 'exit;' . PHP_EOL;
}
$index .= '?>' . PHP_EOL;
$index .= '<!DOCTYPE html>' . PHP_EOL;
$index .= '<html>' . PHP_EOL;
$index .= '<head>' . PHP_EOL;
$index .= ' <title>' . ($_POST['title'] ?? 'Pastebin') . '</title>' . PHP_EOL;
$index .= '</head>' . PHP_EOL;
$index .= '<body>' . PHP_EOL;
$index .= '' . PHP_EOL;
$index .= '</body>' . PHP_EOL;
$index .= '</html>';
file_put_contents("paste/$dir/index.php", $index);
header("Location: /paste/$dir");
echo "/paste/$dir";
}
</code>
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (!file_exists('paste')) {
mkdir('paste');
$redirectIndex = '<?php' . PHP_EOL;
$redirectIndex .= 'header("Location: ..");' . PHP_EOL;
$redirectIndex .= 'exit;' . PHP_EOL;
file_put_contents('paste/index.php', $redirectIndex);
}
$dir = uniqid();
mkdir("paste/$dir");
$decodedText = '';
if (isset($_POST['text']) && $_POST['text'] !== '') {
$decodedText = base64_decode($_POST['text']); // Decode the base64 text
$decodedText = mb_convert_encoding($decodedText, 'UTF-8', 'auto'); // Convert the encoding to UTF-8
}
$index = '<?php' . PHP_EOL;
if (isset($_POST['password']) && $_POST['password'] !== '') {
$index .= 'if (!isset($_GET["password"]) || $_GET["password"] !== "' . $_POST['password'] . '") {' . PHP_EOL;
$index .= ' echo "<h1>Incorrect password</h1>";' . PHP_EOL;
$index .= ' exit;' . PHP_EOL;
$index .= '}' . PHP_EOL;
}
$index .= 'echo <<<EOT' . PHP_EOL; // Use the decoded text
$index .= $decodedText . PHP_EOL;
$index .= 'EOT;' . PHP_EOL;
if (isset($_POST['burn'])) {
$index .= 'file_put_contents(__FILE__, "<h1>This paste has been deleted</h1>");' . PHP_EOL;
$index .= 'exit;' . PHP_EOL;
}
$index .= '?>' . PHP_EOL;
$index .= '<!DOCTYPE html>' . PHP_EOL;
$index .= '<html>' . PHP_EOL;
$index .= '<head>' . PHP_EOL;
$index .= ' <title>' . ($_POST['title'] ?? 'Pastebin') . '</title>' . PHP_EOL;
$index .= '</head>' . PHP_EOL;
$index .= '<body>' . PHP_EOL;
$index .= '' . PHP_EOL;
$index .= '</body>' . PHP_EOL;
$index .= '</html>';
file_put_contents("paste/$dir/index.php", $index);
header("Location: /paste/$dir");
echo "/paste/$dir";
}