$jobvalues['cron'] = $_POST['weekcronminutes'] . ' ' . $_POST['weekcronhours'] . ' * * ' . $_POST['weekcronwday'];
     }
     if ($_POST['cronbtype'] == 'day') {
         $jobvalues['cron'] = $_POST['daycronminutes'] . ' ' . $_POST['daycronhours'] . ' * * *';
     }
     if ($_POST['cronbtype'] == 'hour') {
         $jobvalues['cron'] = $_POST['hourcronminutes'] . ' * * * *';
     }
 }
 $jobvalues['cronnextrun'] = backwpup_cron_next($jobvalues['cron']);
 $jobvalues['mailaddresslog'] = isset($_POST['mailaddresslog']) ? sanitize_email($_POST['mailaddresslog']) : '';
 $jobvalues['mailerroronly'] = isset($_POST['mailerroronly']) && $_POST['mailerroronly'] == 1 ? true : false;
 $checedtables = array();
 if (isset($_POST['jobtabs'])) {
     foreach ($_POST['jobtabs'] as $dbtable) {
         $checedtables[] = backwpup_base64($dbtable);
     }
 }
 global $wpdb;
 $tables = $wpdb->get_col('SHOW TABLES FROM `' . DB_NAME . '`');
 $jobvalues['dbexclude'] = array();
 foreach ($tables as $dbtable) {
     if (!in_array($dbtable, $checedtables)) {
         $jobvalues['dbexclude'][] = $dbtable;
     }
 }
 $jobvalues['dbshortinsert'] = isset($_POST['dbshortinsert']) && $_POST['dbshortinsert'] == 1 ? true : false;
 $jobvalues['maintenance'] = isset($_POST['maintenance']) && $_POST['maintenance'] == 1 ? true : false;
 $jobvalues['fileexclude'] = isset($_POST['fileexclude']) ? stripslashes($_POST['fileexclude']) : '';
 $jobvalues['dirinclude'] = isset($_POST['dirinclude']) ? stripslashes($_POST['dirinclude']) : '';
 $jobvalues['backuproot'] = isset($_POST['backuproot']) && $_POST['backuproot'] == 1 ? true : false;
