licenseBootstrap/inc/class.license.bootstrap.php

375 lines
10 KiB
PHP
Raw Normal View History

2021-08-17 21:25:45 +00:00
<?php
2021-09-02 19:43:35 +00:00
/**
* @brief licenseBootstrap, a plugin for Dotclear 2
2022-11-20 21:36:32 +00:00
*
2021-09-02 19:43:35 +00:00
* @package Dotclear
* @subpackage Plugin
2022-11-20 21:36:32 +00:00
*
2021-09-02 19:43:35 +00:00
* @author Jean-Christian Denis
2022-11-20 21:36:32 +00:00
*
2021-09-02 19:43:35 +00:00
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
2021-08-17 21:25:45 +00:00
if (!defined('DC_CONTEXT_ADMIN')) {
2021-08-17 21:46:17 +00:00
return null;
2021-08-17 21:25:45 +00:00
}
class licenseBootstrap
{
2022-11-20 21:36:32 +00:00
protected static $licenses = [];
2021-08-17 21:46:17 +00:00
/**
* Add license to a module.
*
* Note that you must check if module exists
* before calling this method.
2022-11-20 21:36:32 +00:00
*
2021-08-17 21:46:17 +00:00
* @param array $module Module info
*/
2022-11-20 21:36:32 +00:00
public static function addLicense($module)
2021-08-17 21:46:17 +00:00
{
2022-12-22 21:28:04 +00:00
$s = dcCore::app()->blog->settings->get(basename(__DIR__));
2021-08-17 21:46:17 +00:00
# --BEHAVIOR-- licenseBootstrapBeforeAddLicense
2022-11-20 21:36:32 +00:00
dcCore::app()->callBehavior(
2021-08-17 21:46:17 +00:00
'licenseBootstrapBeforeAddLicense',
$module
);
2022-12-22 21:59:31 +00:00
if ($s->get('write_full')) {
2021-08-17 21:46:17 +00:00
licenseBootstrap::writeFullContent(
2022-12-22 21:59:31 +00:00
$s->get('license_name'),
2021-08-17 21:46:17 +00:00
$module,
2022-12-22 21:59:31 +00:00
$s->get('overwrite')
2021-08-17 21:46:17 +00:00
);
}
licenseBootstrap::writeHeadContent(
2022-12-22 21:59:31 +00:00
$s->get('license_name'),
licenseBootstrap::decode($s->get('license_head')),
2021-08-17 21:46:17 +00:00
$module,
2022-11-20 21:36:32 +00:00
dcCore::app()->auth,
2022-12-22 21:59:31 +00:00
$s->get('overwrite'),
$s->get('write_php'),
$s->get('write_js'),
$s->get('exclude_locales')
2021-08-17 21:46:17 +00:00
);
# --BEHAVIOR-- licenseBootstrapAfterAddLicense
2022-11-20 21:36:32 +00:00
dcCore::app()->callBehavior(
2021-08-17 21:46:17 +00:00
'licenseBootstrapAfterAddLicense',
$module
);
}
/**
* Get available licenses.
2022-11-20 21:36:32 +00:00
*
2021-08-17 21:46:17 +00:00
* @return array List of licenses names
*/
public static function getLicenses()
{
if (empty(licenseBootstrap::$licenses)) {
$file_reg = '/^([a-z0-9]+)\.head\.txt$/';
2022-11-20 21:36:32 +00:00
$res = [];
2021-08-17 21:46:17 +00:00
foreach (files::scandir(dirname(__FILE__) . '/licenses/') as $file) {
if (preg_match($file_reg, $file, $matches)) {
$res[] = $matches[1];
}
}
licenseBootstrap::$licenses = $res;
}
return licenseBootstrap::$licenses;
}
/**
* Get license name.
*
* Check requested license name and return existing one.
2022-11-20 21:36:32 +00:00
*
2021-08-17 21:46:17 +00:00
* @param string $name License name
* @return string License name
*/
2022-11-20 21:36:32 +00:00
public static function getName($name = 'gpl2')
2021-08-17 21:46:17 +00:00
{
return in_array($name, self::getLicenses()) ? $name : 'gpl2';
}
2021-09-02 19:43:35 +00:00
2021-08-17 21:46:17 +00:00
/**
* Get license header.
2022-11-20 21:36:32 +00:00
*
2021-08-17 21:46:17 +00:00
* @param string $name License name
* @param string $content Header content
* @return string Header content
*/
2022-11-20 21:36:32 +00:00
public static function getHead($name = 'gpl2', $content = '')
2021-08-17 21:46:17 +00:00
{
if (!in_array($name, self::getLicenses())) {
2022-11-20 21:36:32 +00:00
$name = 'gpl2';
2021-08-17 21:46:17 +00:00
$content = '';
}
2022-11-20 21:36:32 +00:00
return empty($content) ?
2021-08-17 21:46:17 +00:00
self::getContent($name, 'head') : $content;
}
/**
* Get full license.
2022-11-20 21:36:32 +00:00
*
2021-08-17 21:46:17 +00:00
* @param string $name License name
* @return string Full license content
*/
2022-11-20 21:36:32 +00:00
public static function getFull($name = 'gpl2')
2021-08-17 21:46:17 +00:00
{
return self::getContent($name, 'full');
}
/**
* Get original license content.
2022-11-20 21:36:32 +00:00
*
2021-08-17 21:46:17 +00:00
* @param string $name License name
* @param string $part License part (head or full)
* @return string License content
*/
2022-11-20 21:36:32 +00:00
public static function getContent($name = 'gpl2', $part = 'head')
2021-08-17 21:46:17 +00:00
{
if (!in_array($name, self::getLicenses())) {
$name = 'gpl2';
}
2022-11-20 21:36:32 +00:00
if (!in_array($part, ['head', 'full'])) {
2021-08-17 21:46:17 +00:00
$part = 'head';
}
return file_get_contents(
dirname(__FILE__) . '/licenses/' . $name . '.' . $part . '.txt'
);
}
/**
* Write license block into module files headers
2022-11-20 21:36:32 +00:00
*
2021-08-17 21:46:17 +00:00
* @param string $name License name
* @param string $content License block content
* @param array $module Module info
* @param object $user dcAuth instance
* @param boolean $overwrite Overwrite existing license
* @param boolean $php Write license in PHP
* @param boolean $js Write license in JS
* @param boolean $locales Excludes locales folder
*/
public static function writeHeadContent($name, $content, $module, $user, $overwrite, $php, $js, $locales)
{
if (!isset($module['root']) || !is_writable($module['root'])) {
throw new Exception();
}
$license = self::replaceInfo(
self::getHead($name, $content),
$module,
$user
);
2022-11-20 21:36:32 +00:00
foreach (self::getModuleFiles($module['root']) as $file) {
2021-08-17 21:46:17 +00:00
if ($locales && preg_match('/(\.lang\.php|libs)/', $file)) {
continue;
}
2022-11-20 21:36:32 +00:00
$path = $module['root'] . '/' . $file;
2021-08-17 21:46:17 +00:00
$extension = files::getExtension($file);
if ($php && $extension == 'php') {
file_put_contents(
$file,
self::replacePhpContent(
file_get_contents($file),
$license,
$overwrite
)
);
2022-11-20 21:36:32 +00:00
} elseif ($js && $extension == 'js') {
2021-08-17 21:46:17 +00:00
file_put_contents(
$file,
self::replaceJsContent(
file_get_contents($file),
$license,
$overwrite
)
);
}
}
}
/**
* Write full license file
2022-11-20 21:36:32 +00:00
*
2021-08-17 21:46:17 +00:00
* @param string $name License name
* @param array $module Module info
* @param boolean $overwrite Overwrite existing license
*/
public static function writeFullContent($name, $module, $overwrite)
{
if (!isset($module['root']) || !is_writable($module['root'])) {
throw new Exception();
}
if (file_exists($module['root'] . '/LICENSE') && !$overwrite) {
return null;
}
file_put_contents(
$module['root'] . '/LICENSE',
self::getFull($name)
);
}
/**
* Replace license block in PHP file
2022-11-20 21:36:32 +00:00
*
2021-08-17 21:46:17 +00:00
* @param string $content File content
* @param string $license License content
* @param boolean $overwrite Overwrite existing license
* @return string File content
*/
protected static function replacePhpContent($content, $license, $overwrite)
{
$clean = preg_replace(
'/((# -- BEGIN LICENSE BLOCK ([-]+))(.*?)' .
'(# -- END LICENSE BLOCK ([-]+))([\n|\r\n]+))/msi',
'',
$content
);
if ($clean != $content && !$overwrite) {
return $content;
}
return preg_replace(
'/(\<\?php)/',
'<?php' .
"\r\n# -- BEGIN LICENSE BLOCK ----------------------------------\r\n" .
"#\r\n" .
2022-11-20 21:36:32 +00:00
'# ' . str_replace("\n", "\n# ", trim($license)) .
2021-08-17 21:46:17 +00:00
"\r\n#" .
"\r\n# -- END LICENSE BLOCK ------------------------------------\r\n",
$clean,
1
);
}
/**
* Replace license block in JS files
2022-11-20 21:36:32 +00:00
*
2021-08-17 21:46:17 +00:00
* @param string $content File content
* @param string $license License content
* @param boolean $overwrite Overwrite existing license
* @return string File content
*/
protected static function replaceJsContent($content, $license, $overwrite)
{
$clean = preg_replace(
'/((\/\* -- BEGIN LICENSE BLOCK ([-]+))(.*?)' .
'(\* -- END LICENSE BLOCK ([-]+)\*\/)([\n|\r\n]+))/msi',
'',
$content
);
if ($clean != $content && !$overwrite) {
return $content;
}
2022-11-20 21:36:32 +00:00
return
2021-08-17 21:46:17 +00:00
"/* -- BEGIN LICENSE BLOCK ----------------------------------\r\n" .
" *\r\n" .
2022-11-20 21:36:32 +00:00
' * ' . str_replace("\n", "\n * ", trim($license)) .
2021-08-17 21:46:17 +00:00
"\r\n *" .
"\r\n * -- END LICENSE BLOCK ------------------------------------*/\r\n\r\n" .
$clean;
}
/**
* Replace info in license
2022-11-20 21:36:32 +00:00
*
2022-12-22 21:59:31 +00:00
* @param string $content License content
* @param array $module Module info
* @param object $user User info
* @return string License content
2021-08-17 21:46:17 +00:00
*/
protected static function replaceInfo($content, $module, $user)
{
return str_replace(
2022-11-20 21:36:32 +00:00
[
2021-08-17 21:46:17 +00:00
'%year%',
'%module_id%',
'%module_name%',
'%module_author%',
'%module_type%',
'%user_cn%',
'%user_name%',
'%user_email%',
2022-11-20 21:36:32 +00:00
'%user_url%',
],
[
2021-08-17 21:46:17 +00:00
date('Y'),
$module['id'],
$module['name'],
$module['author'],
$module['type'],
$user->getInfo('user_cn'),
$user->getinfo('user_name'),
$user->getInfo('user_email'),
2022-11-20 21:36:32 +00:00
$user->getInfo('user_url'),
],
2021-08-17 21:46:17 +00:00
$content
);
}
/**
* Get list of module files
2022-11-20 21:36:32 +00:00
*
2021-08-17 21:46:17 +00:00
* @param string $path Path to scan
* @param string $dir Ignore
* @param array $res Ignore
* @return array List of files
*/
2022-11-20 21:36:32 +00:00
protected static function getModuleFiles($path, $dir = '', $res = [])
2021-08-17 21:46:17 +00:00
{
$path = path::real($path);
if (!is_dir($path) || !is_readable($path)) {
2022-11-20 21:36:32 +00:00
return [];
2021-08-17 21:46:17 +00:00
}
if (!$dir) {
$dir = $path;
}
2021-09-02 19:43:35 +00:00
2021-08-17 21:46:17 +00:00
$files = files::scandir($path);
2021-09-02 19:43:35 +00:00
2022-11-20 21:36:32 +00:00
foreach ($files as $file) {
2021-08-17 21:46:17 +00:00
if (substr($file, 0, 1) == '.') {
continue;
}
if (is_dir($path . '/' . $file)) {
$res = self::getModuleFiles(
2022-11-20 21:36:32 +00:00
$path . '/' . $file,
$dir . '/' . $file,
2021-08-17 21:46:17 +00:00
$res
);
2022-11-20 21:36:32 +00:00
} else {
2021-08-17 21:46:17 +00:00
$res[] = empty($dir) ? $file : $dir . '/' . $file;
}
}
return $res;
}
public static function encode($a)
{
2022-12-22 21:45:38 +00:00
return json_encode($a);
2021-08-17 21:46:17 +00:00
}
2021-09-02 19:43:35 +00:00
2021-08-17 21:46:17 +00:00
public static function decode($a)
{
2022-12-22 21:45:38 +00:00
return json_decode($a, true);
2021-08-17 21:46:17 +00:00
}
2022-11-20 21:36:32 +00:00
}