11<?php
2+ require_once (dirname (__FILE__ ) . 'autoload.php ' );
3+
24/**
35 * CleanTalk anti-spam script for any web form
46 *
1113 * @see https://github.com/CleanTalk/php-antispam
1214 *
1315 */
14-
16+ use lib \CleantalkRequest ;
17+ use lib \Cleantalk ;
18+ use lib \CleantalkHelper ;
1519/*
1620 CleanTalk's global vars
1721*/
@@ -100,19 +104,26 @@ function ct_process_submission() {
100104 }
101105 }
102106 }
107+ // Take params from config
108+ $ config_url = 'http://moderate.cleantalk.ru ' ;
109+ $ auth_key = null ; // Set Cleantalk auth key
110+
103111
104- $ data = array (
105- 'auth_key ' => '__CT_KEY__ ' ,
106- 'method_name ' => 'check_newuser ' ,
107- 'agent ' => 'php-1.1 ' ,
108- 'sender_ip ' => ct_session_ip ($ _SERVER ['REMOTE_ADDR ' ]),
109- 'sender_email ' => $ sender_email ,
110- 'js_on ' => $ ct_checkjs ,
111- 'submit_time ' => $ ct_submit_time ,
112- 'sender_info ' => null ,
113- );
112+ // The facility in which to store the query parameters
113+ $ ct_request = new CleantalkRequest ();
114114
115- $ result = ct_send_request ($ data , $ ct_server_url );
115+ $ ct_request ->auth_key = $ auth_key ;
116+ $ ct_request ->sender_email = $ sender_email ;
117+ $ ct_request ->agent = 'php-api ' ;
118+ $ ct_request ->sender_ip = CleantalkHelper::ip_get (array ('real ' ), false );
119+ $ ct_request ->js_on = $ ct_checkjs ; # Site visitor has JavaScript
120+ $ ct_request ->submit_time = $ ct_submit_time ; # Seconds from start form filling till the form POST
121+
122+ $ ct = new Cleantalk ();
123+ $ ct ->server_url = $ config_url ;
124+
125+ // Check
126+ $ ct_result = $ ct ->isAllowUser ($ ct_request );
116127
117128 if ($ result ->errno != 0 ) {
118129 error_log ($ result ->errstr );
@@ -127,130 +138,3 @@ function ct_process_submission() {
127138
128139 return null ;
129140}
130-
131-
132- /**
133- * Send JSON request to servers
134- * @param $msg
135- * @return boolean|\CleantalkResponse
136- */
137- function ct_send_request ($ data = null , $ url , $ server_timeout = 3 ) {
138- // Convert to array
139- $ data = json_decode (json_encode ($ data ), true );
140-
141- // Convert to JSON
142- $ data = json_encode ($ data );
143-
144- $ result = false ;
145- $ curl_error = null ;
146- if (function_exists ('curl_init ' )) {
147- $ ch = curl_init ();
148- curl_setopt ($ ch , CURLOPT_URL , $ url );
149- curl_setopt ($ ch , CURLOPT_TIMEOUT , $ server_timeout );
150- curl_setopt ($ ch , CURLOPT_POST , 1 );
151- curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ data );
152- // receive server response ...
153- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
154- // resolve 'Expect: 100-continue' issue
155- curl_setopt ($ ch , CURLOPT_HTTPHEADER , array ('Expect: ' ));
156-
157- $ result = curl_exec ($ ch );
158- if (!$ result ) {
159- $ curl_error = curl_error ($ ch );
160- }
161-
162- curl_close ($ ch );
163- }
164-
165- if (!$ result ) {
166- $ allow_url_fopen = ini_get ('allow_url_fopen ' );
167- if (function_exists ('file_get_contents ' ) && isset ($ allow_url_fopen ) && $ allow_url_fopen == '1 ' ) {
168- $ opts = array ('http ' =>
169- array (
170- 'method ' => 'POST ' ,
171- 'header ' => "Content-Type: text/html \r\n" ,
172- 'content ' => $ data ,
173- 'timeout ' => $ server_timeout
174- )
175- );
176-
177- $ context = stream_context_create ($ opts );
178- $ result = @file_get_contents ($ url , false , $ context );
179- }
180- }
181-
182- if (!$ result ) {
183- $ response = null ;
184- $ response ['errno ' ] = 1 ;
185- if ($ curl_error ) {
186- $ response ['errstr ' ] = sprintf ("CURL error: '%s' " , $ curl_error );
187- } else {
188- $ response ['errstr ' ] = 'No CURL support compiled in ' ;
189- }
190- $ response ['errstr ' ] .= ' or disabled allow_url_fopen in php.ini. ' ;
191- $ response = json_decode (json_encode ($ response ));
192-
193- return $ response ;
194- }
195-
196- $ errstr = null ;
197- $ response = json_decode ($ result );
198- if ($ result !== false && is_object ($ response )) {
199- $ response ->errno = 0 ;
200- $ response ->errstr = $ errstr ;
201- } else {
202- $ errstr = 'Unknown response from ' . $ url . '. ' . ' ' . $ result ;
203-
204- $ response = null ;
205- $ response ['errno ' ] = 1 ;
206- $ response ['errstr ' ] = $ errstr ;
207- $ response = json_decode (json_encode ($ response ));
208- }
209-
210-
211- return $ response ;
212- }
213- /**
214- * Get user IP behind proxy server
215- */
216- function ct_session_ip ( $ data_ip ) {
217- if (!$ data_ip || !preg_match ("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ " , $ data_ip )) {
218- return $ data_ip ;
219- }
220- if (isset ($ _SERVER ['HTTP_X_FORWARDED_FOR ' ])) {
221-
222- $ forwarded_ip = explode (", " , $ _SERVER ['HTTP_X_FORWARDED_FOR ' ]);
223-
224- // Looking for first value in the list, it should be sender real IP address
225- if (!preg_match ("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ " , $ forwarded_ip [0 ])) {
226- return $ data_ip ;
227- }
228-
229- $ private_src_ip = false ;
230- $ private_nets = array (
231- '10.0.0.0/8 ' ,
232- '127.0.0.0/8 ' ,
233- '176.16.0.0/12 ' ,
234- '192.168.0.0/16 ' ,
235- );
236-
237- foreach ($ private_nets as $ v ) {
238-
239- // Private IP found
240- if ($ private_src_ip ) {
241- continue ;
242- }
243-
244- if ($ this ->net_match ($ v , $ data_ip )) {
245- $ private_src_ip = true ;
246- }
247- }
248- if ($ private_src_ip ) {
249- // Taking first IP from the list HTTP_X_FORWARDED_FOR
250- $ data_ip = $ forwarded_ip [0 ];
251- }
252- }
253-
254- return $ data_ip ;
255- }
256- ?>
0 commit comments