Compare commits

...

10 Commits

31 changed files with 866 additions and 632 deletions

View File

@ -1,3 +1,21 @@
2023.05.13
- require dotclea r2.26
- require php 8.1
- use define php_min
- fix dump() method
2023.04.22
- require dotclea r2.26
- require php 8.1
- add plugin Uninstaller features
- use latest helper namespace
- fix nullsafe warnings
2023.03.25
- require dotclea r2.26
- require php 8.1
- use namespace
2022.12.22 2022.12.22
- add wtfpl license - add wtfpl license
- use SVG icon - use SVG icon

View File

@ -3,7 +3,7 @@
[![Release](https://img.shields.io/github/v/release/JcDenis/licenseBootstrap)](https://github.com/JcDenis/licenseBootstrap/releases) [![Release](https://img.shields.io/github/v/release/JcDenis/licenseBootstrap)](https://github.com/JcDenis/licenseBootstrap/releases)
[![Date](https://img.shields.io/github/release-date/JcDenis/licenseBootstrap)](https://github.com/JcDenis/licenseBootstrap/releases) [![Date](https://img.shields.io/github/release-date/JcDenis/licenseBootstrap)](https://github.com/JcDenis/licenseBootstrap/releases)
[![Issues](https://img.shields.io/github/issues/JcDenis/licenseBootstrap)](https://github.com/JcDenis/licenseBootstrap/issues) [![Issues](https://img.shields.io/github/issues/JcDenis/licenseBootstrap)](https://github.com/JcDenis/licenseBootstrap/issues)
[![Dotclear](https://img.shields.io/badge/dotclear-v2.24-blue.svg)](https://fr.dotclear.org/download) [![Dotclear](https://img.shields.io/badge/dotclear-v2.26-blue.svg)](https://fr.dotclear.org/download)
[![Dotaddict](https://img.shields.io/badge/dotaddict-official-green.svg)](https://plugins.dotaddict.org/dc2/details/licenseBootstrap) [![Dotaddict](https://img.shields.io/badge/dotaddict-official-green.svg)](https://plugins.dotaddict.org/dc2/details/licenseBootstrap)
[![License](https://img.shields.io/github/license/JcDenis/licenseBootstrap)](https://github.com/JcDenis/licenseBootstrap/blob/master/LICENSE) [![License](https://img.shields.io/github/license/JcDenis/licenseBootstrap)](https://github.com/JcDenis/licenseBootstrap/blob/master/LICENSE)
@ -20,7 +20,8 @@ themes and plugins from Dotclear administration pages.
licenseBootstrap requires: licenseBootstrap requires:
* Super administrator permissions * Super administrator permissions
* Dotclear 2.24 * Dotclear 2.26
* Php >= 8.1
## USAGE ## USAGE
@ -33,6 +34,10 @@ go to ''configure plugin'', fill in form.
Once it's done you can manage your modules license from menu Once it's done you can manage your modules license from menu
''License bootstrap'' on sidebar or you can add dashboard icon. ''License bootstrap'' on sidebar or you can add dashboard icon.
By default, licenseBootstrap uses old style code blocs,
if you want a modern style you should use a more recent plugin
like the one called ''improve''.
## LINKS ## LINKS
* License : [GNU GPL v2](https://www.gnu.org/licenses/old-licenses/lgpl-2.0.html) * License : [GNU GPL v2](https://www.gnu.org/licenses/old-licenses/lgpl-2.0.html)

View File

@ -1,41 +0,0 @@
<?php
/**
* @brief licenseBootstrap, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_CONTEXT_ADMIN')) {
return null;
}
dcCore::app()->blog->settings->addNamespace(basename(__DIR__));
dcCore::app()->addBehavior('adminDashboardFavoritesV2', function ($favs) {
$favs->register(basename(__DIR__), [
'title' => __('License bootstrap'),
'url' => dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__)),
'small-icon' => urldecode(dcPage::getPF(basename(__DIR__) . '/icon.svg')),
'large-icon' => urldecode(dcPage::getPF(basename(__DIR__) . '/icon.svg')),
//'permissions' => dcCore::app()->auth->isSuperAdmin(),
]);
});
dcCore::app()->addBehavior('packmanBeforeCreatePackage', function ($module) {
if (dcCore::app()->blog->settings->get(basename(__DIR__))->get('behavior_packman')) {
licenseBootstrap::addLicense($module);
}
});
dcCore::app()->menu[dcAdmin::MENU_PLUGINS]->addItem(
__('License bootstrap'),
dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__)),
urldecode(dcPage::getPF(basename(__DIR__) . '/icon.svg')),
preg_match('/' . preg_quote(dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__))) . '(&.*)?$/', $_SERVER['REQUEST_URI']),
dcCore::app()->auth->isSuperAdmin()
);

View File

@ -1,114 +0,0 @@
<?php
/**
* @brief licenseBootstrap, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_CONTEXT_MODULE')) {
return null;
}
$redir = empty($_REQUEST['redir']) ?
dcCore::app()->admin->__get('list')->getURL() . '#plugins' : $_REQUEST['redir'];
# -- Get settings --
$s = dcCore::app()->blog->settings->addNamespace(basename(__DIR__));
$lb_overwrite = (bool) $s->get('overwrite');
$lb_write_full = (bool) $s->get('write_full');
$lb_write_php = (bool) $s->get('write_php');
$lb_write_js = (bool) $s->get('write_js');
$lb_exclude_locales = (bool) $s->get('exclude_locales');
$lb_license_name = licenseBootstrap::getName($s->get('license_name'));
$lb_license_head = licenseBootstrap::gethead($s->get('license_name'), licenseBootstrap::decode($s->get('license_head')));
# -- Set settings --
if (!empty($_POST['save'])) {
try {
$lb_overwrite = !empty($_POST['lb_overwrite']);
$lb_write_full = !empty($_POST['lb_write_full']);
$lb_write_php = !empty($_POST['lb_write_php']);
$lb_write_js = !empty($_POST['lb_write_js']);
$lb_exclude_locales = !empty($_POST['lb_exclude_locales']);
$lb_license_name = $_POST['lb_license_name'];
$lb_license_head = licenseBootstrap::gethead($lb_license_name, !empty($_POST['lb_license_head_' . $lb_license_name]) ? $_POST['lb_license_head_' . $lb_license_name] : '');
$s->put('overwrite', $lb_overwrite);
$s->put('write_full', $lb_write_full);
$s->put('write_php', $lb_write_php);
$s->put('write_js', $lb_write_js);
$s->put('exclude_locales', $lb_exclude_locales);
$s->put('license_name', licenseBootstrap::getName($lb_license_name));
$s->put('license_head', licenseBootstrap::encode($lb_license_head));
dcPage::addSuccessNotice(
__('Configuration has been successfully updated.')
);
http::redirect(
dcCore::app()->admin->__get('list')->getURL('module=' . basename(__DIR__) . '&conf=1&redir=' .
dcCore::app()->admin->__get('list')->getRedir())
);
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
}
}
# -- Display form --
echo '
<div class="fieldset">
<h4>' . __('Files') . '</h4>
<p><label class="classic" for="lb_overwrite">' .
form::checkbox('lb_overwrite', 1, $lb_overwrite) . ' ' .
__('Overwrite existing licenses') .
'</label></p>
<p><label class="classic" for="lb_write_full">' .
form::checkbox('lb_write_full', 1, $lb_write_full) . ' ' .
__('Add full LICENSE file to module root') .
'</label></p>
<p><label class="classic" for="lb_write_php">' .
form::checkbox('lb_write_php', 1, $lb_write_php) . ' ' .
__('Add license block to PHP files') .
'</label></p>
<p><label class="classic" for="lb_write_js">' .
form::checkbox('lb_write_js', 1, $lb_write_js) . ' ' .
__('Add license block to JS files') .
'</label></p>
<p><label class="classic" for="lb_exclude_locales">' .
form::checkbox('lb_exclude_locales', 1, $lb_exclude_locales) . ' ' .
__('Do not add license block to files from locales folder') .
'</label></p>
</div>
<div class="fieldset">
<h4>' . __('Licenses') . '</h4>';
foreach (licenseBootstrap::getLicenses() as $name) {
$check = false;
$head = licenseBootstrap::getHead($name);
if ($name == $lb_license_name) {
$check = true;
$head = licenseBootstrap::getHead($name, $lb_license_head);
}
echo '
<p><label class="classic" for="license_' . $name . '">' .
form::radio(['lb_license_name', 'license_' . $name], $name, $check) . ' ' .
sprintf(__('License %s:'), $name) . '</label></p>
<p class="area">' .
form::textarea('lb_license_head_' . $name, 50, 10, html::escapeHTML($head)) . '
</p>';
}
echo '
</div>';

View File

@ -18,9 +18,12 @@ $this->registerModule(
'License bootstrap', 'License bootstrap',
'Add license to your plugins and themes', 'Add license to your plugins and themes',
'Jean-Christian Denis', 'Jean-Christian Denis',
'2022.12.22', '2023.05.13',
[ [
'requires' => [['core', '2.24']], 'requires' => [
['php', '8.1'],
['core', '2.26'],
],
'permissions' => null, 'permissions' => null,
'type' => 'plugin', 'type' => 'plugin',
'support' => 'https://github.com/JcDenis/' . basename(__DIR__), 'support' => 'https://github.com/JcDenis/' . basename(__DIR__),

View File

@ -1,121 +0,0 @@
<?php
/**
* @brief licenseBootstrap, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_CONTEXT_ADMIN')) {
return null;
}
# -- Module specs --
$mod_conf = [
[
'overwrite',
'Overwrite existing licence',
false,
'boolean',
],
[
'write_full',
'Add complete licence file',
true,
'boolean',
],
[
'write_php',
'Write license into php files',
true,
'boolean',
],
[
'write_js',
'Write license into js files',
false,
'boolean',
],
[
'exclude_locales',
'Exclude locales from license',
true,
'boolean',
],
[
'license_name',
'License short name',
'gpl2',
'string',
],
[
'license_head',
'File header licence text',
licenseBootstrap::encode(
licenseBootstrap::getHead('gpl2')
),
'string',
],
[
'behavior_packman',
'Add LicenceBootstrap to plugin pacKman',
false,
'boolean',
],
];
# -- Nothing to change below --
try {
# Check module version
if (!dcCore::app()->newVersion(
basename(__DIR__),
dcCore::app()->plugins->moduleInfo(basename(__DIR__), 'version')
)) {
return null;
}
// version < 2022.12.22 : upgrade settings ns and array
$current = dcCore::app()->getVersion(basename(__DIR__));
if ($current && version_compare($current, '2022.12.22', '<')) {
$record = dcCore::app()->con->select(
'SELECT * FROM ' . dcCore::app()->prefix . dcNamespace::NS_TABLE_NAME . ' ' .
"WHERE setting_ns = 'licenseBootstrap' "
);
$cur = dcCore::app()->con->openCursor(dcCore::app()->prefix . dcNamespace::NS_TABLE_NAME);
while ($record->fetch()) {
if (in_array($record->setting_id, ['license_head'])) {
$cur->setting_value = (string) unserialize(base64_decode($record->setting_value));
}
$cur->setting_ns = basename(__DIR__);
$cur->update(
"WHERE setting_id = '" . $record->setting_id . "' and setting_ns = 'licenseBootstrap' " .
'AND blog_id ' . (null === $record->blog_id ? 'IS NULL ' : ("= '" . dcCore::app()->con->escape($record->blog_id) . "' "))
);
$cur->clean();
}
}
# Set module settings
dcCore::app()->blog->settings->addNamespace(basename(__DIR__));
foreach ($mod_conf as $v) {
dcCore::app()->blog->settings->__get(basename(__DIR__))->put(
$v[0],
$v[2],
$v[3],
$v[1],
false,
true
);
}
return true;
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
return false;
}

View File

@ -1,20 +0,0 @@
<?php
/**
* @brief licenseBootstrap, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_RC_PATH')) {
return null;
}
Clearbricks::lib()->autoload([
'licenseBootstrap' => __DIR__ . '/inc/class.license.bootstrap.php',
'libLicenseBootstrap' => __DIR__ . '/inc/lib.license.bootstrap.php',
]);

View File

@ -1,59 +0,0 @@
<?php
/**
* @brief licenseBootstrap, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_CONTEXT_ADMIN')) {
return null;
}
$this->addUserAction(
/* type */
'settings',
/* action */
'delete_all',
/* ns */
basename(__DIR__),
/* desc */
__('delete all settings')
);
$this->addUserAction(
/* type */
'plugins',
/* action */
'delete',
/* ns */
basename(__DIR__),
/* desc */
__('delete plugin files')
);
$this->addDirectAction(
/* type */
'settings',
/* action */
'delete_all',
/* ns */
basename(__DIR__),
/* desc */
sprintf(__('delete all %s settings'), basename(__DIR__))
);
$this->addDirectAction(
/* type */
'plugins',
/* action */
'delete',
/* ns */
basename(__DIR__),
/* desc */
sprintf(__('delete %s plugin files'), basename(__DIR__))
);

View File

@ -2,11 +2,11 @@
<modules xmlns:da="http://dotaddict.org/da/"> <modules xmlns:da="http://dotaddict.org/da/">
<module id="licenseBootstrap"> <module id="licenseBootstrap">
<name>License bootstrap</name> <name>License bootstrap</name>
<version>2022.12.22</version> <version>2023.05.13</version>
<author>Jean-Christian Denis</author> <author>Jean-Christian Denis</author>
<desc>Add license to your plugins and themes</desc> <desc>Add license to your plugins and themes</desc>
<file>https://github.com/JcDenis/licenseBootstrap/releases/download/v2022.12.22/plugin-licenseBootstrap.zip</file> <file>https://github.com/JcDenis/licenseBootstrap/releases/download/v2023.05.13/plugin-licenseBootstrap.zip</file>
<da:dcmin>2.24</da:dcmin> <da:dcmin>2.26</da:dcmin>
<da:details>https://plugins.dotaddict.org/dc2/details/licenseBootstrap</da:details> <da:details>https://plugins.dotaddict.org/dc2/details/licenseBootstrap</da:details>
<da:support>https://github.com/JcDenis/licenseBootstrap</da:support> <da:support>https://github.com/JcDenis/licenseBootstrap</da:support>
</module> </module>

View File

@ -1,96 +0,0 @@
<?php
/**
* @brief licenseBootstrap, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_CONTEXT_ADMIN')) {
return null;
}
class libLicenseBootstrap
{
public static function modules($modules, $type, $title)
{
$type = $type == 'themes' ? 'themes' : 'plugins';
echo
'<div class="multi-part" ' .
'id="packman-' . $type . '" title="' . $title . '">' .
'<h3>' . $title . '</h3>';
if (empty($modules) && !is_array($modules)) {
echo
'<p><strong>' . __('There are no modules.') . '</strong></p>' .
'<div>';
return null;
}
echo
'<form action="plugin.php" method="post">' .
'<table class="clear"><tr>' .
'<th class="nowrap">' . __('Id') . '</th>' .
'<th class="nowrap">' . __('Version') . '</th>' .
'<th class="nowrap maximal">' . __('Name') . '</th>' .
'<th class="nowrap">' . __('Root') . '</th>' .
'</tr>';
foreach (self::sort($modules) as $id => $module) {
echo
'<tr class="line">' .
'<td class="nowrap"><label class="classic">' .
form::checkbox(['modules[' . html::escapeHTML($id) . ']'], 1) .
html::escapeHTML($id) .
'</label></td>' .
'<td class="nowrap count">' .
html::escapeHTML($module['version']) .
'</td>' .
'<td class="nowrap maximal">' .
__(html::escapeHTML($module['name'])) .
'</td>' .
'<td class="nowrap">' .
dirname(path::real($module['root'], false)) .
'</td>' .
'</tr>';
}
echo
'</table>' .
'<p class="checkboxes-helpers"></p>' .
'<p>' .
(
!empty($_REQUEST['redir']) ?
form::hidden(
['redir'],
html::escapeHTML($_REQUEST['redir'])
) : ''
) .
form::hidden(['p'], 'licenseBootstrap') .
form::hidden(['type'], $type) .
form::hidden(['action'], 'addlicense') .
'<input type="submit" name="addlicense" value="' .
__('Add license to selected modules') . '" />' .
dcCore::app()->formNonce() . '</p>' .
'</form>' .
'</div>';
}
protected static function sort($modules)
{
$sorter = [];
foreach ($modules as $id => $module) {
$sorter[$id] = $id;
}
array_multisort($sorter, SORT_ASC, $modules);
return $modules;
}
}

103
index.php
View File

@ -1,103 +0,0 @@
<?php
/**
* @brief licenseBootstrap, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_CONTEXT_ADMIN')) {
return null;
}
dcPage::checkSuper();
# Queries
$action = $_POST['action'] ?? '';
$type = isset($_POST['type']) && in_array($_POST['type'], ['plugins', 'themes']) ? $_POST['type'] : '';
# Settings
$s = dcCore::app()->blog->settings->addNamespace(basename(__DIR__));
# Modules
if (!(dcCore::app()->themes instanceof dcThemes)) {
dcCore::app()->themes = new dcThemes();
dcCore::app()->themes->loadModules(dcCore::app()->blog->themes_path, null);
}
$themes = dcCore::app()->themes;
$plugins = dcCore::app()->plugins;
# Rights
$is_editable = !empty($type)
&& !empty($_POST['modules'])
&& is_array($_POST['modules']);
# Actions
try {
# Add license to modules
if ($action == 'addlicense' && $is_editable) {
$modules = array_keys($_POST['modules']);
foreach ($modules as $id) {
if (!${$type}->moduleExists($id)) {
throw new Exception('No such module');
}
$module = ${$type}->getModules($id);
$module['id'] = $id;
$module['type'] = $type == 'themes' ? 'theme' : 'plugin';
licenseBootstrap::addLicense($module);
}
dcAdminNotices::addSuccessNotice(
__('License successfully added.')
);
http::redirect(
empty($_POST['redir']) ?
dcCore::app()->admin->getPageURL() : $_POST['redir']
);
}
} catch(Exception $e) {
dcCore::app()->error->add($e->getMessage());
}
# Display
echo
'<html><head><title>' . __('License bootstrap') . '</title>' .
dcPage::jsPageTabs() .
dcPage::jsModuleLoad(basename(__DIR__) . '/js/licensebootstrap.js') .
# --BEHAVIOR-- licenseBootstrapAdminHeader
dcCore::app()->callBehavior('licenseBootstrapAdminHeader') .
'</head><body>' .
dcPage::breadcrumb(
[
__('Plugins') => '',
__('License bootstrap') => '',
]
) .
dcPage::notices();
libLicenseBootstrap::modules(
$plugins->getModules(),
'plugins',
__('Installed plugins')
);
libLicenseBootstrap::modules(
$themes->getModules(),
'themes',
__('Installed themes')
);
dcPage::helpBlock('licenseBootstrap');
echo
'</body></html>';

View File

@ -9,17 +9,19 @@
# DOT NOT MODIFY THIS FILE ! # DOT NOT MODIFY THIS FILE !
# #
l10n::$locales['Configuration has been successfully updated.'] = 'La configuration a été mise à jour avec succés.'; use Dotclear\Helper\L10n;
l10n::$locales['Files'] = 'Fichiers';
l10n::$locales['Overwrite existing licenses'] = 'Écraser les licences existantes'; L10n::$locales['Configuration has been successfully updated.'] = 'La configuration a été mise à jour avec succés.';
l10n::$locales['Add full LICENSE file to module root'] = 'Ajouter le fichier de licence complet à la racine du module'; L10n::$locales['Files'] = 'Fichiers';
l10n::$locales['Add license block to PHP files'] = 'Ajouter le bloc de licence aux ficheirs PHP'; L10n::$locales['Overwrite existing licenses'] = 'Écraser les licences existantes';
l10n::$locales['Add license block to JS files'] = 'Ajouter le bloc de licence aux ficheirs JS'; L10n::$locales['Add full LICENSE file to module root'] = 'Ajouter le fichier de licence complet à la racine du module';
l10n::$locales['Do not add license block to files from locales folder'] = 'Ne pas ajouter la licence aux fichiers du dossier locales'; L10n::$locales['Add license block to PHP files'] = 'Ajouter le bloc de licence aux ficheirs PHP';
l10n::$locales['Licenses'] = 'Licences'; L10n::$locales['Add license block to JS files'] = 'Ajouter le bloc de licence aux ficheirs JS';
l10n::$locales['License %s:'] = 'Licence %s :'; L10n::$locales['Do not add license block to files from locales folder'] = 'Ne pas ajouter la licence aux fichiers du dossier locales';
l10n::$locales['Id'] = 'Id'; L10n::$locales['Licenses'] = 'Licences';
l10n::$locales['Root'] = 'Racine'; L10n::$locales['License %s:'] = 'Licence %s :';
l10n::$locales['Add license to selected modules'] = 'Ajouter la licence aux modules selectionner'; L10n::$locales['Id'] = 'Id';
l10n::$locales['License successfully added.'] = 'Licence ajouter avec succès'; L10n::$locales['Root'] = 'Racine';
l10n::$locales['Configuration'] = 'Configuration'; L10n::$locales['Add license to selected modules'] = 'Ajouter la licence aux modules selectionner';
L10n::$locales['License successfully added.'] = 'Licence ajouter avec succès';
L10n::$locales['Configuration'] = 'Configuration';

66
src/Backend.php 100644
View File

@ -0,0 +1,66 @@
<?php
/**
* @brief licenseBootstrap, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\licenseBootstrap;
use dcAdmin;
use dcCore;
use dcFavorites;
use dcNsProcess;
use dcPage;
class Backend extends dcNsProcess
{
public static function init(): bool
{
static::$init = defined('DC_CONTEXT_ADMIN')
&& dcCore::app()->auth?->isSuperAdmin();
return static::$init;
}
public static function process(): bool
{
if (!static::$init) {
return false;
}
dcCore::app()->addBehaviors([
'adminDashboardFavoritesV2' => function (dcFavorites $favs): void {
$favs->register(My::id(), [
'title' => My::name(),
'url' => dcCore::app()->adminurl?->get('admin.plugin.' . My::id()),
'small-icon' => dcPage::getPF(My::id() . '/icon.svg'),
'large-icon' => dcPage::getPF(My::id() . '/icon.svg'),
//'permissions' => dcCore::app()->auth->isSuperAdmin(),
]);
},
'packmanBeforeCreatePackage' => function ($module) {
if (Settings::init()->behavior_packman) {
Utils::addLicense($module);
}
},
]);
dcCore::app()->menu[dcAdmin::MENU_PLUGINS]->addItem(
My::name(),
dcCore::app()->adminurl?->get('admin.plugin.' . My::id()),
dcPage::getPF(My::id() . '/icon.svg'),
preg_match('/' . preg_quote((string) dcCore::app()->adminurl?->get('admin.plugin.' . My::id())) . '(&.*)?$/', $_SERVER['REQUEST_URI']),
dcCore::app()->auth?->isSuperAdmin()
);
return true;
}
}

148
src/Config.php 100644
View File

@ -0,0 +1,148 @@
<?php
/**
* @brief licenseBootstrap, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\licenseBootstrap;
use dcCore;
use dcPage;
use dcNsProcess;
use Dotclear\Helper\Html\Form\{
Checkbox,
Div,
Fieldset,
Label,
Legend,
Para,
Radio,
Textarea
};
use Exception;
class Config extends dcNsProcess
{
public static function init(): bool
{
static::$init == defined('DC_CONTEXT_ADMIN')
&& dcCore::app()->auth?->isSuperAdmin();
return static::$init;
}
public static function process(): bool
{
if (!static::$init) {
return false;
}
if (empty($_POST['save'])) {
return true;
}
$s = Settings::init();
# -- Set settings --
try {
$license_name = $_POST['lb_license_name'];
$license_head = Utils::gethead(
$license_name,
!empty($_POST['lb_license_head_' . $license_name]) ? $_POST['lb_license_head_' . $license_name] : ''
);
$s->writeSetting('hide_distrib', !empty($_POST['lb_hide_distrib']));
$s->writeSetting('overwrite', !empty($_POST['lb_overwrite']));
$s->writeSetting('write_full', !empty($_POST['lb_write_full']));
$s->writeSetting('write_php', !empty($_POST['lb_write_php']));
$s->writeSetting('write_js', !empty($_POST['lb_write_js']));
$s->writeSetting('exclude_locales', !empty($_POST['lb_exclude_locales']));
$s->writeSetting('license_name', Utils::getName($license_name));
$s->writeSetting('license_head', Utils::encode($license_head));
dcPage::addSuccessNotice(
__('Configuration has been successfully updated.')
);
dcCore::app()->adminurl?->redirect('admin.plugins', [
'module' => My::id(),
'conf' => '1',
'redir' => dcCore::app()->admin->__get('list')->getRedir(),
]);
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
}
return true;
}
public static function render(): void
{
if (!static::$init) {
return;
}
$s = Settings::init();
$licences = [];
foreach (Utils::getLicenses() as $name) {
$check = false;
$head = Utils::getHead($name);
if ($name == Utils::getName($s->license_name)) {
$check = true;
$head = Utils::getHead($name, Utils::getHead($s->license_name, Utils::decode($s->license_head)));
}
$licences[] = (new Para())->items([
(new Radio(['lb_license_name', 'license_' . $name], $check))->value($name),
(new Label(sprintf(__('License %s:'), $name), Label::OUTSIDE_LABEL_AFTER))->for('license_' . $name)->class('classic'),
]);
$licences[] = (new Para())->items([
(new Textarea('lb_license_head_' . $name, $head))->class('maximal')->cols(50)->rows(10),
]);
}
echo
(new Div())->items([
(new Fieldset())->class('fieldset')->legend((new Legend(__('Files'))))->fields([
// hide_distrib
(new Para())->items([
(new Checkbox('lb_hide_distrib', $s->hide_distrib))->value(1),
(new Label(__('Hide distributed modules from lists'), Label::OUTSIDE_LABEL_AFTER))->for('lb_hide_distrib')->class('classic'),
]),
// overwrite
(new Para())->items([
(new Checkbox('lb_overwrite', $s->overwrite))->value(1),
(new Label(__('Overwrite existing licenses'), Label::OUTSIDE_LABEL_AFTER))->for('lb_overwrite')->class('classic'),
]),
// write_full
(new Para())->items([
(new Checkbox('lb_write_full', $s->write_full))->value(1),
(new Label(__('Add full LICENSE file to module root'), Label::OUTSIDE_LABEL_AFTER))->for('lb_write_full')->class('classic'),
]),
// write_php
(new Para())->items([
(new Checkbox('lb_write_php', $s->write_php))->value(1),
(new Label(__('Add license block to PHP files'), Label::OUTSIDE_LABEL_AFTER))->for('lb_write_php')->class('classic'),
]),
// write_js
(new Para())->items([
(new Checkbox('lb_write_js', $s->write_js))->value(1),
(new Label(__('Add license block to JS files'), Label::OUTSIDE_LABEL_AFTER))->for('lb_write_js')->class('classic'),
]),
// exclude_locales
(new Para())->items([
(new Checkbox('lb_exclude_locales', $s->exclude_locales))->value(1),
(new Label(__('Do not add license block to files from locales folder'), Label::OUTSIDE_LABEL_AFTER))->for('lb_exclude_locales')->class('classic'),
]),
]),
(new Fieldset())->class('fieldset')->legend((new Legend(__('Licenses'))))->fields($licences),
])->render();
}
}

74
src/Install.php 100644
View File

@ -0,0 +1,74 @@
<?php
/**
* @brief licenseBootstrap, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\licenseBootstrap;
use dcCore;
use dcNamespace;
use dcNsProcess;
use Exception;
class Install extends dcNsProcess
{
public static function init(): bool
{
static::$init = defined('DC_CONTEXT_ADMIN')
&& dcCore::app()->newVersion(My::id(), dcCore::app()->plugins->moduleInfo(My::id(), 'version'));
return static::$init;
}
public static function process(): bool
{
if (!static::$init) {
return false;
}
try {
// Upgrade
self::growUp();
return true;
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
return false;
}
}
public static function growUp(): void
{
$current = dcCore::app()->getVersion(My::id());
// Update settings id, ns
if ($current && version_compare($current, '2022.12.22', '<')) {
$record = dcCore::app()->con->select(
'SELECT * FROM ' . dcCore::app()->prefix . dcNamespace::NS_TABLE_NAME . ' ' .
"WHERE setting_ns = 'licenseBootstrap' "
);
$cur = dcCore::app()->con->openCursor(dcCore::app()->prefix . dcNamespace::NS_TABLE_NAME);
while ($record->fetch()) {
if (in_array($record->f('setting_id'), ['license_head'])) {
$cur->setField('setting_value', (string) unserialize(base64_decode($record->f('setting_value'))));
}
$cur->setField('setting_ns', My::id());
$cur->update(
"WHERE setting_id = '" . $record->f('setting_id') . "' and setting_ns = 'licenseBootstrap' " .
'AND blog_id ' . (null === $record->f('blog_id') ? 'IS NULL ' : ("= '" . dcCore::app()->con->escapeStr($record->f('blog_id')) . "' "))
);
$cur->clean();
}
}
}
}

222
src/Manage.php 100644
View File

@ -0,0 +1,222 @@
<?php
/**
* @brief licenseBootstrap, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\licenseBootstrap;
use dcCore;
use dcPage;
use dcThemes;
use dcNsProcess;
use Dotclear\Helper\Html\Html;
use Dotclear\Helper\Html\Form\{
Checkbox,
Hidden,
Label,
Para,
Submit,
Text
};
use Dotclear\Helper\File\Path;
use Dotclear\Helper\Network\Http;
use Exception;
class Manage extends dcNsProcess
{
public static function init(): bool
{
static::$init = defined('DC_CONTEXT_ADMIN')
&& dcCore::app()->auth?->isSuperAdmin();
return static::$init;
}
public static function process(): bool
{
if (!static::$init) {
return false;
}
$type = in_array($_POST['type'] ?? '', ['plugins', 'themes']) ? $_POST['type'] : '';
if (($_POST['action'] ?? '') != 'addlicense'
|| empty($type)
|| empty($_POST['modules'])
|| !is_array($_POST['modules'])
) {
return true;
}
$m = self::loadModules();
# Actions
try {
$modules = array_values($_POST['modules']);
foreach ($modules as $id) {
if (!$m[$type]->moduleExists($id)) {
throw new Exception('No such module');
}
$module = $m[$type]->getModules($id);
$module['id'] = $id;
$module['type'] = $type == 'themes' ? 'theme' : 'plugin';
Utils::addLicense($module);
}
dcPage::addSuccessNotice(
__('License successfully added.')
);
Http::redirect(
empty($_POST['redir']) ?
dcCore::app()->admin->getPageURL() : $_POST['redir']
);
} catch(Exception $e) {
dcCore::app()->error->add($e->getMessage());
}
return true;
}
public static function render(): void
{
if (!static::$init) {
return;
}
$m = self::loadModules();
# Display
dcPage::openModule(
My::name(),
dcPage::jsPageTabs() .
dcPage::jsModuleLoad(My::id() . '/js/licensebootstrap.js') .
# --BEHAVIOR-- licenseBootstrapAdminHeader
dcCore::app()->callBehavior('licenseBootstrapAdminHeader')
);
echo
dcPage::breadcrumb([
__('Plugins') => '',
My::name() => '',
]) .
dcPage::notices();
self::displayModulesList(
$m['plugins']->getModules(),
'plugins',
__('Installed plugins')
);
self::displayModulesList(
$m['themes']->getModules(),
'themes',
__('Installed themes')
);
dcPage::helpBlock('licenseBootstrap');
dcPage::closeModule();
}
private static function displayModulesList(array $modules, string $type, string $title): void
{
if (Settings::init()->hide_distrib) {
$modules = array_diff_key($modules, array_flip(array_values(array_merge(explode(',', DC_DISTRIB_PLUGINS), explode(',', DC_DISTRIB_THEMES)))));
}
echo
'<div class="multi-part" ' .
'id="packman-' . $type . '" title="' . $title . '">' .
'<h3>' . $title . '</h3>';
if (empty($modules)) {
echo
'<p><strong>' . __('There are no modules.') . '</strong></p>' .
'<div>';
return;
}
echo
'<form action="plugin.php" method="post">' .
'<table class="clear"><tr>' .
'<th class="nowrap">' . __('Id') . '</th>' .
'<th class="nowrap">' . __('Version') . '</th>' .
'<th class="nowrap maximal">' . __('Name') . '</th>' .
'<th class="nowrap">' . __('Root') . '</th>' .
'</tr>';
foreach (self::sortModules($modules) as $id => $module) {
echo
'<tr class="line">' .
'<td class="nowrap">' .
(new Para())->items([
(new Checkbox(['modules[]', 'modules_' . Html::escapeHTML($id)], false))->value(Html::escapeHTML($id)),
(new Label(Html::escapeHTML($id), Label::OUTSIDE_LABEL_AFTER))->for('modules_' . Html::escapeHTML($id))->class('classic'),
])->render() .
'</label></td>' .
'<td class="nowrap count">' .
Html::escapeHTML($module['version']) .
'</td>' .
'<td class="nowrap maximal">' .
__(Html::escapeHTML($module['name'])) .
'</td>' .
'<td class="nowrap">' .
dirname((string) Path::real($module['root'], false)) .
'</td>' .
'</tr>';
}
echo
'</table>' .
'<p class="checkboxes-helpers"></p>' .
(new Para())->items([
(new Hidden(['redir'], empty($_REQUEST['redir']) ? '' : Html::escapeHTML($_REQUEST['redir']))),
(new Hidden(['p'], My::id())),
(new Hidden(['type'], $type)),
(new Hidden(['action'], 'addlicense')),
(new Submit('addlicense'))->accesskey('s')->value(__('Add license to selected modules')),
dcCore::app()->formNonce(false),
])->render() .
'</form>' .
'</div>';
}
private static function sortModules(array $modules): array
{
$sorter = [];
foreach ($modules as $id => $module) {
$sorter[$id] = $id;
}
array_multisort($sorter, SORT_ASC, $modules);
return $modules;
}
private static function loadModules(): array
{
if (!(dcCore::app()->themes instanceof dcThemes)) {
dcCore::app()->themes = new dcThemes();
dcCore::app()->themes->loadModules((string) dcCore::app()->blog?->themes_path, null);
}
return [
'themes' => dcCore::app()->themes,
'plugins' => dcCore::app()->plugins,
];
}
}

52
src/My.php 100644
View File

@ -0,0 +1,52 @@
<?php
/**
* @brief licenseBootstrap, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\licenseBootstrap;
use dcCore;
/**
* This module definitions.
*/
class My
{
/** @var string Licenses default templates folder name */
public const TEMPLATE_FOLDER = 'licenses';
/**
* This module id.
*/
public static function id(): string
{
return basename(dirname(__DIR__));
}
/**
* This module name.
*/
public static function name(): string
{
$name = dcCore::app()->plugins->moduleInfo(self::id(), 'name');
return __(is_string($name) ? $name : self::id());
}
/**
* This module path.
*/
public static function path(): string
{
return dirname(__DIR__);
}
}

112
src/Settings.php 100644
View File

@ -0,0 +1,112 @@
<?php
/**
* @brief licenseBootstrap, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\licenseBootstrap;
use dcCore;
class Settings
{
/** @var Settings self instance */
private static $settings;
// Hide distributed modules from lists
public readonly bool $hide_distrib;
// Overwrite existing licence
public readonly bool $overwrite;
// Add complete licence file
public readonly bool $write_full;
// Write license into php files
public readonly bool $write_php;
// Write license into js files
public readonly bool $write_js;
// Exclude locales from license
public readonly bool $exclude_locales;
// License short name
public readonly string $license_name;
// File header licence text
public readonly string $license_head;
// Add LicenceBootstrap to plugin pacKman
public readonly bool $behavior_packman;
/**
* Constructor set up plugin settings
*/
public function __construct()
{
$s = dcCore::app()->blog?->settings->get(My::id());
$this->hide_distrib = (bool) ($s?->get('hide_distrib') ?? false);
$this->overwrite = (bool) ($s?->get('overwrite') ?? false);
$this->write_full = (bool) ($s?->get('write_full') ?? true);
$this->write_php = (bool) ($s?->get('pack_overwrite') ?? true);
$this->write_js = (bool) ($s?->get('pack_filename') ?? false);
$this->exclude_locales = (bool) ($s?->get('exclude_locales') ?? true);
$this->license_name = (string) ($s?->get('license_name') ?? 'gpl2');
$this->license_head = (string) ($s?->get('license_head') ?? Utils::encode(Utils::getHead('gpl2')));
$this->behavior_packman = (bool) ($s?->get('behavior_packman') ?? false);
}
public static function init(): Settings
{
if (!(self::$settings instanceof self)) {
self::$settings = new self();
}
return self::$settings;
}
public function getSetting(string $key): mixed
{
return $this->{$key} ?? null;
}
/**
* Overwrite a plugin settings (in db)
*
* @param string $key The setting ID
* @param mixed $value The setting value
*
* @return bool True on success
*/
public function writeSetting(string $key, mixed $value): bool
{
if (property_exists($this, $key) && settype($value, gettype($this->{$key})) === true) {
dcCore::app()->blog?->settings->get(My::id())->drop($key);
dcCore::app()->blog?->settings->get(My::id())->put($key, $value, gettype($this->{$key}), '', true, true);
return true;
}
return false;
}
/**
* List defined settings keys
*
* @return array The settings keys
*/
public function listSettings(): array
{
return array_diff_key(get_object_vars($this), ['settings' => '']);
}
}

72
src/Uninstall.php 100644
View File

@ -0,0 +1,72 @@
<?php
/**
* @brief licenseBootstrap, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\licenseBootstrap;
use dcCore;
use dcNsProcess;
use Dotclear\Plugin\Uninstaller\Uninstaller;
class Uninstall extends dcNsProcess
{
public static function init(): bool
{
static::$init = defined('DC_CONTEXT_ADMIN');
return static::$init;
}
public static function process(): bool
{
if (!static::$init || !dcCore::app()->plugins->moduleExists('Uninstaller')) {
return false;
}
Uninstaller::instance()
->addUserAction(
'settings',
'delete_all',
My::id()
)
->addUserAction(
'plugins',
'delete',
My::id()
)
->addUserAction(
'versions',
'delete',
My::id()
)
->addDirectAction(
'settings',
'delete_all',
My::id()
)
->addDirectAction(
'plugins',
'delete',
My::id()
)
->addDirectAction(
'versions',
'delete',
My::id()
)
;
// no custom action
return false;
}
}

View File

@ -10,13 +10,19 @@
* @copyright Jean-Christian Denis * @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_CONTEXT_ADMIN')) { declare(strict_types=1);
return null;
}
class licenseBootstrap namespace Dotclear\Plugin\licenseBootstrap;
use dcAuth;
use dcCore;
use Dotclear\Helper\File\Files;
use Dotclear\Helper\File\Path;
use Exception;
class Utils
{ {
protected static $licenses = []; protected static array $licenses = [];
/** /**
* Add license to a module. * Add license to a module.
@ -26,9 +32,13 @@ class licenseBootstrap
* *
* @param array $module Module info * @param array $module Module info
*/ */
public static function addLicense($module) public static function addLicense(array $module): void
{ {
$s = dcCore::app()->blog->settings->get(basename(__DIR__)); if (is_null(dcCore::app()->auth)) {
return;
}
$s = Settings::init();
# --BEHAVIOR-- licenseBootstrapBeforeAddLicense # --BEHAVIOR-- licenseBootstrapBeforeAddLicense
dcCore::app()->callBehavior( dcCore::app()->callBehavior(
@ -36,22 +46,22 @@ class licenseBootstrap
$module $module
); );
if ($s->get('write_full')) { if ($s->write_full) {
licenseBootstrap::writeFullContent( self::writeFullContent(
$s->get('license_name'), $s->license_name,
$module, $module,
$s->get('overwrite') $s->overwrite
); );
} }
licenseBootstrap::writeHeadContent( self::writeHeadContent(
$s->get('license_name'), $s->license_name,
licenseBootstrap::decode($s->get('license_head')), self::decode($s->license_head),
$module, $module,
dcCore::app()->auth, dcCore::app()->auth,
$s->get('overwrite'), $s->overwrite,
$s->get('write_php'), $s->write_php,
$s->get('write_js'), $s->write_js,
$s->get('exclude_locales') $s->exclude_locales
); );
# --BEHAVIOR-- licenseBootstrapAfterAddLicense # --BEHAVIOR-- licenseBootstrapAfterAddLicense
@ -66,20 +76,20 @@ class licenseBootstrap
* *
* @return array List of licenses names * @return array List of licenses names
*/ */
public static function getLicenses() public static function getLicenses(): array
{ {
if (empty(licenseBootstrap::$licenses)) { if (empty(self::$licenses)) {
$file_reg = '/^([a-z0-9]+)\.head\.txt$/'; $file_reg = '/^([a-z0-9]+)\.head\.txt$/';
$res = []; $res = [];
foreach (files::scandir(dirname(__FILE__) . '/licenses/') as $file) { foreach (Files::scandir(__DIR__ . DIRECTORY_SEPARATOR . My::TEMPLATE_FOLDER . DIRECTORY_SEPARATOR) as $file) {
if (preg_match($file_reg, $file, $matches)) { if (preg_match($file_reg, $file, $matches)) {
$res[] = $matches[1]; $res[] = $matches[1];
} }
} }
licenseBootstrap::$licenses = $res; self::$licenses = $res;
} }
return licenseBootstrap::$licenses; return self::$licenses;
} }
/** /**
@ -90,7 +100,7 @@ class licenseBootstrap
* @param string $name License name * @param string $name License name
* @return string License name * @return string License name
*/ */
public static function getName($name = 'gpl2') public static function getName(string $name = 'gpl2'): string
{ {
return in_array($name, self::getLicenses()) ? $name : 'gpl2'; return in_array($name, self::getLicenses()) ? $name : 'gpl2';
} }
@ -102,7 +112,7 @@ class licenseBootstrap
* @param string $content Header content * @param string $content Header content
* @return string Header content * @return string Header content
*/ */
public static function getHead($name = 'gpl2', $content = '') public static function getHead(string $name = 'gpl2', string $content = ''): string
{ {
if (!in_array($name, self::getLicenses())) { if (!in_array($name, self::getLicenses())) {
$name = 'gpl2'; $name = 'gpl2';
@ -119,7 +129,7 @@ class licenseBootstrap
* @param string $name License name * @param string $name License name
* @return string Full license content * @return string Full license content
*/ */
public static function getFull($name = 'gpl2') public static function getFull(string $name = 'gpl2'): string
{ {
return self::getContent($name, 'full'); return self::getContent($name, 'full');
} }
@ -131,7 +141,7 @@ class licenseBootstrap
* @param string $part License part (head or full) * @param string $part License part (head or full)
* @return string License content * @return string License content
*/ */
public static function getContent($name = 'gpl2', $part = 'head') public static function getContent(string $name = 'gpl2', string $part = 'head'): string
{ {
if (!in_array($name, self::getLicenses())) { if (!in_array($name, self::getLicenses())) {
$name = 'gpl2'; $name = 'gpl2';
@ -140,8 +150,8 @@ class licenseBootstrap
$part = 'head'; $part = 'head';
} }
return file_get_contents( return (string) file_get_contents(
dirname(__FILE__) . '/licenses/' . $name . '.' . $part . '.txt' implode(DIRECTORY_SEPARATOR, [__DIR__, My::TEMPLATE_FOLDER, $name . '.' . $part . '.txt'])
); );
} }
@ -151,13 +161,13 @@ class licenseBootstrap
* @param string $name License name * @param string $name License name
* @param string $content License block content * @param string $content License block content
* @param array $module Module info * @param array $module Module info
* @param object $user dcAuth instance * @param dcAuth $user dcAuth instance
* @param boolean $overwrite Overwrite existing license * @param boolean $overwrite Overwrite existing license
* @param boolean $php Write license in PHP * @param boolean $php Write license in PHP
* @param boolean $js Write license in JS * @param boolean $js Write license in JS
* @param boolean $locales Excludes locales folder * @param boolean $locales Excludes locales folder
*/ */
public static function writeHeadContent($name, $content, $module, $user, $overwrite, $php, $js, $locales) public static function writeHeadContent(string $name, string $content, array $module, dcAuth $user, bool $overwrite, bool $php, bool $js, bool $locales): void
{ {
if (!isset($module['root']) || !is_writable($module['root'])) { if (!isset($module['root']) || !is_writable($module['root'])) {
throw new Exception(); throw new Exception();
@ -174,14 +184,14 @@ class licenseBootstrap
continue; continue;
} }
$path = $module['root'] . '/' . $file; $path = $module['root'] . DIRECTORY_SEPARATOR . $file;
$extension = files::getExtension($file); $extension = Files::getExtension($file);
if ($php && $extension == 'php') { if ($php && $extension == 'php') {
file_put_contents( file_put_contents(
$file, $file,
self::replacePhpContent( self::replacePhpContent(
file_get_contents($file), (string) file_get_contents($file),
$license, $license,
$overwrite $overwrite
) )
@ -190,7 +200,7 @@ class licenseBootstrap
file_put_contents( file_put_contents(
$file, $file,
self::replaceJsContent( self::replaceJsContent(
file_get_contents($file), (string) file_get_contents($file),
$license, $license,
$overwrite $overwrite
) )
@ -206,17 +216,17 @@ class licenseBootstrap
* @param array $module Module info * @param array $module Module info
* @param boolean $overwrite Overwrite existing license * @param boolean $overwrite Overwrite existing license
*/ */
public static function writeFullContent($name, $module, $overwrite) public static function writeFullContent(string $name, array $module, bool $overwrite): void
{ {
if (!isset($module['root']) || !is_writable($module['root'])) { if (!isset($module['root']) || !is_writable($module['root'])) {
throw new Exception(); throw new Exception();
} }
if (file_exists($module['root'] . '/LICENSE') && !$overwrite) { if (file_exists($module['root'] . DIRECTORY_SEPARATOR . 'LICENSE') && !$overwrite) {
return null; return;
} }
file_put_contents( file_put_contents(
$module['root'] . '/LICENSE', $module['root'] . DIRECTORY_SEPARATOR . 'LICENSE',
self::getFull($name) self::getFull($name)
); );
} }
@ -229,9 +239,9 @@ class licenseBootstrap
* @param boolean $overwrite Overwrite existing license * @param boolean $overwrite Overwrite existing license
* @return string File content * @return string File content
*/ */
protected static function replacePhpContent($content, $license, $overwrite) protected static function replacePhpContent(string $content, string $license, bool $overwrite): string
{ {
$clean = preg_replace( $clean = (string) preg_replace(
'/((# -- BEGIN LICENSE BLOCK ([-]+))(.*?)' . '/((# -- BEGIN LICENSE BLOCK ([-]+))(.*?)' .
'(# -- END LICENSE BLOCK ([-]+))([\n|\r\n]+))/msi', '(# -- END LICENSE BLOCK ([-]+))([\n|\r\n]+))/msi',
'', '',
@ -242,7 +252,7 @@ class licenseBootstrap
return $content; return $content;
} }
return preg_replace( return (string) preg_replace(
'/(\<\?php)/', '/(\<\?php)/',
'<?php' . '<?php' .
"\r\n# -- BEGIN LICENSE BLOCK ----------------------------------\r\n" . "\r\n# -- BEGIN LICENSE BLOCK ----------------------------------\r\n" .
@ -263,7 +273,7 @@ class licenseBootstrap
* @param boolean $overwrite Overwrite existing license * @param boolean $overwrite Overwrite existing license
* @return string File content * @return string File content
*/ */
protected static function replaceJsContent($content, $license, $overwrite) protected static function replaceJsContent(string $content, string $license, bool $overwrite): string
{ {
$clean = preg_replace( $clean = preg_replace(
'/((\/\* -- BEGIN LICENSE BLOCK ([-]+))(.*?)' . '/((\/\* -- BEGIN LICENSE BLOCK ([-]+))(.*?)' .
@ -290,10 +300,10 @@ class licenseBootstrap
* *
* @param string $content License content * @param string $content License content
* @param array $module Module info * @param array $module Module info
* @param object $user User info * @param dcAuth $user User info
* @return string License content * @return string License content
*/ */
protected static function replaceInfo($content, $module, $user) protected static function replaceInfo(string $content, array $module, dcAuth $user): string
{ {
return str_replace( return str_replace(
[ [
@ -330,10 +340,10 @@ class licenseBootstrap
* @param array $res Ignore * @param array $res Ignore
* @return array List of files * @return array List of files
*/ */
protected static function getModuleFiles($path, $dir = '', $res = []) protected static function getModuleFiles(string $path, string $dir = '', array $res = []): array
{ {
$path = path::real($path); $path = Path::real($path);
if (!is_dir($path) || !is_readable($path)) { if ($path === false || !is_dir($path) || !is_readable($path)) {
return []; return [];
} }
@ -341,34 +351,38 @@ class licenseBootstrap
$dir = $path; $dir = $path;
} }
$files = files::scandir($path); $files = Files::scandir($path);
foreach ($files as $file) { foreach ($files as $file) {
if (substr($file, 0, 1) == '.') { if (substr($file, 0, 1) == '.') {
continue; continue;
} }
if (is_dir($path . '/' . $file)) { if (is_dir($path . DIRECTORY_SEPARATOR . $file)) {
$res = self::getModuleFiles( $res = self::getModuleFiles(
$path . '/' . $file, $path . DIRECTORY_SEPARATOR . $file,
$dir . '/' . $file, $dir . DIRECTORY_SEPARATOR . $file,
$res $res
); );
} else { } else {
$res[] = empty($dir) ? $file : $dir . '/' . $file; $res[] = empty($dir) ? $file : $dir . DIRECTORY_SEPARATOR . $file;
} }
} }
return $res; return $res;
} }
public static function encode($a) public static function encode(string $a): string
{ {
return json_encode($a); $r = json_encode($a);
return $r === false ? '' : $r;
} }
public static function decode($a) public static function decode(string $a): string
{ {
return json_decode($a, true); $r = json_decode($a, true);
return $r === false ? '' : $r;
} }
} }