Skip to content

Commit 6b18d29

Browse files
committed
Fixes issues with nodejs and with formula execution when using massiveactions
1 parent 8caf70a commit 6b18d29

File tree

8 files changed

+188
-103
lines changed

8 files changed

+188
-103
lines changed

formvalidation.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
</authors>
2626
<versions>
2727
<version>
28-
<num>1.0.3</num>
28+
<num>1.0.11</num>
2929
<compatibility>9.5</compatibility>
3030
</version>
3131
<version>

inc/hook.class.php

+13-12
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,13 @@ static public function plugin_pre_item_update_formvalidation($parm) {
3838
if (strstr($_SERVER['PHP_SELF'], "/front/massiveaction.php")) {
3939
$ret=[];
4040

41-
//return;
4241
//clean input values
4342
$input = $parm->input;
44-
unset( $input['id'] );
45-
foreach ($input as $key => $val) {
46-
if (preg_match("/^_/", $key )) {
47-
unset( $input[$key] );
48-
}
43+
unset($input['id']);
44+
if (isset($input['_no_history'])) {
45+
unset($input['_no_history']);
4946
}
47+
5048
$itemvalues = array_merge( $parm->fields, $input );
5149
$formulas = [];
5250
$fieldnames = [];
@@ -96,11 +94,14 @@ static public function plugin_pre_item_update_formvalidation($parm) {
9694
]
9795
];
9896

99-
if (!empty($input)) {
100-
$key = array_keys($input);
101-
$query2['WHERE']['AND']["glpi_plugin_formvalidation_fields.css_selector_value"] = ['LIKE', '%'.$key[0].'%'];
97+
// if $input is empty, then do not continue the validation
98+
if (empty($input)) {
99+
return;
102100
}
103101

102+
$key = array_keys($input);
103+
$query2['WHERE']['AND']["glpi_plugin_formvalidation_fields.css_selector_value"] = ['LIKE', '%'.$key[0].'%'];
104+
104105
foreach ($DB->request( $query2) as $form) {
105106
foreach ($DB->request('glpi_plugin_formvalidation_fields', ['AND' => ['forms_id' => $form['id'], 'is_active' => 1]]) as $field) {
106107
$matches = [];
@@ -113,7 +114,7 @@ static public function plugin_pre_item_update_formvalidation($parm) {
113114

114115
$values=[];
115116
foreach ($formulas as $fieldnum => $formula) {
116-
$values[$fieldnum] = ($itemvalues[$fieldnames[$fieldnum]] ? $itemvalues[$fieldnames[$fieldnum]] : "" );
117+
$values[$fieldnum] = $DB->escape($itemvalues[$fieldnames[$fieldnum]] ? $itemvalues[$fieldnames[$fieldnum]] : "" );
117118
}
118119
$formulaJS=[];
119120
foreach ($formulas as $fieldnum => $formula) {
@@ -131,7 +132,7 @@ static public function plugin_pre_item_update_formvalidation($parm) {
131132
$ret=[];
132133
$helpers = file_get_contents(__DIR__ . "/../js/helpers_function.js.tpl");
133134
$helpers = str_replace('$dateFormat', 'YYYY-MM-DD', $helpers);
134-
$moment = file_get_contents(GLPI_ROOT."/lib/moment.min.js");
135+
$moment = file_get_contents(__DIR__ . "/../lib/moment/moment.min.js");
135136
foreach ($formulaJS as $index => $formula) {
136137
try {
137138
if (extension_loaded('v8js')) {
@@ -146,7 +147,7 @@ static public function plugin_pre_item_update_formvalidation($parm) {
146147
if (file_exists($path)) {
147148
$tmpfile = tempnam(GLPI_ROOT.'/files/_tmp', 'tmp');
148149
$handle = fopen($tmpfile, "w");
149-
fwrite($handle, "var moment = require('../../lib/moment.min.js');\n".$helpers."\nif($formula){console.log(1);}else{console.log(0);}");
150+
fwrite($handle, "var moment = require('".str_replace('\\', '/', __DIR__)."/../lib/moment/moment.min.js');\n".$helpers."\nif($formula){console.log(1);}else{console.log(0);}");
150151
fclose($handle);
151152
$valid = exec("\"$path\" \"$tmpfile\"");
152153
if ($valid == 0 || is_null($valid)) {

inc/itemtype.class.php

+17-7
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,10 @@ function showForm($id = null, $options = ['candel'=>false]) {
2626
$this->showFormHeader(['formtitle'=>'Item type','colspan' => 4]);
2727

2828
echo "<tr class='tab_bg_1'>";
29-
echo "<td><label for='name'>".__('Name')."</label></td>";
29+
echo "<td><label for='name'>".__('Item type name (examples: Project, Computer, ...)')."</label></td>";
3030
echo "<td><input type='text' name='name' value='".$this->fields['name']."'/></td>";
31-
echo "</tr'>";
32-
echo "<tr class='tab_bg_1'>";
33-
echo "<td><label for='name'>".__('Item type (example: Project)', 'formvalidation')."</label></td>";
34-
echo "<td><input type='text' name='itemtype' value='".$this->fields['itemtype']."'/></td>";
35-
echo "</tr>";
3631
echo "<tr class='tab_bg_1'>";
37-
echo "<td><label for='name'>".__('URL path part (example: /front/project.form.php)', 'formvalidation')."</label></td>";
32+
echo "<td><label for='name'>".__('URL path part of the item type form (examples: /front/project.form.php, /front/computer.form.php, ...)', 'formvalidation')."</label></td>";
3833
echo "<td><input type='text' id='urlpath' name='URL_path_part' value='".$this->fields['URL_path_part']."'/></td>";
3934
echo "</tr'>";
4035

@@ -47,4 +42,19 @@ function showForm($id = null, $options = ['candel'=>false]) {
4742
//echo '</script>';
4843

4944
}
45+
46+
function post_addItem() {
47+
global $DB,$CFG_GLPI;
48+
$id = $this->fields['id'];
49+
$guid = $CFG_GLPI['url_base']."/plugins/formvalidation/ajax/itemtype/".time()."/".rand()."/".$id;
50+
$DB->updateOrDie(
51+
'glpi_plugin_formvalidation_itemtypes',
52+
[
53+
'guid' => md5($guid)
54+
],
55+
[
56+
'id' => $id
57+
]
58+
);
59+
}
5060
}

install/update.php

+60-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
function formvalidation_update() {
44
global $DB, $CFG_GLPI;
55
$dbu = new DbUtils();
6-
6+
$config = PluginFormvalidationConfig::getInstance();
77
/*** Update 0.5.3 ****/
88
$migration = new Migration(100);
99
if (!$DB->fieldExists('glpi_plugin_formvalidation_configs', 'db_version')) {
@@ -189,15 +189,71 @@ function formvalidation_update() {
189189
}
190190
}
191191

192+
/* update value guid field */
193+
if ($DB->fieldExists('glpi_plugin_formvalidation_itemtypes', 'guid', false)) {
194+
$guid = $CFG_GLPI['url_base']."/plugins/formvalidation/ajax/".$obj[0]."/".time()."/".rand()."/";
195+
foreach ($DB->request('glpi_plugin_formvalidation_itemtypes', ['guid' => ""]) as $row) {
196+
$guid = $guid."".$row['id'];
197+
$id = $row['id'];
198+
$DB->updateOrDie(
199+
'glpi_plugin_formvalidation_itemtypes',
200+
['guid' => md5($guid)],
201+
['id' => $id],
202+
'Error updating value into glpi_plugin_formvalidation_itemtypes'
203+
);
204+
}
205+
}
206+
/* update value guid field */
207+
if ($DB->fieldExists('glpi_plugin_formvalidation_pages', 'guid', false)) {
208+
$guid = $CFG_GLPI['url_base']."/plugins/formvalidation/ajax/".$obj[1]."/".time()."/".rand()."/";
209+
foreach ($DB->request('glpi_plugin_formvalidation_pages', ['guid'=>""]) as $row) {
210+
$guid = $guid."".$row['id'];
211+
$id = $row['id'];
212+
$DB->updateOrDie(
213+
'glpi_plugin_formvalidation_pages',
214+
['guid' => md5($guid)],
215+
['id' => $id],
216+
'Error updating value into glpi_plugin_formvalidation_pages'
217+
);
218+
}
219+
}
220+
/* Update value guid field */
221+
if ($DB->fieldExists('glpi_plugin_formvalidation_forms', 'guid', false)) {
222+
$guid = $CFG_GLPI['url_base']."/plugins/formvalidation/ajax/".$obj[2]."/".time()."/".rand()."/";
223+
foreach ($DB->request('glpi_plugin_formvalidation_forms', ['guid' => ""]) as $row) {
224+
$guid = $guid."".$row['id'];
225+
$id = $row['id'];
226+
$DB->updateOrDie(
227+
'glpi_plugin_formvalidation_forms',
228+
['guid' => md5($guid)],
229+
['id' => $id],
230+
'Error updating value into glpi_plugin_formvalidation_forms'
231+
);
232+
}
233+
}
234+
/* Update value guid field */
235+
if ($DB->fieldExists('glpi_plugin_formvalidation_fields', 'guid', false)) {
236+
$guid = $CFG_GLPI['url_base']."/plugins/formvalidation/ajax/".$obj[3]."/".time()."/".rand()."/";
237+
foreach ($DB->request('glpi_plugin_formvalidation_fields', ['guid' => ""]) as $row) {
238+
$guid = $guid."".$row['id'];
239+
$id = $row['id'];
240+
$DB->updateOrDie(
241+
'glpi_plugin_formvalidation_fields',
242+
['guid' => md5($guid)],
243+
['id' => $id],
244+
'Error updating value into glpi_plugin_formvalidation_fields'
245+
);
246+
}
247+
}
192248
if ($DB->tableExists("glpi_plugin_formvalidation_fields")) {
193249
$oldFormula = ['isValidDate', 'isValidInteger', 'countWords', 'isValidIPv4', 'isValidIPv6', 'isValidEmail', 'isValidMacAddress'];
194250
$newFormula = ['FVH.isValidDate', 'FVH.isValidInteger', 'FVH.countWords', 'FVH.isValidIPv4', 'FVH.isValidIPv6', 'FVH.isValidEmail', 'FVH.isValidMacAddress'];
195251
$datas = $DB->query("SELECT id, formula FROM glpi_plugin_formvalidation_fields ");
196252
if ($DB->numrows($datas) > 0) {
197-
while ($row = $DB->fetch_assoc($datas)) {
198-
if ($row['formula'] != null) {
253+
while ($row = $DB->fetchAssoc($datas)) {
254+
if ($row['formula'] != null && strpos($row['formula'], 'FVH.') < 0) {
199255
$res = str_replace($oldFormula, $newFormula, $row['formula']);
200-
$DB->query("UPDATE glpi_plugin_formvalidation_fields SET formula = '".$res."' WHERE id = ".$row['id']);
256+
$DB->query("UPDATE glpi_plugin_formvalidation_fields SET formula = '".$DB->escape($res)."' WHERE id = ".$row['id']);
201257
}
202258
}
203259
}

js/formvalidation.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -990,11 +990,13 @@ var Formvalidation = {
990990
switch (this.localName) {
991991
case 'input':
992992
case 'textarea':
993-
jqValueElt = $(this);
993+
jqValueElt = $(this);
994+
var endSelectorValue = jqValueElt[0].name === "" ? jqValueElt[0].type : jqValueElt[0].name;
995+
var attr = jqValueElt[0].name ? "name" : "type";
994996
field = {
995997
'jq_value_elt': jqValueElt,
996998
'jq_focus_elt': jqValueElt,
997-
'selector_value': jqValueElt[0].localName + '[name="' + jqValueElt[0].name + '"]'
999+
'selector_value': jqValueElt[0].localName + '['+attr+'="' + endSelectorValue + '"]'
9981000
};
9991001
break;
10001002
case 'div':
@@ -1064,7 +1066,7 @@ if (location.href.indexOf('withtemplate=1') == -1) {
10641066
itemtype = location.pathname.match(/(plugin)s\/(formcreator)\/front\/(form)display.php$/);
10651067
}
10661068
if (!itemtype) {
1067-
itemtype = location.pathname.match(/front\/helpdesk.public.php$/);
1069+
itemtype = location.pathname.match(/front\/helpdesk.public.php$/);
10681070
if (itemtype && !location.search.match(/^\?create_ticket=1$/)) {
10691071
itemtype = null;
10701072
}
@@ -1075,6 +1077,13 @@ if (location.href.indexOf('withtemplate=1') == -1) {
10751077
itemtype[0] = 'selfservice';
10761078
itemtype[1] = 'ticket';
10771079
}
1080+
}
1081+
if (!itemtype) {
1082+
itemtype = location.pathname.match(/front\/transfer.action.php$/);
1083+
if (itemtype) {
1084+
itemtype[0] = 'central';
1085+
itemtype[1] = 'transfer';
1086+
}
10781087
}
10791088
// normal case
10801089
if (!itemtype) {

js/helpers_function.js

+78-73
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,89 @@
11
var FVH = {
2-
3-
//------------------------------------------
4-
// helper function to verify if a string
5-
// is really a date
6-
// uses the datepicker JQuery plugin
7-
//------------------------------------------
8-
isValidDate: function (string) {
9-
try {
10-
if (string.length == 0) {
11-
return false;
12-
}
13-
$.datepicker.parseDate($('.hasDatepicker').datepicker('option', 'dateFormat'), string);
14-
return true;
15-
} catch (e) {
16-
return false;
2+
//------------------------------------------
3+
// helper function to verify if a string
4+
// is really a date
5+
// uses the datepicker JQuery plugin
6+
//------------------------------------------
7+
isValidDate: function (string) {
8+
try {
9+
if (string.length == 0) {
10+
return false;
1711
}
18-
},
12+
flatpickr.parseDate(string, "Y-m-d H:i:S");
13+
return true;
14+
} catch (e) {
15+
return false;
16+
}
17+
},
1918

20-
//------------------------------------------
21-
// helper function to verify a if a string
22-
// is really a time from 00:00[:00] to 23:59[:59]
23-
//------------------------------------------
24-
isValidTime: function (str) {
25-
return /^(?:[0-1]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?$/.test(str);
26-
},
19+
//------------------------------------------
20+
// helper function to verify a if a string
21+
// is really a time from 00:00[:00] to 23:59[:59]
22+
//------------------------------------------
23+
isValidTime: function (str) {
24+
return /^(?:[0-1]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?$/.test(str);
25+
},
2726

28-
//------------------------------------------
29-
// helper function to verify a if a string
30-
// is really an integer
31-
//------------------------------------------
32-
isValidInteger: function (str) {
33-
return /^\d+$/.test(str);
34-
},
27+
//------------------------------------------
28+
// helper function to verify a if a string
29+
// is really an integer
30+
//------------------------------------------
31+
isValidInteger: function (str) {
32+
return /^\d+$/.test(str);
33+
},
3534

36-
//------------------------------------------
37-
// helper function to count words in a given string
38-
// returns quantity of words
39-
//------------------------------------------
40-
countWords: function (str) {
41-
return str.split(/\W+/).length;
42-
},
35+
//------------------------------------------
36+
// helper function to count words in a given string
37+
// returns quantity of words
38+
//------------------------------------------
39+
countWords: function (str) {
40+
return str.split(/\W+/).length;
41+
},
4342

44-
//------------------------------------------
45-
// helper function to verify a if a string
46-
// is really an IPV4 address
47-
// uses the datapicker JQuery plugin
48-
//------------------------------------------
49-
isValidIPv4: function (ipaddress) {
50-
return /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ipaddress);
51-
},
43+
//------------------------------------------
44+
// helper function to verify a if a string
45+
// is really an IPV4 address
46+
// uses the datapicker JQuery plugin
47+
//------------------------------------------
48+
isValidIPv4: function (ipaddress) {
49+
return /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(
50+
ipaddress
51+
);
52+
},
5253

53-
//------------------------------------------
54-
// helper function to verify a if a string
55-
// is really an IPV6 address
56-
// uses the datapicker JQuery plugin
57-
//------------------------------------------
58-
isValidIPv6: function (ipaddress) {
59-
return /^((?:[0-9A-Fa-f]{1,4}))((?::[0-9A-Fa-f]{1,4}))*::((?:[0-9A-Fa-f]{1,4}))((?::[0-9A-Fa-f]{1,4}))*|((?:[0-9A-Fa-f]{1,4}))((?::[0-9A-Fa-f]{1,4})){7}$/.test(ipaddress);
60-
},
54+
//------------------------------------------
55+
// helper function to verify a if a string
56+
// is really an IPV6 address
57+
// uses the datapicker JQuery plugin
58+
//------------------------------------------
59+
isValidIPv6: function (ipaddress) {
60+
return /^((?:[0-9A-Fa-f]{1,4}))((?::[0-9A-Fa-f]{1,4}))*::((?:[0-9A-Fa-f]{1,4}))((?::[0-9A-Fa-f]{1,4}))*|((?:[0-9A-Fa-f]{1,4}))((?::[0-9A-Fa-f]{1,4})){7}$/.test(
61+
ipaddress
62+
);
63+
},
6164

62-
//------------------------------------------
63-
// helper function to verify a if a string
64-
// is really an email address
65-
// will use the input type=email if it exists (HTML5)
66-
// otherwise will use a basic verification.
67-
//------------------------------------------
68-
isValidEmail: function (value) {
69-
var input = document.createElement('input');
65+
//------------------------------------------
66+
// helper function to verify a if a string
67+
// is really an email address
68+
// will use the input type=email if it exists (HTML5)
69+
// otherwise will use a basic verification.
70+
//------------------------------------------
71+
isValidEmail: function (value) {
72+
var input = document.createElement("input");
7073

71-
input.type = 'email';
72-
input.value = value;
74+
input.type = "email";
75+
input.value = value;
7376

74-
return typeof input.checkValidity == 'function' ? input.checkValidity() : /^\S+@\S+\.\S+$/.test(value);
75-
},
77+
return typeof input.checkValidity == "function"
78+
? input.checkValidity()
79+
: /^\S+@\S+\.\S+$/.test(value);
80+
},
7681

77-
//------------------------------------------
78-
// helper function to verify a if a string
79-
// is really a MAC address
80-
//------------------------------------------
81-
isValidMacAddress: function (sftr) {
82-
return /^[\da-f]{2}([:-])(?:[\da-f]{2}\1){4}[\da-f]{2}$/i.test(str);
83-
},
84-
}
82+
//------------------------------------------
83+
// helper function to verify a if a string
84+
// is really a MAC address
85+
//------------------------------------------
86+
isValidMacAddress: function (sftr) {
87+
return /^[\da-f]{2}([:-])(?:[\da-f]{2}\1){4}[\da-f]{2}$/i.test(str);
88+
},
89+
};

0 commit comments

Comments
 (0)