';
}
public function description() {
return __( 'Save the entry\'s files (and the entry as JSON/CSV) to a Dropbox folder.', 'fieldwren' );
}
public function help() {
$key = $this->cred( 'app_key' );
$url = 'https://www.dropbox.com/oauth2/authorize?client_id=' . rawurlencode( $key ? $key : 'APP_KEY' ) . '&response_type=code&token_access_type=offline';
return '- ' . esc_html__( 'dropbox.com/developers/apps → Create app → Scoped access → App folder or Full Dropbox. On the Permissions tab tick files.content.write (and files.content.read), then Submit.', 'fieldwren' ) . '
- ' . esc_html__( 'Copy the App key and App secret here and Save.', 'fieldwren' ) . '
- ' . esc_html__( 'Open this link while signed in to Dropbox, allow access and copy the code it shows:', 'fieldwren' ) . '
' . esc_html( $url ) . ' - ' . esc_html__( 'Paste the code in "Access code" and Save within a few minutes: it is exchanged for a refresh token, which is stored instead.', 'fieldwren' ) . '
';
}
public function credential_fields() {
return array(
'app_key' => array( 'label' => __( 'App key', 'fieldwren' ), 'type' => 'text', 'required' => true ),
'app_secret' => array( 'label' => __( 'App secret', 'fieldwren' ), 'type' => 'password', 'required' => true ),
'auth_code' => array( 'label' => __( 'Access code (one-time)', 'fieldwren' ), 'type' => 'password', 'desc' => __( 'From the authorize link in the help below; exchanged for a refresh token on save.', 'fieldwren' ) ),
'refresh_token' => array( 'label' => __( 'Refresh token', 'fieldwren' ), 'type' => 'password', 'required' => true, 'desc' => __( 'Filled automatically from the access code, or paste one.', 'fieldwren' ) ),
);
}
protected function error_message( $body, $code ) {
if ( is_array( $body ) && isset( $body['error_summary'] ) ) {
return 'Dropbox: ' . rtrim( (string) $body['error_summary'], '/.' );
}
if ( is_array( $body ) && isset( $body['error_description'] ) ) {
return 'Dropbox: ' . $body['error_description'];
}
return parent::error_message( $body, $code );
}
public function before_save_credentials( array $creds, array $old ) {
$code = trim( (string) ( $creds['auth_code'] ?? '' ) );
if ( '' === $code || ( $old['auth_code'] ?? '' ) === $code ) {
$creds['auth_code'] = '';
return $creds;
}
$this->secrets = array( $code, (string) $creds['app_secret'] );
$b = self::body(
$this->http(
'POST',
'https://api.dropboxapi.com/oauth2/token',
array(
'form' => true,
'auth' => array( (string) $creds['app_key'], (string) $creds['app_secret'] ),
'body' => array( 'grant_type' => 'authorization_code', 'code' => $code ),
)
)
);
if ( is_wp_error( $b ) ) {
return $b;
}
if ( empty( $b['refresh_token'] ) ) {
return new \WP_Error( 'fwren_dropbox', __( 'Dropbox returned no refresh token. Use the link with token_access_type=offline and a fresh code.', 'fieldwren' ) );
}
$creds['refresh_token'] = (string) $b['refresh_token'];
$creds['auth_code'] = '';
unset( $creds['verified'] );
return $creds;
}
/** @return string|\WP_Error Short-lived access token (cached). */
private function token( array $creds = null ) {
$c = null === $creds ? $this->creds() : $creds;
$key = 'fwren_dropbox_at_' . md5( ( $c['app_key'] ?? '' ) . ( $c['refresh_token'] ?? '' ) );
$hit = get_transient( $key );
if ( is_string( $hit ) && '' !== $hit ) {
$this->secrets[] = $hit;
return $hit;
}
$b = self::body(
$this->http(
'POST',
'https://api.dropboxapi.com/oauth2/token',
array(
'form' => true,
'auth' => array( (string) ( $c['app_key'] ?? '' ), (string) ( $c['app_secret'] ?? '' ) ),
'body' => array( 'grant_type' => 'refresh_token', 'refresh_token' => (string) ( $c['refresh_token'] ?? '' ) ),
)
)
);
if ( is_wp_error( $b ) ) {
return $b;
}
if ( empty( $b['access_token'] ) ) {
return new \WP_Error( 'fwren_dropbox', __( 'Dropbox did not return an access token.', 'fieldwren' ) );
}
set_transient( $key, (string) $b['access_token'], max( 300, (int) ( $b['expires_in'] ?? 14400 ) - 300 ) );
$this->secrets[] = (string) $b['access_token'];
return (string) $b['access_token'];
}
public function verify( array $creds ) {
$this->secrets = $this->secret_values( $creds );
$t = $this->token( $creds );
if ( is_wp_error( $t ) ) {
return $t;
}
$b = self::body( $this->http( 'POST', 'https://api.dropboxapi.com/2/users/get_current_account', array( 'headers' => array( 'Authorization' => 'Bearer ' . $t, 'Content-Type' => 'application/json' ), 'body' => 'null' ) ) );
return is_wp_error( $b ) ? $b : (string) ( $b['email'] ?? '' );
}
public function feed_fields( $form_id ) {
return array_merge(
array(
array( 'key' => 'path', 'type' => 'text', 'label' => __( 'Folder path', 'fieldwren' ), 'required' => true, 'default' => '/Forms/{form_title}/{entry.id}/', 'desc' => __( 'Smartcodes allowed. With an "App folder" app the path is inside Apps/<your app>.', 'fieldwren' ) ),
array( 'key' => 'mode', 'type' => 'select', 'label' => __( 'When the file exists', 'fieldwren' ), 'default' => 'autorename', 'no_empty' => true, 'options' => array( 'autorename' => __( 'Keep both (rename the new file)', 'fieldwren' ), 'overwrite' => __( 'Overwrite', 'fieldwren' ) ) ),
),
$this->storage_fields()
);
}
/** @return string Dropbox folder path: leading slash, no trailing slash, safe segments. */
private function folder( $template ) {
$parts = array();
foreach ( explode( '/', str_replace( '\\', '/', $this->val( $template ) ) ) as $seg ) {
$seg = self::clean_name( $seg );
if ( '' !== $seg ) {
$parts[] = $seg;
}
}
return $parts ? '/' . implode( '/', $parts ) : '';
}
public function process( array $feed, array $entry, array $values ) {
$s = $feed['settings'];
$items = $this->storage_items( $s );
if ( ! $items ) {
return __( 'No files in this entry; nothing to upload.', 'fieldwren' );
}
$token = $this->token();
if ( is_wp_error( $token ) ) {
return $token;
}
$dir = $this->folder( $s['path'] ?? '' );
$overwrite = 'overwrite' === ( $s['mode'] ?? '' );
$done = 0;
$skipped = array();
foreach ( $items as $item ) {
if ( $item['size'] > self::MAX ) {
$skipped[] = $item['name'];
\FWREN\Logs::add( (int) ( $entry['form_id'] ?? 0 ), (int) ( $entry['id'] ?? 0 ), $this->slug(), 'skipped', $item['name'], __( 'Larger than 150 MB; not uploaded.', 'fieldwren' ) );
continue;
}
$content = self::item_content( $item );
if ( is_wp_error( $content ) ) {
return $content;
}
// Dropbox-API-Arg must be ASCII: json_encode escapes non-ASCII as \uXXXX (and 0x7F explicitly).
$arg = str_replace( "\x7f", '\u007f', (string) wp_json_encode( array( 'path' => $dir . '/' . $item['name'], 'mode' => $overwrite ? 'overwrite' : 'add', 'autorename' => ! $overwrite, 'mute' => true ), JSON_UNESCAPED_SLASHES ) );
$res = $this->http(
'POST',
'https://content.dropboxapi.com/2/files/upload',
array(
'headers' => array(
'Authorization' => 'Bearer ' . $token,
'Dropbox-API-Arg' => $arg,
'Content-Type' => 'application/octet-stream',
),
'body' => $content,
'binary' => true,
'timeout' => 300,
)
);
unset( $content );
if ( is_wp_error( $res ) ) {
return $res;
}
$done++;
}
/* translators: 1: count, 2: folder */
$msg = sprintf( _n( '%1$d file uploaded to Dropbox %2$s.', '%1$d files uploaded to Dropbox %2$s.', $done, 'fieldwren' ), $done, $dir ? $dir : '/' );
if ( $skipped ) {
/* translators: %s file names */
$msg .= ' ' . sprintf( __( 'Skipped (over 150 MB): %s', 'fieldwren' ), implode( ', ', $skipped ) );
}
return $msg;
}
}