PluginProbe
Media Cloud Sync / 1.2.5
Media Cloud Sync v1.2.5
1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 All 34 releases
media-cloud-sync / includes / api.php

api.php in Media Cloud Sync 1.2.5, at includes/api.php

335 lines 9.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Dudlewebs\WPMCS;
3
4 use WP_REST_Response;
5
6 defined('ABSPATH') || exit;
7
8 class Api {
9 private static $instance = null;
10 private $token;
11 private $version;
12 private $assets_url;
13
14 /**
15 * Constructor
16 * @since 1.0.0
17 */
18
19 public function __construct() {
20 $this->assets_url = WPMCS_ASSETS_URL;
21 $this->version = WPMCS_VERSION;
22 $this->token = WPMCS_TOKEN;
23
24 add_action( 'rest_api_init', array( $this, 'register_routes' ) );
25 }
26
27
28 /**
29 * Register API routes
30 */
31
32 public function register_routes() {
33 $this->add_route( '/verifyCredentials', 'verifyCredentials', 'POST' );
34 $this->add_route( '/permissions/checkExist', 'verifyBucketExist', 'POST' );
35 $this->add_route( '/permissions/addNewBucket', 'createBucket', 'POST' );
36 $this->add_route( '/permissions/write', 'verifyObjectWrite', 'POST' );
37 $this->add_route( '/permissions/delete', 'verifyObjectDelete', 'POST' );
38 $this->add_route( '/permissions/read', 'verifyObjectRead', 'POST' );
39
40 $this->add_route( '/security/get_security', 'getBucketSecuritySettings', 'POST' );
41 $this->add_route( '/security/block_public_access', 'changePublicAccess', 'POST' );
42 $this->add_route( '/security/object_ownership_enforce', 'changeObjectOwnership', 'POST' );
43
44 $this->add_route( '/saveConfig', 'saveConfig', 'POST' );
45 $this->add_route( '/getSettings', 'getSettings' );
46 $this->add_route( '/uploadCredentials', 'uploadCredentials', 'POST' );
47 }
48
49 /**
50 * Verify the Service Credentials
51 */
52 public function verifyCredentials( $data ) {
53 return new WP_REST_Response( Service::instance()->verifyCredentials($data->get_params()), 200 );
54 }
55
56 /**
57 * Verify the Bucket exist
58 */
59 public function verifyBucketExist( $data ) {
60 return new WP_REST_Response( Service::instance()->verifyBucketExist($data->get_params()), 200 );
61 }
62
63 /**
64 * Create the bucket
65 */
66 public function createBucket( $data ) {
67 return new WP_REST_Response( Service::instance()->createBucket($data->get_params()), 200 );
68 }
69
70
71 /**
72 * Verify the Bucket Write Permission
73 */
74 public function verifyObjectWrite( $data ) {
75 return new WP_REST_Response( Service::instance()->verifyObjectWritePermission($data->get_params()), 200 );
76 }
77
78 /**
79 * Verify the Bucket delete Permission
80 */
81 public function verifyObjectDelete( $data ) {
82 return new WP_REST_Response( Service::instance()->verifyObjectDeletePermission($data->get_params()), 200 );
83 }
84
85 /**
86 * Verify the Bucket Read Permission
87 */
88 public function verifyObjectRead( $data ) {
89 return new WP_REST_Response( Service::instance()->verifyObjectReadPermission($data->get_params()), 200 );
90 }
91
92 /**
93 * Get the Security Settings
94 */
95 public function getBucketSecuritySettings( $data ) {
96 return new WP_REST_Response( Service::instance()->getBucketSecuritySettings($data->get_params()), 200 );
97 }
98
99
100 /**
101 * Change the Public Access
102 */
103 public function changePublicAccess( $data ) {
104 return new WP_REST_Response( Service::instance()->changePublicAccess($data->get_params()), 200 );
105 }
106
107
108 /**
109 * Change the Object Ownership
110 */
111 public function changeObjectOwnership( $data ) {
112 return new WP_REST_Response( Service::instance()->changeObjectOwnership($data->get_params()), 200 );
113 }
114
115 /**
116 * Get Settings
117 */
118 public function getSettings( ) {
119 $data = [
120 'credentials' => Utils::get_credentials(),
121 'common' => [
122 'version' => WPMCS_VERSION,
123 'uploaded' => Counter::get( 'uploaded' ),
124 'status' => Utils::get_status('', false),
125 'settings' => Utils::get_settings(),
126 ]
127 ];
128
129 return new WP_REST_Response( $data, 200 );
130 }
131
132 /**
133 * Save the Configurations
134 */
135 public function saveConfig( $data ) {
136 $saveData = $data->get_params();
137 $result = [
138 'success' => false,
139 'message' => esc_html__('Something went wrong. Invalid data recieved', 'media-cloud-sync')
140 ];
141
142 $action = isset($saveData['action']) ? $saveData['action'] : false;
143 if($action === false) return new WP_REST_Response( $result, 200 );
144
145 $service = isset($saveData['service']) ? $saveData['service'] : false;
146 $cdn = isset($saveData['cdn']) ? $saveData['cdn'] : [];
147 $config = isset($saveData['config']) ? $saveData['config'] : false;
148 $bucketConfig = isset($saveData['bucketConfig']) ? $saveData['bucketConfig'] : [];
149 $security = isset($saveData['security']) ? $saveData['security'] : [];
150 $settings = isset($saveData['settings']) ? $saveData['settings'] : [];
151
152 $serviceOk = true;
153 $settingsOk = true;
154
155 if( $action === 'cdn' ){
156 $existing = Utils::get_credentials();
157 $existing['cdn'] = $cdn;
158
159 $updated = Utils::update_option('credentials', $existing, Schema::getConstant('GLOBAL_SETTINGS_KEY'));
160 $updatedSettings = Utils::update_option('settings', $settings, Schema::getConstant('GLOBAL_SETTINGS_KEY'));
161
162 if(!$updated) $serviceOk = false;
163 }
164
165
166 if($action === 'all' || $action === 'service'){
167 if ( $service === false || empty($config) || empty($bucketConfig)) return new WP_REST_Response( $result, 200 );
168
169 $updated = Utils::update_option(
170 'credentials',
171 [
172 'service' => $service,
173 'cdn' => $cdn,
174 'config' => $config,
175 'bucketConfig' => $bucketConfig,
176 'security' => $security
177 ],
178 Schema::getConstant('GLOBAL_SETTINGS_KEY')
179 );
180 if(!$updated) $serviceOk = false;
181 }
182
183 if(($action === 'all' || $action === 'settings')) {
184 $updatedSettings = Utils::update_option('settings', $settings, Schema::getConstant('GLOBAL_SETTINGS_KEY'));
185 if(!$updatedSettings) $settingsOk = false;
186 }
187
188
189 if($serviceOk && $settingsOk) {
190 Utils::set_status('cdnRead', [
191 'status' => false,
192 'message' => '',
193 'lastChecked' => null
194 ]);
195 $result = [
196 'success' => true,
197 'message' => esc_html__('Configuration saved successfully', 'media-cloud-sync')
198 ];
199 }
200
201 return new WP_REST_Response( $result, 200 );
202 }
203
204
205 /**
206 * Upload Credentials
207 * @since 1.0.0
208 */
209 public function uploadCredentials($request) {
210 if (isset($_FILES['file']) && !empty($_FILES['file'])) {
211 $config_file = $_FILES['file'];
212 if (isset($config_file['type']) && $config_file['type'] == 'application/json') {
213 if (file_exists($config_file["tmp_name"])) {
214
215 add_filter( 'upload_dir', [ $this, 'change_file_upload_dir' ]);
216 add_filter( 'mime_types', [ $this, 'add_custom_mime_type_json' ]);
217
218 if ( ! function_exists( 'wp_handle_upload' ) ) {
219 require_once( ABSPATH . 'wp-admin/includes/file.php' );
220 }
221
222 $upload_overrides = [
223 'test_form' => false,
224 'test_type' => true,
225 'mimes' => [ 'json'=>'application/json' ]
226 ];
227
228 $movefile = wp_handle_upload( $config_file, $upload_overrides );
229
230 remove_filter( 'upload_dir', [ $this, 'change_file_upload_dir' ]);
231 remove_filter( 'mime_types', [ $this, 'add_custom_mime_type_json' ]);
232
233 if ($movefile && !isset($movefile['error'])) {
234 $result = [
235 'success' => true,
236 'file_path' => $movefile['file'],
237 'file_name' => basename($movefile['file']),
238 ];
239 return new WP_REST_Response($result, 200);
240 } else {
241 $result = [
242 'success' => false,
243 'message' => $movefile['error'],
244 ];
245 return new WP_REST_Response($result, 200);
246 }
247 }
248 } else {
249 $result = [
250 'success' => false,
251 'message' => esc_html__('Invalid file format', 'media-cloud-sync'),
252 ];
253 return new WP_REST_Response($result, 200);
254 }
255 } else {
256 $result = [
257 'success' => false,
258 'message' => esc_html__('Insufficient data. Please try again', 'media-cloud-sync'),
259 ];
260 return new WP_REST_Response($result, 200);
261 }
262 }
263
264 /**
265 * Change File Upload Directory
266 * @since 1.0.0
267 */
268 public function change_file_upload_dir($upload) {
269 $wpmcs_path = '/' . Schema::getConstant('UPLOADS');
270 $path = $upload['basedir'] . $wpmcs_path;
271 $url = $upload['baseurl'] . $wpmcs_path;
272
273 if (!is_dir($path)) {
274 do_action( $this->token.'_create_plugin_dir' );
275 }
276
277 $upload['subdir'] = $wpmcs_path;
278 $upload['path'] = $upload['basedir'] . $wpmcs_path;
279 $upload['url'] = $upload['baseurl'] . $wpmcs_path;
280
281 return $upload;
282 }
283
284 /**
285 * Add Custom Mime Type JSON
286 * @since 1.0.0
287 */
288 public function add_custom_mime_type_json($mimes) {
289 $mimes['json'] = 'application/json';
290 // Return the array back to the function with our added mime type.
291 return $mimes;
292 }
293
294
295 /**
296 * Helper function to create Adding Route
297 * @since 1.0.0
298 */
299 private function add_route( $slug, $callBack, $method = 'GET' ) {
300 register_rest_route(
301 $this->token . '/v1',
302 $slug,
303 array(
304 'methods' => $method,
305 'callback' => array( $this, $callBack ),
306 'permission_callback' => array( $this, 'getPermission' ),
307 ) );
308 }
309
310 /**
311 * Permission Callback
312 **/
313 public function getPermission() {
314 if ( current_user_can( 'administrator' ) ) {
315 return true;
316 } else {
317 return true;
318 }
319 }
320
321 /**
322 * Ensures only one instance of Class is loaded or can be loaded.
323 *
324 * @return Api Class instance
325 * @since 1.0.0
326 * @static
327 */
328 public static function instance(){
329 if (is_null(self::$instance)) {
330 self::$instance = new self();
331 }
332 return self::$instance;
333 }
334 }
335