File: new_connect.php

package info (click to toggle)
bbclone 0.4.6-8
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,304 kB
  • ctags: 528
  • sloc: php: 15,858; sh: 349; makefile: 41
file content (173 lines) | stat: -rw-r--r-- 7,057 bytes parent folder | download | duplicates (2)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
# This file is part of BBClone (The PHP web counter on steroids)

# $Header: /cvs/bbclone/lib/new_connect.php,v 1.27 2005/02/21 00:31:17 olliver Exp $

# Copyright (C) 2001-2005, the BBClone Team (see file doc/authors.txt
# distributed with this library)

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# See doc/copying.txt for details

# fallback in case something can't be resolved
function bbc_legacy_ext($ext, $array) {
  if (preg_match(":^[0-9]+$:", $ext)) return "numeric";
  elseif (!in_array($ext, $array)) return "unknown";
  else return $ext;
}

function bbc_get_extension($host, $addr) {
  global $BBC_IP2EXT_PATH;

  # generic extensions which need to be looked up first
  $gen_ext = array(
    "ac", "aero", "ag", "arpa", "as", "biz", "cc", "cd", "com", "coop", "cx", "edu", "eu", "gb", "gov", "gs", "info",
    "int", "mil", "ms", "museum", "name", "net", "nu", "org", "pro", "sc", "st", "su", "tk", "to", "tv", "vu", "ws"
  );
  # hosts with reliable country extension don't need to be looked up
  $cnt_ext = array(
    "ad", "ae", "af", "ai", "al", "am", "an", "ao", "aq", "ar", "at", "au", "aw", "az", "ba", "bb", "bd", "be", "bf",
    "bg", "bh", "bi", "bj", "bm", "bn", "bo", "br", "bs", "bt", "bv", "bw", "by", "bz", "ca", "cf", "cg", "ch", "ci",
    "ck", "cl", "cm", "cn", "co", "cr", "cs", "cu", "cv", "cy", "cz", "de", "dj", "dk", "dm", "do", "dz", "ec", "ee",
    "eg", "eh", "er", "es", "et", "fi", "fj", "fk", "fm", "fo", "fr", "ga", "gd", "ge", "gf", "gg", "gh", "gi", "gl",
    "gm", "gn", "gp", "gq", "gr", "gt", "gu", "gw", "gy", "hk", "hm", "hn", "hr", "ht", "hu", "id", "ie", "il", "im",
    "in", "io", "iq", "ir", "is", "it", "je", "jm", "jo", "jp", "ke", "kg", "kh", "ki", "km", "kn", "kp", "kr", "kw",
    "ky", "kz", "la", "lb", "lc", "li", "lk", "lr", "ls", "lt", "lu", "lv", "ly", "ma", "mc", "md", "mg", "mh", "mk",
    "ml", "mm", "mn", "mo", "mp", "mq", "mr", "mt", "mu", "mv", "mw", "mx", "my", "mz", "na", "nc", "ne", "nf", "ng",
    "ni", "nl", "no", "np", "nr", "nz", "om", "pa", "pe", "pf", "pg", "ph", "pk", "pl", "pm", "pn", "pr", "ps", "pt",
    "pw", "py", "qa", "re", "ro", "ru", "rw", "sa", "sb", "sd", "se", "sg", "sh", "si", "sj", "sk", "sl", "sm", "sn",
    "so", "sr", "sv", "sy", "sz", "tc", "td", "tf", "tg", "th", "tj", "tl", "tm", "tn", "tp", "tr", "tt", "tw", "tz",
    "ua", "ug", "uk", "um", "us", "uy", "uz", "va", "vc", "ve", "vg", "vi", "vn", "wf", "ye", "yt", "yu", "za", "zm",
    "zr", "zw"
  );

  $file = $BBC_IP2EXT_PATH.(substr($addr, 0, strpos($addr, ".")).".inc");
  $ext = strtolower(substr($host, (strrpos($host, ".") + 1)));

  # Don't look up if there's already a country extension
  if (in_array($ext, $cnt_ext)) return $ext;
  if (!is_readable($file)) return bbc_legacy_ext($ext, $gen_ext);

  $long = ip2long($addr);
  $long = sprintf("%u", $long);
  $fp = fopen($file, "rb");

  while (($range = fgetcsv($fp, 24, "|")) !== false) {
    if (($long >= $range[1]) && ($long <= ($range[1] + $range[2] - 1))) {
      # don't hose our stats if the database returns an unexpected extension
      $db_ext = (in_array($range[0], $cnt_ext) || in_array($range[0], $gen_ext)) ? $range[0] :
                bbc_legacy_ext($ext, $gen_ext);
      break;
    }
  }
  fclose($fp);

  return (!empty($db_ext) ? $db_ext : bbc_legacy_ext($ext, $gen_ext));
}

