Subversion Repositories php_antispam

Rev

Rev 6 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 6 Rev 7
1
<?php
1
<?php
2
 
2
 
3
/*
3
/*
4
 * ViaThinkSoft Anti-Spam Script for PHP
4
 * ViaThinkSoft Anti-Spam Script for PHP
5
 * (C) 2009-2022 ViaThinkSoft
5
 * (C) 2009-2022 ViaThinkSoft
6
 * Revision: 2022-01-09 (Version 4.1)
6
 * Revision: 2022-11-05 (Version 4.1.1)
7
 * License: Apache 2.0 License
7
 * License: Apache 2.0 License
8
 */
8
 */
9
 
9
 
10
class VtsAntiSpam4 {
10
class VtsAntiSpam4 {
11
 
11
 
12
        public $garbageLength = 5;
12
        public $garbageLength = 5;
13
 
13
 
14
        public function __construct() {
14
        public function __construct() {
15
                self::randomize();
15
                self::randomize();
16
        }
16
        }
17
 
17
 
18
        private static function randomize() {
18
        private static function randomize() {
19
                // Anfagswert über aktuelle Mikrosekunde setzen
19
                // Anfagswert über aktuelle Mikrosekunde setzen
20
                // http://de2.php.net/manual/de/function.srand.php
20
                // http://de2.php.net/manual/de/function.srand.php
21
                list($usec, $sec) = explode(' ', microtime());
21
                list($usec, $sec) = explode(' ', microtime());
22
                $seed = (int)($sec + ((float)$usec * 100000));
22
                $seed = (int)((int)$sec + ((float)$usec * 100000));
23
                srand($seed);
23
                srand($seed);
24
        }
24
        }
25
 
25
 
26
        private function RandomString($len) {
26
        private function RandomString($len) {
27
                // http://www.jonasjohn.de/snippets/php/rand-str.htm
27
                // http://www.jonasjohn.de/snippets/php/rand-str.htm
28
                $randstr = '';
28
                $randstr = '';
29
                //srand((double)microtime()*1000000);
29
                //srand((double)microtime()*1000000);
30
                for($i=0;$i<$len;$i++) {
30
                for($i=0;$i<$len;$i++) {
31
                        $n = rand(48,120);
31
                        $n = rand(48,120);
32
                        while (($n >= 58 && $n <= 64) || ($n >= 91 && $n <= 96)) {
32
                        while (($n >= 58 && $n <= 64) || ($n >= 91 && $n <= 96)) {
33
                                $n = rand(48,120);
33
                                $n = rand(48,120);
34
                        }
34
                        }
35
                        $randstr .= chr($n);
35
                        $randstr .= chr($n);
36
                }
36
                }
37
                return $randstr;
37
                return $randstr;
38
        }
38
        }
39
 
39
 
40
        private function js_randombreaks() {
40
        private function js_randombreaks() {
41
                $len = rand(0, $this->garbageLength);
41
                $len = rand(0, $this->garbageLength);
42
                $r = '';
42
                $r = '';
43
                $one_line_comment = false;
43
                $one_line_comment = false;
44
                for($i=0;$i<$len;$i++) {
44
                for($i=0;$i<$len;$i++) {
45
                        $m = rand(0, 3);
45
                        $m = rand(0, 3);
46
                        if ($m == 0) {
46
                        if ($m == 0) {
47
                                $r .= ' ';
47
                                $r .= ' ';
48
                        } else if ($m == 1) {
48
                        } else if ($m == 1) {
49
                                $r .= '//';
49
                                $r .= '//';
50
                                $r .= $this->RandomString($i);
50
                                $r .= $this->RandomString($i);
51
                                $one_line_comment = true;
51
                                $one_line_comment = true;
52
                        } else if ($m == 2) {
52
                        } else if ($m == 2) {
53
                                $r .= "\r\n";
53
                                $r .= "\r\n";
54
                                $one_line_comment = false;
54
                                $one_line_comment = false;
55
                        } else {
55
                        } else {
56
                                $r .= "\t";
56
                                $r .= "\t";
57
                        }
57
                        }
58
                }
58
                }
59
                if ($one_line_comment) $r .= "\r\n";
59
                if ($one_line_comment) $r .= "\r\n";
60
                return $r;
60
                return $r;
61
        }
61
        }
62
 
62
 
63
        private function alas_js_crypt($text) {
63
        private function alas_js_crypt($text) {
64
                $tmp = '';
64
                $tmp = '';
65
                for ($i=0; $i<strlen($text); $i++) {
65
                for ($i=0; $i<strlen($text); $i++) {
66
                        $tmp .= $this->js_randombreaks();
66
                        $tmp .= $this->js_randombreaks();
67
                        $tmp .= 'document.write("&#'.ord(substr($text, $i, 1)).';");';
67
                        $tmp .= 'document.write("&#'.ord(substr($text, $i, 1)).';");';
68
                        $tmp .= $this->js_randombreaks();
68
                        $tmp .= $this->js_randombreaks();
69
                }
69
                }
70
                $tmp = $this->js_randombreaks().$tmp.$this->js_randombreaks();
70
                $tmp = $this->js_randombreaks().$tmp.$this->js_randombreaks();
71
                return $tmp;
71
                return $tmp;
72
        }
72
        }
73
 
73
 
74
        private function alas_noscript_crypt($text){
74
        private function alas_noscript_crypt($text){
75
                $tmp = '';
75
                $tmp = '';
76
                for ($i=0; $i<strlen($text); $i++) {
76
                for ($i=0; $i<strlen($text); $i++) {
77
                        $tmp .= '<span style="display:inline;">&#'.ord(substr($text, $i, 1)).';</span>';
77
                        $tmp .= '<span style="display:inline;">&#'.ord(substr($text, $i, 1)).';</span>';
78
                        $tmp .= '<!--'.$this->js_randombreaks().'-->';
78
                        $tmp .= '<!--'.$this->js_randombreaks().'-->';
79
                        $tmp .= '<span style="display:none;">'.$this->RandomString(rand(0, $this->garbageLength)).'</span>';
79
                        $tmp .= '<span style="display:none;">'.$this->RandomString(rand(0, $this->garbageLength)).'</span>';
80
                }
80
                }
81
                return $tmp;
81
                return $tmp;
82
        }
82
        }
83
 
83
 
84
        private function alas_js_write($text) {
84
        private function alas_js_write($text) {
85
                $text = str_replace('\\', '\\\\', $text);
85
                $text = str_replace('\\', '\\\\', $text);
86
                $text = str_replace('"', '\"', $text);
86
                $text = str_replace('"', '\"', $text);
87
                $text = str_replace('/', '\/', $text); // W3C Validation </a> -> <\/a>
87
                $text = str_replace('/', '\/', $text); // W3C Validation </a> -> <\/a>
88
 
88
 
89
                $ret  = '';
89
                $ret  = '';
90
                $ret .= $this->js_randombreaks();
90
                $ret .= $this->js_randombreaks();
91
                $ret .= 'document.write("'.$text.'");';
91
                $ret .= 'document.write("'.$text.'");';
92
                $ret .= $this->js_randombreaks();
92
                $ret .= $this->js_randombreaks();
93
 
93
 
94
                return $ret;
94
                return $ret;
95
        }
95
        }
96
 
96
 
97
        public function secure_email($email, $linktext, $crypt_linktext)
97
        public function secure_email($email, $linktext, $crypt_linktext)
98
        {
98
        {
99
                // No new lines to avoid a JavaScript error!
99
                // No new lines to avoid a JavaScript error!
100
                $linktext = str_replace("\r", ' ', $linktext);
100
                $linktext = str_replace("\r", ' ', $linktext);
101
                $linktext = str_replace("\n", ' ', $linktext);
101
                $linktext = str_replace("\n", ' ', $linktext);
102
 
102
 
103
                $aus = '';
103
                $aus = '';
104
                if ($email != '') {
104
                if ($email != '') {
105
                        $zid  = 'ALAS-4.0-'.DecHex(crc32($email)).'-'.DecHex(crc32($linktext)).'-'.($crypt_linktext ? 'S' : 'L');
105
                        $zid  = 'ALAS-4.0-'.DecHex(crc32($email)).'-'.DecHex(crc32($linktext)).'-'.($crypt_linktext ? 'S' : 'L');
106
                        $title = 'ViaThinkSoft "ALAS" Anti-Spam';
106
                        $title = 'ViaThinkSoft "ALAS" Anti-Spam';
107
 
107
 
108
                        $aus .= "<!-- BEGIN $title [ID $zid] -->\r\n";
108
                        $aus .= "<!-- BEGIN $title [ID $zid] -->\r\n";
109
                        $aus .= '<script language="JavaScript" type="text/javascript"><!--'."\n";
109
                        $aus .= '<script language="JavaScript" type="text/javascript"><!--'."\n";
110
                        $aus .= $this->alas_js_write('<a href="');
110
                        $aus .= $this->alas_js_write('<a href="');
111
                        $aus .= $this->alas_js_crypt('mailto:'.$email);
111
                        $aus .= $this->alas_js_crypt('mailto:'.$email);
112
                        $aus .= $this->alas_js_write('">');
112
                        $aus .= $this->alas_js_write('">');
113
                        $aus .= $crypt_linktext ? $this->alas_js_crypt($linktext) : $this->alas_js_write($linktext);
113
                        $aus .= $crypt_linktext ? $this->alas_js_crypt($linktext) : $this->alas_js_write($linktext);
114
                        $aus .= $this->alas_js_write('</a>').'// --></script>';
114
                        $aus .= $this->alas_js_write('</a>').'// --></script>';
115
 
115
 
116
                        $aus .= '<noscript>';
116
                        $aus .= '<noscript>';
117
                        if ($linktext != $email) $aus .= ($crypt_linktext ? $this->alas_noscript_crypt($linktext) : $linktext).' ';
117
                        if ($linktext != $email) $aus .= ($crypt_linktext ? $this->alas_noscript_crypt($linktext) : $linktext).' ';
118
                        $aus .= $this->alas_noscript_crypt("[ $email ]");
118
                        $aus .= $this->alas_noscript_crypt("[ $email ]");
119
                        $aus .= '</noscript>';
119
                        $aus .= '</noscript>';
120
                        $aus .= "\r\n<!-- END $title [ID $zid] -->\r\n";
120
                        $aus .= "\r\n<!-- END $title [ID $zid] -->\r\n";
121
                }
121
                }
122
 
122
 
123
                return $aus;
123
                return $aus;
124
        }
124
        }
125
 
125
 
126
        public function secure_email_autodetect($email, $linktext) {
126
        public function secure_email_autodetect($email, $linktext) {
127
                // Automatisch erkennen, ob der $linktext für Spambots interessant ist oder nicht
127
                // Automatisch erkennen, ob der $linktext für Spambots interessant ist oder nicht
128
                $pos = strpos($linktext, '@');
128
                $pos = strpos($linktext, '@');
129
 
129
 
130
                return $this->secure_email($email, $linktext, $pos !== false);
130
                return $this->secure_email($email, $linktext, $pos !== false);
131
        }
131
        }
132
 
132
 
133
        public function secure_email_identical_text($email) {
133
        public function secure_email_identical_text($email) {
134
                return $this->secure_email_autodetect($email, $email);
134
                return $this->secure_email_autodetect($email, $email);
135
        }
135
        }
136
 
136
 
137
}
137
}
138
 
