diff --git a/_admin.php b/_admin.php index e0aae8f..ace60c8 100644 --- a/_admin.php +++ b/_admin.php @@ -17,10 +17,11 @@ $core->addBehavior('adminDashboardFavorites', ['ImproveBehaviors', 'adminDashboa $core->addBehavior('improveAddAction', ['ImproveActionDcstore', 'create']); $core->addBehavior('improveAddAction', ['ImproveActionEndoffile', 'create']); +$core->addBehavior('improveAddAction', ['ImproveActionGitshields', 'create']); +$core->addBehavior('improveAddAction', ['ImproveActionLicensefile', 'create']); //$core->addBehavior('improveAddAction', ['ImproveActionLicense', 'create']); $core->addBehavior('improveAddAction', ['ImproveActionNewline', 'create']); $core->addBehavior('improveAddAction', ['ImproveActionPhpheader', 'create']); -$core->addBehavior('improveAddAction', ['ImproveActionGitshields', 'create']); $core->addBehavior('improveAddAction', ['ImproveActionTab', 'create']); $core->addBehavior('improveAddAction', ['ImproveActionZip', 'create']); diff --git a/_prepend.php b/_prepend.php index be80fe9..4785a56 100644 --- a/_prepend.php +++ b/_prepend.php @@ -21,10 +21,10 @@ $__autoload['ImproveAction'] = $d . 'class.improve.action.php'; $__autoload['ImproveActionDcstore'] = $d . 'lib.improve.action.dcstore.php'; $__autoload['ImproveActionEndoffile'] = $d . 'lib.improve.action.php'; -//$__autoload['ImproveActionLicense'] = $d . 'lib.improve.action.license.php'; +$__autoload['ImproveActionGitshields'] = $d . 'lib.improve.action.gitshields.php'; +$__autoload['ImproveActionLicensefile'] = $d . 'lib.improve.action.licensefile.php'; $__autoload['ImproveActionNewline'] = $d . 'lib.improve.action.php'; $__autoload['ImproveActionPhpheader'] = $d . 'lib.improve.action.phpheader.php'; -$__autoload['ImproveActionGitshields'] = $d . 'lib.improve.action.gitshields.php'; $__autoload['ImproveActionTab'] = $d . 'lib.improve.action.php'; $__autoload['ImproveActionZip'] = $d . 'lib.improve.action.zip.php'; $__autoload['ImproveZipFileZip'] = $d . 'lib.improve.action.zip.php'; \ No newline at end of file diff --git a/inc/lib.improve.action.license.php b/inc/lib.improve.action.license.php deleted file mode 100644 index b47fda3..0000000 --- a/inc/lib.improve.action.license.php +++ /dev/null @@ -1,331 +0,0 @@ -setProperties([ - 'id' => 'license', - 'name' => __('Fix license'), - 'desc' => __('Add or remove license bloc at the begining of file and full license file to module root'), - 'priority' => 320, - 'config' => true, - 'types' => ['plugin', 'theme'] - ]); - - $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', - __('Massachusetts Institute of Technolog mit') => 'mit' - ]; - $this->action_full = [ - __('Do nothing') => 0, - __('Add file if it does not exist') => 'create', - __('Add file even if it exists') => 'overwrite', - __('Remove license file') => 'remove' - ]; - $this->action_bloc = [ - __('Do nothing') => 0, - __('Add bloc if it does not exist') => 'create', - __('Add bloc even if it exists') => 'overwrite' , - __('Remove old style bloc') => 'remove' - ]; - - return true; - } - - public function isConfigured(): bool - { - return true; - } - - public function configure($url): ?string - { - if (!empty($_POST['save'])) { - $this->setPreferences([ - 'action_version' => !empty($_POST['action_version']) ? $_POST['action_version'] : '', - 'action_full' => !empty($_POST['action_full']) ? $_POST['action_full'] : '', - 'action_php' => !empty($_POST['action_php']) ? $_POST['action_php'] : '', - 'action_js' => !empty($_POST['action_js']) ? $_POST['action_js'] : '', - 'exclude_locales' => !empty($_POST['lb_exclude_locales']), - 'use_custom_bloc' => !empty($_POST['lb_use_custom_bloc']), - 'custom_bloc' => !empty($_POST['lb_custom_bloc']) ? $_POST['lb_custom_bloc'] : '' - ]); - $this->redirect($url); - } - - return ' -

' . __('This manages old style bloc, it is better to use modern phpDdoc style.') . '

-

' . - form::combo('action_version', $this->action_version, $this->getPreference('action_version')) . ' -

- -

' . - form::combo('action_full', $this->action_full, $this->getPreference('action_full')) . ' -

- -

' . - form::combo('action_php', $this->action_bloc, $this->getPreference('action_php')) . ' -

- -

' . - form::combo('action_js', $this->action_bloc, $this->getPreference('action_js')) . ' -

- -

- -

- -

' . - form::textarea('lb_custom_bloc', 50, 10, html::escapeHTML($this->getPreference('custom_bloc'))) . ' -

' . - 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 - ); - } -} \ No newline at end of file diff --git a/inc/lib.improve.action.licensefile.php b/inc/lib.improve.action.licensefile.php new file mode 100644 index 0000000..cf1e14b --- /dev/null +++ b/inc/lib.improve.action.licensefile.php @@ -0,0 +1,139 @@ +setProperties([ + 'id' => 'license', + 'name' => __('Fix license file'), + 'desc' => __('Add or remove full license file to module root'), + 'priority' => 330, + 'config' => true, + 'types' => ['plugin', 'theme'] + ]); + $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', + __('Massachusetts Institute of Technolog mit') => 'mit' + ]; + $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', + __('Remove license files') => 'remove' + ]; + return true; + } + + public function isConfigured(): bool + { + return true; + } + + public function configure($url): ?string + { + if (!empty($_POST['save'])) { + $this->setPreferences([ + 'action_version' => !empty($_POST['action_version']) ? $_POST['action_version'] : '', + 'action_full' => !empty($_POST['action_full']) ? $_POST['action_full'] : '' + ]); + $this->redirect($url); + } + + return ' +

