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

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