' .
sprintf(
__('You can use wildcards %s') ,
'%year%, %module_id%, %module_name%, %module_author%, %module_type%, %user_cn%, %user_name%, %user_email%, %user_url%'
) . ' ' . __('Do not put structural elements to the begining of lines.') . '
';
}
public function openModule(string $module_type, array $module_info): ?bool
{
$this->type = $module_type;
$this->module = $module_info;
$this->replaceInfo();
if (in_array($this->getPreference('action_full'), ['create', 'overwrite'])) {
if (empty($this->getPreference('action_version'))) {
self::notice(__('no full license type selected'), false);
} else {
$this->writeFullLicense();
}
}
return null;
}
public function openDirectory(string $path): ?bool
{
$this->stop_scan = false;
if (!empty($this->getPreference('exclude_locales')) && preg_match('/\/(locales|libs)(\/.*?|)$/', $path)) {
$this->stop_scan = true;
}
return null;
}
public function readFile($path, $extension, &$content): ?bool
{
if (
$this->stop_scan || !in_array($extension, ['php', 'js']) || self::hasNotice()
|| empty($this->getPreference('action_php')) && empty($this->getPreference('action_js'))
) {
return null;
}
if (
!empty($this->getPreference('action_version'))
|| !empty($this->getPreference('use_custom_bloc')) && !empty($this->getPreference('custom_bloc'))
) {
if ($extension == 'php' && in_array($this->getPreference('action_php'), ['create', 'overwrite'])) {
$content = $this->writePhpBloc($content);
}
if ($extension == 'js' && in_array($this->getPreference('action_js'), ['create', 'overwrite'])) {
$content = $this->writeJsBloc($content);
}
}
if ($extension == 'php' && $this->getPreference('action_php') == 'remove') {
$content = $this->deletePhpBloc($content);
}
if ($extension == 'js' && $this->getPreference('action_js') == 'remove') {
$content = $this->deleteJsBloc($content);
}
return true;
}
public function closeModule(string $module_type, array $module_info): ?bool
{
if ('remove' == $this->getPreference('action_full')) {
$this->deleteFullLicense();
}
return null;
}
private function replaceInfo()
{
$bloc = '';
if (!empty($this->getPreference('custom_bloc')) && !empty($this->getPreference('use_custom_bloc'))) {
$bloc = $this->getPreference('custom_bloc');
} elseif ($this->getPreference('version')) {
try {
$bloc = file_get_contents(
dirname(__FILE__) . '/license/' . $this->getPreference('version') . '.head.txt'
);
} catch (Exception $e) {
self::notice(__('failed to load license bloc'));
return null;
}
}
if (empty($bloc)) {
self::notice(__('license bloc is empty'), false);
return null;
}
try {
$this->bloc = str_replace(
$this->bloc_wildcards,
[
date('Y'),
$this->module['id'],
$this->module['name'],
$this->module['author'],
$this->module['type'],
$this->core->auth->getInfo('user_cn'),
$this->core->auth->getinfo('user_name'),
$this->core->auth->getInfo('user_email'),
$this->core->auth->getInfo('user_url')
],
$bloc
);
} catch (Exception $e) {
self::notice(__('failed to parse license bloc'));
return null;
}
}
private function writeFullLicense()
{
if (file_exists($this->module['root'] . '/LICENSE') && $this->getPreference('action_full') != 'overwrite') {
self::notice(__('full license already exists'), false);
}
try {
$full = file_get_contents(dirname(__FILE__) . '/license/' . $this->getPreference('action_version') . '.full.txt');
if (empty($full)) {
self::notice(__('failed to load full license'));
return null;
}
files::putContent($this->module['root'] . '/LICENSE', $full);
} catch (Exception $e) {
self::notice(__('failed to write full license'));
return null;
}
return true;
}
private function deleteFullLicense()
{
if (!files::isDeletable($this->module['root'] . '/LICENSE')) {
self::notice(__('full license is not deletable'), false);
return null;
}
if (!@unlink($this->module['root'] . '/LICENSE')) {
self::notice(__('failed to delete full license'), false);
return null;
}
return true;
}
private function writePhpBloc($content)
{
$clean = $this->deletePhpBloc($content);
if ($clean != $content && 'overwrite' != $this->getPreference('action_php')) {
return $content;
}
return preg_replace(
'/(\<\?php)/',
'bloc)).
"\n#" .
"\n# -- END LICENSE BLOCK ------------------------------------\n",
$clean,
1
);
}
private function deletePhpBloc($content)
{
return preg_replace(
'/((# -- BEGIN LICENSE BLOCK ([-]+))(.*?)(# -- END LICENSE BLOCK ([-]+))([\n|\r\n]+))/msi',
"\n",
$content
);
}
private function writeJsBloc($content)
{
$clean = $this->deleteJsBloc($content);
if ($clean != $content && 'overwrite' == $this->getPreference('action_js')) {
return $content;
}
return
"/* -- BEGIN LICENSE BLOCK ----------------------------------\n" .
" *\n" .
' * ' . str_replace("\n", "\n * ", trim($this->bloc)).
"\n *" .
"\n * -- END LICENSE BLOCK ------------------------------------*/\n\n" .
$clean;
}
private function deleteJsBloc($content)
{
return preg_replace(
'/((\/\* -- BEGIN LICENSE BLOCK ([-]+))(.*?)(\* -- END LICENSE BLOCK ([-]+)\*\/)([\n|\r\n]+))/msi',
'',
$content
);
}
}