OSDN Git Service

uri_pickup_normalize_composite_path():
[pukiwiki/pukiwiki_sandbox.git] / spam / spam_pickup.php
index bb306d2..b0ecc99 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-// $Id: spam_pickup.php,v 1.56 2007/08/20 14:50:31 henoheno Exp $
+// $Id: spam_pickup.php,v 1.66 2009/01/02 09:30:50 henoheno Exp $
 // Copyright (C) 2006-2007 PukiWiki Developers Team
 // License: GPL v2 or (at your option) any later version
 //
@@ -34,7 +34,7 @@ function uri_pickup($string = '')
                        '[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
+               '((?:/+[^\s<>"\'\[\]/\#]+)*/+)?' .      // 5: Directory path
                '([^\s<>"\'\[\]\#?]+)?' .                       // 6: File?
                '(?:\?([^\s<>"\'\[\]\#]+))?' .          // 7: Query string
                '(?:\#([a-z0-9._~%!$&\'()*+,;=:@-]*))?' .       // 8: Fragment
@@ -42,18 +42,18 @@ function uri_pickup($string = '')
                 $string, $array, PREG_SET_ORDER | PREG_OFFSET_CAPTURE
        );
 
-       // Format the $array
+       // Reformat the $array
        static $parts = array(
                1 => 'scheme', 2 => 'userinfo', 3 => 'host', 4 => 'port',
                5 => 'path', 6 => 'file', 7 => 'query', 8 => 'fragment'
        );
-       $default = array('');
+       $default = array(0 => '', 1 => -1);
        foreach(array_keys($array) as $uri) {
                $_uri = & $array[$uri];
                array_rename_keys($_uri, $parts, TRUE, $default);
                $offset = $_uri['scheme'][1]; // Scheme's offset = URI's offset
                foreach(array_keys($_uri) as $part) {
-                       $_uri[$part] = & $_uri[$part][0];       // Remove offsets
+                       $_uri[$part] = $_uri[$part][0]; // Remove offsets
                }
        }
 
@@ -115,13 +115,12 @@ function uri_pickup_implode($uri = array())
        return implode('', $tmp);
 }
 
-
 // ---------------------
 // URI normalization
 
 // Normalize an array of URI arrays
 // NOTE: Give me the uri_pickup() results
-function uri_pickup_normalize(& $pickups, $destructive = TRUE)
+function uri_pickup_normalize(& $pickups, $destructive = TRUE, $pathfile = FALSE)
 {
        if (! is_array($pickups)) return $pickups;
 
@@ -146,6 +145,28 @@ function uri_pickup_normalize(& $pickups, $destructive = TRUE)
                }
        }
 
+       if ($pathfile) {
+               return uri_pickup_normalize_pathfile($pickups);
+       } else {
+               return $pickups;
+       }
+}
+
+// Normalize: 'path' + 'file' = 'path' (Similar structure using PHP's "parse_url()" function)
+// NOTE: In some case, 'file' DOES NOT mean _filename_.
+// [EXAMPLE] http://example.com/path/to/directory-accidentally-not-ended-with-slash
+function uri_pickup_normalize_pathfile(& $pickups)
+{
+       if (! is_array($pickups)) return $pickups;
+
+       foreach (array_keys($pickups) as $key) {
+               $_key = & $pickups[$key];
+               if (isset($_key['path'], $_key['file'])) {
+                       $_key['path'] = $_key['path'] . $_key['file'];
+                       unset($_key['file']);
+               }
+       }
+
        return $pickups;
 }
 
@@ -189,13 +210,14 @@ function scheme_normalize($scheme = '', $abbrevs_harmfull = TRUE)
 // www.foo.bar => foo.bar
 // www.10.20   => www.10.20 (Invalid hostname)
 // NOTE:
