Skip to content

Commit

Permalink
Add "_instance_id" setting
Browse files Browse the repository at this point in the history
  • Loading branch information
JustBlackBird committed Jun 5, 2015
1 parent fbe53e3 commit 25054c4
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/mibew/libs/classes/Mibew/Maintenance/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,46 @@ protected function prepopulateDatabase()
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;
}

Expand Down
9 changes: 9 additions & 0 deletions src/mibew/libs/classes/Mibew/Maintenance/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,15 @@ protected function update20100()
. '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());

Expand Down
34 changes: 34 additions & 0 deletions src/mibew/libs/classes/Mibew/Maintenance/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,40 @@ public static function getUpdates($container)
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
*/
Expand Down
4 changes: 4 additions & 0 deletions src/mibew/libs/classes/Mibew/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ protected function __construct()
// 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
Expand Down

0 comments on commit 25054c4

Please sign in to comment.