PluginProbe
Media Cloud Sync / 1.2.4
Media Cloud Sync v1.2.4
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.4, at includes/api.php

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