1 <?php
2 
3 /**
4  * Created by IntelliJ IDEA.
5  * User: clay
6  * Date: 8/23/15
7  * Time: 9:49 PM
8  */
9 
10 class ResponseCategory {
11     public $frequency;
12     public $confidence;
13 
14     function __construct($frequency = 0, $confidence = 0.0)
15     {
16         $this->frequency = $frequency;
17         $this->confidence = $confidence;
18     }
19 
20 }
21 
22 class Response {
23     public $email;
24     public $username;
25     public $ip;
26 
27     public function __construct($username = null, $email = null, $ip = null)
28     {
29         if ($username == null) {
30             $username = new ResponseCategory();
31         }
32         if ($email == null) {
33             $email = new ResponseCategory();
34         }
35         if ($ip == null) {
36             $ip = new ResponseCategory();
37         }
38 
39         $this->email = $email;
40         $this->username = $username;
41         $this->ip = $ip;
42     }
43 
44     public function fromJson($string)
45     {
46         $response = json_decode($string, true);
47 
48         if (isset($response['username'])) {
49             $this->username = $this->ResponseCategoryFromAssoc($response['username']);
50         }
51 
52         if (isset($response['email'])) {
53             $this->email = $this->ResponseCategoryFromAssoc($response['email']);
54         }
55 
56         if (isset($response['ip'])) {
57             $this->ip = $this->ResponseCategoryFromAssoc($response['ip']);
58         }
59     }
60 
61     /**
62      * @param $assoc
63      */
64     protected function ResponseCategoryFromAssoc($assoc)
65     {
66         $frequency = 0;
67         $confidence = 0.0;
68 
69         if (isset($assoc['frequency'])) {
70             $frequency = $assoc['frequency'];
71         }
72         if (isset($assoc['confidence'])) {
73             $confidence = $assoc['confidence'];
74         }
75         return new ResponseCategory($frequency, $confidence);
76     }
77 }
78 
79 
80 class ResponseChecker
81 {
82 
83     public $accepted;
84     public $confidence;
85     public $frequency;
86     public $trigger;
87 
88     /// Maximum Confidence which a category may have and be considered a valid user
89     public $tolerance;
90 
91     public function __construct($tolerance = 10.0)
92     {
93         $this->accepted = true;
94         $this->confidence = 0.0;
95         $this->trigger = "";
96 
97         $this->tolerance = $tolerance;
98     }
99 
100     /**
101      * Check a StopForumSpam.com response, with side effects of populating the object's properties.
102      *
103      * @param $json Raw JSON input string
104      * @return bool
105      */
106     public function userIsValid($json)
107     {
108         $response = new Response();
109         $response->fromJson($json);
110         $result = true;
111 
112         if (($result = $this->categoryIsValid($response->username)) === false) {
113             $this->trigger = "username";
114         } else if (($result = $this->categoryIsValid($response->email)) === false) {
115             $this->trigger = "email";
116         } else if (($result = $this->categoryIsValid($response->ip)) === false) {
117             $this->trigger = "ip";
118         }
119 
120         $this->accepted = $result;
121 
122         return $result;
123     }
124 
125     /**
126      * Check an individual category to see if it represents a valid user.  To be a valid user the category must have
127      * occurs of 0 or confidence of less than the global tolerance.
128      *
129      * *Side Effects:* Populates $confidence if validity check fails.
130      *
131      * @param ResponseCategory $category
132      * @return bool
133      */
134     private function categoryIsValid(ResponseCategory $category)
135     {
136         if ($category->frequency > 0 && $category->confidence > $this->tolerance) {
137             $this->confidence = $category->confidence;
138             $this->frequency = $category->frequency;
139             return false;
140         }
141         return true;
142     }
143 
144 
145 }