summaryrefslogtreecommitdiff
path: root/modules/statistics.module
blob: 4e4f2fb8c80f15633a204e0022ea05e571d059e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
// $Id$

if (variable_get("referer", 0) && $referer = getenv("HTTP_REFERER")) {
  db_query("INSERT INTO referer (URL, timestamp) values ('". check_input($referer) ."', '". time() ."')");
}

function statistics_cron() {
  db_query("DELETE FROM referer WHERE ". time() ." - timestamp > ". variable_get("referer_clear", 604800));
}

function statistics_perm() {
  return array("administer statistics");
}

function statistics_link($type) {
  if ($type == "admin" && user_access("administer statistics")) {
    $links[] = "<a href=\"admin.php?mod=statistics\">statistics</a>";
  }

  return $links ? $links : array();
}

function statistics_conf_options() {
  $period = array(3600 => format_interval(3600), 10800 => format_interval(10800), 21600 => format_interval(21600), 32400 => format_interval(32400), 43200 => format_interval(43200), 86400 => format_interval(86400), 172800 => format_interval(172800), 259200 => format_interval(259200), 604800 => format_interval(604800), 1209600 => format_interval(1209600), 2419200 => format_interval(2419200), 4838400 => format_interval(4838400), 9676800 => format_interval(9676800));
  $output .= form_select("Track referers", "referer", variable_get("referer", 0), array("Disabled", "Enabled"), "If enabled, Drupal will count how many times your website is referred to by other websites.");
  $output .= form_select("Discard referers older than", "referer_clear", variable_get("referer_clear", 604800), $period, "The time referer entries should be kept.  Older entries will be automatically discarded.  Requires crontab.");
  return $output;
}

function statistics_table_1($query) {
  $result = db_query($query);

  $output .= "<table border=\"1\" cellpadding=\"3\" cellspacing=\"0\">\n";
  $output .= " <tr><th>URL</th><th>date</th></tr>\n";
  while ($referer = db_fetch_object($result)) {
    $output .= "<tr><td><a href=\"". check_output($referer->url) ."\">". substr(check_output($referer->url), 0, 100) ."</a></td><td>". format_date($referer->timestamp, "small") ."</td></tr>";
  }
  $output .= "</table>\n";

  return $output;
}

function statistics_table_2($query) {
  $result = db_query($query);

  $output .= "<table border=\"1\" cellpadding=\"3\" cellspacing=\"0\">\n";
  $output .= " <tr><th>URL</th><th>number</th></tr>\n";
  while ($referer = db_fetch_object($result)) {
    $output .= "<tr><td><a href=\"". check_output($referer->url) ."\">". substr(check_output($referer->url), 0, 100) ."</a></td><td>". check_output($referer->count) ."</td></tr>";
  }
  $output .= "</table>\n";

  return $output;
}

function statistics_referer_internal() {
  global $HTTP_HOST;

  $output .= "<h3>Most recent internal referers</h3>\n";
  $output .= statistics_table_1("SELECT url, timestamp FROM referer WHERE url LIKE '%". check_input($HTTP_HOST) ."%' ORDER BY timestamp DESC LIMIT 15");

  $output .= "<h3>Internal referers of the last ". format_interval(variable_get("referer_clear", 604800)) ."</h3>\n";
  $output .= statistics_table_2("SELECT url, COUNT(url) AS count FROM referer WHERE url LIKE '%". check_input($HTTP_HOST) ."%' GROUP BY url ORDER BY count DESC");

  return $output;
}

function statistics_referer_external() {
  global $HTTP_HOST;

  $output .= "<h3>Most recent external referers</h3>\n";
  $output .= statistics_table_1("SELECT url, timestamp FROM referer WHERE url NOT LIKE '%". check_input($HTTP_HOST) ."%' ORDER BY timestamp DESC LIMIT 15");

  $output .= "<h3>External referers of the last ". format_interval(variable_get("referer_clear", 604800)) ."</h3>\n";
  $output .= statistics_table_2("SELECT url, COUNT(url) AS count FROM referer WHERE url NOT LIKE '%". check_input($HTTP_HOST) ."%' GROUP BY url ORDER BY count DESC");

  return $output;
}

function statistics_admin() {
  global $type;

  if (user_access("administer statistics")) {

    print "<small><a href=\"admin.php?mod=statistics&type=internal+referer\">internal referers</a> | <a href=\"admin.php?mod=statistics&type=external+referer\">external referers</a></small><hr />\n";

    switch ($type) {
      case "internal referer":
        print statistics_referer_internal();
        break;
      case "external referer":
        // fall through:
      default:
        print statistics_referer_external();
    }
  }
}

?>