public static function metadata() { // Set path $path = null; if (isset($_GET['path'])) { $path = trim($_GET['path'], '/'); } // Set Dropbox client $dropbox = new ServMaskDropboxClient(get_option('ai1wmde_dropbox_token'), get_option('ai1wmde_dropbox_ssl', true)); // List folder $metadata = $dropbox->metadata($path); // Set folder structure $response = array('path' => null, 'items' => array()); // Set folder path if (isset($metadata['path'])) { $response['path'] = $metadata['path']; } // Set folder items if (isset($metadata['contents']) && ($items = $metadata['contents'])) { foreach ($items as $item) { $response['items'][] = array('size' => isset($item['size']) ? $item['size'] : null, 'path' => isset($item['path']) ? $item['path'] : null, 'icon' => isset($item['icon']) ? $item['icon'] : null, 'bytes' => isset($item['bytes']) ? $item['bytes'] : null, 'is_dir' => isset($item['is_dir']) ? $item['is_dir'] : null); } } echo json_encode($response); exit; }
public static function execute($params) { $completed = false; // Set startBytes if (!isset($params['startBytes'])) { $params['startBytes'] = 0; } // Set endBytes if (!isset($params['endBytes'])) { $params['endBytes'] = ServMaskDropboxClient::CHUNK_SIZE; } // Set retry if (!isset($params['retry'])) { $params['retry'] = 0; } // Set Dropbox client $dropbox = new ServMaskDropboxClient(get_option('ai1wmde_dropbox_token'), get_option('ai1wmde_dropbox_ssl', true)); // Get archive file $archive = fopen(ai1wm_archive_path($params), 'ab'); try { // Increase number of retries $params['retry'] += 1; // Download file chunk $dropbox->getFile($params['filePath'], $archive, $params); } catch (Exception $e) { // Retry 3 times if ($params['retry'] <= 3) { return $params; } throw $e; } // Reset retry counter $params['retry'] = 0; // Close the archive file fclose($archive); // Calculate percent $percent = (int) ($params['startBytes'] / $params['totalBytes'] * 100); // Set progress Ai1wm_Status::progress($percent); // Next file chunk if (empty($params['totalBytes'])) { throw new Ai1wm_Import_Exception('Unable to import the archive! Please check file size parameter. '); } else { if ($params['totalBytes'] == $params['startBytes']) { $completed = true; } } // Set completed flag $params['completed'] = $completed; return $params; }
public function account() { // Set Dropbox client $dropbox = new ServMaskDropboxClient(get_option('ai1wmde_dropbox_token'), get_option('ai1wmde_dropbox_ssl', true)); // Get account info $account = $dropbox->getAccountInfo(); // Set account name $name = null; if (isset($account['display_name'])) { $name = $account['display_name']; } // Set used quota $used = null; if (isset($account['quota_info']['normal'])) { $used = $account['quota_info']['normal']; } // Set total quota $total = null; if (isset($account['quota_info']['quota'])) { $total = $account['quota_info']['quota']; } return array('name' => $name, 'used' => size_format($used), 'total' => size_format($total), 'progress' => ceil($used / $total * 100)); }
public static function execute($params) { // Set Dropbox client $dropbox = new ServMaskDropboxClient(get_option('ai1wmde_dropbox_token'), get_option('ai1wmde_dropbox_ssl', true)); // Get metadata $metadata = $dropbox->metadata(ai1wm_archive_folder()); // Number of backups if ($backups = get_option('ai1wmde_dropbox_backups')) { if ($backups = count($metadata['contents']) - $backups) { for ($i = 0; $i < $backups; $i++) { if (empty($metadata['contents'][$i]['is_dir'])) { $dropbox->delete($metadata['contents'][$i]['path']); } } } } // Size of backups if ($total = ai1wm_parse_size(get_option('ai1wmde_dropbox_total'))) { $bytes = 0; if (isset($metadata['contents']) && ($contents = $metadata['contents'])) { foreach ($contents as $content) { $bytes += $content['bytes']; } // Delete backups foreach ($contents as $content) { if ($bytes > $total) { if (empty($content['is_dir'])) { $dropbox->delete($content['path']); // Decrease bytes $bytes -= $content['bytes']; } } } } } return $params; }
public static function execute($params) { $completed = false; // Set offset if (!isset($params['offset'])) { $params['offset'] = 0; } // Set retry if (!isset($params['retry'])) { $params['retry'] = 0; } // Set Dropbox client $dropbox = new ServMaskDropboxClient(get_option('ai1wmde_dropbox_token'), get_option('ai1wmde_dropbox_ssl', true)); // Get archive file $archive = fopen(ai1wm_archive_path($params), 'rb'); // Read file chunk if (fseek($archive, $params['offset']) !== -1 && ($chunk = fread($archive, ServMaskDropboxClient::CHUNK_SIZE))) { try { // Increase number of retries $params['retry'] += 1; // Upload file chunk $dropbox->uploadFileChunk($chunk, $params); } catch (Exception $e) { // Retry 3 times if ($params['retry'] <= 3) { return $params; } throw $e; } // Reset retry counter $params['retry'] = 0; // Set archive details $name = ai1wm_archive_name($params); $bytes = ai1wm_archive_bytes($params); $size = ai1wm_archive_size($params); // Get progress if (isset($params['offset'])) { $progress = (int) ($params['offset'] / $bytes * 100); } else { $progress = 100; } // Set progress Ai1wm_Status::info(__("<i class=\"ai1wm-icon-dropbox\"></i> " . "Uploading <strong>{$name}</strong> ({$size})<br />{$progress}% complete", AI1WMDE_PLUGIN_NAME)); } else { // Set archive details $name = ai1wm_archive_name($params); $folder = ai1wm_archive_folder(); // Commit upload file chunk $dropbox->uploadFileChunkCommit(sprintf('%s/%s', $folder, $name), $params); // Set last backup date update_option('ai1wmde_dropbox_timestamp', current_time('timestamp')); // Set progress Ai1wm_Status::done(__('Your WordPress archive has been uploaded to Dropbox.', AI1WMDE_PLUGIN_NAME), __('Dropbox', AI1WMDE_PLUGIN_NAME)); // Upload completed $completed = true; self::notify_admin_of_new_backup($params); } // Close the archive file fclose($archive); // Set completed flag $params['completed'] = $completed; return $params; }