/var/www/(Del)hsihk.com/wp-content/plugins/backupbuddy/destinations/site/init.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php

// DO NOT CALL THIS CLASS DIRECTLY. CALL VIA: pb_backupbuddy_destination in bootstrap.php.

class pb_backupbuddy_destination_site {
    
    const 
TIME_WIGGLE_ROOM 5;                                // Number of seconds to fudge up the time elapsed to give a little wiggle room so we don't accidently hit the edge and time out.
    
    
public static $destination_info = array(
        
'name'            =>        'BackupBuddy Deployment',
        
'description'    =>        'Push to or Pull from another instance of this WordPress site running BackupBuddy. Great for rapidly copying a site to and from a development version back and forth with a live site.',
    );
    
    
// Default settings. Should be public static for auto-merging.
    
public static $default_settings = array(
        
'type'            =>        'site',    // MUST MATCH your destination slug.
        
'title'            =>        '',        // Required destination field.
        
'api_key'        =>        '',
        
'max_payload'    =>        '2',    // Max payload in MB to send per chunk. This WILL be read into memory.
        
'max_time'        =>        '30',    // Default max time in seconds to allow a send to run for. This should be set on the fly prior to calling send overriding this default.
        
'resume_point'    =>        '',        // fseek resume point (via ftell).
        
'chunks_total'    =>        1,
        
'chunks_sent'    =>        0,
        
'sendType'        =>        '',        // Set on the fly prior to calling send. Valid types: backup, media, theme, plugin. These determine the destination root location for a file.
        
'sendFilePath'    =>        '',        // Location to store file on remote server relative to the root storage location based on send type. Optional.
    
);
    
    private static 
$_timeStart 0;
    
    
    
/*    send()
     *    
     *    Send one or more files.
     *    
     *    @param        array            $files        Array of one or more files to send. IMPORTANT: Currently only supports ONE file.
     *    @return        boolean                        True on success, else false.
     */
    
public static function send$settings = array(), $files = array(), $send_id '' ) {
        
self::$_timeStart microtimetrue );
        
        if ( 
count$files ) > ) {
            return 
'Error #84545894585: This destination currently only supports one file per send.';
        }
        
        require_once( 
pb_backupbuddy::plugin_path() . '/classes/remote_api.php' );
        
        
$apiSettings backupbuddy_remote_api::key_to_array$settings['api_key'] );
        
        
$apiURL $apiSettings['siteurl'];
        
$file $files[0];
        
$filePath '';
        if ( 
'' != $settings['sendFilePath'] ) {
            
$filePath $settings['sendFilePath'];
        }
        
$maxPayload $settings['max_payload'] * 1024 1024// Convert to bytes.
        
$encodeReducedPayload floor( ( $settings['max_payload'] - ( $settings['max_payload'] * 0.37 ) ) * 1024 1024 ); // Take into account 37% base64 encoding overhead. Convert to bytes. Remove any decimals down via floor.
        
        // Open file for reading.
        
if ( FALSE === ( $fs = @fopen$file'r' ) ) ) {
            
pb_backupbuddy::status'error''Error #438934894: Unable to open file `' $file '` for reading.' );
            return 
false;
        }
        
        
// If chunked resuming then seek to the correct place in the file.
        
if ( ( '' != $settings['resume_point'] ) && ( $settings['resume_point'] > ) ) { // Resuming send of a partially transferred file.
            
if ( !== fseek$fs$settings['resume_point'] ) ) { // Returns 0 on success.
                
pb_backupbuddy::status'error''Error #327834783: Failed to seek file to resume point `' $settings['resume_point'] . '` via fseek().' );
                return 
false;
            }
            
$prevPointer $settings['resume_point'];
        } else { 
// New file send.
            
$size filesize$file );
            
$encodedSize = ( $size 0.37 ) + $size;
            
