2017-11-16 23:21:05 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class pluginBackup extends Plugin {
|
|
|
|
|
2018-07-01 14:17:24 +02:00
|
|
|
// Directories to backup
|
2017-11-16 23:21:05 +01:00
|
|
|
private $directoriesToBackup = array(
|
|
|
|
PATH_PAGES,
|
|
|
|
PATH_DATABASES,
|
|
|
|
PATH_UPLOADS
|
|
|
|
);
|
|
|
|
|
|
|
|
// This variable define if the extension zip is loaded
|
|
|
|
private $zip = false;
|
|
|
|
|
|
|
|
public function init()
|
|
|
|
{
|
|
|
|
// Disable default form buttons (Save and Cancel)
|
|
|
|
$this->formButtons = false;
|
|
|
|
|
|
|
|
// Check for zip extension installed
|
|
|
|
$this->zip = extension_loaded('zip');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function post()
|
|
|
|
{
|
|
|
|
if (isset($_POST['createBackup'])) {
|
|
|
|
return $this->createBackup();
|
2018-02-26 17:44:39 +01:00
|
|
|
} elseif (isset($_POST['restoreBackup'])) {
|
2017-11-16 23:21:05 +01:00
|
|
|
return $this->restoreBackup($_POST['restoreBackup']);
|
2018-02-26 17:44:39 +01:00
|
|
|
} elseif (isset($_POST['deleteBackup'])) {
|
|
|
|
return $this->deleteBackup($_POST['deleteBackup']);
|
2017-11-16 23:21:05 +01:00
|
|
|
}
|
|
|
|
|
2019-01-28 22:12:33 +01:00
|
|
|
return true;
|
2017-11-16 23:21:05 +01:00
|
|
|
}
|
|
|
|
|
2018-07-02 00:24:53 +02:00
|
|
|
public function adminSidebar()
|
|
|
|
{
|
|
|
|
$backups = $this->backupList();
|
2018-08-24 00:17:31 +02:00
|
|
|
return '<a class="nav-link" href="'.HTML_PATH_ADMIN_ROOT.'configure-plugin/'.$this->className().'">Backups <span class="badge badge-primary badge-pill">'.count($backups).'</span></a>';
|
2018-07-02 00:24:53 +02:00
|
|
|
}
|
|
|
|
|
2017-11-16 23:21:05 +01:00
|
|
|
public function form()
|
|
|
|
{
|
2018-08-05 17:54:20 +02:00
|
|
|
global $L;
|
2017-11-16 23:21:05 +01:00
|
|
|
|
2018-07-02 00:24:53 +02:00
|
|
|
$backups = $this->backupList();
|
2017-11-16 23:21:05 +01:00
|
|
|
|
2018-07-01 14:17:24 +02:00
|
|
|
$html = '';
|
|
|
|
if (empty($backups)) {
|
|
|
|
$html .= '<div class="alert alert-primary" role="alert">';
|
2018-08-05 17:54:20 +02:00
|
|
|
$html .= $L->get('There are no backups for the moment');
|
2018-07-01 14:17:24 +02:00
|
|
|
$html .= '</div>';
|
|
|
|
}
|
|
|
|
|
|
|
|
$html .= '<div>';
|
2019-05-18 11:54:39 +02:00
|
|
|
$html .= '<button name="createBackup" value="true" class="btn btn-primary btn-sm" type="submit"><span class="fa fa-play-circle"></span> '.$L->get('create-backup').'</button>';
|
2017-11-16 23:21:05 +01:00
|
|
|
$html .= '</div>';
|
|
|
|
$html .= '<hr>';
|
|
|
|
|
|
|
|
foreach ($backups as $backup) {
|
|
|
|
$filename = pathinfo($backup,PATHINFO_FILENAME);
|
|
|
|
$basename = pathinfo($backup,PATHINFO_BASENAME);
|
|
|
|
|
|
|
|
$html .= '<div>';
|
2018-07-01 14:17:24 +02:00
|
|
|
$html .= '<h4 class="font-weight-normal">'.Date::format($filename, BACKUP_DATE_FORMAT, 'F j, Y, g:i a').'</h4>';
|
2018-02-26 17:44:39 +01:00
|
|
|
// Allow download if a zip file
|
|
|
|
if ($this->zip) {
|
2019-05-18 11:54:39 +02:00
|
|
|
$html .= '<a class="btn btn-outline-secondary btn-sm mr-1 mt-1" href="'.DOMAIN_CONTENT.'workspaces/backup/'.$filename.'.zip"><span class="fa fa-download"></span> '.$L->get('download').'</a>';
|
2018-02-26 17:44:39 +01:00
|
|
|
}
|
2019-05-18 11:54:39 +02:00
|
|
|
$html .= '<button name="restoreBackup" value="'.$filename.'" class="btn btn-outline-secondary btn-sm mr-1 mt-1" type="submit"><span class="fa fa-rotate-left"></span> '.$L->get('restore-backup').'</button>';
|
|
|
|
$html .= '<button name="deleteBackup" value="'.$filename.'" class="btn btn-outline-danger btn-sm mr-1 mt-1" type="submit"><span class="fa fa-trash"></span> '.$L->get('delete-backup').'</button>';
|
2017-11-16 23:21:05 +01:00
|
|
|
$html .= '</div>';
|
|
|
|
$html .= '<hr>';
|
|
|
|
}
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
2018-07-02 00:24:53 +02:00
|
|
|
public function backupList()
|
|
|
|
{
|
|
|
|
if ($this->zip) {
|
|
|
|
$backups = Filesystem::listFiles($this->workspace(), '*', 'zip', true);
|
|
|
|
} else {
|
|
|
|
$backups = Filesystem::listDirectories($this->workspace(), '*', true);
|
|
|
|
}
|
|
|
|
return $backups;
|
|
|
|
}
|
|
|
|
|
2017-11-16 23:21:05 +01:00
|
|
|
public function createBackup()
|
|
|
|
{
|
|
|
|
// Current backup directory
|
|
|
|
$currentDate = Date::current(BACKUP_DATE_FORMAT);
|
|
|
|
$backupDir = $this->workspace().$currentDate;
|
|
|
|
|
|
|
|
// Copy directories to backup directory
|
|
|
|
// $directoriesToBackup is a private variable of this class
|
|
|
|
foreach ($this->directoriesToBackup as $dir) {
|
|
|
|
$destination = $backupDir.DS.basename($dir);
|
|
|
|
Filesystem::copyRecursive($dir, $destination);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compress backup directory
|
|
|
|
if ($this->zip) {
|
|
|
|
if (Filesystem::zip($backupDir, $backupDir.'.zip')) {
|
|
|
|
Filesystem::deleteRecursive($backupDir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-26 17:44:39 +01:00
|
|
|
public function restoreBackup($filename)
|
2017-11-16 23:21:05 +01:00
|
|
|
{
|
|
|
|
// Remove current files
|
|
|
|
foreach ($this->directoriesToBackup as $dir) {
|
|
|
|
Filesystem::deleteRecursive($dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recover backuped files
|
2018-02-26 17:44:39 +01:00
|
|
|
// Zip format
|
2017-11-16 23:21:05 +01:00
|
|
|
if ($this->zip) {
|
2018-02-26 17:44:39 +01:00
|
|
|
$tmp = $this->workspace().$filename.'.zip';
|
2017-11-16 23:21:05 +01:00
|
|
|
return Filesystem::unzip($tmp, PATH_CONTENT);
|
|
|
|
}
|
2018-02-26 17:44:39 +01:00
|
|
|
|
|
|
|
// Directory format
|
|
|
|
$tmp = $this->workspace().$filename;
|
2017-11-16 23:21:05 +01:00
|
|
|
return Filesystem::copyRecursive($tmp, PATH_CONTENT);
|
|
|
|
}
|
|
|
|
|
2018-02-26 17:44:39 +01:00
|
|
|
public function deleteBackup($filename)
|
|
|
|
{
|
|
|
|
// Zip format
|
|
|
|
if ($this->zip) {
|
|
|
|
$tmp = $this->workspace().$filename.'.zip';
|
|
|
|
return Filesystem::rmfile($tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Directory format
|
|
|
|
$tmp = $this->workspace().$filename;
|
|
|
|
return Filesystem::deleteRecursive($tmp);
|
|
|
|
}
|
2018-08-24 00:17:31 +02:00
|
|
|
}
|