Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 68 lines (55 sloc) 2.382 kb
e2de9b8 @Joony Added some ReCAPTCHA files
authored
1 % Copyright (c) 2009 Buddy Moore
2 %
3 % Permission is hereby granted, free of charge, to any person
4 % obtaining a copy of this software and associated documentation
5 % files (the "Software"), to deal in the Software without
6 % restriction, including without limitation the rights to use,
7 % copy, modify, merge, publish, distribute, sublicense, and/or sell
8 % copies of the Software, and to permit persons to whom the
9 % Software is furnished to do so, subject to the following
10 % conditions:
11 %
12 % The above copyright notice and this permission notice shall be
13 % included in all copies or substantial portions of the Software.
14 %
15 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17 % OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 % NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 % HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 % WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 % FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 % OTHER DEALINGS IN THE SOFTWARE.
23
24 -module(util_recaptcha).
25
26 -export([check_answer/3, get_error/1]).
27
28 get_verify_url() ->
29 "http://api-verify.recaptcha.net/verify".
30
31 get_challenge_url() ->
32 "http://api-secure.recaptcha.net/challenge".
33
34 private_key() ->
35 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA".
36
37 public_key() ->
38 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA".
39
40 check_answer(RemoteIp, Challenge, Response) ->
41 %inets:start(),
42 Data = "privatekey=" ++ private_key() ++ "&remoteip=" ++ RemoteIp ++ "&challenge=" ++ Challenge ++ "&response=" ++ Response,
43
44 BodyStr =
45 case make_http_request(get_verify_url(), Data) of
46 {ok, saved_to_file} ->
47 "saved";
48 {ok, Result} ->
49 case Result of
50 {_Status, _Headers, Body} -> Body;
51 {_Status, Body} -> Body
52 end;
53 {error, _Reason} -> "error"
54 end,
55 Lines = string:tokens(BodyStr, "\r\n"),
56 [Line1 | Rest] = Lines,
57 [Line2 | _Rest2] = Rest,
58 {Line1, Line2}.
59
60 make_http_request(URL, Data) ->
61 http:request(post, {URL, [{"Host", URL}, {"User-Agent", "reCAPTCHA/PHP"}, {"Content-Length", integer_to_list(string:len(Data))} ], "application/x-www-form-urlencoded", Data }, [], []).
62
63 get_error(ErrorCode) ->
64 Data = "?k=" ++ public_key() ++ "&error=" ++ ErrorCode,
65 http:request(get_challenge_url() ++ Data).
66
67
Something went wrong with that request. Please try again.