-//   'www' is  mostly used as traditional hostname of WWW server.
-//   'www.foo.bar' may be identical with 'foo.bar'.
+//   'www' is basically traditional hostname for WWW server.
+//   In these case, 'www.foo.bar' MAY be identical with 'foo.bar'.
 function host_normalize($host = '')
 {
        if (! is_string($host)) return '';
 
        $host = strtolower($host);
+
        $matches = array();
        if (preg_match('/^www\.(.+\.[a-z]+)$/', $host, $matches)) {
                return $matches[1];
@@ -548,7 +570,7 @@ function area_pickup($string = '', $method = array())
        // [OK] [link]http://nasty.example.com/[/link]
        // [OK] [url=http://nasty.example.com]visit http://nasty.example.com/[/url]
        // [OK] [link http://nasty.example.com/]buy something[/link]
-       $regex = '#\[(url|link)\b[^\]]*\].*?\[/\1\b[^\]]*(\])#is';
+       $regex = '#\[(url|link|img|email)\b[^\]]*\].*?\[/\1\b[^\]]*(\])#is';
        if (isset($method['area_bbcode'])) {
                $areas = array();
                $count = isset($method['asap']) ?
@@ -606,9 +628,12 @@ function area_measure($areas, & $array, $belief = -1, $a_key = 'area', $o_key =
 // ---------------------
 // Spam-uri pickup
 
-// Preprocess: Removing uninterest part for URI detection
+// Preprocess: Removing/Modifying uninterest part for URI detection
 function spam_uri_removing_hocus_pocus($binary = '', $method = array())
 {
+       $from = $to = array();
+
+       // Remove sequential spaces and too short lines
        $length = 4 ; // 'http'(1) and '://'(2) and 'fqdn'(1)
        if (is_array($method)) {
                // '<a'(2) or 'href='(5) or '>'(1) or '</a>'(4)
@@ -617,14 +642,17 @@ function spam_uri_removing_hocus_pocus($binary = '', $method = array())
                    isset($method['area_bbcode']) || isset($method['uri_bbcode']))
                                $length = 1;    // Seems not effective
        }
-
-       // Removing sequential spaces and too short lines
        $binary = strings($binary, $length, TRUE, FALSE); // Multibyte NOT needed
 
+       // Remove/Replace quoted-spaces within tags
+       $from[] = '#(<\w+ [^<>]*?\w ?= ?")([^"<>]*? [^"<>]*)("[^<>]*?>)#ie';
+       $to[]   = "'$1' . str_replace(' ' , '%20' , trim('$2')) . '$3'";
+
        // Remove words (has no '<>[]:') between spaces
-       $binary = preg_replace('/[ \t][\w.,()\ \t]+[ \t]/', ' ', $binary);
+       $from[] = '/[ \t][\w.,()\ \t]+[ \t]/';
+       $to[]   = ' ';
 
-       return $binary;
+       return preg_replace($from, $to, $binary);
 }
 
 // Preprocess: Domain exposure callback (See spam_uri_pickup_preprocess())
@@ -681,7 +709,6 @@ function spam_uri_pickup_preprocess($string = '', $method = array())
        );
 
        $string = spam_uri_removing_hocus_pocus($string, $method);
-       //var_dump(htmlspecialchars($string));
 
        // Domain exposure (simple)
        // http://victim.example.org/nasty.example.org/path#frag
@@ -689,15 +716,20 @@ function spam_uri_pickup_preprocess($string = '', $method = array())
        $string = preg_replace(
                '#h?ttp://' .
                '(' .
+                       'a9\.com/' . '|' .
+                       'aboutus\.org/' . '|' .
+                       'alexa\.com/data/details\?url='  . '|' .
                        'ime\.(?:nu|st)/' . '|' .       // 2ch.net
                        'link\.toolbot\.com/' . '|' .
                        'urlx\.org/' . '|' .
                        'big5.51job.com/gate/big5/'      . '|' .
                        'big5.china.com/gate/big5/'      . '|' .
+                       'big5.shippingchina.com:8080/' . '|' .
                        'big5.xinhuanet.com/gate/big5/' . '|' .
                        'bhomiyo.com/en.xliterate/' . '|' .
                        'google.com/translate_c\?u=(?:http://)?' . '|' .
-                       'web.archive.org/web/2[^/]*/(?:http://)?' .
+                       'web.archive.org/web/2[^/]*/(?:http://)?' . '|' .
+                       'technorati.com/blogs/' .
                ')' .
                '([a-z0-9.%_-]+\.[a-z0-9.%_-]+)' .      // nasty.example.org
                '#i',
@@ -723,7 +755,8 @@ function spam_uri_pickup_preprocess($string = '', $method = array())
                                // ...
                        ')' .
                        '/' .
-                       '([a-z0-9?=&.%_/\'\\\+-]+)' .                           // 3:path/?query=foo+bar+
+                       //TODO: Specify URL-enable characters
+                       '([a-z0-9?=&.,%_/\'\\\+-]+)' .                          // 3:path/?query=foo+bar+
                        '(?:\b|%20)site:([a-z0-9.%_-]+\.[a-z0-9.%_-]+)' .       // 4:site:nasty.example.com
                        '()' .                                                                          // 5:Preserve or remove?
                        '#i',
@@ -781,8 +814,9 @@ function spam_uri_pickup($string = '', $method = array())
        }
 
        // Remove 'offset's for area_measure()
-       foreach(array_keys($array) as $key)
+       foreach(array_keys($array) as $key) {
                unset($array[$key]['area']['offset']);
+       }
 
        return $array;
 }