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

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