__( 'IP2nation', 'newstatpress' ), 'update' => __( 'Update', 'newstatpress' ), 'export' => __( 'Export', 'newstatpress' ), 'optimize' => __( 'Optimize', 'newstatpress' ), 'repair' => __( 'Repair', 'newstatpress' ), 'remove' => __( 'Remove', 'newstatpress' ), 'info' => __( 'Informations', 'newstatpress' ), ); $default_tab = 'IP2nation'; print "

" . esc_html__( 'Database Tools', 'newstatpress' ) . '

'; if ( isset( $_GET['tab'] ) ) { nsp_display_tabs_navbar_for_menu_page( $tools_page_tabs, sanitize_text_field( wp_unslash( $_GET['tab'] ) ), $page ); } else { nsp_display_tabs_navbar_for_menu_page( $tools_page_tabs, $default_tab, $page ); } if ( 'admin.php' === $pagenow && isset( $_GET['page'] ) && $page === $_GET['page'] ) { if ( isset( $_GET['tab'] ) ) { $tab = sanitize_text_field( wp_unslash( $_GET['tab'] ) ); } else { $tab = $default_tab; } switch ( $tab ) { case 'IP2nation': nsp_ip2nation(); break; case 'export': nsp_export(); break; case 'update': nsp_update(); break; case 'optimize': nsp_optimize(); break; case 'repair': nsp_repair(); break; case 'remove': nsp_remove_plugin_database(); break; case 'info': nsp_display_database_info(); break; } } } /** * Get table size of index * * @param string $table table to search. */ function nsp_index_table_size( $table ) { global $wpdb; // no needs prepare. $res = $wpdb->get_results( $wpdb->prepare( 'SHOW TABLE STATUS LIKE %s', $table ) ); // db call ok; no-cache ok. foreach ( $res as $fstatus ) { $index_lenght = $fstatus->Index_length; // phpcs:ignore -- not in valid snake_case format: it is a DB field! } return number_format( ( $index_lenght / 1024 / 1024 ), 2, ',', ' ' ) . ' Mb'; } /** * IP2nation form function *************************/ function nsp_ip2nation() { // Install or Remove if requested by user. if ( isset( $_POST['installation'] ) && 'install' === $_POST['installation'] ) { check_admin_referer( 'nsp_tool', 'nsp_tool_post' ); if ( ! current_user_can( 'administrator' ) ) { die( 'NO permission' ); } if ( ! ( isset( $_REQUEST['nsp_tool_post'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['nsp_tool_post'] ) ), 'nsp_tool' ) ) ) { die( 'Failed security check' ); } $install_result = nsp_ip2nation_install(); } elseif ( isset( $_POST['installation'] ) && 'remove' === $_POST['installation'] ) { check_admin_referer( 'nsp_tool', 'nsp_tool_post' ); if ( ! current_user_can( 'administrator' ) ) { die( 'NO permission' ); } if ( ! ( isset( $_REQUEST['nsp_tool_post'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['nsp_tool_post'] ) ), 'nsp_tool' ) ) ) { die( 'Failed security check' ); } $install_result = nsp_ip2nation_remove(); } // Display message if present. if ( isset( $install_result ) && '' !== $install_result ) { print "

" . esc_html( $install_result ) . '

'; } global $nsp_option_vars; global $wpdb; // Create IP2nation variable if not exists: value 'none' by default or date when installed. $installed = get_option( $nsp_option_vars['ip2nation']['name'] ); if ( '' === $installed ) { add_option( $nsp_option_vars['ip2nation']['name'], $nsp_option_vars['ip2nation']['value'], '', 'yes' ); } echo '

