Fossil SCM

Add a really simple captcha - the user just has to press a button.

drh 2025-08-15 15:06 robot-squelch
Commit b3337295f1d8ee4a9a608f41c4ba9f5b502ebec83f4640a29e33363e65eb1d08
1 file changed +30 -7
+30 -7
--- src/robot.c
+++ src/robot.c
@@ -34,15 +34,39 @@
3434
*/
3535
3636
/*
3737
** Rewrite the current page with a robot squelch captcha.
3838
*/
39
-static void robot_send_captcha(void){
40
- /* Actually, for now, redirect to /login?anon&g=...
41
- ** We can work on a more efficient implementation later.
42
- */
43
- login_needed(1);
39
+static int robot_send_captcha(void){
40
+ unsigned h = 0;
41
+ const char *z;
42
+
43
+ z = P("REMOTE_ADDR");
44
+ if( z ){
45
+ while( *z ){ h = (h + *(unsigned char*)(z++))*0x9e3779b1; }
46
+ }
47
+ z = P("HTTP_USER_AGENT");
48
+ if( z ){
49
+ while( *z ){ h = (h + *(unsigned char*)(z++))*0x9e3779b1; }
50
+ }
51
+ h %= 1000000000;
52
+ z = P("proof");
53
+ if( z && atoi(z)==h ) return 0;
54
+ cgi_tag_query_parameter("proof");
55
+
56
+ cgi_reset_content();
57
+ cgi_set_content_type("text/html");
58
+ style_header("Captcha");
59
+ @ <h1>Prove That You Are Human</h1>
60
+ @ <form method="GET">
61
+ @ <p>Press the button below</p><p>
62
+ cgi_query_parameters_to_hidden();
63
+ @ <input type="hidden" name="proof" value="%u(h)">
64
+ @ <input type="submit" value="I Am Human">
65
+ @ </form>
66
+ style_finish_page();
67
+ return 1;
4468
}
4569
4670
4771
/*
4872
** WEBPAGE functions can invoke this routine with an argument
@@ -77,11 +101,10 @@
77101
){
78102
return 0; /* There is a valid token= query parameter */
79103
}
80104
iSquelch = db_get_int("robot-squelch",200);
81105
if( iSquelch<=0 ) return 0;
82
- if( n+iSquelch>=1000 ){
83
- robot_send_captcha();
106
+ if( n+iSquelch>=1000 && robot_send_captcha() ){
84107
return 1;
85108
}
86109
return 0;
87110
}
88111
--- src/robot.c
+++ src/robot.c
@@ -34,15 +34,39 @@
34 */
35
36 /*
37 ** Rewrite the current page with a robot squelch captcha.
38 */
39 static void robot_send_captcha(void){
40 /* Actually, for now, redirect to /login?anon&g=...
41 ** We can work on a more efficient implementation later.
42 */
43 login_needed(1);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44 }
45
46
47 /*
48 ** WEBPAGE functions can invoke this routine with an argument
@@ -77,11 +101,10 @@
77 ){
78 return 0; /* There is a valid token= query parameter */
79 }
80 iSquelch = db_get_int("robot-squelch",200);
81 if( iSquelch<=0 ) return 0;
82 if( n+iSquelch>=1000 ){
83 robot_send_captcha();
84 return 1;
85 }
86 return 0;
87 }
88
--- src/robot.c
+++ src/robot.c
@@ -34,15 +34,39 @@
34 */
35
36 /*
37 ** Rewrite the current page with a robot squelch captcha.
38 */
39 static int robot_send_captcha(void){
40 unsigned h = 0;
41 const char *z;
42
43 z = P("REMOTE_ADDR");
44 if( z ){
45 while( *z ){ h = (h + *(unsigned char*)(z++))*0x9e3779b1; }
46 }
47 z = P("HTTP_USER_AGENT");
48 if( z ){
49 while( *z ){ h = (h + *(unsigned char*)(z++))*0x9e3779b1; }
50 }
51 h %= 1000000000;
52 z = P("proof");
53 if( z && atoi(z)==h ) return 0;
54 cgi_tag_query_parameter("proof");
55
56 cgi_reset_content();
57 cgi_set_content_type("text/html");
58 style_header("Captcha");
59 @ <h1>Prove That You Are Human</h1>
60 @ <form method="GET">
61 @ <p>Press the button below</p><p>
62 cgi_query_parameters_to_hidden();
63 @ <input type="hidden" name="proof" value="%u(h)">
64 @ <input type="submit" value="I Am Human">
65 @ </form>
66 style_finish_page();
67 return 1;
68 }
69
70
71 /*
72 ** WEBPAGE functions can invoke this routine with an argument
@@ -77,11 +101,10 @@
101 ){
102 return 0; /* There is a valid token= query parameter */
103 }
104 iSquelch = db_get_int("robot-squelch",200);
105 if( iSquelch<=0 ) return 0;
106 if( n+iSquelch>=1000 && robot_send_captcha() ){
 
107 return 1;
108 }
109 return 0;
110 }
111

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button