function __construct() { global $itsec_globals; //make sure the log file info is there or generate it. This should only affect beta users. if ( ! isset( $itsec_globals['settings']['log_info'] ) ) { $itsec_globals['settings']['log_info'] = substr( sanitize_title( get_bloginfo( 'name' ) ), 0, 20 ) . '-' . ITSEC_Lib::get_random( mt_rand( 0, 10 ) ); update_site_option( 'itsec_global', $itsec_globals['settings'] ); } //Make sure the logs directory was created if ( ! is_dir( $itsec_globals['ithemes_log_dir'] ) ) { @mkdir( trailingslashit( $itsec_globals['ithemes_dir'] ) . 'logs' ); } //don't create a log file if we don't need it. if ( isset( $itsec_globals['settings']['log_type'] ) && $itsec_globals['settings']['log_type'] !== 0 ) { $this->log_file = $itsec_globals['ithemes_log_dir'] . '/event-log-' . $itsec_globals['settings']['log_info'] . '.log'; $this->start_log(); //create a log file if we don't have one } $this->logger_modules = array(); //array to hold information on modules using this feature $this->logger_displays = array(); //array to hold metabox information $this->module_path = ITSEC_Lib::get_module_path( __FILE__ ); add_action( 'plugins_loaded', array( $this, 'register_modules' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'admin_script' ) ); //enqueue scripts for admin page //Run database cleanup daily with cron if ( ! wp_next_scheduled( 'itsec_purge_logs' ) ) { wp_schedule_event( time(), 'daily', 'itsec_purge_logs' ); } add_action( 'itsec_purge_logs', array( $this, 'purge_logs' ) ); if ( is_admin() ) { require( trailingslashit( $itsec_globals['plugin_dir'] ) . 'core/lib/class-itsec-wp-list-table.php' ); //used for generating log tables add_action( 'itsec_add_admin_meta_boxes', array( $this, 'add_admin_meta_boxes' ) ); //add log meta boxes } if ( isset( $_POST['itsec_clear_logs'] ) && $_POST['itsec_clear_logs'] === 'clear_logs' ) { global $itsec_clear_all_logs; $itsec_clear_all_logs = true; add_action( 'plugins_loaded', array( $this, 'purge_logs' ) ); } }
/** * Requires a unique nicename on profile update or activate. * * @since 4.0 * * @return void */ public function force_unique_nicename(&$errors, $update, &$user) { $display_name = isset($user->display_name) ? $user->display_name : ITSEC_Lib::get_random(14); if (!empty($user->nickname)) { if ($user->nickname == $user->user_login) { $errors->add('user_error', __('Your Nickname must be different than your login name. Please choose a different Nickname.', 'it-l10n-better-wp-security')); } else { $user->user_nicename = sanitize_title($user->nickname, $display_name); } } elseif (!empty($user->first_name) && !empty($user->last_name)) { $full_name = $user->first_name . ' ' . $user->last_name; $user->nickname = $full_name; $user->user_nicename = sanitize_title($full_name, $display_name); } else { $errors->add('user_error', __('A Nickname is required. Please choose a nickname or fill out your first and last name.', 'it-l10n-better-wp-security')); } }
/** * Execute activation. * * @since 4.0 * * @param boolean $upgrade true if the plugin is updating * * @return void */ private function activate_execute() { global $itsec_globals, $itsec_files; //if this is multisite make sure they're network activating or die if (defined('ITSEC_DO_ACTIVATION') && ITSEC_DO_ACTIVATION == true && is_multisite() && !strpos($_SERVER['REQUEST_URI'], 'wp-admin/network/plugins.php')) { die(__('<strong>ERROR</strong>: You must activate this plugin from the network dashboard.', 'it-l10n-better-wp-security')); } //make sure directories are present and they are not remotely accessible if (!is_dir($itsec_globals['ithemes_dir'])) { @mkdir($itsec_globals['ithemes_dir']); $handle = @fopen($itsec_globals['ithemes_dir'] . '/.htaccess', 'w+'); @fwrite($handle, 'Deny from all'); @fclose($handle); } if (!is_dir($itsec_globals['ithemes_log_dir'])) { @mkdir($itsec_globals['ithemes_log_dir']); $handle = @fopen($itsec_globals['ithemes_log_dir'] . '/.htaccess', 'w+'); @fwrite($handle, 'Deny from all'); @fclose($handle); } if (!is_dir($itsec_globals['ithemes_backup_dir'])) { @mkdir($itsec_globals['ithemes_backup_dir']); $handle = @fopen($itsec_globals['ithemes_backup_dir'] . '/.htaccess', 'w+'); @fwrite($handle, 'Deny from all'); @fclose($handle); } if (($site_data = get_site_option('itsec_data')) === false) { add_site_option('itsec_data', array(), false); } if (get_site_option('itsec_initials') === false) { add_site_option('itsec_initials', array(), false); } if (get_site_option('itsec_api_nag') === false) { //show the nag to activate an API key add_site_option('itsec_api_nag', true, false); } $options = get_site_option('itsec_global'); if ($options === false || isset($options['log_info']) && sizeof($options) <= 2) { $this->defaults['log_info'] = substr(sanitize_title(get_bloginfo('name')), 0, 20) . '-' . ITSEC_Lib::get_random(mt_rand(0, 10)); $itsec_globals['settings'] = $this->defaults; update_site_option('itsec_global', $this->defaults); } //load utility functions if (!class_exists('ITSEC_Lib')) { require trailingslashit($itsec_globals['plugin_dir']) . 'core/class-itsec-lib.php'; } ITSEC_Lib::create_database_tables(); $this->do_modules(); }
/** * 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; $current_time = current_time('timestamp'); //save file $file = 'backup-' . substr(sanitize_title(get_bloginfo('name')), 0, 20) . '-' . $current_time . '-' . ITSEC_Lib::get_random(mt_rand(5, 10)); if (!is_dir($itsec_globals['ithemes_backup_dir'])) { @mkdir(trailingslashit($itsec_globals['ithemes_dir']) . 'backups'); } $handle = @fopen($itsec_globals['ithemes_backup_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($itsec_globals['ithemes_backup_dir'] . '/' . $file . '.zip'); if (0 != $zip->create($itsec_globals['ithemes_backup_dir'] . '/' . $file . '.sql')) { //delete .sql and keep zip @unlink($itsec_globals['ithemes_backup_dir'] . '/' . $file . '.sql'); $fileext = '.zip'; } } else { $fileext = '.sql'; } if (2 !== $this->settings['method'] || true === $one_time) { $option = get_site_option('itsec_global'); $attachment = array($itsec_globals['ithemes_backup_dir'] . '/' . $file . $fileext); $body = __('Attached is the backup file for the database powering', 'it-l10n-better-wp-security') . ' ' . get_option('siteurl') . __(' taken', 'it-l10n-better-wp-security') . ' ' . date('l, F jS, Y \\a\\t g:i a', $itsec_globals['current_time']); //Setup the remainder of the email $recipients = $option['backup_email']; $subject = __('Site Database Backup', 'it-l10n-better-wp-security') . ' ' . date('l, F jS, Y \\a\\t g:i a', $itsec_globals['current_time']); $subject = apply_filters('itsec_backup_email_subject', $subject); $headers = 'From: ' . get_bloginfo('name') . ' <' . get_option('admin_email') . '>' . "\r\n"; $mail_success = false; //Use HTML Content type add_filter('wp_mail_content_type', array($this, 'set_html_content_type')); //Send emails to all recipients foreach ($recipients as $recipient) { if (is_email(trim($recipient))) { if (defined('ITSEC_DEBUG') && true === ITSEC_DEBUG) { $body .= '<p>' . __('Debug info (source page): ' . esc_url($_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"])) . '</p>'; } $mail_success = wp_mail(trim($recipient), $subject, '<html>' . $body . '</html>', $headers, $attachment); } } //Remove HTML Content type remove_filter('wp_mail_content_type', array($this, 'set_html_content_type')); } if (1 === $this->settings['method']) { @unlink($itsec_globals['ithemes_backup_dir'] . '/' . $file . $fileext); } else { $retain = isset($this->settings['retain']) ? absint($this->settings['retain']) : 0; //delete extra files if (0 < $retain) { $files = scandir($itsec_globals['ithemes_backup_dir'], 1); $count = 0; if (is_array($files) && 0 < count($files)) { foreach ($files as $file) { if (strstr($file, 'backup')) { if ($count >= $retain) { @unlink(trailingslashit($itsec_globals['ithemes_backup_dir']) . $file); } $count++; } } } } } if (false === $one_time) { $this->settings['last_run'] = $itsec_globals['current_time_gmt']; update_site_option('itsec_backup', $this->settings); } switch ($this->settings['method']) { case 0: if (false === $mail_success) { $status = array('status' => __('Error', 'it-l10n-better-wp-security'), 'details' => __('saved locally but email to backup recipients could not be sent.', 'it-l10n-better-wp-security')); } else { $status = array('status' => __('Success', 'it-l10n-better-wp-security'), 'details' => __('emailed to backup recipients and saved locally', 'it-l10n-better-wp-security')); } break; case 1: if (false === $mail_success) { $status = array('status' => __('Error', 'it-l10n-better-wp-security'), 'details' => __('email to backup recipients could not be sent.', 'it-l10n-better-wp-security')); } else { $status = array('status' => __('Success', 'it-l10n-better-wp-security'), 'details' => __('emailed to backup recipients', 'it-l10n-better-wp-security')); } break; default: $status = array('status' => __('Success', 'it-l10n-better-wp-security'), 'details' => __('saved locally', 'it-l10n-better-wp-security')); break; } $itsec_logger->log_event('backup', 3, array($status)); }
/** * Generate Salts * * Generates a random string using alpha-numeric and special characters with a length of 64 characters. * * @since 4.6.0 * * @access private * * @return void */ private function generate_salts() { $salts = ''; for ($i = 0; 1 > $i; $i++) { $salts .= ITSEC_Lib::get_random(64, false, true) . ' '; } return $salts; }
/** * Ajax generate new key. * * Generates a new two-factor key via AJAX. * * @since 1.2.0 * * @return void */ public function wp_ajax_itsec_two_factor_profile_ajax() { if ( ! wp_verify_nonce( sanitize_text_field( $_POST['nonce'] ), 'itsec_two_factor_profile' ) ) { die( __( 'Security error!', 'it-l10n-ithemes-security-pro' ) ); } die( ITSEC_Lib::get_random( 16, true ) ); }