' . + form::combo('action_version', $this->action_version, $this->getPreference('action_version')) . ' +

+ +

' . + form::combo('action_full', $this->action_full, $this->getPreference('action_full')) . + '

'; + } + + public function openModule(string $module_type, array $module_info): ?bool + { + $this->type = $module_type; + $this->module = $module_info; + + if (in_array($this->getPreference('action_full'), ['remove', 'full','overwrite'])) { + $this->deleteFullLicense(($this->getPreference('action_full') == 'overwrite')); + } + if (in_array($this->getPreference('action_full'), ['create', 'overwrite', 'full'])) { + if (empty($this->getPreference('action_version'))) { + self::notice(__('no full license type selected'), false); + } else { + $this->writeFullLicense(); + } + } + return null; + } + + private function writeFullLicense() + { + 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', str_replace("\r\n","\n",$full)); + } catch (Exception $e) { + self::notice(__('failed to write full license')); + + return null; + } + return true; + } + + private function deleteFullLicense($only_one = false) + { + foreach(self::fileExists($this->module['root']) as $file) { + if ($only_one && $file != 'license') { + continue; + } + if (!files::isDeletable($this->module['root'] . '/' . $file)) { + self::notice(sprintf(__('full license is not deletable (%s)'), $file), false); + } + if (!@unlink($this->module['root'] . '/' . $file)) { + self::notice(sprintf(__('failed to delete full license (%s)'), $file), false); + } + } + return true; + } + + private static function fileExists($root) + { + $existing = []; + foreach(self::$license_filenames as $file) { + if (file_exists($root . '/' . strtolower($file))) { + $existing[] = strtolower($file); + } + } + return $existing; + } +} \ No newline at end of file diff --git a/inc/license/gpl2.head.txt b/inc/license/gpl2.head.txt deleted file mode 100644 index 996c417..0000000 --- a/inc/license/gpl2.head.txt +++ /dev/null @@ -1,8 +0,0 @@ -This file is part of %module_id%, a %module_type% for Dotclear 2. - -Copyright (c) %year% %user_cn% and contributors -%user_email% - -Licensed under the GPL version 2.0 license. -A copy of this license is available in LICENSE file or at -http://www.gnu.org/licenses/old-licenses/gpl-2.0.html \ No newline at end of file diff --git a/inc/license/gpl3.head.txt b/inc/license/gpl3.head.txt deleted file mode 100644 index 9136ec9..0000000 --- a/inc/license/gpl3.head.txt +++ /dev/null @@ -1,8 +0,0 @@ -This file is part of %module_id%, a %module_type% for Dotclear 2. - -Copyright (c) %year% %user_cn% and contributors -%user_email% - -Licensed under the GPL version 3.0 license. -A copy of this license is available in LICENSE file or at -http://www.gnu.org/licenses/gpl-3.0.html \ No newline at end of file diff --git a/inc/license/lgpl3.head.txt b/inc/license/lgpl3.head.txt deleted file mode 100644 index c897daf..0000000 --- a/inc/license/lgpl3.head.txt +++ /dev/null @@ -1,8 +0,0 @@ -This file is part of %module_id%, a %module_type% for Dotclear 2. - -Copyright (c) %year% %user_cn% and contributors -%user_email% - -Licensed under the LGPL version 3.0 license. -A copy of this license is available in LICENSE file or at -http://www.gnu.org/licenses/lgpl-3.0.html \ No newline at end of file diff --git a/inc/license/mit.head.txt b/inc/license/mit.head.txt deleted file mode 100644 index dbc4224..0000000 --- a/inc/license/mit.head.txt +++ /dev/null @@ -1,7 +0,0 @@ -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. \ No newline at end of file