public/wordpress/wp-content/plugins/backupbuddy/classes/remote_api.php
/* _sendFiles()
	 *
	 * Calling site is wanting to send file(s) TO this site. Called by various verbs that pass the appropriate $type that determines root path. Valid types: backup, theme, plugin, media
	 *
	 */
_sendFiles()
Calling site is wanting to send file(s) TO this site. Called by various verbs that pass the appropriate $type that determines root path. Valid types: backup, theme, plugin, media
_sendFiles () Calling site is wanting to send file ( s ) TO this site . Called by various verbs that pass the appropriate $type that determines root path . Valid types : backup theme plugin media
private static function _sendFiles( $type = '' ) {
		$rootDir = self::_getFilePathByType( $type ); 
		$fileReceiveCount = 0;
		$bytesReceived = 0;
		foreach( self::$_incomingPayload['files'] as $file ) {
			$filePath = '';
			if ( isset( $file[ 'filepath' ] ) ) {
				$filePath = $file[ 'filepath' ];
			}
			if ( '' != $filePath ) { 
				if ( $file != basename( $filePath ) ) {
					$utf_decoded_filePath = utf8_decode( $filePath );
					if ( $file == basename( $utf_decoded_filePath ) ) {
						$filePath = $subFilePath = $utf_decoded_filePath;
					} else {
						$message = 'Error #493844: The specified filename within the filepath parameter does not match the supplied filename parameter. | cleanfile: ' . $file . ' | filePath: | ' . $filePath;
						pb_backupbuddy::status( 'error', $message );
						self::_reply( array( 'success' => false, 'error' => $message ) );
					}
				} else { 
					$subFilePath = $filePath;
				}
			} else { 
				$subFilePath = $file['file'];
			}
			$saveFile = $rootDir . $subFilePath;
			$seekTo = $file[ 'seekto' ];
			if ( ! is_numeric( $seekTo ) ) {
				$seekTo = 0;
			}
			$saveDir = dirname( $saveFile );
			if ( ( 0 == $seekTo ) && ( file_exists( $saveFile ) ) ) { 
				if ( true !== @unlink( $saveFile ) ) {
					$message = 'Error #238722: Unable to delete existing file `' . $saveFile . '`.';
					pb_backupbuddy::status( 'error', $message );
					self::_reply( array( 'success' => false, 'error' => $message ) );
				}
			}
			if ( ! is_dir( $saveDir ) ) {
				if ( true !== pb_backupbuddy::$filesystem->mkdir( $saveDir ) ) {
					$message = 'Error #327832: Unable to create directory `' . $saveDir . '`. Check permissions or manually create. Halting to preserve deployment integrity';
					pb_backupbuddy::status( 'error', $message );
					self::_reply( array( 'success' => false, 'error' => $message ) );
				}
			}
			if ( false === ( $fs = fopen( $saveFile, 'a' ) )) {
				$message = 'Error #489339848: Unable to fopen file `' . $saveFile . '`.';
				pb_backupbuddy::status( 'error', $message );
				self::_reply( array( 'success' => false, 'error' => $message ) );
			}
			if ( 0 != fseek( $fs, $seekTo ) ) {
				@fclose( $fs );
				$message = 'Error #8584884: Unable to fseek file.';
				pb_backupbuddy::status( 'error', $message );
				self::_reply( array( 'success' => false, 'error' => $message ) );
			}
			$gotLength = strlen( $file[ 'data' ] );
			if ( $file['datalen'] != $gotLength ) {
				@fclose( $fs );
				$message = 'Error #4355445: Received data of length `' . $gotLength . '` did not match sent length of `' . $file[ 'datalen' ] . '`. Data may have been truncated.';
				pb_backupbuddy::status( 'error', $message );
				self::_reply( array( 'success' => false, 'error' => $message ) );
			}
			if ( false === ( $bytesWritten = fwrite( $fs, $file[ 'data' ] ) ) ) {
				@fclose( $fs );
				@unlink( $saveFile );
				$message = 'Error #3984394: Error writing to file `' . $saveFile . '`.';
				pb_backupbuddy::status( 'error', $message );
				self::_reply( array( 'success' => false, 'error' => $message ) );
			} else {
				@fclose( $fs );
				$message = 'Wrote `' . $bytesWritten . '` bytes to `' . $saveFile . '`.';
				$bytesReceived += $bytesWritten;
				pb_backupbuddy::status( 'details', $message );
				if ( ( '1' == $file[ 'test' ] ) || ( 'test' == $type ) ) {
					@unlink( $saveFile );
				} else {
					if ( true === $file['done'] ) {
						$destFile = ABSPATH . basename( $saveFile );
						$fileReceiveCount++;
					}
				}
				continue;
			}
		}
		self::_reply( array( 'success' => true, 'message' => 'Received a total of `' . $fileReceiveCount . ' files, `' . $bytesReceived . '` bytes.' ) );
	}
