<commit_before><?php
// error_reporting(E_ALL);
// ini_set('display_errors', True);

header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json; charset=utf-8');
header('Cache-Control: no-cache'); // recommended to prevent caching of event data.

define('MAX_LENGTH', 4294967295);

$opt = strtoupper($_POST['option']);
$app = $_POST['app'];
$password = <PASSWORD>($_POST['password']);
if (array_key_exists('version', $_POST)) {
    $version = $_POST['version'];
} else {
    $version = 0;
}
$version = $version . '';

$response = '';

$db = new mysqli("172.16.31.10","feifeiha_public","p0OnMM722iqZ","feifeiha_big_db");

// Check connection
if ($db->connect_error) {
    $response = "{\"code\": 500, \"status\": \"error\", \"error\": \"Failed to connect to MySQL: " . $db->connect_error . "\"}";
}
else {
    switch ($opt) {
        case 'UPGRADE':
        $response = newVersion($app, $password, $db);
        break;

        case 'TOP':
        $response = listVersions($app, $password, $db);
        break;

        case 'PULL':
        $response = fetchDB($app, $password, $version, $db);
        break;

        case 'PULL KEY':
        $response = fetchKeyDB($app, $password, $version, $_POST['key'], $db);
        break;

        case 'PUSH':
        $response = pushDB($app, $password, $version, $_POST['json'], $db);
        break;

        case 'UPDATE':
        $response = updateKeyDB($app, $password, $version, $_POST['key'], $_POST['json'], $db);
        break;

        case 'REMOVE':
        $response = removeKeyDB($app, $password, $version, $_POST['key'], $db);
        break;

        case 'CREATE':
        $response = createDB($app, $password, $db);
        break;

        default:
        $response = "{\"code\": 500, \"status\": \"error\", \"error\": \"Bad request.\"}";
        break;
    }
}

function newVersion($app, $password, $db) {

    $stmt = $db->prepare('SELECT password FROM Basement WHERE AppName = ?');
    $stmt->bind_param('s', $app);
    $stmt->execute();

    $stmt->store_result();

    $stmt->bind_result($pass);
    while ($stmt->fetch()) {
        if ($pass !== $password) {
            $stmt->close();
            return "{\"status\": \"error\", \"error\": \"wrong password.\", \"code\": 403}";
        }
    }
    $rows = $stmt->num_rows;

    $stmt->close();

    $time = time();
    $json = '{}';
    $version = $rows;

    $stmt = $db->prepare('INSERT INTO Basement (AppName, Password, LastModified, JSON, Version) VALUES (?, ?, ?, ?, ?)');
    $stmt->bind_param('sssss', $app, $password, $time, $json, $version);<commit_msg>more detailed status information<commit_after><?php
// error_reporting(E_ALL);
// ini_set('display_errors', True);

header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json; charset=utf-8');
header('Cache-Control: no-cache'); // recommended to prevent caching of event data.

define('MAX_LENGTH', 4294967295);

if (!array_key_exists('option', $_POST) ||
    !array_key_exists('app', $_POST) ||
    !array_key_exists('password', $_POST)) {
    echo "{\"code\": 500, \"status\": \"error\", \"error\": \"Bad parameters.\"}";
}

$opt = strtoupper($_POST['option']);
$app = $_POST['app'];
$password = <PASSWORD>($_POST['password']);
if (array_key_exists('version', $_POST)) {
    $version = $_POST['version'];
} else {
    $version = 0;
}
$version = $version . '';

$response = '';

$db = new mysqli("172.16.31.10","feifeiha_public","p0OnMM722iqZ","feifeiha_big_db");

// Check connection
if ($db->connect_error) {
    $response = "{\"code\": 500, \"status\": \"error\", \"error\": \"Failed to connect to MySQL: " . $db->connect_error . "\"}";
}
else {
    try {
        switch ($opt) {
            case 'UPGRADE':
            $response = newVersion($app, $password, $db);
            break;

            case 'TOP':
            $response = listVersions($app, $password, $db);
            break;

            case 'PULL':
            $response = fetchDB($app, $password, $version, $db);
            break;

            case 'PULL KEY':
            if (!array_key_exists('key', $_POST)) {
                $response = "{\"code\": 500, \"status\": \"error\", \"error\": \"Missing 'key' parameter.\"}";
            } else {
                $response = fetchKeyDB($app, $password, $version, $_POST['key'], $db);
            }

            break;

            case 'PUSH':
            if (!array_key_exists('json', $_POST)) {
                $response = "{\"code\": 500, \"status\": \"error\", \"error\": \"Missing 'JSON' parameter.\"}";
            } else {
                $response = pushDB($app, $password, $version, $_POST['json'], $db);
            }
            break;

            case 'UPDATE':
            if (!array_key_exists('key', $_POST) ||
                !array_key_exists('json', $_POST)) {
                $response = "{\"code\": 500, \"status\": \"error\", \"error\": \"Missing 'key' or 'JSON' parameter.\"}";
            } else {
                $response = updateKeyDB($app, $password, $version, $_POST['key'], $_POST['json'], $db);
            }
            break;

            case 'REMOVE':
            if (!array_key_exists('key', $_POST)) {
                $response = "{\"code\": 500, \"status\": \"error\", \"error\": \"Missing 'key' parameter.\"}";
            } else {
                $response = removeKeyDB($app, $password, $version, $_POST['key'], $db);
            }
            break;

            case 'CREATE':
            $response = createDB($app, $password, $db);
            break;

            default:
            $response = "{\"code\": 400, \"status\": \"error\", \"error\": \"Bad request.\"}";
            break;
        }
    } catch (Exception $e) {
        mysqli_close($db);

        echo "{\"code\": 0, \"status\": \"unknown error\", \"error\": \"".$e->getMessage()."\"}";
    }
}

function newVersion($app, $password, $db) {

    $stmt = $db->prepare('SELECT password FROM Basement WHERE AppName = ?');
    $stmt->bind_param('s', $app);
    $stmt->execute();

    $stmt->store_result();

    $stmt->bind_result($pass);
    while ($stmt->fetch()) {
        if ($pass !== $password) {
            $stmt->close();
            return "{\"status\": \"error\", \"error\": \"wrong password.\", \"code\": 403}";
        }
    }
    $rows = $stmt->num_rows;

    $stmt->close();

    $time = time();
    $json = '{}';
    $version = $rows;

    $stmt = $db->prepare('INSERT INTO Basement (AppName, Password, LastModified, JSON, Version) VALUES (?, ?, ?, ?, ?)');
    $stmt->bind_param('sssss', $app, $password, $time, $json, $version);
{'fraction_non_alphanumeric': 0.21745068285280728, 'fraction_numerical': 0.012746585735963581, 'mean_word_length': 2.4292403746097815, 'pattern_counts': {'":': 36, '<': 7, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7173108', 'n_tokens_mistral': 2170, 'n_tokens_neox': 2038, 'n_words': 464}