'; $file_ip2nation = plugin_dir_path( __FILE__ ) . '/includes/ip2nation.sql'; $date = gmdate( 'd/m/Y', filemtime( $file_ip2nation ) ); $table_name = 'ip2nation'; $val = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table_name ) ); // db call ok; no-cache ok. if ( $val !== $table_name ) { $value_remove = 'none'; $class_inst = 'desactivated'; $installed = $nsp_option_vars['ip2nation']['value']; } else { $value_remove = 'remove'; $class_inst = ''; $installed = get_option( $nsp_option_vars['ip2nation']['name'] ); if ( 'none' === $installed ) { $installed = esc_html__( 'unknow', 'newstatpress' ); } } // Display status. // translators: placeholder for number of version. $i = sprintf( __( 'Last version available: %s', 'newstatpress' ), $date ); echo esc_html( $i ) . '
'; if ( 'none' !== $installed ) { // translators: placeholder for number of version. $i = sprintf( __( 'Last version installed: %s', 'newstatpress' ), $installed ); echo esc_html( $i ) . '

'; esc_html_e( 'To update the IP2nation database, just click on the button bellow.', 'newstatpress' ); if ( $installed === $date ) { $button_name = __( 'Update', 'newstatpress' ); $value_install = 'none'; $class_install = 'desactivated'; } else { $button_name = __( 'Install', 'newstatpress' ); } } else { esc_html_e( 'Last version installed: none ', 'newstatpress' ); echo '

'; esc_html_e( 'To download and to install the IP2nation database, just click on the button bellow.', 'newstatpress' ); $button_name = __( 'Install', 'newstatpress' ); } ?>

'; echo wp_kses( _e( 'ip2nation is a free MySQL database that offers a quick way to map an IP to a country. The database is optimized to ensure fast lookups and is based on information from ARIN, APNIC, RIPE etc. You may install the database using the link to the left. (see: http://www.ip2nation.com)', 'newstatpress' ), array( 'a' => array( 'href' => array(), ), ) ); echo "

" . esc_html__( 'Note: The installation may take some times to complete.', 'newstatpress' ) . ''; ?>
query( $wpdb->prepare( '%s', $val ) ); // db call ok; no-cache ok. } $date = gmdate( 'd/m/Y', filemtime( $file_ip2nation ) ); update_option( $nsp_option_vars['ip2nation']['name'], $date ); $install_status = __( 'Installation of IP2nation database was successful', 'newstatpress' ); return $install_status; } // TODO integrate error check. /** * Remove ip2nation table */ function nsp_ip2nation_remove() { global $wpdb; global $nsp_option_vars; // no need prepare. $wpdb->query( 'DROP TABLE IF EXISTS ip2nation;' ); // db call ok; no-cache ok. $wpdb->query( 'DROP TABLE IF EXISTS ip2nationCountries;' ); // db call ok; no-cache ok. update_option( $nsp_option_vars['ip2nation']['name'], $nsp_option_vars['ip2nation']['value'] ); $install_status = __( 'IP2nation database was remove successfully', 'newstatpress' ); return $install_status; } /** * Export form function */ function nsp_export() { $export_description = esc_html__( 'The export tool allows you to save your statistics in a local file for a date interval defined by yourself.', 'newstatpress' ); $export_description .= '
'; $export_description .= esc_html__( 'You can define the filename and the file extension, and also the fields delimiter used to separate the data.', 'newstatpress' ); $export_description2 = esc_html__( 'Note: the parameters chosen will be saved automatically as default values.', 'newstatpress' ); $delimiter_description = esc_html__( 'default value : semicolon', 'newstatpress' ); $extension_description = esc_html__( 'default value : CSV (readable by Excel)', 'newstatpress' ); $filename_description = esc_html__( 'If the field remain blank, the default value is \'BLOG_TITLE-newstatpress\'.', 'newstatpress' ); $filename_description .= '
'; $filename_description .= esc_html__( 'The date interval will be added to the filename (i.e. BLOG_TITLE-newstatpress_20160229-20160331.csv).', 'newstatpress' ); $export_option = get_option( 'newstatpress_exporttool' ); ?>

'>
'>
' value="">

