<?php
/*
* BACKUPLY
* https://backuply.com
* (c) Backuply Team
*/
if(!defined('ABSPATH')) {
die('HACKING ATTEMPT!');
}
function backuply_get_protocols(){
$protocols['ftp'] = 'FTP';
$protocols['gdrive'] = 'Google Drive';
$protocols['bcloud'] = 'Backuply Cloud';
if(defined('BACKUPLY_PRO')) {
if(!function_exists('backuply_get_pro_backups') && defined('BACKUPLY_PRO_DIR')) {
include_once(BACKUPLY_PRO_DIR . '/functions.php');
}
$protocols += backuply_get_pro_backups();
}
return $protocols;
}
function backuply_create_backup_folders(){
// Creating Backuply Folder
if(!file_exists(BACKUPLY_BACKUP_DIR)) {
@mkdir(BACKUPLY_BACKUP_DIR, 0755, true);
}
$random_string = wp_generate_password(6, false);
// Creating backups_info folder
if(file_exists(BACKUPLY_BACKUP_DIR . 'backups_info')){
@rename(BACKUPLY_BACKUP_DIR . 'backups_info', BACKUPLY_BACKUP_DIR . 'backups_info-'. $random_string);
}
// Creating backups folder
if(file_exists(BACKUPLY_BACKUP_DIR . 'backups')){
@rename(BACKUPLY_BACKUP_DIR . 'backups', BACKUPLY_BACKUP_DIR . 'backups-'. $random_string);
}
$backup_info = backuply_glob('backups_info');
$backups = backuply_glob('backups');
if(empty($backup_info)){
@mkdir(BACKUPLY_BACKUP_DIR . 'backups_info-' . $random_string, 0755, true);
}
if(empty($backups)){
@mkdir(BACKUPLY_BACKUP_DIR . 'backups-' . $random_string, 0755, true);
}
}
// Add the htaccess file to protect us !
function backuply_add_htaccess(){
if(!file_exists(BACKUPLY_BACKUP_DIR)) {
@mkdir(BACKUPLY_BACKUP_DIR, 0755, true);
}
$htaccess = @fopen(BACKUPLY_BACKUP_DIR . '.htaccess', 'w');
if(!$htaccess) {
return false;
}
@fwrite($htaccess, 'deny from all');
@fclose($htaccess);
return true;
}
// Add the webconfig file to protect us !
function backuply_add_web_config(){
if(file_exists(BACKUPLY_BACKUP_DIR . 'web.config')){
return true;
}
$web_config = @fopen(BACKUPLY_BACKUP_DIR . 'web.config', 'w');
if(!$web_config) {
return false;
}
$web_conf = '<configuration>
<system.webServer>
<authorization>
<deny users="*" />
</authorization>
</system.webServer>
</configuration>';
@fwrite($web_config, $web_conf);
@fclose($web_config);
return true;
}
// Add the htaccess folder to protect us !
function backuply_add_index_files(){
if(!file_exists(BACKUPLY_BACKUP_DIR)) {
@mkdir(BACKUPLY_BACKUP_DIR, 0755, true);
}
$php_protection = '<?php //Backuply';
$html_protection = '<html><body><a href="https://backuply.com" target="_blank">WordPress backups by Backuply</a></body></html>';
@file_put_contents(BACKUPLY_BACKUP_DIR . 'index.html', $html_protection);
@file_put_contents(BACKUPLY_BACKUP_DIR . 'index.php', $php_protection);
$backups = backuply_glob('backups');
if(!empty($backups)){
if(!file_exists($backups . '/index.html')){
@file_put_contents($backups . '/index.html', $html_protection);
}
if(!file_exists($backups . '/index.php')){
@file_put_contents($backups . '/index.php', $php_protection);
}
// Protecting backups-*/tmp folder
if(!file_exists($backups . '/tmp/index.html')){
@mkdir($backups . '/tmp');
@file_put_contents($backups . '/tmp/index.html', $html_protection);
}
if(!file_exists($backups . '/tmp/index.php')){
@file_put_contents($backups . '/tmp/index.php', $php_protection);
}
}
// Protecting backups_info folder
$backups_info = backuply_glob('backups_info');
if(!empty($backups_info)){
if(!file_exists($backups_info . '/index.html')){
@file_put_contents($backups_info . '/index.html', $html_protection);
}
if(!file_exists($backups_info . '/index.php')){
@file_put_contents($backups_info . '/index.php', $php_protection);
}
}
}
function backuply_glob($relative_path){
$glob = glob(BACKUPLY_BACKUP_DIR . $relative_path . '-*', GLOB_ONLYDIR);
if(!empty($glob[0])){
return $glob[0];
}
return false;
}
function backuply_kill_process($is_restore = false) {
delete_option('backuply_status');
update_option('backuply_backup_stopped', true);
if(!empty($is_restore)){
backuply_clean_restoration_file();
}
die();
}
function backuply_clean_restoration_file(){
// Restore is complete now we dont need this
if(file_exists(BACKUPLY_BACKUP_DIR.'/restoration/restoration.php')) {
@unlink(BACKUPLY_BACKUP_DIR.'/restoration/restoration.php');
}
if(is_dir(BACKUPLY_BACKUP_DIR.'/restoration')) {
@rmdir(BACKUPLY_BACKUP_DIR.'/restoration');
}
// Deleting restore index file
if(file_exists(ABSPATH . '/backuply-restore.php')){
unlink(ABSPATH . '/backuply-restore.php');
}
}
// If there is a restore or backup task running
function backuply_active(){
global $backuply;
$backuply['status'] = get_option('backuply_status');
// Nothing there
if(empty($backuply['status']['last_time'])){
return false;
}
// No updates since 5 min
if((time() - BACKUPLY_TIMEOUT_TIME) > $backuply['status']['last_time']){
return false;
}
return true;
}
// Verifies the backuply key
function backuply_verify_self($key, $restore_key = false) {
if(empty($key)) {
return false;
}
$config = backuply_get_config();
if(!empty($restore_key)){
if(!empty($config['RESTORE_KEY']) && urldecode($key) == $config['RESTORE_KEY']) {
return true;
}
return false;
}
if(urldecode($key) == $config['BACKUPLY_KEY']) {
return true;
}
return false;
}
// Wp-Cron handle for timeout check i.e. clean dead processes
// Terminates process if no update for 30 min
function backuply_timeout_check($is_restore) {
global $backuply;
// Is it a restore check ?
if(!empty($is_restore)) {
$file = BACKUPLY_BACKUP_DIR . '/restoration/restoration.php';
if(!file_exists($file)) {
die();
}
$fm_time = filemtime($file);
if((time() - $fm_time) >= BACKUPLY_TIMEOUT_TIME) {
backuply_kill_process(true);
}
// Its a backup process
} else {
if(empty($backuply['status']['last_update'])){
backuply_kill_process();
}
if((time() - $backuply['status']['last_update']) >= BACKUPLY_TIMEOUT_TIME) {
backuply_kill_process();
}
}
// To check after 5 minutes again
wp_schedule_single_event(time() + BACKUPLY_TIMEOUT_TIME, 'backuply_timeout_check', array('is_restore' => $is_restore));
}
// Create a config file and set it with a key
function backuply_set_config() {
$write['BACKUPLY_KEY'] = backuply_csrf_get_token();
// $write['RESTORE_KEY'] = backuply_csrf_get_token();
update_option('backuply_config_keys', $write);
}
function backuply_set_config_file(){
$write = get_option('backuply_config_keys', []);
if(empty($write)){
return false;
}
$config_file = BACKUPLY_BACKUP_DIR . 'backuply_config.php';
$fp = @fopen($config_file, 'w');
if(!is_resource($fp)){
return;
}
@fwrite($fp, "<?php exit();?>\n" . json_encode($write, JSON_PRETTY_PRINT));
@fclose($fp);
@chmod($config_file, 0600);
return true;
}
function backuply_update_restore_key(){
$config = get_option('backuply_config_keys');
if(empty($config)) {
backuply_set_config();
return;
}
$restore_key = backuply_csrf_get_token();
$config['RESTORE_KEY'] = $restore_key;
update_option('backuply_config_keys', $config);
}
// Sets Backup Location details in Restoration File
function backuply_set_restoration_file($loc) {
$write['protocol'] = $loc['protocol'];
$write['name'] = $loc['name'];
$restoration_file = BACKUPLY_BACKUP_DIR . 'restoration/restoration.php';
$fp = @fopen($restoration_file, 'w');
if(!is_resource($fp)){
return;
}
if (0 == filesize($restoration_file)){
// file is empty
@fwrite($fp, "<?php exit();?>\n");
}
@fwrite($fp, json_encode($write, JSON_PRETTY_PRINT));
@fclose($fp);
@chmod($restoration_file, 0600);
}
// Sets Backup Location details in Restoration File
function backuply_get_restoration_data() {
$restoration_file = BACKUPLY_BACKUP_DIR . 'restoration/restoration.php';
$fp = @fopen($restoration_file, 'r');
@fseek($fp, 16);
if(filesize($restoration_file) == 0){
return;
}
$content = @fread($fp, filesize($restoration_file));
@fclose($fp);
if(empty($content)) {
return [];
}
$restro = json_decode($content, true);
return $restro;
}
// Get Config