2017-01-27 19:41:02 +00:00
|
|
|
<?php
|
2018-01-26 13:05:47 +00:00
|
|
|
|
2017-01-30 09:39:45 +00:00
|
|
|
require_once __DIR__.'/../../include/php/const';
|
2017-01-27 19:41:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [1] Function that generates a random secret
|
|
|
|
=========================================================*/
|
|
|
|
function generate_secret(){
|
|
|
|
|
|
|
|
/* (1) Generate random set */
|
|
|
|
$charlist = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_';
|
|
|
|
|
|
|
|
/* (2) Set useful variables */
|
|
|
|
$clen = strlen($charlist);
|
|
|
|
$secret = '';
|
|
|
|
|
|
|
|
/* (3) Generate random characters one by one */
|
|
|
|
for( $i = 0 ; $i < SECRET_SIZE ; $i++ )
|
|
|
|
$secret .= $charlist[rand(0, $clen - 1)];
|
|
|
|
|
|
|
|
/* (4) Return the secret */
|
|
|
|
return $secret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-01-26 13:05:47 +00:00
|
|
|
function cyclichash_decr($decr_factor=-1){
|
2017-01-27 19:41:02 +00:00
|
|
|
|
|
|
|
/* [2] Fetch necessary data
|
|
|
|
=========================================================*/
|
|
|
|
|
|
|
|
/* (1) Fetch secret file */
|
|
|
|
$secret = @file_get_contents(SECRET_CONF);
|
2018-01-26 13:05:47 +00:00
|
|
|
|
2017-01-27 19:41:02 +00:00
|
|
|
/* (2) Check secret file format */
|
2017-01-28 17:58:49 +00:00
|
|
|
if( !is_string($secret) || !preg_match("/^(.{".SECRET_SIZE."}):(\d+):(.{".SECRET_SIZE."})$/", $secret, $match) ){
|
2017-01-27 19:41:02 +00:00
|
|
|
|
2017-01-28 17:58:49 +00:00
|
|
|
// Generate full secret
|
|
|
|
$secret = generate_secret().':999:'.generate_secret();
|
2017-01-27 19:41:02 +00:00
|
|
|
|
|
|
|
// Try to override the secret file
|
|
|
|
if( @file_put_contents(SECRET_CONF, $secret) ){
|
2017-01-30 09:59:52 +00:00
|
|
|
slog("Random secret generated successfully", 'cyclic-hash:decr', 'update');
|
2017-01-27 19:41:02 +00:00
|
|
|
return 0;
|
|
|
|
}else{
|
2017-01-30 09:59:52 +00:00
|
|
|
slog("Error while generating new random secret", 'cyclic-hash:decr', 'update');
|
2017-01-27 19:41:02 +00:00
|
|
|
return 127;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (3) Extract data */
|
|
|
|
$key = (string) $match[1];
|
|
|
|
$depth = (int) $match[2];
|
2017-01-28 17:58:49 +00:00
|
|
|
$next = (string) $match[3];
|
2017-01-27 19:41:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [3] If can decrement, decrement
|
|
|
|
=========================================================*/
|
|
|
|
if( $depth > 1 ){
|
|
|
|
|
|
|
|
/* (1) Decrement the depth */
|
2018-01-26 13:05:47 +00:00
|
|
|
$depth += $decr_factor;
|
2017-01-27 19:41:02 +00:00
|
|
|
|
|
|
|
/* (2) Try to override the secret file */
|
2017-01-28 17:58:49 +00:00
|
|
|
if( @file_put_contents(SECRET_CONF, "$key:$depth:$next") ){
|
2017-01-30 09:59:52 +00:00
|
|
|
slog("Secret depth decremented to $depth", 'cyclic-hash:decr', 'update');
|
2017-01-27 19:41:02 +00:00
|
|
|
return 0;
|
|
|
|
}else{
|
2017-01-30 09:59:52 +00:00
|
|
|
slog("Error while decrementing secret depth", 'cyclic-hash:decr', 'update');
|
2017-01-27 19:41:02 +00:00
|
|
|
return 127;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-28 17:58:49 +00:00
|
|
|
/* [4] If cannot decrement, use new secret and generate next
|
2017-01-27 19:41:02 +00:00
|
|
|
=========================================================*/
|
|
|
|
}else{
|
2018-01-26 13:05:47 +00:00
|
|
|
|
2017-01-27 19:41:02 +00:00
|
|
|
// Generate new secret
|
2017-01-28 17:58:49 +00:00
|
|
|
$secret = $next.':999:'.generate_secret();
|
2017-01-27 19:41:02 +00:00
|
|
|
|
|
|
|
// Try to override the secret file
|
|
|
|
if( @file_put_contents(SECRET_CONF, $secret) ){
|
2017-01-30 09:59:52 +00:00
|
|
|
slog("Random secret generated successfully", 'cyclic-hash:decr', 'update');
|
2017-01-27 19:41:02 +00:00
|
|
|
return 0;
|
|
|
|
}else{
|
2017-01-30 09:59:52 +00:00
|
|
|
slog("Error while generating new random secret", 'cyclic-hash:decr', 'update');
|
2017-01-27 19:41:02 +00:00
|
|
|
return 127;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-01-26 13:05:47 +00:00
|
|
|
// IF received 'revert' as $1 -> increment ELSE decrement (default)
|
|
|
|
$decr_factor = ( $argc < 2 || $argv[1] != 'revert' ) ? -1 : +1;
|
|
|
|
|
|
|
|
echo cyclichash_decr($decr_factor);
|
2017-01-27 19:41:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
?>
|