@@ -24,92 +24,6 @@ static public function plugin_post_item_form_formvalidation($parm) {
2424 }
2525 }
2626
27- const HELPER_FUNCTIONS = <<<'EOT'
28- //------------------------------------------
29- // helper function to verify if a string
30- // is really a date
31- // uses the datepicker JQuery plugin
32- //------------------------------------------
33- function isValidDate(string) {
34- try {
35- if (string.length == 0) {
36- return false;
37- }
38- $.datepicker.parseDate($('.hasDatepicker').datepicker('option', 'dateFormat'), string);
39- return true;
40- } catch (e) {
41- return false;
42- }
43- }
44-
45- //------------------------------------------
46- // helper function to verify a if a string
47- // is really a time from 00:00[:00] to 23:59[:59]
48- //------------------------------------------
49- function isValidTime(str) {
50- return /^(?:[0-1]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?$/.test(str);
51- }
52-
53- //------------------------------------------
54- // helper function to verify a if a string
55- // is really an integer
56- //------------------------------------------
57- function isValidInteger(str) {
58- return /^\d+$/.test(str);
59- }
60-
61- //------------------------------------------
62- // helper function to count words in a given string
63- // returns quantity of words
64- //------------------------------------------
65- function countWords(str) {
66- return str.split(/\W+/).length;
67- }
68-
69- //------------------------------------------
70- // helper function to verify a if a string
71- // is really an IPV4 address
72- // uses the datapicker JQuery plugin
73- //------------------------------------------
74- function isValidIPv4(ipaddress) {
75- 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) ;
76- }
77-
78-
79- //------------------------------------------
80- // helper function to verify a if a string
81- // is really an IPV6 address
82- // uses the datapicker JQuery plugin
83- //------------------------------------------
84- function isValidIPv6(ipaddress) {
85- 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);
86- }
87-
88- //------------------------------------------
89- // helper function to verify a if a string
90- // is really an email address
91- // will use the input type=email if it exists (HTML5)
92- // otherwise will use a basic verification.
93- //------------------------------------------
94- function isValidEmail(value) {
95- var input = document.createElement("input");
96-
97- input.type = "email";
98- input.value = value;
99-
100- return typeof input.checkValidity == "function" ? input.checkValidity() : /^\S+@\S+\.\S+$/.test(value);
101- }
102-
103- //------------------------------------------
104- // helper function to verify a if a string
105- // is really a MAC address
106- //------------------------------------------
107- function isValidMacAddress(str) {
108- return /^[\da-f]{2}([:-])(?:[\da-f]{2}\1){4}[\da-f]{2}$/i.test(str);
109- }
110-
111- EOT;
112-
11327 /**
11428 * Summary of plugin_pre_item_update_formvalidation
11529 * @param mixed $parm the object that is going to be updated
@@ -118,6 +32,8 @@ function isValidMacAddress(str) {
11832 static public function plugin_pre_item_update_formvalidation ($ parm ) {
11933 global $ DB ;
12034
35+ $ config = PluginFormvalidationConfig::getInstance ();
36+ $ path = $ config ->fields ["js_path " ];
12137 // to be executed only for massive actions
12238 if (strstr ($ _SERVER ['PHP_SELF ' ], "/front/massiveaction.php " )) {
12339 $ ret =[];
@@ -167,19 +83,19 @@ static public function plugin_pre_item_update_formvalidation($parm) {
16783 ]
16884 ]
16985 ];
170-
86+
17187 if (!empty ($ input )) {
17288 $ key = array_keys ($ input );
17389 $ query2 ['WHERE ' ]['AND ' ]["glpi_plugin_formvalidation_fields.css_selector_value " ] = ['LIKE ' , '% ' .$ key [0 ].'% ' ];
17490 }
17591
17692 foreach ($ DB ->request ( $ query2 ) as $ form ) {
177- foreach ($ DB ->request ('glpi_plugin_formvalidation_fields ' , ['forms_id ' => $ form ['id ' ]]) as $ field ) {
93+ foreach ($ DB ->request ('glpi_plugin_formvalidation_fields ' , ['AND ' => [ ' forms_id ' => $ form ['id ' ], ' is_active ' => 1 ]]) as $ field ) {
17894 $ matches = [];
17995 if (preg_match ('/\[(name|id\^)= \\\\{0,1}"(?<name>[a-z_\-0-9]+) \\\\{0,1}"\]/i ' , $ field ['css_selector_value ' ], $ matches )) {
180- $ fieldnames [$ field ['id ' ]]= $ matches ['name ' ];
181- $ formulas [$ field ['id ' ]]= ($ field ['formula ' ] ? $ field ['formula ' ] : '#>0 || #!="" ' );
182- $ fieldtitles [$ field ['id ' ]]= $ field ['name ' ];
96+ $ fieldnames [$ field ['id ' ]] = trim ( $ matches ['name ' ], " _ " ) ;
97+ $ formulas [$ field ['id ' ]] = ($ field ['formula ' ] ? $ field ['formula ' ] : '#>0 || #!="" ' );
98+ $ fieldtitles [$ field ['id ' ]] = $ field ['name ' ];
18399 }
184100 }
185101
@@ -196,21 +112,40 @@ static public function plugin_pre_item_update_formvalidation($parm) {
196112 } else {
197113 $ regex = '/# ' .$ valnum .'\b/i ' ;
198114 }
199- //$formulaJS[$fieldnum] = preg_replace( $regex, '"'.Toolbox::addslashes_deep( $val ).'"', $formulaJS[$fieldnum] ) ;
200- $ formulaJS [$ fieldnum ] = preg_replace ( $ regex , "PHP.val[ $ valnum] " , $ formulaJS [$ fieldnum ] );
115+ $ formulaJS [$ fieldnum ] = preg_replace ( $ regex , '" ' .$ values [$ valnum ].'" ' , $ formulaJS [$ fieldnum ] );
201116 }
202117 }
203118
204- $ v8 = new V8Js ();
205- $ v8 ->val = $ values ;
206119 $ ret =[];
120+ $ helpers = file_get_contents (__DIR__ . "/../js/helpers_function.js.tpl " );
121+ $ helpers = str_replace ('$dateFormat ' , 'YYYY-MM-DD ' , $ helpers );
122+ $ moment = file_get_contents (GLPI_ROOT ."/lib/moment.min.js " );
207123 foreach ($ formulaJS as $ index => $ formula ) {
208124 try {
209- if (!$ v8 ->executeString (self ::HELPER_FUNCTIONS ."
210- exec = $ formula;
211- " ) ) {
212- Session::addMessageAfterRedirect ( __ ('Mandatory fields or wrong value: ' ).__ ($ fieldtitles [$ index ]), true , ERROR );
213- $ ret []=$ fieldnames [$ index ];
125+ if (extension_loaded ('v8js ' )) {
126+ $ v8 = new V8Js ();
127+ if (!$ v8 ->executeString ($ moment ."\n" .$ helpers ."\n
128+ exec = $ formula; " )
129+ ) {
130+ Session::addMessageAfterRedirect ( __ ('Mandatory fields or wrong value: ' ).__ ($ fieldtitles [$ index ]), true , ERROR );
131+ $ ret [] = $ fieldnames [$ index ];
132+ }
133+ } else {
134+ if (file_exists ($ path )) {
135+ $ tmpfile = tempnam (GLPI_ROOT .'/files/_tmp ' , 'tmp ' );
136+ $ handle = fopen ($ tmpfile , "w " );
137+ fwrite ($ handle , "var moment = require('../../lib/moment.min.js'); \n" .$ helpers ."\nif( $ formula){console.log(1);}else{console.log(0);} " );
138+ fclose ($ handle );
139+ $ valid = exec ("\"$ path \" \"$ tmpfile \"" );
140+ if ($ valid == 0 || is_null ($ valid )) {
141+ Session::addMessageAfterRedirect ( __ ('Mandatory fields or wrong value: ' ).__ ($ fieldtitles [$ index ]), true , ERROR );
142+ $ ret [] = $ fieldnames [$ index ];
143+ }
144+ unlink ($ tmpfile );
145+ } else {
146+ Session::addMessageAfterRedirect ( __ ('The field was not updated because node.js and v8js are not installed/enabled. Contact your system administrator ' ), true , ERROR );
147+ $ ret [] = $ fieldnames [$ index ];
148+ }
214149 }
215150 } catch (Exception $ ex ) {
216151 Session::addMessageAfterRedirect ( __ ('Error: ' ).__ ($ ex ->message ), false , ERROR );
0 commit comments