Example #2
0
function backwpup_jobstart($jobid = '', $cronstart = false)
{
    global $wpdb, $wp_version;
    $jobid = (int) trim($jobid);
    if (empty($jobid) or !is_integer($jobid)) {
        return false;
    }
    //check if a job running
    if ($infile = backwpup_get_working_file()) {
        trigger_error(__("A job already running!", "backwpup"), E_USER_WARNING);
        return false;
    }
    //clean var
    $backwpup_static = array();
    $backwpup_working = array();
    //get temp dir
    $backwpup_static['TEMPDIR'] = backwpup_get_temp();
    if (!is_writable($backwpup_static['TEMPDIR'])) {
        trigger_error(__("Temp dir not writeable", "backwpup"), E_USER_ERROR);
        return false;
    } else {
        //clean up old temp files
        if ($dir = opendir($backwpup_static['TEMPDIR'])) {
            while (($file = readdir($dir)) !== false) {
                if (is_readable($backwpup_static['TEMPDIR'] . $file) and is_file($backwpup_static['TEMPDIR'] . $file)) {
                    if ($file != '.' and $file != '..') {
                        unlink($backwpup_static['TEMPDIR'] . $file);
                    }
                }
            }
            closedir($dir);
        }
        //create .htaccess for apache and index.php for folder security
        if (!is_file($backwpup_static['TEMPDIR'] . '.htaccess')) {
            file_put_contents($backwpup_static['TEMPDIR'] . '.htaccess', "<Files \"*\">\n<IfModule mod_access.c>\nDeny from all\n</IfModule>\n<IfModule !mod_access_compat>\n<IfModule mod_authz_host.c>\nDeny from all\n</IfModule>\n</IfModule>\n<IfModule mod_access_compat>\nDeny from all\n</IfModule>\n</Files>");
        }
        if (!is_file($backwpup_static['TEMPDIR'] . 'index.php')) {
            file_put_contents($backwpup_static['TEMPDIR'] . 'index.php', "\n");
        }
    }
    //Write running file to prevent dobble runnging
    file_put_contents($backwpup_static['TEMPDIR'] . '.running', serialize(array('timestamp' => time(), 'JOBID' => $jobid, 'LOGFILE' => '', 'STEPSPERSENT' => 0, 'STEPPERSENT' => 0, 'WORKING' => array('PID' => 0))));
    //Set needed WP vars
    $backwpup_static['WP']['DB_NAME'] = DB_NAME;
    $backwpup_static['WP']['DB_USER'] = DB_USER;
    $backwpup_static['WP']['DB_PASSWORD'] = DB_PASSWORD;
    $backwpup_static['WP']['DB_HOST'] = DB_HOST;
    $backwpup_static['WP']['DB_CHARSET'] = DB_CHARSET;
    $backwpup_static['WP']['DB_COLLATE'] = DB_COLLATE;
    $backwpup_static['WP']['OPTIONS_TABLE'] = $wpdb->options;
    $backwpup_static['WP']['TABLE_PREFIX'] = $wpdb->prefix;
    $backwpup_static['WP']['BLOGNAME'] = get_bloginfo('name');
    if (defined('WP_SITEURL')) {
        $backwpup_static['WP']['SITEURL'] = trailingslashit(WP_SITEURL);
    } else {
        $backwpup_static['WP']['SITEURL'] = trailingslashit(get_option('siteurl'));
    }
    $backwpup_static['WP']['TIMEDIFF'] = get_option('gmt_offset') * 3600;
    $backwpup_static['WP']['WPLANG'] = WPLANG;
    $backwpup_static['WP']['VERSION'] = $wp_version;
    $backwpup_static['WP']['CHARSET'] = get_option('blog_charset');
    $backwpup_static['WP']['MEMORY_LIMIT'] = WP_MEMORY_LIMIT;
    if (defined('ALTERNATE_WP_CRON')) {
        $backwpup_static['WP']['ALTERNATE_CRON'] = ALTERNATE_WP_CRON;
    } else {
        $backwpup_static['WP']['ALTERNATE_CRON'] = false;
    }
    //WP folder
    $backwpup_static['WP']['ABSPATH'] = rtrim(str_replace('\\', '/', ABSPATH), '/') . '/';
    $backwpup_static['WP']['WP_CONTENT_DIR'] = rtrim(str_replace('\\', '/', WP_CONTENT_DIR), '/') . '/';
    $backwpup_static['WP']['WP_PLUGIN_DIR'] = rtrim(str_replace('\\', '/', WP_PLUGIN_DIR), '/') . '/';
    $backwpup_static['WP']['WP_THEMES_DIR'] = rtrim(str_replace('\\', '/', trailingslashit(WP_CONTENT_DIR) . 'themes/'), '/') . '/';
    $backwpup_static['WP']['WP_UPLOAD_DIR'] = backwpup_get_upload_dir();
    $backwpup_static['WP']['WPINC'] = WPINC;
    $backwpup_static['WP']['MULTISITE'] = is_multisite();
    $backwpup_static['WP']['ADMINURL'] = backwpup_admin_url('admin.php');
    //Set plugin data
    $backwpup_static['BACKWPUP']['PLUGIN_BASEDIR'] = BACKWPUP_PLUGIN_BASEDIR;
    $backwpup_static['BACKWPUP']['VERSION'] = BACKWPUP_VERSION;
    $backwpup_static['BACKWPUP']['BACKWPUP_DESTS'] = BACKWPUP_DESTS;
    //Set config data
    $backwpup_static['CFG'] = get_option('backwpup');
    //check exists gzip functions
    if (!function_exists('gzopen')) {
        $backwpup_static['CFG']['gzlogs'] = false;
    }
    if (!class_exists('ZipArchive')) {
        $backwpup_static['CFG']['phpzip'] = false;
    }
    //Check working times
    if (empty($backwpup_static['CFG']['jobstepretry']) or !is_int($backwpup_static['CFG']['jobstepretry']) or $backwpup_static['CFG']['jobstepretry'] > 100) {
        $backwpup_static['CFG']['jobstepretry'] = 3;
    }
    if (empty($backwpup_static['CFG']['jobscriptretry']) or !is_int($backwpup_static['CFG']['jobscriptretry']) or $backwpup_static['CFG']['jobscriptretry'] > 100) {
        $backwpup_static['CFG']['jobscriptretry'] = 5;
    }
    if (empty($backwpup_static['CFG']['jobscriptruntime']) or !is_int($backwpup_static['CFG']['jobscriptruntime']) or $backwpup_static['CFG']['jobscriptruntime'] > 100) {
        $backwpup_static['CFG']['jobscriptruntime'] = ini_get('max_execution_time');
    }
    if (empty($backwpup_static['CFG']['jobscriptruntimelong']) or !is_int($backwpup_static['CFG']['jobscriptruntimelong']) or $backwpup_static['CFG']['jobscriptruntimelong'] > 1000) {
        $backwpup_static['CFG']['jobscriptruntimelong'] = 300;
    }
    //Set job data
    $backwpup_static['JOB'] = backwpup_get_job_vars($jobid);
    //STATIC data
    $backwpup_static['JOBRUNURL'] = BACKWPUP_PLUGIN_BASEURL . '/job/job_run.php';
    //Setup Logs dir
    $backwpup_static['CFG']['dirlogs'] = rtrim(str_replace('\\', '/', $backwpup_static['CFG']['dirlogs']), '/') . '/';
    if (!is_dir($backwpup_static['CFG']['dirlogs'])) {
        if (!mkdir(rtrim($backwpup_static['CFG']['dirlogs'], '/'), 0777, true)) {
            trigger_error(printf(__('Can not create folder for log files: %s', 'backwpup'), $backwpup_static['CFG']['dirlogs']), E_USER_ERROR);
            return false;
        }
    }
    //create .htaccess for apache and index.php for folder security
    if (!is_file($backwpup_static['CFG']['dirlogs'] . '.htaccess')) {
        file_put_contents($backwpup_static['CFG']['dirlogs'] . '.htaccess', "<Files \"*\">\n<IfModule mod_access.c>\nDeny from all\n</IfModule>\n<IfModule !mod_access_compat>\n<IfModule mod_authz_host.c>\nDeny from all\n</IfModule>\n</IfModule>\n<IfModule mod_access_compat>\nDeny from all\n</IfModule>\n</Files>");
    }
    if (!is_file($backwpup_static['CFG']['dirlogs'] . 'index.php')) {
        file_put_contents($backwpup_static['CFG']['dirlogs'] . 'index.php', "\n");
    }
    if (!is_writable($backwpup_static['CFG']['dirlogs'])) {
        trigger_error(__("Log folder not writeable!", "backwpup"), E_USER_ERROR);
        return false;
    }
    //set Logfile
    $backwpup_static['LOGFILE'] = $backwpup_static['CFG']['dirlogs'] . 'backwpup_log_' . backwpup_date_i18n('Y-m-d_H-i-s') . '.html';
    //create log file
    $fd = fopen($backwpup_static['LOGFILE'], 'w');
    //Create log file header
    fwrite($fd, "<html>\n<head>\n");
    fwrite($fd, "<meta name=\"robots\" content=\"noindex, nofollow\" />\n");
    fwrite($fd, "<meta name=\"backwpup_version\" content=\"" . BACKWPUP_VERSION . "\" />\n");
    fwrite($fd, "<meta name=\"backwpup_logtime\" content=\"" . time() . "\" />\n");
    fwrite($fd, str_pad("<meta name=\"backwpup_errors\" content=\"0\" />", 100) . "\n");
    fwrite($fd, str_pad("<meta name=\"backwpup_warnings\" content=\"0\" />", 100) . "\n");
    fwrite($fd, "<meta name=\"backwpup_jobid\" content=\"" . $backwpup_static['JOB']['jobid'] . "\" />\n");
    fwrite($fd, "<meta name=\"backwpup_jobname\" content=\"" . $backwpup_static['JOB']['name'] . "\" />\n");
    fwrite($fd, "<meta name=\"backwpup_jobtype\" content=\"" . $backwpup_static['JOB']['type'] . "\" />\n");
    fwrite($fd, str_pad("<meta name=\"backwpup_backupfilesize\" content=\"0\" />", 100) . "\n");
    fwrite($fd, str_pad("<meta name=\"backwpup_jobruntime\" content=\"0\" />", 100) . "\n");
    fwrite($fd, "<style type=\"text/css\">\n");
    fwrite($fd, ".timestamp {background-color:grey;}\n");
    fwrite($fd, ".warning {background-color:yellow;}\n");
    fwrite($fd, ".error {background-color:red;}\n");
    fwrite($fd, "#body {font-family:monospace;font-size:12px;white-space:nowrap;}\n");
    fwrite($fd, "</style>\n");
    fwrite($fd, "<title>" . sprintf(__('BackWPup log for %1$s from %2$s at %3$s', 'backwpup'), $backwpup_static['JOB']['name'], backwpup_date_i18n(get_option('date_format')), backwpup_date_i18n(get_option('time_format'))) . "</title>\n</head>\n<body id=\"body\">\n");
    fclose($fd);
    //Set job start settings
    $jobs = get_option('backwpup_jobs');
    $jobs[$backwpup_static['JOB']['jobid']]['starttime'] = time();
    //set start time for job
    $backwpup_static['JOB']['starttime'] = $jobs[$backwpup_static['JOB']['jobid']]['starttime'];
    $jobs[$backwpup_static['JOB']['jobid']]['logfile'] = $backwpup_static['LOGFILE'];
    //Set current logfile
    $jobs[$backwpup_static['JOB']['jobid']]['cronnextrun'] = backwpup_cron_next($jobs[$backwpup_static['JOB']['jobid']]['cron']);
    //set next run
    $backwpup_static['JOB']['cronnextrun'] = $jobs[$backwpup_static['JOB']['jobid']]['cronnextrun'];
    $jobs[$backwpup_static['JOB']['jobid']]['lastbackupdownloadurl'] = '';
    $backwpup_static['JOB']['lastbackupdownloadurl'] = '';
    update_option('backwpup_jobs', $jobs);
    //Save job Settings
    //Set todo
    $backwpup_static['TODO'] = explode('+', $backwpup_static['JOB']['type']);
    //only for jos that makes backups
    if (in_array('FILE', $backwpup_static['TODO']) or in_array('DB', $backwpup_static['TODO']) or in_array('WPEXP', $backwpup_static['TODO'])) {
        //make emty file list
        $backwpup_working['FILELIST'] = array();
        $backwpup_working['ALLFILESIZE'] = 0;
        //set Backup Dir if not set
        if (empty($backwpup_static['JOB']['backupdir'])) {
            $backwpup_static['JOB']['backupdir'] = $backwpup_static['TEMPDIR'];
        } else {
            //clear path
            $backwpup_static['JOB']['backupdir'] = rtrim(str_replace('\\', '/', $backwpup_static['JOB']['backupdir']), '/') . '/';
            //create backup dir if it not exists
            if (!is_dir($backwpup_static['JOB']['backupdir'])) {
                if (!mkdir(rtrim($backwpup_static['JOB']['backupdir'], '/'), 0777, true)) {
                    trigger_error(sprintf(__('Can not create folder for backups: %1$s', 'backwpup'), $backwpup_static['JOB']['backupdir']), E_USER_ERROR);
                    return false;
                }
            }
            //create .htaccess and index.php for folder security
            if (!is_file($backwpup_static['JOB']['backupdir'] . '.htaccess')) {
                file_put_contents($backwpup_static['JOB']['backupdir'] . '.htaccess', "<Files \"*\">\n<IfModule mod_access.c>\nDeny from all\n</IfModule>\n<IfModule !mod_access_compat>\n<IfModule mod_authz_host.c>\nDeny from all\n</IfModule>\n</IfModule>\n<IfModule mod_access_compat>\nDeny from all\n</IfModule>\n</Files>");
            }
            if (!is_file($backwpup_static['JOB']['backupdir'] . 'index.php')) {
                file_put_contents($backwpup_static['JOB']['backupdir'] . 'index.php', "\n");
            }
        }
        //check backup dir
        if (!is_writable($backwpup_static['JOB']['backupdir'])) {
            trigger_error(__("Backup folder not writeable!", "backwpup"), E_USER_ERROR);
            return false;
        }
        //set Backup file Name
        $backwpup_static['backupfile'] = $backwpup_static['JOB']['fileprefix'] . backwpup_date_i18n('Y-m-d_H-i-s') . $backwpup_static['JOB']['fileformart'];
    }
    $backwpup_static['CRONSTART'] = $cronstart;
    $backwpup_working['NONCE'] = wp_create_nonce('BackWPupJob');
    $backwpup_working['PID'] = 0;
    $backwpup_working['WARNING'] = 0;
    $backwpup_working['ERROR'] = 0;
    $backwpup_working['RESTART'] = 0;
    $backwpup_working['STEPSDONE'] = array();
    $backwpup_working['STEPTODO'] = 0;
    $backwpup_working['STEPDONE'] = 0;
    //build working steps
    $backwpup_working['STEPS'] = array();
    //setup job steps
    if (in_array('DB', $backwpup_static['TODO'])) {
        $backwpup_working['STEPS'][] = 'DB_DUMP';
    }
    if (in_array('WPEXP', $backwpup_static['TODO'])) {
        $backwpup_working['STEPS'][] = 'WP_EXPORT';
    }
    if (in_array('FILE', $backwpup_static['TODO'])) {
        $backwpup_working['STEPS'][] = 'FILE_LIST';
    }
    if (in_array('DB', $backwpup_static['TODO']) or in_array('WPEXP', $backwpup_static['TODO']) or in_array('FILE', $backwpup_static['TODO'])) {
        $backwpup_working['STEPS'][] = 'BACKUP_CREATE';
        //ADD Destinations
        if (!empty($backwpup_static['JOB']['backupdir']) and $backwpup_static['JOB']['backupdir'] != '/' and $backwpup_static['JOB']['backupdir'] != $backwpup_static['TEMPDIR']) {
            $backwpup_working['STEPS'][] = 'DEST_FOLDER';
        }
        if (!empty($backwpup_static['JOB']['mailaddress'])) {
            $backwpup_working['STEPS'][] = 'DEST_MAIL';
        }
        if (!empty($backwpup_static['JOB']['ftphost']) and !empty($backwpup_static['JOB']['ftpuser']) and !empty($backwpup_static['JOB']['ftppass']) and in_array('FTP', explode(',', strtoupper(BACKWPUP_DESTS)))) {
            $backwpup_working['STEPS'][] = 'DEST_FTP';
        }
        if (!empty($backwpup_static['JOB']['dropetoken']) and !empty($backwpup_static['JOB']['dropesecret']) and in_array('DROPBOX', explode(',', strtoupper(BACKWPUP_DESTS)))) {
            $backwpup_working['STEPS'][] = 'DEST_DROPBOX';
        }
        if (!empty($backwpup_static['JOB']['sugarrefreshtoken']) and !empty($backwpup_static['JOB']['sugarroot']) and in_array('SUGARSYNC', explode(',', strtoupper(BACKWPUP_DESTS)))) {
            $backwpup_working['STEPS'][] = 'DEST_SUGARSYNC';
        }
        if (!empty($backwpup_static['JOB']['awsAccessKey']) and !empty($backwpup_static['JOB']['awsSecretKey']) and !empty($backwpup_static['JOB']['awsBucket']) and in_array('S3', explode(',', strtoupper(BACKWPUP_DESTS)))) {
            $backwpup_working['STEPS'][] = 'DEST_S3';
        }
        if (!empty($backwpup_static['JOB']['GStorageAccessKey']) and !empty($backwpup_static['JOB']['GStorageSecret']) and !empty($backwpup_static['JOB']['GStorageBucket']) and in_array('GSTORAGE', explode(',', strtoupper(BACKWPUP_DESTS)))) {
            $backwpup_working['STEPS'][] = 'DEST_GSTORAGE';
        }
        if (!empty($backwpup_static['JOB']['rscUsername']) and !empty($backwpup_static['JOB']['rscAPIKey']) and !empty($backwpup_static['JOB']['rscContainer']) and in_array('RSC', explode(',', strtoupper(BACKWPUP_DESTS)))) {
            $backwpup_working['STEPS'][] = 'DEST_RSC';
        }
        if (!empty($backwpup_static['JOB']['msazureHost']) and !empty($backwpup_static['JOB']['msazureAccName']) and !empty($backwpup_static['JOB']['msazureKey']) and !empty($backwpup_static['JOB']['msazureContainer']) and in_array('MSAZURE', explode(',', strtoupper(BACKWPUP_DESTS)))) {
            $backwpup_working['STEPS'][] = 'DEST_MSAZURE';
        }
    }
    if (in_array('CHECK', $backwpup_static['TODO'])) {
        $backwpup_working['STEPS'][] = 'DB_CHECK';
    }
    if (in_array('OPTIMIZE', $backwpup_static['TODO'])) {
        $backwpup_working['STEPS'][] = 'DB_OPTIMIZE';
    }
    $backwpup_working['STEPS'][] = 'JOB_END';
    //mark all as not done
    foreach ($backwpup_working['STEPS'] as $step) {
        $backwpup_working[$step]['DONE'] = false;
    }
    //write working file
    file_put_contents($backwpup_static['TEMPDIR'] . '.running', serialize(array('timestamp' => time(), 'JOBID' => $backwpup_static['JOB']['jobid'], 'LOGFILE' => $backwpup_static['LOGFILE'], 'STEPSPERSENT' => 0, 'STEPPERSENT' => 0, 'WORKING' => $backwpup_working)));
    //write static file
    file_put_contents($backwpup_static['TEMPDIR'] . '.static', serialize($backwpup_static));
    //Run job
    $httpauthheader = '';
    if (!empty($backwpup_static['CFG']['httpauthuser']) and !empty($backwpup_static['CFG']['httpauthpassword'])) {
        $httpauthheader = array('Authorization' => 'Basic ' . base64_encode($backwpup_static['CFG']['httpauthuser'] . ':' . backwpup_base64($backwpup_static['CFG']['httpauthpassword'])));
    }
    if (!$backwpup_static['WP']['ALTERNATE_CRON']) {
        wp_remote_post($backwpup_static['JOBRUNURL'], array('timeout' => 3, 'blocking' => false, 'sslverify' => false, 'headers' => $httpauthheader, 'body' => array('nonce' => $backwpup_working['NONCE'], 'type' => 'start'), 'user-agent' => 'BackWPup'));
    }
    return $backwpup_static['LOGFILE'];
}
Example #3
0
function job_shutdown($signal = '')
{
    global $WORKING, $STATIC;
    if (empty($STATIC['LOGFILE'])) {
        //nothing on empty
        return;
    }
    //Put last error to log if one
    $lasterror = error_get_last();
    if (($lasterror['type'] == E_ERROR or $lasterror['type'] == E_PARSE or $lasterror['type'] == E_CORE_ERROR or $lasterror['type'] == E_COMPILE_ERROR or !empty($signal)) and is_writable($STATIC['LOGFILE'])) {
        if (!empty($signal)) {
            file_put_contents($STATIC['LOGFILE'], "<span class=\"timestamp\" title=\"[Line: " . __LINE__ . "|File: " . basename(__FILE__) . "|Mem: " . formatbytes(@memory_get_usage(true)) . "|Mem Max: " . formatbytes(@memory_get_peak_usage(true)) . "|Mem Limit: " . ini_get('memory_limit') . "|PID: " . getmypid() . "]\">" . date('Y/m/d H:i.s', time() + $STATIC['WP']['TIMEDIFF']) . ":</span> <span class=\"error\">[ERROR]" . sprintf(__('Signal %d send to script!', 'backwpup'), $signal) . "</span><br />\n", FILE_APPEND);
        }
        file_put_contents($STATIC['LOGFILE'], "<span class=\"timestamp\" title=\"[Line: " . $lasterror['line'] . "|File: " . basename($lasterror['file']) . "|Mem: " . formatbytes(@memory_get_usage(true)) . "|Mem Max: " . formatbytes(@memory_get_peak_usage(true)) . "|Mem Limit: " . ini_get('memory_limit') . "|PID: " . getmypid() . "]\">" . date('Y/m/d H:i.s', time() + $STATIC['WP']['TIMEDIFF']) . ":</span> <span class=\"error\">[ERROR]" . $lasterror['message'] . "</span><br />\n", FILE_APPEND);
        //write new log header
        $WORKING['ERROR']++;
        $fd = fopen($STATIC['LOGFILE'], 'r+');
        while (!feof($fd)) {
            $line = fgets($fd);
            if (stripos($line, "<meta name=\"backwpup_errors\"") !== false) {
                fseek($fd, $filepos);
                fwrite($fd, str_pad("<meta name=\"backwpup_errors\" content=\"" . $WORKING['ERROR'] . "\" />", 100) . "\n");
                break;
            }
            $filepos = ftell($fd);
        }
        fclose($fd);
    }
    //no more restarts
    $WORKING['RESTART']++;
    if ((!empty($STATIC['WP']['ALTERNATE_CRON']) or $WORKING['RESTART'] >= $STATIC['CFG']['jobscriptretry']) and is_file($STATIC['TEMPDIR'] . '.running') and is_writable($STATIC['LOGFILE'])) {
        //only x restarts allowed
        if (!empty($STATIC['WP']['ALTERNATE_CRON'])) {
            file_put_contents($STATIC['LOGFILE'], "<span class=\"timestamp\" title=\"[Line: " . __LINE__ . "|File: " . basename(__FILE__) . "\"|Mem: " . formatbytes(@memory_get_usage(true)) . "|Mem Max: " . formatbytes(@memory_get_peak_usage(true)) . "|Mem Limit: " . ini_get('memory_limit') . "|PID: " . getmypid() . "]>" . date('Y/m/d H:i.s', time() + $STATIC['WP']['TIMEDIFF']) . ":</span> <span class=\"error\">[ERROR]" . __('Can not restart on alternate cron....', 'backwpup') . "</span><br />\n", FILE_APPEND);
        } else {
            file_put_contents($STATIC['LOGFILE'], "<span class=\"timestamp\" title=\"[Line: " . __LINE__ . "|File: " . basename(__FILE__) . "\"|Mem: " . formatbytes(@memory_get_usage(true)) . "|Mem Max: " . formatbytes(@memory_get_peak_usage(true)) . "|Mem Limit: " . ini_get('memory_limit') . "|PID: " . getmypid() . "]>" . date('Y/m/d H:i.s', time() + $STATIC['WP']['TIMEDIFF']) . ":</span> <span class=\"error\">[ERROR]" . __('To many restarts....', 'backwpup') . "</span><br />\n", FILE_APPEND);
        }
        $WORKING['ERROR']++;
        $fd = fopen($STATIC['LOGFILE'], 'r+');
        while (!feof($fd)) {
            $line = fgets($fd);
            if (stripos($line, "<meta name=\"backwpup_errors\"") !== false) {
                fseek($fd, $filepos);
                fwrite($fd, str_pad("<meta name=\"backwpup_errors\" content=\"" . $WORKING['ERROR'] . "\" />", 100) . "\n");
                break;
            }
            $filepos = ftell($fd);
        }
        fclose($fd);
        job_end();
    }
    //set PID to 0
    $WORKING['PID'] = 0;
    //Excute jobrun again
    if (!is_file($STATIC['TEMPDIR'] . '.running')) {
        exit;
    }
    if (is_writable($STATIC['LOGFILE'])) {
        file_put_contents($STATIC['LOGFILE'], "<span class=\"timestamp\" title=\"[Line: " . __LINE__ . "|File: " . basename(__FILE__) . "|Mem: " . formatbytes(@memory_get_usage(true)) . "|Mem Max: " . formatbytes(@memory_get_peak_usage(true)) . "|Mem Limit: " . ini_get('memory_limit') . "|PID: " . getmypid() . "]\">" . date('Y/m/d H:i.s', time() + $STATIC['WP']['TIMEDIFF']) . ":</span> <span>" . $WORKING['RESTART'] . '. ' . __('Script stop! Will started again now!', 'backwpup') . "</span><br />\n", FILE_APPEND);
    }
    update_working_file(true);
    if (!empty($STATIC['JOBRUNURL'])) {
        if (function_exists('curl_exec')) {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $STATIC['JOBRUNURL']);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, array('BackWPupJobTemp' => $STATIC['TEMPDIR'], 'nonce' => $WORKING['NONCE'], 'type' => 'restart'));
            curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($ch, CURLOPT_USERAGENT, 'BackWPup');
            if (!empty($STATIC['CFG']['httpauthuser']) and !empty($STATIC['CFG']['httpauthpassword'])) {
                curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
                curl_setopt($ch, CURLOPT_USERPWD, $STATIC['CFG']['httpauthuser'] . ':' . backwpup_base64($STATIC['CFG']['httpauthpassword']));
            }
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
            curl_exec($ch);
        } else {
            //use fopen if no curl
            $urlParsed = parse_url($STATIC['JOBRUNURL']);
            if ($urlParsed['scheme'] == 'https') {
                $host = 'ssl://' . $urlParsed['host'];
                $port = !empty($urlParsed['port']) ? $urlParsed['port'] : 443;
            } else {
                $host = $urlParsed['host'];
                $port = !empty($urlParsed['port']) ? $urlParsed['port'] : 80;
            }
            $query = http_build_query(array('nonce' => $WORKING['NONCE'], 'type' => 'restart'));
            $path = (isset($urlParsed['path']) ? $urlParsed['path'] : '/') . (isset($urlParsed['query']) ? '?' . $urlParsed['query'] : '');
            $header = "POST " . $path . " HTTP/1.1\r\n";
            $header .= "Host: " . $urlParsed['host'] . "\r\n";
            $header .= "User-Agent: BackWPup\r\n";
            $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
            $header .= "Content-Length: " . strlen($query) . "\r\n";
            if (!empty($STATIC['CFG']['httpauthuser']) and !empty($STATIC['CFG']['httpauthpassword'])) {
                $header .= "Authorization: Basic " . base64_encode($STATIC['CFG']['httpauthuser'] . ':' . backwpup_base64($STATIC['CFG']['httpauthpassword'])) . "\r\n";
            }
            $header .= "Connection: Close\r\n\r\n";
            $header .= $query;
            $fp = fsockopen($host, $port, $errno, $errstr, 3);
            fwrite($fp, $header);
            fclose($fp);
        }
    }
    exit;
}
             if (function_exists('ftp_ssl_connect') and $jobvalue['ftpssl']) {
                 //make SSL FTP connection
                 $ftp_conn_id = ftp_ssl_connect($jobvalue['ftphost'], $jobvalue['ftphostport'], 10);
             } elseif (!$jobvalue['ftpssl']) {
                 //make normal FTP conection if SSL not work
                 $ftp_conn_id = ftp_connect($jobvalue['ftphost'], $jobvalue['ftphostport'], 10);
             }
             $loginok = false;
             if ($ftp_conn_id) {
                 //FTP Login
                 if (@ftp_login($ftp_conn_id, $jobvalue['ftpuser'], backwpup_base64($jobvalue['ftppass']))) {
                     $loginok = true;
                 } else {
                     //if PHP ftp login don't work use raw login
                     ftp_raw($ftp_conn_id, 'USER ' . $jobvalue['ftpuser']);
                     $return = ftp_raw($ftp_conn_id, 'PASS ' . backwpup_base64($jobvalue['ftppass']));
                     if (substr(trim($return[0]), 0, 3) <= 400) {
                         $loginok = true;
                     }
                 }
             }
             if ($loginok) {
                 ftp_pasv($ftp_conn_id, $jobvalue['ftppasv']);
                 ftp_delete($ftp_conn_id, $backupfile);
             } else {
                 $backwpup_message .= 'FTP: ' . __('Login failure!', 'backwpup') . '<br />';
             }
         }
     }
 }
 delete_transient('backwpup_backups_chache');
