Add "_instance_id" setting

This commit is contained in:
Dmitriy Simushev 2015-06-05 11:33:19 +00:00
parent fbe53e3b29
commit 25054c4e0f
4 changed files with 87 additions and 0 deletions

View File

@ -440,6 +440,46 @@ class Installer
return false;
}
// Generate Unique ID for Mibew Instance
try {
list($count) = $db->query(
'SELECT COUNT(*) FROM {config} WHERE vckey = :key',
array(':key' => '_instance_id'),
array(
'return_rows' => Database::RETURN_ONE_ROW,
'fetch_type' => Database::FETCH_NUM,
)
);
if ($count == 0) {
$db->query(
'INSERT INTO {config} (vckey, vcvalue) VALUES (:key, :value)',
array(
':key' => '_instance_id',
':value' => Utils::generateInstanceId(),
)
);
} else {
// The option is already in the database. It seems that
// something went wrong with the previous installation attempt.
// Just update the instance ID.
$db->query(
'UPDATE {config} SET vcvalue = :value WHERE vckey = :key',
array(
':key' => '_instance_id',
':value' => Utils::generateInstanceId(),
)
);
}
} catch (\Exception $e) {
$this->errors[] = getlocal(
'Cannot store instance ID. Error {0}',
array($e->getMessage())
);
return false;
}
return true;
}

View File

@ -350,6 +350,15 @@ class Updater
. 'description text, '
. 'UNIQUE KEY target (target) '
. ') charset utf8 ENGINE=InnoDb');
// Generate Unique ID of Mibew instance.
$db->query(
'INSERT INTO {config} (vckey, vcvalue) VALUES (:key, :value)',
array(
':key' => '_instance_id',
':value' => Utils::generateInstanceId(),
)
);
} catch (\Exception $e) {
$this->errors[] = getlocal('Cannot update tables: {0}', $e->getMessage());

View File

@ -101,6 +101,40 @@ class Utils
return $updates;
}
/**
* Generates random unique 64 characters length ID for Mibew instance.
*
* WARNING: This ID should not be used for any security/cryptographic. If
* you need an ID for such purpose you have to use PHP's
* {@link openssl_random_pseudo_bytes()} function instead.
*
* @return string
*/
public static function generateInstanceId()
{
$chars = '0123456789abcdefghijklmnopqrstuvwxyz';
$rnd = (string)microtime(true);
// Add ten random characters before and after the timestamp
$max_char = strlen($chars) - 1;
for ($i = 0; $i < 10; $i++) {
$rnd = $chars[rand(0, $max_char)] . $rnd . $chars[rand(0, $max_char)];
}
if (function_exists('hash')) {
// There is hash function that can give us 64-length hash.
return hash('sha256', $rnd);
}
// We should build random 64 character length hash using old'n'good md5
// function.
$middle = (int)floor(strlen($rnd) / 2);
$rnd_left = substr($rnd, 0, $middle);
$rnd_right = substr($rnd, $middle);
return md5($rnd_left) . md5($rnd_right);
}
/**
* This class should not be instantiated
*/

View File

@ -115,6 +115,10 @@ class Settings
// underscore sign(_).
// Unix timestamp when cron job ran last time.
'_last_cron_run' => 0,
// Random unique ID which is used for getting info about new
// updates. This value is initialized during Installation or Update
// process.
'_instance_id' => '',
);
// Load values from database