PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/public/wp-content/plugins/wp-statistics/includes/optimization/wps-optimization.php

https://gitlab.com/kath.de/cibedo_cibedo.de
PHP | 267 lines | 189 code | 55 blank | 23 comment | 42 complexity | 34b0c7e0e8d4003d5adaffcfbf4bec73 MD5 | raw file
  1. <?php
  2. GLOBAL $wpdb, $WP_Statistics;
  3. $wp_prefix = $wpdb->prefix;
  4. if( !is_super_admin() )
  5. wp_die(__('Access denied!', 'wp_statistics'));
  6. if( array_key_exists( 'populate', $_GET ) ) {
  7. if( intval($_GET['populate']) == 1 ) {
  8. require_once( plugin_dir_path( __FILE__ ) . '../functions/geoip-populate.php' );
  9. echo wp_statistics_populate_geoip_info();
  10. }
  11. }
  12. if( array_key_exists( 'hash-ips', $_GET ) ) {
  13. if( intval($_GET['hash-ips']) == 1 ) {
  14. // Generate a random salt
  15. $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  16. $randomString = '';
  17. for ($i = 0; $i < 50; $i++) {
  18. $randomString .= $characters[rand(0, strlen($characters) - 1)];
  19. }
  20. // Get the rows from the Visitors table.
  21. $result = $wpdb->get_results( "SELECT DISTINCT ip FROM {$wp_prefix}statistics_visitor" );
  22. foreach( $result as $row ) {
  23. if( substr( $row->ip, 0, 6 ) != '#hash#' ) {
  24. $wpdb->update(
  25. $wp_prefix . "statistics_visitor",
  26. array(
  27. 'ip' => '#hash#' . sha1( $row->ip . $randomString ),
  28. ),
  29. array(
  30. 'ip' => $row->ip,
  31. )
  32. );
  33. }
  34. }
  35. echo "<div class='updated settings-error'><p><strong>" . __('IP Addresses replaced with hash values.', 'wp_statistics') . "</strong></p></div>";
  36. }
  37. }
  38. if( array_key_exists( 'install', $_GET ) ) {
  39. if( intval($_GET['install']) == 1 ) {
  40. $WPS_Installed = "1.0";
  41. include( plugin_dir_path( __FILE__ ) . "../../wps-install.php" );
  42. echo "<div class='updated settings-error'><p><strong>" . __('Install routine complete.', 'wp_statistics') . "</strong></p></div>";
  43. }
  44. }
  45. if( array_key_exists( 'index', $_GET ) ) {
  46. if( intval($_GET['index']) == 1 ) {
  47. // Check the number of index's on the visitors table, if it's only 5 we need to check for duplicate entries and remove them
  48. $result = $wpdb->query("SHOW INDEX FROM {$wp_prefix}statistics_visitor WHERE Key_name = 'date_ip'");
  49. if( $result != 5 ) {
  50. // We have to loop through all the rows in the visitors table to check for duplicates that may have been created in error.
  51. $result = $wpdb->get_results( "SELECT ID, last_counter, ip FROM {$wp_prefix}statistics_visitor ORDER BY last_counter, ip" );
  52. // Setup the inital values.
  53. $lastrow = array( 'last_counter' => '', 'ip' => '' );
  54. $deleterows = array();
  55. // Ok, now iterate over the results.
  56. foreach( $result as $row ) {
  57. // if the last_counter (the date) and IP is the same as the last row, add the row to be deleted.
  58. if( $row->last_counter == $lastrow['last_counter'] && $row->ip == $lastrow['ip'] ) { $deleterows[] .= $row->ID;}
  59. // Update the lastrow data.
  60. $lastrow['last_counter'] = $row->last_counter;
  61. $lastrow['ip'] = $row->ip;
  62. }
  63. // Now do the acutal deletions.
  64. foreach( $deleterows as $row ) {
  65. $wpdb->delete( $wp_prefix . 'statistics_visitor', array( 'ID' => $row ) );
  66. }
  67. // The table should be ready to be updated now with the new index, so let's do it.
  68. $result = $wpdb->get_results( "ALTER TABLE " . $wp_prefix . 'statistics_visitor' . " ADD UNIQUE `date_ip_agent` ( `last_counter`, `ip`, `agent` (75), `platform` (75), `version` (75) )" );
  69. // We might have an old index left over from 7.1-7.3 so lets make sure to delete it.
  70. $wpdb->query( "DROP INDEX `date_ip` ON {$wp_prefix}statistics_visitor" );
  71. // Record in the options that we've done this update.
  72. $dbupdates = $WP_Statistics->get_option('pending_db_updates');
  73. $dbupdates['date_ip_agent'] = false;
  74. $WP_Statistics->update_option('pending_db_updates', $dbupdates);
  75. }
  76. }
  77. }
  78. if( array_key_exists( 'visits', $_GET ) ) {
  79. if( intval($_GET['visits']) == 1 ) {
  80. // Check the number of index's on the visits table, if it's only 5 we need to check for duplicate entries and remove them
  81. $result = $wpdb->query("SHOW INDEX FROM {$wp_prefix}statistics_visit WHERE Key_name = 'unique_date'");
  82. // Note, the result will be the number of fields contained in the index, so in our case 1.
  83. if( $result != 1 ) {
  84. // We have to loop through all the rows in the visitors table to check for duplicates that may have been created in error.
  85. $result = $wpdb->get_results( "SELECT ID, last_counter, visit FROM {$wp_prefix}statistics_visit ORDER BY last_counter, visit DESC" );
  86. // Setup the initial values.
  87. $lastrow = array( 'last_counter' => '', 'visit' => 0, 'id' => 0 );
  88. $deleterows = array();
  89. // Ok, now iterate over the results.
  90. foreach( $result as $row ) {
  91. // if the last_counter (the date) and IP is the same as the last row, add the row to be deleted.
  92. if( $row->last_counter == $lastrow['last_counter'] ) { $deleterows[] .= $row->ID; }
  93. // Update the lastrow data.
  94. $lastrow['last_counter'] = $row->last_counter;
  95. $lastrow['id'] = $row->ID;
  96. $lastrow['visit'] = $row->visit;
  97. }
  98. // Now do the acutal deletions.
  99. foreach( $deleterows as $row ) {
  100. $wpdb->delete( $wp_prefix . 'statistics_visit', array( 'ID' => $row ) );
  101. }
  102. // The table should be ready to be updated now with the new index, so let's do it.
  103. $result = $wpdb->get_results( "ALTER TABLE " . $wp_prefix . 'statistics_visit' . " ADD UNIQUE `unique_date` ( `last_counter` )" );
  104. // Record in the options that we've done this update.
  105. $dbupdates = $WP_Statistics->get_option('pending_db_updates');
  106. $dbupdates['unique_date'] = false;
  107. $WP_Statistics->update_option('pending_db_updates', $dbupdates);
  108. }
  109. }
  110. }
  111. if( array_key_exists( 'historical-submit', $_POST ) ) {
  112. if( array_key_exists( 'wps_historical_visitors', $_POST ) ) {
  113. $result = $wpdb->update( $wp_prefix . "statistics_historical", array( 'value' => $_POST['wps_historical_visitors'] ), array( 'category' => 'visitors' ) );
  114. if( $result == 0 ) {
  115. $result = $wpdb->insert( $wp_prefix . "statistics_historical", array( 'value' => $_POST['wps_historical_visitors'], 'category' => 'visitors', 'page_id' => -1, 'uri' => '-1' ) );
  116. }
  117. }
  118. if( array_key_exists( 'wps_historical_visits', $_POST ) ) {
  119. $result = $wpdb->update( $wp_prefix . "statistics_historical", array( 'value' => $_POST['wps_historical_visits'] ), array( 'category' => 'visits' ) );
  120. if( $result == 0 ) {
  121. $result = $wpdb->insert( $wp_prefix . "statistics_historical", array( 'value' => $_POST['wps_historical_visits'], 'category' => 'visits', 'page_id' => -2, 'uri' => '-2' ) );
  122. }
  123. }
  124. }
  125. if( array_key_exists( 'search', $_GET ) ) {
  126. // Make sure we get all the search engines, even the ones the disabled ones.
  127. $se_list = wp_statistics_searchengine_list();
  128. $total = 0;
  129. $limitsize = 10000;
  130. foreach( $se_list as $key => $se ) {
  131. $sql = wp_statistics_searchengine_query( $key );
  132. $rowcount = $wpdb->get_var( "SELECT count(*) FROM `{$wpdb->prefix}statistics_visitor` WHERE {$sql}" );
  133. $offset = 0;
  134. while( $rowcount > 0 ) {
  135. $result = $wpdb->get_results( "SELECT * FROM `{$wpdb->prefix}statistics_visitor` WHERE {$sql} LIMIT {$offset}, {$limitsize}" );
  136. foreach( $result as $row ) {
  137. $parts = parse_url( $row->referred );
  138. $data['last_counter'] = $row->last_counter;
  139. $data['engine'] = $key;
  140. $data['host'] = $parts['host'];
  141. $data['words'] = $WP_Statistics->Search_Engine_QueryString( $row->referred );
  142. $data['visitor'] = $row->ID;
  143. if( $data['words'] == 'No search query found!' ) { $data['words'] = ''; }
  144. $wpdb->insert( $wpdb->prefix . 'statistics_search', $data );
  145. $total++;
  146. }
  147. $rowcount -= $limitsize;
  148. $offset += $limitsize;
  149. }
  150. }
  151. $WP_Statistics->update_option('search_converted', 1);
  152. echo "<div class='updated settings-error'><p><strong>" . sprintf( __('Search table conversion complete, %d rows added.', 'wp_statistics'), $total ) . "</strong></p></div>";
  153. }
  154. $selected_tab = "";
  155. if( array_key_exists( 'tab', $_GET ) ) { $selected_tab = $_GET['tab']; }
  156. switch( $selected_tab )
  157. {
  158. case 'export':
  159. $current_tab = 1;
  160. break;
  161. case 'purging':
  162. $current_tab = 2;
  163. break;
  164. case 'database':
  165. $current_tab = 3;
  166. break;
  167. case 'updates':
  168. $current_tab = 4;
  169. break;
  170. case 'historical':
  171. $current_tab = 5;
  172. break;
  173. default:
  174. $current_tab = 0;
  175. }
  176. ?>
  177. <script type="text/javascript">
  178. jQuery(document).ready(function() {
  179. jQuery("#tabs").tabs();
  180. <?php if( $current_tab != 0 ) { echo 'jQuery("#tabs").tabs("option", "active",' . $current_tab. ');' . "\n"; }?>
  181. } );
  182. </script>
  183. <div class="wrap">
  184. <div id="tabs" class="ui-tabs ui-widget ui-widget-content ui-corner-all">
  185. <ul class="ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all">
  186. <li class="ui-state-default ui-corner-top"><a href="#resources" class="ui-tabs-anchor"><span><?php _e('Resources/Information', 'wp_statistics'); ?></span></a></li>
  187. <li class="ui-state-default ui-corner-top"><a href="#export" class="ui-tabs-anchor"><span><?php _e('Export', 'wp_statistics'); ?></span></a></li>
  188. <li class="ui-state-default ui-corner-top"><a href="#purging" class="ui-tabs-anchor"><span><?php _e('Purging', 'wp_statistics'); ?></span></a></li>
  189. <li class="ui-state-default ui-corner-top"><a href="#database" class="ui-tabs-anchor"><span><?php _e('Database', 'wp_statistics'); ?></span></a></li>
  190. <li class="ui-state-default ui-corner-top"><a href="#updates" class="ui-tabs-anchor"><span><?php _e('Updates', 'wp_statistics'); ?></span></a></li>
  191. <li class="ui-state-default ui-corner-top"><a href="#historical" class="ui-tabs-anchor"><span><?php _e('Historical', 'wp_statistics'); ?></span></a></li>
  192. </ul>
  193. <div id="resources">
  194. <?php include( dirname( __FILE__ ) . '/tabs/wps-optimization-resources.php' ); ?>
  195. </div>
  196. <div id="export">
  197. <?php include( dirname( __FILE__ ) . '/tabs/wps-optimization-export.php' ); ?>
  198. </div>
  199. <div id="purging">
  200. <?php include( dirname( __FILE__ ) . '/tabs/wps-optimization-purging.php' ); ?>
  201. </div>
  202. <div id="database">
  203. <?php include( dirname( __FILE__ ) . '/tabs/wps-optimization-database.php' ); ?>
  204. </div>
  205. <div id="updates">
  206. <?php include( dirname( __FILE__ ) . '/tabs/wps-optimization-updates.php' ); ?>
  207. </div>
  208. <div id="historical">
  209. <?php include( dirname( __FILE__ ) . '/tabs/wps-optimization-historical.php' ); ?>
  210. </div>
  211. </div>
  212. </div>