| @@ -49,18 +49,21 @@ | ||
| 49 | 49 | |
| 50 | 50 | return array_key_first($array); |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | - public static function safePregReplace($replace_regex, $replace_string, $element) { | |
| 54 | - if (!is_string($replace_regex) || !is_string($replace_string) || !is_string($element)) { | |
| 53 | + public static function safePregReplace($replace_regex, $replace_string, $element, $limit = -1) { | |
| 54 | + if (!is_string($replace_regex) || !is_string($replace_string) || !is_string($element) || !is_int($limit)) { | |
| 55 | 55 | return $element; |
| 56 | 56 | } |
| 57 | - $updated_element = preg_replace($replace_regex, $replace_string, $element); | |
| 57 | + | |
| 58 | + $updated_element = preg_replace($replace_regex, $replace_string, $element, $limit); | |
| 59 | + | |
| 58 | 60 | if ($updated_element === null && preg_last_error() !== PREG_NO_ERROR) { |
| 59 | 61 | return $element; |
| 60 | 62 | } |
| 63 | + | |
| 61 | 64 | return $updated_element; |
| 62 | - } | |
| 65 | + } | |
| 63 | 66 | |
| 64 | 67 | public static function safeStrReplace($search, $replace, $subject) { |
| 65 | 68 | if (!is_string($search) || !is_string($replace) || !is_string($subject)) { |
| 66 | 69 | return $subject; |
| @@ -121,11 +124,14 @@ | ||
| 121 | 124 | public static function fileRemovePattern($fname, $pattern, $is_regex = false) { |
| 122 | 125 | if (!is_string($fname) || !is_string($pattern)) { |
| 123 | 126 | return; |
| 124 | 127 | } |
| 125 | - if (!file_exists($fname)) return; | |
| 126 | 128 | |
| 127 | - $content = file_get_contents($fname); | |
| 129 | + if (!WPRWPFileSystem::getInstance()->exists($fname)) { | |
| 130 | + return; | |
| 131 | + } | |
| 132 | + | |
| 133 | + $content = WPRWPFileSystem::getInstance()->getContents($fname); | |
| 128 | 134 | if ($content !== false) { |
| 129 | 135 | if ($is_regex !== false) { |
| 130 | 136 | $modified_content = preg_replace($pattern, "", $content); |
| 131 | 137 | } else { |
| @@ -136,9 +142,10 @@ | ||
| 136 | 142 | return; |
| 137 | 143 | } |
| 138 | 144 | |
| 139 | 145 | if ($content !== $modified_content) { |
| 140 | - file_put_contents($fname, $modified_content); | |
| 146 | + WPRWPFileSystem::getInstance()->putContents($fname, $modified_content, | |
| 147 | + WPRWPFileSystem::getInstance()->getchmodOctal($fname)); | |
| 141 | 148 | } |
| 142 | 149 | } |
| 143 | 150 | } |
| 144 | 151 | |
| @@ -217,6 +224,130 @@ | ||
| 217 | 224 | } |
| 218 | 225 | |
| 219 | 226 | return array(true, $decrypted_data); |
| 220 | 227 | } |
| 228 | + | |
| 229 | + public static function get_direct_filesystem() { | |
| 230 | + require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php'; | |
| 231 | + require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php'; | |
| 232 | + return new WP_Filesystem_Direct(new StdClass()); | |
| 233 | + } | |
| 234 | + | |
| 235 | + /** | |
| 236 | + * Maybe unslash a value if WordPress is loaded | |
| 237 | + * | |
| 238 | + * @param string $value The value to potentially unslash | |
| 239 | + * @return string The unslashed value if WP is loaded, original value otherwise | |
| 240 | + */ | |
| 241 | + public static function maybeUnslashValue($value) { | |
| 242 | + if (function_exists('wp_unslash')) { | |
| 243 | + return wp_unslash($value); | |
| 244 | + } | |
| 245 | + return $value; | |
| 246 | + } | |
| 247 | + | |
| 248 | + /** | |
| 249 | + * Get and sanitize a string parameter from superglobal | |
| 250 | + * | |
| 251 | + * @param string $superglobal The superglobal type ('GET', 'POST', etc.) | |
| 252 | + * @param string $key The parameter key to retrieve | |
| 253 | + * @param string $context The sanitization context ('text', 'email', 'url') | |
| 254 | + * @return string|null Sanitized string value or null if invalid or unknown context | |
| 255 | + */ | |
| 256 | + public static function getStringParamSanitized($superglobal, $key, $context) { | |
| 257 | + $raw_value = self::getRawParam($superglobal, $key); | |
| 258 | + | |
| 259 | + if (!is_string($raw_value)) { | |
| 260 | + return null; | |
| 261 | + } | |
| 262 | + | |
| 263 | + switch ($context) { | |
| 264 | + case 'text': | |
| 265 | + if (!function_exists('sanitize_text_field')) { | |
| 266 | + return null; | |
| 267 | + } | |
| 268 | + return sanitize_text_field($raw_value); | |
| 269 | + case 'email': | |
| 270 | + if (!function_exists('sanitize_email')) { | |
| 271 | + return null; | |
| 272 | + } | |
| 273 | + return sanitize_email($raw_value); | |
| 274 | + case 'url': | |
| 275 | + if (!function_exists('esc_url_raw')) { | |
| 276 | + return null; | |
| 277 | + } | |
| 278 | + return esc_url_raw($raw_value); | |
| 279 | + default: | |
| 280 | + return null; | |
| 281 | + } | |
| 282 | + } | |
| 283 | + | |
| 284 | + /** | |
| 285 | + * Get and escape a string parameter from superglobal | |
| 286 | + * | |
| 287 | + * @param string $superglobal The superglobal type ('GET', 'POST', etc.) | |
| 288 | + * @param string $key The parameter key to retrieve | |
| 289 | + * @param string $context The escaping context ('attr', 'html', 'url') | |
| 290 | + * @return string|null Escaped string value or null if invalid or unknown context | |
| 291 | + */ | |
| 292 | + public static function getStringParamEscaped($superglobal, $key, $context) { | |
| 293 | + $raw_value = self::getRawParam($superglobal, $key); | |
| 294 | + | |
| 295 | + if (!is_string($raw_value)) { | |
| 296 | + return null; | |
| 297 | + } | |
| 298 | + | |
| 299 | + switch ($context) { | |
| 300 | + case 'attr': | |
| 301 | + if (!function_exists('esc_attr')) { | |
| 302 | + return null; | |
| 303 | + } | |
| 304 | + return esc_attr($raw_value); | |
| 305 | + case 'html': | |
| 306 | + if (!function_exists('esc_html')) { | |
| 307 | + return null; | |
| 308 | + } | |
| 309 | + return esc_html($raw_value); | |
| 310 | + case 'url': | |
| 311 | + if (!function_exists('esc_url')) { | |
| 312 | + return null; | |
| 313 | + } | |
| 314 | + return esc_url($raw_value); | |
| 315 | + default: | |
| 316 | + return null; | |
| 317 | + } | |
| 318 | + } | |
| 319 | + | |
| 320 | + // phpcs:disable WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Missing | |
| 321 | + /** | |
| 322 | + * Get raw parameter value from superglobal | |
| 323 | + * | |
| 324 | + * @param string $superglobal The superglobal type ('GET', 'POST', etc.) | |
| 325 | + * @param string $key The parameter key to retrieve | |
| 326 | + * @return mixed Raw parameter value or null if not found | |
| 327 | + */ | |
| 328 | + public static function getRawParam($superglobal, $key) { | |
| 329 | + $value = null; | |
| 330 | + | |
| 331 | + switch (strtoupper($superglobal)) { | |
| 332 | + case 'GET': | |
| 333 | + $value = isset($_GET[$key]) ? $_GET[$key] : null; | |
| 334 | + break; | |
| 335 | + case 'POST': | |
| 336 | + $value = isset($_POST[$key]) ? $_POST[$key] : null; | |
| 337 | + break; | |
| 338 | + case 'COOKIE': | |
| 339 | + $value = isset($_COOKIE[$key]) ? $_COOKIE[$key] : null; | |
| 340 | + break; | |
| 341 | + case 'REQUEST': | |
| 342 | + $value = isset($_REQUEST[$key]) ? $_REQUEST[$key] : null; | |
| 343 | + break; | |
| 344 | + case 'SERVER': | |
| 345 | + $value = isset($_SERVER[$key]) ? $_SERVER[$key] : null; | |
| 346 | + break; | |
| 347 | + } | |
| 348 | + | |
| 349 | + return $value !== null ? self::maybeUnslashValue($value) : null; | |
| 350 | + } | |
| 351 | + // phpcs:enable WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Missing | |
| 221 | 352 | } |
| 222 | 353 | endif; |