$cFile, 'u+rw') ||
!is_readable($cFile)
) {
continue;
}
$filesToCheck[] = $cFile;
}
$installerTplCheck = '/const\s+ARCHIVE_FILENAME\s*=\s*[\'"](.+?)[\'"]\s*;.*const\s+PACKAGE_HASH\s*=\s*[\'"](.+?)[\'"]\s*;/s';
foreach ($filesToCheck as $file) {
$fileName = basename($file);
if ($fileTimeDelay > 0 && (time() - filemtime($file)) < $fileTimeDelay) {
continue;
}
if (($content = @file_get_contents($file, false, null)) === false) {
continue;
}
$matches = null;
if (preg_match($installerTplCheck, $content, $matches) !== 1) {
continue;
}
$archiveName = $matches[1];
$hash = $matches[2];
$matches = null;
if (preg_match(DUPLICATOR_PRO_ARCHIVE_REGEX_PATTERN, $archiveName, $matches) !== 1) {
if (SnapIO::unlink($file)) {
self::$migrationCleanupReport['instFile'][] = "
"
. " "
. sprintf(__('Installer file %s removed for security reasons', 'duplicator-pro'), esc_html($fileName))
. "
";
} else {
self::$migrationCleanupReport['instFile'][] = ""
. ' '
. sprintf(__('Can\'t remove installer file %s, please remove it for security reasons', 'duplicator-pro'), esc_html($fileName))
. '
';
}
continue;
}
$archiveHash = $matches[1];
if (strpos($file, $archiveHash) === false) {
if (SnapIO::rename($file, dirname($file) . '/' . $archiveHash . '_installer.php', true)) {
self::$migrationCleanupReport['instFile'][] = ""
. " "
. sprintf(__('Installer file %s renamed with HASH', 'duplicator-pro'), esc_html($fileName))
. "
";
} else {
self::$migrationCleanupReport['instFile'][] = ""
. ' '
. sprintf(
__('Can\'t rename installer file %s with HASH, please remove it for security reasons', 'duplicator-pro'),
esc_html($fileName)
)
. '
';
}
}
}
}
/**
* Store migratuion files
*
* @param MigrateData $migrationData Migration data
*
* @return void
*/
public static function storeMigrationFiles(MigrateData $migrationData)
{
wp_mkdir_p(DUPLICATOR_PRO_SSDIR_PATH_INSTALLER);
SnapIO::emptyDir(DUPLICATOR_PRO_SSDIR_PATH_INSTALLER);
SnapIO::createSilenceIndex(DUPLICATOR_PRO_SSDIR_PATH_INSTALLER);
$filesToMove = array(
$migrationData->installerLog,
$migrationData->installerBootLog,
$migrationData->origFileFolderPath,
);
foreach ($filesToMove as $path) {
if (file_exists($path)) {
if (SnapIO::rcopy($path, DUPLICATOR_PRO_SSDIR_PATH_INSTALLER . '/' . basename($path))) {
self::$migrationCleanupReport['stored'][] = ""
. " "
. __('Original files folder moved in installer backup directory', 'duplicator-pro') . " - " . esc_html($path) .
"
";
} else {
self::$migrationCleanupReport['stored'][] = ""
. ' '
. sprintf(__('Can\'t move %1$s to %2$s', 'duplicator-pro'), esc_html($path), DUPLICATOR_PRO_SSDIR_PATH_INSTALLER)
. '
';
}
}
}
}
/**
* Get file list to store
*
* @return array
*/
public static function getStoredMigrationLists()
{
if (($migrationData = self::getMigrationData()) == false) {
$filesToCheck = array();
} else {
$filesToCheck = array(
$migrationData->installerLog => __('Installer log', 'duplicator-pro'),
$migrationData->installerBootLog => __('Installer boot log', 'duplicator-pro'),
$migrationData->origFileFolderPath => __('Original files folder', 'duplicator-pro'),
);
}
$result = array();
foreach ($filesToCheck as $path => $label) {
$storedPath = DUPLICATOR_PRO_SSDIR_PATH_INSTALLER . '/' . basename($path);
if (!file_exists($storedPath)) {
continue;
}
$result[$storedPath] = $label;
}
return $result;
}
/**
* Check if exist file to remove
*
* @return bool
*/
public static function haveFileToClean()
{
return count(self::checkInstallerFilesList()) > 0;
}
/**
* Gets a list of all the installer files and directory by name and full path
*
* @remarks
* FILES: installer.php, installer-backup.php, dup-installer-bootlog__[HASH].txt
* DIRS: dup-installer
* Last set is for lazy developer cleanup files that a developer may have
* accidentally left around lets be proactive for the user just in case.
*
* @return string[] file names
*/
public static function getGenericInstallerFiles()
{
return array(
'installer.php',
'[HASH]installer-backup.php',
'dup-installer',
'dup-installer[HASH]',
'dup-installer-bootlog__[HASH].txt',
'[HASH]_archive.zip|daf',
);
}
/**
* Get installer files list
*
* @return string[]
*/
public static function checkInstallerFilesList()
{
$migrationData = self::getMigrationData();
$foldersToChkeck = array(
SnapIO::safePathTrailingslashit(ABSPATH),
SnapWP::getHomePath(),
);
$result = array();
if (!empty($migrationData)) {
if (
file_exists($migrationData->archivePath) &&
!DUP_PRO_Archive::isBackupPathChild($migrationData->archivePath) &&
!PackageImporter::isImportPath($migrationData->archivePath) &&
!RecoveryPackage::isRecoverPath($migrationData->archivePath) &&
!StoragesUtil::isLocalStorageChildPath($migrationData->archivePath)
) {
$result[] = $migrationData->archivePath;
}
if (
file_exists($migrationData->installerPath) &&
self::isInstallerFile($migrationData->installerPath) &&
!DUP_PRO_Archive::isBackupPathChild($migrationData->installerPath) &&
!RecoveryPackage::isRecoverPath($migrationData->installerPath) &&
!StoragesUtil::isLocalStorageChildPath($migrationData->installerPath)
) {
$result[] = $migrationData->installerPath;
}
if (file_exists($migrationData->installerBootLog)) {
$result[] = $migrationData->installerBootLog;
}
if (file_exists($migrationData->dupInstallerPath)) {
$result[] = $migrationData->dupInstallerPath;
}
}
foreach ($foldersToChkeck as $folder) {
$result = array_merge($result, SnapIO::regexGlob($folder, array(
'regexFile' => array(
DUPLICATOR_PRO_ARCHIVE_REGEX_PATTERN,
DUPLICATOR_PRO_INSTALLER_REGEX_PATTERN,
DUPLICATOR_PRO_DUP_INSTALLER_BOOTLOG_REGEX_PATTERN,
DUPLICATOR_PRO_DUP_INSTALLER_OWRPARAM_REGEX_PATTERN,
),
'regexFolder' => array(DUPLICATOR_PRO_DUP_INSTALLER_FOLDER_REGEX_PATTERN),
)));
}
$result = array_map(array('\\Duplicator\\Libs\\Snap\\SnapIO', 'safePathUntrailingslashit'), $result);
return array_unique($result);
}
/**
* Check if file is installer file
*
* @param string $path path to check
*
* @return bool true if the file at current path is the installer file
*/
public static function isInstallerFile($path)
{
if (!is_file($path) || !is_array($last5Lines = SnapIO::getLastLinesOfFile($path, 5)) || empty($last5Lines)) {
return false;
}
return strpos(implode("", $last5Lines), "DUPLICATOR_PRO_INSTALLER_EOF") !== false;
}
/**
* Clean installer files
*
* @param bool $deleteCleanInstallReportOption Delete clean install report option
* @param int $fileTimeDelay File time delay
*
* @return array Clean result
*/
public static function cleanMigrationFiles($deleteCleanInstallReportOption = true, $fileTimeDelay = 0)
{
$cleanList = self::checkInstallerFilesList();
$result = array();
foreach ($cleanList as $path) {
$success = false;
try {
if ($fileTimeDelay <= 0 || time() - filectime($path) > $fileTimeDelay) {
$success = (SnapIO::rrmdir($path) !== false);
} else {
// The file does not even need to be removed yet
$success = true;
}
} catch (Exception $ex) {
$success = false;
} catch (Error $ex) {
$success = false;
}
$result[$path] = $success;
}
if ($deleteCleanInstallReportOption) {
delete_option(self::CLEAN_INSTALL_REPORT_OPTION);
}
return $result;
}
/**
* Recalculate active schedules next run time
*
* @return void
*/
public static function updateNextSchedules()
{
$schedules = DUP_PRO_Schedule_Entity::get_active();
foreach ($schedules as $schedule) {
$schedule->updateNextRuntime();
}
}
}