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