bludit/bl-plugins/updater/plugin.php

121 lines
2.9 KiB
PHP
Raw Normal View History

2018-03-07 16:43:17 +01:00
<?php
class pluginUpdater extends Plugin {
// Define if the extension zip is loaded
private $zip = false;
2018-06-24 13:37:45 +02:00
private $urlLatestVersionFile = 'https://';
private $localLatestVersionFile = '';
2018-03-07 16:43:17 +01:00
public function init()
{
// Disable default form buttons (Save and Cancel)
$this->formButtons = false;
// Check for zip extension installed
$this->zip = extension_loaded('zip');
2018-06-24 13:37:45 +02:00
// Local full path of the file of the latest version of Bludit
$this->localLatestVersionFile = $this->workspace().DS.'bludit-latest.zip';
}
// Redefine workspace
public function workspace()
{
return PATH_CONTENT.'updater'.DS;
2018-03-07 16:43:17 +01:00
}
// Install the plugin and create the workspace directory
public function install($position=0)
{
parent::install($position);
$workspace = $this->workspace();
return mkdir($workspace, 0755, true);
}
// Uninstall the plugin and delete the workspace directory
public function uninstall()
{
parent::uninstall();
$workspace = $this->workspace();
return Filesystem::deleteRecursive($workspace);
}
// Check if the root directory is writable
2018-06-24 13:37:45 +02:00
public function isWritable()
2018-03-07 16:43:17 +01:00
{
return is_writable(PATH_ROOT);
}
// Create a copy of all the system and compress it
// Returns the name of the backup directory
2018-06-24 13:37:45 +02:00
public function makeFullBackup()
2018-03-07 16:43:17 +01:00
{
$currentDate = Date::current(BACKUP_DATE_FORMAT);
$backupDirectory = $this->workspace().$currentDate;
2018-06-24 13:37:45 +02:00
// Copy all files from PATH_ROOT to $backupDirectory, also omit the directory $backupDirectory
Filesystem::copyRecursive(PATH_ROOT, $backupDirectory, $backupDirectory);
2018-03-07 16:43:17 +01:00
// Compress the backup directory
if (Filesystem::zip($backupDirectory, $backupDirectory.'.zip')) {
Filesystem::deleteRecursive($backupDirectory);
}
return $backupDirectory;
}
// Download the latest version of Bludit
2018-06-24 13:37:45 +02:00
public function downloadLatestVersion()
2018-03-07 16:43:17 +01:00
{
2018-06-24 13:37:45 +02:00
return TCP::download($this->urlLatestVersionFile, $this->localLatestVersionFile);
}
public function validChecksum()
{
// IMPLEMENT !!!
return true;
}
// Unzip the latest version and replace the old files
public function upgradeFiles()
{
return Filesystem::unzip($this->localLatestVersionFile, PATH_ROOT);
2018-03-07 16:43:17 +01:00
}
public function post()
{
2018-06-24 13:37:45 +02:00
if (isset($_POST['updateNow'])) {
echo 'Making a backup';
$this->makeFullBackup();
echo 'Downloading the latest version of Bludit';
$this->downloadLatestVersion();
echo 'Validating checksum';
if ($this->validChecksum()) {
echo 'Updating files';
return $this->upgradeFiles();
}
2018-03-07 16:43:17 +01:00
}
return false;
}
public function form()
{
global $Language;
2018-06-24 13:37:45 +02:00
if ($this->zip===false) {
//return '<div class="alert alert-success">The extension zip file is not installed, to use this plugin you need install the extension first.</div>';
2018-03-07 16:43:17 +01:00
}
$html = '<div>';
2018-06-24 13:37:45 +02:00
$html .= '<button name="updateNow" value="true" class="btn btn-primary" type="submit">'.$Language->get('Update Now').'</button>';
2018-03-07 16:43:17 +01:00
$html .= '</div>';
return $html;
}
2018-06-24 13:37:45 +02:00
}