mirror of
https://github.com/processone/ejabberd.git
synced 2024-06-18 22:15:20 +02:00
* src/ejabberd_captcha.erl: Small code reorganization; export new
functions build_captcha_html/2 and check_captcha/2 SVN Revision: 2001
This commit is contained in:
parent
a93a3370ef
commit
46efb20947
|
@ -1,3 +1,8 @@
|
||||||
|
2009-03-30 Badlop <badlop@process-one.net>
|
||||||
|
|
||||||
|
* src/ejabberd_captcha.erl: Small code reorganization; export new
|
||||||
|
functions build_captcha_html/2 and check_captcha/2
|
||||||
|
|
||||||
2009-03-24 Badlop <badlop@process-one.net>
|
2009-03-24 Badlop <badlop@process-one.net>
|
||||||
|
|
||||||
* src/ejabberd_sm.erl: Partially retract SVN r1976
|
* src/ejabberd_sm.erl: Partially retract SVN r1976
|
||||||
|
|
|
@ -16,7 +16,8 @@
|
||||||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
||||||
terminate/2, code_change/3]).
|
terminate/2, code_change/3]).
|
||||||
|
|
||||||
-export([create_captcha/6, process_reply/1, process/2]).
|
-export([create_captcha/6, build_captcha_html/2, check_captcha/2,
|
||||||
|
process_reply/1, process/2]).
|
||||||
|
|
||||||
-include("jlib.hrl").
|
-include("jlib.hrl").
|
||||||
-include("ejabberd.hrl").
|
-include("ejabberd.hrl").
|
||||||
|
@ -91,6 +92,60 @@ create_captcha(Id, SID, From, To, Lang, Args)
|
||||||
error
|
error
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
%% @spec (Id::string(), Lang::string()) -> {FormEl, {ImgEl, TextEl, IdEl, KeyEl}} | captcha_not_found
|
||||||
|
%% where FormEl = xmlelement()
|
||||||
|
%% ImgEl = xmlelement()
|
||||||
|
%% TextEl = xmlelement()
|
||||||
|
%% IdEl = xmlelement()
|
||||||
|
%% KeyEl = xmlelement()
|
||||||
|
build_captcha_html(Id, Lang) ->
|
||||||
|
case mnesia:dirty_read(captcha, Id) of
|
||||||
|
[#captcha{}] ->
|
||||||
|
ImgEl = {xmlelement, "img", [{"src", get_url(Id ++ "/image")}], []},
|
||||||
|
TextEl = {xmlcdata, ?CAPTCHA_TEXT(Lang)},
|
||||||
|
IdEl = {xmlelement, "input", [{"type", "hidden"},
|
||||||
|
{"name", "id"},
|
||||||
|
{"value", Id}], []},
|
||||||
|
KeyEl = {xmlelement, "input", [{"type", "text"},
|
||||||
|
{"name", "key"},
|
||||||
|
{"size", "10"}], []},
|
||||||
|
FormEl = {xmlelement, "form", [{"action", get_url(Id)},
|
||||||
|
{"name", "captcha"},
|
||||||
|
{"method", "POST"}],
|
||||||
|
[ImgEl,
|
||||||
|
{xmlelement, "br", [], []},
|
||||||
|
TextEl,
|
||||||
|
{xmlelement, "br", [], []},
|
||||||
|
IdEl,
|
||||||
|
KeyEl,
|
||||||
|
{xmlelement, "br", [], []},
|
||||||
|
{xmlelement, "input", [{"type", "submit"},
|
||||||
|
{"name", "enter"},
|
||||||
|
{"value", "OK"}], []}
|
||||||
|
]},
|
||||||
|
{FormEl, {ImgEl, TextEl, IdEl, KeyEl}};
|
||||||
|
_ ->
|
||||||
|
captcha_not_found
|
||||||
|
end.
|
||||||
|
|
||||||
|
%% @spec (Id::string(), ProvidedKey::string()) -> captcha_valid | captcha_non_valid | captcha_not_found
|
||||||
|
check_captcha(Id, ProvidedKey) ->
|
||||||
|
?T(case mnesia:read(captcha, Id, write) of
|
||||||
|
[#captcha{pid=Pid, args=Args, key=StoredKey, tref=Tref}] ->
|
||||||
|
mnesia:delete({captcha, Id}),
|
||||||
|
erlang:cancel_timer(Tref),
|
||||||
|
if StoredKey == ProvidedKey ->
|
||||||
|
Pid ! {captcha_succeed, Args},
|
||||||
|
captcha_valid;
|
||||||
|
true ->
|
||||||
|
Pid ! {captcha_failed, Args},
|
||||||
|
captcha_non_valid
|
||||||
|
end;
|
||||||
|
_ ->
|
||||||
|
captcha_not_found
|
||||||
|
end).
|
||||||
|
|
||||||
|
|
||||||
process_reply({xmlelement, "captcha", _, _} = El) ->
|
process_reply({xmlelement, "captcha", _, _} = El) ->
|
||||||
case xml:get_subtag(El, "x") of
|
case xml:get_subtag(El, "x") of
|
||||||
false ->
|
false ->
|
||||||
|
@ -117,27 +172,15 @@ process_reply({xmlelement, "captcha", _, _} = El) ->
|
||||||
process_reply(_) ->
|
process_reply(_) ->
|
||||||
{error, malformed}.
|
{error, malformed}.
|
||||||
|
|
||||||
|
|
||||||
process(_Handlers, #request{method='GET', lang=Lang, path=[_, Id]}) ->
|
process(_Handlers, #request{method='GET', lang=Lang, path=[_, Id]}) ->
|
||||||
case mnesia:dirty_read(captcha, Id) of
|
case build_captcha_html(Id, Lang) of
|
||||||
[#captcha{}] ->
|
{FormEl, _} when is_tuple(FormEl) ->
|
||||||
Form =
|
Form =
|
||||||
{xmlelement, "div", [{"align", "center"}],
|
{xmlelement, "div", [{"align", "center"}],
|
||||||
[{xmlelement, "form", [{"action", get_url(Id)},
|
[FormEl]},
|
||||||
{"name", "captcha"},
|
|
||||||
{"method", "POST"}],
|
|
||||||
[{xmlelement, "img", [{"src", get_url(Id ++ "/image")}], []},
|
|
||||||
{xmlelement, "br", [], []},
|
|
||||||
{xmlcdata, ?CAPTCHA_TEXT(Lang)},
|
|
||||||
{xmlelement, "br", [], []},
|
|
||||||
{xmlelement, "input", [{"type", "text"},
|
|
||||||
{"name", "key"},
|
|
||||||
{"size", "10"}], []},
|
|
||||||
{xmlelement, "br", [], []},
|
|
||||||
{xmlelement, "input", [{"type", "submit"},
|
|
||||||
{"name", "enter"},
|
|
||||||
{"value", "OK"}], []}]}]},
|
|
||||||
ejabberd_web:make_xhtml([Form]);
|
ejabberd_web:make_xhtml([Form]);
|
||||||
_ ->
|
captcha_not_found ->
|
||||||
ejabberd_web:error(not_found)
|
ejabberd_web:error(not_found)
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
@ -158,22 +201,22 @@ process(_Handlers, #request{method='GET', path=[_, Id, "image"]}) ->
|
||||||
ejabberd_web:error(not_found)
|
ejabberd_web:error(not_found)
|
||||||
end;
|
end;
|
||||||
|
|
||||||
process(_Handlers, #request{method='POST', q=Q, path=[_, Id]}) ->
|
process(_Handlers, #request{method='POST', q=Q, lang=Lang, path=[_, Id]}) ->
|
||||||
?T(case mnesia:read(captcha, Id, write) of
|
ProvidedKey = proplists:get_value("key", Q, none),
|
||||||
[#captcha{pid=Pid, args=Args, key=Key, tref=Tref}] ->
|
case check_captcha(Id, ProvidedKey) of
|
||||||
mnesia:delete({captcha, Id}),
|
captcha_valid ->
|
||||||
erlang:cancel_timer(Tref),
|
Form =
|
||||||
Input = proplists:get_value("key", Q, none),
|
{xmlelement, "p", [],
|
||||||
if Input == Key ->
|
[{xmlcdata,
|
||||||
Pid ! {captcha_succeed, Args},
|
translate:translate(Lang, "The captcha is valid.")
|
||||||
ejabberd_web:make_xhtml([]);
|
}]},
|
||||||
true ->
|
ejabberd_web:make_xhtml([Form]);
|
||||||
Pid ! {captcha_failed, Args},
|
captcha_non_valid ->
|
||||||
ejabberd_web:error(not_allowed)
|
ejabberd_web:error(not_allowed);
|
||||||
end;
|
captcha_not_found ->
|
||||||
_ ->
|
|
||||||
ejabberd_web:error(not_found)
|
ejabberd_web:error(not_found)
|
||||||
end).
|
end.
|
||||||
|
|
||||||
|
|
||||||
%%====================================================================
|
%%====================================================================
|
||||||
%% gen_server callbacks
|
%% gen_server callbacks
|
||||||
|
|
Loading…
Reference in New Issue
Block a user