138
 
139
# ------------------------------------------------------------------------------
139
# ------------------------------------------------------------------------------
140
 
140
 
141
function secure_email($email, $linktext, $crypt_linktext, $css_class='') {
141
function secure_email($email, $linktext, $crypt_linktext, $css_class='') {
142
        if (!empty($css_class)) {
142
        if (!empty($css_class)) {
143
                // TODO
143
                // TODO
144
                throw new Exception("CSSClass is not yet implemented in AntiSpam v4");
144
                throw new Exception("CSSClass is not yet implemented in AntiSpam v4");
145
        }
145
        }
146
 
146
 
147
        $antispam = new VtsAntiSpam4();
147
        $antispam = new VtsAntiSpam4();
148
        $res = $antispam->secure_email($email, $linktext, $crypt_linktext);
148
        $res = $antispam->secure_email($email, $linktext, $crypt_linktext);
149
        return $res;
149
        return $res;
150
}
150
}
151
 
151
 
152
function secure_email_autodetect($email, $linktext) {
152
function secure_email_autodetect($email, $linktext) {
153
        $antispam = new VtsAntiSpam4();
153
        $antispam = new VtsAntiSpam4();
154
        $res = $antispam->secure_email_autodetect($email, $linktext);
154
        $res = $antispam->secure_email_autodetect($email, $linktext);
155
        return $res;
155
        return $res;
156
}
156
}
157
 
157
 
158
function secure_email_identical_text($email) {
158
function secure_email_identical_text($email) {
159
        $antispam = new VtsAntiSpam4();
159
        $antispam = new VtsAntiSpam4();
160
        $res = $antispam->secure_email_identical_text($email);
160
        $res = $antispam->secure_email_identical_text($email);
161
        return $res;
161
        return $res;
162
}
162
}
163
 
163