statistics.module 3.43 KB
Newer Older
Dries's avatar
 
Dries committed
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php

if (variable_get("referer", 0)) {
  if ($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() {
Dries's avatar
   
Dries committed
14
  return array("administer statistics");
Dries's avatar
 
Dries committed
15
16
}

Dries's avatar
   
Dries committed
17
function statistics_link($type) {
Dries's avatar
   
Dries committed
18
  if ($type == "admin" && user_access("administer statistics")) {
Dries's avatar
   
Dries committed
19
20
21
22
23
24
    $links[] = "<a href=\"admin.php?mod=statistics\">statistics</a>";
  }

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

Dries's avatar
 
Dries committed
25
26
27
28
29
30
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), 1000000000 => t("Never"));
  $output .= form_select(t("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(t("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;
}

Dries's avatar
   
Dries committed
31
32
function statistics_referer_internal() {
  $result = db_query("SELECT url, COUNT(url) AS count FROM referer WHERE url LIKE '". path_uri() ."%' GROUP BY url ORDER BY count DESC");
Dries's avatar
   
Dries committed
33

Dries's avatar
   
Dries committed
34
  $output .= "<P>Internal referers of the last ". format_interval(variable_get("referer_clear", 604800)) .":</P>\n";
Dries's avatar
   
Dries committed
35
36
37
38

  $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)) {
Dries's avatar
   
Dries committed
39
    $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>";
Dries's avatar
   
Dries committed
40
41
42
  }
  $output .= "</TABLE>\n";

Dries's avatar
   
Dries committed
43
44
45
46
47
48
49
  return $output;
}

function statistics_referer_external() {
  $result = db_query("SELECT url, COUNT(url) AS count FROM referer WHERE url NOT LIKE '". path_uri() ."%' GROUP BY url ORDER BY count DESC");

  $output .= "<P>External referers of the last ". format_interval(variable_get("referer_clear", 604800)) .":</P>\n";
Dries's avatar
   
Dries committed
50

Dries's avatar
 
Dries committed
51
52
53
  $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)) {
Dries's avatar
   
Dries committed
54
    $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>";
Dries's avatar
 
Dries committed
55
56
57
58
59
60
61
  }
  $output .= "</TABLE>\n";

  return $output;
}

function statistics_admin() {
Dries's avatar
   
Dries committed
62
63
64
65
66
  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";
Dries's avatar
 
Dries committed
67

Dries's avatar
   
Dries committed
68
69
70
71
72
73
74
75
76
    switch ($type) {
      case "internal referer":
        print statistics_referer_internal();
        break;
      case "external referer":
        // fall through:
      default:
        print statistics_referer_external();
    }
Dries's avatar
 
Dries committed
77
78
79
80
  }
}

?>