torrentpier/library/includes/datastore/build_files_integrity.php
Roman Kelesidis 4dcee993cc
Minor improvements (#1676)
* Minor improvements

* Update functions.php

* Update common.php

* Update init_bb.php

* Updated

* Update build_files_integrity.php

* Updated

* Update CHANGELOG.md

* Update legacy-changes.txt

* Update legacy-changes.txt

* Update legacy-changes.txt

* Update .gitignore

* Update legacy-changes.txt
2024-11-11 01:17:35 +07:00

98 lines
2.7 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* TorrentPier Bull-powered BitTorrent tracker engine
*
* @copyright Copyright (c) 2005-2024 TorrentPier (https://torrentpier.com)
* @link https://github.com/torrentpier/torrentpier for the canonical source repository
* @license https://github.com/torrentpier/torrentpier/blob/master/LICENSE MIT License
*/
if (!defined('BB_ROOT')) {
die(basename(__FILE__));
}
global $bb_cfg;
// TODO: if (!local environment) { ... }
if (!$bb_cfg['integrity_check']) {
return;
}
$filesList = [];
$wrongFilesList = [];
$checksumFile = new SplFileObject(CHECKSUMS_FILE, 'r');
$checksumFile->setFlags(SplFileObject::SKIP_EMPTY | SplFileObject::DROP_NEW_LINE);
$ignoreFiles = [
'.env.example',
'.htaccess',
'robots.txt',
'opensearch_desc.xml',
'opensearch_desc_bt.xml',
'favicon.png',
'composer.json',
'composer.lock',
hide_bb_path(CHECKSUMS_FILE),
hide_bb_path(BB_ENABLED),
'library/config.php',
'library/defines.php',
'styles/images/logo/logo.png'
];
foreach ($checksumFile as $line) {
$parts = explode(' ', $line);
if (!isset($parts[0]) || !isset($parts[1])) {
// Skip end line
break;
}
if (!empty($ignoreFiles) && in_array($parts[1], $ignoreFiles)) {
// Skip files from "Ignoring list"
continue;
}
$filesList[] = [
'path' => trim($parts[1]),
'hash' => trim($parts[0])
];
}
foreach ($filesList as $file) {
if (!file_exists(BB_ROOT . $file['path']) || (strtolower(md5_file(BB_ROOT . $file['path'])) !== strtolower($file['hash']))) {
$wrongFilesList[] = $file['path'];
}
}
// Restore corrupt files
if (is_file(RESTORE_CORRUPT_CONFIRM_FILE)) {
$buildDownloader = new \TorrentPier\Updater();
if ($buildDownloader->download(INT_DATA_DIR . '/', $bb_cfg['tp_version'])) {
// Unzip downloaded build file
$zipArchive = new ZipArchive;
$extractDownloadedFile = $zipArchive->open($buildDownloader->savePath);
if ($extractDownloadedFile === true) {
if ($zipArchive->extractTo(BB_ROOT, $wrongFilesList)) {
$wrongFilesList = [];
}
$zipArchive->close();
}
}
// Delete restore confirm file & build file
if (isset($buildDownloader->savePath)) {
unlink($buildDownloader->savePath);
}
if (is_file(RESTORE_CORRUPT_CONFIRM_FILE)) {
unlink(RESTORE_CORRUPT_CONFIRM_FILE);
}
}
$data = [
'success' => empty($wrongFilesList),
'wrong_files' => $wrongFilesList,
'wrong_files_num' => count($wrongFilesList),
'total_num' => count($filesList),
'timestamp' => TIMENOW,
];
$this->store('files_integrity', $data);