| 1 |
<?php |
| 2 |
/** |
| 3 |
* API Handler |
| 4 |
* |
| 5 |
* |
| 6 |
* |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
namespace wpCloud\StatelessMedia { |
| 10 |
|
| 11 |
if( !class_exists( 'wpCloud\StatelessMedia\API' ) ) { |
| 12 |
|
| 13 |
/** |
| 14 |
* Class API |
| 15 |
* |
| 16 |
* @package wpCloud\StatelessMedia |
| 17 |
*/ |
| 18 |
final class API { |
| 19 |
|
| 20 |
/** |
| 21 |
* Decoded auth_token data |
| 22 |
* |
| 23 |
* @var null|\stdClass |
| 24 |
*/ |
| 25 |
static private $tokenData = null; |
| 26 |
|
| 27 |
/** |
| 28 |
* Validate auth token and save data for further processing |
| 29 |
* |
| 30 |
* @param \WP_REST_Request $request |
| 31 |
* @return bool|\WP_Error |
| 32 |
*/ |
| 33 |
static public function authCheck( \WP_REST_Request $request ) { |
| 34 |
$auth_token = $request->get_header('authorization'); |
| 35 |
if ( !$auth_token ) return false; |
| 36 |
try { |
| 37 |
self::$tokenData = Utility::verify_jwt_token( $auth_token ); |
| 38 |
} catch ( \Exception $e ) { |
| 39 |
self::$tokenData = null; |
| 40 |
return new \WP_Error( 'auth_failed', $e->getMessage(), ['status' => 401] ); |
| 41 |
} |
| 42 |
return true; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* API Status Endpoint. |
| 47 |
* |
| 48 |
* @return \WP_REST_Response |
| 49 |
*/ |
| 50 |
static public function status() { |
| 51 |
return new \WP_REST_Response( array( |
| 52 |
"ok" => true, |
| 53 |
"message" => "API up." |
| 54 |
), 200 ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Get settings |
| 59 |
* |
| 60 |
* @todo Implement this if needed |
| 61 |
* @param \WP_REST_Request $request |
| 62 |
* @return \WP_REST_Response|\WP_Error |
| 63 |
*/ |
| 64 |
static public function getSettings( \WP_REST_Request $request ) { |
| 65 |
return new \WP_Error('not_implemented', 'Method not implemented', ['status' => 501]); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Update settings |
| 70 |
* |
| 71 |
* @param \WP_REST_Request $request |
| 72 |
* @return \WP_REST_Response|\WP_Error |
| 73 |
*/ |
| 74 |
static public function updateSettings( \WP_REST_Request $request ) { |
| 75 |
if ( self::$tokenData === null || empty( self::$tokenData->user_id ) ) { |
| 76 |
return new \WP_Error( 'unauthorized', 'Auth token looks incorrect', ['status' => 401] ); |
| 77 |
} |
| 78 |
$is_gae = isset($_SERVER["GAE_VERSION"]) ? true : false; |
| 79 |
$upload_dir = wp_upload_dir(); |
| 80 |
$is_upload_dir_writable = is_writable( $upload_dir['basedir'] ); |
| 81 |
|
| 82 |
try { |
| 83 |
$queryParams = $request->get_json_params(); |
| 84 |
if ( empty( $queryParams ) ) throw new \Exception('Query is empty'); |
| 85 |
|
| 86 |
$bucketName = isset($queryParams['bucket_name'])?$queryParams['bucket_name']:null; |
| 87 |
$privateKeyData = isset($queryParams['private_key_data'])?$queryParams['private_key_data']:null; |
| 88 |
|
| 89 |
if ( !$bucketName || !$privateKeyData ) { |
| 90 |
throw new \Exception('bucket_name and private_key_data are required'); |
| 91 |
} |
| 92 |
|
| 93 |
if ($privateKeyData) { |
| 94 |
$privateKeyData = base64_decode($privateKeyData); |
| 95 |
} |
| 96 |
|
| 97 |
switch ( self::$tokenData->is_network ) { |
| 98 |
case true: |
| 99 |
if ( !user_can( self::$tokenData->user_id, 'manage_network_options' ) ) { |
| 100 |
return new \WP_Error( 'not_allowed', 'Sorry, you are not allowed to perform this action', ['status' => 403] ); |
| 101 |
} |
| 102 |
/** |
| 103 |
* If Google App Engine detected - set Stateless mode |
| 104 |
* and Google App Engine compatibility by default |
| 105 |
*/ |
| 106 |
if ( $is_gae || !$is_upload_dir_writable ) { |
| 107 |
update_site_option( 'sm_mode', 'stateless' ); |
| 108 |
|
| 109 |
$modules = get_site_option('stateless-modules', array()); |
| 110 |
if ( $is_gae && empty($modules['google-app-engine']) || $modules['google-app-engine'] != 'true') { |
| 111 |
$modules['google-app-engine'] = 'true'; |
| 112 |
update_site_option('stateless-modules', $modules ); |
| 113 |
} |
| 114 |
} |
| 115 |
elseif ( get_site_option('sm_mode', 'disabled') == 'disabled' ) { |
| 116 |
update_site_option( 'sm_mode', 'cdn'); |
| 117 |
} |
| 118 |
update_site_option( 'sm_bucket', $bucketName); |
| 119 |
update_site_option( 'sm_key_json', $privateKeyData); |
| 120 |
break; |
| 121 |
|
| 122 |
case false: |
| 123 |
if ( !user_can( self::$tokenData->user_id, 'manage_options' ) ) { |
| 124 |
return new \WP_Error( 'not_allowed', 'Sorry, you are not allowed to perform this action', ['status' => 403] ); |
| 125 |
} |
| 126 |
/** |
| 127 |
* If Google App Engine detected - set Stateless mode |
| 128 |
* and Google App Engine compatibility by default |
| 129 |
*/ |
| 130 |
if ( $is_gae || !$is_upload_dir_writable ) { |
| 131 |
update_option( 'sm_mode', 'stateless' ); |
| 132 |
|
| 133 |
$modules = get_option('stateless-modules', array()); |
| 134 |
if ( $is_gae && empty($modules['google-app-engine']) || $modules['google-app-engine'] != 'true') { |
| 135 |
$modules['google-app-engine'] = 'true'; |
| 136 |
update_option('stateless-modules', $modules ); |
| 137 |
} |
| 138 |
} |
| 139 |
elseif ( get_option('sm_mode', 'disabled') == 'disabled') { |
| 140 |
update_option('sm_mode', 'cdn'); |
| 141 |
} |
| 142 |
update_option( 'sm_bucket', $bucketName); |
| 143 |
update_option( 'sm_key_json', $privateKeyData); |
| 144 |
break; |
| 145 |
} |
| 146 |
|
| 147 |
return new \WP_REST_Response(array( |
| 148 |
'ok' => true, |
| 149 |
'message' => 'Settings updated successfully' |
| 150 |
)); |
| 151 |
} catch (\Throwable $e) { |
| 152 |
return new \WP_Error( 'internal_server_error', $e->getMessage(), ['status' => 500] ); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
} |
| 157 |
|
| 158 |
} |
| 159 |
|
| 160 |
} |
| 161 |
|