pb_backupbuddy::status'details''File size of file to send: ' pb_backupbuddy::$format->file_size$size ) . '. After encoding overhead: ' pb_backupbuddy::$format->file_size$encodedSize ) );
            if ( 
$encodedSize $maxPayload ) {
                
$chunksTotal ceil$encodedSize $maxPayload );
                
pb_backupbuddy::status'details''This file + encoding exceeds the maximum per-chunk payload size so will be read in and sent in chunks of ' $settings['max_payload'] . 'MB (' $maxPayload ' bytes) totaling approximately ' $chunksTotal ' chunks.' );
            } else {
                
pb_backupbuddy::status'details''This file + encoding does not exceed per-chunk payload size of ' $settings['max_payload'] . 'MB (' $maxPayload ' bytes) so sending in one pass.' );
            }
            
$prevPointer 0;
        }
        
        
pb_backupbuddy::status'details''Reading in `' $encodeReducedPayload '` bytes at a time.' );
        
$dataRemains true;
        
//$loopCount = 0;
        //$loopTimeSum = 0; // Used for average send time per chunk.
        
while ( ( TRUE === $dataRemains ) && ( FALSE !== ( $fileData fread$fs$encodeReducedPayload ) ) ) ) { // Grab one chunk of data at a time.
            
pb_backupbuddy::status'details''Read in file data.' );
            if ( 
feof$fs ) ) {
                
pb_backupbuddy::status'details''Read to end of file (feof true). No more chunks left after this send.' );
                
$dataRemains false;
            }
            
            
            
            
            
$isFileTest false;
            if ( 
false !== stristrbasename$file ), 'remote-send-test.php' ) ) {
                
$isFileTest true;
            }
            
$isFileDone true;
            if ( 
$dataRemains === true ) {
                
$isFileDone false;
            }
            
            if ( ! isset( 
$size ) ) {
                
$size '';
            }
            
            
            
pb_backupbuddy::status'details''Connecting to remote server to send data.' );
            
$response backupbuddy_remote_api::remoteCall$apiSettings'sendFile_' $settings['sendType'], array(), $settings['max_time'], $file$fileData$prevPointer$isFileTest$isFileDone$size$filePath );
            unset( 
$fileData ); // Free up memory.
            
            
            
$settings['chunks_sent']++;
            
pb_backupbuddy::status'details''Connection finished sending part ' $settings['chunks_sent'] . ' of ' $settings['chunks_total'] . '.' );
            
            
            if ( 
false === $response ) {
                echo 
implode', 'backupbuddy_remote_api::getErrors() ) . ' ';
                
pb_backupbuddy::status'error''Errors encountered details: ' implode', 'backupbuddy_remote_api::getErrors() ) );
                global 
$pb_backupbuddy_destination_errors;
                
$pb_backupbuddy_destination_errors backupbuddy_remote_api::getErrors();
                return 
false//implode( ', ', backupbuddy_remote_api::getErrors() );
            
} else {
                return 
true;
            }
            
            
            if ( 
FALSE === ( $prevPointer ftell$fs ) ) ) {
                
pb_backupbuddy::status'error''Error #438347844: Unable to get ftell pointer of file handle for passing to prevPointer.' );
                @
fclose$fs );
                return 
false;
            } else {
                
pb_backupbuddy::status'details''File pointer: `' $prevPointer '`.' );
            }
            
            
            if ( 
true === $dataRemains ) { // More data remains so see if we need to consider chunking to a new PHP process.
                // If we are within X second of reaching maximum PHP runtime then stop here so that it can be picked up in another PHP process...
                
if ( ( ( microtimetrue ) - self::$_timeStart ) + self::TIME_WIGGLE_ROOM ) >= $settings['max_time'] ) {
                    
pb_backupbuddy::status'message''Approaching limit of available PHP chunking time of `' $settings['max_time'] . '` sec. Ran for ' roundmicrotimetrue ) - self::$_timeStart) . ' sec. Proceeding to use chunking.' );
                    @
