get_results( "SELECT `id`, `country_code` FROM {$wpdb->base_prefix}bws_list_countries" ); if ( ! empty( $countries_data ) ) { foreach ( $countries_data as $data ) { $array_compare[ $data->id ] = $data->country_code; } $first_import = false; } else { $first_import = true; } $current_file_country = fopen( $file_country_name, 'r' ); if ( $current_file_country ) { while ( ( $line = fgetcsv( $current_file_country, 150, ',' ) ) !== false ) { /* The first line of the file is skipped, there is overhead */ if ( ! (int) $line[0] ) { continue; } else { $line[4] = preg_replace( '#\W#', '', $line[4] ); $country_array[ $line[0] ] = array( 'short' => $line[2], 'name' => $line[4], ); } } } else { if ( false == $noscript ) { echo 0; die(); } else { return 0; } } $current_file = fopen( $upload_dir . '/file_' . $file_number . '.csv', 'a' ); if ( $current_file ) { $first_line = 0; $i = 0; while ( ( $line = fgetcsv( $handle, 100, ',' ) ) !== false ) { if ( ! (int) $line[0] ) { continue; } else { if ( $first_line < 2 ) { $first_line++; } else { if ( ! empty( $line ) ) { /* Start - forming a string to write to the file */ $country_short_name = ''; $country_full_name = ''; $country_id = ''; if ( is_array( $country_array ) ) { if ( $country_array[ $line[0] ] ) { $line[4] = preg_replace( '#\W#', '', $line[4] ); $country_short_name = $line[2]; $country_full_name = $line[4]; if ( ! empty( $country_full_name ) ) { if ( in_array( $country_short_name, $array_compare ) ) { $country_id = empty( $array_compare ) ? 1 : array_search( $country_short_name, $array_compare ); if ( ! in_array( $country_short_name, $country_in_file ) ) { $update_ignore[] = "( {$country_id}, '{$country_short_name}', '{$country_full_name}' )"; $country_in_file[] = $country_short_name; } } else { if ( $first_import ) { $country_id = empty( $array_compare ) ? 1 : max( array_keys( $array_compare ) ) + 1; $insert_ignore[] = "( {$country_id}, '{$country_short_name}', '{$country_full_name}' )"; } else { /* add record about new country */ $wpdb->insert( "{$wpdb->base_prefix}bws_list_countries", array( 'country_code' => $country_short_name, 'country_name' => $country_full_name, ) ); $country_id = $wpdb->insert_id; } $array_compare[ $country_id ] = $country_short_name; $country_in_file[] = $country_short_name; } } } } $start_ip = $line[0]; $end_ip = $line[1]; $long_start_ip = long2ip( $start_ip ); $long_end_ip = long2ip( $end_ip ); $file_line = "'{$long_start_ip}', '{$long_end_ip}','{$start_ip}','{$end_ip}','{$country_id}'"; /* Finish - forming a string to write to the file */ fwrite( $current_file, $file_line . PHP_EOL ); $i++; if ( $i == $lines_count ) { fclose( $current_file ); if ( ! empty( $update_ignore ) ) { vstrsnln_insert_countries( $update_ignore ); } if ( ! empty( $insert_ignore ) ) { vstrsnln_insert_countries( $insert_ignore ); } $file_number++; $current_file = fopen( $upload_dir . '/file_' . $file_number . '.csv', 'a' ); if ( $current_file ) { $i = 0; } else { if ( false == $noscript ) { echo 0; die(); } else { return 0; } } } } } } } fclose( $current_file ); fclose( $handle ); if ( ! empty( $update_ignore ) ) { vstrsnln_insert_countries( $update_ignore ); } if ( ! empty( $insert_ignore ) ) { vstrsnln_insert_countries( $insert_ignore ); } if ( false == $noscript ) { echo absint( $file_number ); die(); } else { return $file_number; } } else { if ( false == $noscript ) { echo 0; die(); } else { return 0; } } } else { if ( false == $noscript ) { echo 0; die(); } else { return 0; } } } } } if ( ! function_exists( 'vstrsnln_insert_countries' ) ) { /** * Insert countries in DB * * @param array $data Data for insert. */ function vstrsnln_insert_countries( $data ) { global $wpdb; $wpdb->query( "INSERT INTO `{$wpdb->base_prefix}bws_list_countries` ( `id`, `country_code`, `country_name` ) VALUES " . implode( ',', $data ) . 'ON DUPLICATE KEY UPDATE `country_name` = VALUES(`country_name`);' ); } } if ( ! function_exists( 'vstrsnln_insert_rows' ) ) { /** * Fill in the table of country * * @param bool $number_file Number for file. * @param bool $noscript Flag for noscript. */ function vstrsnln_insert_rows( $number_file = false, $noscript = false ) { global $wpdb, $wp_filesystem; if ( false == $noscript ) { check_ajax_referer( 'bws_plugin', 'vstrsnln_ajax_nonce_field' ); } $vstrsnln_access_type = get_filesystem_method(); if ( 'direct' == $vstrsnln_access_type ) { $vstrsnln_creds = request_filesystem_credentials( site_url() . '/wp-admin/', '', false, false, array() ); if ( ! WP_Filesystem( $vstrsnln_creds ) ) { if ( false == $number_file ) { echo false; } else { return false; } } if ( false == $number_file ) { if ( isset( $_POST['count'] ) && file_exists( plugin_dir_path( dirname( __FILE__ ) ) . 'geolite-country/file_' . absint( $_POST['count'] ) . '.csv' ) ) { $filename = plugin_dir_path( dirname( __FILE__ ) ) . 'geolite-country/file_' . absint( $_POST['count'] ) . '.csv'; $data_array = $wp_filesystem->get_contents_array( $filename ); if ( false !== $data_array && is_array( $data_array ) && ! empty( $data_array ) ) { $sql = 'INSERT IGNORE INTO `' . $wpdb->base_prefix . 'bws_list_ip` ( `ip_from`, `ip_to`, `ip_from_int`, `ip_to_int`, `country_id` ) VALUES ( ' . implode( ' ) , ( ', $data_array ) . ' );'; $result = $wpdb->query( $sql ); unlink( $filename ); echo esc_html( $result ); } } } else { if ( $number_file > 0 && file_exists( plugin_dir_path( dirname( __FILE__ ) ) . 'geolite-country/file_' . $number_file . '.csv' ) ) { $filename = plugin_dir_path( dirname( __FILE__ ) ) . 'geolite-country/file_' . $number_file . '.csv'; $data_array = $wp_filesystem->get_contents_array( $filename ); if ( false !== $data_array && is_array( $data_array ) && ! empty( $data_array ) ) { $sql = 'INSERT IGNORE INTO `' . $wpdb->base_prefix . 'bws_list_ip` ( `ip_from`, `ip_to`, `ip_from_int`, `ip_to_int`, `country_id`) VALUES ( ' . implode( ' ) , ( ', $data_array ) . ' );'; $result = $wpdb->query( $sql ); unlink( $filename ); return $result; } } } } /* This is required to terminate immediately and return a proper response */ wp_die(); } } if ( ! function_exists( 'vstrsnln_import_noscript' ) ) { /** * Importing countries with javascript disabled * * @param bool $count_files Count all files. * @return bool $result Flag for result. */ function vstrsnln_import_noscript( $count_files ) { for ( $count = 1; $count <= $count_files; $count++ ) { $result = vstrsnln_insert_rows( $count, true ); } if ( empty( $result ) ) { $result = true; } return $result; } } if ( ! function_exists( 'vstrsnln_form_import_country' ) ) { /** * The conclusion to the settings page of information on imports of tabl * * @param string $page_url Page url. */ function vstrsnln_form_import_country( $page_url ) { global $wpdb; /* Table exists */ if ( $wpdb->get_var( "SHOW TABLES LIKE '" . $wpdb->base_prefix . "bws_list_ip'" ) == $wpdb->base_prefix . 'bws_list_ip' ) { $vstrsnln_table_full = $wpdb->get_var( ' SELECT `id` FROM `' . $wpdb->base_prefix . 'bws_list_ip` LIMIT 1' ); } else { $vstrsnln_table_full = 0; } ?>