improve/inc/module/phpheader.php

294 lines
8.6 KiB
PHP
Raw Normal View History

2021-09-01 23:35:23 +00:00
<?php
/**
* @brief improve, a plugin for Dotclear 2
2021-11-01 21:17:44 +00:00
*
2021-09-01 23:35:23 +00:00
* @package Dotclear
2021-09-06 21:36:50 +00:00
* @subpackage Plugin
2021-11-01 21:17:44 +00:00
*
2021-09-01 23:35:23 +00:00
* @author Jean-Christian Denis and contributors
2021-11-01 21:17:44 +00:00
*
2021-09-01 23:35:23 +00:00
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace plugins\improve\module;
/* improve */
use plugins\improve\action;
/* clearbricks */
use form;
use html;
/* php */
use Exception;
/**
* Improve action module php header
*/
class phpheader extends action
2021-09-01 23:35:23 +00:00
{
2021-11-07 23:59:52 +00:00
/** @var string Exemple of header */
private static $exemple = <<<EOF
@brief %module_id%, a %module_type% for Dotclear 2
@package Dotclear
@subpackage \u%module_type%
@author %module_author%
@copyright %user_cn%
@copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
EOF;
2021-11-07 23:59:52 +00:00
/** @var array<string> Allowed bloc replacement */
2021-09-01 23:35:23 +00:00
private $bloc_wildcards = [
'%year%',
'%module_id%',
'%module_name%',
'%module_author%',
'%module_type%',
'%user_cn%',
'%user_name%',
'%user_email%',
'%user_url%',
2021-09-01 23:35:23 +00:00
];
2021-11-07 23:59:52 +00:00
/** @var array Allowed action for header */
private $action_bloc = [];
/** @var string Parsed bloc */
private $bloc = '';
/** @var boolean Stop parsing files */
private $stop_scan = false;
2021-09-01 23:35:23 +00:00
/** @var string Settings bloc content */
private $bloc_content = '';
2021-09-01 23:35:23 +00:00
protected function init(): bool
{
$this->setProperties([
'id' => 'phpheader',
'name' => __('PHP header'),
'description' => __('Add or remove phpdoc header bloc from php file'),
'priority' => 340,
'configurator' => true,
'types' => ['plugin', 'theme'],
2021-09-01 23:35:23 +00:00
]);
$this->action_bloc = [
__('Do nothing') => 0,
__('Add bloc if it does not exist') => 'create',
2021-11-01 21:17:44 +00:00
__('Add and overwrite bloc') => 'overwrite',
2021-09-01 23:35:23 +00:00
__('Overwrite bloc only if it exists') => 'replace',
__('Remove existing bloc header') => 'remove',
2021-09-01 23:35:23 +00:00
];
$bloc_content = $this->getSetting('bloc_content');
$this->bloc_content = is_string($bloc_content) ? $bloc_content : '';
2021-09-01 23:35:23 +00:00
return true;
}
public function isConfigured(): bool
{
return !empty($this->getSetting('bloc_action')) || !empty($this->getSetting('remove_old'));
2021-09-01 23:35:23 +00:00
}
public function configure($url): ?string
{
if (!empty($_POST['save'])) {
$this->setSettings([
2021-09-01 23:35:23 +00:00
'bloc_action' => !empty($_POST['bloc_action']) ? $_POST['bloc_action'] : '',
'bloc_content' => !empty($_POST['bloc_content']) ? $_POST['bloc_content'] : '',
'remove_old' => !empty($_POST['remove_old']),
'exclude_locales' => !empty($_POST['exclude_locales']),
2021-09-01 23:35:23 +00:00
]);
$this->redirect($url);
}
return '
<p><label for="bloc_action">' . __('Action:') . '</label>' .
form::combo('bloc_action', $this->action_bloc, $this->getSetting('bloc_action')) . '
2021-09-01 23:35:23 +00:00
</p>
<p><label class="classic" for="remove_old">' .
form::checkbox('remove_old', 1, $this->getSetting('remove_old')) . ' ' .
2021-09-01 23:35:23 +00:00
__('Remove old style bloc header (using #)') .
'</label></p>
<p><label class="classic" for="exclude_locales">' .
form::checkbox('exclude_locales', 1, $this->getSetting('exclude_locales')) . ' ' .
2021-09-01 23:35:23 +00:00
__('Do not add bloc to files from "locales" and "libs" folder') .
'</label></p>
<p>' . __('Bloc content:') . '</p>
<p class="area">' .
form::textarea('bloc_content', 50, 10, html::escapeHTML($this->bloc_content)) . '
2021-11-01 21:17:44 +00:00
</p><p class="form-note">' .
2021-09-01 23:35:23 +00:00
sprintf(
2021-11-01 21:17:44 +00:00
__('You can use wildcards %s'),
2021-09-01 23:35:23 +00:00
'%year%, %module_id%, %module_name%, %module_author%, %module_type%, %user_cn%, %user_name%, %user_email%, %user_url%'
) . '<br />' . __('Do not put structural elements to the begining of lines.') . '</p>' .
2021-11-01 21:17:44 +00:00
'<div class="fieldset box"><h4>' . __('Exemple') . '</h4><pre class="code">' . self::$exemple . '</pre></div>';
2021-09-01 23:35:23 +00:00
}
public function openModule(): ?bool
2021-09-01 23:35:23 +00:00
{
$bloc = trim($this->bloc_content);
2021-09-01 23:35:23 +00:00
2021-11-07 23:59:52 +00:00
if (empty($bloc)) {
$this->setWarning(__('bloc is empty'));
return null;
}
$bloc = trim(str_replace("\r\n", "\n", $bloc));
try {
$this->bloc = (string) preg_replace_callback(
// use \u in bloc content for first_upper_case
'/(\\\u([a-z]{1}))/',
function ($str) {
return ucfirst($str[2]);
},
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'),
2021-11-07 23:59:52 +00:00
],
(string) $bloc
)
);
$this->setSuccess(__('Prepare header info'));
return null;
} catch (Exception $e) {
$this->setError(__('Failed to parse bloc'));
return null;
}
2021-09-01 23:35:23 +00:00
}
public function openDirectory(): ?bool
2021-09-01 23:35:23 +00:00
{
2021-11-01 21:17:44 +00:00
$skipped = $this->stop_scan;
2021-09-01 23:35:23 +00:00
$this->stop_scan = false;
if (!empty($this->getSetting('exclude_locales')) && preg_match('/\/(locales|libs)(\/.*?|)$/', $this->path_full)) {
if (!$skipped) {
$this->setSuccess(__('Skip directory'));
}
2021-09-01 23:35:23 +00:00
$this->stop_scan = true;
}
return null;
}
public function readFile(&$content): ?bool
2021-09-01 23:35:23 +00:00
{
2021-11-01 21:17:44 +00:00
if ($this->stop_scan || $this->path_extension != 'php' || $this->hasError()) {
2021-09-01 23:35:23 +00:00
return null;
}
if (!empty($this->getSetting('remove_old'))) {
2021-09-01 23:35:23 +00:00
$content = $this->deleteOldBloc($content);
}
if (empty($this->getSetting('bloc_action'))) {
2021-09-01 23:35:23 +00:00
return null;
}
$clean = $this->deleteDocBloc($content);
if ($this->getSetting('bloc_action') == 'remove') {
2021-09-01 23:35:23 +00:00
$content = $clean;
return null;
}
if ($content != $clean && $this->getSetting('bloc_action') == 'create') {
2021-09-01 23:35:23 +00:00
return null;
}
if ($content == $clean && $this->getSetting('bloc_action') == 'replace') {
2021-09-01 23:35:23 +00:00
return null;
}
$content = $this->writeDocBloc($clean);
return true;
}
2021-11-07 23:59:52 +00:00
/**
* Write bloc content in file content
*
* @param string $content Old content
* @return string New content
*/
private function writeDocBloc(string $content): string
2021-09-01 23:35:23 +00:00
{
$res = preg_replace(
2021-09-01 23:35:23 +00:00
'/^(\<\?php[\n|\r\n]+)/',
2021-11-01 10:36:35 +00:00
'<?php' . "\n/**\n * " . str_replace("\n", "\n * ", trim($this->bloc)) . "\n */\n",
2021-09-01 23:35:23 +00:00
$content,
1,
$count
2021-09-01 23:35:23 +00:00
);
2021-11-07 23:59:52 +00:00
if ($count && $res) {
2021-11-01 10:36:35 +00:00
$res = str_replace("\n * \n", "\n *\n", $res);
$this->setSuccess(__('Write new doc bloc content'));
}
2021-11-01 21:17:44 +00:00
2021-11-07 23:59:52 +00:00
return (string) $res;
2021-09-01 23:35:23 +00:00
}
2021-11-07 23:59:52 +00:00
/**
* Delete bloc content in file content
*
* @param string $content Old content
* @return string New content
*/
private function deleteDocBloc(string $content): string
2021-09-01 23:35:23 +00:00
{
$res = preg_replace(
'/^(\<\?php\s*[\n|\r\n]{0,1}\s*\/\*\*.*?\s*\*\/\s*[\n|\r\n]+)/msi',
2021-09-01 23:35:23 +00:00
"<?php\n",
$content,
-1,
$count
2021-09-01 23:35:23 +00:00
);
if ($count) {
$this->setSuccess(__('Delete old doc bloc content'));
}
2021-11-01 21:17:44 +00:00
2021-11-07 23:59:52 +00:00
return (string) $res;
2021-09-01 23:35:23 +00:00
}
2021-11-07 23:59:52 +00:00
/**
* Delete old style bloc content in file content
*
* @param string $content Old content
* @return string New content
*/
private function deleteOldBloc(string $content): string
2021-09-01 23:35:23 +00:00
{
$res = preg_replace(
2021-09-01 23:35:23 +00:00
'/((# -- BEGIN LICENSE BLOCK ([-]+))(.*?)(# -- END LICENSE BLOCK ([-]+))([\n|\r\n]{1,}))/msi',
2021-11-01 21:17:44 +00:00
'',
$content,
-1,
$count
2021-09-01 23:35:23 +00:00
);
if ($count) {
$this->setSuccess(__('Delete old style bloc content'));
}
2021-11-01 21:17:44 +00:00
2021-11-07 23:59:52 +00:00
return (string) $res;
2021-09-01 23:35:23 +00:00
}
2021-11-01 21:17:44 +00:00
}