2014-05-02 04:02:20 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace ManiaControl\Files;
|
2014-05-02 17:40:47 +02:00
|
|
|
|
2014-08-03 13:29:54 +02:00
|
|
|
use ManiaControl\Logger;
|
2014-05-02 15:20:25 +02:00
|
|
|
use ManiaControl\ManiaControl;
|
2014-05-02 04:02:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Backup Utility Class
|
2014-05-02 17:40:47 +02:00
|
|
|
*
|
|
|
|
* @author ManiaControl Team <mail@maniacontrol.com>
|
2020-01-22 10:39:35 +01:00
|
|
|
* @copyright 2014-2020 ManiaControl Team
|
2014-05-02 17:40:47 +02:00
|
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
2014-05-02 04:02:20 +02:00
|
|
|
*/
|
|
|
|
abstract class BackupUtil {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform a Full Backup of ManiaControl
|
2014-05-02 17:40:47 +02:00
|
|
|
*
|
2014-05-02 04:02:20 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function performFullBackup() {
|
2014-05-07 14:36:17 +02:00
|
|
|
$backupFolder = self::getBackupFolder();
|
|
|
|
if (!$backupFolder) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-04-15 23:35:17 +02:00
|
|
|
|
|
|
|
$time = date('y-m-d_H-i-s');
|
2017-04-16 16:41:30 +02:00
|
|
|
if (defined('PHP_UNIT_TEST')) {
|
2017-04-15 23:35:17 +02:00
|
|
|
$time = date('y-m-d_H-i');
|
|
|
|
}
|
|
|
|
|
|
|
|
$backupFileName = $backupFolder . 'backup_' . ManiaControl::VERSION . '_' . $time . '.zip';
|
2014-05-02 17:40:47 +02:00
|
|
|
$backupZip = new \ZipArchive();
|
|
|
|
if ($backupZip->open($backupFileName, \ZipArchive::CREATE) !== true) {
|
2014-08-03 13:29:54 +02:00
|
|
|
Logger::logError("Couldn't create backup zip!");
|
2014-05-02 04:02:20 +02:00
|
|
|
return false;
|
|
|
|
}
|
2014-05-07 17:09:29 +02:00
|
|
|
$excludes = array();
|
|
|
|
$baseFileNames = array('configs', 'core', 'plugins', 'ManiaControl.php');
|
2014-08-08 14:41:58 +02:00
|
|
|
$pathInfo = pathInfo(MANIACONTROL_PATH);
|
2014-05-07 17:09:29 +02:00
|
|
|
$parentPath = $pathInfo['dirname'] . DIRECTORY_SEPARATOR;
|
|
|
|
$dirName = $pathInfo['basename'];
|
2014-05-02 04:02:20 +02:00
|
|
|
$backupZip->addEmptyDir($dirName);
|
2014-08-08 14:41:58 +02:00
|
|
|
self::zipDirectory($backupZip, MANIACONTROL_PATH, strlen($parentPath), $excludes, $baseFileNames);
|
2014-05-07 17:09:29 +02:00
|
|
|
return $backupZip->close();
|
2014-05-02 04:02:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Backup Folder Path and create it if necessary
|
2014-05-02 17:40:47 +02:00
|
|
|
*
|
2014-06-20 15:17:52 +02:00
|
|
|
* @return string|bool
|
2014-05-02 04:02:20 +02:00
|
|
|
*/
|
|
|
|
private static function getBackupFolder() {
|
2014-08-08 14:41:58 +02:00
|
|
|
$backupFolder = MANIACONTROL_PATH . 'backup' . DIRECTORY_SEPARATOR;
|
2014-05-07 14:36:17 +02:00
|
|
|
if (!is_dir($backupFolder) && !mkdir($backupFolder)) {
|
2014-08-03 13:29:54 +02:00
|
|
|
Logger::logError("Couldn't create backup folder!");
|
2014-05-07 14:36:17 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!is_writeable($backupFolder)) {
|
2014-08-03 13:29:54 +02:00
|
|
|
Logger::logError("ManiaControl doesn't have the necessary write rights for the backup folder!");
|
2014-05-07 14:36:17 +02:00
|
|
|
return false;
|
2014-05-02 04:02:20 +02:00
|
|
|
}
|
|
|
|
return $backupFolder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-07 17:09:29 +02:00
|
|
|
* Add a Directory to the ZipArchive
|
2014-05-02 17:40:47 +02:00
|
|
|
*
|
2014-05-02 04:02:20 +02:00
|
|
|
* @param \ZipArchive $zipArchive
|
2014-05-02 17:40:47 +02:00
|
|
|
* @param string $folderName
|
|
|
|
* @param int $prefixLength
|
|
|
|
* @param array $excludes
|
2014-05-07 17:09:29 +02:00
|
|
|
* @param array $baseFileNames
|
2014-05-02 04:02:20 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2014-08-25 15:33:22 +02:00
|
|
|
private static function zipDirectory(\ZipArchive &$zipArchive, $folderName, $prefixLength, array $excludes = array(), array $baseFileNames = array()) {
|
2014-05-02 04:02:20 +02:00
|
|
|
$folderHandle = opendir($folderName);
|
2014-05-07 14:36:17 +02:00
|
|
|
if (!is_resource($folderHandle)) {
|
2014-08-03 13:29:54 +02:00
|
|
|
Logger::logError("Couldn't open folder '{$folderName}' for backup!");
|
2014-05-02 04:02:20 +02:00
|
|
|
return false;
|
|
|
|
}
|
2014-06-14 15:48:27 +02:00
|
|
|
$useBaseFileNames = !empty($baseFileNames);
|
2014-05-02 04:02:20 +02:00
|
|
|
while (false !== ($file = readdir($folderHandle))) {
|
2017-04-16 16:41:30 +02:00
|
|
|
if (FileUtil::isHiddenFile($file)) {
|
2014-05-07 17:09:29 +02:00
|
|
|
// Skip such .files
|
2014-05-07 14:36:17 +02:00
|
|
|
continue;
|
|
|
|
}
|
2014-05-02 04:02:20 +02:00
|
|
|
if (in_array($file, $excludes)) {
|
2014-05-07 17:09:29 +02:00
|
|
|
// Excluded
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($useBaseFileNames && !in_array($file, $baseFileNames)) {
|
|
|
|
// Not one of the base files
|
2014-05-02 04:02:20 +02:00
|
|
|
continue;
|
|
|
|
}
|
2014-05-03 21:37:28 +02:00
|
|
|
$filePath = $folderName . DIRECTORY_SEPARATOR . $file;
|
2014-05-02 04:02:20 +02:00
|
|
|
$localPath = substr($filePath, $prefixLength);
|
|
|
|
if (is_file($filePath)) {
|
|
|
|
$zipArchive->addFile($filePath, $localPath);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (is_dir($filePath)) {
|
|
|
|
$zipArchive->addEmptyDir($localPath);
|
2014-05-02 15:12:28 +02:00
|
|
|
self::zipDirectory($zipArchive, $filePath, $prefixLength, $excludes);
|
2014-05-02 04:02:20 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir($folderHandle);
|
|
|
|
return true;
|
|
|
|
}
|
2014-05-02 17:40:47 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform a Backup of the Plugins
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function performPluginsBackup() {
|
2014-05-07 14:36:17 +02:00
|
|
|
$backupFolder = self::getBackupFolder();
|
|
|
|
if (!$backupFolder) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$backupFileName = $backupFolder . 'backup_plugins_' . ManiaControl::VERSION . date('y-m-d_H-i') . '_' . time() . '.zip';
|
2014-05-02 17:40:47 +02:00
|
|
|
$backupZip = new \ZipArchive();
|
|
|
|
if ($backupZip->open($backupFileName, \ZipArchive::CREATE) !== true) {
|
2014-08-03 13:29:54 +02:00
|
|
|
Logger::logError("Couldn't create backup zip!");
|
2014-05-02 17:40:47 +02:00
|
|
|
return false;
|
|
|
|
}
|
2014-08-08 14:41:58 +02:00
|
|
|
$directory = MANIACONTROL_PATH . 'plugins';
|
2014-05-07 17:09:29 +02:00
|
|
|
$pathInfo = pathInfo($directory);
|
2014-05-03 21:37:28 +02:00
|
|
|
$parentPath = $pathInfo['dirname'] . DIRECTORY_SEPARATOR;
|
2014-05-02 17:40:47 +02:00
|
|
|
$dirName = $pathInfo['basename'];
|
|
|
|
$backupZip->addEmptyDir($dirName);
|
2014-05-07 17:09:29 +02:00
|
|
|
self::zipDirectory($backupZip, $directory, strlen($parentPath));
|
|
|
|
return $backupZip->close();
|
2014-05-02 17:40:47 +02:00
|
|
|
}
|
2014-05-02 04:02:20 +02:00
|
|
|
}
|