| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm; |
| 4 |
|
| 5 |
use BitCode\BitForm\Core\Util\Log; |
| 6 |
|
| 7 |
if (!defined('ABSPATH')) { |
| 8 |
exit; |
| 9 |
} |
| 10 |
|
| 11 |
class GlobalHelper |
| 12 |
{ |
| 13 |
/** |
| 14 |
* Get all forms. |
| 15 |
* |
| 16 |
* @return array |
| 17 |
*/ |
| 18 |
public static function getForms() |
| 19 |
{ |
| 20 |
global $wpdb; |
| 21 |
|
| 22 |
// Direct query: table name interpolation only (no user input). $wpdb->prepare() cannot parameterize table names. |
| 23 |
$allForms = $wpdb->get_results("SELECT forms.id,forms.entries as fm_entries,forms.form_name,forms.status,forms.views,forms.created_at,COUNT(entries.id) as entries |
| 24 |
FROM `{$wpdb->prefix}bitforms_form` as forms |
| 25 |
LEFT JOIN `{$wpdb->prefix}bitforms_form_entries` as entries ON forms.id = entries.form_id |
| 26 |
GROUP BY forms.id"); |
| 27 |
|
| 28 |
if (is_wp_error($allForms)) { |
| 29 |
return $allForms; |
| 30 |
} |
| 31 |
$forms = array_reduce($allForms, function ($carry, $form) { |
| 32 |
$carry[$form->id] = $form->form_name . ' (' . $form->id . ')'; |
| 33 |
return $carry; |
| 34 |
}, []); |
| 35 |
|
| 36 |
if (!empty($forms)) { |
| 37 |
$forms[0] = __('Select a Bitform', 'bit-form'); |
| 38 |
} |
| 39 |
return $forms; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Request data formatting. |
| 44 |
* |
| 45 |
* Receives raw data from ajax request and formats it into an object. |
| 46 |
* @param mixed $data |
| 47 |
* @return object |
| 48 |
*/ |
| 49 |
public static function formatRequestData(): object |
| 50 |
{ |
| 51 |
// JSON body from AJAX request; nonce verified upstream by the calling AJAX handler before this method is invoked. |
| 52 |
// wp_unslash only — sanitize_text_field strips HTML tags and would destroy rich-text content (email template bodies). |
| 53 |
$data = isset($_POST['data']) ? wp_unslash($_POST['data']) : null; |
| 54 |
if (null === $data) { |
| 55 |
throw new \InvalidArgumentException('The "data" parameter is required.'); |
| 56 |
} |
| 57 |
|
| 58 |
if (is_array($data)) { |
| 59 |
return (object) $data; |
| 60 |
} |
| 61 |
$userData = $data; |
| 62 |
$result = json_decode($userData); |
| 63 |
if (JSON_ERROR_NONE !== json_last_error()) { |
| 64 |
throw new \InvalidArgumentException('Invalid JSON provided in data.'); |
| 65 |
} |
| 66 |
if ('object' !== gettype($result)) { |
| 67 |
$emptyObject = new \stdClass(); |
| 68 |
Log::debug_log([ |
| 69 |
'message'=> 'Invalid JSON provided in data. Returning empty object.', |
| 70 |
'data' => $userData, |
| 71 |
'result' => $result, |
| 72 |
'type' => gettype($result) |
| 73 |
]); |
| 74 |
return $emptyObject; |
| 75 |
} |
| 76 |
return $result; |
| 77 |
|
| 78 |
// // Normalize to array |
| 79 |
// if (is_string($userData)) { |
| 80 |
// $decoded = json_decode($userData, true); |
| 81 |
// if (JSON_ERROR_NONE !== json_last_error()) { |
| 82 |
// throw new \InvalidArgumentException('Invalid JSON provided in data.'); |
| 83 |
// } |
| 84 |
// $userData = $decoded ?? []; |
| 85 |
// } elseif (is_object($userData)) { |
| 86 |
// $userData = (array) $userData; |
| 87 |
// } |
| 88 |
|
| 89 |
// if (!is_array($userData)) { |
| 90 |
// // You may want to throw here instead of returning empty object |
| 91 |
// return new \stdClass(); |
| 92 |
// } |
| 93 |
|
| 94 |
// // Recursive sanitizer — always returns objects |
| 95 |
// $sanitizeRecursive = function ($value) use (&$sanitizeRecursive) { |
| 96 |
// if (is_array($value)) { |
| 97 |
// $clean = new \stdClass(); |
| 98 |
// foreach ($value as $k => $v) { |
| 99 |
// $clean->$k = $sanitizeRecursive($v); |
| 100 |
// } |
| 101 |
// return $clean; |
| 102 |
// } elseif (is_object($value)) { |
| 103 |
// $clean = new \stdClass(); |
| 104 |
// foreach ($value as $k => $v) { |
| 105 |
// $clean->$k = $sanitizeRecursive($v); |
| 106 |
// } |
| 107 |
// return $clean; |
| 108 |
// } else { |
| 109 |
// return sanitize_text_field($value); |
| 110 |
// } |
| 111 |
// }; |
| 112 |
|
| 113 |
// return $sanitizeRecursive($userData); // already an object |
| 114 |
} |
| 115 |
|
| 116 |
public static function requirePostMethod(): void |
| 117 |
{ |
| 118 |
if (!isset($_SERVER['REQUEST_METHOD']) || 'POST' !== sanitize_text_field(wp_unslash($_SERVER['REQUEST_METHOD']))) { |
| 119 |
Log::debug_log('Invalid request method. POST required.'); |
| 120 |
wp_send_json_error(__('Invalid request method. POST required.', 'bit-form'), 405); |
| 121 |
return; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
public static function sanitize_files_input(array $files): array |
| 126 |
{ |
| 127 |
$sanitized = []; |
| 128 |
foreach ($files as $field_key => $file_data) { |
| 129 |
$field_key = sanitize_key((string) $field_key); |
| 130 |
if (!isset($file_data['name'])) { |
| 131 |
continue; |
| 132 |
} |
| 133 |
if (is_array($file_data['name'])) { |
| 134 |
$sanitized[$field_key] = [ |
| 135 |
'name' => array_map(function ($v) { |
| 136 |
return is_array($v) ? array_map('sanitize_file_name', $v) : sanitize_file_name((string) $v); |
| 137 |
}, $file_data['name']), |
| 138 |
'type' => array_map(function ($v) { |
| 139 |
return is_array($v) ? array_map('sanitize_mime_type', $v) : sanitize_mime_type((string) $v); |
| 140 |
}, $file_data['type']), |
| 141 |
'tmp_name' => array_map(function ($v) { |
| 142 |
return is_array($v) ? array_map('sanitize_text_field', $v) : sanitize_text_field((string) $v); |
| 143 |
}, $file_data['tmp_name']), |
| 144 |
'error' => array_map(function ($v) { |
| 145 |
return is_array($v) ? array_map('absint', $v) : absint($v); |
| 146 |
}, $file_data['error']), |
| 147 |
'size' => array_map(function ($v) { |
| 148 |
return is_array($v) ? array_map('absint', $v) : absint($v); |
| 149 |
}, $file_data['size']), |
| 150 |
]; |
| 151 |
if (isset($file_data['full_path'])) { |
| 152 |
$sanitized[$field_key]['full_path'] = array_map(function ($v) { |
| 153 |
return is_array($v) ? array_map('sanitize_text_field', $v) : sanitize_text_field((string) $v); |
| 154 |
}, $file_data['full_path']); |
| 155 |
} |
| 156 |
if (isset($file_data['new_name'])) { |
| 157 |
$sanitized[$field_key]['new_name'] = array_map(function ($v) { |
| 158 |
return is_array($v) ? array_map('sanitize_file_name', $v) : sanitize_file_name((string) $v); |
| 159 |
}, $file_data['new_name']); |
| 160 |
} |
| 161 |
if (isset($file_data['file_path'])) { |
| 162 |
$sanitized[$field_key]['file_path'] = array_map(function ($v) { |
| 163 |
return is_array($v) ? array_map('sanitize_text_field', $v) : sanitize_text_field((string) $v); |
| 164 |
}, $file_data['file_path']); |
| 165 |
} |
| 166 |
} else { |
| 167 |
$sanitized[$field_key] = [ |
| 168 |
'name' => sanitize_file_name($file_data['name']), |
| 169 |
'type' => sanitize_mime_type($file_data['type']), |
| 170 |
'tmp_name' => sanitize_text_field($file_data['tmp_name']), |
| 171 |
'error' => absint($file_data['error']), |
| 172 |
'size' => absint($file_data['size']), |
| 173 |
]; |
| 174 |
if (isset($file_data['full_path'])) { |
| 175 |
$v = $file_data['full_path']; |
| 176 |
$sanitized[$field_key]['full_path'] = is_array($v) ? array_map('sanitize_text_field', $v) : sanitize_text_field($v); |
| 177 |
} |
| 178 |
if (isset($file_data['new_name'])) { |
| 179 |
$v = $file_data['new_name']; |
| 180 |
$sanitized[$field_key]['new_name'] = is_array($v) ? array_map('sanitize_file_name', $v) : sanitize_file_name($v); |
| 181 |
} |
| 182 |
if (isset($file_data['file_path'])) { |
| 183 |
$v = $file_data['file_path']; |
| 184 |
$sanitized[$field_key]['file_path'] = is_array($v) ? array_map('sanitize_text_field', $v) : sanitize_text_field($v); |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
return $sanitized; |
| 189 |
} |
| 190 |
} |
| 191 |
|