PluginProbe
404 Solution / 4.3.0
404 Solution v4.3.0
4.3.5 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 4.2.0 4.1.19 4.1.18 4.1.17 4.1.16 4.1.15 4.1.13 4.1.12 4.1.11 4.1.10 4.1.9 4.1.8 4.1.7 4.1.6 4.1.5 4.1.4 4.1.3 trunk 2.30.0 All 109 releases
404-solution / includes / feedback / FeedbackRuntimeEnvironment.php

FeedbackRuntimeEnvironment.php in 404 Solution 4.3.0, at includes/feedback/FeedbackRuntimeEnvironment.php

125 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH')) {
4 exit;
5 }
6
7 /**
8 * Collects runtime-environment fields for feedback payloads.
9 */
10 class ABJ_404_Solution_FeedbackRuntimeEnvironment {
11
12 /**
13 * @return array<string, int>
14 */
15 public function resourceLimits(): array {
16 return array(
17 'php_memory' => function_exists('ini_get') ? self::iniSizeToBytesForPayload((string)ini_get('memory_limit')) : 0,
18 'wp_memory' => defined('WP_MEMORY_LIMIT') ? self::iniSizeToBytesForPayload((string)WP_MEMORY_LIMIT) : 0,
19 'php_max_execution_seconds' => function_exists('ini_get') ? max(0, (int)ini_get('max_execution_time')) : 0,
20 'php_post_max_size' => function_exists('ini_get') ? self::iniSizeToBytesForPayload((string)ini_get('post_max_size')) : 0,
21 'php_upload_max_size' => function_exists('ini_get') ? self::iniSizeToBytesForPayload((string)ini_get('upload_max_filesize')) : 0,
22 );
23 }
24
25 public function wpMemoryLimitBytes(): int {
26 if (!defined('WP_MEMORY_LIMIT')) {
27 return 0;
28 }
29 return self::iniSizeToBytesForPayload((string)WP_MEMORY_LIMIT);
30 }
31
32 /**
33 * Convert PHP shorthand byte notation to a non-negative byte count for
34 * the feedback schema. Malformed values and "no limit" normalize to 0.
35 */
36 public static function iniSizeToBytesForPayload(string $value): int {
37 $str = trim($value);
38 if ($str === '') {
39 return 0;
40 }
41
42 if (preg_match('/^([+-]?\d+)\s*([gmk]?)$/i', $str, $matches) !== 1) {
43 return 0;
44 }
45
46 $num = (int)$matches[1];
47 if ($num <= 0) {
48 return 0;
49 }
50
51 $unit = strtolower((string)$matches[2]);
52 switch ($unit) {
53 case 'g':
54 return $num * 1024 * 1024 * 1024;
55 case 'm':
56 return $num * 1024 * 1024;
57 case 'k':
58 return $num * 1024;
59 default:
60 return $num;
61 }
62 }
63
64 /**
65 * @return array<string, bool>
66 */
67 public function loadedExtensionsMap(): array {
68 if (!function_exists('get_loaded_extensions')) {
69 return array();
70 }
71 $names = get_loaded_extensions();
72 $out = array();
73 foreach ($names as $name) {
74 if ($name !== '') {
75 $out[strtolower($name)] = true;
76 }
77 }
78 return $out;
79 }
80
81 /**
82 * Detect a development host so reports can be flagged as non-production.
83 */
84 public function isDevelopmentEnvironment(): bool {
85 $home = function_exists('home_url') ? (string)home_url() : '';
86 $host = '';
87 if (function_exists('wp_parse_url')) {
88 $parsed = wp_parse_url($home, PHP_URL_HOST);
89 $host = is_string($parsed) ? $parsed : '';
90 } else {
91 $parsed = parse_url($home, PHP_URL_HOST);
92 $host = is_string($parsed) ? $parsed : '';
93 }
94 if ($host === '') {
95 return false;
96 }
97 if ($host === 'localhost') {
98 return true;
99 }
100 if (preg_match('/\.(test|local|dev|localhost)$/i', $host) === 1) {
101 return true;
102 }
103 if (defined('WP_DEBUG') && WP_DEBUG && strpos((string)$home, 'localhost') !== false) {
104 return true;
105 }
106 return false;
107 }
108
109 /**
110 * Strip site-identifying noise from the SERVER_SOFTWARE banner.
111 */
112 public function sanitizeServerSoftware(string $raw): string {
113 if ($raw === '') {
114 return '';
115 }
116 $marker = stripos($raw, ' Server at ');
117 $clean = $marker === false ? $raw : substr($raw, 0, $marker);
118 $clean = trim($clean);
119 if (strlen($clean) > 100) {
120 $clean = substr($clean, 0, 100);
121 }
122 return $clean;
123 }
124 }
125