>
get_results( $wpdb->prepare( 'SELECT * FROM %s WHERE date>= %s AND date<= %s; ', $table_name, gmdate( 'Ymd', $i_from ), gmdate( 'Ymd', $i_to ) ) ); // db call ok; no-cache ok. if ( 't' === $del ) { $del = "\t"; } print 'date' . esc_html( $del ) . 'time' . esc_html( $del ) . 'ip' . esc_html( $del ) . 'urlrequested' . esc_html( $del ) . 'agent' . esc_html( $del ) . 'referrer' . esc_html( $del ) . 'search' . esc_html( $del ) . 'nation' . esc_html( $del ) . 'os' . esc_html( $del ) . 'browser' . esc_html( $del ) . 'searchengine' . esc_html( $del ) . 'spider' . esc_html( $del ) . "feed\n"; foreach ( $qry as $rk ) { print '"' . esc_html( $rk->date ) . '"' . esc_html( $del ) . '"' . esc_html( $rk->time ) . '"' . esc_html( $del ) . '"' . esc_html( $rk->ip ) . '"' . esc_html( $del ) . '"' . esc_html( $rk->urlrequested ) . '"' . esc_html( $del ) . '"' . esc_html( $rk->agent ) . '"' . esc_html( $del ) . '"' . esc_html( $rk->referrer ) . '"' . esc_html( $del ) . '"' . esc_html( $rk->search ) . '"' . esc_html( $del ) . '"' . esc_html( $rk->nation ) . '"' . esc_html( $del ) . '"' . esc_html( $rk->os ) . '"' . esc_html( $del ) . '"' . esc_html( $rk->browser ) . '"' . esc_html( $del ) . '"' . esc_html( $rk->searchengine ) . '"' . esc_html( $del ) . '"' . esc_html( $rk->spider ) . '"' . esc_html( $del ) . '"' . esc_html( $rk->feed ) . '"' . "\n"; } die(); } /** * Generate HTML for remove menu in WordPress */ function nsp_remove_plugin_database() { if ( isset( $_POST['removeit'] ) && 'yes' === $_POST['removeit'] ) { check_admin_referer( 'nsp_tool', 'nsp_tool_post' ); if ( ! current_user_can( 'administrator' ) ) { die( 'NO permission' ); } if ( ! ( isset( $_POST['nsp_tool_post'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nsp_tool_post'] ) ), 'nsp_tool' ) ) ) { die( 'Failed security check' ); } global $wpdb; $table_name = NSP_TABLENAME; $results = $wpdb->query( $wpdb->prepare( 'DELETE FROM %s', $table_name ) ); // db call ok; no-cache ok. print "

" . esc_html__( 'All data removed', 'newstatpress' ) . '!

'; } else { ?>








" . esc_html__( 'Note: The update may take some times to complete.', 'newstatpress' ) . ''; ?>
flush(); // flush for counting right the queries. $start_time = microtime( true ); $days = nsp_duration_to_days(); // get the number of days for the update. $to_date = gmdate( 'Ymd', current_time( 'timestamp' ) ); if ( -1 === $days ) { $from_date = '19990101'; // use a date where this plugin was not present. } else { $from_date = gmdate( 'Ymd', current_time( 'timestamp' ) - 86400 * $days ); } $_newstatpress_url = nsp_plugin_url(); $wpdb->show_errors(); ?>


flush(); // flush for counting right the queries. $start_time = microtime( true ); $days = nsp_duration_to_days(); // get the number of days for the update. $to_date = gmdate( 'Ymd', current_time( 'timestamp' ) ); if ( -1 === $days ) { $from_date = '19990101'; // use a date where this plugin was not present. } else { $from_date = gmdate( 'Ymd', current_time( 'timestamp' ) - 86400 * $days ); } $_newstatpress_url = nsp_plugin_url(); $wpdb->show_errors(); // add by chab // $var requesting the absolute path. $img_ok = $_newstatpress_url . 'images/ok.gif'; print "

" . esc_html__( 'Database Update', 'newstatpress' ) . '


