PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.7.0
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.7.0
V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 2.10.2 All 137 releases
bit-form / includes / Admin / Form / Helpers.php

Helpers.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 2.7.0, at includes/Admin/Form/Helpers.php

215 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace BitCode\BitForm\Admin\Form;
4
5 class Helpers
6 {
7 public static function scriptLoader($src, $id, $instanceObj = null, $selector = '', $attrs = [], $integrity = null, $contentId = '')
8 {
9 $attributes = json_encode($attrs);
10 $instObj = '';
11 if ($instanceObj) {
12 $instObj .= <<<INST
13 script.onload = function () {
14 bfSelect('#{$contentId}').querySelectorAll('{$selector}').forEach(function(fld){
15 $instanceObj;
16 });
17 }
18 INST;
19 }
20 return <<<LOAD_SECRIPT
21 var script = document.createElement('script'), integrity = '$integrity', attrs = $attributes, id = '$id';
22 script.src = '$src';
23 script.id = id;
24 if(integrity){
25 script.integrity = integrity;
26 script.crossOrigin = 'anonymous';
27 }
28 if(attrs){
29 Object.entries(attrs).forEach(function([key, val]){
30 script.setAttribute(key,val);
31 })
32 }
33 $instObj;
34 var bodyElm = document.body;
35 var alreadyExistScriptElm = bodyElm ? bodyElm.querySelector('script#$id'):null;
36 if(alreadyExistScriptElm){
37 bodyElm.removeChild(alreadyExistScriptElm)
38 }
39 if(!(window.recaptcha && id === 'g-recaptcha-script')){
40 bodyElm.appendChild(script);
41 }
42 LOAD_SECRIPT;
43 }
44
45 public static function minifyJs($input)
46 {
47 if ('' === trim($input)) {
48 return $input;
49 }
50 return preg_replace(
51 [
52 '/ {2,}/',
53 '/\s*=\s*/',
54 '/\s*,\s*/',
55 '/\s+(?=\(|\{|\:|\?)|\t|(?:\r?\n[ \t]*)+/s'
56 ],
57 [' ', '=', ',', ''],
58 $input
59 );
60 }
61
62 /**
63 * @method name : saveFile
64 * @description : save js/css field to disk
65 * @param : $path => like(dirName/css), $fileName => main.css, $script
66 * @return : boolean
67 */
68 public static function saveFile($path, $fileName, $script, $fileOpenMode = 'a')
69 {
70 $rootDir = BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR;
71 $path = trim($path, '/');
72 $pathArr = explode('/', $path); // like "fieldname/user => [Fieldname, user]
73 foreach ($pathArr as $d) {
74 $rootDir .= $d . DIRECTORY_SEPARATOR;
75 if (!realpath($rootDir)) {
76 mkdir($rootDir);
77 }
78 }
79 $fullPath = $rootDir . $fileName;
80 $file = fopen($fullPath, $fileOpenMode);
81 fwrite($file, $script);
82 fclose($file);
83 return true;
84 }
85
86 /**
87 * @method name : generatePathDirOrFile
88 * @dscription : generate path for js/css file
89 * @params : $path => like(dirName/css)
90 * @return : a string of full path
91 */
92 public static function generatePathDirOrFile($path)
93 {
94 $rootDir = BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR;
95 $path = trim($path, '/');
96 $pathArr = explode('/', $path); // like "fieldname/user => [Fieldname, user]
97 foreach ($pathArr as $d) {
98 $rootDir .= $d . DIRECTORY_SEPARATOR;
99 }
100 return rtrim($rootDir, DIRECTORY_SEPARATOR);
101 }
102
103 public static function fileRead($filePath)
104 {
105 $fileContent = '';
106 if (file_exists($filePath)) {
107 $file = fopen($filePath, 'r');
108 $fileContent .= fread($file, filesize($filePath));
109 fclose($file);
110 }
111 return $fileContent;
112 }
113
114 public static function getDataFromNestedPath($data, $key)
115 {
116 $keys = explode('->', $key);
117 $lastKey = array_pop($keys);
118 $dataType = is_array($data) ? 'array' : (is_object($data) ? 'object' : '');
119 if ('array' === $dataType) {
120 return self::accessFromArray($data, $keys, $lastKey);
121 }
122 if ('object' === $dataType) {
123 return self::accessFromObject($data, $keys, $lastKey);
124 }
125 }
126
127 private static function accessFromObject($data, $keys, $lastKey)
128 {
129 foreach ($keys as $k) {
130 if (!property_exists($data, $k)) {
131 return null;
132 }
133 $data = $data->$k;
134 }
135 return isset($data->$lastKey) ? $data->$lastKey : null;
136 }
137
138 private static function accessFromArray($data, $keys, $lastKey)
139 {
140 foreach ($keys as $k) {
141 if (!array_key_exists($k, $data)) {
142 return null;
143 }
144 $data = $data[$k];
145 }
146 return isset($data[$lastKey]) ? $data[$lastKey] : null;
147 }
148
149 public static function setDataToNestedPath($data, $key, $value)
150 {
151 $keys = explode('->', $key);
152 $lastKey = array_pop($keys);
153 foreach ($keys as $k) {
154 if (!array_key_exists($k, $data)) {
155 $data->$k = (object) [];
156 }
157 $data = $data->$k;
158 }
159 $data->$lastKey = json_decode(json_encode($value));
160 ;
161 return $data;
162 }
163
164 public static function property_exists_nested($obj, $path = '', $valToCheck = null, $checkNegativeVal = 0)
165 {
166 $path = explode('->', $path);
167 $current = $obj;
168 foreach ($path as $key) {
169 if (is_object($current)) {
170 if (property_exists($current, $key)) {
171 $current = $current->{$key};
172 } else {
173 return false;
174 }
175 } else {
176 return false;
177 }
178 }
179 if (isset($valToCheck)) {
180 if ($checkNegativeVal) {
181 return $current !== $valToCheck;
182 }
183 return $current === $valToCheck;
184 }
185 return true;
186 }
187
188 public static function honeypotEncryptedToken($str)
189 {
190 $token = base64_encode(base64_encode($str));
191 return $token;
192 }
193
194 public static function csrfEecrypted()
195 {
196 $secretKey = get_option('bf_csrf_secret');
197 if (!$secretKey) {
198 $secretKey = 'bf-' . time();
199 update_option('bf_csrf_secret', $secretKey);
200 }
201 $tIdenty = base64_encode(random_bytes(32));
202 $csrf = \base64_encode(\hash_hmac('sha256', $tIdenty, $secretKey, true));
203 return ['csrf' => $csrf, 't_identity' => $tIdenty];
204 }
205
206 public static function csrfDecrypted($identy, $token)
207 {
208 $secretKey = get_option('bf_csrf_secret');
209 return \hash_equals(
210 \base64_encode(\hash_hmac('sha256', $identy, $secretKey, true)),
211 $token
212 );
213 }
214 }
215