PluginProbe
WP Synchro – The Ultimate WordPress Migration Tool / trunk
WP Synchro – The Ultimate WordPress Migration Tool vtrunk
1.16.1 1.16.0 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.1.0 1.10.0 1.11.0 1.11.1 1.11.2 1.11.3 1.11.4 1.11.5 1.12.0 1.13.0 1.14.0 1.15.0 1.2.0 1.3.0 1.3.1 1.3.2 All 45 releases
wpsynchro / src / API / LoadAPI.php

LoadAPI.php in WP Synchro – The Ultimate WordPress Migration Tool trunk, at src/API/LoadAPI.php

251 lines 9.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class for handling API for WP Synchro
5 */
6
7 namespace WPSynchro\API;
8
9 use WPSynchro\Transport\TransferToken;
10
11 class LoadAPI
12 {
13 /**
14 * Get endpoints
15 */
16 public function getEndpoints()
17 {
18 return [
19 'wpsynchro_initiate' => [
20 'check_permission' => false,
21 'class' => '\WPSynchro\API\Initiate',
22 ],
23 'wpsynchro_masterdata' => [
24 'check_permission' => true,
25 'class' => '\WPSynchro\API\MasterData',
26 ],
27 'wpsynchro_backupdatabase' => [
28 'check_permission' => true,
29 'class' => '\WPSynchro\API\DatabaseBackup',
30 ],
31 'wpsynchro_db_sync' => [
32 'check_permission' => true,
33 'class' => '\WPSynchro\API\ClientSyncDatabase',
34 ],
35 'wpsynchro_file_populate' => [
36 'check_permission' => true,
37 'class' => '\WPSynchro\API\PopulateFileList',
38 ],
39 'wpsynchro_file_populate_status' => [
40 'check_permission' => true,
41 'class' => '\WPSynchro\API\PopulateFileListStatus',
42 ],
43 'wpsynchro_file_push' => [
44 'check_permission' => true,
45 'class' => '\WPSynchro\API\FileTransfer',
46 ],
47 'wpsynchro_file_pull' => [
48 'check_permission' => true,
49 'class' => '\WPSynchro\API\GetFiles',
50 ],
51 'wpsynchro_file_finalize' => [
52 'check_permission' => true,
53 'class' => '\WPSynchro\API\FileFinalize',
54 ],
55 'wpsynchro_frontend_filesystem' => [
56 'check_permission' => function ($token) {
57 if ($this->permissionCheck($token)) {
58 return true;
59 } else {
60 $nonce = $_REQUEST['nonce'] ?? '';
61 if (!wp_verify_nonce($nonce, 'wpsynchro-addedit')) {
62 return false;
63 }
64 return current_user_can('manage_options');
65 }
66 },
67 'class' => '\WPSynchro\API\Filesystem',
68 ],
69 'wpsynchro_frontend_verify_remote' => [
70 'check_permission' => function ($token) {
71 // Check nonce
72 $nonce = $_REQUEST['nonce'] ?? '';
73 if (!wp_verify_nonce($nonce, 'wpsynchro-addedit')) {
74 return false;
75 }
76 return current_user_can('manage_options');
77 },
78 'class' => '\WPSynchro\API\VerifyMigration',
79 ],
80 'wpsynchro_frontend_healthcheck' => [
81 'check_permission' => function ($token) {
82 return current_user_can('manage_options');
83 },
84 'class' => '\WPSynchro\API\HealthCheck',
85 ],
86 'wpsynchro_test' => [
87 'check_permission' => function ($token) {
88 return true;
89 },
90 'class' => function () {
91 echo "it-works";
92 return;
93 },
94 ],
95 'wpsynchro_execute_action' => [
96 'check_permission' => true,
97 'class' => '\WPSynchro\API\ExecuteAction',
98 ],
99 'wpsynchro_frontend_download_log' => [
100 'check_permission' => function ($token) {
101 // Check nonce
102 $nonce = $_REQUEST['nonce'] ?? '';
103 if (!wp_verify_nonce($nonce, 'wpsynchro_download_log')) {
104 return false;
105 }
106 return current_user_can('manage_options');
107 },
108 'class' => '\WPSynchro\API\DownloadLog',
109 ],
110 'wpsynchro_frontend_download_db_backup' => [
111 'check_permission' => function ($token) {
112 // Check nonce
113 $nonce = $_REQUEST['nonce'] ?? '';
114 if (!wp_verify_nonce($nonce, 'wpsynchro_download_db_backup')) {
115 return false;
116 }
117 return current_user_can('manage_options');
118 },
119 'class' => '\WPSynchro\API\DownloadLogDBBackup',
120 ],
121 'wpsynchro_run_synchronize' => [
122 'check_permission' => function ($token) {
123 if ($this->permissionCheck($token)) {
124 return true;
125 } else {
126 return current_user_can('manage_options');
127 }
128 },
129 'class' => '\WPSynchro\API\Migrate',
130 ],
131 'wpsynchro_run_status' => [
132 'check_permission' => function ($token) {
133 if ($this->permissionCheck($token)) {
134 return true;
135 } else {
136 return current_user_can('manage_options');
137 }
138 },
139 'class' => '\WPSynchro\API\Status',
140 ],
141 'wpsynchro_run_status_file_changed_get' => [
142 'check_permission' => true,
143 'class' => function () {
144 $obj = new StatusFileChanges();
145 $obj->getFileChanges();
146 },
147 ],
148 'wpsynchro_run_status_file_changed_accept' => [
149 'check_permission' => true,
150 'class' => function () {
151 $obj = new StatusFileChanges();
152 $obj->acceptFileChanges();
153 },
154 ],
155 'wpsynchro_save_migration' => [
156 'check_permission' => function ($token) {
157 // Check nonce
158 $nonce = $_REQUEST['nonce'] ?? '';
159 if (!wp_verify_nonce($nonce, 'wpsynchro-addedit')) {
160 return false;
161 }
162 return current_user_can('manage_options');
163 },
164 'class' => '\WPSynchro\API\SaveMigration',
165 ],
166 'wpsynchro_scheduled_migration_run' => [
167 'check_permission' => function ($token) {
168 return true;
169 },
170 'class' => '\WPSynchro\API\RunScheduledMigration',
171 ],
172 ];
173 }
174
175 /**
176 * Load and handle API request if it is one
177 */
178 public function setup()
179 {
180 // Check if it is a WP Synchro service request
181 $request_query = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
182 if (strpos(strval($request_query), "action=wpsynchro") !== false) {
183 // We have a WP Synchro action
184 $query_parsed = [];
185 parse_str($request_query, $query_parsed);
186 $action = "";
187 if (isset($query_parsed['action'])) {
188 $action = $query_parsed['action'];
189 }
190
191 $action_to_handler_mapping = $this->getEndpoints();
192
193 // Check if it is known action
194 if (isset($action_to_handler_mapping[$action])) {
195 // Get handler
196 $handler = $action_to_handler_mapping[$action];
197
198 // If we need to check permission, do that first
199 if ($handler['check_permission']) {
200 $token = "";
201 if (isset($_REQUEST['token'])) {
202 $token = $_REQUEST['token'];
203 }
204 // Check if check_permission is a custom function or we just check the token
205 if (is_callable($handler['check_permission'])) {
206 $permission_check_result = $handler['check_permission']($token);
207 } else {
208 $permission_check_result = $this->permissionCheck($token);
209 }
210 if ($permission_check_result != true) {
211 echo "<div class='notice wpsynchro-notice notice-error'><p>" . __('You do not have access to this service or security token is no longer valid - Go back and try again.', 'wpsynchro') . '</p></div>';
212 http_response_code(401);
213 die();
214 }
215 }
216
217 if (is_callable($handler['class'])) {
218 $handler['class']();
219 } else {
220 $handler_class = $handler['class'];
221 $obj = new $handler_class();
222 $obj->service();
223 }
224
225 // Flushy flushy
226 $ob_levels = ob_get_level();
227 for ($i = 0; $i < $ob_levels; $i++) {
228 ob_end_flush();
229 }
230 flush();
231
232 die();
233 }
234 }
235 }
236
237 /**
238 * Validates access to WP Synchro services
239 */
240 public function permissionCheck(string $token)
241 {
242 if ($token == null || strlen($token) < 20) {
243 return false;
244 }
245 $token = trim($token);
246
247 // Check if it is a transfer token
248 return TransferToken::validateTransferToken($token);
249 }
250 }
251