utiObj = new Utility;
$this->fiObj = new ActifendFileIntegrity( ABSPATH );
}
/**
* Zip extension check
*/
public function zip_check() {
if (extension_loaded('zip')) {
return True;
}
return False;
}
/**
* Zip extension and OS check
*/
public function zip_and_os_check() {
if (extension_loaded('zip') || (strtoupper(substr(PHP_OS, 0, 3)) !== "WIN")) {
return True;
}
return False;
}
/**
* actifend_backup_process
* After completion of upload to Actifend-cloud status is to be sent
* Actifend BE
*/
public function actifend_backup_process() {
// get asset id
$result = $this->utiObj->getActifendInfo();
$assetid = $result->asset_id;
// Execute Backup process
$this->actifend_wordpress_backup($assetid);
$statusOpt = get_option('ActifendBackupStatus');
// check if no requests are pending exit without doing anything
if ($statusOpt == 'None') exit(0);
// Send the status to Actifend BE
if ($statusOpt == 'complete') {
$backupStatus = 'done';
$statusOpt = 'Backup to Actifend-cloud successful.';
} else {
$backupStatus = 'failed';
}
$actifend_array = array('status' => $backupStatus,
'zip_enabled' => $this->zip_check(),
'reason' => $statusOpt,
'reqid' => $this->req_id);
$actifend_params = json_encode($actifend_array);
// send status done status by curl usind the PATCH method when backup send to azure successfully.
$actifend_url = ACTIFEND_BACKUP_END_POINT . $assetid . "/wpbackup";
$this->utiObj->actifend_postViaCurl($actifend_url, $actifend_params, "PATCH");
debug_log("Status update sent to Actifend BE.");
}
/**
* actifend_wordpress_backup
*
* This function create the receive the backup request after that create
* the zip of all your wordpress files and database as per the request full
* backup and the at send those files to the azure stroage in
* chunks of 3MB and then send the done status after completion of all steps.
*
* @return null
*/
public function actifend_wordpress_backup($asset_id) {
try {
debug_log("Check for backup requests initiated ...");
// check if any backup requests are waiting at BE
$response = $this->utiObj->getViaCurl(ACTIFEND_BACKUP_END_POINT, $asset_id, 'wpbackup');
if (isset($response) && !empty($response) && !is_wp_error($response)) {
if (($response['ResponseCode'] == '2000') && ($response['Message'] == 'success')) {
// It create the backup of the files of the wordpress//
$assetid = $response['Result']['asset_id'];
$account_name = $response['Result']['stor_name'];
$share_name = $response['Result']['share_name'];
$sas_token = $response['Result']['sas_token'];
$this->req_id = $response['Result']['_id'];
$backup_type = $response['Result']['backup_type'];
// suspend the crons until backup & restore is completed
$this->utiObj->reset_actifend_crons('remove');
// option setting
update_option('ActifendBackupStatus', 'initiated');
$file = BACKUP_FILE; // defined in actifendConstants
$max_range = 3 * 1024 * 1024;
if ($backup_type == 'full') {
// create the backup zip file
$this->actifend_wp_backup();
$dir_path = BACKUP_DIR; // defined in actifendConstants
// remove the directory used to keep the backup content directory.
$this->utiObj->actifend_rrmdir($dir_path);
}
// List the files and the directory ----------------------------------
// check if the folder with assetid as name exists
$uri = 'https://' . $account_name . '.file.core.windows.net/' . $share_name . "/" . $assetid . "?" . $sas_token . "&restype=directory";
$today = gmdate("D, d M Y G:i:s T");
$az_headers = array("x-ms-date" => $today, "x-ms-version" => "2015-04-05");
$response = wp_remote_head($uri, array('headers' => $az_headers));
$resp_status = $response['response']['code'];
if ($resp_status === 404) {
debug_log("$assetid Folder does not exist!");
// create the folder!
$uri = 'http://' . $account_name . '.file.core.windows.net/' . $share_name . "/" . $assetid . "?" . $sas_token . "&restype=directory";
$az_headers = array_merge($az_headers, array('Content-Length' => '0'));
$response = wp_remote_request($uri, array('headers' => $az_headers,
'method' => 'PUT'));
if ($response['response']['code'] == 201) {
debug_log("$assetid Folder created on Actifend-cloud!");
}
// Now create a sub folder
$subfolder = date("Y-m-d_G_i_s");
$uri = 'http://' . $account_name . '.file.core.windows.net/' . $share_name . "/" . $assetid . "/" . $subfolder . "?" . $sas_token . "&restype=directory";
$addl_headers = array("x-ms-meta-bkp_type" => "full",
"x-ms-meta-category" => "backup");
$args = array_merge($az_headers, $addl_headers);
$response = wp_remote_request($uri, array('headers' => $args, 'method' => 'PUT'));
if ($response['response']['code'] === 201) {
debug_log("Subfolder " . $subfolder . " with metadata created. Now move on and place the file there ...
");
}
} elseif ($resp_status === 200) {
debug_log("Folder " . $assetid . " exists! move on and look for the subfolder...
");
// Build the subfolder name
// create the subfolder as it would not be existing!!
$subfolder = date("Y-m-d_G_i_s");
$uri = 'http://' . $account_name . '.file.core.windows.net/' . $share_name . "/" . $assetid . "/" . $subfolder . "?" . $sas_token . "&restype=directory";
$addl_headers = array("x-ms-meta-bkp_type" => "full",
"x-ms-meta-category" => "backup",
"Content-length" => '0');
$args = array_merge($az_headers, $addl_headers);
$response = wp_remote_request($uri, array('headers' => $args, 'method' => 'PUT'));
if ($response['response']['code'] === 201) {
debug_log("Subfolder " . $subfolder . " with metadata created. Now move on and place the file there ...
");
} else {
update_option('ActifendBackupStatus',
'Unable to create the Subfolder in Actifend-cloud.');
exit("Unable to create the folder, exiting!");
}
} else {
debug_log("
%s", $response['response']); update_option('ActifendBackupStatus', $response['response']); return; } // Call Azure API to save the file in the storage. $fsize = filesize(trailingslashit( WP_CONTENT_DIR ) . BACKUP_FILE); $uri = 'https://' . $account_name . '.file.core.windows.net/' . $share_name . "/" . $assetid . "/" . $subfolder . "/" . $file . "?" . $sas_token; $today = gmdate("D, d M Y G:i:s T"); $addl_headers = array('Content-Length' => '0', 'x-ms-type' => 'file', 'x-ms-content-length' => (string) $fsize); $heads = array_merge($az_headers, $addl_headers); $response = wp_remote_request($uri, array('headers' => $heads, 'method' => 'PUT')); if (is_wp_error($response) || ($response['response']['code'] == 400)) { debug_log("Starting file upload to Actifend-cloud failed!"); update_option('ActifendBackupStatus', 'File upload to Actifend-cloud incomplete!'); return; } $handle = fopen(trailingslashit( WP_CONTENT_DIR ) . BACKUP_FILE, "rb"); $uri_range = $uri . '&comp=range'; // For an update operation, the range can be up to 4 MB in size. // file needs to be uploaded in chunks of $max_range (defined above) if ($fsize > $max_range) { $mod = $fsize / $max_range; $iters = round($mod, 0); if ($iters < $mod) { $iters += 1; } } else { $iters = 1; } $start_byte = 0; $ie = 0; for ($i = 1; $i <= $iters; $i++) { // set the start and end byte values for the range $end_byte = $start_byte + $max_range - 1; if ($i == $iters) { $end_byte = $fsize - 1; } // Read the contents in the file $contents = fread($handle, ($end_byte - $start_byte + 1)); // initiate the api call $addl_headers = array("x-ms-write" => "update", "x-ms-range" => "bytes=" . $start_byte . "-" . $end_byte); $heads = array_merge($az_headers, $addl_headers); $response = wp_remote_request($uri_range, array('headers' => $heads, 'body' => $contents, 'method' => 'PUT')); if (is_wp_error($response) || ($response['response']['code'] !== 201)) { debug_log("Upload to Actifend-cloud failed!"); debug_log($response); update_option('ActifendBackupStatus', $response); } else { $start_byte = $end_byte + 1; $ie = $i; } } if ($ie != $iters) { debug_log("File upload incomplete! :("); update_option('ActifendBackupStatus', 'File upload to Actifend-cloud incomplete!'); } else { debug_log("File upload to Azure storage completed!