PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.10.1
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.10.1
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.10.1, at includes/Admin/Form/Helpers.php

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