| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDataAccess\Drive; |
| 4 |
|
| 5 |
use WPDataAccess\Utilities\WPDA_Remote_Call; |
| 6 |
use WPDataAccess\WPDA; |
| 7 |
|
| 8 |
// phpcs:disable PluginCheck.CodeAnalysis.Offloading.OffloadedContent |
| 9 |
class WPDA_Google_Drive extends WPDA_Drive { |
| 10 |
|
| 11 |
const GOOGLE_CLIENT_ID = '400971594152-30c2ro61ohe4jbb2jt09n9q86itnkjvm.apps.googleusercontent.com'; |
| 12 |
const GOOGLE_CLIENT_SECRET = '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'; |
| 13 |
const GOOGLE_REDIRECT_URI = 'http://localhost'; |
| 14 |
|
| 15 |
private $folder_id = '1xZ0CemnSlq_mnEC2860Q2W2i5n9rNAa1'; |
| 16 |
|
| 17 |
public function __construct( $drive = array(), $enabled = false ) { |
| 18 |
|
| 19 |
$this->drive_type = 'google_drive'; |
| 20 |
|
| 21 |
parent::__construct( 'google_drive', $drive, $enabled ); |
| 22 |
|
| 23 |
} |
| 24 |
|
| 25 |
public function authorize( $authorization, $enabled = true ) { |
| 26 |
|
| 27 |
// TODO: Get authorization code from reply |
| 28 |
|
| 29 |
$response = WPDA_Remote_Call::post( |
| 30 |
'https://oauth2.googleapis.com/token', |
| 31 |
array( |
| 32 |
'code' => $authorization, |
| 33 |
'client_id' => self::GOOGLE_CLIENT_ID, |
| 34 |
'client_secret' => self::GOOGLE_CLIENT_SECRET, |
| 35 |
'redirect_uri' => self::GOOGLE_REDIRECT_URI, |
| 36 |
'grant_type' => 'authorization_code', |
| 37 |
) |
| 38 |
); |
| 39 |
|
| 40 |
if ( ! isset( $response['body'] ) ) return false; |
| 41 |
|
| 42 |
$body_content = json_decode( $response['body'] ); |
| 43 |
if ( isset( $body_content->access_token, $body_content->refresh_token ) ) { |
| 44 |
$this->drive = array( |
| 45 |
'access_token' => $body_content->access_token, |
| 46 |
'refresh_token' => $body_content->refresh_token, |
| 47 |
); |
| 48 |
$this->enabled = $enabled; |
| 49 |
$this->save(); |
| 50 |
|
| 51 |
return true; |
| 52 |
} |
| 53 |
|
| 54 |
return false; |
| 55 |
} |
| 56 |
|
| 57 |
public function refresh_token() { |
| 58 |
|
| 59 |
if ( ! $this->enabled ) { return false; } |
| 60 |
|
| 61 |
if ( ! isset( $this->drive['access_token'], $this->drive['refresh_token'] ) ) { return false; } |
| 62 |
|
| 63 |
$response = WPDA_Remote_Call::post( |
| 64 |
'https://oauth2.googleapis.com/token', |
| 65 |
array( |
| 66 |
'client_id' => self::GOOGLE_CLIENT_ID, |
| 67 |
'client_secret' => self::GOOGLE_CLIENT_SECRET, |
| 68 |
'refresh_token' => $this->drive['refresh_token'], |
| 69 |
'grant_type' => 'refresh_token', |
| 70 |
) |
| 71 |
); |
| 72 |
|
| 73 |
if ( ! isset( $response['body'] ) ) return false; |
| 74 |
|
| 75 |
$body_content = json_decode( $response['body'] ); |
| 76 |
if ( ! isset( $body_content->access_token ) ) return false; |
| 77 |
|
| 78 |
$this->drive['access_token'] = $body_content->access_token; |
| 79 |
return true; |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
public function connect() { |
| 84 |
|
| 85 |
return true; // N.A. |
| 86 |
// |
| 87 |
} |
| 88 |
|
| 89 |
public function close() { |
| 90 |
|
| 91 |
// N.A. |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
public function upload_file( $local_file, $remote_file ) { |
| 96 |
|
| 97 |
if ( ! isset( $this->drive['access_token'] ) ) return false; |
| 98 |
|
| 99 |
$file = fopen( $local_file, 'r' ); // phpcs:ignore |
| 100 |
$file_size = filesize( $local_file ); |
| 101 |
|
| 102 |
$metadata = array( |
| 103 |
'name' => $remote_file, |
| 104 |
'parents' => array( $this->folder_id ), |
| 105 |
); |
| 106 |
|
| 107 |
$boundary = uniqid(); |
| 108 |
$delimiter = '----' . $boundary; |
| 109 |
|
| 110 |
fseek( $file, 0 ); |
| 111 |
$file_content = fread( $file, $file_size ); // phpcs:ignore |
| 112 |
|
| 113 |
$body = |
| 114 |
"--$delimiter\r\n" . |
| 115 |
"Content-Type: application/json; charset=UTF-8\r\n\r\n" . |
| 116 |
json_encode( $metadata ) . "\r\n" . |
| 117 |
"--$delimiter\r\n" . |
| 118 |
"Content-Type: application/octet-stream\r\n\r\n" . |
| 119 |
$file_content . "\r\n" . |
| 120 |
"--$delimiter--\r\n"; |
| 121 |
|
| 122 |
$response = WPDA_Remote_Call::post( |
| 123 |
'https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart', |
| 124 |
$body, |
| 125 |
false, |
| 126 |
array( |
| 127 |
'Authorization' => 'Bearer ' . $this->drive['access_token'], |
| 128 |
'Content-Type' => 'multipart/related; boundary=' . $delimiter, |
| 129 |
) |
| 130 |
); |
| 131 |
|
| 132 |
if ( ! isset( $response['body'] ) ) return false; |
| 133 |
|
| 134 |
$body_content = json_decode( $response['body'] ); |
| 135 |
return isset( $body_content->id ) ? true : $response['body']; |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
public function cleanup( $query, $keep ) { |
| 140 |
|
| 141 |
if ( 'ALL' === $keep ) return true; |
| 142 |
|
| 143 |
$files = $this->search( $query ); |
| 144 |
if ( 0 < count( $files ) ) { |
| 145 |
|
| 146 |
// Sort by created time (newest first) |
| 147 |
usort( $files, function( $a, $b ) { |
| 148 |
return strcmp( $b['createdTime'], $a['createdTime'] ); |
| 149 |
}); |
| 150 |
|
| 151 |
$keep_count = 0; |
| 152 |
foreach ( $files as $file ) { |
| 153 |
$keep_count++; |
| 154 |
if ( $keep_count > (int) $keep ) { |
| 155 |
if ( ! $this->delete( $file['id'] ) ) { |
| 156 |
WPDA::wpda_log_wp_error( "ERROR: Error deleting file {$file['id']}" ); |
| 157 |
WPDA::wpda_log_wp_error( $file ); |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
return true; |
| 163 |
} |
| 164 |
|
| 165 |
return false; |
| 166 |
|
| 167 |
} |
| 168 |
|
| 169 |
private function search( $query ) { |
| 170 |
|
| 171 |
if ( ! isset( $this->drive['access_token'] ) ) return false; |
| 172 |
|
| 173 |
$encoded_query = sprintf( |
| 174 |
"'%s' in parents and name contains '%s' and trashed = false", |
| 175 |
$this->folder_id, |
| 176 |
$query |
| 177 |
); |
| 178 |
|
| 179 |
$url = add_query_arg( |
| 180 |
array( |
| 181 |
'q' => $encoded_query, |
| 182 |
'fields' => 'files(id,name,createdTime)', |
| 183 |
'pageSize' => 1000, |
| 184 |
), |
| 185 |
'https://www.googleapis.com/drive/v3/files' |
| 186 |
); |
| 187 |
|
| 188 |
$response = WPDA_Remote_Call::get( |
| 189 |
$url, |
| 190 |
array( |
| 191 |
'headers' => array( |
| 192 |
'Authorization' => "Bearer {$this->drive['access_token']}", |
| 193 |
'Accept: application/json', |
| 194 |
) |
| 195 |
) |
| 196 |
); |
| 197 |
|
| 198 |
if ( isset( $response['body'] ) ) { |
| 199 |
$body = json_decode( $response['body'], true ); |
| 200 |
|
| 201 |
if ( isset( $body['error'] ) ) { |
| 202 |
WPDA::wpda_log_wp_error( $body['error'] ); |
| 203 |
} |
| 204 |
|
| 205 |
if ( isset( $body['files'] ) ) { |
| 206 |
return $body['files']; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
return array(); |
| 211 |
|
| 212 |
} |
| 213 |
|
| 214 |
private function delete( $file_id ) { |
| 215 |
|
| 216 |
if ( ! isset( $this->drive['access_token'] ) ) return false; |
| 217 |
|
| 218 |
return WPDA_Remote_Call::delete( |
| 219 |
'https://www.googleapis.com/drive/v3/files/' . $file_id, |
| 220 |
array(), |
| 221 |
array( |
| 222 |
'Authorization' => "Bearer {$this->drive['access_token']}", |
| 223 |
) |
| 224 |
); |
| 225 |
|
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
public function get_folder_id( $folder_name ) { |
| 230 |
|
| 231 |
if ( ! isset( $this->drive['access_token'] ) ) return false; |
| 232 |
|
| 233 |
$url = 'https://www.googleapis.com/drive/v3/files'; |
| 234 |
$args = array( |
| 235 |
'headers' => array( |
| 236 |
'Authorization' => 'Bearer ' . $this->drive['access_token'], |
| 237 |
), |
| 238 |
'body' => array( |
| 239 |
'q' => "mimeType='application/vnd.google-apps.folder' and name='" . $folder_name . "' and trashed=false", |
| 240 |
'fields' => 'files(id, name)', |
| 241 |
), |
| 242 |
); |
| 243 |
|
| 244 |
$response = wp_remote_get($url, $args); |
| 245 |
|
| 246 |
if (is_wp_error($response)) { |
| 247 |
return false; |
| 248 |
} |
| 249 |
|
| 250 |
$body = json_decode($response['body'], true); |
| 251 |
return !empty($body['files']) ? $body['files'][0] : false; |
| 252 |
} |
| 253 |
|
| 254 |
public function toggle( $enabled ) { |
| 255 |
|
| 256 |
$this->enabled = $enabled; |
| 257 |
$this->save(); |
| 258 |
|
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
} |