butterfly/setup.php
Claude b409b65895 v2.3.1: corrige campo de CSS personalizado travado (Monaco criado oculto)
O editor era criado dentro de uma div com d-none: com display:none o
Monaco mede 0x0 e nasce sem área editável, enquanto o textarea ficava
oculto e disabled — resultado, campo impossível de editar.

- container passa a ser exibido ANTES do create (dimensões reais),
  mais layout() explícito e automaticLayout para relayout no resize;
- _force_default_lang evita o caminho de clonagem de linguagem do core
  (que quebra se a linguagem base não estiver registrada);
- textarea deixa de ser disabled: fica só oculto, com o valor do editor
  copiado nos eventos submit E formdata (form.submit() programático não
  dispara submit; formdata cobre os dois casos) — sem risco de perder a
  edição silenciosamente no POST;
- se o create falhar, rollback para o textarea comum.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-31 13:11:16 -03:00

150 lines
5.1 KiB
PHP

<?php
define('PLUGIN_BUTTERFLY_VERSION', '2.3.1');
// Minimal GLPI version, inclusive
define("PLUGIN_BUTTERFLY_MIN_GLPI_VERSION", "11.0.0");
// Maximum GLPI version, exclusive
define("PLUGIN_BUTTERFLY_MAX_GLPI_VERSION", "11.0.99");
$folder = basename(__DIR__);
if (strtolower($folder) !== "butterfly") {
$msg = sprintf("Please, rename the plugin folder \"%s\" to \"butterfly\"", $folder);
Session::addMessageAfterRedirect($msg, true, ERROR);
}
/**
* Carrega a classe License do Mindplace caso o autoload ainda não tenha rodado.
* Necessário porque o plugin pode inicializar antes do mindplace (KB-PLUGIN-013).
*/
function plugin_butterfly_load_license()
{
if (class_exists('\GlpiPlugin\Mindplace\License')) {
return;
}
foreach ([
GLPI_ROOT . '/plugins/mindplace/src/License.php',
GLPI_ROOT . '/marketplace/mindplace/src/License.php',
] as $path) {
if (file_exists($path)) {
include_once $path;
return;
}
}
}
// Init the hooks of the plugins -Needed
function plugin_init_butterfly()
{
global $PLUGIN_HOOKS, $CFG_GLPI;
$PLUGIN_HOOKS['csrf_compliant']['butterfly'] = true;
// Register firewall strategies for legacy scripts even before installation
if (class_exists(\Glpi\Http\Firewall::class)) {
\Glpi\Http\Firewall::addPluginStrategyForLegacyScripts(
'butterfly',
'#^/front/picture\.send\.php$#',
\Glpi\Http\Firewall::STRATEGY_NO_CHECK
);
\Glpi\Http\Firewall::addPluginStrategyForLegacyScripts(
'butterfly',
'#^/front/internal\.css\.php$#',
\Glpi\Http\Firewall::STRATEGY_NO_CHECK
);
\Glpi\Http\Firewall::addPluginStrategyForLegacyScripts(
'butterfly',
'#^/front/internal\.js\.php$#',
\Glpi\Http\Firewall::STRATEGY_NO_CHECK
);
}
// ──────────────────────────────────────────────────────────────────
// KILL SWITCH — aborta silenciosamente se a licença Mindtek for inválida
// (KB-PLUGIN-013; camada 2 — a camada 1 é o check_config)
// ──────────────────────────────────────────────────────────────────
plugin_butterfly_load_license();
if (!class_exists('\GlpiPlugin\Mindplace\License') || !\GlpiPlugin\Mindplace\License::isValid()) {
return;
}
$plugin = new Plugin();
if ($plugin->isInstalled('butterfly') && $plugin->isActivated('butterfly')) {
$autoload = __DIR__ . '/vendor/autoload.php';
if (file_exists($autoload)) {
include_once $autoload;
}
;
// Sem barra invertida inicial: isPluginItemType() rejeita '\GlpiPlugin\...'
// e registerClass retornaria false silenciosamente (::class nunca tem o \)
Plugin::registerClass(\GlpiPlugin\Butterfly\Config::class, [
'addtabon' => ['Config']
]);
// Register config page only when installed & activated
$PLUGIN_HOOKS['config_page']['butterfly'] = '../../front/config.form.php?itemtype=Config&glpi_tab=' . rawurlencode('GlpiPlugin\Butterfly\Config') . '$1';
$PLUGIN_HOOKS['display_login']['butterfly'] = "plugin_butterfly_display_login";
// GLPI 11 appends ?v=<plugin files version> itself; the endpoints
// handle config-change cache busting via their own timestamp redirect
$PLUGIN_HOOKS["add_css"]['butterfly'] = 'front/internal.css.php';
$PLUGIN_HOOKS["add_javascript"]['butterfly'] = 'front/internal.js.php';
$CFG_GLPI['javascript']['config']['config'][] = 'tinymce';
$CFG_GLPI['javascript']['config']['config'][] = 'monaco';
}
}
// Get the name and the version of the plugin - Needed
function plugin_version_butterfly()
{
return [
'name' => t_butterfly('Butterfly'),
'version' => PLUGIN_BUTTERFLY_VERSION,
'author' => 'Rodolpho Lopes',
'homepage' => 'https://github.com/rodolpholopes',
'license' => 'GPL v2+',
'minGlpiVersion' => PLUGIN_BUTTERFLY_MIN_GLPI_VERSION,
'requirements' => [
'glpi' => [
'min' => PLUGIN_BUTTERFLY_MIN_GLPI_VERSION,
'max' => PLUGIN_BUTTERFLY_MAX_GLPI_VERSION,
]
]
];
}
// Optional : check prerequisites before install : may print errors or add to message after redirect
function plugin_butterfly_check_prerequisites()
{
if (version_compare(GLPI_VERSION, PLUGIN_BUTTERFLY_MIN_GLPI_VERSION, 'lt')) {
echo "This plugin requires GLPI >= " . PLUGIN_BUTTERFLY_MIN_GLPI_VERSION;
return false;
} else {
return true;
}
}
function plugin_butterfly_check_config($verbose = false)
{
plugin_butterfly_load_license();
if (!class_exists('\GlpiPlugin\Mindplace\License') || !\GlpiPlugin\Mindplace\License::isValid()) {
if ($verbose && !isCommandLine()) {
echo "<div class='alert alert-danger'>Licença Mindtek inativa ou Mind Place ausente. O plugin foi desativado por segurança.</div>";
}
return false;
}
return true;
}
function t_butterfly($str)
{
return __($str, 'butterfly');
}