1 |
#!/usr/bin/perl |
2 |
use strict; |
3 |
|
4 |
use lib qw[/home/httpd/html/www/markup/html/whatpm |
5 |
/home/wakaba/work/manakai2/lib]; |
6 |
|
7 |
use CGI::Carp qw[fatalsToBrowser]; |
8 |
require Message::CGI::Carp; |
9 |
|
10 |
require 'users.pl'; |
11 |
|
12 |
require Message::CGI::HTTP; |
13 |
require Encode; |
14 |
my $cgi = Message::CGI::HTTP->new; |
15 |
$cgi->{decoder}->{'#default'} = sub { |
16 |
return Encode::decode ('utf-8', $_[1]); |
17 |
}; |
18 |
|
19 |
require Message::DOM::DOMImplementation; |
20 |
my $dom = Message::DOM::DOMImplementation->new; |
21 |
|
22 |
my $path = $cgi->path_info; |
23 |
$path = '' unless defined $path; |
24 |
|
25 |
my @path = split m#/#, percent_decode ($path), -1; |
26 |
shift @path; |
27 |
|
28 |
if (@path == 1 and $path[0] eq 'new-user') { |
29 |
if ($cgi->request_method eq 'POST') { |
30 |
lock_start (); |
31 |
binmode STDOUT, ':encoding(utf-8)'; |
32 |
|
33 |
my $user_id = $cgi->get_parameter ('user-id'); |
34 |
|
35 |
if ($user_id !~ /\A[0-9a-z-]{4,20}\z/) { |
36 |
print_error (400, qq[User id "$user_id" is invalid; use characters [0-9a-z-]{4,20}]); |
37 |
exit; |
38 |
} |
39 |
|
40 |
if (get_user_prop ($user_id)) { |
41 |
print_error (400, qq[User id "$user_id" is already used]); |
42 |
exit; |
43 |
} |
44 |
|
45 |
my $pass_crypted = check_password ($cgi); |
46 |
|
47 |
my $user_prop = {id => $user_id, pass_crypted => $pass_crypted}; |
48 |
set_user_prop ($user_id, $user_prop); |
49 |
|
50 |
commit (); |
51 |
|
52 |
my $user_url = get_absolute_url ('../edit/users/' . $user_id . '/'); |
53 |
|
54 |
print qq[Status: 201 User registered |
55 |
Location: $user_url |
56 |
Content-Type: text/html; charset=utf-8 |
57 |
|
58 |
<!DOCTYPE HTML> |
59 |
<html lang=en> |
60 |
<title>User "@{[htescape ($user_id)]}" registered</title> |
61 |
<link rel=stylesheet href="/www/style/html/xhtml"> |
62 |
<h1>User "@{[htescape ($user_id)]}" registered</h1> |
63 |
<p>Your user account is created successfully. |
64 |
<p>See <a href="@{[htescape ($user_url)]}">your user account information page</a>.]; |
65 |
exit; |
66 |
} else { |
67 |
binmode STDOUT, ":encoding(utf-8)"; |
68 |
print qq[Content-Type: text/html; charset=utf-8 |
69 |
|
70 |
<!DOCTYPE HTML> |
71 |
<html lang=en> |
72 |
<title>Create a new user account</title> |
73 |
<link rel=stylesheet href="/www/style/html/xhtml"> |
74 |
<h1>Create a new user account</h1> |
75 |
|
76 |
<form action=new-user accept-charset=utf-8 method=post> |
77 |
|
78 |
<p><strong>User id</strong>: <input type=text name=user-id |
79 |
maxlength=20 size=10 required pattern="[0-9a-z-]{4,20}" |
80 |
title="Use a string of characters 'a'..'z', '0'..'9', and '-' with length 4..10 (inclusive)"> |
81 |
|
82 |
<p><strong>Password</strong>: <input type=password name=user-pass |
83 |
size=10 required pattern=".{4,}" title="Type 4 characters at minimum"> |
84 |
|
85 |
<p><strong>Password</strong> (type again): <input type=password |
86 |
name=user-pass2 size=10 required pattern=".{4,}"> |
87 |
|
88 |
<p><input type=submit value=Create> |
89 |
|
90 |
</form>]; |
91 |
exit; |
92 |
} |
93 |
} elsif (@path == 0) { |
94 |
my $root_url = get_absolute_url ('add/new-user'); |
95 |
|
96 |
print qq[Status: 301 Moved permanently |
97 |
Location: $root_url |
98 |
Content-Type: text/html; charset=us-ascii |
99 |
|
100 |
See <a href="@{[htescape ($root_url)]}">other page</a>.]; |
101 |
exit; |
102 |
} |
103 |
|
104 |
print_error (404, 'Not found'); |
105 |
exit; |
106 |
|
107 |
sub percent_decode ($) { |
108 |
return $dom->create_uri_reference ($_[0]) |
109 |
->get_iri_reference |
110 |
->uri_reference; |
111 |
} # percent_decode |
112 |
|
113 |
sub get_absolute_url ($) { |
114 |
return $dom->create_uri_reference ($_[0]) |
115 |
->get_absolute_reference ($cgi->request_uri) |
116 |
->get_iri_reference |
117 |
->uri_reference; |
118 |
} # get_absolute_url |