Example #5
0
<p><?php 
_e('Is your blog behind a http basic authentication (.htaccess)? Then you must set the username and password four authentication.', 'backwpup');
?>
</p>
<table class="form-table"> 
</tr>
<th scope="row"><label for="httpauthuser"><?php 
_e('Username:'******'backwpup');
?>
</label></th> 
<td><input name="httpauthuser" type="text" id="httpauthuser" value="<?php 
echo $cfg['httpauthuser'];
?>
" class="regular-text" autocomplete="off" />
</td> 
</tr>
<tr valign="top"> 
<th scope="row"><label for="httpauthpassword"><?php 
_e('Password:'******'backwpup');
?>
</label></th>
<td><input name="httpauthpassword" type="password" id="httpauthpassword" value="<?php 
echo backwpup_base64($cfg['httpauthpassword']);
?>
" class="regular-text" autocomplete="off" />
</tr>
</table>

<p class="submit"><input type="submit" name="submit" id="submit" class="button-primary" value="Save Changes"  /></p>
</form>
</div>
Example #6
0
function backwpup_get_backup_files($jobid, $dest)
{
    global $backwpup_message;
    if (empty($jobid) or !in_array(strtoupper($dest), explode(',', strtoupper(BACKWPUP_DESTS))) and $dest != 'FOLDER') {
        return false;
    }
    $jobs = get_option('backwpup_jobs');
    //Load jobs
    $jobvalue = $jobs[$jobid];
    $filecounter = 0;
    $files = array();
    //Get files/filinfo in backup folder
    if ($dest == 'FOLDER' and !empty($jobvalue['backupdir']) and is_dir($jobvalue['backupdir'])) {
        if ($dir = opendir($jobvalue['backupdir'])) {
            while (($file = readdir($dir)) !== false) {
                if (substr($file, 0, 1) == '.') {
                    continue;
                }
                if (is_file($jobvalue['backupdir'] . $file)) {
                    $files[$filecounter]['JOBID'] = $jobid;
                    $files[$filecounter]['DEST'] = $dest;
                    $files[$filecounter]['folder'] = $jobvalue['backupdir'];
                    $files[$filecounter]['file'] = $jobvalue['backupdir'] . $file;
                    $files[$filecounter]['filename'] = $file;
                    $files[$filecounter]['downloadurl'] = backwpup_admin_url('admin.php') . '?page=backwpupbackups&action=download&file=' . $jobvalue['backupdir'] . $file;
                    $files[$filecounter]['filesize'] = filesize($jobvalue['backupdir'] . $file);
                    $files[$filecounter]['time'] = filemtime($jobvalue['backupdir'] . $file);
                    $filecounter++;
                }
            }
            closedir($dir);
        }
    }
    //Get files/filinfo from Dropbox
    if ($dest == 'DROPBOX' and !empty($jobvalue['dropetoken']) and !empty($jobvalue['dropesecret'])) {
        require_once realpath(dirname(__FILE__) . '/../libs/dropbox.php');
        try {
            $dropbox = new backwpup_Dropbox('dropbox');
            $dropbox->setOAuthTokens($jobvalue['dropetoken'], $jobvalue['dropesecret']);
            $contents = $dropbox->metadata($jobvalue['dropedir']);
            if (is_array($contents)) {
                foreach ($contents['contents'] as $object) {
                    if ($object['is_dir'] != true) {
                        $files[$filecounter]['JOBID'] = $jobid;
                        $files[$filecounter]['DEST'] = $dest;
                        $files[$filecounter]['folder'] = "https://api-content.dropbox.com/1/files/" . $jobvalue['droperoot'] . "/" . dirname($object['path']) . "/";
                        $files[$filecounter]['file'] = $object['path'];
                        $files[$filecounter]['filename'] = basename($object['path']);
                        $files[$filecounter]['downloadurl'] = backwpup_admin_url('admin.php') . '?page=backwpupbackups&action=downloaddropbox&file=' . $object['path'] . '&jobid=' . $jobid;
                        $files[$filecounter]['filesize'] = $object['bytes'];
                        $files[$filecounter]['time'] = strtotime($object['modified']);
                        $filecounter++;
                    }
                }
            }
        } catch (Exception $e) {
            $backwpup_message .= 'DROPBOX: ' . $e->getMessage() . '<br />';
        }
    }
    //Get files/filinfo from Sugarsync
    if ($dest == 'SUGARSYNC' and !empty($jobvalue['sugarrefreshtoken'])) {
        if (!class_exists('SugarSync')) {
            require_once dirname(__FILE__) . '/../libs/sugarsync.php';
        }
        if (class_exists('SugarSync')) {
            try {
                $sugarsync = new SugarSync($jobvalue['sugarrefreshtoken']);
                $dirid = $sugarsync->chdir($jobvalue['sugardir'], $jobvalue['sugarroot']);
                $user = $sugarsync->user();
                $dir = $sugarsync->showdir($dirid);
                $getfiles = $sugarsync->getcontents('file');
                if (is_object($getfiles)) {
                    foreach ($getfiles->file as $getfile) {
                        $files[$filecounter]['JOBID'] = $jobid;
                        $files[$filecounter]['DEST'] = $dest;
                        $files[$filecounter]['folder'] = 'https://' . $user->nickname . '.sugarsync.com/' . $dir;
                        $files[$filecounter]['file'] = (string) $getfile->ref;
                        $files[$filecounter]['filename'] = utf8_decode((string) $getfile->displayName);
                        $files[$filecounter]['downloadurl'] = backwpup_admin_url('admin.php') . '?page=backwpupbackups&action=downloadsugarsync&file=' . (string) $getfile->ref . '&jobid=' . $jobid;
                        $files[$filecounter]['filesize'] = (int) $getfile->size;
                        $files[$filecounter]['time'] = strtotime((string) $getfile->lastModified);
                        $filecounter++;
                    }
                }
            } catch (Exception $e) {
                $backwpup_message .= 'SUGARSYNC: ' . $e->getMessage() . '<br />';
            }
        }
    }
    //Get files/filinfo from S3
    if ($dest == 'S3' and !empty($jobvalue['awsAccessKey']) and !empty($jobvalue['awsSecretKey']) and !empty($jobvalue['awsBucket'])) {
        if (!class_exists('AmazonS3')) {
            require_once dirname(__FILE__) . '/../libs/aws/sdk.class.php';
        }
        if (class_exists('AmazonS3')) {
            try {
                $s3 = new AmazonS3(array('key' => $jobvalue['awsAccessKey'], 'secret' => $jobvalue['awsSecretKey'], 'certificate_authority' => true));
                if (($contents = $s3->list_objects($jobvalue['awsBucket'], array('prefix' => $jobvalue['awsdir']))) !== false) {
                    foreach ($contents->body->Contents as $object) {
                        $files[$filecounter]['JOBID'] = $jobid;
                        $files[$filecounter]['DEST'] = $dest;
                        $files[$filecounter]['folder'] = "https://" . $jobvalue['awsBucket'] . ".s3.amazonaws.com/" . dirname((string) $object->Key) . '/';
                        $files[$filecounter]['file'] = (string) $object->Key;
                        $files[$filecounter]['filename'] = basename($object->Key);
                        $files[$filecounter]['downloadurl'] = backwpup_admin_url('admin.php') . '?page=backwpupbackups&action=downloads3&file=' . $object->Key . '&jobid=' . $jobid;
                        $files[$filecounter]['filesize'] = (string) $object->Size;
                        $files[$filecounter]['time'] = strtotime($object->LastModified);
                        $filecounter++;
                    }
                }
            } catch (Exception $e) {
                $backwpup_message .= 'Amazon S3: ' . $e->getMessage() . '<br />';
            }
        }
    }
    //Get files/filinfo from Google Storage
    if ($dest == 'GSTORAGE' and !empty($jobvalue['GStorageAccessKey']) and !empty($jobvalue['GStorageSecret']) and !empty($jobvalue['GStorageBucket'])) {
        if (!class_exists('AmazonS3')) {
            require_once dirname(__FILE__) . '/../libs/aws/sdk.class.php';
        }
        if (class_exists('AmazonS3')) {
            try {
                $gstorage = new AmazonS3(array('key' => $jobvalue['GStorageAccessKey'], 'secret' => $jobvalue['GStorageSecret'], 'certificate_authority' => true));
                $gstorage->set_hostname('storage.googleapis.com');
                $gstorage->allow_hostname_override(false);
                if (($contents = $gstorage->list_objects($jobvalue['GStorageBucket'], array('prefix' => $jobvalue['GStoragedir']))) !== false) {
                    foreach ($contents->body->Contents as $object) {
                        $files[$filecounter]['JOBID'] = $jobid;
                        $files[$filecounter]['DEST'] = $dest;
                        $files[$filecounter]['folder'] = "https://storage.cloud.google.com/" . $jobvalue['GStorageBucket'] . "/" . dirname((string) $object->Key) . '/';
                        $files[$filecounter]['file'] = (string) $object->Key;
                        $files[$filecounter]['filename'] = basename($object->Key);
                        $files[$filecounter]['downloadurl'] = "https://storage.cloud.google.com/" . $jobvalue['GStorageBucket'] . "/" . (string) $object->Key;
                        $files[$filecounter]['filesize'] = (string) $object->Size;
                        $files[$filecounter]['time'] = strtotime($object->LastModified);
                        $filecounter++;
                    }
                }
            } catch (Exception $e) {
                $backwpup_message .= sprintf(__('GStorage API: %s', 'backwpup'), $e->getMessage()) . '<br />';
            }
        }
    }
    //Get files/filinfo from Microsoft Azure
    if ($dest == 'MSAZURE' and !empty($jobvalue['msazureHost']) and !empty($jobvalue['msazureAccName']) and !empty($jobvalue['msazureKey']) and !empty($jobvalue['msazureContainer'])) {
        if (!class_exists('Microsoft_WindowsAzure_Storage_Blob')) {
            require_once dirname(__FILE__) . '/../libs/Microsoft/WindowsAzure/Storage/Blob.php';
        }
        if (class_exists('Microsoft_WindowsAzure_Storage_Blob')) {
            try {
                $storageClient = new Microsoft_WindowsAzure_Storage_Blob($jobvalue['msazureHost'], $jobvalue['msazureAccName'], $jobvalue['msazureKey']);
                $blobs = $storageClient->listBlobs($jobvalue['msazureContainer'], $jobvalue['msazuredir']);
                if (is_array($blobs)) {
                    foreach ($blobs as $blob) {
                        $files[$filecounter]['JOBID'] = $jobid;
                        $files[$filecounter]['DEST'] = $dest;
                        $files[$filecounter]['folder'] = "https://" . $jobvalue['msazureAccName'] . '.' . $jobvalue['msazureHost'] . "/" . $jobvalue['msazureContainer'] . "/" . dirname($blob->Name) . "/";
                        $files[$filecounter]['file'] = $blob->Name;
                        $files[$filecounter]['filename'] = basename($blob->Name);
                        $files[$filecounter]['downloadurl'] = backwpup_admin_url('admin.php') . '?page=backwpupbackups&action=downloadmsazure&file=' . $blob->Name . '&jobid=' . $jobid;
                        $files[$filecounter]['filesize'] = $blob->size;
                        $files[$filecounter]['time'] = strtotime($blob->lastmodified);
                        $filecounter++;
                    }
                }
            } catch (Exception $e) {
                $backwpup_message .= 'MSAZURE: ' . $e->getMessage() . '<br />';
            }
        }
    }
    //Get files/filinfo from RSC
    if ($dest == 'RSC' and !empty($jobvalue['rscUsername']) and !empty($jobvalue['rscAPIKey']) and !empty($jobvalue['rscContainer'])) {
        if (!class_exists('CF_Authentication')) {
            require_once dirname(__FILE__) . '/../libs/rackspace/cloudfiles.php';
        }
        if (class_exists('CF_Authentication')) {
            try {
                $auth = new CF_Authentication($jobvalue['rscUsername'], $jobvalue['rscAPIKey']);
                $auth->ssl_use_cabundle();
                if ($auth->authenticate()) {
                    $conn = new CF_Connection($auth);
                    $conn->ssl_use_cabundle();
                    $backwpupcontainer = $conn->get_container($jobvalue['rscContainer']);
                    $contents = $backwpupcontainer->get_objects(0, NULL, NULL, $jobvalue['rscdir']);
                    foreach ($contents as $object) {
                        $files[$filecounter]['JOBID'] = $jobid;
                        $files[$filecounter]['DEST'] = $dest;
                        $files[$filecounter]['folder'] = "RSC://" . $jobvalue['rscContainer'] . "/" . dirname($object->name) . "/";
                        $files[$filecounter]['file'] = $object->name;
                        $files[$filecounter]['filename'] = basename($object->name);
                        $files[$filecounter]['downloadurl'] = backwpup_admin_url('admin.php') . '?page=backwpupbackups&action=downloadrsc&file=' . $object->name . '&jobid=' . $jobid;
                        $files[$filecounter]['filesize'] = $object->content_length;
                        $files[$filecounter]['time'] = strtotime($object->last_modified);
                        $filecounter++;
                    }
                }
            } catch (Exception $e) {
                $backwpup_message .= 'RSC: ' . $e->getMessage() . '<br />';
            }
        }
    }
    //Get files/filinfo from FTP
    if ($dest == 'FTP' and !empty($jobvalue['ftphost']) and function_exists('ftp_connect') and !empty($jobvalue['ftpuser']) and !empty($jobvalue['ftppass'])) {
        if (function_exists('ftp_ssl_connect') and $jobvalue['ftpssl']) {
            //make SSL FTP connection
            $ftp_conn_id = ftp_ssl_connect($jobvalue['ftphost'], $jobvalue['ftphostport'], 10);
        } elseif (!$jobvalue['ftpssl']) {
            //make normal FTP conection if SSL not work
            $ftp_conn_id = ftp_connect($jobvalue['ftphost'], $jobvalue['ftphostport'], 10);
        }
        $loginok = false;
        if ($ftp_conn_id) {
            //FTP Login
            if (@ftp_login($ftp_conn_id, $jobvalue['ftpuser'], backwpup_base64($jobvalue['ftppass']))) {
                $loginok = true;
            } else {
                //if PHP ftp login don't work use raw login
                ftp_raw($ftp_conn_id, 'USER ' . $jobvalue['ftpuser']);
                $return = ftp_raw($ftp_conn_id, 'PASS ' . backwpup_base64($jobvalue['ftppass']));
                if (substr(trim($return[0]), 0, 3) <= 400) {
                    $loginok = true;
                }
            }
        }
        if ($loginok) {
            ftp_chdir($ftp_conn_id, $jobvalue['ftpdir']);
            $currentftpdir = rtrim(ftp_pwd($ftp_conn_id), '/') . '/';
            ftp_pasv($ftp_conn_id, $jobvalue['ftppasv']);
            if ($ftpfilelist = ftp_nlist($ftp_conn_id, $currentftpdir)) {
                foreach ($ftpfilelist as $ftpfiles) {
                    if (substr(basename($ftpfiles), 0, 1) == '.') {
                        continue;
                    }
                    $files[$filecounter]['JOBID'] = $jobid;
                    $files[$filecounter]['DEST'] = $dest;
                    $files[$filecounter]['folder'] = "ftp://" . $jobvalue['ftphost'] . ':' . $jobvalue['ftphostport'] . dirname($ftpfiles) . "/";
                    $files[$filecounter]['file'] = $ftpfiles;
                    $files[$filecounter]['filename'] = basename($ftpfiles);
                    $files[$filecounter]['downloadurl'] = "ftp://" . rawurlencode($jobvalue['ftpuser']) . ":" . rawurlencode(backwpup_base64($jobvalue['ftppass'])) . "@" . $jobvalue['ftphost'] . ':' . $jobvalue['ftphostport'] . $ftpfiles;
                    $files[$filecounter]['filesize'] = ftp_size($ftp_conn_id, $ftpfiles);
                    $files[$filecounter]['time'] = ftp_mdtm($ftp_conn_id, $ftpfiles);
                    $filecounter++;
                }
            }
        } else {
            $backwpup_message .= 'FTP: ' . __('Login failure!', 'backwpup') . '<br />';
        }
        $donefolders[] = $jobvalue['ftphost'] . '|' . $jobvalue['ftpuser'] . '|' . $jobvalue['ftpdir'];
    }
    return $files;
}
Example #7
0
function wp_export()
{
    global $WORKING, $STATIC;
    $WORKING['STEPTODO'] = 1;
    trigger_error(sprintf(__('%d. try for wordpress export to XML file...', 'backwpup'), $WORKING['WP_EXPORT']['STEP_TRY']), E_USER_NOTICE);
    need_free_memory(10485760);
    //10MB free memory
    if (function_exists('curl_exec')) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, substr($STATIC['JOBRUNURL'], 0, -11) . 'wp_export_generate.php');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, array('nonce' => $WORKING['NONCE'], 'type' => 'getxmlexport'));
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
        curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_USERAGENT, 'BackWPup');
        if (defined('CURLOPT_PROGRESSFUNCTION')) {
            curl_setopt($ch, CURLOPT_NOPROGRESS, false);
            curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'curl_progresscallback');
            curl_setopt($ch, CURLOPT_BUFFERSIZE, 1048576);
        }
        if (!empty($STATIC['CFG']['httpauthuser']) and !empty($STATIC['CFG']['httpauthpassword'])) {
            curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
            curl_setopt($ch, CURLOPT_USERPWD, $STATIC['CFG']['httpauthuser'] . ':' . backwpup_base64($STATIC['CFG']['httpauthpassword']));
        }
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
        $return = curl_exec($ch);
        $status = curl_getinfo($ch);
        if ($status['http_code'] >= 300 or $status['http_code'] < 200 or curl_errno($ch) > 0) {
            if (0 != curl_errno($ch)) {
                trigger_error(__('cURL:', 'backwpup') . ' (' . curl_errno($ch) . ') ' . curl_error($ch), E_USER_ERROR);
            } else {
                trigger_error(__('cURL:', 'backwpup') . ' (' . $status['http_code'] . ')  Invalid response.', E_USER_ERROR);
            }
        } else {
            file_put_contents($STATIC['TEMPDIR'] . preg_replace('/[^a-z0-9_\\-]/', '', strtolower($STATIC['WP']['BLOGNAME'])) . '.wordpress.' . date('Y-m-d') . '.xml', $return);
        }
        curl_close($ch);
    } else {
        //use fopen if no curl
        $urlParsed = parse_url(substr($STATIC['JOBRUNURL'], 0, -11) . 'wp_export_generate.php');
        if ($urlParsed['scheme'] == 'https') {
            $host = 'ssl://' . $urlParsed['host'];
            $port = !empty($urlParsed['port']) ? $urlParsed['port'] : 443;
        } else {
            $host = $urlParsed['host'];
            $port = !empty($urlParsed['port']) ? $urlParsed['port'] : 80;
        }
        $query = http_build_query(array('nonce' => $WORKING['NONCE'], 'type' => 'getxmlexport'));
        $path = (isset($urlParsed['path']) ? $urlParsed['path'] : '/') . (isset($urlParsed['query']) ? '?' . $urlParsed['query'] : '');
        $header = "POST " . $path . " HTTP/1.1\r\n";
        $header .= "Host: " . $urlParsed['host'] . "\r\n";
        $header .= "User-Agent: BackWPup\r\n";
        $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
        $header .= "Content-Length: " . strlen($query) . "\r\n";
        if (!empty($STATIC['CFG']['httpauthuser']) and !empty($STATIC['CFG']['httpauthpassword'])) {
            $header .= "Authorization: Basic " . base64_encode($STATIC['CFG']['httpauthuser'] . ':' . backwpup_base64($STATIC['CFG']['httpauthpassword'])) . "\r\n";
        }
        $header .= "Connection: Close\r\n\r\n";
        $header .= $query;
        $fp = fsockopen($host, $port, $errno, $errstr, 300);
        fwrite($fp, $header);
        $responseHeader = '';
        do {
            $responseHeader .= fread($fp, 1);
        } while (!preg_match('/\\r\\n\\r\\n$/', $responseHeader));
        while (!feof($fp)) {
            update_working_file();
            file_put_contents($STATIC['TEMPDIR'] . preg_replace('/[^a-z0-9_\\-]/', '', strtolower($STATIC['WP']['BLOGNAME'])) . '.wordpress.' . date('Y-m-d') . '.xml', fgets($fp, 256), FILE_APPEND);
        }
        fclose($fp);
    }
    //add XML file to backupfiles
    if (is_readable($STATIC['TEMPDIR'] . preg_replace('/[^a-z0-9_\\-]/', '', strtolower($STATIC['WP']['BLOGNAME'])) . '.wordpress.' . date('Y-m-d') . '.xml')) {
        $filestat = stat($STATIC['TEMPDIR'] . preg_replace('/[^a-z0-9_\\-]/', '', strtolower($STATIC['WP']['BLOGNAME'])) . '.wordpress.' . date('Y-m-d') . '.xml');
        trigger_error(sprintf(__('Add XML export "%1$s" to backup list with %2$s', 'backwpup'), preg_replace('/[^a-z0-9_\\-]/', '', strtolower($STATIC['WP']['BLOGNAME'])) . '.wordpress.' . date('Y-m-d') . '.xml', formatbytes($filestat['size'])), E_USER_NOTICE);
        $WORKING['ALLFILESIZE'] += $filestat['size'];
        add_file(array(array('FILE' => $STATIC['TEMPDIR'] . preg_replace('/[^a-z0-9_\\-]/', '', strtolower($STATIC['WP']['BLOGNAME'])) . '.wordpress.' . date('Y-m-d') . '.xml', 'OUTFILE' => preg_replace('/[^a-z0-9_\\-]/', '', strtolower($STATIC['WP']['BLOGNAME'])) . '.wordpress.' . date('Y-m-d') . '.xml', 'SIZE' => $filestat['size'], 'ATIME' => $filestat['atime'], 'MTIME' => $filestat['mtime'], 'CTIME' => $filestat['ctime'], 'UID' => $filestat['uid'], 'GID' => $filestat['gid'], 'MODE' => $filestat['mode'])));
    }
    $WORKING['STEPDONE'] = 1;
    $WORKING['STEPSDONE'][] = 'WP_EXPORT';
    //set done
}
Example #8
0
function dest_mail()
{
    global $WORKING, $STATIC;
    $WORKING['STEPTODO'] = filesize($STATIC['JOB']['backupdir'] . $STATIC['backupfile']);
    $WORKING['STEPDONE'] = 0;
    trigger_error(sprintf(__('%d. try to sending backup with mail...', 'backwpup'), $WORKING['DEST_MAIL']['STEP_TRY']), E_USER_NOTICE);
    //Create PHP Mailer
    require_once realpath($STATIC['WP']['ABSPATH'] . $STATIC['WP']['WPINC']) . '/class-phpmailer.php';
    $phpmailer = new PHPMailer();
    $phpmailer->CharSet = $STATIC['WP']['CHARSET'];
    //Setting den methode
    if ($STATIC['CFG']['mailmethod'] == "SMTP") {
        require_once realpath($STATIC['WP']['ABSPATH'] . $STATIC['WP']['WPINC']) . '/class-smtp.php';
        $phpmailer->Host = $STATIC['CFG']['mailhost'];
        $phpmailer->Port = $STATIC['CFG']['mailhostport'];
        $phpmailer->SMTPSecure = $STATIC['CFG']['mailsecure'];
        $phpmailer->Username = $STATIC['CFG']['mailuser'];
        $phpmailer->Password = backwpup_base64($STATIC['CFG']['mailpass']);
        if (!empty($STATIC['CFG']['mailuser']) and !empty($STATIC['CFG']['mailpass'])) {
            $phpmailer->SMTPAuth = true;
        }
        $phpmailer->IsSMTP();
        trigger_error(__('Send mail with SMTP', 'backwpup'), E_USER_NOTICE);
    } elseif ($STATIC['CFG']['mailmethod'] == "Sendmail") {
        $phpmailer->Sendmail = $STATIC['CFG']['mailsendmail'];
        $phpmailer->IsSendmail();
        trigger_error(__('Send mail with Sendmail', 'backwpup'), E_USER_NOTICE);
    } else {
        $phpmailer->IsMail();
        trigger_error(__('Send mail with PHP mail', 'backwpup'), E_USER_NOTICE);
    }
    trigger_error(__('Creating mail', 'backwpup'), E_USER_NOTICE);
    $phpmailer->From = $STATIC['CFG']['mailsndemail'];
    $phpmailer->FromName = $STATIC['CFG']['mailsndname'];
    $phpmailer->AddAddress($STATIC['JOB']['mailaddress']);
    $phpmailer->Subject = sprintf(__('BackWPup archive from %1$s: %2$s', 'backwpup'), date('Y/m/d @ H:i', $STATIC['JOB']['starttime'] + $STATIC['WP']['TIMEDIFF']), $STATIC['JOB']['name']);
    $phpmailer->IsHTML(false);
    $phpmailer->Body = sprintf(__('Backup archive: %s', 'backwpup'), $STATIC['backupfile']);
    //check file Size
    if (!empty($STATIC['JOB']['mailefilesize'])) {
        if (filesize($STATIC['JOB']['backupdir'] . $STATIC['backupfile']) > abs($STATIC['JOB']['mailefilesize'] * 1024 * 1024)) {
            trigger_error(__('Backup archive too big for sending by mail!', 'backwpup'), E_USER_ERROR);
            $WORKING['STEPDONE'] = 1;
            $WORKING['STEPSDONE'][] = 'DEST_MAIL';
            //set done
            return;
        }
    }
    trigger_error(__('Adding backup archive to mail', 'backwpup'), E_USER_NOTICE);
    need_free_memory(filesize($STATIC['JOB']['backupdir'] . $STATIC['backupfile']) * 6);
    $phpmailer->AddAttachment($STATIC['JOB']['backupdir'] . $STATIC['backupfile']);
    trigger_error(__('Send mail....', 'backwpup'), E_USER_NOTICE);
    if (false == $phpmailer->Send()) {
        trigger_error(sprintf(__('Error "%s" on sending mail!', 'backwpup'), $phpmailer->ErrorInfo), E_USER_ERROR);
    } else {
        $WORKING['STEPTODO'] = filesize($STATIC['JOB']['backupdir'] . $STATIC['backupfile']);
        trigger_error(__('Mail send!!!', 'backwpup'), E_USER_NOTICE);
    }
    $WORKING['STEPSDONE'][] = 'DEST_MAIL';
    //set done
}
Example #9
0
function backwpup_jobedit_metabox_destftp($jobvalue)
{
    ?>
  <b><?php 
    _e('Hostname:', 'backwpup');
    ?>
</b><br />
  <input name="ftphost" type="text" value="<?php 
    echo $jobvalue['ftphost'];
    ?>
" class="large-text" /><br />
  <b><?php 
    _e('Port:', 'backwpup');
    ?>
</b><br />
  <input name="ftphostport" type="text" value="<?php 
    echo $jobvalue['ftphostport'];
    ?>
" class="small-text" /><br />
  <b><?php 
    _e('Username:'******'backwpup');
    ?>
</b><br />
  <input name="ftpuser" type="text" value="<?php 
    echo $jobvalue['ftpuser'];
    ?>
" class="user large-text" autocomplete="off" /><br />
  <b><?php 
    _e('Password:'******'backwpup');
    ?>
</b><br />
  <input name="ftppass" type="password" value="<?php 
    echo backwpup_base64($jobvalue['ftppass']);
    ?>
" class="password large-text" autocomplete="off" /><br />
  <b><?php 
    _e('Folder on Server:', 'backwpup');
    ?>
</b><br />
  <input name="ftpdir" type="text" value="<?php 
    echo $jobvalue['ftpdir'];
    ?>
" class="large-text" /><br />
  <?php 
    if (!is_numeric($jobvalue['ftpmaxbackups'])) {
        $jobvalue['ftpmaxbackups'] = 0;
    }
    ?>
  <?php 
    _e('Max. backup files in FTP folder:', 'backwpup');
    ?>
 <input name="ftpmaxbackups" type="text" size="3" value="<?php 
    echo $jobvalue['ftpmaxbackups'];
    ?>
" class="small-text" /><span class="description"><?php 
    _e('(Oldest files will be deleted first.)', 'backwpup');
    ?>
</span><br />
  <input class="checkbox" value="1" type="checkbox" <?php 
    checked($jobvalue['ftpssl'], true);
    ?>
 name="ftpssl" /> <?php 
    _e('Use SSL-FTP Connection.', 'backwpup');
    ?>
<br />
  <input class="checkbox" value="1" type="checkbox" <?php 
    checked($jobvalue['ftppasv'], true);
    ?>
 name="ftppasv" /> <?php 
    _e('Use FTP Passive mode.', 'backwpup');
    ?>
<br />
  <?php 
}
Example #10
0
function backwpup_cron()
{
    if (is_file(backwpup_get_temp() . '.running')) {
        $cfg = get_option('backwpup');
        $revtime = time() - 600;
        //10 min no progress.
        $infile = backwpup_get_working_file();
        $httpauthheader = '';
        if (!empty($cfg['httpauthuser']) and !empty($cfg['httpauthpassword'])) {
            $httpauthheader = array('Authorization' => 'Basic ' . base64_encode($cfg['httpauthuser'] . ':' . backwpup_base64($cfg['httpauthpassword'])));
        }
        if (!empty($infile['timestamp']) and $infile['timestamp'] < $revtime) {
            wp_remote_post(BACKWPUP_PLUGIN_BASEURL . '/job/job_run.php', array('timeout' => 3, 'blocking' => false, 'sslverify' => false, 'headers' => $httpauthheader, 'body' => array('nonce' => $infile['WORKING']['NONCE'], 'type' => 'restarttime'), 'user-agent' => 'BackWPup'));
        }
    } else {
        $jobs = get_option('backwpup_jobs');
        if (!empty($jobs)) {
            foreach ($jobs as $jobid => $jobvalue) {
                if (!isset($jobvalue['activated']) or !$jobvalue['activated']) {
                    continue;
                }
                if ($jobvalue['cronnextrun'] <= current_time('timestamp')) {
                    require_once dirname(__FILE__) . '/job/job_start.php';
                    backwpup_jobstart($jobid, true);
                }
            }
        }
    }
}
Example #11
0
function dest_ftp()
{
    global $WORKING, $STATIC;
    if (empty($STATIC['JOB']['ftphost']) or empty($STATIC['JOB']['ftpuser']) or empty($STATIC['JOB']['ftppass'])) {
        $WORKING['STEPSDONE'][] = 'DEST_FTP';
        //set done
        return;
    }
    $WORKING['STEPTODO'] = filesize($STATIC['JOB']['backupdir'] . $STATIC['backupfile']);
    trigger_error(sprintf(__('%d. try to sending backup file to a FTP Server...', 'backwpup'), $WORKING['DEST_FTP']['STEP_TRY']), E_USER_NOTICE);
    if ($STATIC['JOB']['ftpssl']) {
        //make SSL FTP connection
        if (function_exists('ftp_ssl_connect')) {
            $ftp_conn_id = ftp_ssl_connect($STATIC['JOB']['ftphost'], $STATIC['JOB']['ftphostport'], 10);
            if ($ftp_conn_id) {
                trigger_error(sprintf(__('Connected by SSL-FTP to Server: %s', 'backwpup'), $STATIC['JOB']['ftphost'] . ':' . $STATIC['JOB']['ftphostport']), E_USER_NOTICE);
            } else {
                trigger_error(sprintf(__('Can not connect by SSL-FTP to Server: %s', 'backwpup'), $STATIC['JOB']['ftphost'] . ':' . $STATIC['JOB']['ftphostport']), E_USER_ERROR);
                return false;
            }
        } else {
            trigger_error(__('PHP function to connect with SSL-FTP to server not exists!', 'backwpup'), E_USER_ERROR);
            return false;
        }
    } else {
        //make normal FTP connection if SSL not work
        $ftp_conn_id = ftp_connect($STATIC['JOB']['ftphost'], $STATIC['JOB']['ftphostport'], 10);
        if ($ftp_conn_id) {
            trigger_error(sprintf(__('Connected to FTP server: %s', 'backwpup'), $STATIC['JOB']['ftphost'] . ':' . $STATIC['JOB']['ftphostport']), E_USER_NOTICE);
        } else {
            trigger_error(sprintf(__('Can not connect to FTP server: %s', 'backwpup'), $STATIC['JOB']['ftphost'] . ':' . $STATIC['JOB']['ftphostport']), E_USER_ERROR);
            return false;
        }
    }
    //FTP Login
    $loginok = false;
    trigger_error(sprintf(__('FTP Client command: %s', 'backwpup'), ' USER ' . $STATIC['JOB']['ftpuser']), E_USER_NOTICE);
    if ($loginok = ftp_login($ftp_conn_id, $STATIC['JOB']['ftpuser'], backwpup_base64($STATIC['JOB']['ftppass']))) {
        trigger_error(sprintf(__('FTP Server reply: %s', 'backwpup'), ' User ' . $STATIC['JOB']['ftpuser'] . ' logged in.'), E_USER_NOTICE);
    } else {
        //if PHP ftp login don't work use raw login
        $return = ftp_raw($ftp_conn_id, 'USER ' . $STATIC['JOB']['ftpuser']);
        trigger_error(sprintf(__('FTP Server reply: %s', 'backwpup'), $return[0]), E_USER_NOTICE);
        if (substr(trim($return[0]), 0, 3) <= 400) {
            trigger_error(sprintf(__('FTP Client command: %s', 'backwpup'), ' PASS *******'), E_USER_NOTICE);
            $return = ftp_raw($ftp_conn_id, 'PASS ' . backwpup_base64($STATIC['JOB']['ftppass']));
            trigger_error(sprintf(__('FTP Server reply: %s', 'backwpup'), $return[0]), E_USER_NOTICE);
            if (substr(trim($return[0]), 0, 3) <= 400) {
                $loginok = true;
            }
        }
    }
    if (!$loginok) {
        return false;
    }
    //SYSTYPE
    trigger_error(sprintf(__('FTP Client command: %s', 'backwpup'), ' SYST'), E_USER_NOTICE);
    $systype = ftp_systype($ftp_conn_id);
    if ($systype) {
        trigger_error(sprintf(__('FTP Server reply: %s', 'backwpup'), $systype), E_USER_NOTICE);
    } else {
        trigger_error(sprintf(__('FTP Server reply: %s', 'backwpup'), __('Error getting SYSTYPE', 'backwpup')), E_USER_ERROR);
    }
    //test ftp dir and create it f not exists
    if ($STATIC['JOB']['ftpdir'] != '/' && $STATIC['JOB']['ftpdir'] != '') {
        $ftpdirs = explode("/", trim($STATIC['JOB']['ftpdir'], '/'));
        foreach ($ftpdirs as $ftpdir) {
            if (empty($ftpdir)) {
                continue;
            }
            if (!@ftp_chdir($ftp_conn_id, $ftpdir)) {
                if (@ftp_mkdir($ftp_conn_id, $ftpdir)) {
                    trigger_error(sprintf(__('FTP folder "%s" created!', 'backwpup'), $ftpdir), E_USER_NOTICE);
                    ftp_chdir($ftp_conn_id, $ftpdir);
                } else {
                    trigger_error(sprintf(__('FTP folder "%s" can not created!', 'backwpup'), $ftpdir), E_USER_ERROR);
                    return false;
                }
            }
        }
    }
    // Get the current working directory
    $currentftpdir = rtrim(ftp_pwd($ftp_conn_id), '/') . '/';
    trigger_error(sprintf(__('FTP current folder is: %s', 'backwpup'), $currentftpdir), E_USER_NOTICE);
    //delete file on ftp if new try
    if ($WORKING['STEPDONE'] == 0) {
        @ftp_delete($ftp_conn_id, $currentftpdir . $STATIC['backupfile']);
    }
    //PASV
    trigger_error(sprintf(__('FTP Client command: %s', 'backwpup'), ' PASV'), E_USER_NOTICE);
    if ($STATIC['JOB']['ftppasv']) {
        if (ftp_pasv($ftp_conn_id, true)) {
            trigger_error(sprintf(__('FTP Server reply: %s', 'backwpup'), __('Entering Passive Mode', 'backwpup')), E_USER_NOTICE);
        } else {
            trigger_error(sprintf(__('FTP Server reply: %s', 'backwpup'), __('Can not Entering Passive Mode', 'backwpup')), E_USER_WARNING);
        }
    } else {
        if (ftp_pasv($ftp_conn_id, false)) {
            trigger_error(sprintf(__('FTP Server reply: %s', 'backwpup'), __('Entering Normal Mode', 'backwpup')), E_USER_NOTICE);
        } else {
            trigger_error(sprintf(__('FTP Server reply: %s', 'backwpup'), __('Can not Entering Normal Mode', 'backwpup')), E_USER_WARNING);
        }
    }
    if ($WORKING['STEPDONE'] < $WORKING['STEPTODO']) {
        trigger_error(__('Upload to FTP now started ... ', 'backwpup'), E_USER_NOTICE);
        $fp = fopen($STATIC['JOB']['backupdir'] . $STATIC['backupfile'], 'r');
        $ret = ftp_nb_fput($ftp_conn_id, $currentftpdir . $STATIC['backupfile'], $fp, FTP_BINARY, $WORKING['STEPDONE']);
        while ($ret == FTP_MOREDATA) {
            $WORKING['STEPDONE'] = ftell($fp);
            update_working_file();
            $ret = ftp_nb_continue($ftp_conn_id);
        }
        if ($ret != FTP_FINISHED) {
            trigger_error(__('Can not transfer backup to FTP server!', 'backwpup'), E_USER_ERROR);
            return false;
        } else {
            $WORKING['STEPDONE'] = filesize($STATIC['JOB']['backupdir'] . $STATIC['backupfile']);
            trigger_error(sprintf(__('Backup transferred to FTP server: %s', 'backwpup'), $currentftpdir . $STATIC['backupfile']), E_USER_NOTICE);
            $STATIC['JOB']['lastbackupdownloadurl'] = "ftp://" . $STATIC['JOB']['ftpuser'] . ":" . backwpup_base64($STATIC['JOB']['ftppass']) . "@" . $STATIC['JOB']['ftphost'] . ':' . $STATIC['JOB']['ftphostport'] . $currentftpdir . $STATIC['backupfile'];
            $WORKING['STEPSDONE'][] = 'DEST_FTP';
            //set done
        }
        fclose($fp);
    }
    if ($STATIC['JOB']['ftpmaxbackups'] > 0) {
        //Delete old backups
        $backupfilelist = array();
        if ($filelist = ftp_nlist($ftp_conn_id, $currentftpdir)) {
            foreach ($filelist as $files) {
                if ($STATIC['JOB']['fileprefix'] == substr(basename($files), 0, strlen($STATIC['JOB']['fileprefix'])) and $STATIC['JOB']['fileformart'] == substr(basename($files), -strlen($STATIC['JOB']['fileformart']))) {
                    $backupfilelist[] = basename($files);
                }
            }
            if (sizeof($backupfilelist) > 0) {
                rsort($backupfilelist);
                $numdeltefiles = 0;
                for ($i = $STATIC['JOB']['ftpmaxbackups']; $i < sizeof($backupfilelist); $i++) {
                    if (ftp_delete($ftp_conn_id, $currentftpdir . $backupfilelist[$i])) {
                        //delte files on ftp
                        $numdeltefiles++;
                    } else {
                        trigger_error(sprintf(__('Can not delete "%s" on FTP server!', 'backwpup'), $currentftpdir . $backupfilelist[$i]), E_USER_ERROR);
                    }
                }
                if ($numdeltefiles > 0) {
                    trigger_error(sprintf(_n('One file deleted on FTP Server', '%d files deleted on FTP Server', $numdeltefiles, 'backwpup'), $numdeltefiles), E_USER_NOTICE);
                }
            }
        }
    }
    ftp_close($ftp_conn_id);
    $WORKING['STEPDONE']++;
}