PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.84
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.84
5.5.84 5.5.83 5.5.82 5.5.81 5.5.80 5.5.79 5.5.77 5.5.76 5.5.75 5.5.73 5.5.72 5.5.22 5.5.23 5.5.29 5.5.3 5.5.31 5.5.32 5.5.34 5.5.35 5.5.36 5.5.37 5.5.4 5.5.40 5.5.41 5.5.42 All 160 releases
wp-data-access / WPDataAccess / Drive / WPDA_Dropbox.php

WPDA_Dropbox.php in WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards 5.5.84, at WPDataAccess/Drive/WPDA_Dropbox.php

212 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPDataAccess\Drive {
4
5 use WPDataAccess\Utilities\WPDA_Remote_Call;
6 use WPDataAccess\WPDA;
7
8 class WPDA_Dropbox extends WPDA_Drive {
9
10 const DROPBOX_CLIENT_ID = '39i1okq5b61s7k0';
11 const DROPBOX_CLIENT_SECRET = '3d8plsy4prg6l3z';
12
13 public function __construct( $drive = array(), $enabled = false ) {
14
15 $this->drive_type = 'dropbox';
16
17 parent::__construct( 'dropbox', $drive, $enabled );
18
19 }
20
21 public function authorize( $authorization, $enabled = true ) {
22
23 // Get authorization code from link:
24 // Template
25 // https://www.dropbox.com/oauth2/authorize?client_id=DROPBOX_CLIENT_ID&response_type=code&token_access_type=offline
26 // Actual
27 // https://www.dropbox.com/oauth2/authorize?client_id=39i1okq5b61s7k0&response_type=code&token_access_type=offline
28
29 $response = WPDA_Remote_Call::post(
30 'https://api.dropboxapi.com/oauth2/token',
31 array(
32 'code' => $authorization,
33 'grant_type' => 'authorization_code',
34 'client_id' => self::DROPBOX_CLIENT_ID,
35 'client_secret' => self::DROPBOX_CLIENT_SECRET,
36 )
37 );
38 if ( ! isset( $response['body'] ) ) return false;
39
40 $body_content = json_decode( $response['body'] );
41 if ( isset( $body_content->error ) ) {
42 return $body_content->error;
43 }
44
45 if ( isset( $body_content->access_token, $body_content->refresh_token ) ) {
46 $this->drive = array(
47 'access_token' => $body_content->access_token,
48 'refresh_token' => $body_content->refresh_token,
49 );
50 $this->enabled = $enabled;
51 $this->save();
52
53 return true;
54 }
55
56 return false;
57
58 }
59
60 public function refresh_token() {
61
62 if ( ! $this->enabled ) { return false; }
63
64 if ( ! isset( $this->drive['access_token'], $this->drive['refresh_token'] ) ) { return false; }
65
66 $response = WPDA_Remote_Call::post(
67 'https://api.dropboxapi.com/oauth2/token',
68 array(
69 'refresh_token' => $this->drive['refresh_token'],
70 'grant_type' => 'refresh_token',
71 'client_id' => self::DROPBOX_CLIENT_ID,
72 'client_secret' => self::DROPBOX_CLIENT_SECRET,
73 )
74 );
75
76 if ( ! isset( $response['body'] ) ) return false;
77
78 $body_content = json_decode( $response['body'] );
79 if ( ! isset( $body_content->access_token ) ) return false;
80
81 $this->drive['access_token'] = $body_content->access_token;
82 return true;
83
84 }
85
86 public function connect() {
87
88 return true; // N.A.
89 //
90 }
91
92 public function close() {
93
94 // N.A.
95
96 }
97
98 public function upload_file( $local_file, $remote_file ) {
99
100 if ( ! isset( $this->drive['access_token'] ) ) return false;
101
102 $file = fopen( $local_file, 'r' ); // phpcs:ignore
103 $file_size = filesize( $local_file );
104
105 fseek( $file, 0 );
106 $response = WPDA_Remote_Call::post(
107 'https://content.dropboxapi.com/2/files/upload',
108 fread( $file, $file_size ), // phpcs:ignore
109 false,
110 array(
111 'Authorization' => "Bearer {$this->drive['access_token']}",
112 'Content-Type' => 'application/octet-stream',
113 'Dropbox-API-Arg' => '{"path":"/' . $remote_file . '","mode":"add","autorename":false,"mute":false,"strict_conflict":false}',
114 )
115 );
116
117 if ( ! isset( $response['body'] ) ) return false;
118
119 $body_content = json_decode( $response['body'] );
120 return isset( $body_content->error_summary ) ? $body_content->error_summary : true;
121
122 }
123
124 public function cleanup( $query, $keep ) {
125
126 if ( 'ALL' === $keep ) return true;
127
128 $response = $this->search( '', $query );
129
130 $body_content = json_decode( $response['body'], true );
131 if ( ! isset( $body_content['matches'] ) ) { return false; }
132
133 $keep_counting = 0;
134 $files_sorted = array();
135
136 foreach ( $body_content['matches'] as $match ) {
137 if ( isset( $match['metadata']['metadata']['name'] ) ) {
138 array_push($files_sorted, $match['metadata']['metadata']['name']);
139 }
140 }
141 rsort( $files_sorted );
142
143 foreach ( $files_sorted as $file ) {
144 $keep_counting++;
145 if ( $keep_counting > (int) $keep ) {
146 // Delete outdated files.
147 $this->delete( '/' . $file );
148 }
149 }
150
151 return true;
152
153 }
154
155 private function search( $path, $query ) {
156
157 if ( ! isset( $this->drive['access_token'] ) ) return false;
158
159 return WPDA_Remote_Call::post(
160 'https://api.dropboxapi.com/2/files/search_v2',
161 json_encode(
162 array(
163 'options' => array(
164 'file_status' => 'active',
165 'filename_only' => false,
166 'max_results' => 999,
167 'path' => substr( $path, 0, strlen( $path ) - 1 )
168 ),
169 'query' => $query,
170 )
171 ),
172 false,
173 array(
174 'Authorization' => "Bearer {$this->drive['access_token']}",
175 'Content-Type' => 'application/json',
176 )
177 );
178
179 }
180
181 private function delete( $path ) {
182
183 if ( ! isset( $this->drive['access_token'] ) ) return false;
184
185 WPDA_Remote_Call::post(
186 'https://api.dropboxapi.com/2/files/delete_v2',
187 json_encode(
188 array(
189 'path' => $path,
190 )
191 ),
192 false,
193 array(
194 'Authorization' => "Bearer {$this->drive['access_token']}",
195 'Content-Type' => 'application/json',
196 )
197 );
198
199 return true;
200
201 }
202
203 public function toggle( $enabled ) {
204
205 $this->enabled = $enabled;
206 $this->save();
207
208 }
209
210 }
211
212 }