Fossil SCM
Add a really simple captcha - the user just has to press a button.
Commit
b3337295f1d8ee4a9a608f41c4ba9f5b502ebec83f4640a29e33363e65eb1d08
Parent
3f4885a9835d5b8…
1 file changed
+30
-7
+30
-7
| --- src/robot.c | ||
| +++ src/robot.c | ||
| @@ -34,15 +34,39 @@ | ||
| 34 | 34 | */ |
| 35 | 35 | |
| 36 | 36 | /* |
| 37 | 37 | ** Rewrite the current page with a robot squelch captcha. |
| 38 | 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); | |
| 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; | |
| 44 | 68 | } |
| 45 | 69 | |
| 46 | 70 | |
| 47 | 71 | /* |
| 48 | 72 | ** WEBPAGE functions can invoke this routine with an argument |
| @@ -77,11 +101,10 @@ | ||
| 77 | 101 | ){ |
| 78 | 102 | return 0; /* There is a valid token= query parameter */ |
| 79 | 103 | } |
| 80 | 104 | iSquelch = db_get_int("robot-squelch",200); |
| 81 | 105 | if( iSquelch<=0 ) return 0; |
| 82 | - if( n+iSquelch>=1000 ){ | |
| 83 | - robot_send_captcha(); | |
| 106 | + if( n+iSquelch>=1000 && robot_send_captcha() ){ | |
| 84 | 107 | return 1; |
| 85 | 108 | } |
| 86 | 109 | return 0; |
| 87 | 110 | } |
| 88 | 111 |
| --- 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 |