forked from pluginsGLPI/formcreator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.php
More file actions
386 lines (327 loc) · 12 KB
/
setup.php
File metadata and controls
386 lines (327 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
<?php
/**
*
* ---------------------------------------------------------------------
* Formcreator is a plugin which allows creation of custom forms of
* easy access.
* ---------------------------------------------------------------------
* LICENSE
*
* This file is part of Formcreator.
*
* Formcreator is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Formcreator is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Formcreator. If not, see <http://www.gnu.org/licenses/>.
* ---------------------------------------------------------------------
* @copyright Copyright © 2011 - 2018-2021 Teclib'
* @license http://www.gnu.org/licenses/gpl.txt GPLv3+
* @link https://github.com/pluginsGLPI/formcreator/
* @link https://pluginsglpi.github.io/formcreator/
* @link http://plugins.glpi-project.org/#/plugin/formcreator
* ---------------------------------------------------------------------
*/
use Glpi\Plugin\Formcreator\EOLInfo;
use Glpi\Plugin\Formcreator\Install;
use Glpi\Plugin\Hooks;
/** @var array $CFG_GLPI */
global $CFG_GLPI;
// Version of the plugin (major.minor.bugfix)
define('PLUGIN_FORMCREATOR_VERSION', '3.0.0');
// Schema version of this version (major.minor only)
define('PLUGIN_FORMCREATOR_SCHEMA_VERSION', '3.0');
// is or is not an official release of the plugin
define('PLUGIN_FORMCREATOR_IS_OFFICIAL_RELEASE', true);
// Minimal GLPI version, inclusive
define ('PLUGIN_FORMCREATOR_GLPI_MIN_VERSION', '11.0.0');
// Maximum GLPI version, exclusive (ignored if PLUGIN_FORMCREATOR_IS_OFFICIAL_RELEASE == false)
define ('PLUGIN_FORMCREATOR_GLPI_MAX_VERSION', '11.0.99');
// Plugin is now migration-only (End of Life for functionality)
define('PLUGIN_FORMCREATOR_IS_EOL', true);
// Use a constant for web directory to avoid deprecated method calls
define('FORMCREATOR_ROOTDOC', '/plugins/formcreator');
// Advanced features for Formcreator
define('PLUGIN_FORMCREATOR_ADVANCED_VALIDATION', 'advform');
/**
* Define the plugin's version and informations
*
* @return array [name, version, author, homepage, license, minGlpiVersion]
*/
function plugin_version_formcreator() {
plugin_formcreator_savePreviousVersion();
return [
'name' => 'Formcreator End-of-Life Updater',
'version' => PLUGIN_FORMCREATOR_VERSION,
'author' => 'Teclib\'',
'homepage' => 'https://github.com/pluginsGLPI/formcreator',
'license' => 'GPLv2',
'requirements' => [
'glpi' => [
'min' => PLUGIN_FORMCREATOR_GLPI_MIN_VERSION,
'max' => PLUGIN_FORMCREATOR_GLPI_MAX_VERSION
]
]
];
}
/**
* Initialize all classes and generic variables of the plugin
* VERSION 3.0.0 - MIGRATION ONLY (END OF LIFE)
*/
function plugin_init_formcreator() {
/** @var array $CFG_GLPI */
global $CFG_GLPI;
// Always set permanent hooks for migration and cleanup
plugin_formcreator_permanent_hook();
$plugin = new Plugin();
if (!$plugin->isActivated('formcreator')) {
return;
}
// This version is always EOL, so always load migration-only functionality
plugin_formcreator_init_migration_only();
// Register plugin classes
plugin_formcreator_registerClasses();
// Load plugin hooks for menu and interface elements
plugin_formcreator_hook();
}
/**
* Initialize migration-only functionality for EOL version
*/
function plugin_formcreator_init_migration_only() {
// Load only essential classes for migration
spl_autoload_register('plugin_formcreator_autoload');
// Display EOL warning in admin interface
if (Session::haveRight('config', UPDATE)) {
plugin_formcreator_show_eol_warning();
}
// Register minimal classes needed for migration
Plugin::registerClass(Install::class);
// Add admin menu for migration status only
if (Session::haveRight('config', UPDATE)) {
/** @var array $PLUGIN_HOOKS */
global $PLUGIN_HOOKS;
$PLUGIN_HOOKS['menu_entry']['formcreator'] = 'front/migration_status.php';
}
}
/**
* Show End-of-Life warning message
*/
function plugin_formcreator_show_eol_warning() {
if (isset($_SESSION['formcreator_eol_warning_shown'])) {
return; // Show only once per session
}
$message = sprintf(
__('Formcreator v%s is now End-of-Life (EOL). This version only provides migration to GLPI 11 native forms. After successful migration, consider uninstalling this plugin and use GLPI\'s native form system.', 'formcreator'),
PLUGIN_FORMCREATOR_VERSION
);
Session::addMessageAfterRedirect($message, true, WARNING);
$_SESSION['formcreator_eol_warning_shown'] = true;
}
/**
* Legacy initialization (preserved for reference, should not be used in v3.0.0)
*/
function plugin_formcreator_init_legacy() {
// This function is disabled in EOL version
// All functional features have been removed
return;
}
/**
* Tells if helpdesk replacement is enabled for the current user
* DISABLED in EOL version
*
* @return boolean
*/
function plugin_formcreator_replaceHelpdesk() {
// Always return false in EOL version - no helpdesk replacement
return false;
}
/**
* Generate unique id for form based on server name, glpi directory and basetime
**/
function plugin_formcreator_getUuid() {
//encode uname -a, ex Linux localhost 2.4.21-0.13mdk #1 Fri Mar 14 15:08:06 EST 2003 i686
$serverSubSha1 = substr(sha1(php_uname('a')), 0, 8);
// encode script current dir, ex : /var/www/glpi_X
$dirSubSha1 = substr(sha1(__FILE__), 0, 8);
return uniqid("$serverSubSha1-$dirSubSha1-", true);
}
/**
* Retrieve an item from the database
*
* @param CommonDBTM $item instance of CommonDBTM object
* @param string $field field of object's table to search in
* @param mixed $value value to search in provided field
*
* @return int|false ID of the item if found, false otherwise
*/
function plugin_formcreator_getFromDBByField(CommonDBTM $item, $field = '', $value = '') {
/** @var \DBmysql $DB */
global $DB;
// != 0 because 0 is consider as empty
if (!$item instanceof Entity
&& (strlen($value) == 0
|| $value === 0)) {
return false;
}
$value = $DB->escape($value);
$found = $item->getFromDBByRequest([
'WHERE' => [$item::getTable() . '.' . $field => $value],
'LIMIT' => 1
]);
if ($found) {
return $item->getID();
} else {
return false;
}
}
/**
* Autoloader for Formcreator classes
* @param string $classname
*/
function plugin_formcreator_autoload($classname) {
// Handle new namespace classes: Glpi\Plugin\Formcreator\*
if (strpos($classname, 'Glpi\\Plugin\\Formcreator\\') === 0) {
$class_name = str_replace('Glpi\\Plugin\\Formcreator\\', '', $classname);
$filename = __DIR__ . '/src/' . $class_name . '.php';
if (is_readable($filename) && is_file($filename)) {
include_once($filename);
return true;
}
}
// Legacy compatibility for old PluginFormcreator classes (if any still exist)
if (strpos($classname, 'PluginFormcreator') === 0) {
// useful only for installer GLPi autoloader already handles inc/ folder
$filename = __DIR__ . '/inc/' . strtolower(str_replace('PluginFormcreator', '', $classname)). '.class.php';
if (is_readable($filename) && is_file($filename)) {
include_once($filename);
return true;
}
}
}
/**
* Show the last SQL error, logs its backtrace and dies
* @param Migration $migration
*/
function plugin_formcreator_upgrade_error(Migration $migration) {
/** @var \DBmysql $DB */
global $DB;
$error = $DB->error() ?: '';
$migration->log($error . "\n" . Toolbox::backtrace($error, '', ['Toolbox::backtrace()']), false);
die($error . "<br><br> Please, check migration log");
}
/**
* Permanent hooks, must be set even when the plugin is disabled
* SIMPLIFIED for EOL version
*
* @return void
*/
function plugin_formcreator_permanent_hook(): void {
/** @var array $PLUGIN_HOOKS */
global $PLUGIN_HOOKS;
// Minimal hooks for migration only
$PLUGIN_HOOKS[Hooks::ITEM_ADD]['formcreator'] = [];
$PLUGIN_HOOKS[Hooks::PRE_ITEM_UPDATE]['formcreator'] = [];
$PLUGIN_HOOKS[Hooks::ITEM_UPDATE]['formcreator'] = [];
$PLUGIN_HOOKS[Hooks::ITEM_DELETE]['formcreator'] = [];
$PLUGIN_HOOKS[Hooks::ITEM_RESTORE]['formcreator'] = [];
$PLUGIN_HOOKS[Hooks::ITEM_PURGE]['formcreator'] = [];
$PLUGIN_HOOKS[Hooks::PRE_ITEM_PURGE]['formcreator'] = [];
// No timeline actions in EOL version
$PLUGIN_HOOKS[Hooks::TIMELINE_ACTIONS]['formcreator'] = [];
// No transfer hooks in EOL version
$PLUGIN_HOOKS[Hooks::ITEM_TRANSFER]['formcreator'] = [];
}
/**
* Hooks to run when the plugin is active
* SIMPLIFIED for EOL version
*
* @return void
*/
function plugin_formcreator_hook(): void {
/**
* @var array $PLUGIN_HOOKS
*/
global $PLUGIN_HOOKS;
// No CSS or JS in EOL version
// No dashboard cards in EOL version
// No menu entries in EOL version - only migration interface in admin
if (Session::getLoginUserID() === false) {
return;
}
// No helpdesk menu in EOL version
// No assistance requests menu in EOL version
// Minimal hooks for EOL version
$PLUGIN_HOOKS['use_massive_action']['formcreator'] = 0;
// Basic menu entry for migration status only
if (Session::haveRight('config', UPDATE)) {
$PLUGIN_HOOKS['menu_entry']['formcreator'] = 'front/migration_status.php';
// Add EOL information button to plugin tile
$PLUGIN_HOOKS['menu_toadd']['formcreator']['tools'] = EOLInfo::class;
// Alternative: Add a direct link to EOL documentation
$PLUGIN_HOOKS['plugin_info_display']['formcreator'] = 'front/eol_info.php';
// Display EOL warning on central dashboard
$PLUGIN_HOOKS['display_central']['formcreator'] = [EOLInfo::class, 'displayCentralEOLWarning'];
}
}
function plugin_formcreator_registerClasses() {
// EOL version - minimal class registration for migration only
// Only register core classes needed for migration
Plugin::registerClass(Install::class);
// Register EOL information class for admin menu
Plugin::registerClass(EOLInfo::class);
// No entity configuration or form classes in EOL version
// No field classes in EOL version
// No notification classes in EOL version
}
function plugin_formcreator_redirect() {
// EOL version - no redirections or helpdesk replacement
// This functionality has been removed in the migration-only version
return;
}
function plugin_formcreator_options() {
return [
Plugin::OPTION_AUTOINSTALL_DISABLED => true,
];
}
/**
* Get the path to the empty SQL schema file
*
* @return string
*/
function plugin_formcreator_getSchemaPath(string $version = ''): string {
if (empty($version)) {
$version = PLUGIN_FORMCREATOR_VERSION;
}
// Drop suffixes for alpha, beta, rc versions
$matches = [];
preg_match('/^(\d+\.\d+\.\d+)/', $version, $matches);
$version = $matches[1];
return Plugin::getPhpDir('formcreator') . "/install/mysql/plugin_formcreator_{$version}_empty.sql";
}
// Installation functions are defined in hook.php
/**
* Detect a versin change and save the previous version in the DB
*
* Used to proceed a DB sanity check before an upgrade
* @see PluginFormcreatorInstall::upgrade
* @see PluginFormcreatorInstall::checkSchema
*
* @return void
*/
function plugin_formcreator_savePreviousVersion(): void {
$plugin = new Plugin();
$plugin->getFromDBbyDir('formcreator');
$oldVersion = $plugin->fields['version'] ?? null;
if ($oldVersion !== null && $oldVersion != PLUGIN_FORMCREATOR_VERSION) {
Config::setConfigurationValues('formcreator', [
'previous_version' => $oldVersion,
]);
}
}