25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-24 16:23:40 +01:00

exmpp fixes in captcha (thanks to Karim Gemayel)

SVN Revision: 2933
This commit is contained in:
Badlop 2010-01-27 18:53:47 +00:00
parent f5e1479afe
commit e4a6d20328

View File

@ -45,8 +45,23 @@
-include("web/ejabberd_http.hrl"). -include("web/ejabberd_http.hrl").
-define(VFIELD(Type, Var, Value), -define(VFIELD(Type, Var, Value),
{xmlelement, "field", [{"type", Type}, {"var", Var}], #xmlel{name = 'field',
[{xmlelement, "value", [], [Value]}]}). attrs = [
#xmlattr{
name = 'type',
value = Type
},
#xmlattr{
name = 'var',
value = Var
}
],
children = [
#xmlel{
name = 'value',
children = [Value]
}
]}.
-define(CAPTCHA_TEXT(Lang), translate:translate(Lang, "Enter the text you see")). -define(CAPTCHA_TEXT(Lang), translate:translate(Lang, "Enter the text you see")).
-define(CAPTCHA_LIFETIME, 120000). % two minutes -define(CAPTCHA_LIFETIME, 120000). % two minutes
@ -80,28 +95,126 @@ create_captcha(Id, SID, From, To, Lang, Args)
B64Image = jlib:encode_base64(binary_to_list(Image)), B64Image = jlib:encode_base64(binary_to_list(Image)),
JID = exmpp_jid:to_list(From), JID = exmpp_jid:to_list(From),
CID = "sha1+" ++ sha:sha(Image) ++ "@bob.xmpp.org", CID = "sha1+" ++ sha:sha(Image) ++ "@bob.xmpp.org",
Data = {xmlelement, "data", % Data = {xmlelement, "data",
[{"xmlns", ?NS_BOB}, {"cid", CID}, % [{"xmlns", ?NS_BOB}, {"cid", CID},
{"max-age", "0"}, {"type", Type}], % {"max-age", "0"}, {"type", Type}],
[{xmlcdata, B64Image}]}, % [{xmlcdata, B64Image}]},
Data =
#xmlel{
name = 'data',
ns = ?NS_BOB,
attrs = [
#xmlattr{
name = 'cid',
value = CID
},
#xmlattr{
name = 'max-age',
value = "0"
},
#xmlattr{
name = 'type',
value = Type
}
],
children = [#xmlcdata{cdata = B64Image}]},
% {xmlelement, "captcha", [{"xmlns", ?NS_CAPTCHA}],
% %% ?NS_DATA_FORMS is 'jabber:x:data'
% [{xmlelement, "x", [{"xmlns", "jabber:x:data"}, {"type", "form"}],
% [?VFIELD("hidden", "FORM_TYPE", {xmlcdata, ?NS_CAPTCHA}),
% ?VFIELD("hidden", "from", {xmlcdata, exmpp_jid:to_list(To)}),
% ?VFIELD("hidden", "challenge", {xmlcdata, Id}),
% ?VFIELD("hidden", "sid", {xmlcdata, SID}),
% {xmlelement, "field", [{"var", "ocr"}, {"label", ?CAPTCHA_TEXT(Lang)}],
% [{xmlelement, "media", [{"xmlns", ?NS_MEDIA}],
% [{xmlelement, "uri", [{"type", Type}],
% [{xmlcdata, "cid:" ++ CID}]}]}]}]}]},
%% TODO : kael : write exmpp_captcha.erl
Captcha = Captcha =
{xmlelement, "captcha", [{"xmlns", ?NS_CAPTCHA}], #xmlel{
%% ?NS_DATA_FORMS is 'jabber:x:data' name = 'captcha',
[{xmlelement, "x", [{"xmlns", "jabber:x:data"}, {"type", "form"}], ns = ?NS_CAPTCHA,
[?VFIELD("hidden", "FORM_TYPE", {xmlcdata, ?NS_CAPTCHA}), children = [
?VFIELD("hidden", "from", {xmlcdata, exmpp_jid:to_list(To)}), #xmlel{
?VFIELD("hidden", "challenge", {xmlcdata, Id}), name = 'x',
?VFIELD("hidden", "sid", {xmlcdata, SID}), ns = ?NS_DATA_FORMS_s,
{xmlelement, "field", [{"var", "ocr"}, {"label", ?CAPTCHA_TEXT(Lang)}], attrs = [
[{xmlelement, "media", [{"xmlns", ?NS_MEDIA}], #xmlattr{
[{xmlelement, "uri", [{"type", Type}], name = 'type',
[{xmlcdata, "cid:" ++ CID}]}]}]}]}]}, value = "form"
}
],
children = [
?VFIELD("hidden", "FORM_TYPE", #xmlcdata{cdata = ?NS_CAPTCHA}),
?VFIELD("hidden", "from", #xmlcdata{cdata = exmpp_jid:to_list(To)}),
?VFIELD("hidden", "challenge", #xmlcdata{cdata = Id}),
?VFIELD("hidden", "sid", #xmlcdata{cdata = SID}),
#xmlel{
name = 'field',
attrs = [
#xmlattr{
name = 'var',
value = "ocr"
},
#xmlattr{
name = 'label',
value = ?CAPTCHA_TEXT(Lang)
}
],
children = [
#xmlel{
name = 'media',
ns = ?NS_MEDIA,
children = [
#xmlel{
name = 'uri',
attrs = [
#xmlattr{
name = 'type',
value = Type
}
],
children = [
#xmlcdata{cdata = "cid:" ++ CID}
]
}
]
}
]
}
]
}
]
},
BodyString1 = translate:translate(Lang, "Your messages to ~s are being blocked. To unblock them, visit ~s"), BodyString1 = translate:translate(Lang, "Your messages to ~s are being blocked. To unblock them, visit ~s"),
BodyString = io_lib:format(BodyString1, [JID, get_url(Id)]), BodyString = io_lib:format(BodyString1, [JID, get_url(Id)]),
Body = {xmlelement, "body", [], Body =
[{xmlcdata, BodyString}]}, #xmlel{
OOB = {xmlelement, "x", [{"xmlns", ?NS_OOB}], name = 'body',
[{xmlelement, "url", [], [{xmlcdata, get_url(Id)}]}]}, children = [
#xmlcdata{
cdata = BodyString
}
]
},
OOB =
#xmlel{
name = 'x',
ns = ?NS_OOB,
children = [
#xmlel{
name = 'url',
children = [
#xmlcdata{
cdata = get_url(Id)}
]
}
]
},
%Body = {xmlelement, "body", [],
% [{xmlcdata, BodyString}]},
%OOB = {xmlelement, "x", [{"xmlns", ?NS_OOB}],
% [{xmlelement, "url", [], [{xmlcdata, get_url(Id)}]}]},
Tref = erlang:send_after(?CAPTCHA_LIFETIME, ?MODULE, {remove_id, Id}), Tref = erlang:send_after(?CAPTCHA_LIFETIME, ?MODULE, {remove_id, Id}),
case ?T(mnesia:write(#captcha{id=Id, pid=self(), key=Key, case ?T(mnesia:write(#captcha{id=Id, pid=self(), key=Key,
tref=Tref, args=Args})) of tref=Tref, args=Args})) of
@ -123,28 +236,125 @@ create_captcha(Id, SID, From, To, Lang, Args)
build_captcha_html(Id, Lang) -> build_captcha_html(Id, Lang) ->
case mnesia:dirty_read(captcha, Id) of case mnesia:dirty_read(captcha, Id) of
[#captcha{}] -> [#captcha{}] ->
ImgEl = {xmlelement, "img", [{"src", get_url(Id ++ "/image")}], []}, %ImgEl = {xmlelement, "img", [{"src", get_url(Id ++ "/image")}], []},
TextEl = {xmlcdata, ?CAPTCHA_TEXT(Lang)}, ImgEl =
IdEl = {xmlelement, "input", [{"type", "hidden"}, #xmlel{
{"name", "id"}, name = 'img',
{"value", Id}], []}, attrs = [
KeyEl = {xmlelement, "input", [{"type", "text"}, #xmlattr{
{"name", "key"}, name = 'src',
{"size", "10"}], []}, value = get_url(Id ++ "/image")
FormEl = {xmlelement, "form", [{"action", get_url(Id)}, }
{"name", "captcha"}, ]
{"method", "POST"}], },
[ImgEl, %TextEl = {xmlcdata, ?CAPTCHA_TEXT(Lang)},
{xmlelement, "br", [], []}, TextEl = #xmlcdata{cdata = ?CAPTCHA_TEXT(Lang)},
TextEl, %IdEl = {xmlelement, "input", [{"type", "hidden"},
{xmlelement, "br", [], []}, % {"name", "id"},
IdEl, % {"value", Id}], []},
KeyEl, IdEl =
{xmlelement, "br", [], []}, #xmlel{
{xmlelement, "input", [{"type", "submit"}, name = 'input',
{"name", "enter"}, attrs = [
{"value", "OK"}], []} #xmlattr{
]}, name = 'type',
value = "hidden"
},
#xmlattr{
name = 'name',
value = "id"
},
#xmlattr{
name = 'value',
value = Id
}
]
},
%KeyEl = {xmlelement, "input", [{"type", "text"},
% {"name", "key"},
% {"size", "10"}], []},
KeyEl =
#xmlel{
name = 'input',
attrs = [
#xmlattr{
name = 'type',
value = "text"
},
#xmlattr{
name = 'name',
value = "key"
},
#xmlattr{
name = 'size',
value = "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 =
#xmlel{
name = 'form',
attrs = [
#xmlattr{
name = 'action',
value = get_url(Id)
},
#xmlattr{
name = 'name',
value = "captcha"
},
#xmlattr{
name = 'method',
value = "POST"
}
],
children = [
ImgEl,
#xmlel{
name = 'br'
},
TextEl,
#xmlel{
name = 'br'
},
IdEl,
KeyEl,
#xmlel{
name = 'br'
},
#xmlel{
name = 'input',
attrs = [
#xmlattr{
name = 'type',
value = "submit"
},
#xmlattr{
name = 'name',
value = "enter"
},
#xmlattr{
name = 'value',
value = "OK"
}
]
}
]
},
{FormEl, {ImgEl, TextEl, IdEl, KeyEl}}; {FormEl, {ImgEl, TextEl, IdEl, KeyEl}};
_ -> _ ->
captcha_not_found captcha_not_found
@ -203,8 +413,18 @@ process(_Handlers, #request{method='GET', lang=Lang, path=[_, Id]}) ->
case build_captcha_html(Id, Lang) of case build_captcha_html(Id, Lang) of
{FormEl, _} when is_tuple(FormEl) -> {FormEl, _} when is_tuple(FormEl) ->
Form = Form =
{xmlelement, "div", [{"align", "center"}], %{xmlelement, "div", [{"align", "center"}],
[FormEl]}, %[FormEl]},
#xmlel{
name = 'div',
attrs = [
#xmlattr{
name = 'align',
value = "center"
}
],
children = [FormEl]
},
ejabberd_web:make_xhtml([Form]); ejabberd_web:make_xhtml([Form]);
captcha_not_found -> captcha_not_found ->
ejabberd_web:error(not_found) ejabberd_web:error(not_found)
@ -232,10 +452,17 @@ process(_Handlers, #request{method='POST', q=Q, lang=Lang, path=[_, Id]}) ->
case check_captcha(Id, ProvidedKey) of case check_captcha(Id, ProvidedKey) of
captcha_valid -> captcha_valid ->
Form = Form =
{xmlelement, "p", [], %{xmlelement, "p", [],
[{xmlcdata, % [{xmlcdata,
translate:translate(Lang, "The captcha is valid.") % translate:translate(Lang, "The captcha is valid.")
}]}, % }]},
#xmlel{
name = 'p',
children = [
#xmlcdata{
cdata = translate:translate(Lang, "The captcha is valid.")}
]
},
ejabberd_web:make_xhtml([Form]); ejabberd_web:make_xhtml([Form]);
captcha_non_valid -> captcha_non_valid ->
ejabberd_web:error(not_allowed); ejabberd_web:error(not_allowed);