/** * Public function to get lock and call backup. * * Attempts to get a lock to prevent concurrant backups and calls the backup function itself. * * @since 4.0.0 * * @param boolean $one_time whether this is a one time backup * * @return mixed false on error or nothing */ public function do_backup($one_time = false) { ITSEC_Lib::set_minimum_memory_limit('256M'); $itsec_files = ITSEC_Core::get_itsec_files(); if ($itsec_files->get_file_lock('backup')) { $this->execute_backup($one_time); $itsec_files->release_file_lock('backup'); switch ($this->settings['method']) { case 0: return __('Backup complete. The backup was sent to the selected email recipients and was saved locally.', 'better-wp-security'); case 1: return __('Backup complete. The backup was sent to the selected email recipients.', 'better-wp-security'); default: return __('Backup complete. The backup was saved locally.', 'better-wp-security'); } } else { return new WP_Error('itsec-backup-do-backup-already-running', __('Unable to create a backup at this time since a backup is currently being created. If you wish to create an additional backup, please wait a few minutes before trying again.', 'better-wp-security')); } }
/** * Public function to get lock and call backup. * * Attempts to get a lock to prevent concurrant backups and calls the backup function itself. * * @since 4.0.0 * * @param boolean $one_time whether this is a one time backup * * @return mixed false on error or nothing */ public function do_backup($one_time = false) { global $itsec_files; ITSEC_Lib::set_minimum_memory_limit('128M'); if ($itsec_files->get_file_lock('backup')) { $this->execute_backup($one_time); $itsec_files->release_file_lock('backup'); if (true === $one_time) { switch ($this->settings['method']) { case 0: $details = __('emailed to backup recipients and saved locally.', 'it-l10n-better-wp-security'); break; case 1: $details = __('emailed to backup recipients.', 'it-l10n-better-wp-security'); break; default: $details = __('saved locally.', 'it-l10n-better-wp-security'); break; } $type = 'updated'; $message = __('Backup Completed and ' . $details, 'it-l10n-better-wp-security'); } $success = true; } else { if (true === $one_time) { $type = 'error'; $message = __('Something went wrong with your backup. It looks like another process might already be trying to backup your database. Please try again in a few minutes. If the problem persists please contact support.', 'it-l10n-better-wp-security'); } $success = false; } if (true === $one_time) { if (is_multisite()) { $error_handler = new WP_Error(); $error_handler->add($type, $message); $this->core->show_network_admin_notice($error_handler); } else { add_settings_error('itsec', esc_attr('settings_updated'), $message, $type); } } return $success; }
/** * Executes file checking * * @param bool $scheduled_call [optional] is this an automatic check * * @return mixed **/ public function execute_file_check($scheduled_call = true) { global $itsec_files, $itsec_logger, $itsec_globals; if ($this->running === false) { $this->running = true; $send_email = true; ITSEC_Lib::set_minimum_memory_limit('128M'); if ($itsec_files->get_file_lock('file_change', 300)) { //make sure it isn't already running define('ITSEC_DOING_FILE_CHECK', true); //figure out what chunk we're on if (isset($this->settings['split']) && $this->settings['split'] === true) { if (isset($this->settings['last_chunk']) && $this->settings['last_chunk'] !== false && $this->settings['last_chunk'] < 6) { $chunk = $this->settings['last_chunk'] + 1; } else { $chunk = 0; } } else { $chunk = false; } if ($chunk !== false) { $db_field = 'itsec_local_file_list_' . $chunk; } else { $db_field = 'itsec_local_file_list'; } //set base memory $memory_used = @memory_get_peak_usage(); $logged_files = get_site_option($db_field); //if there are no old files old file list is an empty array if ($logged_files === false) { $send_email = false; $logged_files = array(); if (is_multisite()) { add_site_option($db_field, $logged_files); } else { add_option($db_field, $logged_files, '', 'no'); } } $current_files = $this->scan_files('', $scheduled_call, $chunk); //scan current files $itsec_files->release_file_lock('file_change'); $files_added = @array_diff_assoc($current_files, $logged_files); //files added $files_removed = @array_diff_assoc($logged_files, $current_files); //files deleted $current_minus_added = @array_diff_key($current_files, $files_added); //remove all added files from current filelist $logged_minus_deleted = @array_diff_key($logged_files, $files_removed); //remove all deleted files from old file list $files_changed = array(); //array of changed files //compare file hashes and mod dates foreach ($current_minus_added as $current_file => $current_attr) { if (array_key_exists($current_file, $logged_minus_deleted)) { //if attributes differ added to changed files array if (isset($current_attr['mod_date']) && strcmp($current_attr['mod_date'], $logged_minus_deleted[$current_file]['mod_date']) != 0 || strcmp($current_attr['d'], $logged_minus_deleted[$current_file]['d']) != 0 || (isset($current_attr['hash']) && strcmp($current_attr['hash'], $logged_minus_deleted[$current_file]['hash']) != 0 || strcmp($current_attr['h'], $logged_minus_deleted[$current_file]['h']) != 0)) { $remote_check = apply_filters('itsec_process_changed_file', true, $current_file, $current_attr['h']); //hook to run actions on a changed file at time of discovery if ($remote_check === true) { //don't list the file if it matches the WordPress.org hash $files_changed[$current_file]['h'] = isset($current_attr['hash']) ? $current_attr['hash'] : $current_attr['h']; $files_changed[$current_file]['d'] = isset($current_attr['mod_date']) ? $current_attr['mod_date'] : $current_attr['d']; } } } } //get count of changes $files_added_count = sizeof($files_added); $files_deleted_count = sizeof($files_removed); $files_changed_count = sizeof($files_changed); if ($files_added_count > 0) { $files_added = apply_filters('itsec_process_added_files', $files_added); //hook to run actions on all files added $files_added_count = sizeof($files_added); } if ($files_deleted_count > 0) { do_action('itsec_process_removed_files', $files_removed); //hook to run actions on all files removed } //create single array of all changes $full_change_list = array('added' => $files_added, 'removed' => $files_removed, 'changed' => $files_changed); update_site_option($db_field, $current_files); //Cleanup variables when we're done with them unset($files_added); unset($files_removed); unset($files_changed); unset($current_files); $this->settings['last_run'] = $itsec_globals['current_time']; $this->settings['last_chunk'] = $chunk; update_site_option('itsec_file_change', $this->settings); //get new max memory $check_memory = @memory_get_peak_usage(); if ($check_memory > $memory_used) { $memory_used = $check_memory - $memory_used; } $full_change_list['memory'] = round($memory_used / 1000000, 2); $itsec_logger->log_event('file_change', 8, $full_change_list); if ($send_email === true && $scheduled_call !== false && isset($this->settings['email']) && $this->settings['email'] === true && ($files_added_count > 0 || $files_changed_count > 0 || $files_deleted_count > 0)) { $email_details = array($files_added_count, $files_deleted_count, $files_changed_count, $full_change_list); $this->send_notification_email($email_details); } if (function_exists('get_current_screen') && (!isset(get_current_screen()->id) || strpos(get_current_screen()->id, 'security_page_toplevel_page_itsec_logs') === false) && isset($this->settings['notify_admin']) && $this->settings['notify_admin'] === true) { add_site_option('itsec_file_change_warning', true); } $itsec_files->release_file_lock('file_change'); if ($files_added_count > 0 || $files_changed_count > 0 || $files_deleted_count > 0) { $this->running = false; return true; } else { $this->running = false; return false; } } $this->running = false; return -1; } }