private static function _sendFiles ( $ type = '' ) { $ rootDir = self :: _getFilePathByType ( $ type ) ; $ fileReceiveCount = 0 ; $ bytesReceived = 0 ; foreach ( self :: $ _incomingPayload [ 'files' ] as $ file ) { $ filePath = '' ; if ( isset ( $ file [ 'filepath' ] ) ) { $ filePath = $ file [ 'filepath' ] ; } if ( '' != $ filePath ) { if ( $ file != basename ( $ filePath ) ) { $ utf_decoded_filePath = utf8_decode ( $ filePath ) ; if ( $ file == basename ( $ utf_decoded_filePath ) ) { $ filePath = $ subFilePath = $ utf_decoded_filePath ; } else { $ message = 'Error #493844: The specified filename within the filepath parameter does not match the supplied filename parameter. | cleanfile: ' . $ file . ' | filePath: | ' . $ filePath ; pb_backupbuddy :: status ( 'error' , $ message ) ; self :: _reply ( array ( 'success' => false , 'error' => $ message ) ) ; } } else { $ subFilePath = $ filePath ; } } else { $ subFilePath = $ file [ 'file' ] ; } $ saveFile = $ rootDir . $ subFilePath ; $ seekTo = $ file [ 'seekto' ] ; if ( ! is_numeric ( $ seekTo ) ) { $ seekTo = 0 ; } $ saveDir = dirname ( $ saveFile ) ; if ( ( 0 == $ seekTo ) && ( file_exists ( $ saveFile ) ) ) { if ( true !== @ unlink ( $ saveFile ) ) { $ message = 'Error #238722: Unable to delete existing file `' . $ saveFile . '`.' ; pb_backupbuddy :: status ( 'error' , $ message ) ; self :: _reply ( array ( 'success' => false , 'error' => $ message ) ) ; } } if ( ! is_dir ( $ saveDir ) ) { if ( true !== pb_backupbuddy :: $ filesystem -> mkdir ( $ saveDir ) ) { $ message = 'Error #327832: Unable to create directory `' . $ saveDir . '`. Check permissions or manually create. Halting to preserve deployment integrity' ; pb_backupbuddy :: status ( 'error' , $ message ) ; self :: _reply ( array ( 'success' => false , 'error' => $ message ) ) ; } } if ( false === ( $ fs = fopen ( $ saveFile , 'a' ) ) ) { $ message = 'Error #489339848: Unable to fopen file `' . $ saveFile . '`.' ; pb_backupbuddy :: status ( 'error' , $ message ) ; self :: _reply ( array ( 'success' => false , 'error' => $ message ) ) ; } if ( 0 != fseek ( $ fs , $ seekTo ) ) { @ fclose ( $ fs ) ; $ message = 'Error #8584884: Unable to fseek file.' ; pb_backupbuddy :: status ( 'error' , $ message ) ; self :: _reply ( array ( 'success' => false , 'error' => $ message ) ) ; } $ gotLength = strlen ( $ file [ 'data' ] ) ; if ( $ file [ 'datalen' ] != $ gotLength ) { @ fclose ( $ fs ) ; $ message = 'Error #4355445: Received data of length `' . $ gotLength . '` did not match sent length of `' . $ file [ 'datalen' ] . '`. Data may have been truncated.' ; pb_backupbuddy :: status ( 'error' , $ message ) ; self :: _reply ( array ( 'success' => false , 'error' => $ message ) ) ; } if ( false === ( $ bytesWritten = fwrite ( $ fs , $ file [ 'data' ] ) ) ) { @ fclose ( $ fs ) ; @ unlink ( $ saveFile ) ; $ message = 'Error #3984394: Error writing to file `' . $ saveFile . '`.' ; pb_backupbuddy :: status ( 'error' , $ message ) ; self :: _reply ( array ( 'success' => false , 'error' => $ message ) ) ; } else { @ fclose ( $ fs ) ; $ message = 'Wrote `' . $ bytesWritten . '` bytes to `' . $ saveFile . '`.' ; $ bytesReceived += $ bytesWritten ; pb_backupbuddy :: status ( 'details' , $ message ) ; if ( ( '1' == $ file [ 'test' ] ) || ( 'test' == $ type ) ) { @ unlink ( $ saveFile ) ; } else { if ( true === $ file [ 'done' ] ) { $ destFile = ABSPATH . basename ( $ saveFile ) ; $ fileReceiveCount ++ ; } } continue ; } } self :: _reply ( array ( 'success' => true , 'message' => 'Received a total of `' . $ fileReceiveCount . ' files, `' . $ bytesReceived . '` bytes.' ) ) ; }
_sendFiles()
 Calling site is wanting to send file(s) TO this site.
