function google_drive_backup($historyID = 0, $args = '', $uploadid = null, $offset = 0)
 {
     require_once $GLOBALS['iwp_mmb_plugin_dir'] . '/lib/Google/Client.php';
     require_once $GLOBALS['iwp_mmb_plugin_dir'] . '/lib/Google/Http/MediaFileUpload.php';
     require_once $GLOBALS['iwp_mmb_plugin_dir'] . '/lib/Google/Service/Drive.php';
     $this->hisID = $historyID;
     $requestParams = $this->getRequiredData($historyID, "requestParams");
     $upload_loop_break_time = $requestParams['account_info']['upload_loop_break_time'];
     //darkcode changed
     $upload_file_block_size = $requestParams['account_info']['upload_file_block_size'];
     $upload_file_block_size = 1 * 1024 * 1024;
     $del_host_file = $requestParams['args']['del_host_file'];
     $iwp_folder_id = '';
     $sub_folder_id = '';
     $sub_folder_name = $this->site_name;
     $task_result = $this->getRequiredData($historyID, "taskResults");
     $fileSizeUploaded = 0;
     $resumeURI = false;
     $current_file_num = 0;
     if ($args == '') {
         //on the next call $args would be ''
         //set $args, $uploadid, $offset  from the DB
         $responseParams = $this->getRequiredData($historyID, "responseParams");
         if (!$responseParams) {
             return $this->statusLog($this->hisID, array('stage' => 'google_drive_upload', 'status' => 'error', 'statusMsg' => 'google Upload failed: Error while fetching table data.', 'statusCode' => 'google_upload_failed_error_fetching_data'));
         }
         $args = $responseParams['gDriveArgs'];
         $prevChunkResults = $responseParams['response_data'];
         if (is_array($prevChunkResults)) {
             $resumeURI = $prevChunkResults['resumeURI'];
             $fileSizeUploaded = $prevChunkResults['fileSizeUploaded'];
         }
         $current_file_num = $responseParams['current_file_num'];
     }
     $create_sub_folder = $args['gdrive_site_folder'];
     $tempArgs = $args;
     $client = new IWP_google_Client();
     $client->setClientId($args['clientID']);
     $client->setClientSecret($args['clientSecretKey']);
     $client->setRedirectUri($args['redirectURL']);
     $client->setScopes(array('https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile'));
     $accessToken = $args['token'];
     $refreshToken = $accessToken['refresh_token'];
     $backup_file = $args['backup_file'];
     if (!is_array($backup_file)) {
         $temp_backup_file = $backup_file;
         $backup_file = array();
         $backup_file[] = $temp_backup_file;
     }
     if (is_array($backup_file)) {
         $backup_files_count = count($backup_file);
         $backup_file = $backup_file[$current_file_num];
     }
     try {
         $client->refreshToken($refreshToken);
     } catch (Exception $e) {
         echo 'google Error ', $e->getMessage(), "\n";
         return array("error" => $e->getMessage(), "error_code" => "google_error_backup_refresh_token");
     }
     //$client = new IWP_google_Client();
     //$accessToken = $client->authenticate($accessToken_early['refresh_token']);
     //$client->setAccessToken($accessToken);
     $service = new IWP_google_Service_Drive($client);
     //create iwp folder folder if it is not present
     try {
         $parameters = array();
         $parameters['q'] = "title = 'infinitewp' and trashed = false and 'root' in parents and 'me' in owners and mimeType= 'application/vnd.google-apps.folder'";
         $files = $service->files->listFiles($parameters);
         $list_result = array();
         $list_result = array_merge($list_result, $files->getItems());
         $list_result = (array) $list_result;
         if (empty($list_result)) {
             $file = new IWP_google_Service_Drive_DriveFile();
             $file->setTitle('infinitewp');
             $file->setMimeType('application/vnd.google-apps.folder');
             $createdFolder = $service->files->insert($file, array('mimeType' => 'application/vnd.google-apps.folder'));
             if ($createdFolder) {
                 $createdFolder = (array) $createdFolder;
                 $iwp_folder_id = $createdFolder['id'];
             }
         } else {
             foreach ($list_result as $k => $v) {
                 $iwp_folder_id = $v->id;
             }
         }
     } catch (Exception $e) {
         print "An error occurred: " . $e->getMessage();
         return array('error' => $e->getMessage(), 'error_code' => 'google_error_occured_list_results');
     }
     //create sub folder by site name
     if ($create_sub_folder) {
         $parameters = array();
         $parameters['q'] = "title = '{$sub_folder_name}' and trashed = false and '{$iwp_folder_id}' in parents and 'me' in owners and mimeType = 'application/vnd.google-apps.folder'";
         //$parameters['corpus'] = "DEFAULT";
         $files = $service->files->listFiles($parameters);
         $list_result = array();
         $list_result = array_merge($list_result, $files->getItems());
         $list_result = (array) $list_result;
         if (empty($list_result)) {
             $file = new IWP_google_Service_Drive_DriveFile();
             $file->setTitle($sub_folder_name);
             $file->setMimeType('application/vnd.google-apps.folder');
             //setting parent as infinitewpFolder
             $parent = new IWP_google_Service_Drive_ParentReference();
             $parent->setId($iwp_folder_id);
             $file->setParents(array($parent));
             $createdFolder = $service->files->insert($file, array('mimeType' => 'application/vnd.google-apps.folder'));
             if ($createdFolder) {
                 $createdFolder = (array) $createdFolder;
                 $sub_folder_id = $createdFolder['id'];
             }
         } else {
             foreach ($list_result as $k => $v) {
                 $sub_folder_id = $v->id;
             }
         }
     }
     //Insert a file
     $file = new IWP_google_Service_Drive_DriveFile();
     $file->setTitle(basename($backup_file));
     $file->setMimeType('binary/octet-stream');
     // Set the Parent Folder on Google Drive
     $parent = new IWP_google_Service_Drive_ParentReference();
     if (empty($sub_folder_id)) {
         $parent->setId($iwp_folder_id);
     } else {
         $parent->setId($sub_folder_id);
     }
     $file->setParents(array($parent));
     $gDriveID = '';
     try {
         if (false) {
             //single upload
             $data = file_get_contents($backup_file);
             $createdFile = (array) $service->files->insert($file, array('data' => $data));
             $gDriveID = $createdFile['id'];
         }
         //multipart upload
         if (true) {
             // Call the API with the media upload, defer so it doesn't immediately return.
             $client->setDefer(true);
             $request = $service->files->insert($file);
             // Create a media file upload to represent our upload process.
             $media = new IWP_google_Http_MediaFileUpload($client, $request, 'application/zip', null, true, $upload_file_block_size);
             $media->setFileSize(filesize($backup_file));
             $status = false;
             $handle = fopen($backup_file, "rb");
             fseek($handle, $fileSizeUploaded);
             $resArray = array('status' => 'completed', 'backupParentHID' => $historyID);
             while (!$status && !feof($handle)) {
                 iwp_mmb_auto_print('gdrive_chucked_upload');
                 $chunk = fread($handle, $upload_file_block_size);
                 $statusArray = $media->nextChunk($chunk, $resumeURI, $fileSizeUploaded);
                 $status = $statusArray['status'];
                 $resumeURI = $statusArray['resumeURI'];
                 //$fileSizeUploaded = ftell($handle);
                 $fileSizeUploaded = $statusArray['progress'];
                 $googleTimeTaken = microtime(1) - $GLOBALS['IWP_MMB_PROFILING']['ACTION_START'];
                 if ($googleTimeTaken > 10 && $status != true) {
                     $chunkResult['resumeURI'] = $resumeURI;
                     $chunkResult['fileSizeUploaded'] = $fileSizeUploaded;
                     echo "<br> file uploaded size in this call: " . $fileSizeUploaded . "<br>";
                     $result_arr = array();
                     $result_arr['response_data'] = $chunkResult;
                     $result_arr['status'] = 'partiallyCompleted';
                     $result_arr['nextFunc'] = 'google_drive_backup';
                     $result_arr['gDriveArgs'] = $tempArgs;
                     $result_arr['current_file_num'] = $current_file_num;
                     /* $task_result['task_results'][$historyID]['gDriveOrgFileName'][] = basename($backup_file);
                     			$task_result['task_results'][$historyID]['gDrive'][] = $gDriveID;
                     			//$task_result['gDrive'] = basename($backup_file);
                     			$task_result['gDrive'][] = $gDriveID; */
                     $this->statusLog($this->hisID, array('stage' => 'amazonMultiCall', 'status' => 'partiallyCOmpleted', 'statusMsg' => 'nextCall', 'nextFunc' => 'amazons3_backup', 'task_result' => $task_result, 'responseParams' => $result_arr));
                     $resArray['status'] = "partiallyCompleted";
                     return $resArray;
                 }
             }
             $result = false;
             if ($status != false) {
                 $result = $status;
             }
             fclose($handle);
             $client->setDefer(false);
             $completeBackupResult = (array) $status;
             //$gDriveID = $createdFile['id'];
             $gDriveID = $completeBackupResult['id'];
         }
     } catch (Exception $e) {
         echo "An error occurred: " . $e->getMessage();
         return array("error" => "gDrive Error" . $e->getMessage(), "error_code" => "google_error_multipart_upload");
     }
     $current_file_num += 1;
     $result_arr = array();
     $result_arr['response_data'] = isset($createdFile) && !empty($createdFile['id']) ? $createdFile['id'] : array();
     $result_arr['status'] = "completed";
     $result_arr['nextFunc'] = 'google_drive_completed';
     $result_arr['gDriveArgs'] = $tempArgs;
     $result_arr['current_file_num'] = $current_file_num;
     $resArray = array('status' => 'completed', 'backupParentHID' => $historyID);
     //$task_result = $this->getRequiredData($historyID, "taskResults");
     $task_result['task_results'][$historyID]['gDriveOrgFileName'][] = basename($backup_file);
     $task_result['task_results'][$historyID]['gDrive'][] = $gDriveID;
     //$task_result['gDrive'] = basename($backup_file);
     $task_result['gDrive'][] = $gDriveID;
     if ($current_file_num == $backup_files_count) {
         $result_arr['nextFunc'] = 'google_drive_completed';
         iwp_mmb_print_flush('Google Drive upload: End');
         unset($task_result['task_results'][$historyID]['server']);
     } else {
         $result_arr['status'] = "partiallyCompleted";
         $result_arr['nextFunc'] = 'google_drive_backup';
         $result_arr['response_data'] = false;
         $resArray['status'] = 'partiallyCompleted';
     }
     if ($del_host_file) {
         @unlink($backup_file);
     }
     $this->statusLog($this->hisID, array('stage' => 'gDriveMultiCall', 'status' => 'completed', 'statusMsg' => 'nextCall', 'nextFunc' => 'google_drive_completed', 'task_result' => $task_result, 'responseParams' => $result_arr));
     return $resArray;
 }
 function google_drive_backup($args = '', $uploadid = null, $offset = 0)
 {
     require_once $GLOBALS['iwp_mmb_plugin_dir'] . '/lib/Google/Client.php';
     require_once $GLOBALS['iwp_mmb_plugin_dir'] . '/lib/Google/Http/MediaFileUpload.php';
     require_once $GLOBALS['iwp_mmb_plugin_dir'] . '/lib/Google/Service/Drive.php';
     //$this -> hisID = $historyID;
     $upload_file_block_size = 1 * 1024 * 1024;
     $iwp_folder_id = '';
     $sub_folder_id = '';
     $create_sub_folder = $args['gdrive_site_folder'];
     $sub_folder_name = $this->site_name;
     //$task_result = $this->getRequiredData($historyID, "taskResults");
     $fileSizeUploaded = 0;
     $resumeURI = false;
     $client = new IWP_google_Client();
     $client->setClientId($args['clientID']);
     $client->setClientSecret($args['clientSecretKey']);
     $client->setRedirectUri($args['redirectURL']);
     $client->setScopes(array('https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile'));
     $accessToken = $args['token'];
     $refreshToken = $accessToken['refresh_token'];
     $backup_file = $args['backup_file'];
     try {
         $client->refreshToken($refreshToken);
     } catch (Exception $e) {
         echo 'google Error ', $e->getMessage(), "\n";
         return array("error" => $e->getMessage(), "error_code" => "google_error_refresh_token");
     }
     $service = new IWP_google_Service_Drive($client);
     //create folder if not present
     try {
         $parameters = array();
         $parameters['q'] = "title = 'infinitewp' and trashed = false and 'root' in parents and 'me' in owners and mimeType= 'application/vnd.google-apps.folder'";
         $files = $service->files->listFiles($parameters);
         $list_result = array();
         $list_result = array_merge($list_result, $files->getItems());
         $list_result = (array) $list_result;
         if (empty($list_result)) {
             $file = new IWP_google_Service_Drive_DriveFile();
             $file->setTitle('infinitewp');
             $file->setMimeType('application/vnd.google-apps.folder');
             $createdFolder = $service->files->insert($file, array('mimeType' => 'application/vnd.google-apps.folder'));
             if ($createdFolder) {
                 $createdFolder = (array) $createdFolder;
                 $iwp_folder_id = $createdFolder['id'];
             }
         } else {
             $list_result = (array) $list_result[0];
             $iwp_folder_id = $list_result['id'];
         }
     } catch (Exception $e) {
         print "An error occurred: " . $e->getMessage();
         return array('error' => $e->getMessage());
     }
     //create sub folder by site name
     if ($create_sub_folder) {
         $parameters = array();
         $parameters['q'] = "title = '{$sub_folder_name}' and trashed = false and '{$iwp_folder_id}' in parents and 'me' in owners and mimeType = 'application/vnd.google-apps.folder'";
         $files = $service->files->listFiles($parameters);
         $list_result = array();
         $list_result = array_merge($list_result, $files->getItems());
         $list_result = (array) $list_result;
         if (empty($list_result)) {
             $file = new IWP_google_Service_Drive_DriveFile();
             $file->setTitle($sub_folder_name);
             $file->setMimeType('application/vnd.google-apps.folder');
             //setting parent as infinitewpFolder
             $parent = new IWP_google_Service_Drive_ParentReference();
             $parent->setId($iwp_folder_id);
             $file->setParents(array($parent));
             $createdFolder = $service->files->insert($file, array('mimeType' => 'application/vnd.google-apps.folder'));
             if ($createdFolder) {
                 $createdFolder = (array) $createdFolder;
                 $sub_folder_id = $createdFolder['id'];
             }
         } else {
             $list_result = (array) $list_result[0];
             $sub_folder_id = $list_result['id'];
         }
     }
     //Insert a file
     $file = new IWP_google_Service_Drive_DriveFile();
     $file->setTitle(basename($backup_file));
     $file->setMimeType('binary/octet-stream');
     // Set the Parent Folder on Google Drive
     $parent = new IWP_google_Service_Drive_ParentReference();
     if (empty($sub_folder_id)) {
         $parent->setId($iwp_folder_id);
     } else {
         $parent->setId($sub_folder_id);
     }
     $file->setParents(array($parent));
     $gDriveID = '';
     try {
         if (false) {
             //single upload
             $data = file_get_contents($backup_file);
             $createdFile = (array) $service->files->insert($file, array('data' => $data));
             $gDriveID = $createdFile['id'];
         }
         //multipart upload
         if (true) {
             // Call the API with the media upload, defer so it doesn't immediately return.
             $client->setDefer(true);
             $request = $service->files->insert($file);
             // Create a media file upload to represent our upload process.
             $media = new IWP_google_Http_MediaFileUpload($client, $request, 'application/zip', null, true, $upload_file_block_size);
             $media->setFileSize(filesize($backup_file));
             $status = false;
             $handle = fopen($backup_file, "rb");
             fseek($handle, $fileSizeUploaded);
             /* $resArray = array (
             			  'status' => 'completed',
             			  'backupParentHID' => $historyID,
             			); */
             while (!$status && !feof($handle)) {
                 iwp_mmb_auto_print('gdrive_chucked_upload');
                 $chunk = fread($handle, $upload_file_block_size);
                 $statusArray = $media->nextChunk($chunk, $resumeURI, $fileSizeUploaded);
                 $status = $statusArray['status'];
                 $resumeURI = $statusArray['resumeURI'];
                 //$fileSizeUploaded = ftell($handle);
                 $fileSizeUploaded = $statusArray['progress'];
             }
             $result = false;
             if ($status != false) {
                 $result = $status;
             }
             fclose($handle);
             $client->setDefer(false);
             $completeBackupResult = (array) $status;
             //$gDriveID = $createdFile['id'];
             $gDriveID = $completeBackupResult['id'];
         }
     } catch (Exception $e) {
         echo "An error occurred: " . $e->getMessage();
         return array("error" => "gDrive Error" . $e->getMessage());
     }
     /* if($del_host_file)
     		{
     			unset($task_result['task_results'][$historyID]['server']);
     			@unlink($backup_file);
     		} */
     $test_this_task = $this->get_this_tasks();
     $tasksThere = unserialize($test_this_task['taskResults']);
     return $gDriveID;
 }