Пример #1
0
    # We have a different size
    $this_size_changed = false;
    if ((int) $partition->data['size'] != (int) $size_data['size']) {
        # Flag size changed
        $size_changed = true;
        $this_size_changed = true;
        # Storing the currently saved size
        $update_queries[] = "\tSET @size_delta = \t(\n\t\t\t\t\t\t\t\t\t\tSELECT \n\t\t\t\t\t\t\t\t\t\t" . (int) $size_data['size'] . "- CONVERT(`size`,SIGNED)\n\t\t\t\t\t\t\t\t\t\tFROM\n\t\t\t\t\t\t\t\t\t\t\t" . NQ_TABLE_PARTITIONS_TABLE . "\n\t\t\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\t\t\t`id`=" . (int) $id . "\n\t\t\t\t\t\t\t\t\t\tLIMIT 1\n\t\t\t\t\t\t\t\t\t)";
        # Updating our size change
        $update_queries[] = "\tSET @table_size_delta = @table_size_delta + @size_delta";
    }
    # Updating the partitions size
    $update_queries[] = "\tUPDATE\n\t\t\t\t\t\t" . NQ_TABLE_PARTITIONS_TABLE . "\n\t\t\t\t\tSET\n\t\t\t\t\t\t" . ($this_size_changed ? "`size` = `size` + @size_delta," : "") . "\n\t\t\t\t\t\t`modified`\t=NOW(),\n\t\t\t\t\t\t`accessed`\t=NOW()\n\t\t\t\t\tWHERE\n\t\t\t\t\t\t`id`\t\t=" . (int) $id . "\n\t\t\t\t\tLIMIT 1";
}
# Updating the table
$update_queries[] = "\tUPDATE\n\t\t\t\t\t" . NQ_TABLE_SETTINGS_TABLE . "\n\t\t\t\tSET\n\t\t\t\t\t" . ($size_changed ? "`size` = `size` + @table_size_delta," : "") . "\n\t\t\t\t\t`modified`\t=NOW(),\n\t\t\t\t\t`accessed`\t=NOW()\n\t\t\t\tWHERE\n\t\t\t\t\t`id`\t\t=" . (int) $G_TABLE_DETAILS['id'] . "\n\t\t\t\tLIMIT 1";
# Adjusting our app database size
if ($size_changed) {
    $update_queries[] = "\tUPDATE\n\t\t\t\t\t\t" . NQ_APPS_TABLE . "\n\t\t\t\t\tSET\n\t\t\t\t\t\t`db_size`=`db_size`+ @table_size_delta\n\t\t\t\t\tWHERE\n\t\t\t\t\t\t`id`=" . (int) $G_APP_DATA['id'] . "\n\t\t\t\t\tLIMIT 1";
}
# Running our update queries
mysqli_multi_sub_query($G_CONTROLLER_DBLINK, implode(';', $update_queries));
# Opening our tracking dblink
$G_TRACKING_DBLINK = mysqli_shared_connect(NQ_TRACKING_HOST, NQ_TRACKING_USERNAME, NQ_TRACKING_PASSWORD, $G_SHARED_DBLINKS);
# Closing the controller dblink
mysqli_shared_close($G_CONTROLLER_DBLINK, $G_SHARED_DBLINKS);
# Adding table analytics
$query = "\tINSERT INTO\n\t\t\t\t" . NQ_TRACKING_TABLE_IO . "\n\t\t\tSET\n\t\t\t\t`app_id`\t=" . (int) $G_APP_DATA['id'] . ",\n\t\t\t\t`table_id`\t=" . (int) $G_TABLE_DETAILS['id'] . ",\n\t\t\t\t`environment`\t='" . mysqli_escape_string($G_TRACKING_DBLINK, $G_APP_ENVIRONMENT) . "',\n\t\t\t\t`writes`\t=1,\n\t\t\t\t`write_rows`\t=" . (int) $content->affected_rows . ",\n\t\t\t\t`created`\t='" . date('Y-m-d H:00:00') . "'";
mysqli_sub_query($G_TRACKING_DBLINK, $query);
# Adding our usage - Always call this last
track_endpoint($G_SHARED_DBLINKS, $G_APP_DATA['id'], $G_APP_ENVIRONMENT, $_ENDPOINT);
Пример #2
0
 }
 # Type of table
 if (isset($_JPOST->partition_size)) {
     $update_columns[] = "`partition_size`='" . mysqli_escape_string($G_CONTROLLER_DBLINK, $_JPOST->partition_size) . "'";
 }
 # If we have something to update
 if (count($update_columns) > 0) {
     $query = "\tUPDATE\n\t\t\t\t\t\t\t" . NQ_TABLE_SETTINGS_TABLE . "\n\t\t\t\t\t\tSET\n\t\t\t\t\t\t\t" . implode(',', $update_columns) . "\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t`id`\t\t=" . (int) $G_TABLE_ID . "\n\t\t\t\t\t\tLIMIT 1";
     mysqli_sub_query($G_CONTROLLER_DBLINK, $query);
 }
 # Loading the table partitions
 if (isset($_JPOST->columns) || isset($_JPOST->indices)) {
     $query = "\tSELECT\n\t\t\t\t\t\t\t`p`.*,\n\t\t\t\t\t\t\t`d`.`host`,\n\t\t\t\t\t\t\t`d`.`username`,\n\t\t\t\t\t\t\t`d`.`password`,\n\t\t\t\t\t\t\t`d`.`database`\n\t\t\t\t\t\tFROM\n\t\t\t\t\t\t\t" . NQ_TABLE_PARTITIONS_TABLE . " `p`\n\t\t\t\t\t\tLEFT JOIN\n\t\t\t\t\t\t\t" . NQ_SERVERS_TABLE . " `d`\n\t\t\t\t\t\t\tON\n\t\t\t\t\t\t\t\t`d`.`id`=`p`.`host_id`\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t`table_id`\t=" . (int) $G_TABLE_ID;
     $result = mysqli_multi_result_query($G_CONTROLLER_DBLINK, $query);
     while ($partition_data = mysqli_fetch_assoc($result)) {
         $partition_data['dblink'] = mysqli_shared_connect($partition_data['host'], $partition_data['username'], $partition_data['password'], $G_SHARED_DBLINKS);
         $G_TABLE_PARTITIONS[] = $partition_data;
     }
 }
 # Switching to big from small/medium/large
 if (isset($_JPOST->partition_size) && $_JPOST->partition_size == 'big' && $G_TABLE_DETAILS['partition_size'] != 'big') {
     foreach ($G_TABLE_PARTITIONS as $partition) {
         $query = "\tALTER TABLE\n\t\t\t\t\t\t\t\t`" . $partition['database'] . "`.`" . $partition['table_name'] . "`\n\t\t\t\t\t\t\tDISABLE KEYS;\n\t\t\t\t\t\t\tALTER TABLE\n\t\t\t\t\t\t\t\t`" . $partition['database'] . "`.`" . $partition['table_name'] . "`\n\t\t\t\t\t\t\tCHANGE COLUMN\n\t\t\t\t\t\t\t\t`id` `id` BIGINT UNSIGNED;\n\t\t\t\t\t\t\tALTER TABLE\n\t\t\t\t\t\t\t\t`" . $partition['database'] . "`.`" . $partition['table_name'] . "`\n\t\t\t\t\t\t\tENABLE KEYS;";
         mysqli_multi_sub_query($partition['dblink'], $query, true);
     }
 }
 # Switching to small/medium/large from big
 if (isset($_JPOST->partition_size) && $_JPOST->partition_size != 'big' && $G_TABLE_DETAILS['partition_size'] == 'big') {
     foreach ($G_TABLE_PARTITIONS as $partition) {
         $query = "\tALTER TABLE\n\t\t\t\t\t\t\t\t`" . $partition['database'] . "`.`" . $partition['table_name'] . "`\n\t\t\t\t\t\t\tDISABLE KEYS;\n\t\t\t\t\t\t\tALTER TABLE\n\t\t\t\t\t\t\t\t`" . $partition['database'] . "`.`" . $partition['table_name'] . "`\n\t\t\t\t\t\t\tCHANGE COLUMN\n\t\t\t\t\t\t\t\t`id` `id` INT UNSIGNED;\n\t\t\t\t\t\t\tALTER TABLE\n\t\t\t\t\t\t\t\t`" . $partition['database'] . "`.`" . $partition['table_name'] . "`\n\t\t\t\t\t\t\tENABLE KEYS;";
         mysqli_multi_sub_query($partition['dblink'], $query, true);
Пример #3
0
require_once __DIR__ . '/shutdown.php';
require_once __DIR__ . '/functions.php';
require_once __DIR__ . '/parsers/post.php';
# Manipulating our endpoint data
$_URI = explode('?', $_SERVER['REQUEST_URI']);
$_ABS_BASEDIR = explode('/', NQ_RELATIVE_PATH);
$_BASEDIR = explode('/', $_URI[0]);
$_BASEDIR_CULLED = array_splice($_BASEDIR, 0, max(0, count($_ABS_BASEDIR) - 1));
$_FILENAME = array_splice($_BASEDIR, -1)[0];
$_ENDPOINT = isset($_BASEDIR[2]) ? $_BASEDIR[2] : $_FILENAME;
$_CGET = array();
isset($_URI[1]) && parse_str($_URI[1], $_CGET);
# Setting up our local connection
$G_SHARED_DBLINKS = [];
$G_CONTROLLER_DBLINK = mysqli_shared_connect(NQ_CONTROLLER_HOST, NQ_CONTROLLER_USERNAME, NQ_CONTROLLER_PASSWORD, $G_SHARED_DBLINKS);
$G_STORAGE_CONTROLLER_DBLINK = mysqli_shared_connect(NQ_FILE_STORAGE_HOST, NQ_FILE_STORAGE_USERNAME, NQ_FILE_STORAGE_PASSWORD, $G_SHARED_DBLINKS);
# If our mysql database is down
if (!$G_CONTROLLER_DBLINK || !$G_STORAGE_CONTROLLER_DBLINK) {
    exit_fail(NQ_ERROR_SERVICE_UNAVAILABLE, 'Service temporarily unavailable.', false);
}
# If we are debugging
if (NQ_DEBUG_ENABLED) {
    # New debug object
    $G_DEBUG_DATA = new stdClass();
    # If we want to include the config
    if (NQ_DEBUG_CONFIG) {
        # Getting the config
        $config = get_defined_constants(true)['user'];
        # Security unsets
        unset($config['NQ_CONTROLLER_PASSWORD']);
        $G_DEBUG_DATA->config = $config;
Пример #4
0
function get_table_partition($controller_dblink, $storage_dblink, $app_data, $table_data, $record_data, $bitmask, &$partition_stack, &$dbstack)
{
    # Calculating our evaluation value
    $evaluation = get_table_partition_evaluation($table_data, $record_data);
    # Checking if we have already selected this partition
    foreach ($partition_stack as $partition) {
        if ($partition->data['table_id'] == $table_data['id'] && $partition->data['evaluation'] == $evaluation) {
            return $partition;
        }
    }
    # Getting our partition
    $query = "\tSELECT\n\t\t\t\t\t*\n\t\t\t\tFROM\n\t\t\t\t\t" . NQ_TABLE_PARTITIONS_TABLE . "\n\t\t\t\tWHERE\n\t\t\t\t\t`table_id`\t\t=" . (int) $table_data['id'] . " AND\n\t\t\t\t\t`evaluation`\t\t='" . mysqli_escape_string($controller_dblink, $evaluation) . "'\n\t\t\t\tLIMIT 1";
    $partition_data = mysqli_single_result_query($controller_dblink, $query);
    # Bad partition, need to create a new one
    if (!isset($partition_data['table_id'])) {
        # Check if we have partitions available
        if ($bitmask[1] == 0 && $table_data['partitions'] > 0 || $bitmask[1] > 0 && $table_data['partitions'] + 1 > bindec(str_repeat('1', $bitmask[1]))) {
            return false;
        }
        # Inserting the new partition
        $query = "\tLOCK TABLE " . NQ_TABLE_PARTITIONS_TABLE . " WRITE;\n\t\t\t\t\tSET @partition_number=\t(\n\t\t\t\t\t\t\t\t\tSELECT\n\t\t\t\t\t\t\t\t\t\tCOUNT(*)\n\t\t\t\t\t\t\t\t\tFROM\n\t\t\t\t\t\t\t\t\t\t" . NQ_TABLE_PARTITIONS_TABLE . "\n\t\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\t\t`table_id`\t=" . (int) $table_data['id'] . "\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\tSET @id=\t\t(\n\t\t\t\t\t\t\t\t\tSELECT\n\t\t\t\t\t\t\t\t\t\t`id`\n\t\t\t\t\t\t\t\t\tFROM\n\t\t\t\t\t\t\t\t\t\t" . NQ_TABLE_PARTITIONS_TABLE . "\n\t\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\t\t`table_id`\t=" . (int) $table_data['id'] . " AND\n\t\t\t\t\t\t\t\t\t\t`evaluation`\t='" . mysqli_escape_string($controller_dblink, $evaluation) . "'\n\t\t\t\t\t\t\t\t\tLIMIT 1\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\tINSERT INTO\n\t\t\t\t\t\t" . NQ_TABLE_PARTITIONS_TABLE . "\n\t\t\t\t\tSET\n\t\t\t\t\t\t`id`\t\t=IF(ISNULL(@id),NULL,@id),\n\t\t\t\t\t\t`app_id`\t=" . (int) $app_data['id'] . ",\n\t\t\t\t\t\t`table_id`\t=" . (int) $table_data['id'] . ",\n\t\t\t\t\t\t`number`\t=@partition_number,\n\t\t\t\t\t\t`evaluation`\t='" . mysqli_escape_string($controller_dblink, $evaluation) . "',\n\t\t\t\t\t\t`created`\t=NOW(),\n\t\t\t\t\t\t`modified`\t=NOW(),\n\t\t\t\t\t\t`accessed`\t=NOW();\n\t\t\t\t\tUNLOCK TABLES";
        mysqli_multi_sub_query($controller_dblink, $query);
        # Getting our partition
        $query = "\tSELECT\n\t\t\t\t\t\t*\n\t\t\t\t\tFROM\n\t\t\t\t\t\t" . NQ_TABLE_PARTITIONS_TABLE . "\n\t\t\t\t\tWHERE\n\t\t\t\t\t\t`table_id`\t=" . (int) $table_data['id'] . " AND\n\t\t\t\t\t\t`evaluation`\t='" . mysqli_escape_string($controller_dblink, $evaluation) . "'\n\t\t\t\t\tLIMIT 1";
        $partition_data = mysqli_single_result_query($controller_dblink, $query);
        # Creating our partition id table
        $create_query = "CREATE TABLE IF NOT EXISTS " . NQ_DATABASE_STORAGE_DATABASE . ".`" . NQ_DATABASE_PARTITION_PREFIX . $app_data['id'] . "_" . $table_data['id'] . "_" . $partition_data['number'] . "`\n\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\t`id` " . (array_sum($bitmask) > 32 ? 'BIGINT' : 'INT') . " UNSIGNED AUTO_INCREMENT PRIMARY KEY,\n\t\t\t\t\t\t\t\t`data` TINYINT(1) UNSIGNED\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\tENGINE=" . NQ_MYSQL_TABLE_ENGINE;
        mysqli_sub_query($storage_dblink, $create_query);
        # Getting our free server space
        $query = "\tSELECT\n\t\t\t\t\t\t*\n\t\t\t\t\tFROM\n\t\t\t\t\t\t" . NQ_SERVERS_TABLE . "\n\t\t\t\t\tWHERE\n\t\t\t\t\t\t`environment` \t\t\tIN ('" . mysqli_escape_string($controller_dblink, $table_data['environment'] == '*' ? 'prod' : $table_data['environment']) . "','*') AND\n\t\t\t\t\t\t(\n\t\t\t\t\t\t\t`host_app_id` \t\tIN (" . (int) $app_data['id'] . ",0) OR\n\t\t\t\t\t\t\t`host_app_group` \tIN (" . (int) $app_data['group'] . ",0)\n\t\t\t\t\t\t)\n\t\t\t\t\tORDER BY\n\t\t\t\t\t\t`environment` DESC,\n\t\t\t\t\t\t`host_app_id` DESC,\n\t\t\t\t\t\t`host_app_group` DESC,\n\t\t\t\t\t\t`available_space` DESC\n\t\t\t\t\tLIMIT 1";
        $server_data = mysqli_single_result_query($controller_dblink, $query);
        # Temp create table hold
        $query = "SHOW CREATE TABLE " . NQ_DATABASE_STORAGE_DATABASE . ".`" . NQ_DATABASE_STRUCTURE_PREFIX . $app_data['id'] . '_' . $partition_data['table_id'] . "`";
        $create_result = mysqli_single_result_query($storage_dblink, $query);
        # Creating the new create syntax
        $partition_table_name = $app_data['id'] . '_' . $partition_data['table_id'] . '_' . $partition_data['id'];
        $syntax = explode('`', $create_result['Create Table']);
        $syntax[0] = "CREATE TABLE IF NOT EXISTS ";
        $syntax[1] = $server_data['database'] . '`.`' . $partition_table_name;
        $syntax = implode('`', $syntax);
        # Update the partition data
        $query = "\tUPDATE\n\t\t\t\t\t\t" . NQ_TABLE_PARTITIONS_TABLE . "\n\t\t\t\t\tSET\n\t\t\t\t\t\t`host_id`\t=" . (int) $server_data['id'] . ",\n\t\t\t\t\t\t`table_name`\t='" . mysqli_escape_string($controller_dblink, $partition_table_name) . "'\n\t\t\t\t\tWHERE\n\t\t\t\t\t\t`id`\t\t=" . $partition_data['id'] . "\n\t\t\t\t\tLIMIT 1";
        mysqli_sub_query($controller_dblink, $query);
        $partition_data['host_id'] = $server_data['id'];
        $partition_data['table_name'] = $partition_table_name;
        # Open up a new connection
        $partition_dblink = mysqli_shared_connect($server_data['host'], $server_data['username'], $server_data['password'], $dbstack);
        $partition_database = $server_data['database'];
        # Creating our table
        mysqli_sub_query($partition_dblink, $syntax);
    } else {
        # Getting our free server space
        $query = "\tSELECT\n\t\t\t\t\t\t*\n\t\t\t\t\tFROM\n\t\t\t\t\t\t" . NQ_SERVERS_TABLE . "\n\t\t\t\t\tWHERE\n\t\t\t\t\t\t`id`=" . (int) $partition_data['host_id'] . "\n\t\t\t\t\tLIMIT 1";
        $server_data = mysqli_single_result_query($controller_dblink, $query);
        # Open up a new connection
        $partition_dblink = mysqli_shared_connect($server_data['host'], $server_data['username'], $server_data['password'], $dbstack);
        $partition_database = $server_data['database'];
    }
    # Returning the partition data
    $partition = (object) ['data' => $partition_data, 'database' => $partition_database, 'dblink' => $partition_dblink];
    $partition_stack[] = $partition;
    return $partition;
}
function track_endpoint(&$dblinks, $app_id, $environment, $endpoint)
{
    # If we want to track our usage
    if (NQ_TRACKING_ENABLED) {
        # Connecting to our tracking database
        $dblink = mysqli_shared_connect(NQ_TRACKING_HOST, NQ_TRACKING_USERNAME, NQ_TRACKING_PASSWORD, $dblinks);
        # Logging our usage
        $query = "\tINSERT INTO\n\t\t\t\t\t\t" . NQ_TRACKING_TABLE . "\n\t\t\t\t\tSET\n\t\t\t\t\t\t`app_id`\t=" . (int) $app_id . ",\n\t\t\t\t\t\t`environment`\t='" . mysqli_escape_string($dblink, $environment) . "',\n\t\t\t\t\t\t`endpoint`\t='" . mysqli_escape_string($dblink, $endpoint) . "',\n\t\t\t\t\t\t`receive_size`\t=" . (defined('NQ_RECEIVE_SIZE') ? NQ_RECEIVE_SIZE : strlen($_SERVER['QUERY_STRING'])) . ",\n\t\t\t\t\t\t`send_size`\t=" . (defined('NQ_SEND_SIZE') ? NQ_SEND_SIZE : 0) . ",\n\t\t\t\t\t\t`send_time`\t=" . ((defined('NQ_SEND_TIME') ? NQ_SEND_TIME : microtime(true)) - NQ_TRACKING_START_TIME) . ",\n\t\t\t\t\t\t`complete_time`\t=" . (microtime(true) - NQ_TRACKING_START_TIME) . ",\n\t\t\t\t\t\t`created`\t=NOW()";
        mysqli_sub_query($dblink, $query);
    }
    # Logging our endpoint
    log_access($endpoint, true);
}
Пример #6
0
define('NQ_EMAIL_BLOCK_HEADER', 'font-weight:bold;margin:20px 0px 0px;background-color:#277BF7;padding:10px;color:white;border-radius:10px 10px 0px 0px;');
define('NQ_EMAIL_BLOCK_BODY', 'padding:10px;border:solid #F0F0FF 1px;border-radius:0px 0px 5px 5px;line-height:1.5;background-color:#F9F9F9;');
define('NQ_EMAIL_BLOCK_LABEL', 'display:inline-block;font-weight:bold;width:125px;');
# Constants for email building
define('CRLF', "\r\n");
# Characters we don't want able to be used
define('NQ_INVALID_PATH_CHARS', '\\/');
define('NQ_INVALID_TAGS', 'iframe,frame,meta,script,style,form');
# Includes
require_once __DIR__ . '/functions.php';
require_once __DIR__ . '/parsers/post.php';
require_once __DIR__ . '/parsers/template.php';
require_once __DIR__ . '/parsers/sqlformatter.php';
# Connecting to our database
$G_SHARED_DBLINKS = [];
$G_CONTROLLER_DBLINK = mysqli_shared_connect(NQ_CONTROLLER_HOST, NQ_CONTROLLER_USERNAME, NQ_CONTROLLER_PASSWORD, $G_SHARED_DBLINKS);
$G_STORAGE_CONTROLLER_DBLINK = $G_CONTROLLER_DBLINK;
# If we are debugging
if (NQ_DEBUG_ENABLED) {
    # New debug object
    $G_DEBUG_DATA = new stdClass();
    # If we want to include the config
    if (NQ_DEBUG_CONFIG) {
        # Getting the config
        $config = get_defined_constants(true)['user'];
        # Security unsets
        unset($config['NQ_CONTROLLER_PASSWORD']);
        $G_DEBUG_DATA->config = $config;
    }
    # Debug object
    $G_DEBUG_DATA->database = 'mysql ' . mysqli_get_server_info($G_CONTROLLER_DBLINK);