Exemplo n.º 1
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'];
}
function backwpup_get_job_vars($jobid = '', $jobnewsettings = '')
{
    global $wpdb;
    //get job data
    $jobs = get_option('backwpup_jobs');
    //load jobs
    if (!empty($jobid)) {
        if (isset($jobs[$jobid])) {
            $jobsettings = $jobs[$jobid];
        }
        $jobsettings['jobid'] = $jobid;
    } else {
        if (empty($jobsettings['jobid'])) {
            //generate jobid if not exists
            $heighestid = 0;
            if (is_array($jobs)) {
                foreach ($jobs as $jobkey => $jobvalue) {
                    if ($jobkey > $heighestid) {
                        $heighestid = $jobkey;
                    }
                }
            }
            $jobsettings['jobid'] = $heighestid + 1;
        }
    }
    unset($jobs);
    unset($jobid);
    if (!empty($jobnewsettings) && is_array($jobnewsettings)) {
        //overwrite with new settings
        $jobsettings = array_merge($jobsettings, $jobnewsettings);
    }
    //check job type
    if (!isset($jobsettings['type']) or !is_string($jobsettings['type'])) {
        $jobsettings['type'] = 'DB+FILE';
    }
    $todo = explode('+', strtoupper($jobsettings['type']));
    foreach ($todo as $key => $value) {
        if (!in_array($value, backwpup_backup_types())) {
            unset($todo[$key]);
        }
    }
    $jobsettings['type'] = implode('+', $todo);
    if (empty($jobsettings['type'])) {
        $jobsettings['type'] = 'DB+FILE';
    }
    if (empty($jobsettings['name']) or !is_string($jobsettings['name'])) {
        $jobsettings['name'] = __('New', 'backwpup');
    }
    if (!isset($jobsettings['activated']) or !is_bool($jobsettings['activated'])) {
        $jobsettings['activated'] = false;
    }
    if (!isset($jobsettings['cronselect']) and !isset($jobsettings['cron'])) {
        $jobsettings['cronselect'] = 'basic';
    } elseif (!isset($jobsettings['cronselect']) and isset($jobsettings['cron'])) {
        $jobsettings['cronselect'] = 'advanced';
    }
    if ($jobsettings['cronselect'] != 'advanced' and $jobsettings['cronselect'] != 'basic') {
        $jobsettings['cronselect'] = 'advanced';
    }
    if (!isset($jobsettings['cron']) or !is_string($jobsettings['cron'])) {
        $jobsettings['cron'] = '0 3 * * *';
    }
    if (!isset($jobsettings['cronnextrun']) or !is_numeric($jobsettings['cronnextrun'])) {
        $jobsettings['cronnextrun'] = backwpup_cron_next($jobsettings['cron']);
    }
    if (!isset($jobsettings['mailaddresslog']) or !is_string($jobsettings['mailaddresslog'])) {
        $jobsettings['mailaddresslog'] = get_option('admin_email');
    }
    if (!isset($jobsettings['mailerroronly']) or !is_bool($jobsettings['mailerroronly'])) {
        $jobsettings['mailerroronly'] = true;
    }
    //Tables to backup (old)
    if (isset($jobsettings['dbtables']) and is_array($jobsettings['dbtables'])) {
        $tables = $wpdb->get_col('SHOW TABLES FROM `' . DB_NAME . '`');
        foreach ($tables as $table) {
            if (!in_array($table, $jobsettings['dbtables'])) {
                $jobsettings['dbexclude'][] = $table;
            }
        }
    }
    //don not backup tables
    if (!isset($jobsettings['dbexclude']) or !is_array($jobsettings['dbexclude'])) {
        $jobsettings['dbexclude'] = array();
        $tables = $wpdb->get_col('SHOW TABLES FROM `' . DB_NAME . '`');
        foreach ($tables as $table) {
            if (substr($table, 0, strlen($wpdb->prefix)) != $wpdb->prefix) {
                $jobsettings['dbexclude'][] = $table;
            }
        }
    }
    if (!isset($jobsettings['dbshortinsert']) or !is_bool($jobsettings['dbshortinsert'])) {
        $jobsettings['dbshortinsert'] = false;
    }
    if (!isset($jobsettings['maintenance']) or !is_bool($jobsettings['maintenance'])) {
        $jobsettings['maintenance'] = false;
    }
    if (!isset($jobsettings['fileexclude']) or !is_string($jobsettings['fileexclude'])) {
        $jobsettings['fileexclude'] = '';
    }
    $fileexclude = explode(',', $jobsettings['fileexclude']);
    foreach ($fileexclude as $key => $value) {
        $fileexclude[$key] = str_replace('//', '/', str_replace('\\', '/', trim($value)));
        if (empty($fileexclude[$key])) {
            unset($fileexclude[$key]);
        }
    }
    sort($fileexclude);
    $jobsettings['fileexclude'] = implode(',', $fileexclude);
    if (!isset($jobsettings['dirinclude']) or !is_string($jobsettings['dirinclude'])) {
        $jobsettings['dirinclude'] = '';
    }
    $dirinclude = explode(',', $jobsettings['dirinclude']);
    foreach ($dirinclude as $key => $value) {
        $dirinclude[$key] = trailingslashit(str_replace('//', '/', str_replace('\\', '/', trim($value))));
        if ($dirinclude[$key] == '/' or empty($dirinclude[$key]) or !is_dir($dirinclude[$key])) {
            unset($dirinclude[$key]);
        }
    }
    sort($dirinclude);
    $jobsettings['dirinclude'] = implode(',', $dirinclude);
    if (!isset($jobsettings['backuproot']) or !is_bool($jobsettings['backuproot'])) {
        $jobsettings['backuproot'] = true;
    }
    if (!isset($jobsettings['backupcontent']) or !is_bool($jobsettings['backupcontent'])) {
        $jobsettings['backupcontent'] = true;
    }
    if (!isset($jobsettings['backupplugins']) or !is_bool($jobsettings['backupplugins'])) {
        $jobsettings['backupplugins'] = true;
    }
    if (!isset($jobsettings['backupthemes']) or !is_bool($jobsettings['backupthemes'])) {
        $jobsettings['backupthemes'] = true;
    }
    if (!isset($jobsettings['backupuploads']) or !is_bool($jobsettings['backupuploads'])) {
        $jobsettings['backupuploads'] = true;
    }
    if (!isset($jobsettings['backuprootexcludedirs']) or !is_array($jobsettings['backuprootexcludedirs'])) {
        $jobsettings['backuprootexcludedirs'] = array();
    }
    foreach ($jobsettings['backuprootexcludedirs'] as $key => $value) {
        $jobsettings['backuprootexcludedirs'][$key] = str_replace('//', '/', str_replace('\\', '/', trim($value)));
        if (empty($jobsettings['backuprootexcludedirs'][$key]) or $jobsettings['backuprootexcludedirs'][$key] == '/' or !is_dir($jobsettings['backuprootexcludedirs'][$key])) {
            unset($jobsettings['backuprootexcludedirs'][$key]);
        }
    }
    sort($jobsettings['backuprootexcludedirs']);
    if (!isset($jobsettings['backupcontentexcludedirs']) or !is_array($jobsettings['backupcontentexcludedirs'])) {
        $jobsettings['backupcontentexcludedirs'] = array();
    }
    foreach ($jobsettings['backupcontentexcludedirs'] as $key => $value) {
        $jobsettings['backupcontentexcludedirs'][$key] = str_replace('//', '/', str_replace('\\', '/', trim($value)));
        if (empty($jobsettings['backupcontentexcludedirs'][$key]) or $jobsettings['backupcontentexcludedirs'][$key] == '/' or !is_dir($jobsettings['backupcontentexcludedirs'][$key])) {
            unset($jobsettings['backupcontentexcludedirs'][$key]);
        }
    }
    sort($jobsettings['backupcontentexcludedirs']);
    if (!isset($jobsettings['backuppluginsexcludedirs']) or !is_array($jobsettings['backuppluginsexcludedirs'])) {
        $jobsettings['backuppluginsexcludedirs'] = array();
    }
    foreach ($jobsettings['backuppluginsexcludedirs'] as $key => $value) {
        $jobsettings['backuppluginsexcludedirs'][$key] = str_replace('//', '/', str_replace('\\', '/', trim($value)));
        if (empty($jobsettings['backuppluginsexcludedirs'][$key]) or $jobsettings['backuppluginsexcludedirs'][$key] == '/' or !is_dir($jobsettings['backuppluginsexcludedirs'][$key])) {
            unset($jobsettings['backuppluginsexcludedirs'][$key]);
        }
    }
    sort($jobsettings['backuppluginsexcludedirs']);
    if (!isset($jobsettings['backupthemesexcludedirs']) or !is_array($jobsettings['backupthemesexcludedirs'])) {
        $jobsettings['backupthemesexcludedirs'] = array();
    }
    foreach ($jobsettings['backupthemesexcludedirs'] as $key => $value) {
        $jobsettings['backupthemesexcludedirs'][$key] = str_replace('//', '/', str_replace('\\', '/', trim($value)));
        if (empty($jobsettings['backupthemesexcludedirs'][$key]) or $jobsettings['backupthemesexcludedirs'][$key] == '/' or !is_dir($jobsettings['backupthemesexcludedirs'][$key])) {
            unset($jobsettings['backupthemesexcludedirs'][$key]);
        }
    }
    sort($jobsettings['backupthemesexcludedirs']);
    if (!isset($jobsettings['backupuploadsexcludedirs']) or !is_array($jobsettings['backupuploadsexcludedirs'])) {
        $jobsettings['backupuploadsexcludedirs'] = array();
    }
    foreach ($jobsettings['backupuploadsexcludedirs'] as $key => $value) {
        $jobsettings['backupuploadsexcludedirs'][$key] = str_replace('//', '/', str_replace('\\', '/', trim($value)));
        if (empty($jobsettings['backupuploadsexcludedirs'][$key]) or $jobsettings['backupuploadsexcludedirs'][$key] == '/' or !is_dir($jobsettings['backupuploadsexcludedirs'][$key])) {
            unset($jobsettings['backupuploadsexcludedirs'][$key]);
        }
    }
    sort($jobsettings['backupuploadsexcludedirs']);
    $fileformarts = array('.zip', '.tar.gz', '.tar.bz2', '.tar');
    if (!isset($jobsettings['fileformart']) or !in_array($jobsettings['fileformart'], $fileformarts)) {
        $jobsettings['fileformart'] = '.zip';
    }
    if (!isset($jobsettings['fileprefix']) or !is_string($jobsettings['fileprefix'])) {
        $jobsettings['fileprefix'] = 'backwpup_' . $jobsettings['jobid'] . '_';
    }
    if (!isset($jobsettings['mailefilesize']) or !is_float($jobsettings['mailefilesize'])) {
        $jobsettings['mailefilesize'] = 0;
    }
    if (!isset($jobsettings['backupdir'])) {
        $jobsettings['backupdir'] = '';
    }
    if (substr($jobsettings['backupdir'], 0, 1) != '/' and substr($jobsettings['backupdir'], 1, 1) != ':' and !empty($jobsettings['backupdir'])) {
        //add abspath if not absolute
        $jobsettings['backupdir'] = rtrim(str_replace('\\', '/', ABSPATH), '/') . '/' . $jobsettings['backupdir'];
    }
    $jobsettings['backupdir'] = trailingslashit(str_replace('//', '/', str_replace('\\', '/', trim($jobsettings['backupdir']))));
    if ($jobsettings['backupdir'] == '/') {
        $jobsettings['backupdir'] = '';
    }
    if (!isset($jobsettings['maxbackups']) or !is_int($jobsettings['maxbackups'])) {
        $jobsettings['maxbackups'] = 0;
    }
    if (!empty($jobsettings['ftphost']) and false !== strpos($jobsettings['ftphost'], ':')) {
        list($jobsettings['ftphost'], $jobsettings['ftphostport']) = explode(':', $jobsettings['ftphost'], 2);
    }
    if (!isset($jobsettings['ftphost']) or !is_string($jobsettings['ftphost'])) {
        $jobsettings['ftphost'] = '';
    }
    if (!isset($jobsettings['ftphostport']) or !is_int($jobsettings['ftphostport'])) {
        $jobsettings['ftphostport'] = 21;
    }
    if (!isset($jobsettings['ftpuser']) or !is_string($jobsettings['ftpuser'])) {
        $jobsettings['ftpuser'] = '';
    }
    if (!isset($jobsettings['ftppass']) or !is_string($jobsettings['ftppass'])) {
        $jobsettings['ftppass'] = '';
    }
    if (!isset($jobsettings['ftpdir']) or !is_string($jobsettings['ftpdir']) or $jobsettings['ftpdir'] == '/') {
        $jobsettings['ftpdir'] = '';
    }
    $jobsettings['ftpdir'] = trailingslashit(str_replace('//', '/', str_replace('\\', '/', trim($jobsettings['ftpdir']))));
    if (substr($jobsettings['ftpdir'], 0, 1) != '/') {
        $jobsettings['ftpdir'] = '/' . $jobsettings['ftpdir'];
    }
    if (!isset($jobsettings['ftpmaxbackups']) or !is_int($jobsettings['ftpmaxbackups'])) {
        $jobsettings['ftpmaxbackups'] = 0;
    }
    if (!isset($jobsettings['ftppasv']) or !is_bool($jobsettings['ftppasv'])) {
        $jobsettings['ftppasv'] = true;
    }
    if (!isset($jobsettings['ftpssl']) or !is_bool($jobsettings['ftpssl'])) {
        $jobsettings['ftpssl'] = false;
    }
    if (!isset($jobsettings['awsAccessKey']) or !is_string($jobsettings['awsAccessKey'])) {
        $jobsettings['awsAccessKey'] = '';
    }
    if (!isset($jobsettings['awsSecretKey']) or !is_string($jobsettings['awsSecretKey'])) {
        $jobsettings['awsSecretKey'] = '';
    }
    if (!isset($jobsettings['awsrrs']) or !is_bool($jobsettings['awsrrs'])) {
        $jobsettings['awsrrs'] = false;
    }
    if (!isset($jobsettings['awsBucket']) or !is_string($jobsettings['awsBucket'])) {
        $jobsettings['awsBucket'] = '';
    }
    if (!isset($jobsettings['awsdir']) or !is_string($jobsettings['awsdir']) or $jobsettings['awsdir'] == '/') {
        $jobsettings['awsdir'] = '';
    }
    $jobsettings['awsdir'] = trailingslashit(str_replace('//', '/', str_replace('\\', '/', trim($jobsettings['awsdir']))));
    if (substr($jobsettings['awsdir'], 0, 1) == '/') {
        $jobsettings['awsdir'] = substr($jobsettings['awsdir'], 1);
    }
    if (!isset($jobsettings['awsmaxbackups']) or !is_int($jobsettings['awsmaxbackups'])) {
        $jobsettings['awsmaxbackups'] = 0;
    }
    if (!isset($jobsettings['GStorageAccessKey']) or !is_string($jobsettings['GStorageAccessKey'])) {
        $jobsettings['GStorageAccessKey'] = '';
    }
    if (!isset($jobsettings['GStorageSecret']) or !is_string($jobsettings['GStorageSecret'])) {
        $jobsettings['GStorageSecret'] = '';
    }
    if (!isset($jobsettings['GStorageBucket']) or !is_string($jobsettings['GStorageBucket'])) {
        $jobsettings['GStorageBucket'] = '';
    }
    if (!isset($jobsettings['GStoragedir']) or !is_string($jobsettings['GStoragedir']) or $jobsettings['GStoragedir'] == '/') {
        $jobsettings['GStoragedir'] = '';
    }
    $jobsettings['GStoragedir'] = trailingslashit(str_replace('//', '/', str_replace('\\', '/', trim($jobsettings['GStoragedir']))));
    if (substr($jobsettings['GStoragedir'], 0, 1) == '/') {
        $jobsettings['GStoragedir'] = substr($jobsettings['GStoragedir'], 1);
    }
    if (!isset($jobsettings['GStoragemaxbackups']) or !is_int($jobsettings['GStoragemaxbackups'])) {
        $jobsettings['GStoragemaxbackups'] = 0;
    }
    if (!isset($jobsettings['msazureHost']) or !is_string($jobsettings['msazureHost'])) {
        $jobsettings['msazureHost'] = 'blob.core.windows.net';
    }
    if (!isset($jobsettings['msazureAccName']) or !is_string($jobsettings['msazureAccName'])) {
        $jobsettings['msazureAccName'] = '';
    }
    if (!isset($jobsettings['msazureKey']) or !is_string($jobsettings['msazureKey'])) {
        $jobsettings['msazureKey'] = '';
    }
    if (!isset($jobsettings['msazureContainer']) or !is_string($jobsettings['msazureContainer'])) {
        $jobsettings['msazureContainer'] = '';
    }
    if (!isset($jobsettings['msazuredir']) or !is_string($jobsettings['msazuredir']) or $jobsettings['msazuredir'] == '/') {
        $jobsettings['msazuredir'] = '';
    }
    $jobsettings['msazuredir'] = trailingslashit(str_replace('//', '/', str_replace('\\', '/', trim($jobsettings['msazuredir']))));
    if (substr($jobsettings['msazuredir'], 0, 1) == '/') {
        $jobsettings['msazuredir'] = substr($jobsettings['msazuredir'], 1);
    }
    if (!isset($jobsettings['msazuremaxbackups']) or !is_int($jobsettings['msazuremaxbackups'])) {
        $jobsettings['msazuremaxbackups'] = 0;
    }
    if (!isset($jobsettings['rscUsername']) or !is_string($jobsettings['rscUsername'])) {
        $jobsettings['rscUsername'] = '';
    }
    if (!isset($jobsettings['rscAPIKey']) or !is_string($jobsettings['rscAPIKey'])) {
        $jobsettings['rscAPIKey'] = '';
    }
    if (!isset($jobsettings['rscContainer']) or !is_string($jobsettings['rscContainer'])) {
        $jobsettings['rscContainer'] = '';
    }
    if (!isset($jobsettings['rscdir']) or !is_string($jobsettings['rscdir']) or $jobsettings['rscdir'] == '/') {
        $jobsettings['rscdir'] = '';
    }
    $jobsettings['rscdir'] = trailingslashit(str_replace('//', '/', str_replace('\\', '/', trim($jobsettings['rscdir']))));
    if (substr($jobsettings['rscdir'], 0, 1) == '/') {
        $jobsettings['rscdir'] = substr($jobsettings['rscdir'], 1);
    }
    if (!isset($jobsettings['rscmaxbackups']) or !is_int($jobsettings['rscmaxbackups'])) {
        $jobsettings['rscmaxbackups'] = 0;
    }
    if (!isset($jobsettings['dropetoken']) or !is_string($jobsettings['dropetoken'])) {
        $jobsettings['dropetoken'] = '';
    }
    if (!isset($jobsettings['dropesecret']) or !is_string($jobsettings['dropesecret'])) {
        $jobsettings['dropesecret'] = '';
    }
    if (!isset($jobsettings['dropedir']) or !is_string($jobsettings['dropedir']) or $jobsettings['dropedir'] == '/') {
        $jobsettings['dropedir'] = '';
    }
    $jobsettings['dropedir'] = trailingslashit(str_replace('//', '/', str_replace('\\', '/', trim($jobsettings['dropedir']))));
    if (substr($jobsettings['dropedir'], 0, 1) == '/') {
        $jobsettings['dropedir'] = substr($jobsettings['dropedir'], 1);
    }
    if (!isset($jobsettings['droperoot']) or $jobsettings['droperoot'] != 'dropbox' and $jobsettings['droperoot'] != 'sandbox') {
        $jobsettings['droperoot'] = 'dropbox';
    }
    if (!isset($jobsettings['dropemaxbackups']) or !is_int($jobsettings['dropemaxbackups'])) {
        $jobsettings['dropemaxbackups'] = 0;
    }
    if (!isset($jobsettings['sugaruser']) or !is_string($jobsettings['sugaruser'])) {
        $jobsettings['sugaruser'] = '';
    }
    if (!isset($jobsettings['sugarpass']) or !is_string($jobsettings['sugarpass'])) {
        $jobsettings['sugarpass'] = '';
    }
    if (!isset($jobsettings['sugarroot']) or !is_string($jobsettings['sugarroot'])) {
        $jobsettings['sugarroot'] = '';
    }
    if (!isset($jobsettings['sugardir']) or !is_string($jobsettings['sugardir']) or $jobsettings['sugardir'] == '/') {
        $jobsettings['sugardir'] = '';
    }
    $jobsettings['sugardir'] = trailingslashit(str_replace('//', '/', str_replace('\\', '/', trim($jobsettings['sugardir']))));
    if (substr($jobsettings['sugardir'], 0, 1) == '/') {
        $jobsettings['sugardir'] = substr($jobsettings['sugardir'], 1);
    }
    if (!isset($jobsettings['sugarmaxbackups']) or !is_int($jobsettings['sugarmaxbackups'])) {
        $jobsettings['sugarmaxbackups'] = 0;
    }
    if (!isset($jobsettings['mailaddress']) or !is_string($jobsettings['mailaddress'])) {
        $jobsettings['mailaddress'] = '';
    }
    //unset old not nedded vars
    unset($jobsettings['scheduletime']);
    unset($jobsettings['scheduleintervaltype']);
    unset($jobsettings['scheduleintervalteimes']);
    unset($jobsettings['scheduleinterval']);
    unset($jobsettings['dropemail']);
    unset($jobsettings['dropepass']);
    unset($jobsettings['dbtables']);
    unset($jobsettings['dropesignmethod']);
    return $jobsettings;
}
Exemplo n.º 3
0
     $jobvalues['cron'] = implode(",", $_POST['cronminutes']) . ' ' . implode(",", $_POST['cronhours']) . ' ' . implode(",", $_POST['cronmday']) . ' ' . implode(",", $_POST['cronmon']) . ' ' . implode(",", $_POST['cronwday']);
 } else {
     if ($_POST['cronbtype'] == 'mon') {
         $jobvalues['cron'] = $_POST['moncronminutes'] . ' ' . $_POST['moncronhours'] . ' ' . $_POST['moncronmday'] . ' * *';
     }
     if ($_POST['cronbtype'] == 'week') {
         $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;
     }
Exemplo n.º 4
0
function backwpup_get_cron_text($args = '')
{
    if (is_array($args)) {
        extract($args);
        $ajax = false;
    } else {
        check_ajax_referer('backwpupeditjob_ajax_nonce');
        if (!current_user_can(BACKWPUP_USER_CAPABILITY)) {
            die('-1');
        }
        if (empty($_POST['cronminutes']) or $_POST['cronminutes'][0] == '*') {
            if (!empty($_POST['cronminutes'][1])) {
                $_POST['cronminutes'] = array('*/' . $_POST['cronminutes'][1]);
            } else {
                $_POST['cronminutes'] = array('*');
            }
        }
        if (empty($_POST['cronhours']) or $_POST['cronhours'][0] == '*') {
            if (!empty($_POST['cronhours'][1])) {
                $_POST['cronhours'] = array('*/' . $_POST['cronhours'][1]);
            } else {
                $_POST['cronhours'] = array('*');
            }
        }
        if (empty($_POST['cronmday']) or $_POST['cronmday'][0] == '*') {
            if (!empty($_POST['cronmday'][1])) {
                $_POST['cronmday'] = array('*/' . $_POST['cronmday'][1]);
            } else {
                $_POST['cronmday'] = array('*');
            }
        }
        if (empty($_POST['cronmon']) or $_POST['cronmon'][0] == '*') {
            if (!empty($_POST['cronmon'][1])) {
                $_POST['cronmon'] = array('*/' . $_POST['cronmon'][1]);
            } else {
                $_POST['cronmon'] = array('*');
            }
        }
        if (empty($_POST['cronwday']) or $_POST['cronwday'][0] == '*') {
            if (!empty($_POST['cronwday'][1])) {
                $_POST['cronwday'] = array('*/' . $_POST['cronwday'][1]);
            } else {
                $_POST['cronwday'] = array('*');
            }
        }
        $cronstamp = implode(",", $_POST['cronminutes']) . ' ' . implode(",", $_POST['cronhours']) . ' ' . implode(",", $_POST['cronmday']) . ' ' . implode(",", $_POST['cronmon']) . ' ' . implode(",", $_POST['cronwday']);
        $ajax = true;
    }
    echo '<div id="cron-text">';
    _e('Working as <a href="http://wikipedia.org/wiki/Cron" target="_blank">Cron</a> job schedule:', 'backwpup');
    echo ' <i><b><nobr>' . $cronstamp . '</nobr></b></i><br />';
    list($cronstr['minutes'], $cronstr['hours'], $cronstr['mday'], $cronstr['mon'], $cronstr['wday']) = explode(' ', $cronstamp, 5);
    if (false !== strpos($cronstr['minutes'], '*/') or $cronstr['minutes'] == '*') {
        $repeatmins = str_replace('*/', '', $cronstr['minutes']);
        if ($repeatmins == '*' or empty($repeatmins)) {
            $repeatmins = 5;
        }
        echo '<span style="color:red;">' . str_replace('%d', $repeatmins, __('ATTENTION: Job runs every %d mins.!!!', 'backwpup')) . '</span><br />';
    }
    if (false !== strpos($cronstr['hours'], '*/') or $cronstr['hours'] == '*') {
        $repeathouer = str_replace('*/', '', $cronstr['hours']);
        if ($repeathouer == '*' or empty($repeathouer)) {
            $repeathouer = 1;
        }
        echo '<span style="color:red;">' . str_replace('%d', $repeathouer, __('ATTENTION: Job runs every %d hours.!!!', 'backwpup')) . '</span><br />';
    }
    $nextrun = backwpup_cron_next($cronstamp);
    if (2147483647 == $nextrun) {
        echo '<span style="color:red;">' . __('ATTENTION: Can\'t calculate cron!!!', 'backwpup') . '</span><br />';
    } else {
        _e('Next runtime:', 'backwpup');
        echo ' <b>' . date_i18n('D, j M Y, H:i', backwpup_cron_next($cronstamp)) . '</b>';
    }
    echo "</div>";
    if ($ajax) {
        die;
    } else {
        return;
    }
}