PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.8.5
ووسلام – همگام سازی ووکامرس و باسلام v1.8.5
1.10.19 1.10.20 1.10.18 1.10.17 1.10.15 1.10.14 1.10.13 1.10.12 1.10.10 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.2 1.9.1 1.9.0 1.8.8 1.8.5 1.8.6 All 53 releases
sync-basalam / includes / Endpoints / InformationEndpoint.php

InformationEndpoint.php in ووسلام – همگام سازی ووکامرس و باسلام 1.8.5, at includes/Endpoints/InformationEndpoint.php

184 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 SyncBasalam\Endpoints;
4
5 use SyncBasalam\Admin\Product\Category\CategoryMapping;
6 use SyncBasalam\Admin\Settings\SettingsConfig;
7 use SyncBasalam\Services\Api\RequestStatusTracker;
8 use SyncBasalam\Utilities\DateConverter;
9 use SyncBasalam\Services\ApiServiceManager;
10
11 defined('ABSPATH') || exit;
12
13 class InformationEndpoint
14 {
15 private const TOKEN_VALIDATION_URL = 'https://api.hamsalam.ir/api/v1/woo-plugin/validate-token';
16 private const SENSITIVE_SETTING_KEYS = [
17 SettingsConfig::TOKEN,
18 SettingsConfig::REFRESH_TOKEN,
19 SettingsConfig::HAMSALAM_TOKEN,
20 SettingsConfig::WEBHOOK_HEADER_TOKEN,
21 ];
22
23 public static function registerRoutes(): void
24 {
25 register_rest_route(
26 'sync-basalam',
27 '/v1/plugin-status',
28 [
29 'methods' => 'GET',
30 'callback' => [__CLASS__, 'getPluginStatus'],
31 'permission_callback' => [__CLASS__, 'checkPermissions'],
32 ]
33 );
34 }
35
36 public static function checkPermissions($request)
37 {
38 $token = self::extractRequestToken($request);
39
40 if (empty($token)) {
41 return new \WP_Error(
42 'sync_basalam_missing_status_token',
43 'Token is required.',
44 ['status' => 403]
45 );
46 }
47
48 if (!self::validateStatusEndpointToken($token)) {
49 return new \WP_Error(
50 'sync_basalam_invalid_status_token',
51 'توکن �
52 عتبر نیست.',
53 ['status' => 403]
54 );
55 }
56
57 return true;
58 }
59
60 public static function getPluginStatus($request)
61 {
62 global $wp_version;
63
64 $settings = syncBasalamSettings()->getSettings();
65
66 return rest_ensure_response([
67 'domain' => get_site_url(),
68 'woosalam_version' => syncBasalamPlugin()->getVersion(),
69 'wordpress_version' => $wp_version,
70 'woocommerce_version' => defined('WC_VERSION') ? WC_VERSION : null,
71 'connection_status' => !empty($settings[SettingsConfig::TOKEN]),
72 'settings' => self::getPublicSettings($settings),
73 'request_status' => RequestStatusTracker::getSummary(),
74 'last_10_log' => [
75 'info' => self::getLastLogsByLevel('INFO', 10),
76 'error' => self::getLastLogsByLevel('ERROR', 10),
77 ],
78 'category_mapping_list' => CategoryMapping::getCategoryMappings(),
79 ]);
80 }
81
82 private static function getPublicSettings(array $settings): array
83 {
84 foreach (self::SENSITIVE_SETTING_KEYS as $settingKey) {
85 unset($settings[$settingKey]);
86 }
87
88 return $settings;
89 }
90
91 private static function extractRequestToken($request): ?string
92 {
93 $headers = $request->get_headers();
94
95 if (!empty($headers['token'][0])) {
96 return sanitize_text_field($headers['token'][0]);
97 }
98
99 if (!empty($headers['authorization'][0])) {
100 $authorization = sanitize_text_field($headers['authorization'][0]);
101
102 if (stripos($authorization, 'Bearer ') === 0) {
103 return trim(substr($authorization, 7));
104 }
105
106 return $authorization;
107 }
108
109 $paramToken = $request->get_param('token');
110
111 return !empty($paramToken) ? sanitize_text_field($paramToken) : null;
112 }
113
114 private static function validateStatusEndpointToken(string $token): bool
115 {
116 $url = add_query_arg(
117 ['token' => $token],
118 self::TOKEN_VALIDATION_URL
119 );
120
121
122 $apiServiceManager = syncBasalamContainer()->get(ApiServiceManager::class);
123
124 $response = $apiServiceManager->get($url);
125
126 if ($response['success'] == true) {
127 return true;
128 }
129
130 return false;
131 }
132
133
134 private static function getLastLogsByLevel(string $targetLevel, int $limit = 10): array
135 {
136 $uploadDir = wp_upload_dir();
137 $logDir = trailingslashit($uploadDir['basedir']) . 'wc-logs/';
138 $logFiles = glob($logDir . 'basalam-sync-plugin*.log');
139
140 if (empty($logFiles)) {
141 return [];
142 }
143
144 usort($logFiles, fn($a, $b) => filemtime($b) <=> filemtime($a));
145
146 $logs = [];
147
148 foreach ($logFiles as $logFile) {
149 if (!is_readable($logFile)) {
150 continue;
151 }
152
153 $lines = file($logFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
154
155 foreach (array_reverse($lines) as $line) {
156 if (!preg_match(
157 '/^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{2}:\d{2}) (INFO|WARNING|ERROR|DEBUG|ALERT) (.*?)( CONTEXT: (.*))?$/',
158 $line,
159 $matches
160 )) {
161 continue;
162 }
163
164 if ($matches[2] !== $targetLevel) {
165 continue;
166 }
167
168 $logs[] = [
169 'date' => DateConverter::utcToJalaliDateTime($matches[1]),
170 'level' => strtolower($matches[2]),
171 'message' => $matches[3],
172 'context' => isset($matches[5]) ? json_decode($matches[5], true) : null,
173 ];
174
175 if (count($logs) >= $limit) {
176 return $logs;
177 }
178 }
179 }
180
181 return $logs;
182 }
183 }
184