2021-09-11 20:25:18 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @brief improve, a plugin for Dotclear 2
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-11 20:25:18 +00:00
|
|
|
* @package Dotclear
|
|
|
|
* @subpackage Plugin
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-11 20:25:18 +00:00
|
|
|
* @author Jean-Christian Denis and contributors
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-11 20:25:18 +00:00
|
|
|
* @copyright Jean-Christian Denis
|
|
|
|
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
|
|
|
|
*/
|
2021-11-13 21:57:51 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-12-21 14:41:27 +00:00
|
|
|
namespace Dotclear\Plugin\improve\Module;
|
2021-11-13 21:57:51 +00:00
|
|
|
|
|
|
|
/* improve */
|
2022-12-21 14:41:27 +00:00
|
|
|
use Dotclear\Plugin\improve\Action;
|
2021-11-13 21:57:51 +00:00
|
|
|
|
|
|
|
/* clearbricks */
|
|
|
|
use form;
|
|
|
|
use files;
|
|
|
|
|
|
|
|
/* php */
|
|
|
|
use Exception;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Improve action module license file
|
|
|
|
*/
|
2022-12-21 14:41:27 +00:00
|
|
|
class licensefile extends Action
|
2021-09-11 20:25:18 +00:00
|
|
|
{
|
2021-11-07 23:59:52 +00:00
|
|
|
/** @var array Possible license filenames */
|
2021-09-11 20:25:18 +00:00
|
|
|
protected static $license_filenames = [
|
|
|
|
'license',
|
|
|
|
'license.md',
|
2021-11-13 21:57:51 +00:00
|
|
|
'license.txt',
|
2021-09-11 20:25:18 +00:00
|
|
|
];
|
2021-11-07 23:59:52 +00:00
|
|
|
|
|
|
|
/** @var array Possible license names */
|
2021-09-11 20:25:18 +00:00
|
|
|
private $action_version = [];
|
2021-11-07 23:59:52 +00:00
|
|
|
|
|
|
|
/** @var array Action */
|
|
|
|
private $action_full = [];
|
2021-09-11 20:25:18 +00:00
|
|
|
|
|
|
|
protected function init(): bool
|
|
|
|
{
|
|
|
|
$this->setProperties([
|
2021-11-08 23:40:39 +00:00
|
|
|
'id' => 'license',
|
|
|
|
'name' => __('License file'),
|
|
|
|
'description' => __('Add or remove full license file to module root'),
|
|
|
|
'priority' => 330,
|
|
|
|
'configurator' => true,
|
2021-11-13 21:57:51 +00:00
|
|
|
'types' => ['plugin', 'theme'],
|
2021-09-11 20:25:18 +00:00
|
|
|
]);
|
|
|
|
$this->action_version = [
|
|
|
|
__('no version selected') => '',
|
|
|
|
__('gpl2 - GNU General Public License v2') => 'gpl2',
|
|
|
|
__('gpl3 - GNU General Public License v3') => 'gpl3',
|
|
|
|
__('lgpl3 - GNU Lesser General Public License v3') => 'lgpl3',
|
2021-11-13 21:57:51 +00:00
|
|
|
__('Massachusetts Institute of Technolog mit') => 'mit',
|
2022-12-10 14:33:07 +00:00
|
|
|
__('Do What The Fuck You Want To Public License') => 'wtfpl',
|
2021-09-11 20:25:18 +00:00
|
|
|
];
|
|
|
|
$this->action_full = [
|
|
|
|
__('Do nothing') => 0,
|
|
|
|
__('Add file if it does not exist') => 'create',
|
|
|
|
__('Add file even if it exists') => 'overwrite',
|
|
|
|
__('Add file and remove others') => 'full',
|
2021-11-13 21:57:51 +00:00
|
|
|
__('Remove license files') => 'remove',
|
2021-09-11 20:25:18 +00:00
|
|
|
];
|
2021-11-01 21:17:44 +00:00
|
|
|
|
2021-09-11 20:25:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isConfigured(): bool
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function configure($url): ?string
|
|
|
|
{
|
|
|
|
if (!empty($_POST['save'])) {
|
2021-09-15 21:48:58 +00:00
|
|
|
$this->setSettings([
|
2021-11-01 21:17:44 +00:00
|
|
|
'action_version' => !empty($_POST['action_version']) ? $_POST['action_version'] : '',
|
2021-11-13 21:57:51 +00:00
|
|
|
'action_full' => !empty($_POST['action_full']) ? $_POST['action_full'] : '',
|
2021-09-11 20:25:18 +00:00
|
|
|
]);
|
|
|
|
$this->redirect($url);
|
|
|
|
}
|
|
|
|
|
|
|
|
return '
|
|
|
|
<p class="field"><label for="action_version">' . __('License version:') . '</label>' .
|
2021-09-15 21:48:58 +00:00
|
|
|
form::combo('action_version', $this->action_version, $this->getSetting('action_version')) . '
|
2021-09-11 20:25:18 +00:00
|
|
|
</p>
|
|
|
|
|
|
|
|
<p class="field"><label for="action_full">' . __('Action on file:') . '</label>' .
|
2021-11-01 21:17:44 +00:00
|
|
|
form::combo('action_full', $this->action_full, $this->getSetting('action_full')) .
|
2021-09-11 20:25:18 +00:00
|
|
|
'</p>';
|
|
|
|
}
|
|
|
|
|
2021-09-15 21:48:58 +00:00
|
|
|
public function openModule(): ?bool
|
2021-09-11 20:25:18 +00:00
|
|
|
{
|
2021-09-15 21:48:58 +00:00
|
|
|
if (in_array($this->getSetting('action_full'), ['remove', 'full','overwrite'])) {
|
|
|
|
$this->deleteFullLicense(($this->getSetting('action_full') == 'overwrite'));
|
2021-09-11 20:25:18 +00:00
|
|
|
}
|
2021-09-15 21:48:58 +00:00
|
|
|
if (in_array($this->getSetting('action_full'), ['create', 'overwrite', 'full'])) {
|
|
|
|
if (empty($this->getSetting('action_version'))) {
|
|
|
|
$this->setWarning(__('No full license type selected'));
|
2021-09-11 20:25:18 +00:00
|
|
|
} else {
|
|
|
|
$this->writeFullLicense();
|
|
|
|
}
|
|
|
|
}
|
2021-11-01 21:17:44 +00:00
|
|
|
|
2021-09-11 20:25:18 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-11-07 23:59:52 +00:00
|
|
|
private function writeFullLicense(): ?bool
|
2021-09-11 20:25:18 +00:00
|
|
|
{
|
|
|
|
try {
|
2022-12-21 14:41:27 +00:00
|
|
|
$full = file_get_contents(__DIR__ . '/licensefile/' . $this->getSetting('action_version') . '.full.txt');
|
2021-09-11 20:25:18 +00:00
|
|
|
if (empty($full)) {
|
2021-09-15 21:48:58 +00:00
|
|
|
$this->setError(__('Failed to load license content'));
|
2021-09-11 20:25:18 +00:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2021-09-15 21:48:58 +00:00
|
|
|
files::putContent($this->module['root'] . '/LICENSE', str_replace("\r\n", "\n", $full));
|
|
|
|
$this->setSuccess(__('Write new license file "LICENSE"'));
|
2021-09-11 20:25:18 +00:00
|
|
|
} catch (Exception $e) {
|
2021-09-15 21:48:58 +00:00
|
|
|
$this->setError(__('Failed to write new license file'));
|
2021-09-11 20:25:18 +00:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2021-11-01 21:17:44 +00:00
|
|
|
|
2021-09-11 20:25:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-11-07 23:59:52 +00:00
|
|
|
private function deleteFullLicense(bool $only_one = false): bool
|
2021-09-11 20:25:18 +00:00
|
|
|
{
|
2021-11-01 21:17:44 +00:00
|
|
|
foreach (self::fileExists($this->module['root']) as $file) {
|
2021-09-15 21:48:58 +00:00
|
|
|
if ($only_one && $file != 'LICENSE') {
|
2021-09-11 20:25:18 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!files::isDeletable($this->module['root'] . '/' . $file)) {
|
2021-09-15 21:48:58 +00:00
|
|
|
$this->setWarning(sprintf(__('Old license file is not deletable (%s)'), $file));
|
|
|
|
} elseif (!@unlink($this->module['root'] . '/' . $file)) {
|
|
|
|
$this->setError(sprintf(__('Failed to delete old license file (%s)'), $file));
|
|
|
|
} else {
|
|
|
|
$this->setSuccess(sprintf(__('Delete old license file "%s"'), $file));
|
2021-09-11 20:25:18 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-01 21:17:44 +00:00
|
|
|
|
2021-09-11 20:25:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-11-07 23:59:52 +00:00
|
|
|
private static function fileExists(string $root): array
|
2021-09-11 20:25:18 +00:00
|
|
|
{
|
|
|
|
$existing = [];
|
2021-11-01 21:17:44 +00:00
|
|
|
foreach (self::$license_filenames as $file) {
|
2021-09-11 20:25:18 +00:00
|
|
|
if (file_exists($root . '/' . strtolower($file))) {
|
|
|
|
$existing[] = strtolower($file);
|
|
|
|
}
|
|
|
|
}
|
2021-11-01 21:17:44 +00:00
|
|
|
|
2021-09-11 20:25:18 +00:00
|
|
|
return $existing;
|
|
|
|
}
|
2021-11-01 21:17:44 +00:00
|
|
|
}
|