fclose$fs );
                    
                    
// Tells next chunk where to pick up.
                    
$settings['resume_point'] = $prevPointer;
                    if ( isset( 
$chunksTotal ) ) {
                        
$settings['chunks_total'] = $chunksTotal;
                    }
                    
                    
// Schedule cron.
                    
$cronTime time();
                    
$cronArgs = array( $settings$files$send_id$delete_after false );
                    
$cronHashID md5$cronTime serialize$cronArgs ) );
                    
$cronArgs[] = $cronHashID;
                    
                    
$schedule_result backupbuddy_core::schedule_single_event$cronTimepb_backupbuddy::cron_tag'destination_send' ), $cronArgs );
                    if ( 
true === $schedule_result ) {
                        
pb_backupbuddy::status'details''Next Site chunk step cron event scheduled.' );
                    } else {
                        
pb_backupbuddy::status'error''Next Site chunk step cron even FAILED to be scheduled.' );
                    }
                    
spawn_crontime() + 150 ); // Adds > 60 seconds to get around once per minute cron running limit.
                    
update_option'_transient_doing_cron'); // Prevent cron-blocking for next item.
                    
                    
                    
return array( $prevPointer'Sent part ' $settings['chunks_sent'] . ' of ~' $settings['chunks_total'] . ' parts.' ); // filepointer location, elapsed time during the import
                
} else { // End if.
                    
pb_backupbuddy::status'details''Not approaching time limit.' );
                }
            } else {
                
pb_backupbuddy::status'details''No more data remains (eg for chunking) so finishing up.' );
            }
            
        } 
// end while data remains in file.
        
        // Update fileoptions stats.
        
pb_backupbuddy::status'details''About to load fileoptions data.' );
        require_once( 
pb_backupbuddy::plugin_path() . '/classes/fileoptions.php' );
        
pb_backupbuddy::status'details''Fileoptions instance #20.' );
        
$fileoptions_obj = new pb_backupbuddy_fileoptionsbackupbuddy_core::getLogDirectory() . 'fileoptions/send-' $send_id '.txt'$read_only false$ignore_lock false$create_file false );
        if ( 
true !== ( $result $fileoptions_obj->is_ok() ) ) {
            
pb_backupbuddy::status'error'__('Fatal Error #9034.2344848. Unable to access fileoptions data.''it-l10n-backupbuddy' ) . ' Error: ' $result );
            return 
false;
        }
        
pb_backupbuddy::status'details''Fileoptions data loaded.' );
        
$fileoptions = &$fileoptions_obj->options;
        
$fileoptions['finish_time'] = time();
        
$fileoptions['status'] = 'success';
        
$fileoptions['_multipart_status'] = 'Sent all parts.';
        if ( isset( 
$uploaded_speed ) ) {
            
$fileoptions['write_speed'] = $uploaded_speed;
        }
        
$fileoptions_obj->save();
        unset( 
$fileoptions );
        
        
// Made it this far so completed!
        
pb_backupbuddy::status'message''Finished sending file. Took ' roundmicrotimetrue ) - self::$_timeStart) . ' seconds this round.' );
        return 
true;
        
    } 
// End send().
    
    
    
    /*    test()
     *    
     *    function description
     *    
     *    @param        array            $settings    Destination settings.
     *    @return        bool|string                    True on success, string error message on failure.
     */
    
public static function test$settings ) {
        
        
/*
        if ( ( $settings['address'] == '' ) || ( $settings['username'] == '' ) || ( $settings['password'] == '' ) ) {
            return __('Missing required input.', 'it-l10n-backupbuddy' );
        }
        */
        
        // Try sending a file.
        
return pb_backupbuddy_destinations::send$settingsdirnamedirname__FILE__ ) ) . '/remote-send-test.php'$send_id 'TEST-' pb_backupbuddy::random_string12 ) ); // 3rd param true forces clearing of any current uploads.
        
    
// End test().
    
    
// End class.