_sendFiles () Calling site is wanting to send file ( s ) TO this site .
//error_log( 'type:' . $type ); // contains trailing slash. //error_log( 'API saving file to dir: `' . $rootDir . '`.' ); //error_log( 'file: ' . $file ); //$file = str_replace( array( '\\', '/' ), '', stripslashes_deep( self::$_incomingPayload[ 'filename' ] ) ); // Filepath specified so goes in a subdirectory under the rootDir. // Check if utf8 decoding the filename helps match correctly // Filename with path. // Just the filename. No path. //error_log( 'a:' . $rootDir ); //error_log( 'b:' . $subFilePath ); //error_log( 'saveFile: ' . $saveFile ); //error_log( print_r( $file, true ) ); // Calculate seek position. // Check if directory exists & create if needed. // Delete existing directory for some types of transfers. // New file transfer only. Do not delete existing file if chunking. // Open/create file for write/append. // Seek to position (if applicable). // Check data length. // Write to file. /*
						if ( false === @copy( $saveFile, $destFile ) ) {
							pb_backupbuddy::status( 'error', 'Error #948454: Unable to copy temporary file `' . $saveFile . '` to `' . $destFile . '`.' );
						}
						@unlink( $saveFile );
						*/ // Media files need their thumbnails regenerated so get attachment ID. /* CANNOT DO THIS HERE ... because item may not be in the DB yet. need to transfer thumbnails?
						if ( 'media' == $type ) {
							global $wpdb;
							$sql = "SELECT post_id FROM `" . DB_NAME . "`.`" . $wpdb->prefix . "postmeta` WHERE `meta_value` = %s AND `meta_key` = '_wp_attached_file'";
							$sql = $wpdb->prepare( $sql, $filePath );
							error_log( $sql );
							$attachment_id = $wpdb->get_var( $sql );
							error_log( 'ID: ' . $attachment_id );
							error_log( 'savefile: ' . $saveFile );
							require ( ABSPATH . 'wp-admin/includes/image.php' );
							$attach_data = wp_generate_attachment_metadata( $attachment_id, $saveFile );
							wp_update_attachment_metadata( $attachment_id,  $attach_data );
						}
						*/