/**
  * Executes backup function.
  *
  * Handles the execution of database backups.
  *
  * @since 4.0.0
  *
  * @param bool $one_time whether this is a one-time backup
  *
  * @return void
  */
 private function execute_backup($one_time = false)
 {
     global $wpdb, $itsec_globals, $itsec_logger;
     //get all of the tables
     if (isset($this->settings['all_sites']) && true === $this->settings['all_sites']) {
         $tables = $wpdb->get_results('SHOW TABLES', ARRAY_N);
         //retrieve a list of all tables in the DB
     } else {
         $tables = $wpdb->get_results('SHOW TABLES LIKE "' . $wpdb->base_prefix . '%"', ARRAY_N);
         //retrieve a list of all tables for this WordPress installation
     }
     $return = '';
     //cycle through each table
     foreach ($tables as $table) {
         $num_fields = sizeof($wpdb->get_results('DESCRIBE `' . $table[0] . '`;'));
         $return .= 'DROP TABLE IF EXISTS `' . $table[0] . '`;';
         $row2 = $wpdb->get_row('SHOW CREATE TABLE `' . $table[0] . '`;', ARRAY_N);
         $return .= PHP_EOL . PHP_EOL . $row2[1] . ";" . PHP_EOL . PHP_EOL;
         if (!in_array(substr($table[0], strlen($wpdb->prefix)), $this->settings['exclude'])) {
             $result = $wpdb->get_results('SELECT * FROM `' . $table[0] . '`;', ARRAY_N);
             foreach ($result as $row) {
                 $return .= 'INSERT INTO `' . $table[0] . '` VALUES(';
                 for ($j = 0; $j < $num_fields; $j++) {
                     $row[$j] = addslashes($row[$j]);
                     $row[$j] = preg_replace('#' . PHP_EOL . '#', "\n", $row[$j]);
                     if (isset($row[$j])) {
                         $return .= '"' . $row[$j] . '"';
                     } else {
                         $return .= '""';
                     }
                     if ($j < $num_fields - 1) {
                         $return .= ',';
                     }
                 }
                 $return .= ");" . PHP_EOL;
             }
         }
         $return .= PHP_EOL . PHP_EOL;
     }
     $return .= PHP_EOL . PHP_EOL;
     //save file
     $file = 'backup-' . substr(sanitize_title(get_bloginfo('name')), 0, 20) . '-' . current_time('Ymd-His') . '-' . wp_generate_password(30, false);
     require_once ITSEC_Core::get_core_dir() . 'lib/class-itsec-lib-directory.php';
     $dir = $this->settings['location'];
     ITSEC_Lib_Directory::create($dir);
     $fileext = '.sql';
     $handle = @fopen($dir . '/' . $file . '.sql', 'w+');
     @fwrite($handle, $return);
     @fclose($handle);
     //zip the file
     if (true === $this->settings['zip']) {
         if (!class_exists('PclZip')) {
             require ABSPATH . 'wp-admin/includes/class-pclzip.php';
         }
         $zip = new PclZip($dir . '/' . $file . '.zip');
         if (0 != $zip->create($dir . '/' . $file . '.sql', PCLZIP_OPT_REMOVE_PATH, $dir)) {
             //delete .sql and keep zip
             @unlink($dir . '/' . $file . '.sql');
             $fileext = '.zip';
         }
     }
     if (2 !== $this->settings['method'] || true === $one_time) {
         require_once ITSEC_Core::get_core_dir() . 'lib/class-itsec-mailer.php';
         $mail = new ITSEC_Mail();
         $mail->add_header(esc_html__('Database Backup', 'better-wp-security'), sprintf(wp_kses(__('Site Database Backup for <b>%s</b>', 'better-wp-security'), array('b' => array())), date_i18n(get_option('date_format'))));
         $mail->add_info_box(esc_html__('Attached is the database backup file for your site.', 'better-wp-security'), 'attachment');
         $mail->add_section_heading(esc_html__('Website', 'better-wp-security'));
         $mail->add_text(esc_html(network_home_url()));
         $mail->add_section_heading(esc_html__('Date', 'better-wp-security'));
         $mail->add_text(esc_html(date_i18n(get_option('date_format'))));
         $mail->add_footer();
         $raw_recipients = ITSEC_Modules::get_setting('global', 'backup_email');
         $recipients = array();
         foreach ($raw_recipients as $recipient) {
             $recipient = trim($recipient);
             if (is_email($recipient)) {
                 $recipients[] = $recipient;
             }
         }
         $subject = sprintf(esc_html__('[%s] Database Backup', 'better-wp-security'), esc_url(network_home_url()));
         $subject = apply_filters('itsec_backup_email_subject', $subject);
         $attachment = array("{$dir}/{$file}{$fileext}");
         $mail_success = $mail->send($recipients, $subject, $attachment);
     }
     if (1 === $this->settings['method']) {
         @unlink($dir . '/' . $file . $fileext);
     } else {
         $retain = isset($this->settings['retain']) ? absint($this->settings['retain']) : 0;
         //delete extra files
         if (0 < $retain) {
             $files = scandir($dir, 1);
             $count = 0;
             if (is_array($files) && 0 < count($files)) {
                 foreach ($files as $file) {
                     if (strstr($file, 'backup')) {
                         if ($count >= $retain) {
                             @unlink(trailingslashit($dir) . $file);
                         }
                         $count++;
                     }
                 }
             }
         }
     }
     if (false === $one_time) {
         ITSEC_Modules::set_setting('backup', 'last_run', ITSEC_Core::get_current_time_gmt());
     }
     switch ($this->settings['method']) {
         case 0:
             if (false === $mail_success) {
                 $status = array('status' => __('Error', 'better-wp-security'), 'details' => __('saved locally but email to backup recipients could not be sent.', 'better-wp-security'));
             } else {
                 $status = array('status' => __('Success', 'better-wp-security'), 'details' => __('emailed to backup recipients and saved locally', 'better-wp-security'));
             }
             break;
         case 1:
             if (false === $mail_success) {
                 $status = array('status' => __('Error', 'better-wp-security'), 'details' => __('email to backup recipients could not be sent.', 'better-wp-security'));
             } else {
                 $status = array('status' => __('Success', 'better-wp-security'), 'details' => __('emailed to backup recipients', 'better-wp-security'));
             }
             break;
         default:
             $status = array('status' => __('Success', 'better-wp-security'), 'details' => __('saved locally', 'better-wp-security'));
             break;
     }
     $itsec_logger->log_event('backup', 3, array($status));
 }
 /**
  * Send the daily digest email.
  *
  * @since 2.6.0
  *
  * @return
  */
 public function send_daily_digest()
 {
     global $itsec_lockout;
     $send_email = false;
     require_once ITSEC_Core::get_core_dir() . 'lib/class-itsec-mailer.php';
     $mail = new ITSEC_Mail();
     $mail->add_header(esc_html__('Daily Security Digest', 'better-wp-security'), sprintf(wp_kses(__('Your Daily Security Digest for <b>%s</b>', 'better-wp-security'), array('b' => array())), date_i18n(get_option('date_format'))));
     $mail->add_info_box(sprintf(wp_kses(__('The following is a summary of security related activity on your site: <b>%s</b>', 'better-wp-security'), array('b' => array())), get_option('siteurl')));
     $mail->add_section_heading(esc_html__('Lockouts', 'better-wp-security'), 'lock');
     $user_count = sizeof($itsec_lockout->get_lockouts('user', true));
     $host_count = sizeof($itsec_lockout->get_lockouts('host', true));
     if ($host_count > 0 || $user_count > 0) {
         $mail->add_lockouts_summary($user_count, $host_count);
         $send_email = true;
     } else {
         $mail->add_text(esc_html__('No lockouts since the last email check.', 'better-wp-security'));
     }
     if (is_array($this->queue) && !empty($this->queue['messages']) && is_array($this->queue['messages'])) {
         if (in_array('file-change', $this->queue['messages'])) {
             $mail->add_section_heading(esc_html__('File Changes', 'better-wp-security'), 'folder');
             $mail->add_text(esc_html__('File changes detected on the site.', 'better-wp-security'));
             $send_email = true;
         }
         $messages = array();
         foreach ($this->queue['messages'] as $message) {
             if ('file-change' === $message) {
                 continue;
             }
             $messages[] = $message;
         }
         if (!empty($messages)) {
             $mail->add_section_heading(esc_html__('Messages', 'better-wp-security'), 'message');
             foreach ($messages as $message) {
                 $mail->add_text($message);
             }
             $send_email = true;
         }
     }
     if (!$send_email) {
         return;
     }
     $mail->add_details_box(sprintf(wp_kses(__('For more details, <a href="%s"><b>visit your security logs</b></a>', 'better-wp-security'), array('a' => array('href' => array()), 'b' => array())), ITSEC_Core::get_logs_page_url()));
     $mail->add_divider();
     $mail->add_large_text(esc_html__('Is your site as secure as it could be?', 'better-wp-security'));
     $mail->add_text(esc_html__('Ensure your site is using recommended settings and features with a security check.', 'better-wp-security'));
     $mail->add_button(esc_html__('Run a Security Check ✓', 'better-wp-security'), ITSEC_Core::get_security_check_page_url());
     if (defined('ITSEC_DEBUG') && true === ITSEC_DEBUG) {
         $mail->add_text(sprintf(esc_html__('Debug info (source page): %s', 'better-wp-security'), esc_url($_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"])));
     }
     $mail->add_footer();
     $raw_recipients = ITSEC_Modules::get_setting('global', 'notification_email');
     $recipients = array();
     foreach ($raw_recipients as $recipient) {
         $recipient = trim($recipient);
         if (is_email($recipient)) {
             $recipients[] = $recipient;
         }
     }
     $this->queue = array('last_sent' => ITSEC_Core::get_current_time_gmt(), 'messages' => array());
     update_site_option('itsec_message_queue', $this->queue);
     $subject = sprintf(esc_html__('[%s] Daily Security Digest', 'better-wp-security'), esc_url(get_option('siteurl')));
     return $mail->send($recipients, $subject);
 }