| @@ -1,11 +1,38 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace BitCode\BitForm\Admin\Form; |
| 4 | 4 | |
| 5 | -class Helpers { | |
| 6 | - public static function scriptLoader($src, $id, $instanceObj = null, $selector = '', $attrs = [], $integrity = null, $contentId = '') { | |
| 7 | - $attributes = json_encode($attrs); | |
| 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); | |
| 8 | 35 | $instObj = ''; |
| 9 | 36 | if ($instanceObj) { |
| 10 | 37 | $instObj .= <<<INST |
| 11 | 38 | script.onload = function () { |
| @@ -15,11 +42,11 @@ | ||
| 15 | 42 | } |
| 16 | 43 | INST; |
| 17 | 44 | } |
| 18 | 45 | return <<<LOAD_SECRIPT |
| 19 | -var script = document.createElement('script'), integrity = '$integrity', attrs = $attributes; | |
| 46 | +var script = document.createElement('script'), integrity = '$integrity', attrs = $attributes, id = '$id'; | |
| 20 | 47 | script.src = '$src'; |
| 21 | -script.id = '$id'; | |
| 48 | +script.id = id; | |
| 22 | 49 | if(integrity){ |
| 23 | 50 | script.integrity = integrity; |
| 24 | 51 | script.crossOrigin = 'anonymous'; |
| 25 | 52 | } |
| @@ -33,14 +60,20 @@ | ||
| 33 | 60 | var alreadyExistScriptElm = bodyElm ? bodyElm.querySelector('script#$id'):null; |
| 34 | 61 | if(alreadyExistScriptElm){ |
| 35 | 62 | bodyElm.removeChild(alreadyExistScriptElm) |
| 36 | 63 | } |
| 37 | -bodyElm.appendChild(script); | |
| 64 | +if(!(window.recaptcha && id === 'g-recaptcha-script')){ | |
| 65 | + bodyElm.appendChild(script); | |
| 66 | +} | |
| 38 | 67 | LOAD_SECRIPT; |
| 39 | 68 | } |
| 40 | 69 | |
| 41 | - public static function minifyJs($content) { | |
| 42 | - $output = preg_replace( | |
| 70 | + public static function minifyJs($input) | |
| 71 | + { | |
| 72 | + if ('' === trim($input)) { | |
| 73 | + return $input; | |
| 74 | + } | |
| 75 | + return preg_replace( | |
| 43 | 76 | [ |
| 44 | 77 | '/ {2,}/', |
| 45 | 78 | '/\s*=\s*/', |
| 46 | 79 | '/\s*,\s*/', |
| @@ -46,12 +79,10 @@ | ||
| 46 | 79 | '/\s*,\s*/', |
| 47 | 80 | '/\s+(?=\(|\{|\:|\?)|\t|(?:\r?\n[ \t]*)+/s' |
| 48 | 81 | ], |
| 49 | 82 | [' ', '=', ',', ''], |
| 50 | - $content | |
| 83 | + $input | |
| 51 | 84 | ); |
| 52 | - | |
| 53 | - return $output; | |
| 54 | 85 | } |
| 55 | 86 | |
| 56 | 87 | /** |
| 57 | 88 | * @method name : saveFile |
| @@ -58,23 +89,36 @@ | ||
| 58 | 89 | * @description : save js/css field to disk |
| 59 | 90 | * @param : $path => like(dirName/css), $fileName => main.css, $script |
| 60 | 91 | * @return : boolean |
| 61 | 92 | */ |
| 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); | |
| 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 | + } | |
| 70 | 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; | |
| 71 | 120 | } |
| 72 | - $fullPath = $rootDir . $fileName; | |
| 73 | - $file = fopen($fullPath, $fileOpenMode); | |
| 74 | - fwrite($file, $script); | |
| 75 | - fclose($file); | |
| 76 | - return true; | |
| 77 | 121 | } |
| 78 | 122 | |
| 79 | 123 | /** |
| 80 | 124 | * @method name : generatePathDirOrFile |
| @@ -81,9 +125,10 @@ | ||
| 81 | 125 | * @dscription : generate path for js/css file |
| 82 | 126 | * @params : $path => like(dirName/css) |
| 83 | 127 | * @return : a string of full path |
| 84 | 128 | */ |
| 85 | - public static function generatePathDirOrFile($path) { | |
| 129 | + public static function generatePathDirOrFile($path) | |
| 130 | + { | |
| 86 | 131 | $rootDir = BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR; |
| 87 | 132 | $path = trim($path, '/'); |
| 88 | 133 | $pathArr = explode('/', $path); // like "fieldname/user => [Fieldname, user] |
| 89 | 134 | foreach ($pathArr as $d) { |
| @@ -91,9 +136,10 @@ | ||
| 91 | 136 | } |
| 92 | 137 | return rtrim($rootDir, DIRECTORY_SEPARATOR); |
| 93 | 138 | } |
| 94 | 139 | |
| 95 | - public static function fileRead($filePath) { | |
| 140 | + public static function fileRead($filePath) | |
| 141 | + { | |
| 96 | 142 | $fileContent = ''; |
| 97 | 143 | if (file_exists($filePath)) { |
| 98 | 144 | $file = fopen($filePath, 'r'); |
| 99 | 145 | $fileContent .= fread($file, filesize($filePath)); |
| @@ -101,9 +147,10 @@ | ||
| 101 | 147 | } |
| 102 | 148 | return $fileContent; |
| 103 | 149 | } |
| 104 | 150 | |
| 105 | - public static function getDataFromNestedPath($data, $key) { | |
| 151 | + public static function getDataFromNestedPath($data, $key) | |
| 152 | + { | |
| 106 | 153 | $keys = explode('->', $key); |
| 107 | 154 | $lastKey = array_pop($keys); |
| 108 | 155 | $dataType = is_array($data) ? 'array' : (is_object($data) ? 'object' : ''); |
| 109 | 156 | if ('array' === $dataType) { |
| @@ -113,9 +160,10 @@ | ||
| 113 | 160 | return self::accessFromObject($data, $keys, $lastKey); |
| 114 | 161 | } |
| 115 | 162 | } |
| 116 | 163 | |
| 117 | - private static function accessFromObject($data, $keys, $lastKey) { | |
| 164 | + private static function accessFromObject($data, $keys, $lastKey) | |
| 165 | + { | |
| 118 | 166 | foreach ($keys as $k) { |
| 119 | 167 | if (!property_exists($data, $k)) { |
| 120 | 168 | return null; |
| 121 | 169 | } |
| @@ -123,9 +171,10 @@ | ||
| 123 | 171 | } |
| 124 | 172 | return isset($data->$lastKey) ? $data->$lastKey : null; |
| 125 | 173 | } |
| 126 | 174 | |
| 127 | - private static function accessFromArray($data, $keys, $lastKey) { | |
| 175 | + private static function accessFromArray($data, $keys, $lastKey) | |
| 176 | + { | |
| 128 | 177 | foreach ($keys as $k) { |
| 129 | 178 | if (!array_key_exists($k, $data)) { |
| 130 | 179 | return null; |
| 131 | 180 | } |
| @@ -133,9 +182,10 @@ | ||
| 133 | 182 | } |
| 134 | 183 | return isset($data[$lastKey]) ? $data[$lastKey] : null; |
| 135 | 184 | } |
| 136 | 185 | |
| 137 | - public static function setDataToNestedPath($data, $key, $value) { | |
| 186 | + public static function setDataToNestedPath($data, $key, $value) | |
| 187 | + { | |
| 138 | 188 | $keys = explode('->', $key); |
| 139 | 189 | $lastKey = array_pop($keys); |
| 140 | 190 | foreach ($keys as $k) { |
| 141 | 191 | if (!array_key_exists($k, $data)) { |
| @@ -142,13 +192,15 @@ | ||
| 142 | 192 | $data->$k = (object) []; |
| 143 | 193 | } |
| 144 | 194 | $data = $data->$k; |
| 145 | 195 | } |
| 146 | - $data->$lastKey = json_decode(json_encode($value)); ; | |
| 196 | + $data->$lastKey = json_decode(wp_json_encode($value)); | |
| 197 | + ; | |
| 147 | 198 | return $data; |
| 148 | 199 | } |
| 149 | 200 | |
| 150 | - public static function property_exists_nested($obj, $path = '', $valToCheck = null, $checkNegativeVal = 0) { | |
| 201 | + public static function property_exists_nested($obj, $path = '', $valToCheck = null, $checkNegativeVal = 0) | |
| 202 | + { | |
| 151 | 203 | $path = explode('->', $path); |
| 152 | 204 | $current = $obj; |
| 153 | 205 | foreach ($path as $key) { |
| 154 | 206 | if (is_object($current)) { |
| @@ -169,14 +221,62 @@ | ||
| 169 | 221 | } |
| 170 | 222 | return true; |
| 171 | 223 | } |
| 172 | 224 | |
| 173 | - public static function honeypotEncryptedToken($str) { | |
| 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 | + { | |
| 174 | 273 | $token = base64_encode(base64_encode($str)); |
| 175 | 274 | return $token; |
| 176 | 275 | } |
| 177 | 276 | |
| 178 | - public static function csrfEecrypted() { | |
| 277 | + public static function csrfEecrypted() | |
| 278 | + { | |
| 179 | 279 | $secretKey = get_option('bf_csrf_secret'); |
| 180 | 280 | if (!$secretKey) { |
| 181 | 281 | $secretKey = 'bf-' . time(); |
| 182 | 282 | update_option('bf_csrf_secret', $secretKey); |
| @@ -182,15 +282,25 @@ | ||
| 182 | 282 | update_option('bf_csrf_secret', $secretKey); |
| 183 | 283 | } |
| 184 | 284 | $tIdenty = base64_encode(random_bytes(32)); |
| 185 | 285 | $csrf = \base64_encode(\hash_hmac('sha256', $tIdenty, $secretKey, true)); |
| 186 | - return ['csrf' => $csrf, 't_identy' => $tIdenty]; | |
| 286 | + return ['csrf' => $csrf, 't_identity' => $tIdenty]; | |
| 187 | 287 | } |
| 188 | 288 | |
| 189 | - public static function csrfDecrypted($identy, $token) { | |
| 289 | + public static function csrfDecrypted($identy, $token) | |
| 290 | + { | |
| 190 | 291 | $secretKey = get_option('bf_csrf_secret'); |
| 191 | 292 | return \hash_equals( |
| 192 | 293 | \base64_encode(\hash_hmac('sha256', $identy, $secretKey, true)), |
| 193 | 294 | $token |
| 194 | 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; | |
| 195 | 305 | } |
| 196 | 306 | } |