TrackManiaControl/core/Files/BackupUtil.php

132 lines
3.9 KiB
PHP
Raw Normal View History

2014-05-02 04:02:20 +02:00
<?php
namespace ManiaControl\Files;
2014-05-02 17:40:47 +02:00
use ManiaControl\Logger;
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>
2015-01-19 11:06:20 +01:00
* @copyright 2014-2015 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;
}
$backupFileName = $backupFolder . 'backup_' . 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) {
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');
$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);
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
*
* @return string|bool
2014-05-02 04:02:20 +02:00
*/
private static function getBackupFolder() {
$backupFolder = MANIACONTROL_PATH . 'backup' . DIRECTORY_SEPARATOR;
2014-05-07 14:36:17 +02:00
if (!is_dir($backupFolder) && !mkdir($backupFolder)) {
Logger::logError("Couldn't create backup folder!");
2014-05-07 14:36:17 +02:00
return false;
}
if (!is_writeable($backupFolder)) {
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)) {
Logger::logError("Couldn't open folder '{$folderName}' for backup!");
2014-05-02 04:02:20 +02:00
return false;
}
$useBaseFileNames = !empty($baseFileNames);
2014-05-02 04:02:20 +02:00
while (false !== ($file = readdir($folderHandle))) {
2014-05-07 14:36:17 +02:00
if (substr($file, 0, 1) === '.') {
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) {
Logger::logError("Couldn't create backup zip!");
2014-05-02 17:40:47 +02:00
return false;
}
$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
}