My favorites | Sign in
Project Logo
                
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
<?php
class SpamCheck_Core{
protected $fields=array();

protected $checks=array();

protected $weights=array();

protected $scores=array();

protected $weighted_scores=array();
/**
* Set add field and value
*
* @param string name
* @param string value
* @return object
*/
public function add_field($name,$value)
{
$this->fields[$name]=$value;

return $this;
}
/**
* Get field by name
*
* @param string name
* @return string
*/
public function get_field($name)
{
if(isset($this->fields[$name]))
return $this->fields[$name];
return '';
}
/**
* Get all fields
*
* @return array
*/
public function get_fields()
{
return $this->fields;
}
/**
* Set weight of check
*
* @param string name
* @param int weight
* @return object
*/
public function set_weight($name,$weight=1)
{
$this->weights[$name]=(int) $weight;
return $this;
}
/**
* Add check class
* @param string name
* @param object check class
* @return object
*/
public function add_check($name,$weight=1)
{
$checker='SpamCheck_'.ucfirst($name);
$this->checks[$name]=new $checker($this);
$this->set_weight($name,(int) $weight);
return $this->checks[$name];
}
/**
* Get check
* @param string name
* @return object
*/
public function get_check($name)
{
if(isset($this->checks[$name]))
return $this->checks[$name];
}
/**
* Check agains all checks
*
* @return int spam score 0 bad, 100 good
*/
public function check()
{
if(empty($this->checks))
throw new Kohana_Exception('spamcheck.no_checks');

$scores=array();
$weighted_scores=array();

foreach($this->checks as $check_name=>$check)
{
$this->scores[$check_name] = $this->calculate_score($check->check());

$this->weighted_scores[$check_name] = $this->scores[$check_name]*$this->weights[$check_name];
}
//Sum of weighted scores
$weighted_sum=array_sum($this->weighted_scores);
//Number of checks
$num_checks=count($this->checks);
//Average score
$avg_score=$weighted_sum/$num_checks;
//Max score
$avg_score= $avg_score > 100 ? 100 : $avg_score;
//Min score
$avg_score= $avg_score < -100 ? -100 : $avg_score;

return $avg_score;
}
/**
* Calculate score of the check
*
* @param mixed score
* @return int number of rows deleted
*/
public function calculate_score($result)
{
if(is_bool($result))
{
return $result==true ? 100 : -100;
}
return (int) $result;
}
/**
* Get weighted scores after check
* @return array weighted scores
*/
public function get_weighted_scores(){
return $this->weighted_scores;
}
/**
* Like check() only returns boolean (score below zero is spam, above not)
* @return boolean
*/
public function is_spam($threshold=0)
{
if($this->check() < $threshold)
return true;
return false;
}
}
?>
Show details Hide details

Change log

r93 by maartenvanvliet on Jul 09, 2008   Diff
added threshold for is_spam, set_field for
links check
Go to: 
Project members, sign in to write a code review

Older revisions

r92 by maartenvanvliet on Jun 26, 2008   Diff
cleanup docs
r91 by maartenvanvliet on Jun 26, 2008   Diff
added is_spam() and
get_weighted_scores()
r89 by maartenvanvliet on Jun 26, 2008   Diff
added empty links checker, changed
algorithm
All revisions of this file

File info

Size: 2878 bytes, 145 lines
Hosted by Google Code