|
|
@ -656,133 +656,131 @@ foreach ($db->getHostPageCrawlQueue(CRAWL_PAGE_LIMIT, time() - CRAWL_PAGE_SECOND |
|
|
|
// Begin snaps |
|
|
|
// Begin snaps |
|
|
|
if (SNAP_STORAGE) { |
|
|
|
if (SNAP_STORAGE) { |
|
|
|
|
|
|
|
|
|
|
|
$crc32data = crc32($content); |
|
|
|
$hostPageSnapTimeAdded = time(); |
|
|
|
|
|
|
|
$hostPageSnapPath = chunk_split($queueHostPage->hostPageId, 1, '/'); |
|
|
|
|
|
|
|
|
|
|
|
$snapTime = time(); |
|
|
|
$hostPageSnapFilenameTmp = __DIR__ . '/../storage/tmp/snap/hp/' . $hostPageSnapPath . $hostPageSnapTimeAdded . '.zip'; |
|
|
|
$snapPath = chunk_split($queueHostPage->hostPageId, 1, '/'); |
|
|
|
@mkdir(__DIR__ . '/../storage/tmp/snap/hp/' . $hostPageSnapPath, 0755, true); |
|
|
|
|
|
|
|
|
|
|
|
$snapTmp = __DIR__ . '/../storage/tmp/snap/hp/' . $snapPath . $snapTime . '.zip'; |
|
|
|
// Create new ZIP container |
|
|
|
@mkdir(__DIR__ . '/../storage/tmp/snap/hp/' . $snapPath, 0755, true); |
|
|
|
$zip = new ZipArchive(); |
|
|
|
|
|
|
|
|
|
|
|
// Create not duplicated data snaps only, even newer by time added |
|
|
|
if (true === $zip->open($hostPageSnapFilenameTmp, ZipArchive::CREATE)) { |
|
|
|
if ($hostPageSnap = $db->findHostPageSnap($queueHostPage->hostPageId, $crc32data)) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$hostPageSnapId = $hostPageSnap->hostPageSnapId; |
|
|
|
// Insert compressed snap data into the tmp storage |
|
|
|
|
|
|
|
if (true === $zip->addFromString('DATA', $content) && |
|
|
|
|
|
|
|
true === $zip->addFromString('META', sprintf('TIMESTAMP: %s', $hostPageSnapTimeAdded) . PHP_EOL . |
|
|
|
|
|
|
|
sprintf('MIME: %s', Filter::mime($contentType)) . PHP_EOL . |
|
|
|
|
|
|
|
sprintf('SOURCE: %s', Filter::url(WEBSITE_DOMAIN . '/explore.php?hp=' . $queueHostPage->hostPageId)) . PHP_EOL . |
|
|
|
|
|
|
|
sprintf('TARGET: %s', Filter::url($queueHostPage->hostPageURL)))) { |
|
|
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
// Done |
|
|
|
|
|
|
|
$zip->close(); |
|
|
|
|
|
|
|
|
|
|
|
// Create new ZIP container |
|
|
|
// Temporarily snap file exists |
|
|
|
$zip = new ZipArchive(); |
|
|
|
if (file_exists($hostPageSnapFilenameTmp)) { |
|
|
|
|
|
|
|
|
|
|
|
if (true === $zip->open($snapTmp, ZipArchive::CREATE)) { |
|
|
|
// Register snap in DB |
|
|
|
|
|
|
|
if ($hostPageSnapId = $db->addHostPageSnap($queueHostPage->hostPageId, $hostPageSnapTimeAdded)) { |
|
|
|
|
|
|
|
|
|
|
|
// Insert compressed snap data into the tmp storage |
|
|
|
// Default storage success |
|
|
|
if (true === $zip->addFromString('DATA', $content) && |
|
|
|
$snapFilesExists = false; |
|
|
|
true === $zip->addFromString('META', sprintf('TIMESTAMP: %s', $snapTime) . PHP_EOL . |
|
|
|
|
|
|
|
sprintf('CRC32: %s', $crc32data . PHP_EOL . |
|
|
|
|
|
|
|
sprintf('MIME: %s', Filter::mime($contentType)) . PHP_EOL . |
|
|
|
|
|
|
|
sprintf('SOURCE: %s', Filter::url(WEBSITE_DOMAIN . '/explore.php?hp=' . $queueHostPage->hostPageId)) . PHP_EOL . |
|
|
|
|
|
|
|
sprintf('TARGET: %s', Filter::url($queueHostPage->hostPageURL))))) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Done |
|
|
|
// Copy files to each storage |
|
|
|
$zip->close(); |
|
|
|
foreach (json_decode(SNAP_STORAGE) as $name => $storages) { |
|
|
|
|
|
|
|
|
|
|
|
// Temporarily snap file exists |
|
|
|
foreach ($storages as $i => $storage) { |
|
|
|
if (file_exists($snapTmp)) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Register snap in DB |
|
|
|
// Generate storage id |
|
|
|
if ($hostPageSnapId = $db->addHostPageSnap($queueHostPage->hostPageId, $crc32data, filesize($snapTmp), $snapTime)) { |
|
|
|
$crc32name = crc32(sprintf('%s.%s', $name, $i)); |
|
|
|
|
|
|
|
|
|
|
|
$hostPagesSnapAdded++; |
|
|
|
switch ($name) { |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Copy files to each storage |
|
|
|
case 'localhost': |
|
|
|
foreach (json_decode(SNAP_STORAGE) as $name => $storages) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach ($storages as $i => $storage) { |
|
|
|
// Validate mime |
|
|
|
|
|
|
|
if (!$storage->quota->mime) continue 2; |
|
|
|
|
|
|
|
|
|
|
|
// Generate storage id |
|
|
|
$snapMimeValid = false; |
|
|
|
$crc32name = crc32(sprintf('%s.%s', $name, $i)); |
|
|
|
foreach ((array) explode(',', $storage->quota->mime) as $mime) { |
|
|
|
|
|
|
|
|
|
|
|
switch ($name) { |
|
|
|
if (false !== stripos(Filter::mime($contentType), Filter::mime($mime))) { |
|
|
|
|
|
|
|
|
|
|
|
case 'localhost': |
|
|
|
$snapMimeValid = true; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Validate size quota |
|
|
|
if (!$snapMimeValid) continue 2; |
|
|
|
if ($db->getTotalHostPageSnapSizeByStorage($hostPageSnapId, $crc32name) >= $storage->quota->size) continue 2; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Validate mime |
|
|
|
// Copy tmp snap file to the permanent storage |
|
|
|
if (!$storage->quota->mime) continue 2; |
|
|
|
@mkdir($storage->directory . $hostPageSnapPath, 0755, true); |
|
|
|
|
|
|
|
|
|
|
|
$snapMimeValid = false; |
|
|
|
if (copy($hostPageSnapFilenameTmp, $storage->directory . $hostPageSnapPath . $hostPageSnapTimeAdded . '.zip')) { |
|
|
|
foreach ((array) explode(',', $storage->quota->mime) as $mime) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (false !== stripos(Filter::mime($contentType), Filter::mime($mime))) { |
|
|
|
// Register storage name |
|
|
|
|
|
|
|
$db->addHostPageSnapStorage($hostPageSnapId, $crc32name, time()); |
|
|
|
|
|
|
|
|
|
|
|
$snapMimeValid = true; |
|
|
|
$snapFilesExists = true; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!$snapMimeValid) continue 2; |
|
|
|
break; |
|
|
|
|
|
|
|
case 'ftp': |
|
|
|
|
|
|
|
|
|
|
|
// Copy tmp snap file to the permanent storage |
|
|
|
// Validate mime |
|
|
|
@mkdir($storage->directory . $snapPath, 0755, true); |
|
|
|
if (!$storage->quota->mime) continue 2; |
|
|
|
|
|
|
|
|
|
|
|
if (copy($snapTmp, $storage->directory . $snapPath . $snapTime . '.zip')) { |
|
|
|
$snapMimeValid = false; |
|
|
|
|
|
|
|
foreach ((array) explode(',', $storage->quota->mime) as $mime) { |
|
|
|
|
|
|
|
|
|
|
|
// Register storage name |
|
|
|
if (false !== stripos(Filter::mime($contentType), Filter::mime($mime))) { |
|
|
|
$db->addHostPageSnapStorage($hostPageSnapId, $crc32name, time()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
break; |
|
|
|
$snapMimeValid = true; |
|
|
|
case 'ftp': |
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Validate size quota |
|
|
|
if (!$snapMimeValid) continue 2; |
|
|
|
if ($db->getTotalHostPageSnapSizeByStorage($hostPageSnapId, $crc32name) >= $storage->quota->size) continue 2; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Validate mime |
|
|
|
// Copy tmp snap file to the permanent storage |
|
|
|
if (!$storage->quota->mime) continue 2; |
|
|
|
$ftp = new Ftp(); |
|
|
|
|
|
|
|
|
|
|
|
$snapMimeValid = false; |
|
|
|
if ($ftp->connect($storage->host, $storage->port, $storage->username, $storage->password, $storage->directory, $storage->timeout, $storage->passive)) { |
|
|
|
foreach ((array) explode(',', $storage->quota->mime) as $mime) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (false !== stripos(Filter::mime($contentType), Filter::mime($mime))) { |
|
|
|
$ftp->mkdir('hp/' . $hostPageSnapPath, true); |
|
|
|
|
|
|
|
|
|
|
|
$snapMimeValid = true; |
|
|
|
if ($ftp->copy($hostPageSnapFilenameTmp, 'hp/' . $hostPageSnapPath . $hostPageSnapTimeAdded . '.zip')) { |
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!$snapMimeValid) continue 2; |
|
|
|
// Register storage name |
|
|
|
|
|
|
|
$db->addHostPageSnapStorage($hostPageSnapId, $crc32name, time()); |
|
|
|
|
|
|
|
|
|
|
|
// Copy tmp snap file to the permanent storage |
|
|
|
$snapFilesExists = true; |
|
|
|
$ftp = new Ftp(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ($ftp->connect($storage->host, $storage->port, $storage->username, $storage->password, $storage->directory, $storage->timeout, $storage->passive)) { |
|
|
|
$ftp->close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$ftp->mkdir('hp/' . $snapPath, true); |
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ($ftp->copy($snapTmp, 'hp/' . $snapPath . $snapTime . '.zip')) { |
|
|
|
// At least one file have been stored |
|
|
|
|
|
|
|
if ($snapFilesExists) { |
|
|
|
|
|
|
|
|
|
|
|
// Register storage name |
|
|
|
$hostPagesSnapAdded++; |
|
|
|
$db->addHostPageSnapStorage($hostPageSnapId, $crc32name, time()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$ftp->close(); |
|
|
|
} else { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
break; |
|
|
|
$db->deleteHostPageSnap($hostPageSnapId); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Delete tmp snap |
|
|
|
// Delete tmp snap |
|
|
|
unlink($snapTmp); |
|
|
|
unlink($hostPageSnapFilenameTmp); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Skip page links following with meta robots:nofollow attribute |
|
|
|
// Skip page links following with meta robots:nofollow attribute |
|
|
|