PluginProbe
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity / 3.3.10
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity v3.3.10
3.3.10 3.3.9 3.3.8 trunk 1.0 1.1.0 1.10.0 1.11.0 1.11.1 1.12.0 1.13.0 1.14.0 1.15.0 1.16.0 1.17.0 1.17.1 1.18.0 1.19.0 1.2.0 1.20.0 1.20.1 1.3.0 1.3.1 1.4.0 1.5.0 All 68 releases
logtivity / Core / Services / Logtivity_Rest_Endpoints.php

Logtivity_Rest_Endpoints.php in Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity 3.3.10, at Core/Services/Logtivity_Rest_Endpoints.php

188 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @package Logtivity
5 * @contact logtivity.io, [email protected]
6 * @copyright 2025-2026 Logtivity. All rights reserved
7 * @license https://www.gnu.org/licenses/gpl.html GNU/GPL
8 *
9 * This file is part of Logtivity.
10 *
11 * Logtivity is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * Logtivity is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with Logtivity. If not, see <https://www.gnu.org/licenses/>.
23 */
24
25 class Logtivity_Rest_Endpoints
26 {
27 /**
28 * @var Logtivity_JsonWebToken
29 */
30 protected Logtivity_JsonWebToken $token;
31
32 public function __construct()
33 {
34 require_once __DIR__ . '/Logtivity_JsonWebToken.php';
35
36 $this->token = new Logtivity_JsonWebToken();
37
38 add_action('rest_api_init', function () {
39 register_rest_route(
40 'logtivity/v1',
41 '/options',
42 [
43 'methods' => 'GET',
44 'permission_callback' => function (WP_REST_Request $request) {
45 return $this->verifyAuthorization($request->get_header('Authorization'));
46 },
47 'callback' => function () {
48 try {
49 $options = new Logtivity_Options();
50
51 $response = $options->getOptions();
52
53 $lastCheckinOption = 'logtivity_last_settings_check_in_at';
54
55 $response[$lastCheckinOption] = get_option($lastCheckinOption);
56 $response[$lastCheckinOption] = $response[$lastCheckinOption]['date'] ?? null;
57 $response['logtivity_checkin_delay'] = $options->checkinDelay;
58 $response['version'] = $this->getLogtivityVersion();
59
60 return $response;
61
62 } catch (Throwable $error) {
63 return new WP_Error(
64 'logtivity_error',
65 $error->getMessage(),
66 [
67 'status' => $error->getCode(),
68 'line' => $error->getLine(),
69 'file' => $error->getFile(),
70 ]
71 );
72 }
73 },
74 ]);
75 });
76 }
77
78 /**
79 * @return self
80 */
81 public static function init(): self
82 {
83 return new static();
84 }
85
86 /**
87 * @param ?string $authHeader
88 *
89 * @return true|WP_Error
90 */
91 protected function verifyAuthorization(?string $authHeader)
92 {
93 $apikey = (new Logtivity_Options())->getApiKey();
94
95 if ($apikey) {
96 try {
97 if ($authHeader == false) {
98 throw new Exception('No authorization provided');
99 }
100
101 $keys = explode(' ', $authHeader);
102 if (count($keys) == 2 && $keys[0] == 'Bearer') {
103 $payload = $this->parseToken($keys[1], $apikey);
104
105 $issuer = $payload->iss ?? '';
106 if ($this->compareDomains($issuer, logtivity_get_app_url()) == false) {
107 throw new Exception(
108 sprintf(
109 'Request Source: %s',
110 $issuer ? 'invalid' : 'unidentified'
111 )
112 );
113 }
114
115 $audience = $payload->aud ?? '';
116 if ($this->compareDomains($audience, site_url()) == false) {
117 throw new Exception(
118 sprintf(
119 'Target Site: %s',
120 $audience ? 'incorrect' : 'unidentified'
121 )
122 );
123 }
124
125 } else {
126 throw new Exception('Malformed header');
127 }
128
129 return true;
130
131 } catch (Throwable $e) {
132 return new WP_Error('invalid_token', $e->getMessage(), ['status' => 401]);
133 }
134 }
135
136 return new WP_Error('missing_api_key', 'The API Key has not been set on this site', ['status' => 401]);
137 }
138
139 /**
140 * @param string $token
141 * @param string $secret
142 *
143 * @return ?object
144 * @throws Exception
145 */
146 protected function parseToken(string $token, string $secret): object
147 {
148 if (empty($this->token)) {
149 require_once __DIR__ . '/Logtivity_JsonWebToken.php';
150 $this->token = new Logtivity_JsonWebToken();
151 }
152
153 return $this->token->parse($token, $secret);
154 }
155
156 /**
157 * @param string $urlLeft
158 * @param string $urlRight
159 *
160 * @return bool
161 */
162 protected function compareDomains(string $urlLeft, string $urlRight): bool
163 {
164 if (
165 preg_match('#^(?:https?://)?(\S*?)/?$#', $urlLeft, $left)
166 && preg_match('#^(?:https?://)?(\S*?)/?$#', $urlRight, $right)
167 ) {
168 return $left[1] == $right[1];
169 }
170
171 return false;
172 }
173
174 /**
175 * @return string
176 */
177 protected function getLogtivityVersion(): string
178 {
179 $path = WP_PLUGIN_DIR . '/logtivity/logtivity.php';
180 if (is_file($path)) {
181 $pluginData = get_plugin_data($path);
182 $version = $pluginData['Version'] ?? 'Unknown';
183 }
184
185 return $version ?? 'Logtivity not available';
186 }
187 }
188