function bbc_update_connect($connect) {
  global $BBC_LIB_PATH;

  # Sanity check has already been made in mark_page.php
  foreach (array("robot", "browser", "os") as $cat) {
    require($BBC_LIB_PATH.$cat.".php");

    for ($i = 0, $j = count($$cat); $i < $j; $i++) {
      # Each line has the following structure (index no => purpose):
      # 0  => internal name used in $access and $last
      # 1  => icon (name + ".png" for the selected image)
      # 2  => full name displayed in global and detailed stats
      # 3  => whether to use version number ("1" = yes, "0" = no)
      # 4+ => identification rules as case insensitive regexps
      $line = explode("%", ${$cat}[$i]);

      for ($k = 4, $l = count($line); $k < $l; $k++) {
        # eregi() is intentionally used because some php installations don't
        # know the "i" switch of preg_match() and would generate phony compile
        # error messages
        if (!eregi($line[$k], $connect['agent'], $regs)) continue;

        $idx = count($regs) - 1;
        $connect[$cat] = $line[0];

        if (!empty($line[3]) && ($idx > 0) && preg_match(":[0-9]+:", $regs[$idx])) {
          $connect[$cat."_note"] = $regs[$idx];
        }
        break 2;
      }
    }
    if (!empty($connect['robot'])) break;
  }
  return $connect;
}

function bbc_update_access($connect) {
  global $access;

  # Assign an identification number to the new connection
  $connect['id'] = isset($access['stat']['totalcount']) ? ($access['stat']['totalcount'] + 1) : 1;

  # Recording the detected extension in the global statistics
  $access['stat']['ext'][$connect['ext']] = !isset($access['stat']['ext'][$connect['ext']]) ? 1 :
                                            ++$access['stat']['ext'][$connect['ext']];

  foreach (array("robot", "browser", "os") as $type) {
    if (($type == "robot") && (empty($connect['robot']))) continue;

    if (isset($access['stat'][$type][$connect[$type]])) $access['stat'][$type][$connect[$type]]++;
    else $access['stat'][$type][$connect[$type]] = 1;

    if (($type == "robot") && (!empty($connect['robot']))) break;
  }
  return $connect;
}


# Checks whether the same software is being used during a visit
function bbc_same_agent($old_connect, $new_connect) {
  $vars = array("browser", "browser_note", "os", "os_note", "robot", "robot_note");
  $max = count($vars);

  for ($i = 0 , $old = "", $new = ""; $i < $max; $i++) {
    $cur = $vars[$i];

    $old .= isset($old_connect[$cur]) && ($old_connect[$cur] != "other") ? $old_connect[$cur] : "";
    $new .= isset($new_connect[$cur]) && ($new_connect[$cur] != "other") ? $new_connect[$cur] : "";

  }

  if  ((empty($old) && empty($new)) ? ($new_connect['agent'] == $old_connect['agent']) : ($old == $new)) return true;

  return false;
}

# Updates the display in detailed stats if necessary
function bbc_update_detect($new, $old, $is_same) {
  $old['agent'] = $new['agent'];

  if (!$is_same) {
    $vars = array("browser", "browser_note", "os", "os_note", "robot", "robot_note");
    $cats = array("browser", "robot");

    foreach ($cats as $old_cat) {
      if (!empty($old[$old_cat])) {
        foreach ($cats as $new_cat) {
          if (!empty($new[$new_cat])) {
            for ($i = 0, $j = count($vars); $i < $j; $i++) {
              $cur_var = $vars[$i];

              if (isset($old[$cur_var])) unset($old[$cur_var]);
              if (isset($new[$cur_var])) $old[$cur_var] = $new[$cur_var];
            }
          }
        }
      }
    }
  }
  return $old;
}
?>