@@ -8,7 +8,7 @@ trait JsUtilsInternalTrait {
8
8
protected $ jquery_code_for_compile = array ();
9
9
10
10
protected $ jquery_code_for_compile_at_last = array ();
11
-
11
+
12
12
protected $ nonce ;
13
13
14
14
protected function _addToCompile ($ jsScript ) {
@@ -73,24 +73,24 @@ protected function minify($input) {
73
73
*/
74
74
protected function _open_script ($ src = '' ) {
75
75
$ str = '<script ' ;
76
- if ( isset ($ this ->params ['nonce ' ])){
77
- $ str .= ' nonce=" ' . $ this ->generateNonce ($ this ->params ['nonce ' ]). ' " ' ;
78
- $ this -> onNonce () ;
76
+ if (! $ this -> isAjax () && isset ($ this ->params ['nonce ' ])) {
77
+ $ nonce = $ this -> nonce ?? $ this ->generateNonce ($ this ->params ['nonce ' ]);
78
+ $ str .= ' nonce=" ' . $ nonce . ' " ' ;
79
79
}
80
80
$ str .= ($ src == '' ) ? '> ' : ' src=" ' . $ src . '"> ' ;
81
81
return $ str ;
82
82
}
83
-
84
- protected function onNonce (){
85
-
86
- }
87
-
88
- protected function generateNonce ($ value =null ): string {
83
+
84
+ protected function onNonce () {}
85
+
86
+ protected function generateNonce ($ value = null ): string {
89
87
$ bytes = \random_bytes ((int ) ($ value ?? 32 ));
90
- return $ this ->nonce =\base64_encode ($ bytes );
88
+ $ this ->nonce = \base64_encode ($ bytes );
89
+ $ this ->onNonce ();
90
+ return $ this ->nonce ;
91
91
}
92
-
93
- public function getNonce (){
92
+
93
+ public function getNonce () {
94
94
return $ this ->nonce ;
95
95
}
96
96
@@ -111,4 +111,8 @@ protected function conflict() {
111
111
public function addToCompile ($ jsScript ) {
112
112
$ this ->_addToCompile ($ jsScript );
113
113
}
114
+
115
+ public function isAjax (): bool {
116
+ return (isset ($ _SERVER ['HTTP_X_REQUESTED_WITH ' ]) && ! empty ($ _SERVER ['HTTP_X_REQUESTED_WITH ' ]) && strtolower ($ _SERVER ['HTTP_X_REQUESTED_WITH ' ]) == 'xmlhttprequest ' );
117
+ }
114
118
}
0 commit comments