'; print "
prefix . 'statpress' ) ); ?> prefix . 'statpress' ) ); ?>
prefix . 'statpress' ) ); ?>
'; print ""; // update table. nsp_build_plugin_sql_table( 'update' ); echo '"; print ''; print ''; print ""; // Update Feed. print ''; // use prepare. // phpcs:ignore -- db call ok; no-cache ok. $wpdb->query( $wpdb->prepare( "UPDATE `$table_name` SET feed='' WHERE date BETWEEN %s AND %s ", $from_date, $to_date ) ); // phpcs:ignore: unprepared SQL OK. // not standard. // use prepare. // phpcs:ignore -- db call ok; no-cache ok. $wpdb->query( $wpdb->prepare( "UPDATE `$table_name` SET feed='RSS2' WHERE urlrequested LIKE %s AND date BETWEEN %s AND %s ", '%%/feed/%%', $from_date, $to_date ) ); // phpcs:ignore: unprepared SQL OK. // use prepare. // phpcs:ignore -- db call ok; no-cache ok. $wpdb->query( $wpdb->prepare( "UPDATE `$table_name` SET feed='RSS2' WHERE urlrequested LIKE %s AND date BETWEEN %s AND %s ", '%%wp-feed.php%%', $from_date, $to_date ) ); // phpcs:ignore: unprepared SQL OK. // standard blog info urls. $s = nsp_extract_feed_req( get_bloginfo( 'comments_atom_url' ) ); if ( '' !== $s ) { // use prepare. // phpcs:ignore -- db call ok; no-cache ok. $wpdb->query( $wpdb->prepare( "UPDATE `$table_name` SET feed='COMMENT' WHERE INSTR(urlrequested, %s)>0 AND date BETWEEN %s AND %s ", $s, $from_date, $to_date ) ); // phpcs:ignore: unprepared SQL OK. } $s = nsp_extract_feed_req( get_bloginfo( 'comments_rss2_url' ) ); if ( '' !== $s ) { // use prepare. // phpcs:ignore -- db call ok; no-cache ok. $wpdb->query( $wpdb->prepare( "UPDATE `$table_name` SET feed='COMMENT' WHERE INSTR(urlrequested, %s)>0 AND date BETWEEN %s AND %s ", $s, $from_date, $to_date ) ); // phpcs:ignore: unprepared SQL OK. } $s = nsp_extract_feed_req( get_bloginfo( 'atom_url' ) ); if ( '' !== $s ) { // use prepare. // phpcs:ignore -- db call ok; no-cache ok. $wpdb->query( $wpdb->prepare( "UPDATE `$table_name` SET feed='ATOM' WHERE INSTR(urlrequested, %s)>0 AND date BETWEEN %s AND %s ", $s, $from_date, $to_date ) ); // phpcs:ignore: unprepared SQL OK. } $s = nsp_extract_feed_req( get_bloginfo( 'rdf_url' ) ); if ( '' !== $s ) { // use prepare. // phpcs:ignore -- db call ok; no-cache ok. $wpdb->query( $wpdb->prepare( "UPDATE `$table_name` SET feed='RDF' WHERE INSTR(urlrequested, %s)>0 AND date BETWEEN %s AND %s ", $s, $from_date, $to_date ) ); // phpcs:ignore: unprepared SQL OK. } $s = nsp_extract_feed_req( get_bloginfo( 'rss_url' ) ); if ( '' !== $s ) { // use prepare. // phpcs:ignore -- db call ok; no-cache ok. $wpdb->query( $wpdb->prepare( "UPDATE `$table_name` SET feed='RSS' WHERE INSTR(urlrequested, %s)>0 AND date BETWEEN %s AND %s ", $s, $from_date, $to_date ) ); // phpcs:ignore: unprepared SQL OK. } $s = nsp_extract_feed_req( get_bloginfo( 'rss2_url' ) ); if ( '' !== $s ) { // use prepare. // phpcs:ignore -- db call ok; no-cache ok. $wpdb->query( $wpdb->prepare( "UPDATE `$table_name` SET feed='RSS2' WHERE INSTR(urlrequested, %s)>0 AND date BETWEEN %s AND %s ", $s, $from_date, $to_date ) ); // phpcs:ignore: unprepared SQL OK. } // use prepare. // phpcs:ignore -- db call ok; no-cache ok. $wpdb->query( $wpdb->prepare( "UPDATE `$table_name` SET feed = '' WHERE isnull(feed) AND date BETWEEN %s AND %s ", $from_date, $to_date ) ); // phpcs:ignore: unprepared SQL OK. print ''; print ""; // Update OS. print ''; // use prepare. // phpcs:ignore -- db call ok; no-cache ok. $wpdb->query( $wpdb->prepare( "UPDATE `$table_name` SET os = '' WHERE date BETWEEN %s AND %s ", $from_date, $to_date ) ); // phpcs:ignore: unprepared SQL OK. $lines = file( $newstatpress_dir . '/def/os.dat' ); foreach ( $lines as $line_num => $os ) { list($nome_os,$id_os) = explode( '|', $os ); // use prepare. // phpcs:ignore -- db call ok; no-cache ok. $wpdb->query( $wpdb->prepare( "UPDATE `$table_name` SET os = %s WHERE os='' AND replace(agent,' ','') LIKE %s AND date BETWEEN %s AND %s ", $nome_os, '%' . $id_os . '%', $from_date, $to_date ) ); // phpcs:ignore: unprepared SQL OK. } print ''; print ""; // Update Browser. print ''; // use prepare. // phpcs:ignore -- db call ok; no-cache ok. $wpdb->query( $wpdb->prepare( "UPDATE `$table_name` SET browser = '' WHERE date BETWEEN %s AND %s ", $from_date, $to_date ) ); // phpcs:ignore: unprepared SQL OK. $lines = file( $newstatpress_dir . '/def/browser.dat' ); foreach ( $lines as $line_num => $browser ) { list($nome,$id) = explode( '|', $browser ); // use prepare. // phpcs:ignore -- db call ok; no-cache ok. $wpdb->query( $wpdb->prepare( "UPDATE `$table_name` SET browser = %s WHERE browser='' AND replace(agent,' ','') LIKE %s AND date BETWEEN %s AND %s ", $nome, '%' . $id . '%', $from_date, $to_date ) ); // phpcs:ignore: unprepared SQL OK. } print ''; print ""; // Update Spider. print ''; // use prepare. // phpcs:ignore -- db call ok; no-cache ok. $wpdb->query( $wpdb->prepare( "UPDATE `$table_name` SET spider = '' WHERE date BETWEEN %s AND %s ", $from_date, $to_date ) ); // phpcs:ignore: unprepared SQL OK. $lines = file( $newstatpress_dir . '/def/spider.dat' ); foreach ( $lines as $line_num => $spider ) { list($nome,$id) = explode( '|', $spider ); // use prepare. // phpcs:ignore -- db call ok; no-cache ok. $wpdb->query( $wpdb->prepare( "UPDATE `$table_name` SET spider = %s,os='',browser='' WHERE spider='' AND replace(agent,' ','') LIKE %s AND date BETWEEN %s AND %s ", $nome, '%' . $id . '%', $from_date, $to_date ) ); // phpcs:ignore: unprepared SQL OK. } print ''; print ""; // Update Search engine. print ''; // use prepare. // phpcs:ignore -- db call ok; no-cache ok. $wpdb->query( $wpdb->prepare( "UPDATE `$table_name` SET searchengine = '', search='' WHERE date BETWEEN %s AND %s ", $from_date, $to_date ) ); // phpcs:ignore: unprepared SQL OK. // use prepare. // phpcs:ignore -- db call ok; no-cache ok. $qry = $wpdb->get_results( $wpdb->prepare( "SELECT id, referrer FROM `$table_name` WHERE length(referrer)!=0 AND date BETWEEN %s AND %s ", $from_date, $to_date ) ); // phpcs:ignore: unprepared SQL OK. foreach ( $qry as $rk ) { list($searchengine,$search_phrase) = explode( '|', nsp_get_se( $rk->referrer ) ); if ( '' !== $searchengine ) { // use prepare. // phpcs:ignore -- db call ok; no-cache ok. $wpdb->query( $wpdb->prepare( "UPDATE `$table_name` SET searchengine = %s, search=%s WHERE id= %d AND date BETWEEN %s AND %s ", $searchengine, addslashes( $search_phrase ), $rk->id, $from_date, $to_date ) ); // phpcs:ignore: unprepared SQL OK. } } print ''; print ""; $end_time = microtime( true ); $sql_queries = $wpdb->num_queries; // Final statistics. print ''; print ''; // todo chab : to clean. print ""; print ''; print ''; // todo chab : to clean. print ""; print ''; print ''; print ""; print ''; print ''; print ""; print "
" . esc_html__( 'Updating...', 'newstatpress' ) . "" . esc_html__( 'Size', 'newstatpress' ) . "" . esc_html__( 'Result', 'newstatpress' ) . '
' . esc_html__( 'Structure', 'newstatpress' ) . ' ' . esc_html( $table_name ) . ' ' . esc_html( nsp_table_size( $wpdb->prefix . 'statpress' ) ) . "
' . esc_html__( 'Index', 'newstatpress' ) . ' ' . esc_html( $table_name ) . '' . esc_html( ( $wpdb->prefix . 'statpress' ) ) . '
' . esc_html__( 'Feeds', 'newstatpress' ) . '
' . esc_html__( 'OSes', 'newstatpress' ) . '
' . esc_html__( 'Browsers', 'newstatpress' ) . '
' . esc_html__( 'Spiders', 'newstatpress' ) . '
' . esc_html__( 'Search engines', 'newstatpress' ) . '
' . esc_html__( 'Final Structure', 'newstatpress' ) . ' ' . esc_html( $table_name ) . '' . esc_html( nsp_table_size( $wpdb->prefix . 'statpress' ) ) . '
' . esc_html__( 'Final Index', 'newstatpress' ) . ' ' . esc_html( $table_name ) . '' . esc_html( nsp_index_table_size( $wpdb->prefix . 'statpress' ) ) . '
' . esc_html__( 'Duration of the update', 'newstatpress' ) . '' . esc_html( round( $end_time - $start_time, 2 ) ) . ' sec
' . esc_html__( 'This update was done in', 'newstatpress' ) . '' . esc_html( $sql_queries ) . ' ' . esc_html__( 'SQL queries', 'newstatpress' ) . '

\n"; $wpdb->hide_errors(); } /** * Optimize form function */ function nsp_optimize() { // database update if requested by user. if ( isset( $_POST['optimize'] ) && 'yes' === $_POST['optimize'] ) { check_admin_referer( 'nsp_tool', 'nsp_tool_post' ); if ( ! current_user_can( 'administrator' ) ) { die( 'NO permission' ); } if ( ! ( isset( $_POST['nsp_tool_post'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nsp_tool_post'] ) ), 'nsp_tool' ) ) ) { die( 'Failed security check' ); } nsp_optimize_now(); die; } ?>









query( $wpdb->prepare( 'OPTIMIZE TABLE %s', $table_name ) ); // db call ok; no-cache ok. print "

" . esc_html__( 'Optimization finished', 'newstatpress' ) . '!

'; } /** * Repair the table */ function nsp_repair_now() { global $wpdb; $table_name = NSP_TABLENAME; $wpdb->query( $wpdb->prepare( 'REPAIR TABLE %s', $table_name ) ); // db call ok; no-cache ok. print "

" . esc_html__( 'Repair finished', 'newstatpress' ) . '!

'; } ?>