|
|
@ -1,5 +1,11 @@ |
|
|
|
<?php |
|
|
|
<?php |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Init version |
|
|
|
|
|
|
|
define( |
|
|
|
|
|
|
|
'_CLITOR_IS_', |
|
|
|
|
|
|
|
'1.3.0' |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
// Init helper |
|
|
|
// Init helper |
|
|
|
function _exec( |
|
|
|
function _exec( |
|
|
|
string $processor, |
|
|
|
string $processor, |
|
|
@ -24,69 +30,81 @@ function _exec( |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Check file exits |
|
|
|
// Check processor exits |
|
|
|
if (!file_exists($argv[2])) |
|
|
|
if (empty($argv[1])) |
|
|
|
{ |
|
|
|
{ |
|
|
|
exit('filename does not exist!' . PHP_EOL); |
|
|
|
print( |
|
|
|
|
|
|
|
'processor required' . PHP_EOL |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exit; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Get file hash sum |
|
|
|
if (!file_exists($argv[1])) |
|
|
|
$md5file = md5_file( |
|
|
|
{ |
|
|
|
$argv[2] |
|
|
|
print( |
|
|
|
|
|
|
|
'processor does not exist' . PHP_EOL |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
// Search namespace by md5file |
|
|
|
exit; |
|
|
|
echo 'Get namespaces list...' . PHP_EOL; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$namespaces = _exec( |
|
|
|
// Check namespace provided |
|
|
|
$argv[1], |
|
|
|
if (empty($argv[2])) |
|
|
|
'keva_list_namespaces' |
|
|
|
{ |
|
|
|
|
|
|
|
print( |
|
|
|
|
|
|
|
'namespace required' . PHP_EOL |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
// Get _CLITOR_IS_ meta |
|
|
|
exit; |
|
|
|
foreach ((array) $namespaces as $namespace) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Check target file does not exist yet |
|
|
|
|
|
|
|
if (!file_exists($argv[3])) |
|
|
|
{ |
|
|
|
{ |
|
|
|
echo sprintf( |
|
|
|
print( |
|
|
|
'Search for _CLITOR_IS_ match file MD5 %s ...' . PHP_EOL, |
|
|
|
'filename does not exist' . PHP_EOL |
|
|
|
$md5file |
|
|
|
|
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
$meta = _exec( |
|
|
|
exit; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Get namespace meta |
|
|
|
|
|
|
|
$clitoris = _exec( |
|
|
|
$argv[1], |
|
|
|
$argv[1], |
|
|
|
sprintf( |
|
|
|
sprintf( |
|
|
|
"%s '%s' '%s'", |
|
|
|
"keva_get %s _CLITOR_IS_", |
|
|
|
'keva_get', |
|
|
|
$argv[2] |
|
|
|
$namespace->namespaceId, |
|
|
|
|
|
|
|
'_CLITOR_IS_' |
|
|
|
|
|
|
|
) |
|
|
|
) |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
if ($value = @json_decode($meta->value)) |
|
|
|
if (empty($clitoris->value)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if ( |
|
|
|
print( |
|
|
|
isset($value->pieces) && |
|
|
|
'_CLITOR_IS_ not found for this namespace' . PHP_EOL |
|
|
|
isset($value->pieces->total) && |
|
|
|
); |
|
|
|
isset($value->pieces->size) && |
|
|
|
|
|
|
|
|
|
|
|
exit; |
|
|
|
isset($value->file) && |
|
|
|
} |
|
|
|
isset($value->file->md5) && |
|
|
|
|
|
|
|
$value->file->md5 === $md5file |
|
|
|
// Validate protocol |
|
|
|
) |
|
|
|
if (empty($clitoris->value)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
// Meta found |
|
|
|
print( |
|
|
|
echo sprintf( |
|
|
|
'_CLITOR_IS_ not found' . PHP_EOL |
|
|
|
'_CLITOR_IS_ found for this file with namespace %s' . PHP_EOL, |
|
|
|
|
|
|
|
$namespace->namespaceId |
|
|
|
|
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
// Split content to smaller parts using _CLITOR_IS_ size defined before |
|
|
|
exit; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Split content to smaller parts (according to the protocol limits) |
|
|
|
$pieces = str_split( |
|
|
|
$pieces = str_split( |
|
|
|
base64_encode( |
|
|
|
base64_encode( |
|
|
|
file_get_contents( |
|
|
|
file_get_contents( |
|
|
|
$argv[2] |
|
|
|
$argv[3] |
|
|
|
) |
|
|
|
) |
|
|
|
), |
|
|
|
), |
|
|
|
$value->pieces->size |
|
|
|
3072 |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
// Count total pieces |
|
|
|
// Count total pieces |
|
|
@ -94,24 +112,15 @@ foreach ((array) $namespaces as $namespace) |
|
|
|
$pieces |
|
|
|
$pieces |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
// Validate pieces count |
|
|
|
|
|
|
|
if ($value->pieces->total !== $total) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
echo '_CLITOR_IS_ have another pieces quantity' . PHP_EOL; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exit; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Begin pieces saving |
|
|
|
// Begin pieces saving |
|
|
|
foreach ($pieces as $key => $value) |
|
|
|
foreach ($pieces as $key => $value) |
|
|
|
{ |
|
|
|
{ |
|
|
|
// Check piece stored is valid |
|
|
|
// Check piece stored in blockchain is valid |
|
|
|
$piece = _exec( |
|
|
|
$piece = _exec( |
|
|
|
$argv[1], |
|
|
|
$argv[1], |
|
|
|
sprintf( |
|
|
|
sprintf( |
|
|
|
"%s '%s' '%s'", |
|
|
|
"keva_get %s %d", |
|
|
|
'keva_get', |
|
|
|
$argv[2], |
|
|
|
$namespace->namespaceId, |
|
|
|
|
|
|
|
$key |
|
|
|
$key |
|
|
|
) |
|
|
|
) |
|
|
|
); |
|
|
|
); |
|
|
@ -119,48 +128,42 @@ foreach ((array) $namespaces as $namespace) |
|
|
|
// Piece value not found |
|
|
|
// Piece value not found |
|
|
|
if (empty($piece->value)) |
|
|
|
if (empty($piece->value)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
echo sprintf( |
|
|
|
printf( |
|
|
|
'Piece %s/%s value not found, creating...' . PHP_EOL, |
|
|
|
'Piece %s/%s value not found, create new record...' . PHP_EOL, |
|
|
|
$key + 1, |
|
|
|
$key + 1, |
|
|
|
$total |
|
|
|
$total |
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Piece value invalid, begin blockchain record |
|
|
|
// Piece value invalid, begin blockchain record |
|
|
|
else if ($piece->value !== $value) |
|
|
|
else if ($piece->value != $value) |
|
|
|
{ |
|
|
|
{ |
|
|
|
echo sprintf( |
|
|
|
printf( |
|
|
|
'Piece %s/%s value invalid (%s <> %s), rewriting...' . PHP_EOL, |
|
|
|
'Piece %s/%s value invalid, overwrite record...' . PHP_EOL, |
|
|
|
$key + 1, |
|
|
|
$key + 1, |
|
|
|
$total, |
|
|
|
$total, |
|
|
|
md5( |
|
|
|
|
|
|
|
$piece->value |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
md5( |
|
|
|
|
|
|
|
$value |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Piece valid |
|
|
|
// Piece valid |
|
|
|
else |
|
|
|
else |
|
|
|
{ |
|
|
|
{ |
|
|
|
echo sprintf( |
|
|
|
printf( |
|
|
|
'Piece %s/%s - OK' . PHP_EOL, |
|
|
|
'Piece %s/%s - OK' . PHP_EOL, |
|
|
|
$key + 1, |
|
|
|
$key + 1, |
|
|
|
$total |
|
|
|
$total |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
continue; |
|
|
|
continue; // skip next operations for this piece |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Record new piece |
|
|
|
print_r( |
|
|
|
print_r( |
|
|
|
_exec( |
|
|
|
_exec( |
|
|
|
$argv[1], |
|
|
|
$argv[1], |
|
|
|
sprintf( |
|
|
|
sprintf( |
|
|
|
"%s '%s' '%s' '%s'", |
|
|
|
"keva_put %s %d %s", |
|
|
|
'keva_put', |
|
|
|
$argv[2], |
|
|
|
$namespace->namespaceId, |
|
|
|
|
|
|
|
$key, |
|
|
|
$key, |
|
|
|
$value |
|
|
|
$value |
|
|
|
) |
|
|
|
) |
|
|
@ -170,27 +173,20 @@ foreach ((array) $namespaces as $namespace) |
|
|
|
// Apply delays to prevent too-long-mempool-chain reject |
|
|
|
// Apply delays to prevent too-long-mempool-chain reject |
|
|
|
$delay = isset($argv[3]) && $argv[3] > 0 ? (int) $argv[3] : 60; |
|
|
|
$delay = isset($argv[3]) && $argv[3] > 0 ? (int) $argv[3] : 60; |
|
|
|
|
|
|
|
|
|
|
|
echo sprintf( |
|
|
|
printf( |
|
|
|
'Piece %s/%s sent, waiting %s seconds...' . PHP_EOL, |
|
|
|
'Piece %s/%s sent, waiting %s seconds...' . PHP_EOL, |
|
|
|
$key + 1, |
|
|
|
$key + 1, |
|
|
|
$total, |
|
|
|
$total, |
|
|
|
$delay |
|
|
|
$delay |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
sleep($delay); |
|
|
|
sleep( |
|
|
|
} |
|
|
|
$delay |
|
|
|
|
|
|
|
|
|
|
|
// Print result |
|
|
|
|
|
|
|
echo sprintf( |
|
|
|
|
|
|
|
'done! run to extract: php %s/get.php %s %s' . PHP_EOL, |
|
|
|
|
|
|
|
__DIR__, |
|
|
|
|
|
|
|
$argv[1], |
|
|
|
|
|
|
|
$namespace->namespaceId |
|
|
|
|
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
echo 'Could not recover this file!' . PHP_EOL; |
|
|
|
// Done |
|
|
|
|
|
|
|
printf( |
|
|
|
|
|
|
|
'data successfully synced with namespace "%s"' . PHP_EOL, |
|
|
|
|
|
|
|
$argv[2] |
|
|
|
|
|
|
|
); |