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

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