forked from pluginsGLPI/formcreator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.php
390 lines (343 loc) · 14.6 KB
/
setup.php
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
387
388
389
390
<?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/>.
* ---------------------------------------------------------------------
* @author Thierry Bugier
* @author Jérémy Moreau
* @copyright Copyright © 2011 - 2019 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
* ---------------------------------------------------------------------
*/
global $CFG_GLPI;
// Version of the plugin
define('PLUGIN_FORMCREATOR_VERSION', '2.8.3');
// Schema version of this version
define('PLUGIN_FORMCREATOR_SCHEMA_VERSION', '2.8');
// 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', '9.3.0');
// Maximum GLPI version, exclusive
define ('PLUGIN_FORMCREATOR_GLPI_MAX_VERSION', '9.5');
define('FORMCREATOR_ROOTDOC', $CFG_GLPI['root_doc'] . '/plugins/formcreator');
/**
* Define the plugin's version and informations
*
* @return Array [name, version, author, homepage, license, minGlpiVersion]
*/
function plugin_version_formcreator() {
$glpiVersion = rtrim(GLPI_VERSION, '-dev');
if (!method_exists('Plugins', 'checkGlpiVersion') && version_compare($glpiVersion, PLUGIN_FORMCREATOR_GLPI_MIN_VERSION, 'lt')) {
echo 'This plugin requires GLPI >= ' . PLUGIN_FORMCREATOR_GLPI_MIN_VERSION;
return false;
}
$requirements = [
'name' => 'Form Creator',
'version' => PLUGIN_FORMCREATOR_VERSION,
'author' => '<a href="http://www.teclib.com">Teclib\'</a>',
'homepage' => 'https://github.com/pluginsGLPI/formcreator',
'license' => '<a href="../plugins/formcreator/LICENSE" target="_blank">GPLv2</a>',
'requirements' => [
'glpi' => [
'min' => PLUGIN_FORMCREATOR_GLPI_MIN_VERSION,
]
]
];
if (PLUGIN_FORMCREATOR_IS_OFFICIAL_RELEASE) {
// This is not a development version
$requirements['requirements']['glpi']['max'] = PLUGIN_FORMCREATOR_GLPI_MAX_VERSION;
}
return $requirements;
}
/**
* Check plugin's prerequisites before installation
*
* @return boolean
*/
function plugin_formcreator_check_prerequisites() {
return true;
}
/**
* Check plugin's config before activation (if needed)
*
* @param string $verbose Set true to show all messages (false by default)
* @return boolean
*/
function plugin_formcreator_check_config($verbose = false) {
return true;
}
/**
* Initialize all classes and generic variables of the plugin
*/
function plugin_init_formcreator() {
global $PLUGIN_HOOKS, $CFG_GLPI;
// Add specific CSS
$PLUGIN_HOOKS['add_css']['formcreator'][] = "css/styles.css";
// Hack for vertical display
if (isset($CFG_GLPI['layout_excluded_pages'])) {
array_push($CFG_GLPI['layout_excluded_pages'], "targetticket.form.php");
}
// Set the plugin CSRF compliance (required since GLPI 0.84)
$PLUGIN_HOOKS['csrf_compliant']['formcreator'] = true;
// Can assign FormAnswer to tickets
$PLUGIN_HOOKS['assign_to_ticket']['formcreator'] = true;
array_push($CFG_GLPI["ticket_types"], PluginFormcreatorFormAnswer::class);
array_push($CFG_GLPI["document_types"], PluginFormcreatorFormAnswer::class);
// hook to update issues when an operation occurs on a ticket
$PLUGIN_HOOKS['item_add']['formcreator'] = [
'Ticket' => 'plugin_formcreator_hook_add_ticket'
];
$PLUGIN_HOOKS['item_update']['formcreator'] = [
'Ticket' => 'plugin_formcreator_hook_update_ticket'
];
$PLUGIN_HOOKS['item_delete']['formcreator'] = [
'Ticket' => 'plugin_formcreator_hook_delete_ticket'
];
$PLUGIN_HOOKS['item_restore']['formcreator'] = [
'Ticket' => 'plugin_formcreator_hook_restore_ticket'
];
$PLUGIN_HOOKS['item_purge']['formcreator'] = [
'Ticket' => 'plugin_formcreator_hook_purge_ticket'
];
$plugin = new Plugin();
if ($plugin->isInstalled('formcreator') && $plugin->isActivated('formcreator')) {
spl_autoload_register('plugin_formcreator_autoload');
if (isset($_SESSION['glpiactiveentities_string'])) {
// Redirect to helpdesk replacement
if (strpos($_SERVER['REQUEST_URI'], "front/helpdesk.public.php") !== false) {
if (!isset($_POST['newprofile']) && !isset($_GET['active_entity'])) {
// Not changing profile or active entity
if (isset($_SESSION['glpiactiveprofile']['interface'])
&& isset($_SESSION['glpiactive_entity'])) {
// Interface and active entity are set in session
if (plugin_formcreator_replaceHelpdesk()) {
Html::redirect($CFG_GLPI["root_doc"]."/plugins/formcreator/front/wizard.php");
}
}
}
}
if (strpos($_SERVER['REQUEST_URI'], "front/ticket.form.php") !== false) {
if (plugin_formcreator_replaceHelpdesk()) {
Html::redirect($CFG_GLPI["root_doc"] . '/plugins/formcreator/front/issue.form.php?id=' . $_GET['id'] . '&sub_itemtype=Ticket');
}
}
// Massive Action definition
$PLUGIN_HOOKS['use_massive_action']['formcreator'] = 1;
// Load menu entries if user is logged in and if he has access to at least one form
if (isset($_SESSION['glpiID'])) {
// If user have acces to one form or more, add link
if (PluginFormcreatorForm::countAvailableForm() > 0) {
$PLUGIN_HOOKS['menu_toadd']['formcreator']['helpdesk'] = 'PluginFormcreatorFormlist';
}
// Add a link in the main menu plugins for technician and admin panel
$PLUGIN_HOOKS['menu_entry']['formcreator'] = 'front/formlist.php';
// Config page
$links = [];
if (Session::haveRight('entity', UPDATE)) {
$PLUGIN_HOOKS['config_page']['formcreator'] = 'front/form.php';
$PLUGIN_HOOKS['menu_toadd']['formcreator']['admin'] = 'PluginFormcreatorForm';
$links['config'] = '/plugins/formcreator/front/form.php';
$links['add'] = '/plugins/formcreator/front/form.form.php';
}
$img = '<img src="' . $CFG_GLPI['root_doc'] . '/plugins/formcreator/pics/check.png"
title="' . __('Forms waiting for validation', 'formcreator') . '" alt="Waiting forms list" />';
$links[$img] = '/plugins/formcreator/front/formanswer.php';
// Set options for pages (title, links, buttons...)
$links['search'] = '/plugins/formcreator/front/formlist.php';
$PLUGIN_HOOKS['submenu_entry']['formcreator']['options'] = [
'config' => ['title' => __('Setup'),
'page' => '/plugins/formcreator/front/form.php',
'links' => $links],
'options' => ['title' => _n('Form', 'Forms', 2, 'formcreator'),
'links' => $links],
];
}
// Load JS and CSS files if we are on a page which need them
if (strpos($_SERVER['REQUEST_URI'], 'plugins/formcreator') !== false
|| strpos($_SERVER['REQUEST_URI'], 'central.php') !== false
|| isset($_SESSION['glpiactiveprofile']) &&
$_SESSION['glpiactiveprofile']['interface'] == 'helpdesk') {
$PLUGIN_HOOKS['add_css']['formcreator'][] = 'lib/pqselect/pqselect.min.css';
$PLUGIN_HOOKS['add_javascript']['formcreator'][] = 'lib/pqselect/pqselect.min.js';
// Add specific JavaScript
$PLUGIN_HOOKS['add_javascript']['formcreator'][] = 'js/scripts.js.php';
}
if (strpos($_SERVER['REQUEST_URI'], 'plugins/formcreator/front/targetticket.form.php') !== false) {
if (version_compare(PluginFormcreatorCommon::getGlpiVersion(), 9.4) >= 0 || $CFG_GLPI['use_rich_text']) {
Html::requireJs('tinymce');
}
}
if (strpos($_SERVER['REQUEST_URI'], 'helpdesk') !== false
|| strpos($_SERVER['REQUEST_URI'], 'central.php') !== false
|| strpos($_SERVER['REQUEST_URI'], 'formcreator/front/formlist.php') !== false
|| strpos($_SERVER['REQUEST_URI'], 'formcreator/front/wizard.php') !== false) {
$PLUGIN_HOOKS['add_javascript']['formcreator'][] = 'lib/slinky/assets/js/jquery.slinky.js';
}
Plugin::registerClass(PluginFormcreatorForm::class, ['addtabon' => Central::class]);
// Load field class and all its method to manage fields
Plugin::registerClass(PluginFormcreatorFields::class);
// Notification
Plugin::registerClass(PluginFormcreatorFormAnswer::class, [
'notificationtemplates_types' => true
]);
Plugin::registerClass(PluginFormcreatorEntityconfig::class, ['addtabon' => Entity::class]);
}
if (strpos($_SERVER['REQUEST_URI'], 'plugins/formcreator') !== false
|| strpos($_SERVER['REQUEST_URI'], 'central.php') !== false
|| isset($_SESSION['glpiactiveprofile']) &&
$_SESSION['glpiactiveprofile']['interface'] == 'helpdesk') {
// Add specific JavaScript
$PLUGIN_HOOKS['add_javascript']['formcreator'][] = 'js/scripts.js.php';
}
}
}
/**
* Encode special chars
*
* @param String $string The string to encode
* @return String The encoded string
*/
function plugin_formcreator_encode($string, $mode_legacy = true) {
if (!is_string($string)) {
return $string;
}
if (!$mode_legacy) {
$string = Html::clean(Html::entity_decode_deep($string));
$string = preg_replace('/\\r\\n/', ' ', $string);
$string = preg_replace('/\\n/', ' ', $string);
$string = preg_replace('/\\\\r\\\\n/', ' ', $string);
$string = preg_replace('/\\\\n/', ' ', $string);
$string = Toolbox::stripslashes_deep($string);
$string = Toolbox::addslashes_deep($string);
} else {
$string = stripcslashes($string);
$string = html_entity_decode($string, ENT_QUOTES, 'UTF-8');
$string = str_replace(''', "'", $string);
$string = htmlentities($string, ENT_QUOTES, 'UTF-8');
}
return $string;
}
/**
* Encode special chars
*
* @param String $string The string to encode
* @return String The encoded string
*/
function plugin_formcreator_decode($string) {
$string = stripcslashes($string);
$string = html_entity_decode($string, ENT_QUOTES, 'UTF-8');
$string = str_replace(''', "'", $string);
return $string;
}
/**
* Tells if helpdesk replacement is enabled for the current user
*/
function plugin_formcreator_replaceHelpdesk() {
if (isset($_SESSION['glpiactiveprofile']['interface'])
&& isset($_SESSION['glpiactive_entity'])) {
// Interface and active entity are set in session
$helpdeskMode = PluginFormcreatorEntityconfig::getUsedConfig('replace_helpdesk', $_SESSION['glpiactive_entity']);
if ($helpdeskMode != '0'
&& $_SESSION['glpiactiveprofile']['interface'] == 'helpdesk') {
return $helpdeskMode;
}
}
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 $item instance of CommonDBTM object
* @param $field field of object's table to search in
* @param $value value to search in provided field
*
* @return true if succeed else false
*/
function plugin_formcreator_getFromDBByField(CommonDBTM $item, $field = '', $value = '') {
global $DB;
// != 0 because 0 is consider as empty
if (!$item instanceof Entity
&& (strlen($value) == 0
|| $value === 0)) {
return false;
}
$field = $DB->escape($field);
$value = $DB->escape($value);
$found = $item->getFromDBByRequest([
'WHERE' => [$item::getTable() . '.' . $field => $value],
'LIMIT' => 1
]);
if ($found) {
return $item->getID();
} else {
return false;
}
}
/**
* Autoloader
* @param string $classname
*/
function plugin_formcreator_autoload($classname) {
if (strpos($classname, 'PluginFormcreator') === 0) {
// Search first for field clases
$filename = __DIR__ . '/inc/fields/' . strtolower(str_replace('PluginFormcreator', '', $classname)) . '.class.php';
if (is_readable($filename) && is_file($filename)) {
include_once($filename);
return true;
}
// 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) {
global $DB;
$error = $DB->error();
$migration->log($error . "\n" . Toolbox::backtrace(false, '', ['Toolbox::backtrace()']), false);
die($error . "<br><br> Please, check migration log");
}
function plugin_formcreator_ldap_warning_handler($errno, $errstr, $errfile, $errline, array $errcontext) {
if (0 === error_reporting()) {
return false;
}
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}