public function chunked_upload_single_call($file, $path = '', $overwrite = true) { $file = str_replace("\\", "/", $file); if (!is_readable($file) or !is_file($file)) { throw new IWP_DropboxException("Error: File \"{$file}\" is not readable or doesn't exist."); } $file_handle = fopen($file, 'r'); $uploadid = null; $offset = 0; $ProgressFunction = null; while ($data = fread($file_handle, 1024 * 1024 * 30)) { //1024*1024*30 = 30MB iwp_mmb_auto_print('dropbox_chucked_upload'); $chunkHandle = fopen('php://memory', 'rw'); fwrite($chunkHandle, $data); rewind($chunkHandle); //overwrite progress function if (!empty($this->ProgressFunction) and function_exists($this->ProgressFunction)) { $ProgressFunction = $this->ProgressFunction; $this->ProgressFunction = false; } $url = self::API_CONTENT_URL . self::API_VERSION_URL . 'chunked_upload'; $output = $this->request($url, array('upload_id' => $uploadid, 'offset' => $offset), 'PUT', $chunkHandle, strlen($data)); fclose($chunkHandle); if ($ProgressFunction) { call_user_func($ProgressFunction, 0, 0, 0, $offset); $this->ProgressFunction = $ProgressFunction; } //args for next chunk $offset = $output['offset']; $uploadid = $output['upload_id']; fseek($file_handle, $offset); } fclose($file_handle); $url = self::API_CONTENT_URL . self::API_VERSION_URL . 'commit_chunked_upload/' . $this->root . '/' . trim($path, '/'); return $this->request($url, array('overwrite' => $overwrite ? 'true' : 'false', 'upload_id' => $uploadid), 'POST'); }
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; }
/** * Creates an Amazon S3 object using the multipart upload APIs. It is analogous to <create_object()>. * * While each individual part of a multipart upload can hold up to 5 GB of data, this method limits the * part size to a maximum of 500 MB. The combined size of all parts can not exceed 5 TB of data. When an * object is stored in Amazon S3, the data is streamed to multiple storage servers in multiple data * centers. This ensures the data remains available in the event of internal network or hardware failure. * * Amazon S3 charges for storage as well as requests to the service. Smaller part sizes (and more * requests) allow for faster failures and better upload reliability. Larger part sizes (and fewer * requests) costs slightly less but has lower upload reliability. * * In certain cases with large objects, it's possible for this method to attempt to open more file system * connections than allowed by the OS. In this case, either * <a href="https://forums.aws.amazon.com/thread.jspa?threadID=70216">increase the number of connections * allowed</a> or increase the value of the <code>partSize</code> parameter to use a larger part size. * * @param string $bucket (Required) The name of the bucket to use. * @param string $filename (Required) The file name for the object. * @param array $opt (Optional) An associative array of parameters that can have the following keys: <ul> * <li><code>fileUpload</code> - <code>string|resource</code> - Required - The URL/path for the file to upload, or an open resource.</li> * <li><code>acl</code> - <code>string</code> - Optional - The ACL settings for the specified object. [Allowed values: <code>AmazonS3::ACL_PRIVATE</code>, <code>AmazonS3::ACL_PUBLIC</code>, <code>AmazonS3::ACL_OPEN</code>, <code>AmazonS3::ACL_AUTH_READ</code>, <code>AmazonS3::ACL_OWNER_READ</code>, <code>AmazonS3::ACL_OWNER_FULL_CONTROL</code>]. The default value is <code>ACL_PRIVATE</code>.</li> * <li><code>contentType</code> - <code>string</code> - Optional - The type of content that is being sent in the body. The default value is <code>application/octet-stream</code>.</li> * <li><code>headers</code> - <code>array</code> - Optional - Standard HTTP headers to send along in the request. Accepts an associative array of key-value pairs.</li> * <li><code>length</code> - <code>integer</code> - Optional - The size of the object in bytes. For more information, see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.13">RFC 2616, section 14.13</a>. The value can also be passed to the <code>header</code> option as <code>Content-Length</code>.</li> * <li><code>limit</code> - <code>integer</code> - Optional - The maximum number of concurrent uploads done by cURL. Gets passed to <code>CFBatchRequest</code>.</li> * <li><code>meta</code> - <code>array</code> - Optional - An associative array of key-value pairs. Any header starting with <code>x-amz-meta-:</code> is considered user metadata. It will be stored with the object and returned when you retrieve the object. The total size of the HTTP request, not including the body, must be less than 4 KB.</li> * <li><code>partSize</code> - <code>integer</code> - Optional - The size of an individual part. The size may not be smaller than 5 MB or larger than 500 MB. The default value is 50 MB.</li> * <li><code>seekTo</code> - <code>integer</code> - Optional - The starting position in bytes for the first piece of the file/stream to upload.</li> * <li><code>storage</code> - <code>string</code> - Optional - Whether to use Standard or Reduced Redundancy storage. [Allowed values: <code>AmazonS3::STORAGE_STANDARD</code>, <code>AmazonS3::STORAGE_REDUCED</code>]. The default value is <code>STORAGE_STANDARD</code>.</li> * <li><code>uploadId</code> - <code>string</code> - Optional - An upload ID identifying an existing multipart upload to use. If this option is not set, one will be created automatically.</li> * <li><code>curlopts</code> - <code>array</code> - Optional - A set of values to pass directly into <code>curl_setopt()</code>, where the key is a pre-defined <code>CURLOPT_*</code> constant.</li> * <li><code>returnCurlHandle</code> - <code>boolean</code> - Optional - A private toggle specifying that the cURL handle be returned rather than actually completing the request. This toggle is useful for manually managed batch requests.</li></ul> * @return CFResponse A <CFResponse> object containing a parsed HTTP response. * @link http://docs.amazonwebservices.com/AmazonS3/latest/dev/RESTAccessPolicy.html REST Access Control Policy */ public function create_mpu_object($bucket, $filename, $opt = null) { if ($this->use_batch_flow) { throw new S3_Exception(__FUNCTION__ . '() cannot be batch requested'); } if (!$opt) { $opt = array(); } // Handle content length. Can also be passed as an HTTP header. if (isset($opt['length'])) { $opt['headers']['Content-Length'] = $opt['length']; unset($opt['length']); } if (!isset($opt['fileUpload'])) { throw new S3_Exception('The `fileUpload` option is required in ' . __FUNCTION__ . '().'); } elseif (is_resource($opt['fileUpload'])) { $opt['limit'] = 1; // We can only read from this one resource. $upload_position = isset($opt['seekTo']) ? (int) $opt['seekTo'] : ftell($opt['fileUpload']); $upload_filesize = isset($opt['headers']['Content-Length']) ? (int) $opt['headers']['Content-Length'] : null; if (!isset($upload_filesize) && $upload_position !== false) { $stats = fstat($opt['fileUpload']); if ($stats && $stats['size'] >= 0) { $upload_filesize = $stats['size'] - $upload_position; } } } else { $upload_position = isset($opt['seekTo']) ? (int) $opt['seekTo'] : 0; if (isset($opt['headers']['Content-Length'])) { $upload_filesize = (int) $opt['headers']['Content-Length']; } else { $upload_filesize = iwp_mmb_get_file_size($opt['fileUpload']); if ($upload_filesize !== false) { $upload_filesize -= $upload_position; } } } if ($upload_position === false || !isset($upload_filesize) || $upload_filesize === false || $upload_filesize < 0) { throw new S3_Exception('The size of `fileUpload` cannot be determined in ' . __FUNCTION__ . '().'); } // Handle part size if (isset($opt['partSize'])) { // If less that 5 MB... if ((int) $opt['partSize'] < 5242880) { $opt['partSize'] = 5242880; // 5 MB } elseif ((int) $opt['partSize'] > 52428800) { $opt['partSize'] = 52428800; // 50 MB } } else { $opt['partSize'] = 52428800; // 50 MB } // If the upload size is smaller than the piece size, failover to create_object(). if ($upload_filesize < $opt['partSize'] && !isset($opt['uploadId'])) { return $this->create_object($bucket, $filename, $opt); } // Initiate multipart upload if (isset($opt['uploadId'])) { $upload_id = $opt['uploadId']; } else { // Compose options for initiate_multipart_upload(). $_opt = array(); foreach (array('contentType', 'acl', 'storage', 'headers', 'meta') as $param) { if (isset($opt[$param])) { $_opt[$param] = $opt[$param]; } } $upload = $this->initiate_multipart_upload($bucket, $filename, $_opt); if (!$upload->isOK()) { return $upload; } // Fetch the UploadId $upload_id = (string) $upload->body->UploadId; } // Get the list of pieces $pieces = $this->get_multipart_counts($upload_filesize, (int) $opt['partSize']); // Queue batch requests $batch = new CFBatchRequest(isset($opt['limit']) ? (int) $opt['limit'] : null); foreach ($pieces as $i => $piece) { iwp_mmb_auto_print('amazonS3_chucked_upload'); $this->batch($batch)->upload_part($bucket, $filename, $upload_id, array('expect' => '100-continue', 'fileUpload' => $opt['fileUpload'], 'partNumber' => $i + 1, 'seekTo' => $upload_position + (int) $piece['seekTo'], 'length' => (int) $piece['length'])); } iwp_mmb_auto_print('amazonS3_chucked_upload'); // Send batch requests $batch_responses = $this->batch($batch)->send(); if (!$batch_responses->areOK()) { return $batch_responses; } // Compose completion XML $parts = array(); foreach ($batch_responses as $i => $response) { $parts[] = array('PartNumber' => $i + 1, 'ETag' => $response->header['etag']); } return $this->complete_multipart_upload($bucket, $filename, $upload_id, $parts); }
function privExtractFile(&$p_entry, $p_path, $p_remove_path, $p_remove_all_path, &$p_options) { iwp_mmb_auto_print('pcl_extracting'); $v_result = 1; // ----- Read the file header if (($v_result = $this->privReadFileHeader($v_header)) != 1) { // ----- Return return $v_result; } // ----- Check that the file header is coherent with $p_entry info if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) { // TBC } // ----- Look for all path to remove if ($p_remove_all_path == true) { // ----- Look for folder entry that not need to be extracted if (($p_entry['external'] & 0x10) == 0x10) { $p_entry['status'] = "filtered"; return $v_result; } // ----- Get the basename of the path $p_entry['filename'] = basename($p_entry['filename']); } else { if ($p_remove_path != "") { if (IWPPclZipUtilPathInclusion($p_remove_path, $p_entry['filename']) == 2) { // ----- Change the file status $p_entry['status'] = "filtered"; // ----- Return return $v_result; } $p_remove_path_size = strlen($p_remove_path); if (substr($p_entry['filename'], 0, $p_remove_path_size) == $p_remove_path) { // ----- Remove the path $p_entry['filename'] = substr($p_entry['filename'], $p_remove_path_size); } } } // ----- Add the path if ($p_path != '') { $p_entry['filename'] = $p_path . "/" . $p_entry['filename']; } // ----- Check a base_dir_restriction if (isset($p_options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION])) { $v_inclusion = IWPPclZipUtilPathInclusion($p_options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION], $p_entry['filename']); if ($v_inclusion == 0) { IWPPclZip::privErrorLog(PCLZIP_ERR_DIRECTORY_RESTRICTION, "Filename '" . $p_entry['filename'] . "' is " . "outside PCLZIP_OPT_EXTRACT_DIR_RESTRICTION"); return IWPPclZip::errorCode(); } } // ----- Look for pre-extract callback if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) { // ----- Generate a local information $v_local_header = array(); $this->privConvertHeader2FileInfo($p_entry, $v_local_header); // ----- Call the callback // Here I do not use call_user_func() because I need to send a reference to the // header. $v_result = $p_options[PCLZIP_CB_PRE_EXTRACT](PCLZIP_CB_PRE_EXTRACT, $v_local_header); if ($v_result == 0) { // ----- Change the file status $p_entry['status'] = "skipped"; $v_result = 1; } // ----- Look for abort result if ($v_result == 2) { // ----- This status is internal and will be changed in 'skipped' $p_entry['status'] = "aborted"; $v_result = PCLZIP_ERR_USER_ABORTED; } // ----- Update the informations // Only some fields can be modified $p_entry['filename'] = $v_local_header['filename']; } // ----- Look if extraction should be done if ($p_entry['status'] == 'ok') { // ----- Look for specific actions while the file exist if (file_exists($p_entry['filename'])) { // ----- Look if file is a directory if (is_dir($p_entry['filename'])) { // ----- Change the file status $p_entry['status'] = "already_a_directory"; // ----- Look for PCLZIP_OPT_STOP_ON_ERROR // For historical reason first PclZip implementation does not stop // when this kind of error occurs. if (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]) && $p_options[PCLZIP_OPT_STOP_ON_ERROR] === true) { IWPPclZip::privErrorLog(PCLZIP_ERR_ALREADY_A_DIRECTORY, "Filename '" . $p_entry['filename'] . "' is " . "already used by an existing directory"); return IWPPclZip::errorCode(); } } else { if (!is_writeable($p_entry['filename'])) { // ----- Change the file status $p_entry['status'] = "write_protected"; // ----- Look for PCLZIP_OPT_STOP_ON_ERROR // For historical reason first PclZip implementation does not stop // when this kind of error occurs. if (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]) && $p_options[PCLZIP_OPT_STOP_ON_ERROR] === true) { IWPPclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL, "Filename '" . $p_entry['filename'] . "' exists " . "and is write protected"); return IWPPclZip::errorCode(); } } else { if (filemtime($p_entry['filename']) > $p_entry['mtime']) { // ----- Change the file status if (isset($p_options[PCLZIP_OPT_REPLACE_NEWER]) && $p_options[PCLZIP_OPT_REPLACE_NEWER] === true) { } else { $p_entry['status'] = "newer_exist"; // ----- Look for PCLZIP_OPT_STOP_ON_ERROR // For historical reason first PclZip implementation does not stop // when this kind of error occurs. if (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]) && $p_options[PCLZIP_OPT_STOP_ON_ERROR] === true) { IWPPclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL, "Newer version of '" . $p_entry['filename'] . "' exists " . "and option PCLZIP_OPT_REPLACE_NEWER is not selected"); return IWPPclZip::errorCode(); } } } else { } } } } else { if (($p_entry['external'] & 0x10) == 0x10 || substr($p_entry['filename'], -1) == '/') { $v_dir_to_check = $p_entry['filename']; } else { if (!strstr($p_entry['filename'], "/")) { $v_dir_to_check = ""; } else { $v_dir_to_check = dirname($p_entry['filename']); } } if (($v_result = $this->privDirCheck($v_dir_to_check, ($p_entry['external'] & 0x10) == 0x10)) != 1) { // ----- Change the file status $p_entry['status'] = "path_creation_fail"; // ----- Return //return $v_result; $v_result = 1; } } } // ----- Look if extraction should be done if ($p_entry['status'] == 'ok') { // ----- Do the extraction (if not a folder) if (!(($p_entry['external'] & 0x10) == 0x10)) { // ----- Look for not compressed file if ($p_entry['compression'] == 0) { // ----- Opening destination file if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) { // ----- Change the file status $p_entry['status'] = "write_error"; // ----- Return return $v_result; } // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks $v_size = $p_entry['compressed_size']; while ($v_size != 0) { $v_read_size = $v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE; $v_buffer = @fread($this->zip_fd, $v_read_size); /* Try to speed up the code $v_binary_data = pack('a'.$v_read_size, $v_buffer); @fwrite($v_dest_file, $v_binary_data, $v_read_size); */ @fwrite($v_dest_file, $v_buffer, $v_read_size); $v_size -= $v_read_size; } // ----- Closing the destination file fclose($v_dest_file); // ----- Change the file mtime touch($p_entry['filename'], $p_entry['mtime']); } else { // ----- TBC // Need to be finished if (($p_entry['flag'] & 1) == 1) { IWPPclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_ENCRYPTION, 'File \'' . $p_entry['filename'] . '\' is encrypted. Encrypted files are not supported.'); return IWPPclZip::errorCode(); } // ----- Look for using temporary file to unzip if (!isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF]) && (isset($p_options[PCLZIP_OPT_TEMP_FILE_ON]) || isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD]) && $p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] <= $p_entry['size'])) { $v_result = $this->privExtractFileUsingTempFile($p_entry, $p_options); if ($v_result < PCLZIP_ERR_NO_ERROR) { return $v_result; } } else { // ----- Read the compressed file in a buffer (one shot) $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']); // ----- Decompress the file $v_file_content = @gzinflate($v_buffer); unset($v_buffer); if ($v_file_content === FALSE) { // ----- Change the file status // TBC $p_entry['status'] = "error"; return $v_result; } // ----- Opening destination file if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) { // ----- Change the file status $p_entry['status'] = "write_error"; return $v_result; } // ----- Write the uncompressed data @fwrite($v_dest_file, $v_file_content, $p_entry['size']); unset($v_file_content); // ----- Closing the destination file @fclose($v_dest_file); } // ----- Change the file mtime @touch($p_entry['filename'], $p_entry['mtime']); } // ----- Look for chmod option if (isset($p_options[PCLZIP_OPT_SET_CHMOD])) { // ----- Change the mode of the file @chmod($p_entry['filename'], $p_options[PCLZIP_OPT_SET_CHMOD]); } } } // ----- Change abort status if ($p_entry['status'] == "aborted") { $p_entry['status'] = "skipped"; } elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) { // ----- Generate a local information $v_local_header = array(); $this->privConvertHeader2FileInfo($p_entry, $v_local_header); // ----- Call the callback // Here I do not use call_user_func() because I need to send a reference to the // header. $v_result = $p_options[PCLZIP_CB_POST_EXTRACT](PCLZIP_CB_POST_EXTRACT, $v_local_header); // ----- Look for abort result if ($v_result == 2) { $v_result = PCLZIP_ERR_USER_ABORTED; } } // ----- Return return $v_result; }
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; }
function getFileList($p_filelist) { $startTime = microtime(true); $v_result = 1; // ----- Reset the error handler $this->privErrorReset(); // ----- Set default values $v_options = array(); $v_options[IWP_PCLZIP_OPT_NO_COMPRESSION] = FALSE; $v_options[IWP_PCLZIP_OPT_FILE_EXCLUDE_SIZE] = 15 * 1024 * 1024 * 1024; // ----- Look for variable options arguments $v_size = func_num_args(); // ----- Look for arguments if ($v_size > 1) { // ----- Get the arguments $v_arg_list = func_get_args(); // ----- Remove form the options list the first argument array_shift($v_arg_list); $v_size--; // ----- Look for first arg if (is_integer($v_arg_list[0]) && $v_arg_list[0] > 77000) { // ----- Parse the options $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options, array(IWP_PCLZIP_OPT_REMOVE_PATH => 'optional', IWP_PCLZIP_OPT_REMOVE_ALL_PATH => 'optional', IWP_PCLZIP_OPT_ADD_PATH => 'optional', IWP_PCLZIP_CB_PRE_ADD => 'optional', IWP_PCLZIP_CB_POST_ADD => 'optional', IWP_PCLZIP_OPT_NO_COMPRESSION => 'optional', IWP_PCLZIP_OPT_COMMENT => 'optional', IWP_PCLZIP_OPT_ADD_COMMENT => 'optional', IWP_PCLZIP_OPT_PREPEND_COMMENT => 'optional', IWP_PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional', IWP_PCLZIP_OPT_TEMP_FILE_ON => 'optional', IWP_PCLZIP_OPT_TEMP_FILE_OFF => 'optional', IWP_PCLZIP_OPT_CHUNK_BLOCK_SIZE => 'optional', IWP_PCLZIP_OPT_FILE_EXCLUDE_SIZE => 'optional', IWP_PCLZIP_OPT_HISTORY_ID => 'optional', IWP_PCLZIP_OPT_IWP_EXCLUDE => 'optional', IWP_PCLZIP_OPT_IWP_EXCLUDE_EXT => 'optional')); if ($v_result != 1) { return 0; } } else { // ----- Get the first argument $v_options[IWP_PCLZIP_OPT_ADD_PATH] = $v_add_path = $v_arg_list[0]; // ----- Look for the optional second argument if ($v_size == 2) { $v_options[IWP_PCLZIP_OPT_REMOVE_PATH] = $v_arg_list[1]; } else { if ($v_size > 2) { // ----- Error log IWPPclZip::privErrorLog(IWP_PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments"); // ----- Return return 0; } } } } // ----- Look for default option values $this->privOptionDefaultThreshold($v_options); // ----- Init $v_string_list = array(); $v_att_list = array(); $v_filedescr_list = array(); $p_result_list = array(); // ----- Look if the $p_filelist is really an array if (is_array($p_filelist)) { // ----- Look if the first element is also an array // This will mean that this is a file description entry if (isset($p_filelist[0]) && is_array($p_filelist[0])) { $v_att_list = $p_filelist; } else { $v_string_list = $p_filelist; } } else { if (is_string($p_filelist)) { // ----- Create a list from the string $v_string_list = explode(IWP_PCLZIP_SEPARATOR, $p_filelist); } else { IWPPclZip::privErrorLog(IWP_PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type '" . gettype($p_filelist) . "' for p_filelist"); return 0; } } // ----- Reformat the string list if (sizeof($v_string_list) != 0) { foreach ($v_string_list as $v_string) { $v_att_list[][IWP_PCLZIP_ATT_FILE_NAME] = $v_string; } } // ----- For each file in the list check the attributes $v_supported_attributes = array(IWP_PCLZIP_ATT_FILE_NAME => 'mandatory', IWP_PCLZIP_ATT_FILE_NEW_SHORT_NAME => 'optional', IWP_PCLZIP_ATT_FILE_NEW_FULL_NAME => 'optional', IWP_PCLZIP_ATT_FILE_MTIME => 'optional', IWP_PCLZIP_ATT_FILE_CONTENT => 'optional', IWP_PCLZIP_ATT_FILE_COMMENT => 'optional'); foreach ($v_att_list as $v_entry) { $v_result = $this->privFileDescrParseAtt($v_entry, $v_filedescr_list[], $v_options, $v_supported_attributes); if ($v_result != 1) { return 0; } } // ----- Expand the filelist (expand directories) $startTImeForlist = microtime(true); $prevFileList = array(); $next_file_index = 0; $complete_folder_list = array(); $historyID = $v_options[IWP_PCLZIP_OPT_HISTORY_ID]; if ($historyID) { $backupObj = new IWP_MMB_Backup_Multicall(); $responseParams = $backupObj->getRequiredData($historyID, "responseParams"); } if (!empty($responseParams)) { $prevFileList = isset($responseParams['response_data']['p_filedescr_list']) ? $responseParams['response_data']['p_filedescr_list'] : array(); $next_file_index = isset($responseParams['response_data']['next_file_index']) ? $responseParams['response_data']['next_file_index'] : 0; $complete_folder_list = isset($responseParams['response_data']['complete_folder_list']) ? $responseParams['response_data']['complete_folder_list'] : array(); } else { } if (!$prevFileList) { $prevFileList = array(); } if (!$next_file_index) { $next_file_index = 0; } if (!$complete_folder_list) { $complete_folder_list = array(); } if (empty($complete_folder_list)) { //first am getting the number of directories and its list foreach ($v_filedescr_list as $value) { $folder_list = array(); if (is_dir($value['filename'])) { $folder_list = $this->getFolderList($value['filename']); $complete_folder_list = $folder_list; } else { $folder_list = $value['filename']; //$folder_list = array_merge($complete_folder_list, array( 0 => array( 0 => $value))); //$folder_list = array_merge($complete_folder_list, $value); $complete_folder_list[][] = $value; } //$complete_folder_list = $folder_list; } //then am getting all the files in each folder /* $dirs_iwp = array(); $files_iwp = array(); foreach($complete_folder_list as $value) { if(is_dir($value)) { $this->getFilesListForCurrentDir($value, $dirs_iwp, $files_iwp); } } */ } $timeTaken65 = microtime(true) - $startTImeForlist; //for the file list prepared am doing the pclZip file preparation $prevlistCount = count($prevFileList); $current_file_array = array(); $current_file_array = $prevFileList; $file_list_result = array(); $file_list_result['status'] = 'completed'; $file_list_result['next_file_index'] = $next_file_index; foreach ($complete_folder_list as $keyy => $value) { if ($keyy < $next_file_index) { continue; } $current_file_array_temp = $value; $v_result = $this->privFileDescrExpand($current_file_array_temp, $v_options, "getFileList"); if (!empty($current_file_array_temp)) { foreach ($current_file_array_temp as $thisVal) { $current_file_array[] = $thisVal; } } unset($current_file_array_temp); $endTime = microtime(true); $timeTaken = $endTime - $GLOBALS['IWP_MMB_PROFILING']['ACTION_START']; //if(count($current_file_array) >= ($prevlistCount + 500)) if ($timeTaken >= 11) { $file_list_result['status'] = 'partiallyCompleted'; $file_list_result['next_file_index'] = $keyy + 1; $file_list_result['complete_folder_list'] = $complete_folder_list; break; } else { $file_list_result['next_file_index'] = 0; } iwp_mmb_auto_print("fileListLoop"); } $file_list_result['p_filedescr_list'] = $current_file_array; if ($v_result != 1) { return 0; } // ----- Call the create fct /* $v_result = $this->privAdd($v_filedescr_list, $p_result_list, $v_options); if ($v_result != 1) { return 0; } */ // ----- Return //return $file_list_iwp; return $file_list_result; }
function restore_db_php($file_name) { $this->wpdb_reconnect(); global $wpdb; $wpdb->query("SET NAMES 'utf8'"); $current_query = ''; // Read in entire file $lines = file($file_name); // Loop through each line if (!empty($lines)) { foreach ($lines as $line) { iwp_mmb_auto_print('restore_db_php'); // Skip it if it's a comment if (substr($line, 0, 2) == '--' || $line == '') { continue; } // Add this line to the current query $current_query .= $line; // If it has a semicolon at the end, it's the end of the query if (substr(trim($line), -1, 1) == ';') { // Perform the query $result = $wpdb->query($current_query); if ($result === false) { return false; } // Reset temp variable to empty $current_query = ''; } } } @unlink($file_name); return true; }