PluginProbe
Media Cloud Sync / 1.2.0
Media Cloud Sync v1.2.0
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.0, at includes/api.php

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