<?php
-// $Id: spam.php,v 1.178 2007/06/16 03:23:34 henoheno Exp $
+// $Id: spam.php,v 1.191 2007/06/24 03:13:09 henoheno Exp $
// Copyright (C) 2006-2007 PukiWiki Developers Team
// License: GPL v2 or (at your option) any later version
//
// References:
// http://www.freebsd.org/cgi/man.cgi?query=strings (Man-page of GNU strings)
// http://www.pcre.org/pcre.txt
-function strings($binary = '', $min_len = 4, $ignore_space = FALSE, $multibyte = TRUE)
+// Note: mb_ereg_replace() is one of mbstring extension's functions
+// and need to init its encoding.
+function strings($binary = '', $min_len = 4, $ignore_space = FALSE, $multibyte = FALSE)
{
// String only
$binary = (is_array($binary) || $binary === TRUE) ? '' : strval($binary);
// 3: Host
'\[[0-9a-f:.]+\]' . '|' . // IPv6([colon-hex and dot]): RFC2732
'(?:[0-9]{1,3}\.){3}[0-9]{1,3}' . '|' . // IPv4(dot-decimal): 001.22.3.44
- '[a-z0-9][a-z0-9.-]+[a-z0-9]' . // hostname(FQDN) : foo.example.org
+ '[a-z0-9_-][a-z0-9_.-]+[a-z0-9_-]' . // hostname(FQDN) : foo.example.org
')' .
'(?::([0-9]*))?' . // 4: Port
'((?:/+[^\s<>"\'\[\]/\#]+)*/+)?' . // 5: Directory path or path-info
}
// Removing sequential spaces and too short lines
- $binary = strings($binary, $length, TRUE, TRUE);
+ $binary = strings($binary, $length, TRUE, FALSE); // Multibyte NOT needed
- // Words between spaces
- $binary = preg_replace('/[ \t][\w \t]+[ \t]/', ' ', $binary);
+ // Remove words (has no '<>[]:') between spaces
+ $binary = preg_replace('/[ \t][\w.,()\ \t]+[ \t]/', ' ', $binary);
return $binary;
}
if (! is_string($string)) return '';
$string = spam_uri_removing_hocus_pocus(rawurldecode($string), $method);
+ //var_dump(htmlspecialchars($string));
// Domain exposure (simple)
// http://victim.example.org/nasty.example.org/path#frag
! is_array($progress['hosts']) ||
empty($progress['hosts'])) return '';
- $result = '';
-
- // Generate a $trie
+ // Generate a responsible $trie
$trie = array();
foreach($progress['hosts'] as $value) {
-
- // Try to shorten (pre) -- array('example.com', 'bar', 'foo')
+ // 'A.foo.bar.example.com'
$resp = whois_responsibility($value); // 'example.com'
- $rest = rtrim(substr($value, 0, - strlen($resp)), '.'); // 'foo.bar'
- if ($rest) {
- $parts = explode('.', delimiter_reverse('.' . $rest));
- array_unshift($parts, $resp);
+ if (empty($resp)) {
+ // One or more test, or do nothing here
+ $resp = strval($value);
+ $rest = '';
} else {
- $parts = array($resp, $rest);
+ $rest = rtrim(substr($value, 0, - strlen($resp)), '.'); // 'A.foo.bar'
}
-
- $trie = array_merge_recursive(
- $trie,
- array_leaf($parts, TRUE, $value)
- );
+ $trie = array_merge_recursive($trie, array($resp => array($rest => NULL)));
}
- // Try to shorten (post, non-recursive) -- 'foo.bar.example.com'
- array_joinbranch_leaf($trie, '.', 0, TRUE);
-
- // Sort and flatten -- 'A.foo.bar.example.com, B.foo.bar.example.com'
+ // Format: var_export_shrink() -like output
+ $result = array();
+ ksort_by_domain($trie);
foreach(array_keys($trie) as $key) {
- if (is_array($trie[$key])) {
- ksort_by_domain($trie[$key]);
- $trie[$key] = implode(', ', array_flat_leaves($trie[$key]));
+ ksort_by_domain($trie[$key]);
+ if (count($trie[$key]) == 1 && key($trie[$key]) == '') {
+ // Just one 'responsibility.example.com'
+ $result[] = ' \'' . $key . '\',';
+ } else {
+ // One subdomain-or-host, or several ones
+ $subs = array();
+ foreach(array_keys($trie[$key]) as $sub) {
+ if ($sub == '') {
+ $subs[] = $key;
+ } else {
+ $subs[] = $sub . '.' . $key;
+ }
+ }
+ $result[] = ' \'' . $key . '\' => \'' . implode(', ', $subs) . '\',';
}
+ unset($trie[$key]);
}
-
- // TODO: ltrim('.') from $trie
-
- ksort_by_domain($trie);
-
- // TODO: from array('foobar' => 'foobar') to 'foobar'
-
- return var_export_shrink($trie, TRUE, TRUE);
+ return
+ 'array (' . "\n" .
+ implode("\n", $result) . "\n" .
+ ')';
}
// ksort() by domain
$array = $result;
}
-// array('F' => array('B' => array('C' => array('d' => array('' => 'foobar')))))
-// to
-// array('F.B.C.d.' => 'foobar')
-function array_joinbranch_leaf(& $array, $delim = '.', $limit = 0, $reverse = FALSE)
-{
- $result = array();
- if (! is_array($array)) return $result; // Nothing to do
-
- $limit = max(0, intval($limit));
- $cstack = array();
-
- foreach(array_keys($array) as $key) {
- $kstack = array();
- $k = -1;
-
- $single = array($key => & $array[$key]); // Keep it single
- $cursor = & $single;
- while(is_array($cursor) && count($cursor) == 1) { // Once
- ++$k;
- $kstack[] = key($cursor);
- $cursor = & $cursor[$kstack[$k]];
- if ($limit != 0 && $k == $limit) break;
- }
-
- // Relink
- if ($k != 0) {
- if ($reverse) $kstack = array_reverse($kstack);
- $joinkey = implode($delim, $kstack);
-
- unset($array[$key]);
- $array[$joinkey] = & $cursor;
- $result[$joinkey] = $k + 1; // Key seems not an single array => joined length
- }
- }
-
- return $result;
-}
-
-
// Check responsibility-root of the FQDN
// 'foo.bar.example.com' => 'example.com' (.com has the last whois for it)
// 'foo.bar.example.au' => 'example.au' (.au has the last whois for it)
'info' => TRUE,
),
+ // ccTLD: Bahrain
+ // NIC : http://www.inet.com.bh/ (.bh policies not found)
+ // Whois: (Not available) http://www.inet.com.bh/
+ 'bh' => array(
+ // Observed
+ 'com' => TRUE,
+ 'edu' => TRUE,
+ 'gov' => TRUE,
+ 'org' => TRUE,
+ ),
+
// ccTLD: China
// NIC : http://www.cnnic.net.cn/en/index/
// Whois: http://ewhois.cnnic.cn/
'zj' => TRUE,
),
+ // ccTLD: India
+ // NIC : http://www.inregistry.in/
+ // Whois: http://www.inregistry.in/whois_search/
+ 'in' => array(
+ // Policies http://www.inregistry.in/policies/
+ 'ac' => TRUE,
+ 'co' => TRUE,
+ 'firm' => TRUE,
+ 'gen' => TRUE,
+ 'gov' => TRUE,
+ 'ind' => TRUE,
+ 'mil' => TRUE,
+ 'net' => TRUE,
+ 'org' => TRUE,
+ 'res' => TRUE,
+ // Reserved Names by the government (for the 2nd level)
+ // http://www.inregistry.in/policies/reserved_names
+ ),
+
// ccTLD: South Korea
// NIC : http://www.nic.or.kr/english/
// Whois: http://whois.nida.or.kr/english/
'ac' => TRUE,
'ad' => TRUE,
'co' => TRUE,
+ 'ed' => TRUE,
'go' => TRUE,
'gr' => TRUE,
'lg' => TRUE,
'yokohama' => TRUE,
),
+ // ccTLD: Mexico
+ // NIC : http://www.nic.mx/
+ // Whois: http://www.nic.mx/es/Busqueda.Who_Is
+ 'mx' => array(
+ // Politicas Generales de Nombres de Dominio
+ // http://www.nic.mx/es/Politicas?CATEGORY=INDICE
+ 'com' => TRUE,
+ 'edu' => TRUE,
+ 'gob' => TRUE,
+ 'net' => TRUE,
+ 'org' => TRUE,
+ ),
+
+ // ccTLD: Russia
+ // NIC : http://www.cctld.ru/en/
+ // Whois: http://www.ripn.net:8080/nic/whois/en/
+ 'ru' => array(
+ // List of Reserved second-level Domain Names
+ // http://www.cctld.ru/en/doc/detail.php?id21=20&i21=2
+
+ // Organizational
+ 'ac' => TRUE,
+ 'com' => TRUE,
+ 'edu' => TRUE,
+ 'gov' => TRUE,
+ 'int' => TRUE,
+ 'mil' => TRUE,
+ 'net' => TRUE,
+ 'org' => TRUE,
+ 'pp' => TRUE,
+ //'test' => TRUE,
+
+ // Geographic
+ 'adygeya' => TRUE,
+ 'altai' => TRUE,
+ 'amur' => TRUE,
+ 'amursk' => TRUE,
+ 'arkhangelsk' => TRUE,
+ 'astrakhan' => TRUE,
+ 'baikal' => TRUE,
+ 'bashkiria' => TRUE,
+ 'belgorod' => TRUE,
+ 'bir' => TRUE,
+ 'bryansk' => TRUE,
+ 'buryatia' => TRUE,
+ 'cbg' => TRUE,
+ 'chel' => TRUE,
+ 'chelyabinsk' => TRUE,
+ 'chita' => TRUE,
+ 'chukotka' => TRUE,
+ 'chuvashia' => TRUE,
+ 'cmw' => TRUE,
+ 'dagestan' => TRUE,
+ 'dudinka' => TRUE,
+ 'e-burg' => TRUE,
+ 'fareast' => TRUE,
+ 'grozny' => TRUE,
+ 'irkutsk' => TRUE,
+ 'ivanovo' => TRUE,
+ 'izhevsk' => TRUE,
+ 'jamal' => TRUE,
+ 'jar' => TRUE,
+ 'joshkar-ola' => TRUE,
+ 'k-uralsk' => TRUE,
+ 'kalmykia' => TRUE,
+ 'kaluga' => TRUE,
+ 'kamchatka' => TRUE,
+ 'karelia' => TRUE,
+ 'kazan' => TRUE,
+ 'kchr' => TRUE,
+ 'kemerovo' => TRUE,
+ 'khabarovsk' => TRUE,
+ 'khakassia' => TRUE,
+ 'khv' => TRUE,
+ 'kirov' => TRUE,
+ 'kms' => TRUE,
+ 'koenig' => TRUE,
+ 'komi' => TRUE,
+ 'kostroma' => TRUE,
+ 'krasnoyarsk' => TRUE,
+ 'kuban' => TRUE,
+ 'kurgan' => TRUE,
+ 'kursk' => TRUE,
+ 'kustanai' => TRUE,
+ 'kuzbass' => TRUE,
+ 'lipetsk' => TRUE,
+ 'magadan' => TRUE,
+ 'magnitka' => TRUE,
+ 'mari-el' => TRUE,
+ 'mari' => TRUE,
+ 'marine' => TRUE,
+ 'mordovia' => TRUE,
+ 'mosreg' => TRUE,
+ 'msk' => TRUE,
+ 'murmansk' => TRUE,
+ 'mytis' => TRUE,
+ 'nakhodka' => TRUE,
+ 'nalchik' => TRUE,
+ 'nkz' => TRUE,
+ 'nnov' => TRUE,
+ 'norilsk' => TRUE,
+ 'nov' => TRUE,
+ 'novosibirsk' => TRUE,
+ 'nsk' => TRUE,
+ 'omsk' => TRUE,
+ 'orenburg' => TRUE,
+ 'oryol' => TRUE,
+ 'oskol' => TRUE,
+ 'palana' => TRUE,
+ 'penza' => TRUE,
+ 'perm' => TRUE,
+ 'pskov' => TRUE,
+ 'ptz' => TRUE,
+ 'pyatigorsk' => TRUE,
+ 'rnd' => TRUE,
+ 'rubtsovsk' => TRUE,
+ 'ryazan' => TRUE,
+ 'sakhalin' => TRUE,
+ 'samara' => TRUE,
+ 'saratov' => TRUE,
+ 'simbirsk' => TRUE,
+ 'smolensk' => TRUE,
+ 'snz' => TRUE,
+ 'spb' => TRUE,
+ 'stavropol' => TRUE,
+ 'stv' => TRUE,
+ 'surgut' => TRUE,
+ 'syzran' => TRUE,
+ 'tambov' => TRUE,
+ 'tatarstan' => TRUE,
+ 'tom' => TRUE,
+ 'tomsk' => TRUE,
+ 'tsaritsyn' => TRUE,
+ 'tsk' => TRUE,
+ 'tula' => TRUE,
+ 'tuva' => TRUE,
+ 'tver' => TRUE,
+ 'tyumen' => TRUE,
+ 'udm' => TRUE,
+ 'udmurtia' => TRUE,
+ 'ulan-ude' => TRUE,
+ 'vdonsk' => TRUE,
+ 'vladikavkaz' => TRUE,
+ 'vladimir' => TRUE,
+ 'vladivostok' => TRUE,
+ 'volgograd' => TRUE,
+ 'vologda' => TRUE,
+ 'voronezh' => TRUE,
+ 'vrn' => TRUE,
+ 'vyatka' => TRUE,
+ 'yakutia' => TRUE,
+ 'yamal' => TRUE,
+ 'yaroslavl' => TRUE,
+ 'yekaterinburg' => TRUE,
+ 'yuzhno-sakhalinsk' => TRUE,
+ 'zgrad' => TRUE,
+ ),
+
+ // ccTLD: Seychelles
+ // NIC : http://www.nic.sc/
+ // Whois: (Not available)
+ 'sc' => array(
+ // http://www.nic.sc/policies.html
+ 'com' => TRUE,
+ 'edu' => TRUE,
+ 'gov' => TRUE,
+ 'net' => TRUE,
+ 'org' => TRUE,
+ ),
+
+ // ccTLD: Taiwan
+ // NIC : http://www.twnic.net.tw/
+ // Whois: http://www.twnic.net.tw/
+ 'tw' => array(
+ // Guidelines for Administration of Domain Name Registration
+ // http://www.twnic.net.tw/english/dn/dn_02.htm
+ // II. Types of TWNIC Domain Names and Application Requirements
+ // http://www.twnic.net.tw/english/dn/dn_02_b.htm
+ 'club' => TRUE,
+ 'com' => TRUE,
+ 'ebiz' => TRUE,
+ 'edu' => TRUE,
+ 'game' => TRUE,
+ 'gov' => TRUE,
+ 'idv' => TRUE,
+ 'mil' => TRUE,
+ 'net' => TRUE,
+ 'org' => TRUE,
+ // Reserved words for the 2nd level
+ // http://mydn.twnic.net.tw/en/dn02/INDEX.htm
+ ),
+
+ // ccTLD: Tanzania
+ // NIC : http://www.psg.com/dns/tz/
+ // Whois: (Not available)
+ 'tz' => array(
+ // TZ DOMAIN NAMING STRUCTURE
+ // http://www.psg.com/dns/tz/tz.txt
+ 'ac' => TRUE,
+ 'co' => TRUE,
+ 'go' => TRUE,
+ 'ne' => TRUE,
+ 'or' => TRUE,
+ ),
+
// ccTLD: Ukraine
// NIC : http://www.nic.net.ua/
// Whois: http://whois.com.ua/
'wv' => TRUE, // West Virginia
'wy' => TRUE, // Wyoming
),
+
+ // ccTLD: South Africa
+ // NIC : http://www.zadna.org.za/
+ // Whois:
+ // ac.za http://www.tenet.ac.za/cgi/cgi_domainquery.exe
+ // co.za http://co.za/whois.shtml
+ // gov.za http://dnsadmin.gov.za/
+ // org.za http://www.org.za/
+ 'za' => array(
+ // Second-level subdomains of .ZA
+ // http://www.zadna.org.za/slds.html
+ 'ac' => TRUE,
+ 'city' => TRUE,
+ 'co' => TRUE,
+ 'edu' => TRUE,
+ 'gov' => TRUE,
+ 'law' => TRUE,
+ 'mil' => TRUE,
+ 'nom' => TRUE,
+ 'org' => TRUE,
+ 'school' => array(
+ // Provincial Domains
+ // http://www.esn.org.za/dns/
+ 'ecape' => TRUE,
+ 'fs.' => TRUE,
+ 'gp' => TRUE,
+ 'kzn' => TRUE,
+ 'lp' => TRUE,
+ 'mpm' => TRUE,
+ 'ncape' => TRUE,
+ 'nw' => TRUE,
+ 'wcape' => TRUE,
+ ),
+ ),
);
if (! is_string($fqdn)) return '';
+ if (is_ip($fqdn)) return $fqdn;
$result = array();
$dcursor = & $domain;
$array = array_reverse(explode('.', $fqdn));
$i = 0;
while(TRUE) {
+ if (! isset($array[$i])) break;
$acursor = $array[$i];
if (is_array($dcursor) && isset($dcursor[$acursor])) {
$result[] = & $array[$i];
// NOTE: Call this function from various blocking feature, to disgueise the reason 'why blocked'
function spam_exit($mode = '', $data = array())
{
-
$exit = TRUE;
+
switch ($mode) {
case '':
echo("\n");
if (empty($progress['is_spam'])) {
spam_dispose();
} else {
- $target = string($target, 0); // Removing "\0" etc
+
+// TODO: detect encoding from $target for mbstring functions
+// $tmp = array();
+// foreach(array_keys($target) as $key) {
+// $tmp[strings($key, 0, FALSE, TRUE)] = strings($target[$key], 0, FALSE, TRUE); // Removing "\0" etc
+// }
+// $target = & $tmp;
+
pkwk_spamnotify($action, $page, $target, $progress, $method);
spam_exit($exitmode, $progress);
}