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

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