Changeset 3835790
- Timestamp:
- 11/30/2026 05:48:23 PM (92 minutes ago)
- Location:
- ggsec-stats
- Files:
-
- 16 edited
- 1 copied
-
tags/1.5.0 (copied) (copied from ggsec-stats/trunk )
-
tags/1.5.0/README.md (modified) (3 diffs)
-
tags/1.5.0/assets/admin.css (modified) (1 diff)
-
tags/1.5.0/ggsec-stats.php (modified) (6 diffs)
-
tags/1.5.0/includes/admin.php (modified) (19 diffs)
-
tags/1.5.0/includes/bots.php (modified) (1 diff)
-
tags/1.5.0/includes/notify.php (modified) (2 diffs)
-
tags/1.5.0/readme.txt (modified) (6 diffs)
-
tags/1.5.0/uninstall.php (modified) (2 diffs)
-
trunk/README.md (modified) (3 diffs)
-
trunk/assets/admin.css (modified) (1 diff)
-
trunk/ggsec-stats.php (modified) (6 diffs)
-
trunk/includes/admin.php (modified) (19 diffs)
-
trunk/includes/bots.php (modified) (1 diff)
-
trunk/includes/notify.php (modified) (2 diffs)
-
trunk/readme.txt (modified) (6 diffs)
-
trunk/uninstall.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
ggsec-stats/tags/1.5.0/README.md
r3830097 r3835790 19 19 - Dedicated **Pages read by AI crawlers** table showing which content is being fetched by AI models. 20 20 - Separate counter and User-Agent breakdown for browser-like clients that never execute JavaScript. 21 - **Visitors from AI assistants:** confirmed human views referred by ChatGPT, Perplexity, Claude, Copilot, Gemini and others (referrer host or `utm_source`; list filterable via `ggsec_stats_ai_referrers`). 21 22 - **Security Reconnaissance & Broken Links:** 22 23 - Tracks failed login attempts (`wp_login_failed`), bot login discovery scans (`wp-login.php`), and `xmlrpc.php` probes. … … 33 34 - **Zero Admin Dependencies:** 34 35 - Renders the 30-day stacked bar chart (humans vs. bots) as inline SVG in PHP without external JavaScript charting libraries. 36 - Headline cards show the change against the previous period, cut at the same time of day. 37 - **Long-term History:** 38 - The daily cron aggregates each finished day into `{$wpdb->prefix}ggsec_stats_daily` (counts only, no personal data) before raw hits past the retention period are deleted; a day is never deleted before it is aggregated. 39 - Monthly chart of the last 12 months; existing raw data is backfilled on update. 35 40 - Includes a compact WordPress Dashboard widget. 36 41 … … 58 63 ## Uninstall 59 64 60 When deleted through the WordPress Plugins screen, `uninstall.php` drops the `{$wpdb->prefix}ggsec_stats_hits` table, removes plugin options (`ggsec_stats_db_version`, `ggsec_stats_salt`, `ggsec_stats_salt_day`, `ggsec_stats_notify`, `ggsec_stats_notify_state`), and clears the daily cleanup and hourly notification cron hooks.65 When deleted through the WordPress Plugins screen, `uninstall.php` drops the `{$wpdb->prefix}ggsec_stats_hits` and `{$wpdb->prefix}ggsec_stats_daily` tables, removes plugin options (`ggsec_stats_db_version`, `ggsec_stats_salt`, `ggsec_stats_salt_day`, `ggsec_stats_notify`, `ggsec_stats_notify_state`, `ggsec_stats_purged_before`), and clears the daily cleanup and hourly notification cron hooks. 61 66 62 67 ## License -
ggsec-stats/tags/1.5.0/assets/admin.css
r3830097 r3835790 28 28 color: #8c8f94; 29 29 font-size: 11px; 30 } 31 .ggsec-stats-card .chg { 32 font-size: 12px; 33 font-weight: 600; 34 white-space: nowrap; 35 cursor: help; 36 } 37 .ggsec-stats-card .chg.good { 38 color: #008a20; 39 } 40 .ggsec-stats-card .chg.bad { 41 color: #d63638; 42 } 43 .ggsec-stats-card .chg.neutral { 44 color: #646970; 30 45 } 31 46 .ggsec-stats-chart { -
ggsec-stats/tags/1.5.0/ggsec-stats.php
r3830097 r3835790 4 4 * Plugin URI: https://github.com/ggsec-de/ggsec-stats 5 5 * Description: Privacy-first page view statistics: humans vs bots, referrers, top pages, online now. No cookies, no external services. 6 * Version: 1. 4.06 * Version: 1.5.0 7 7 * Requires at least: 6.3 8 8 * Requires PHP: 7.4 … … 22 22 } 23 23 24 const GGSEC_STATS_VERSION = '1. 4.0';24 const GGSEC_STATS_VERSION = '1.5.0'; 25 25 // Bumped also when scheduled events change: ggsec_stats_maybe_upgrade() then re-runs the install routine, 26 26 // which schedules missing events on sites updated without re-activation (WordPress.org updates, FTP). 27 const GGSEC_STATS_DB_VERSION = ' 3';27 const GGSEC_STATS_DB_VERSION = '4'; 28 28 29 29 // Hit kinds. … … 53 53 global $wpdb; 54 54 return $wpdb->prefix . 'ggsec_stats_hits'; 55 } 56 57 /** 58 * Daily totals (counts only, no personal data); kept after the raw hits are deleted. 59 */ 60 function ggsec_stats_daily_table() { 61 global $wpdb; 62 return $wpdb->prefix . 'ggsec_stats_daily'; 55 63 } 56 64 … … 102 110 ); 103 111 112 $daily = ggsec_stats_daily_table(); 113 dbDelta( 114 "CREATE TABLE {$daily} ( 115 day date NOT NULL, 116 human_views int(10) unsigned NOT NULL DEFAULT 0, 117 visitors int(10) unsigned NOT NULL DEFAULT 0, 118 bots_known int(10) unsigned NOT NULL DEFAULT 0, 119 bots_nojs int(10) unsigned NOT NULL DEFAULT 0, 120 probes int(10) unsigned NOT NULL DEFAULT 0, 121 downloads int(10) unsigned NOT NULL DEFAULT 0, 122 recon int(10) unsigned NOT NULL DEFAULT 0, 123 ai_referrals int(10) unsigned NOT NULL DEFAULT 0, 124 PRIMARY KEY (day) 125 ) {$charset};" 126 ); 127 104 128 update_option( 'ggsec_stats_db_version', GGSEC_STATS_DB_VERSION, false ); 129 130 // Fills the daily history from the raw hits already recorded (first install of 1.5.0). 131 ggsec_stats_rollup(); 105 132 106 133 if ( ! wp_next_scheduled( 'ggsec_stats_daily' ) ) { … … 129 156 130 157 /** 131 * Daily maintenance: delete raw hits past the retention period, in batches. 158 * Aggregate complete local days (before today) from the raw hits into the daily table. 159 * 160 * Re-aggregates from the day before the last aggregated day (late beacons may still have upgraded hits after 161 * midnight), but never a day that retention cleanup may already have partly deleted: such a day would overwrite 162 * its correct totals with smaller ones. 163 * 164 * @return bool False on a database error. 165 */ 166 function ggsec_stats_rollup() { 167 global $wpdb; 168 $hits = ggsec_stats_table(); 169 $daily = ggsec_stats_daily_table(); 170 $tz = wp_timezone(); 171 172 // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin's own tables, maintenance job. 173 $last = (string) $wpdb->get_var( $wpdb->prepare( 'SELECT MAX(day) FROM %i', $daily ) ); 174 $from = '' !== $last 175 ? ( new DateTimeImmutable( $last, $tz ) )->modify( '-1 day' )->format( 'Y-m-d' ) 176 : (string) $wpdb->get_var( $wpdb->prepare( 'SELECT MIN(day) FROM %i', $hits ) ); 177 if ( '' === $from ) { 178 return true; // No hits recorded yet. 179 } 180 181 // Hits created before the last cleanup cutoff are gone; the local day containing it is incomplete. 182 // Without a recorded cutoff (sites updated from 1.4 or older), assume the most recent possible one. 183 $purged = (int) get_option( 'ggsec_stats_purged_before', time() - ggsec_stats_retention_days() * DAY_IN_SECONDS ); 184 $safe_from = ( new DateTimeImmutable( '@' . $purged ) )->setTimezone( $tz )->modify( '+1 day' )->format( 'Y-m-d' ); 185 $from = max( $from, $safe_from ); 186 $to = ( new DateTimeImmutable( 'yesterday', $tz ) )->format( 'Y-m-d' ); 187 if ( $from > $to ) { 188 return true; 189 } 190 191 $cutoff = ggsec_stats_unverified_cutoff(); 192 // Same definitions as ggsec_stats_summary(), ggsec_stats_recon_summary() and ggsec_stats_ai_referrals(). 193 $result = $wpdb->query( 194 $wpdb->prepare( 195 "REPLACE INTO %i (day, human_views, visitors, bots_known, bots_nojs, probes, downloads, recon, ai_referrals) 196 SELECT day, 197 SUM(kind = %d AND status = %d), 198 COUNT(DISTINCT CASE WHEN kind = %d AND status = %d THEN visitor END), 199 SUM(kind = %d AND status <> %d), 200 SUM(kind = %d AND status <> %d AND created_at < %s), 201 SUM(status = %d), 202 SUM(status = %d), 203 COALESCE(SUM((kind = %d AND bot_type = 'scanner') 204 OR (status IN (%d, %d) AND (kind = %d OR (kind = %d AND (status = %d OR created_at < %s))) AND path REGEXP %s)), 0), 205 COALESCE(SUM(kind = %d AND status = %d AND (ref_host REGEXP %s OR utm_source REGEXP %s)), 0) 206 FROM %i WHERE day BETWEEN %s AND %s GROUP BY day", 207 $daily, 208 GGSEC_STATS_KIND_HUMAN, 209 GGSEC_STATS_STATUS_OK, 210 GGSEC_STATS_KIND_HUMAN, 211 GGSEC_STATS_STATUS_OK, 212 GGSEC_STATS_KIND_BOT, 213 GGSEC_STATS_STATUS_PROBE, 214 GGSEC_STATS_KIND_UNVERIFIED, 215 GGSEC_STATS_STATUS_PROBE, 216 $cutoff, 217 GGSEC_STATS_STATUS_PROBE, 218 GGSEC_STATS_STATUS_DOWNLOAD, 219 GGSEC_STATS_KIND_BOT, 220 GGSEC_STATS_STATUS_404, 221 GGSEC_STATS_STATUS_PROBE, 222 GGSEC_STATS_KIND_BOT, 223 GGSEC_STATS_KIND_UNVERIFIED, 224 GGSEC_STATS_STATUS_PROBE, 225 $cutoff, 226 ggsec_stats_sensitive_path_regex(), 227 GGSEC_STATS_KIND_HUMAN, 228 GGSEC_STATS_STATUS_OK, 229 ggsec_stats_ai_referrer_regex(), 230 ggsec_stats_ai_referrer_regex(), 231 $hits, 232 $from, 233 $to 234 ) 235 ); 236 // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching 237 return false !== $result; 238 } 239 240 /** 241 * Daily maintenance: aggregate finished days, then delete raw hits past the retention period, in batches. 132 242 */ 133 243 function ggsec_stats_cleanup() { 134 244 global $wpdb; 135 $cutoff = gmdate( 'Y-m-d H:i:s', time() - ggsec_stats_retention_days() * DAY_IN_SECONDS ); 245 246 // A day is deleted only after it was aggregated. 247 if ( ! ggsec_stats_rollup() ) { 248 return; 249 } 250 251 $purged = time() - ggsec_stats_retention_days() * DAY_IN_SECONDS; 252 $cutoff = gmdate( 'Y-m-d H:i:s', $purged ); 253 update_option( 'ggsec_stats_purged_before', $purged, false ); 136 254 137 255 for ( $i = 0; $i < 50; $i++ ) { … … 158 276 __( 'Statistics data is deleted automatically after %d days.', 'ggsec-stats' ), 159 277 ggsec_stats_retention_days() 160 ) . ' </p>';278 ) . ' ' . __( 'Only daily totals (numbers of page views, visitors and bot requests, without any personal data) are kept longer.', 'ggsec-stats' ) . '</p>'; 161 279 wp_add_privacy_policy_content( 'GGSEC Stats', wp_kses_post( $content ) ); 162 280 } -
ggsec-stats/tags/1.5.0/includes/admin.php
r3830097 r3835790 37 37 38 38 /** 39 * Optional UTC upper bound on created_at; null means no bound (the range is limited by day only). 40 */ 41 function ggsec_stats_before_utc( $before = null ) { 42 return null === $before ? '9999-12-31 23:59:59' : (string) $before; 43 } 44 45 /** 39 46 * Unverified hits created before this UTC time never got a beacon and are reported as bots. 40 47 */ … … 57 64 * Headline numbers for a range. 58 65 * 66 * @param string $since First local day (Y-m-d). 67 * @param string|null $until Last local day; default today. 68 * @param string|null $before Only hits created before this UTC time (previous-period comparison up to the same clock time). 59 69 * @return array<string,int> 60 70 */ 61 function ggsec_stats_summary( $since, $until = null ) {71 function ggsec_stats_summary( $since, $until = null, $before = null ) { 62 72 global $wpdb; 63 73 $row = $wpdb->get_row( … … 70 80 SUM(status = %d) AS probes, 71 81 SUM(status = %d) AS downloads 72 FROM %i WHERE day BETWEEN %s AND %s ',82 FROM %i WHERE day BETWEEN %s AND %s AND created_at < %s', 73 83 GGSEC_STATS_KIND_HUMAN, 74 84 GGSEC_STATS_STATUS_OK, … … 84 94 ggsec_stats_table(), 85 95 $since, 86 ggsec_stats_until_day( $until ) 96 ggsec_stats_until_day( $until ), 97 ggsec_stats_before_utc( $before ) 87 98 ), 88 99 ARRAY_A … … 148 159 } 149 160 return $series; 161 } 162 163 /** 164 * Humans and bots per month for the last 12 months (current month included, missing months zero): 165 * aggregated days from the daily table, days not aggregated yet (today, ...) from the raw hits. 166 * 167 * @return array<string,array{h:int,b:int}> Keyed by "Y-m". 168 */ 169 function ggsec_stats_monthly_series() { 170 global $wpdb; 171 $tz = wp_timezone(); 172 $month = new DateTimeImmutable( 'first day of this month', $tz ); 173 $start = $month->modify( '-11 months' ); 174 175 $series = array(); 176 for ( $i = 0; $i < 12; $i++ ) { 177 $series[ $start->modify( '+' . $i . ' months' )->format( 'Y-m' ) ] = array( 'h' => 0, 'b' => 0 ); 178 } 179 180 $daily = ggsec_stats_daily_table(); 181 $last = (string) $wpdb->get_var( $wpdb->prepare( 'SELECT MAX(day) FROM %i', $daily ) ); 182 $rows = $wpdb->get_results( 183 $wpdb->prepare( 184 'SELECT LEFT(day, 7) AS m, SUM(human_views) AS h, SUM(bots_known + bots_nojs) AS b FROM %i WHERE day >= %s GROUP BY m', 185 $daily, 186 $start->format( 'Y-m-d' ) 187 ), 188 ARRAY_A 189 ); 190 $rows = array_merge( 191 $rows, 192 $wpdb->get_results( 193 $wpdb->prepare( 194 'SELECT LEFT(day, 7) AS m, 195 SUM(kind = %d AND status = %d) AS h, 196 SUM(status <> %d AND (kind = %d OR (kind = %d AND created_at < %s))) AS b 197 FROM %i WHERE day > %s AND day >= %s GROUP BY m', 198 GGSEC_STATS_KIND_HUMAN, 199 GGSEC_STATS_STATUS_OK, 200 GGSEC_STATS_STATUS_PROBE, 201 GGSEC_STATS_KIND_BOT, 202 GGSEC_STATS_KIND_UNVERIFIED, 203 ggsec_stats_unverified_cutoff(), 204 ggsec_stats_table(), 205 '' !== $last ? $last : '0000-00-00', 206 $start->format( 'Y-m-d' ) 207 ), 208 ARRAY_A 209 ) 210 ); 211 foreach ( $rows as $r ) { 212 if ( isset( $series[ $r['m'] ] ) ) { 213 $series[ $r['m'] ]['h'] += (int) $r['h']; 214 $series[ $r['m'] ]['b'] += (int) $r['b']; 215 } 216 } 217 return $series; 218 } 219 220 /** 221 * First day of the long-term history (daily table or raw hits), or '' without data. 222 */ 223 function ggsec_stats_history_start() { 224 global $wpdb; 225 $daily = (string) $wpdb->get_var( $wpdb->prepare( 'SELECT MIN(day) FROM %i', ggsec_stats_daily_table() ) ); 226 $raw = ggsec_stats_first_day(); 227 if ( '' === $daily || '' === $raw ) { 228 return $daily . $raw; 229 } 230 return min( $daily, $raw ); 150 231 } 151 232 … … 293 374 * @return array{scanner:int,sensitive:int,total:int} 294 375 */ 295 function ggsec_stats_recon_summary( $since, $until = null ) {376 function ggsec_stats_recon_summary( $since, $until = null, $before = null ) { 296 377 global $wpdb; 297 378 $row = $wpdb->get_row( … … 302 383 SELECT (kind = %d AND bot_type = 'scanner') AS is_scanner, 303 384 (status IN (%d, %d) AND (kind = %d OR (kind = %d AND (status = %d OR created_at < %s))) AND path REGEXP %s) AS is_sensitive 304 FROM %i WHERE day BETWEEN %s AND %s 385 FROM %i WHERE day BETWEEN %s AND %s AND created_at < %s 305 386 ) AS recon", 306 387 GGSEC_STATS_KIND_BOT, … … 314 395 ggsec_stats_table(), 315 396 $since, 316 ggsec_stats_until_day( $until ) 397 ggsec_stats_until_day( $until ), 398 ggsec_stats_before_utc( $before ) 317 399 ), 318 400 ARRAY_A … … 346 428 347 429 /** 430 * Human page views that arrived from AI assistants (by referrer host or utm_source). 431 * 432 * @return array{total:int,assistants:array<string,array{views:int,visitors:int}>,pages:array} 433 */ 434 function ggsec_stats_ai_referrals( $since, $until = null, $before = null ) { 435 global $wpdb; 436 $table = ggsec_stats_table(); 437 $regex = ggsec_stats_ai_referrer_regex(); 438 439 // Source = the referrer host if it is an assistant, otherwise the utm_source tag. 440 $sources = $wpdb->get_results( 441 $wpdb->prepare( 442 'SELECT IF(ref_host REGEXP %s, ref_host, utm_source) AS source, COUNT(*) AS views, 443 COUNT(DISTINCT CONCAT(day, visitor)) AS visitors 444 FROM %i WHERE day BETWEEN %s AND %s AND created_at < %s AND kind = %d AND status = %d 445 AND (ref_host REGEXP %s OR utm_source REGEXP %s) 446 GROUP BY source', 447 $regex, 448 $table, 449 $since, 450 ggsec_stats_until_day( $until ), 451 ggsec_stats_before_utc( $before ), 452 GGSEC_STATS_KIND_HUMAN, 453 GGSEC_STATS_STATUS_OK, 454 $regex, 455 $regex 456 ), 457 ARRAY_A 458 ); 459 460 $total = 0; 461 $assistants = array(); 462 foreach ( $sources as $r ) { 463 $name = ggsec_stats_ai_assistant_name( $r['source'] ); 464 if ( null === $name ) { 465 continue; 466 } 467 if ( ! isset( $assistants[ $name ] ) ) { 468 $assistants[ $name ] = array( 469 'views' => 0, 470 'visitors' => 0, 471 ); 472 } 473 $assistants[ $name ]['views'] += (int) $r['views']; 474 $assistants[ $name ]['visitors'] += (int) $r['visitors']; 475 $total += (int) $r['views']; 476 } 477 uasort( 478 $assistants, 479 function ( $a, $b ) { 480 return $b['views'] <=> $a['views']; 481 } 482 ); 483 484 $pages = $total ? $wpdb->get_results( 485 $wpdb->prepare( 486 'SELECT path, MAX(post_id) AS post_id, COUNT(*) AS views 487 FROM %i WHERE day BETWEEN %s AND %s AND created_at < %s AND kind = %d AND status = %d 488 AND (ref_host REGEXP %s OR utm_source REGEXP %s) 489 GROUP BY path ORDER BY views DESC LIMIT 10', 490 $table, 491 $since, 492 ggsec_stats_until_day( $until ), 493 ggsec_stats_before_utc( $before ), 494 GGSEC_STATS_KIND_HUMAN, 495 GGSEC_STATS_STATUS_OK, 496 $regex, 497 $regex 498 ), 499 ARRAY_A 500 ) : array(); 501 502 return array( 503 'total' => $total, 504 'assistants' => $assistants, 505 'pages' => $pages, 506 ); 507 } 508 509 /** 510 * First local day with recorded hits (Y-m-d), or '' when the table is empty. 511 */ 512 function ggsec_stats_first_day() { 513 global $wpdb; 514 return (string) $wpdb->get_var( $wpdb->prepare( 'SELECT MIN(day) FROM %i', ggsec_stats_table() ) ); 515 } 516 517 /** 348 518 * File downloads clicked by real visitors (PDF reports, archives, ...). 349 519 */ … … 432 602 * Stacked bar chart (humans bottom, bots top) as inline SVG; no JavaScript library. 433 603 * 434 * @param array<string,array{h:int,b:int}> $series 435 */ 436 function ggsec_stats_chart_svg( $series ) { 604 * @param array<string,array{h:int,b:int}> $series Keyed by day (Y-m-d) or month (Y-m). 605 * @param string $label Accessible chart description. 606 */ 607 function ggsec_stats_chart_svg( $series, $label = '' ) { 437 608 $w = 760; 438 609 $h = 200; … … 448 619 $bar_w = max( 2, $slot * 0.7 ); 449 620 450 $svg = '<svg class="ggsec-stats-chart" viewBox="0 0 ' . $w . ' ' . $h . '" role="img" aria-label="' . esc_attr__( 'Humans and bots per day', 'ggsec-stats' ) . '">'; 621 $label = '' !== $label ? $label : __( 'Humans and bots per day', 'ggsec-stats' ); 622 $every = $n <= 12 ? 1 : 5; 623 624 $svg = '<svg class="ggsec-stats-chart" viewBox="0 0 ' . $w . ' ' . $h . '" role="img" aria-label="' . esc_attr( $label ) . '">'; 451 625 $svg .= '<line x1="' . $pad_l . '" y1="' . ( 8 + $plot_h ) . '" x2="' . $w . '" y2="' . ( 8 + $plot_h ) . '" class="axis"/>'; 452 626 $svg .= '<text x="' . ( $pad_l - 6 ) . '" y="14" class="lbl" text-anchor="end">' . esc_html( number_format_i18n( $max ) ) . '</text>'; … … 471 645 $svg .= sprintf( '<rect class="b" x="%.1f" y="%.1f" width="%.1f" height="%.1f"/>', $x, $y0 - $hh - $bh, $bar_w, $bh ); 472 646 $svg .= '</g>'; 473 if ( 0 === $i % 5 || $i === $n - 1 ) { 474 $svg .= sprintf( '<text class="lbl" x="%.1f" y="%d" text-anchor="middle">%s</text>', $x + $bar_w / 2, $h - 6, esc_html( substr( $day, 5 ) ) ); 647 if ( 0 === $i % $every || $i === $n - 1 ) { 648 // Days as "11-27", months as the short month name. 649 $text = 7 === strlen( $day ) ? wp_date( 'M', ( new DateTimeImmutable( $day . '-01', wp_timezone() ) )->getTimestamp() ) : substr( $day, 5 ); 650 $svg .= sprintf( '<text class="lbl" x="%.1f" y="%d" text-anchor="middle">%s</text>', $x + $bar_w / 2, $h - 6, esc_html( $text ) ); 475 651 } 476 652 $i++; … … 499 675 500 676 /** 677 * Change indicator for a card: [text, tone, tooltip] or null when there is nothing to compare. 678 * 679 * @param int $current Value in the shown period. 680 * @param int|null $previous Value in the previous period; null when the previous period has no data yet. 681 * @param string $rise Tone of an increase: 'good', 'bad' or 'neutral'. 682 * @param string $tooltip What the value is compared with. 683 * @return array{0:string,1:string,2:string}|null 684 */ 685 function ggsec_stats_change( $current, $previous, $rise, $tooltip ) { 686 if ( null === $previous ) { 687 return null; 688 } 689 $text = ggsec_stats_mail_change( (int) $current, (int) $previous ); 690 if ( '' === $text ) { 691 return null; 692 } 693 $fall = array( 694 'good' => 'bad', 695 'bad' => 'good', 696 'neutral' => 'neutral', 697 ); 698 if ( (int) $current === (int) $previous ) { 699 $tone = 'neutral'; 700 } else { 701 $tone = $current > $previous ? $rise : ( $fall[ $rise ] ?? 'neutral' ); 702 } 703 return array( $text, $tone, $tooltip ); 704 } 705 706 /** 501 707 * One headline card. $value is a number or a short preformatted string (e.g. "42%"). 502 */ 503 function ggsec_stats_card( $label, $value, $hint = '' ) { 504 echo '<div class="ggsec-stats-card"><span class="v">' . esc_html( is_int( $value ) ? number_format_i18n( $value ) : (string) $value ) . '</span>'; 708 * 709 * @param array{0:string,1:string,2:string}|null $change Indicator from ggsec_stats_change(). 710 */ 711 function ggsec_stats_card( $label, $value, $hint = '', $change = null ) { 712 echo '<div class="ggsec-stats-card"><span class="v">' . esc_html( is_int( $value ) ? number_format_i18n( $value ) : (string) $value ); 713 if ( $change ) { 714 echo ' <span class="chg ' . esc_attr( $change[1] ) . '" title="' . esc_attr( $change[2] ) . '">' . esc_html( $change[0] ) . '</span>'; 715 } 716 echo '</span>'; 505 717 echo '<span class="l">' . esc_html( $label ) . '</span>'; 506 718 if ( '' !== $hint ) { … … 551 763 $range = 'today'; 552 764 } 553 $since = ggsec_stats_since_day( $ranges[ $range ][0] ); 765 $days = $ranges[ $range ][0]; 766 $since = ggsec_stats_since_day( $days ); 554 767 555 768 $sum = ggsec_stats_summary( $since ); … … 560 773 $recon = ggsec_stats_recon_summary( $since ); 561 774 $downloads = ggsec_stats_downloads( $since ); 775 $ai_refs = ggsec_stats_ai_referrals( $since ); 776 777 // Previous period: the same number of days before, up to the same clock time (today 10:00 vs yesterday 10:00). 778 // Compared only when it lies completely within the recorded data. 779 $prev_since = ggsec_stats_since_day( 2 * $days ); 780 $first_day = ggsec_stats_first_day(); 781 $prev = array(); 782 if ( '' !== $first_day && $prev_since >= $first_day ) { 783 $prev_until = ( new DateTimeImmutable( 'today', wp_timezone() ) )->modify( '-' . $days . ' days' )->format( 'Y-m-d' ); 784 $before = gmdate( 'Y-m-d H:i:s', time() - $days * DAY_IN_SECONDS ); 785 $prev = ggsec_stats_summary( $prev_since, $prev_until, $before ); 786 $prev_recon = ggsec_stats_recon_summary( $prev_since, $prev_until, $before ); 787 $prev_ai = ggsec_stats_ai_referrals( $prev_since, $prev_until, $before ); 788 789 $prev['recon'] = $prev_recon['total']; 790 $prev['ai'] = $prev_ai['total']; 791 } 792 $tooltip = 1 === $days 793 ? __( 'Compared with yesterday up to the same time', 'ggsec-stats' ) 794 /* translators: %d: number of days in the compared period. */ 795 : sprintf( __( 'Compared with the previous %d days up to the same time', 'ggsec-stats' ), $days ); 796 $change = function ( $key, $current, $rise ) use ( $prev, $tooltip ) { 797 return ggsec_stats_change( $current, $prev[ $key ] ?? null, $rise, $tooltip ); 798 }; 562 799 ?> 563 800 <div class="wrap ggsec-stats"> … … 573 810 <div class="ggsec-stats-cards"> 574 811 <?php 575 ggsec_stats_card( __( 'Human page views', 'ggsec-stats' ), $sum['human_views'] ); 576 ggsec_stats_card( __( 'Unique visitors', 'ggsec-stats' ), $sum['visitors'], __( 'per day, summed', 'ggsec-stats' ) ); 577 ggsec_stats_card( __( 'Known bots', 'ggsec-stats' ), $sum['bots_known'], __( 'identified by user agent', 'ggsec-stats' ) ); 578 ggsec_stats_card( __( 'Bots without JS', 'ggsec-stats' ), $sum['bots_nojs'], __( 'browser-like, never rendered the page', 'ggsec-stats' ) ); 812 ggsec_stats_card( __( 'Human page views', 'ggsec-stats' ), $sum['human_views'], '', $change( 'human_views', $sum['human_views'], 'good' ) ); 813 ggsec_stats_card( __( 'Unique visitors', 'ggsec-stats' ), $sum['visitors'], __( 'per day, summed', 'ggsec-stats' ), $change( 'visitors', $sum['visitors'], 'good' ) ); 814 ggsec_stats_card( __( 'From AI assistants', 'ggsec-stats' ), $ai_refs['total'], __( 'human views via ChatGPT, Perplexity, Claude, ...', 'ggsec-stats' ), $change( 'ai', $ai_refs['total'], 'good' ) ); 815 ggsec_stats_card( __( 'Known bots', 'ggsec-stats' ), $sum['bots_known'], __( 'identified by user agent', 'ggsec-stats' ), $change( 'bots_known', $sum['bots_known'], 'neutral' ) ); 816 ggsec_stats_card( __( 'Bots without JS', 'ggsec-stats' ), $sum['bots_nojs'], __( 'browser-like, never rendered the page', 'ggsec-stats' ), $change( 'bots_nojs', $sum['bots_nojs'], 'neutral' ) ); 579 817 ggsec_stats_card( __( 'Bot share', 'ggsec-stats' ), $bot_share, __( 'of all page requests', 'ggsec-stats' ) ); 580 ggsec_stats_card( __( 'Login/XML-RPC probes', 'ggsec-stats' ), $sum['probes'], __( 'failed logins, bot login scans, XML-RPC', 'ggsec-stats' ) );818 ggsec_stats_card( __( 'Login/XML-RPC probes', 'ggsec-stats' ), $sum['probes'], __( 'failed logins, bot login scans, XML-RPC', 'ggsec-stats' ), $change( 'probes', $sum['probes'], 'bad' ) ); 581 819 ggsec_stats_card( __( 'Online now', 'ggsec-stats' ), ggsec_stats_online_now(), __( 'humans, last 5 minutes', 'ggsec-stats' ) ); 582 ggsec_stats_card( __( 'Downloads', 'ggsec-stats' ), $sum['downloads'], __( 'file links clicked by visitors', 'ggsec-stats' ) );820 ggsec_stats_card( __( 'Downloads', 'ggsec-stats' ), $sum['downloads'], __( 'file links clicked by visitors', 'ggsec-stats' ), $change( 'downloads', $sum['downloads'], 'good' ) ); 583 821 ggsec_stats_card( 584 822 __( 'Security recon', 'ggsec-stats' ), … … 589 827 number_format_i18n( $recon['scanner'] ), 590 828 number_format_i18n( $recon['sensitive'] ) 591 ) 829 ), 830 $change( 'recon', $recon['total'], 'bad' ) 592 831 ); 593 832 ?> … … 599 838 </h2> 600 839 <?php echo wp_kses( ggsec_stats_chart_svg( ggsec_stats_daily_series( 30 ) ), ggsec_stats_allowed_svg() ); ?> 840 841 <?php 842 $history_start = ggsec_stats_history_start(); 843 if ( '' !== $history_start ) : 844 ?> 845 <h2> 846 <?php esc_html_e( 'Last 12 months', 'ggsec-stats' ); ?> 847 <span class="ggsec-stats-legend"><i class="h"></i> <?php esc_html_e( 'humans', 'ggsec-stats' ); ?> <i class="b"></i> <?php esc_html_e( 'bots', 'ggsec-stats' ); ?></span> 848 </h2> 849 <?php echo wp_kses( ggsec_stats_chart_svg( ggsec_stats_monthly_series(), __( 'Humans and bots per month', 'ggsec-stats' ) ), ggsec_stats_allowed_svg() ); ?> 850 <p class="description"> 851 <?php 852 echo esc_html( 853 sprintf( 854 /* translators: %s: date the statistics history starts. */ 855 __( 'History since %s. Daily totals are kept after the raw data is deleted, so this chart keeps growing.', 'ggsec-stats' ), 856 ggsec_stats_mail_date( $history_start ) 857 ) 858 ); 859 ?> 860 </p> 861 <?php endif; ?> 601 862 602 863 <div class="ggsec-stats-grid"> … … 721 982 722 983 <section> 984 <h2><?php esc_html_e( 'Visitors from AI assistants', 'ggsec-stats' ); ?></h2> 985 <?php 986 $rows = array(); 987 foreach ( $ai_refs['assistants'] as $name => $r ) { 988 $rows[] = array( esc_html( $name ), ggsec_stats_num( $r['views'] ), ggsec_stats_num( $r['visitors'] ) ); 989 } 990 ggsec_stats_table_html( array( __( 'Assistant', 'ggsec-stats' ), __( 'Views', 'ggsec-stats' ), __( 'Visitors', 'ggsec-stats' ) ), $rows ); 991 if ( $ai_refs['pages'] ) : 992 ?> 993 <h3><?php esc_html_e( 'Landing pages', 'ggsec-stats' ); ?></h3> 994 <?php 995 $rows = array(); 996 foreach ( $ai_refs['pages'] as $r ) { 997 $title = $r['post_id'] ? get_the_title( (int) $r['post_id'] ) : ''; 998 $rows[] = array( 999 esc_html( $r['path'] ) . ( '' !== $title ? '<br><small>' . esc_html( $title ) . '</small>' : '' ), 1000 ggsec_stats_num( $r['views'] ), 1001 ); 1002 } 1003 ggsec_stats_table_html( array( __( 'Page', 'ggsec-stats' ), __( 'Views', 'ggsec-stats' ) ), $rows ); 1004 endif; 1005 ?> 1006 <p class="description"><?php esc_html_e( 'Real visitors who followed a link in an AI assistant answer (referrer or utm_source of ChatGPT, Perplexity, Claude, Copilot, Gemini and others). AI crawlers fetching pages are listed separately above.', 'ggsec-stats' ); ?></p> 1007 </section> 1008 1009 <section> 723 1010 <h2><?php esc_html_e( 'Downloads', 'ggsec-stats' ); ?></h2> 724 1011 <?php -
ggsec-stats/tags/1.5.0/includes/bots.php
r3824418 r3835790 205 205 } 206 206 207 /** 208 * AI assistants that send visitors to the site: referrer host (without "www.") => display name. 209 * The same hosts are matched against utm_source, because ChatGPT tags its links with utm_source=chatgpt.com 210 * and its apps often send no referrer. 211 * 212 * @return array<string,string> 213 */ 214 function ggsec_stats_ai_referrers() { 215 $hosts = array( 216 'chatgpt.com' => 'ChatGPT', 217 'chat.openai.com' => 'ChatGPT', 218 'perplexity.ai' => 'Perplexity', 219 'claude.ai' => 'Claude', 220 'copilot.microsoft.com' => 'Microsoft Copilot', 221 'gemini.google.com' => 'Google Gemini', 222 'chat.mistral.ai' => 'Mistral Le Chat', 223 'chat.deepseek.com' => 'DeepSeek', 224 'meta.ai' => 'Meta AI', 225 'grok.com' => 'Grok', 226 'you.com' => 'You.com', 227 'phind.com' => 'Phind', 228 'poe.com' => 'Poe', 229 ); 230 231 /** 232 * Filter the AI assistant referrer list (host without "www." => display name). 233 */ 234 $hosts = (array) apply_filters( 'ggsec_stats_ai_referrers', $hosts ); 235 236 // Hosts end up in a regular expression; only plain host names are accepted. 237 $clean = array(); 238 foreach ( $hosts as $host => $name ) { 239 $host = strtolower( (string) $host ); 240 if ( preg_match( '/^[a-z0-9-]+(\.[a-z0-9-]+)+$/', $host ) ) { 241 $clean[ $host ] = (string) $name; 242 } 243 } 244 return $clean; 245 } 246 247 /** 248 * Regular expression matching a referrer host or utm_source of an AI assistant, for MySQL/MariaDB REGEXP 249 * (whole value, optional "www."). Matches nothing when the list is empty. 250 */ 251 function ggsec_stats_ai_referrer_regex() { 252 $hosts = array_map( 253 function ( $host ) { 254 return str_replace( '.', '\.', $host ); 255 }, 256 array_keys( ggsec_stats_ai_referrers() ) 257 ); 258 return $hosts ? '^(www\.)?(' . implode( '|', $hosts ) . ')$' : '^$.'; 259 } 260 261 /** 262 * Assistant name for a referrer host or utm_source value, or null if it is not an AI assistant. 263 */ 264 function ggsec_stats_ai_assistant_name( $value ) { 265 $value = preg_replace( '/^www\./', '', strtolower( trim( (string) $value ) ) ); 266 $list = ggsec_stats_ai_referrers(); 267 return $list[ $value ] ?? null; 268 } 269 207 270 function ggsec_stats_bot_type_label( $type ) { 208 271 $labels = array( -
ggsec-stats/tags/1.5.0/includes/notify.php
r3830097 r3835790 368 368 $total = $sum['human_views'] + $sum['bots']; 369 369 370 // No comparison while the previous period starts before the first recorded day (fresh install): 371 // every number would show "new". 372 $first_day = ggsec_stats_first_day(); 373 $compare = '' === $first_day || $prev_from >= $first_day; 374 $change = function ( $current, $previous ) use ( $compare ) { 375 return $compare ? ggsec_stats_mail_change( $current, $previous ) : ''; 376 }; 377 370 378 $range = $from === $to ? ggsec_stats_mail_date( $from ) : ggsec_stats_mail_date( $from ) . ' – ' . ggsec_stats_mail_date( $to ); 371 379 if ( 'daily' === $frequency ) { 372 380 /* translators: %s: report date. */ 373 381 $title = sprintf( __( 'Daily report: %s', 'ggsec-stats' ), $range ); 374 $intro = __( 'Change compared with the day before.', 'ggsec-stats' ); 382 $intro = $compare 383 ? __( 'Change compared with the day before.', 'ggsec-stats' ) 384 /* translators: %s: date of the first recorded day. */ 385 : sprintf( __( 'No comparison with the day before: statistics start on %s.', 'ggsec-stats' ), ggsec_stats_mail_date( $first_day ) ); 375 386 } else { 376 387 /* translators: %s: report date range. */ 377 388 $title = sprintf( __( 'Weekly report: %s', 'ggsec-stats' ), $range ); 378 $intro = __( 'Change compared with the week before.', 'ggsec-stats' ); 389 $intro = $compare 390 ? __( 'Change compared with the week before.', 'ggsec-stats' ) 391 /* translators: %s: date of the first recorded day. */ 392 : sprintf( __( 'No comparison with the week before: statistics start on %s.', 'ggsec-stats' ), ggsec_stats_mail_date( $first_day ) ); 379 393 } 380 394 381 395 $body = ggsec_stats_mail_cards( 382 396 array( 383 array( number_format_i18n( $sum['human_views'] ), __( 'Human page views', 'ggsec-stats' ), ggsec_stats_mail_change( $sum['human_views'], $prev['human_views'] ) ),384 array( number_format_i18n( $sum['visitors'] ), __( 'Unique visitors (per day, summed)', 'ggsec-stats' ), ggsec_stats_mail_change( $sum['visitors'], $prev['visitors'] ) ),385 array( number_format_i18n( $sum['bots'] ), __( 'Bot requests', 'ggsec-stats' ), ggsec_stats_mail_change( $sum['bots'], $prev['bots'] ) ),397 array( number_format_i18n( $sum['human_views'] ), __( 'Human page views', 'ggsec-stats' ), $change( $sum['human_views'], $prev['human_views'] ) ), 398 array( number_format_i18n( $sum['visitors'] ), __( 'Unique visitors (per day, summed)', 'ggsec-stats' ), $change( $sum['visitors'], $prev['visitors'] ) ), 399 array( number_format_i18n( $sum['bots'] ), __( 'Bot requests', 'ggsec-stats' ), $change( $sum['bots'], $prev['bots'] ) ), 386 400 array( $total ? number_format_i18n( round( 100 * $sum['bots'] / $total ) ) . '%' : '-', __( 'Bot share of all page requests', 'ggsec-stats' ), '' ), 387 array( number_format_i18n( $sum['probes'] ), __( 'Login/XML-RPC probes', 'ggsec-stats' ), ggsec_stats_mail_change( $sum['probes'], $prev['probes'] ) ),388 array( number_format_i18n( $recon['total'] ), __( 'Security recon requests', 'ggsec-stats' ), ggsec_stats_mail_change( $recon['total'], $prev_recon['total'] ) ),401 array( number_format_i18n( $sum['probes'] ), __( 'Login/XML-RPC probes', 'ggsec-stats' ), $change( $sum['probes'], $prev['probes'] ) ), 402 array( number_format_i18n( $recon['total'] ), __( 'Security recon requests', 'ggsec-stats' ), $change( $recon['total'], $prev_recon['total'] ) ), 389 403 ) 390 404 ); … … 405 419 $body .= ggsec_stats_mail_heading( __( 'Top referrers', 'ggsec-stats' ) ) 406 420 . ggsec_stats_mail_table( array( __( 'Source site', 'ggsec-stats' ), __( 'Views', 'ggsec-stats' ) ), $rows ); 421 422 // Shown only when there were such visits, to keep the report short for sites without them. 423 $ai_refs = ggsec_stats_ai_referrals( $from, $to ); 424 if ( $ai_refs['total'] ) { 425 $prev_ai = ggsec_stats_ai_referrals( $prev_from, $prev_to ); 426 $rows = array(); 427 foreach ( array_slice( $ai_refs['assistants'], 0, 5, true ) as $name => $r ) { 428 $rows[] = array( $name, number_format_i18n( $r['views'] ), number_format_i18n( $r['visitors'] ) ); 429 } 430 $ai_chg = $change( $ai_refs['total'], $prev_ai['total'] ); 431 $body .= ggsec_stats_mail_heading( 432 trim( 433 sprintf( 434 /* translators: 1: number of human page views from AI assistants, 2: change vs previous period (may be empty). */ 435 __( 'Visitors from AI assistants: %1$s %2$s', 'ggsec-stats' ), 436 number_format_i18n( $ai_refs['total'] ), 437 '' !== $ai_chg ? '(' . $ai_chg . ')' : '' 438 ) 439 ) 440 ) . ggsec_stats_mail_table( array( __( 'Assistant', 'ggsec-stats' ), __( 'Views', 'ggsec-stats' ), __( 'Visitors', 'ggsec-stats' ) ), $rows, 1 ); 441 } 407 442 408 443 $bots = ggsec_stats_bot_breakdown( $from, $to ); -
ggsec-stats/tags/1.5.0/readme.txt
r3830097 r3835790 5 5 Tested up to: 7.1 6 6 Requires PHP: 7.4 7 Stable tag: 1. 4.07 Stable tag: 1.5.0 8 8 License: GPLv2 or later 9 9 License URI: https://www.gnu.org/licenses/gpl-2.0.html … … 20 20 21 21 * **Humans vs. bots** - human page views, unique visitors, known bots, browser-like clients that never ran JavaScript, bot share and visitors online now. 22 * **Change indicators** - every headline number shows its change against the previous period, compared up to the same time of day. 22 23 * **30-day chart** of humans and bots, rendered as inline SVG (no JavaScript chart library). 24 * **12-month history** - daily totals are kept after the raw data is deleted, for a monthly chart of the last 12 months. 23 25 * **Top pages** and **referrers** (source sites, full referring URLs without query strings, `utm_source` / `ref` campaign tags). 24 26 * **Bot breakdown** by name and type: search engines, AI crawlers, SEO tools, social previews, monitoring services, security scanners and HTTP libraries. 25 27 * **Pages read by AI crawlers** (GPTBot, ClaudeBot, PerplexityBot and others). 28 * **Visitors from AI assistants** - real visitors who followed a link in ChatGPT, Perplexity, Claude, Copilot, Gemini and other assistants, with their landing pages. 26 29 * **Security reconnaissance** - failed logins, bot scans of the login page, XML-RPC requests, 404 requests from non-humans and requests for sensitive paths such as `.env`, `.git` or backup archives. 27 30 * **Broken links** - 404 pages opened by real visitors, with the referring site. … … 39 42 * Query strings are removed from recorded paths and referrers. 40 43 * The user agent of a confirmed human visit is erased; it is kept only for bots and unconfirmed requests. 41 * Raw data is deleted automatically after 90 days. 44 * Raw data is deleted automatically after 90 days. Only daily totals (numbers, no personal data) are kept for the long-term history. 42 45 * Logged-in editors and administrators are not counted. 43 46 … … 78 81 Only if you enable it under **Dashboard > GGSEC Stats > E-mail reports and alerts**; reports and alerts are off by default. Mails are sent with your site's own mail setup (`wp_mail`) to the administration e-mail address or a recipient you choose. They contain aggregate numbers, paths, bot names and user agents only, no IP addresses. Alerts are checked hourly by WP-Cron and sent at most once every 6 hours per kind. 79 82 83 = How are visitors from AI assistants detected? = 84 85 By the referring site (chatgpt.com, perplexity.ai, claude.ai, copilot.microsoft.com, gemini.google.com and others) or by the `utm_source` tag that ChatGPT adds to its links. Only page views confirmed as human are counted. The list can be changed with the `ggsec_stats_ai_referrers` filter. 86 87 = How long is the history kept? = 88 89 Raw hits are deleted after 90 days (or `GGSEC_STATS_RETENTION_DAYS`). Before that, each finished day is stored as one row of totals (human views, visitors, bots, probes, downloads, security recon, AI referrals), which is kept until the plugin is deleted. Existing data is aggregated automatically when you update to 1.5.0. 90 80 91 = What happens on uninstall? = 81 92 82 Deleting the plugin from the Plugins screen drops its database table and removes its options and scheduled events.93 Deleting the plugin from the Plugins screen drops its database tables and removes its options and scheduled events. 83 94 84 95 == Screenshots == … … 90 101 91 102 == Changelog == 103 104 = 1.5.0 = 105 * Change indicators on the headline cards, compared with the previous period up to the same time of day. 106 * Visitors from AI assistants (ChatGPT, Perplexity, Claude, Copilot, Gemini and others): card, table with landing pages and a section in the e-mail report. 107 * Long-term history: daily totals are kept after raw data cleanup; new chart of the last 12 months. 92 108 93 109 = 1.4.0 = … … 119 135 == Upgrade Notice == 120 136 137 = 1.5.0 = 138 Change indicators, visitors from AI assistants and a 12-month history chart. Existing data is aggregated on update. 139 121 140 = 1.4.0 = 122 141 Optional e-mail reports and security alerts. Off by default; enable them under Dashboard > GGSEC Stats. -
ggsec-stats/tags/1.5.0/uninstall.php
r3830097 r3835790 11 11 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange -- removes the plugin's own table on uninstall. 12 12 $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $wpdb->prefix . 'ggsec_stats_hits' ) ); 13 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange -- removes the plugin's own table on uninstall. 14 $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $wpdb->prefix . 'ggsec_stats_daily' ) ); 13 15 14 16 delete_option( 'ggsec_stats_db_version' ); … … 17 19 delete_option( 'ggsec_stats_notify' ); 18 20 delete_option( 'ggsec_stats_notify_state' ); 21 delete_option( 'ggsec_stats_purged_before' ); 19 22 wp_clear_scheduled_hook( 'ggsec_stats_daily' ); 20 23 wp_clear_scheduled_hook( 'ggsec_stats_hourly' ); -
ggsec-stats/trunk/README.md
r3830097 r3835790 19 19 - Dedicated **Pages read by AI crawlers** table showing which content is being fetched by AI models. 20 20 - Separate counter and User-Agent breakdown for browser-like clients that never execute JavaScript. 21 - **Visitors from AI assistants:** confirmed human views referred by ChatGPT, Perplexity, Claude, Copilot, Gemini and others (referrer host or `utm_source`; list filterable via `ggsec_stats_ai_referrers`). 21 22 - **Security Reconnaissance & Broken Links:** 22 23 - Tracks failed login attempts (`wp_login_failed`), bot login discovery scans (`wp-login.php`), and `xmlrpc.php` probes. … … 33 34 - **Zero Admin Dependencies:** 34 35 - Renders the 30-day stacked bar chart (humans vs. bots) as inline SVG in PHP without external JavaScript charting libraries. 36 - Headline cards show the change against the previous period, cut at the same time of day. 37 - **Long-term History:** 38 - The daily cron aggregates each finished day into `{$wpdb->prefix}ggsec_stats_daily` (counts only, no personal data) before raw hits past the retention period are deleted; a day is never deleted before it is aggregated. 39 - Monthly chart of the last 12 months; existing raw data is backfilled on update. 35 40 - Includes a compact WordPress Dashboard widget. 36 41 … … 58 63 ## Uninstall 59 64 60 When deleted through the WordPress Plugins screen, `uninstall.php` drops the `{$wpdb->prefix}ggsec_stats_hits` table, removes plugin options (`ggsec_stats_db_version`, `ggsec_stats_salt`, `ggsec_stats_salt_day`, `ggsec_stats_notify`, `ggsec_stats_notify_state`), and clears the daily cleanup and hourly notification cron hooks.65 When deleted through the WordPress Plugins screen, `uninstall.php` drops the `{$wpdb->prefix}ggsec_stats_hits` and `{$wpdb->prefix}ggsec_stats_daily` tables, removes plugin options (`ggsec_stats_db_version`, `ggsec_stats_salt`, `ggsec_stats_salt_day`, `ggsec_stats_notify`, `ggsec_stats_notify_state`, `ggsec_stats_purged_before`), and clears the daily cleanup and hourly notification cron hooks. 61 66 62 67 ## License -
ggsec-stats/trunk/assets/admin.css
r3830097 r3835790 28 28 color: #8c8f94; 29 29 font-size: 11px; 30 } 31 .ggsec-stats-card .chg { 32 font-size: 12px; 33 font-weight: 600; 34 white-space: nowrap; 35 cursor: help; 36 } 37 .ggsec-stats-card .chg.good { 38 color: #008a20; 39 } 40 .ggsec-stats-card .chg.bad { 41 color: #d63638; 42 } 43 .ggsec-stats-card .chg.neutral { 44 color: #646970; 30 45 } 31 46 .ggsec-stats-chart { -
ggsec-stats/trunk/ggsec-stats.php
r3830097 r3835790 4 4 * Plugin URI: https://github.com/ggsec-de/ggsec-stats 5 5 * Description: Privacy-first page view statistics: humans vs bots, referrers, top pages, online now. No cookies, no external services. 6 * Version: 1. 4.06 * Version: 1.5.0 7 7 * Requires at least: 6.3 8 8 * Requires PHP: 7.4 … … 22 22 } 23 23 24 const GGSEC_STATS_VERSION = '1. 4.0';24 const GGSEC_STATS_VERSION = '1.5.0'; 25 25 // Bumped also when scheduled events change: ggsec_stats_maybe_upgrade() then re-runs the install routine, 26 26 // which schedules missing events on sites updated without re-activation (WordPress.org updates, FTP). 27 const GGSEC_STATS_DB_VERSION = ' 3';27 const GGSEC_STATS_DB_VERSION = '4'; 28 28 29 29 // Hit kinds. … … 53 53 global $wpdb; 54 54 return $wpdb->prefix . 'ggsec_stats_hits'; 55 } 56 57 /** 58 * Daily totals (counts only, no personal data); kept after the raw hits are deleted. 59 */ 60 function ggsec_stats_daily_table() { 61 global $wpdb; 62 return $wpdb->prefix . 'ggsec_stats_daily'; 55 63 } 56 64 … … 102 110 ); 103 111 112 $daily = ggsec_stats_daily_table(); 113 dbDelta( 114 "CREATE TABLE {$daily} ( 115 day date NOT NULL, 116 human_views int(10) unsigned NOT NULL DEFAULT 0, 117 visitors int(10) unsigned NOT NULL DEFAULT 0, 118 bots_known int(10) unsigned NOT NULL DEFAULT 0, 119 bots_nojs int(10) unsigned NOT NULL DEFAULT 0, 120 probes int(10) unsigned NOT NULL DEFAULT 0, 121 downloads int(10) unsigned NOT NULL DEFAULT 0, 122 recon int(10) unsigned NOT NULL DEFAULT 0, 123 ai_referrals int(10) unsigned NOT NULL DEFAULT 0, 124 PRIMARY KEY (day) 125 ) {$charset};" 126 ); 127 104 128 update_option( 'ggsec_stats_db_version', GGSEC_STATS_DB_VERSION, false ); 129 130 // Fills the daily history from the raw hits already recorded (first install of 1.5.0). 131 ggsec_stats_rollup(); 105 132 106 133 if ( ! wp_next_scheduled( 'ggsec_stats_daily' ) ) { … … 129 156 130 157 /** 131 * Daily maintenance: delete raw hits past the retention period, in batches. 158 * Aggregate complete local days (before today) from the raw hits into the daily table. 159 * 160 * Re-aggregates from the day before the last aggregated day (late beacons may still have upgraded hits after 161 * midnight), but never a day that retention cleanup may already have partly deleted: such a day would overwrite 162 * its correct totals with smaller ones. 163 * 164 * @return bool False on a database error. 165 */ 166 function ggsec_stats_rollup() { 167 global $wpdb; 168 $hits = ggsec_stats_table(); 169 $daily = ggsec_stats_daily_table(); 170 $tz = wp_timezone(); 171 172 // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin's own tables, maintenance job. 173 $last = (string) $wpdb->get_var( $wpdb->prepare( 'SELECT MAX(day) FROM %i', $daily ) ); 174 $from = '' !== $last 175 ? ( new DateTimeImmutable( $last, $tz ) )->modify( '-1 day' )->format( 'Y-m-d' ) 176 : (string) $wpdb->get_var( $wpdb->prepare( 'SELECT MIN(day) FROM %i', $hits ) ); 177 if ( '' === $from ) { 178 return true; // No hits recorded yet. 179 } 180 181 // Hits created before the last cleanup cutoff are gone; the local day containing it is incomplete. 182 // Without a recorded cutoff (sites updated from 1.4 or older), assume the most recent possible one. 183 $purged = (int) get_option( 'ggsec_stats_purged_before', time() - ggsec_stats_retention_days() * DAY_IN_SECONDS ); 184 $safe_from = ( new DateTimeImmutable( '@' . $purged ) )->setTimezone( $tz )->modify( '+1 day' )->format( 'Y-m-d' ); 185 $from = max( $from, $safe_from ); 186 $to = ( new DateTimeImmutable( 'yesterday', $tz ) )->format( 'Y-m-d' ); 187 if ( $from > $to ) { 188 return true; 189 } 190 191 $cutoff = ggsec_stats_unverified_cutoff(); 192 // Same definitions as ggsec_stats_summary(), ggsec_stats_recon_summary() and ggsec_stats_ai_referrals(). 193 $result = $wpdb->query( 194 $wpdb->prepare( 195 "REPLACE INTO %i (day, human_views, visitors, bots_known, bots_nojs, probes, downloads, recon, ai_referrals) 196 SELECT day, 197 SUM(kind = %d AND status = %d), 198 COUNT(DISTINCT CASE WHEN kind = %d AND status = %d THEN visitor END), 199 SUM(kind = %d AND status <> %d), 200 SUM(kind = %d AND status <> %d AND created_at < %s), 201 SUM(status = %d), 202 SUM(status = %d), 203 COALESCE(SUM((kind = %d AND bot_type = 'scanner') 204 OR (status IN (%d, %d) AND (kind = %d OR (kind = %d AND (status = %d OR created_at < %s))) AND path REGEXP %s)), 0), 205 COALESCE(SUM(kind = %d AND status = %d AND (ref_host REGEXP %s OR utm_source REGEXP %s)), 0) 206 FROM %i WHERE day BETWEEN %s AND %s GROUP BY day", 207 $daily, 208 GGSEC_STATS_KIND_HUMAN, 209 GGSEC_STATS_STATUS_OK, 210 GGSEC_STATS_KIND_HUMAN, 211 GGSEC_STATS_STATUS_OK, 212 GGSEC_STATS_KIND_BOT, 213 GGSEC_STATS_STATUS_PROBE, 214 GGSEC_STATS_KIND_UNVERIFIED, 215 GGSEC_STATS_STATUS_PROBE, 216 $cutoff, 217 GGSEC_STATS_STATUS_PROBE, 218 GGSEC_STATS_STATUS_DOWNLOAD, 219 GGSEC_STATS_KIND_BOT, 220 GGSEC_STATS_STATUS_404, 221 GGSEC_STATS_STATUS_PROBE, 222 GGSEC_STATS_KIND_BOT, 223 GGSEC_STATS_KIND_UNVERIFIED, 224 GGSEC_STATS_STATUS_PROBE, 225 $cutoff, 226 ggsec_stats_sensitive_path_regex(), 227 GGSEC_STATS_KIND_HUMAN, 228 GGSEC_STATS_STATUS_OK, 229 ggsec_stats_ai_referrer_regex(), 230 ggsec_stats_ai_referrer_regex(), 231 $hits, 232 $from, 233 $to 234 ) 235 ); 236 // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching 237 return false !== $result; 238 } 239 240 /** 241 * Daily maintenance: aggregate finished days, then delete raw hits past the retention period, in batches. 132 242 */ 133 243 function ggsec_stats_cleanup() { 134 244 global $wpdb; 135 $cutoff = gmdate( 'Y-m-d H:i:s', time() - ggsec_stats_retention_days() * DAY_IN_SECONDS ); 245 246 // A day is deleted only after it was aggregated. 247 if ( ! ggsec_stats_rollup() ) { 248 return; 249 } 250 251 $purged = time() - ggsec_stats_retention_days() * DAY_IN_SECONDS; 252 $cutoff = gmdate( 'Y-m-d H:i:s', $purged ); 253 update_option( 'ggsec_stats_purged_before', $purged, false ); 136 254 137 255 for ( $i = 0; $i < 50; $i++ ) { … … 158 276 __( 'Statistics data is deleted automatically after %d days.', 'ggsec-stats' ), 159 277 ggsec_stats_retention_days() 160 ) . ' </p>';278 ) . ' ' . __( 'Only daily totals (numbers of page views, visitors and bot requests, without any personal data) are kept longer.', 'ggsec-stats' ) . '</p>'; 161 279 wp_add_privacy_policy_content( 'GGSEC Stats', wp_kses_post( $content ) ); 162 280 } -
ggsec-stats/trunk/includes/admin.php
r3830097 r3835790 37 37 38 38 /** 39 * Optional UTC upper bound on created_at; null means no bound (the range is limited by day only). 40 */ 41 function ggsec_stats_before_utc( $before = null ) { 42 return null === $before ? '9999-12-31 23:59:59' : (string) $before; 43 } 44 45 /** 39 46 * Unverified hits created before this UTC time never got a beacon and are reported as bots. 40 47 */ … … 57 64 * Headline numbers for a range. 58 65 * 66 * @param string $since First local day (Y-m-d). 67 * @param string|null $until Last local day; default today. 68 * @param string|null $before Only hits created before this UTC time (previous-period comparison up to the same clock time). 59 69 * @return array<string,int> 60 70 */ 61 function ggsec_stats_summary( $since, $until = null ) {71 function ggsec_stats_summary( $since, $until = null, $before = null ) { 62 72 global $wpdb; 63 73 $row = $wpdb->get_row( … … 70 80 SUM(status = %d) AS probes, 71 81 SUM(status = %d) AS downloads 72 FROM %i WHERE day BETWEEN %s AND %s ',82 FROM %i WHERE day BETWEEN %s AND %s AND created_at < %s', 73 83 GGSEC_STATS_KIND_HUMAN, 74 84 GGSEC_STATS_STATUS_OK, … … 84 94 ggsec_stats_table(), 85 95 $since, 86 ggsec_stats_until_day( $until ) 96 ggsec_stats_until_day( $until ), 97 ggsec_stats_before_utc( $before ) 87 98 ), 88 99 ARRAY_A … … 148 159 } 149 160 return $series; 161 } 162 163 /** 164 * Humans and bots per month for the last 12 months (current month included, missing months zero): 165 * aggregated days from the daily table, days not aggregated yet (today, ...) from the raw hits. 166 * 167 * @return array<string,array{h:int,b:int}> Keyed by "Y-m". 168 */ 169 function ggsec_stats_monthly_series() { 170 global $wpdb; 171 $tz = wp_timezone(); 172 $month = new DateTimeImmutable( 'first day of this month', $tz ); 173 $start = $month->modify( '-11 months' ); 174 175 $series = array(); 176 for ( $i = 0; $i < 12; $i++ ) { 177 $series[ $start->modify( '+' . $i . ' months' )->format( 'Y-m' ) ] = array( 'h' => 0, 'b' => 0 ); 178 } 179 180 $daily = ggsec_stats_daily_table(); 181 $last = (string) $wpdb->get_var( $wpdb->prepare( 'SELECT MAX(day) FROM %i', $daily ) ); 182 $rows = $wpdb->get_results( 183 $wpdb->prepare( 184 'SELECT LEFT(day, 7) AS m, SUM(human_views) AS h, SUM(bots_known + bots_nojs) AS b FROM %i WHERE day >= %s GROUP BY m', 185 $daily, 186 $start->format( 'Y-m-d' ) 187 ), 188 ARRAY_A 189 ); 190 $rows = array_merge( 191 $rows, 192 $wpdb->get_results( 193 $wpdb->prepare( 194 'SELECT LEFT(day, 7) AS m, 195 SUM(kind = %d AND status = %d) AS h, 196 SUM(status <> %d AND (kind = %d OR (kind = %d AND created_at < %s))) AS b 197 FROM %i WHERE day > %s AND day >= %s GROUP BY m', 198 GGSEC_STATS_KIND_HUMAN, 199 GGSEC_STATS_STATUS_OK, 200 GGSEC_STATS_STATUS_PROBE, 201 GGSEC_STATS_KIND_BOT, 202 GGSEC_STATS_KIND_UNVERIFIED, 203 ggsec_stats_unverified_cutoff(), 204 ggsec_stats_table(), 205 '' !== $last ? $last : '0000-00-00', 206 $start->format( 'Y-m-d' ) 207 ), 208 ARRAY_A 209 ) 210 ); 211 foreach ( $rows as $r ) { 212 if ( isset( $series[ $r['m'] ] ) ) { 213 $series[ $r['m'] ]['h'] += (int) $r['h']; 214 $series[ $r['m'] ]['b'] += (int) $r['b']; 215 } 216 } 217 return $series; 218 } 219 220 /** 221 * First day of the long-term history (daily table or raw hits), or '' without data. 222 */ 223 function ggsec_stats_history_start() { 224 global $wpdb; 225 $daily = (string) $wpdb->get_var( $wpdb->prepare( 'SELECT MIN(day) FROM %i', ggsec_stats_daily_table() ) ); 226 $raw = ggsec_stats_first_day(); 227 if ( '' === $daily || '' === $raw ) { 228 return $daily . $raw; 229 } 230 return min( $daily, $raw ); 150 231 } 151 232 … … 293 374 * @return array{scanner:int,sensitive:int,total:int} 294 375 */ 295 function ggsec_stats_recon_summary( $since, $until = null ) {376 function ggsec_stats_recon_summary( $since, $until = null, $before = null ) { 296 377 global $wpdb; 297 378 $row = $wpdb->get_row( … … 302 383 SELECT (kind = %d AND bot_type = 'scanner') AS is_scanner, 303 384 (status IN (%d, %d) AND (kind = %d OR (kind = %d AND (status = %d OR created_at < %s))) AND path REGEXP %s) AS is_sensitive 304 FROM %i WHERE day BETWEEN %s AND %s 385 FROM %i WHERE day BETWEEN %s AND %s AND created_at < %s 305 386 ) AS recon", 306 387 GGSEC_STATS_KIND_BOT, … … 314 395 ggsec_stats_table(), 315 396 $since, 316 ggsec_stats_until_day( $until ) 397 ggsec_stats_until_day( $until ), 398 ggsec_stats_before_utc( $before ) 317 399 ), 318 400 ARRAY_A … … 346 428 347 429 /** 430 * Human page views that arrived from AI assistants (by referrer host or utm_source). 431 * 432 * @return array{total:int,assistants:array<string,array{views:int,visitors:int}>,pages:array} 433 */ 434 function ggsec_stats_ai_referrals( $since, $until = null, $before = null ) { 435 global $wpdb; 436 $table = ggsec_stats_table(); 437 $regex = ggsec_stats_ai_referrer_regex(); 438 439 // Source = the referrer host if it is an assistant, otherwise the utm_source tag. 440 $sources = $wpdb->get_results( 441 $wpdb->prepare( 442 'SELECT IF(ref_host REGEXP %s, ref_host, utm_source) AS source, COUNT(*) AS views, 443 COUNT(DISTINCT CONCAT(day, visitor)) AS visitors 444 FROM %i WHERE day BETWEEN %s AND %s AND created_at < %s AND kind = %d AND status = %d 445 AND (ref_host REGEXP %s OR utm_source REGEXP %s) 446 GROUP BY source', 447 $regex, 448 $table, 449 $since, 450 ggsec_stats_until_day( $until ), 451 ggsec_stats_before_utc( $before ), 452 GGSEC_STATS_KIND_HUMAN, 453 GGSEC_STATS_STATUS_OK, 454 $regex, 455 $regex 456 ), 457 ARRAY_A 458 ); 459 460 $total = 0; 461 $assistants = array(); 462 foreach ( $sources as $r ) { 463 $name = ggsec_stats_ai_assistant_name( $r['source'] ); 464 if ( null === $name ) { 465 continue; 466 } 467 if ( ! isset( $assistants[ $name ] ) ) { 468 $assistants[ $name ] = array( 469 'views' => 0, 470 'visitors' => 0, 471 ); 472 } 473 $assistants[ $name ]['views'] += (int) $r['views']; 474 $assistants[ $name ]['visitors'] += (int) $r['visitors']; 475 $total += (int) $r['views']; 476 } 477 uasort( 478 $assistants, 479 function ( $a, $b ) { 480 return $b['views'] <=> $a['views']; 481 } 482 ); 483 484 $pages = $total ? $wpdb->get_results( 485 $wpdb->prepare( 486 'SELECT path, MAX(post_id) AS post_id, COUNT(*) AS views 487 FROM %i WHERE day BETWEEN %s AND %s AND created_at < %s AND kind = %d AND status = %d 488 AND (ref_host REGEXP %s OR utm_source REGEXP %s) 489 GROUP BY path ORDER BY views DESC LIMIT 10', 490 $table, 491 $since, 492 ggsec_stats_until_day( $until ), 493 ggsec_stats_before_utc( $before ), 494 GGSEC_STATS_KIND_HUMAN, 495 GGSEC_STATS_STATUS_OK, 496 $regex, 497 $regex 498 ), 499 ARRAY_A 500 ) : array(); 501 502 return array( 503 'total' => $total, 504 'assistants' => $assistants, 505 'pages' => $pages, 506 ); 507 } 508 509 /** 510 * First local day with recorded hits (Y-m-d), or '' when the table is empty. 511 */ 512 function ggsec_stats_first_day() { 513 global $wpdb; 514 return (string) $wpdb->get_var( $wpdb->prepare( 'SELECT MIN(day) FROM %i', ggsec_stats_table() ) ); 515 } 516 517 /** 348 518 * File downloads clicked by real visitors (PDF reports, archives, ...). 349 519 */ … … 432 602 * Stacked bar chart (humans bottom, bots top) as inline SVG; no JavaScript library. 433 603 * 434 * @param array<string,array{h:int,b:int}> $series 435 */ 436 function ggsec_stats_chart_svg( $series ) { 604 * @param array<string,array{h:int,b:int}> $series Keyed by day (Y-m-d) or month (Y-m). 605 * @param string $label Accessible chart description. 606 */ 607 function ggsec_stats_chart_svg( $series, $label = '' ) { 437 608 $w = 760; 438 609 $h = 200; … … 448 619 $bar_w = max( 2, $slot * 0.7 ); 449 620 450 $svg = '<svg class="ggsec-stats-chart" viewBox="0 0 ' . $w . ' ' . $h . '" role="img" aria-label="' . esc_attr__( 'Humans and bots per day', 'ggsec-stats' ) . '">'; 621 $label = '' !== $label ? $label : __( 'Humans and bots per day', 'ggsec-stats' ); 622 $every = $n <= 12 ? 1 : 5; 623 624 $svg = '<svg class="ggsec-stats-chart" viewBox="0 0 ' . $w . ' ' . $h . '" role="img" aria-label="' . esc_attr( $label ) . '">'; 451 625 $svg .= '<line x1="' . $pad_l . '" y1="' . ( 8 + $plot_h ) . '" x2="' . $w . '" y2="' . ( 8 + $plot_h ) . '" class="axis"/>'; 452 626 $svg .= '<text x="' . ( $pad_l - 6 ) . '" y="14" class="lbl" text-anchor="end">' . esc_html( number_format_i18n( $max ) ) . '</text>'; … … 471 645 $svg .= sprintf( '<rect class="b" x="%.1f" y="%.1f" width="%.1f" height="%.1f"/>', $x, $y0 - $hh - $bh, $bar_w, $bh ); 472 646 $svg .= '</g>'; 473 if ( 0 === $i % 5 || $i === $n - 1 ) { 474 $svg .= sprintf( '<text class="lbl" x="%.1f" y="%d" text-anchor="middle">%s</text>', $x + $bar_w / 2, $h - 6, esc_html( substr( $day, 5 ) ) ); 647 if ( 0 === $i % $every || $i === $n - 1 ) { 648 // Days as "11-27", months as the short month name. 649 $text = 7 === strlen( $day ) ? wp_date( 'M', ( new DateTimeImmutable( $day . '-01', wp_timezone() ) )->getTimestamp() ) : substr( $day, 5 ); 650 $svg .= sprintf( '<text class="lbl" x="%.1f" y="%d" text-anchor="middle">%s</text>', $x + $bar_w / 2, $h - 6, esc_html( $text ) ); 475 651 } 476 652 $i++; … … 499 675 500 676 /** 677 * Change indicator for a card: [text, tone, tooltip] or null when there is nothing to compare. 678 * 679 * @param int $current Value in the shown period. 680 * @param int|null $previous Value in the previous period; null when the previous period has no data yet. 681 * @param string $rise Tone of an increase: 'good', 'bad' or 'neutral'. 682 * @param string $tooltip What the value is compared with. 683 * @return array{0:string,1:string,2:string}|null 684 */ 685 function ggsec_stats_change( $current, $previous, $rise, $tooltip ) { 686 if ( null === $previous ) { 687 return null; 688 } 689 $text = ggsec_stats_mail_change( (int) $current, (int) $previous ); 690 if ( '' === $text ) { 691 return null; 692 } 693 $fall = array( 694 'good' => 'bad', 695 'bad' => 'good', 696 'neutral' => 'neutral', 697 ); 698 if ( (int) $current === (int) $previous ) { 699 $tone = 'neutral'; 700 } else { 701 $tone = $current > $previous ? $rise : ( $fall[ $rise ] ?? 'neutral' ); 702 } 703 return array( $text, $tone, $tooltip ); 704 } 705 706 /** 501 707 * One headline card. $value is a number or a short preformatted string (e.g. "42%"). 502 */ 503 function ggsec_stats_card( $label, $value, $hint = '' ) { 504 echo '<div class="ggsec-stats-card"><span class="v">' . esc_html( is_int( $value ) ? number_format_i18n( $value ) : (string) $value ) . '</span>'; 708 * 709 * @param array{0:string,1:string,2:string}|null $change Indicator from ggsec_stats_change(). 710 */ 711 function ggsec_stats_card( $label, $value, $hint = '', $change = null ) { 712 echo '<div class="ggsec-stats-card"><span class="v">' . esc_html( is_int( $value ) ? number_format_i18n( $value ) : (string) $value ); 713 if ( $change ) { 714 echo ' <span class="chg ' . esc_attr( $change[1] ) . '" title="' . esc_attr( $change[2] ) . '">' . esc_html( $change[0] ) . '</span>'; 715 } 716 echo '</span>'; 505 717 echo '<span class="l">' . esc_html( $label ) . '</span>'; 506 718 if ( '' !== $hint ) { … … 551 763 $range = 'today'; 552 764 } 553 $since = ggsec_stats_since_day( $ranges[ $range ][0] ); 765 $days = $ranges[ $range ][0]; 766 $since = ggsec_stats_since_day( $days ); 554 767 555 768 $sum = ggsec_stats_summary( $since ); … … 560 773 $recon = ggsec_stats_recon_summary( $since ); 561 774 $downloads = ggsec_stats_downloads( $since ); 775 $ai_refs = ggsec_stats_ai_referrals( $since ); 776 777 // Previous period: the same number of days before, up to the same clock time (today 10:00 vs yesterday 10:00). 778 // Compared only when it lies completely within the recorded data. 779 $prev_since = ggsec_stats_since_day( 2 * $days ); 780 $first_day = ggsec_stats_first_day(); 781 $prev = array(); 782 if ( '' !== $first_day && $prev_since >= $first_day ) { 783 $prev_until = ( new DateTimeImmutable( 'today', wp_timezone() ) )->modify( '-' . $days . ' days' )->format( 'Y-m-d' ); 784 $before = gmdate( 'Y-m-d H:i:s', time() - $days * DAY_IN_SECONDS ); 785 $prev = ggsec_stats_summary( $prev_since, $prev_until, $before ); 786 $prev_recon = ggsec_stats_recon_summary( $prev_since, $prev_until, $before ); 787 $prev_ai = ggsec_stats_ai_referrals( $prev_since, $prev_until, $before ); 788 789 $prev['recon'] = $prev_recon['total']; 790 $prev['ai'] = $prev_ai['total']; 791 } 792 $tooltip = 1 === $days 793 ? __( 'Compared with yesterday up to the same time', 'ggsec-stats' ) 794 /* translators: %d: number of days in the compared period. */ 795 : sprintf( __( 'Compared with the previous %d days up to the same time', 'ggsec-stats' ), $days ); 796 $change = function ( $key, $current, $rise ) use ( $prev, $tooltip ) { 797 return ggsec_stats_change( $current, $prev[ $key ] ?? null, $rise, $tooltip ); 798 }; 562 799 ?> 563 800 <div class="wrap ggsec-stats"> … … 573 810 <div class="ggsec-stats-cards"> 574 811 <?php 575 ggsec_stats_card( __( 'Human page views', 'ggsec-stats' ), $sum['human_views'] ); 576 ggsec_stats_card( __( 'Unique visitors', 'ggsec-stats' ), $sum['visitors'], __( 'per day, summed', 'ggsec-stats' ) ); 577 ggsec_stats_card( __( 'Known bots', 'ggsec-stats' ), $sum['bots_known'], __( 'identified by user agent', 'ggsec-stats' ) ); 578 ggsec_stats_card( __( 'Bots without JS', 'ggsec-stats' ), $sum['bots_nojs'], __( 'browser-like, never rendered the page', 'ggsec-stats' ) ); 812 ggsec_stats_card( __( 'Human page views', 'ggsec-stats' ), $sum['human_views'], '', $change( 'human_views', $sum['human_views'], 'good' ) ); 813 ggsec_stats_card( __( 'Unique visitors', 'ggsec-stats' ), $sum['visitors'], __( 'per day, summed', 'ggsec-stats' ), $change( 'visitors', $sum['visitors'], 'good' ) ); 814 ggsec_stats_card( __( 'From AI assistants', 'ggsec-stats' ), $ai_refs['total'], __( 'human views via ChatGPT, Perplexity, Claude, ...', 'ggsec-stats' ), $change( 'ai', $ai_refs['total'], 'good' ) ); 815 ggsec_stats_card( __( 'Known bots', 'ggsec-stats' ), $sum['bots_known'], __( 'identified by user agent', 'ggsec-stats' ), $change( 'bots_known', $sum['bots_known'], 'neutral' ) ); 816 ggsec_stats_card( __( 'Bots without JS', 'ggsec-stats' ), $sum['bots_nojs'], __( 'browser-like, never rendered the page', 'ggsec-stats' ), $change( 'bots_nojs', $sum['bots_nojs'], 'neutral' ) ); 579 817 ggsec_stats_card( __( 'Bot share', 'ggsec-stats' ), $bot_share, __( 'of all page requests', 'ggsec-stats' ) ); 580 ggsec_stats_card( __( 'Login/XML-RPC probes', 'ggsec-stats' ), $sum['probes'], __( 'failed logins, bot login scans, XML-RPC', 'ggsec-stats' ) );818 ggsec_stats_card( __( 'Login/XML-RPC probes', 'ggsec-stats' ), $sum['probes'], __( 'failed logins, bot login scans, XML-RPC', 'ggsec-stats' ), $change( 'probes', $sum['probes'], 'bad' ) ); 581 819 ggsec_stats_card( __( 'Online now', 'ggsec-stats' ), ggsec_stats_online_now(), __( 'humans, last 5 minutes', 'ggsec-stats' ) ); 582 ggsec_stats_card( __( 'Downloads', 'ggsec-stats' ), $sum['downloads'], __( 'file links clicked by visitors', 'ggsec-stats' ) );820 ggsec_stats_card( __( 'Downloads', 'ggsec-stats' ), $sum['downloads'], __( 'file links clicked by visitors', 'ggsec-stats' ), $change( 'downloads', $sum['downloads'], 'good' ) ); 583 821 ggsec_stats_card( 584 822 __( 'Security recon', 'ggsec-stats' ), … … 589 827 number_format_i18n( $recon['scanner'] ), 590 828 number_format_i18n( $recon['sensitive'] ) 591 ) 829 ), 830 $change( 'recon', $recon['total'], 'bad' ) 592 831 ); 593 832 ?> … … 599 838 </h2> 600 839 <?php echo wp_kses( ggsec_stats_chart_svg( ggsec_stats_daily_series( 30 ) ), ggsec_stats_allowed_svg() ); ?> 840 841 <?php 842 $history_start = ggsec_stats_history_start(); 843 if ( '' !== $history_start ) : 844 ?> 845 <h2> 846 <?php esc_html_e( 'Last 12 months', 'ggsec-stats' ); ?> 847 <span class="ggsec-stats-legend"><i class="h"></i> <?php esc_html_e( 'humans', 'ggsec-stats' ); ?> <i class="b"></i> <?php esc_html_e( 'bots', 'ggsec-stats' ); ?></span> 848 </h2> 849 <?php echo wp_kses( ggsec_stats_chart_svg( ggsec_stats_monthly_series(), __( 'Humans and bots per month', 'ggsec-stats' ) ), ggsec_stats_allowed_svg() ); ?> 850 <p class="description"> 851 <?php 852 echo esc_html( 853 sprintf( 854 /* translators: %s: date the statistics history starts. */ 855 __( 'History since %s. Daily totals are kept after the raw data is deleted, so this chart keeps growing.', 'ggsec-stats' ), 856 ggsec_stats_mail_date( $history_start ) 857 ) 858 ); 859 ?> 860 </p> 861 <?php endif; ?> 601 862 602 863 <div class="ggsec-stats-grid"> … … 721 982 722 983 <section> 984 <h2><?php esc_html_e( 'Visitors from AI assistants', 'ggsec-stats' ); ?></h2> 985 <?php 986 $rows = array(); 987 foreach ( $ai_refs['assistants'] as $name => $r ) { 988 $rows[] = array( esc_html( $name ), ggsec_stats_num( $r['views'] ), ggsec_stats_num( $r['visitors'] ) ); 989 } 990 ggsec_stats_table_html( array( __( 'Assistant', 'ggsec-stats' ), __( 'Views', 'ggsec-stats' ), __( 'Visitors', 'ggsec-stats' ) ), $rows ); 991 if ( $ai_refs['pages'] ) : 992 ?> 993 <h3><?php esc_html_e( 'Landing pages', 'ggsec-stats' ); ?></h3> 994 <?php 995 $rows = array(); 996 foreach ( $ai_refs['pages'] as $r ) { 997 $title = $r['post_id'] ? get_the_title( (int) $r['post_id'] ) : ''; 998 $rows[] = array( 999 esc_html( $r['path'] ) . ( '' !== $title ? '<br><small>' . esc_html( $title ) . '</small>' : '' ), 1000 ggsec_stats_num( $r['views'] ), 1001 ); 1002 } 1003 ggsec_stats_table_html( array( __( 'Page', 'ggsec-stats' ), __( 'Views', 'ggsec-stats' ) ), $rows ); 1004 endif; 1005 ?> 1006 <p class="description"><?php esc_html_e( 'Real visitors who followed a link in an AI assistant answer (referrer or utm_source of ChatGPT, Perplexity, Claude, Copilot, Gemini and others). AI crawlers fetching pages are listed separately above.', 'ggsec-stats' ); ?></p> 1007 </section> 1008 1009 <section> 723 1010 <h2><?php esc_html_e( 'Downloads', 'ggsec-stats' ); ?></h2> 724 1011 <?php -
ggsec-stats/trunk/includes/bots.php
r3824418 r3835790 205 205 } 206 206 207 /** 208 * AI assistants that send visitors to the site: referrer host (without "www.") => display name. 209 * The same hosts are matched against utm_source, because ChatGPT tags its links with utm_source=chatgpt.com 210 * and its apps often send no referrer. 211 * 212 * @return array<string,string> 213 */ 214 function ggsec_stats_ai_referrers() { 215 $hosts = array( 216 'chatgpt.com' => 'ChatGPT', 217 'chat.openai.com' => 'ChatGPT', 218 'perplexity.ai' => 'Perplexity', 219 'claude.ai' => 'Claude', 220 'copilot.microsoft.com' => 'Microsoft Copilot', 221 'gemini.google.com' => 'Google Gemini', 222 'chat.mistral.ai' => 'Mistral Le Chat', 223 'chat.deepseek.com' => 'DeepSeek', 224 'meta.ai' => 'Meta AI', 225 'grok.com' => 'Grok', 226 'you.com' => 'You.com', 227 'phind.com' => 'Phind', 228 'poe.com' => 'Poe', 229 ); 230 231 /** 232 * Filter the AI assistant referrer list (host without "www." => display name). 233 */ 234 $hosts = (array) apply_filters( 'ggsec_stats_ai_referrers', $hosts ); 235 236 // Hosts end up in a regular expression; only plain host names are accepted. 237 $clean = array(); 238 foreach ( $hosts as $host => $name ) { 239 $host = strtolower( (string) $host ); 240 if ( preg_match( '/^[a-z0-9-]+(\.[a-z0-9-]+)+$/', $host ) ) { 241 $clean[ $host ] = (string) $name; 242 } 243 } 244 return $clean; 245 } 246 247 /** 248 * Regular expression matching a referrer host or utm_source of an AI assistant, for MySQL/MariaDB REGEXP 249 * (whole value, optional "www."). Matches nothing when the list is empty. 250 */ 251 function ggsec_stats_ai_referrer_regex() { 252 $hosts = array_map( 253 function ( $host ) { 254 return str_replace( '.', '\.', $host ); 255 }, 256 array_keys( ggsec_stats_ai_referrers() ) 257 ); 258 return $hosts ? '^(www\.)?(' . implode( '|', $hosts ) . ')$' : '^$.'; 259 } 260 261 /** 262 * Assistant name for a referrer host or utm_source value, or null if it is not an AI assistant. 263 */ 264 function ggsec_stats_ai_assistant_name( $value ) { 265 $value = preg_replace( '/^www\./', '', strtolower( trim( (string) $value ) ) ); 266 $list = ggsec_stats_ai_referrers(); 267 return $list[ $value ] ?? null; 268 } 269 207 270 function ggsec_stats_bot_type_label( $type ) { 208 271 $labels = array( -
ggsec-stats/trunk/includes/notify.php
r3830097 r3835790 368 368 $total = $sum['human_views'] + $sum['bots']; 369 369 370 // No comparison while the previous period starts before the first recorded day (fresh install): 371 // every number would show "new". 372 $first_day = ggsec_stats_first_day(); 373 $compare = '' === $first_day || $prev_from >= $first_day; 374 $change = function ( $current, $previous ) use ( $compare ) { 375 return $compare ? ggsec_stats_mail_change( $current, $previous ) : ''; 376 }; 377 370 378 $range = $from === $to ? ggsec_stats_mail_date( $from ) : ggsec_stats_mail_date( $from ) . ' – ' . ggsec_stats_mail_date( $to ); 371 379 if ( 'daily' === $frequency ) { 372 380 /* translators: %s: report date. */ 373 381 $title = sprintf( __( 'Daily report: %s', 'ggsec-stats' ), $range ); 374 $intro = __( 'Change compared with the day before.', 'ggsec-stats' ); 382 $intro = $compare 383 ? __( 'Change compared with the day before.', 'ggsec-stats' ) 384 /* translators: %s: date of the first recorded day. */ 385 : sprintf( __( 'No comparison with the day before: statistics start on %s.', 'ggsec-stats' ), ggsec_stats_mail_date( $first_day ) ); 375 386 } else { 376 387 /* translators: %s: report date range. */ 377 388 $title = sprintf( __( 'Weekly report: %s', 'ggsec-stats' ), $range ); 378 $intro = __( 'Change compared with the week before.', 'ggsec-stats' ); 389 $intro = $compare 390 ? __( 'Change compared with the week before.', 'ggsec-stats' ) 391 /* translators: %s: date of the first recorded day. */ 392 : sprintf( __( 'No comparison with the week before: statistics start on %s.', 'ggsec-stats' ), ggsec_stats_mail_date( $first_day ) ); 379 393 } 380 394 381 395 $body = ggsec_stats_mail_cards( 382 396 array( 383 array( number_format_i18n( $sum['human_views'] ), __( 'Human page views', 'ggsec-stats' ), ggsec_stats_mail_change( $sum['human_views'], $prev['human_views'] ) ),384 array( number_format_i18n( $sum['visitors'] ), __( 'Unique visitors (per day, summed)', 'ggsec-stats' ), ggsec_stats_mail_change( $sum['visitors'], $prev['visitors'] ) ),385 array( number_format_i18n( $sum['bots'] ), __( 'Bot requests', 'ggsec-stats' ), ggsec_stats_mail_change( $sum['bots'], $prev['bots'] ) ),397 array( number_format_i18n( $sum['human_views'] ), __( 'Human page views', 'ggsec-stats' ), $change( $sum['human_views'], $prev['human_views'] ) ), 398 array( number_format_i18n( $sum['visitors'] ), __( 'Unique visitors (per day, summed)', 'ggsec-stats' ), $change( $sum['visitors'], $prev['visitors'] ) ), 399 array( number_format_i18n( $sum['bots'] ), __( 'Bot requests', 'ggsec-stats' ), $change( $sum['bots'], $prev['bots'] ) ), 386 400 array( $total ? number_format_i18n( round( 100 * $sum['bots'] / $total ) ) . '%' : '-', __( 'Bot share of all page requests', 'ggsec-stats' ), '' ), 387 array( number_format_i18n( $sum['probes'] ), __( 'Login/XML-RPC probes', 'ggsec-stats' ), ggsec_stats_mail_change( $sum['probes'], $prev['probes'] ) ),388 array( number_format_i18n( $recon['total'] ), __( 'Security recon requests', 'ggsec-stats' ), ggsec_stats_mail_change( $recon['total'], $prev_recon['total'] ) ),401 array( number_format_i18n( $sum['probes'] ), __( 'Login/XML-RPC probes', 'ggsec-stats' ), $change( $sum['probes'], $prev['probes'] ) ), 402 array( number_format_i18n( $recon['total'] ), __( 'Security recon requests', 'ggsec-stats' ), $change( $recon['total'], $prev_recon['total'] ) ), 389 403 ) 390 404 ); … … 405 419 $body .= ggsec_stats_mail_heading( __( 'Top referrers', 'ggsec-stats' ) ) 406 420 . ggsec_stats_mail_table( array( __( 'Source site', 'ggsec-stats' ), __( 'Views', 'ggsec-stats' ) ), $rows ); 421 422 // Shown only when there were such visits, to keep the report short for sites without them. 423 $ai_refs = ggsec_stats_ai_referrals( $from, $to ); 424 if ( $ai_refs['total'] ) { 425 $prev_ai = ggsec_stats_ai_referrals( $prev_from, $prev_to ); 426 $rows = array(); 427 foreach ( array_slice( $ai_refs['assistants'], 0, 5, true ) as $name => $r ) { 428 $rows[] = array( $name, number_format_i18n( $r['views'] ), number_format_i18n( $r['visitors'] ) ); 429 } 430 $ai_chg = $change( $ai_refs['total'], $prev_ai['total'] ); 431 $body .= ggsec_stats_mail_heading( 432 trim( 433 sprintf( 434 /* translators: 1: number of human page views from AI assistants, 2: change vs previous period (may be empty). */ 435 __( 'Visitors from AI assistants: %1$s %2$s', 'ggsec-stats' ), 436 number_format_i18n( $ai_refs['total'] ), 437 '' !== $ai_chg ? '(' . $ai_chg . ')' : '' 438 ) 439 ) 440 ) . ggsec_stats_mail_table( array( __( 'Assistant', 'ggsec-stats' ), __( 'Views', 'ggsec-stats' ), __( 'Visitors', 'ggsec-stats' ) ), $rows, 1 ); 441 } 407 442 408 443 $bots = ggsec_stats_bot_breakdown( $from, $to ); -
ggsec-stats/trunk/readme.txt
r3830097 r3835790 5 5 Tested up to: 7.1 6 6 Requires PHP: 7.4 7 Stable tag: 1. 4.07 Stable tag: 1.5.0 8 8 License: GPLv2 or later 9 9 License URI: https://www.gnu.org/licenses/gpl-2.0.html … … 20 20 21 21 * **Humans vs. bots** - human page views, unique visitors, known bots, browser-like clients that never ran JavaScript, bot share and visitors online now. 22 * **Change indicators** - every headline number shows its change against the previous period, compared up to the same time of day. 22 23 * **30-day chart** of humans and bots, rendered as inline SVG (no JavaScript chart library). 24 * **12-month history** - daily totals are kept after the raw data is deleted, for a monthly chart of the last 12 months. 23 25 * **Top pages** and **referrers** (source sites, full referring URLs without query strings, `utm_source` / `ref` campaign tags). 24 26 * **Bot breakdown** by name and type: search engines, AI crawlers, SEO tools, social previews, monitoring services, security scanners and HTTP libraries. 25 27 * **Pages read by AI crawlers** (GPTBot, ClaudeBot, PerplexityBot and others). 28 * **Visitors from AI assistants** - real visitors who followed a link in ChatGPT, Perplexity, Claude, Copilot, Gemini and other assistants, with their landing pages. 26 29 * **Security reconnaissance** - failed logins, bot scans of the login page, XML-RPC requests, 404 requests from non-humans and requests for sensitive paths such as `.env`, `.git` or backup archives. 27 30 * **Broken links** - 404 pages opened by real visitors, with the referring site. … … 39 42 * Query strings are removed from recorded paths and referrers. 40 43 * The user agent of a confirmed human visit is erased; it is kept only for bots and unconfirmed requests. 41 * Raw data is deleted automatically after 90 days. 44 * Raw data is deleted automatically after 90 days. Only daily totals (numbers, no personal data) are kept for the long-term history. 42 45 * Logged-in editors and administrators are not counted. 43 46 … … 78 81 Only if you enable it under **Dashboard > GGSEC Stats > E-mail reports and alerts**; reports and alerts are off by default. Mails are sent with your site's own mail setup (`wp_mail`) to the administration e-mail address or a recipient you choose. They contain aggregate numbers, paths, bot names and user agents only, no IP addresses. Alerts are checked hourly by WP-Cron and sent at most once every 6 hours per kind. 79 82 83 = How are visitors from AI assistants detected? = 84 85 By the referring site (chatgpt.com, perplexity.ai, claude.ai, copilot.microsoft.com, gemini.google.com and others) or by the `utm_source` tag that ChatGPT adds to its links. Only page views confirmed as human are counted. The list can be changed with the `ggsec_stats_ai_referrers` filter. 86 87 = How long is the history kept? = 88 89 Raw hits are deleted after 90 days (or `GGSEC_STATS_RETENTION_DAYS`). Before that, each finished day is stored as one row of totals (human views, visitors, bots, probes, downloads, security recon, AI referrals), which is kept until the plugin is deleted. Existing data is aggregated automatically when you update to 1.5.0. 90 80 91 = What happens on uninstall? = 81 92 82 Deleting the plugin from the Plugins screen drops its database table and removes its options and scheduled events.93 Deleting the plugin from the Plugins screen drops its database tables and removes its options and scheduled events. 83 94 84 95 == Screenshots == … … 90 101 91 102 == Changelog == 103 104 = 1.5.0 = 105 * Change indicators on the headline cards, compared with the previous period up to the same time of day. 106 * Visitors from AI assistants (ChatGPT, Perplexity, Claude, Copilot, Gemini and others): card, table with landing pages and a section in the e-mail report. 107 * Long-term history: daily totals are kept after raw data cleanup; new chart of the last 12 months. 92 108 93 109 = 1.4.0 = … … 119 135 == Upgrade Notice == 120 136 137 = 1.5.0 = 138 Change indicators, visitors from AI assistants and a 12-month history chart. Existing data is aggregated on update. 139 121 140 = 1.4.0 = 122 141 Optional e-mail reports and security alerts. Off by default; enable them under Dashboard > GGSEC Stats. -
ggsec-stats/trunk/uninstall.php
r3830097 r3835790 11 11 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange -- removes the plugin's own table on uninstall. 12 12 $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $wpdb->prefix . 'ggsec_stats_hits' ) ); 13 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange -- removes the plugin's own table on uninstall. 14 $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $wpdb->prefix . 'ggsec_stats_daily' ) ); 13 15 14 16 delete_option( 'ggsec_stats_db_version' ); … … 17 19 delete_option( 'ggsec_stats_notify' ); 18 20 delete_option( 'ggsec_stats_notify_state' ); 21 delete_option( 'ggsec_stats_purged_before' ); 19 22 wp_clear_scheduled_hook( 'ggsec_stats_daily' ); 20 23 wp_clear_scheduled_hook( 'ggsec_stats_hourly' );
Note:
See TracChangeset
for help on using the changeset viewer.