This repository has been archived by the owner on Jan 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
myapp_T.pl
executable file
·737 lines (645 loc) · 18.3 KB
/
myapp_T.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
#!/usr/bin/env perl
use v5.16;
use utf8;
use Mojolicious::Lite;
use DBI;
my $DBH = DBI->connect(
'dbi:mysql:Book',
#DB오류시 확인
'root',
'*Hay990729', #password(mysql)
#'user',
#'0000',
{
RaiseError => 1,
AutoCommit => 1,
mysql_enable_utf8 => 1,
},
);
helper db_select => sub {
my ( $self, $input_id ) = @_;
my $sth = $DBH->prepare(qq{ SELECT * FROM MEMO WHERE id=$input_id});
$sth->execute();
my %articles;
my ( $db_id, $name, $title, $content, $date ) = $sth->fetchrow_array;
my ($wdate) = split / /, $date;
$articles{$db_id} = {
name => $name,
title => $title,
content => $content,
wdate => $wdate,
};
return \%articles;
};
get '/' => sub {
my $self = shift;
$self->redirect_to( $self->url_for('/login') );
};
get '/:userid/list' => sub {
my $self = shift;
my $userid=$self->param('userid'); #사용자 id 문자열 가져오기
my $sth = $DBH->prepare(qq{ SELECT id, name, title, content, wdate FROM MEMO });
$sth->execute();
my %articles;
while ( my @row = $sth->fetchrow_array ) {
my ( $id, $name, $title, $content, $date ) = @row;
my ($wdate) = split / /, $date;
$articles{$id} = {
name => $name,
title => $title,
content => $content,
wdate => $wdate,
};
}
$self->session(USERID=>$userid);
$self->stash( articles => \%articles );
$self->render('list');
};
#회원가입
get '/createID' => sub { #페이지를 열기
my $self = shift;
$self->render('createID');
};
#회원가입
post '/createID' => sub { #DB로 저장하는 POST
my $self = shift;
my $userid = $self->param('userid');
my $passwd = $self->param('passwd');
#id 중복검사
my $sth1=$DBH->prepare(qq{SELECT userid FROM USER});
$sth1->execute();
my @id;
my $result = 0;
while(my @row = $sth1->fetchrow_array){
@id = @row;
if($userid eq $id[0]){
$result = 1;
}
}
#이미 테이블에 존재하는 아이디라면 경고창으로 이동
if ($result ==1){
$self->redirect_to($self->url_for('/alert2'));
}
else{
my $sth = $DBH->prepare(qq{
INSERT INTO `USER` (`userid`,`passwd`) VALUES (?,?)});
$sth->execute($userid, $passwd);
$self->redirect_to( $self->url_for('/login') );
}
};
get '/login' => sub {
my $self = shift;
$self->render('login');
};
#로그인창에 입력한 정보가 USER의 데이터가 맞는지 확인
get '/protected'=>sub{
my $self=shift;
$self->render('protected');
};
#로그인창에 입력한 정보가 USER의 데이터가 맞는지 확인
post '/protected'=> sub{
my $self=shift;
my $ID=$self->param('loginId');
my $PASSWD=$self->param('password');
#####DB
my $sth=$DBH->prepare(qq{SELECT userid,passwd FROM USER});
$sth->execute();
my $select=0;
my %articles;
while(my @row=$sth->fetchrow_array){
my($userid, $passwd)=@row;
if(($ID eq $userid)&&($PASSWD eq $passwd)){
$select=1;
}
}
if($select==1){
$self->redirect_to($self->url_for($ID.'/list'));
}
else{
$self->redirect_to($self->url_for('/alert'));
}
};
#로그인 실패시 알림
get '/alert' => sub { #alert
my $self = shift;
$self->render('alert');
};
#회원가입 실패시 알림
get '/alert2' => sub { #alert
my $self = shift;
$self->render('alert2');
};
get '/:userid/write' => sub {
my $self = shift;
$self->render('write');
};
post '/:userid/write' => sub {
my $self = shift;
my $name = $self->param('userid');
my $title = $self->param('title');
my $content = $self->param('content');
my $sth = $DBH->prepare(qq{
INSERT INTO `MEMO` (`name`,`title`,`content`) VALUES (?,?,?)
});
$sth->execute( $name, $title, $content );
$self->redirect_to( $self->url_for('list') );
};
get '/:userid/read/:id' => sub {
my $self = shift;
my $input_id = $self->param('id');
my $articles = $self->db_select ( $input_id );
my ($id) = keys %$articles;
$self->stash(
articles => $articles,
id => $id,
);
$self->render('read');
};
get '/:userid/edit/:id' => sub {
my $self = shift;
my $input_id = $self->param('id');
my $articles = $self->db_select ( $input_id );
my ($id) = keys %$articles;
$self->stash(
articles => $articles,
id => $id,
);
$self->render('edit');
};
post '/:userid/edit' => sub {
my $self = shift;
my $userid = $self->param('userid');
my $id = $self->param('id');
warn $id;
my $name = $self->param('name');
my $title = $self->param('title');
my $content = $self->param('content');
my $sth = $DBH->prepare(qq{
UPDATE `MEMO` SET `name`=?,`title`=?,`content`=? WHERE `id`=$id
});
$sth->execute( $name, $title, $content );
$self->redirect_to( $self->url_for('/'.$userid.'/list') );
};
get '/:userid/delete/:id' => sub {
my $self = shift;
my $userid = $self->param('userid');
my $id = $self->param('id');
my $sth = $DBH->prepare(qq{ DELETE FROM `MEMO` WHERE `id`=$id });
$sth->execute();
$self->redirect_to( $self->url_for('/'.$userid.'/list') );
};
app->start;
__DATA__
@@ layouts/default.html.ep
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
</head>
<body>
<%= content %>
</body>
</html>
#로그인 HTML
@@ login.html.ep
% layout 'default';
% title 'SIGN IN';
<style>
a {
color: #333;
text-decoration: none;
float: right;
}
input {
-webkit-writing-mode: horizontal-tb !important;
text-rendering: auto;
color: initial;
letter-spacing: normal;
word-spacing: normal;
text-transform: none;
text-indent: 0px;
text-shadow: none;
display: inline-block;
text-align: start;
-webkit-appearance: textfield;
background-color: white;
-webkit-rtl-ordering: logical;
cursor: text;
margin: 0em;
font: 400 13.3333px Arial;
padding: 1px 0px;
border-width: 2px;
border-style: inset;
border-color: initial;
border-image: initial;
}
.inner_login {
position: absolute;
left: 50%;
top: 50%;
margin: -145px 0 0 -160px;
}
.login_pananyang{
position: relative;
width: 320px;
margin: 0 auto;
}
.screen_out {
position: absolute;
width: 0;
height: 0;
overflow: hidden;
line-height: 0;
text-indent: -9999px;
}
body, button, input, select, td, textarea, th {
font-size: 13px;
line-height: 1.5;
-webkit-font-smoothing: antialiased;
}
fieldset, img {
border: 0;
}
.login_pananyang .box_login {
margin: 35px 0 0;
border: 1px solid #ddd;
border-radius: 3px;
background-color: #fff;
box-sizing: border-box;
}
.login_pananyang .inp_text {
position: relative;
width: 100%;
margin: 0;
padding: 18px 19px 19px;
box-sizing: border-box;
}
.login_pananyang .inp_text+.inp_text {
border-top: 1px solid #ddd;
}
.inp_text input {
display: block;
width: 100%;
height: 100%;
font-size: 13px;
color: #000;
border: none;
outline: 0;
-webkit-appearance: none;
background-color: transparent;
}
.btn_login {
margin: 20px 0 0;
width: 100%;
height: 48px;
border-radius: 3px;
border-color: #777;
font-size: 16px;
color: #fff;
background-color: #777;
}
</style>
<div class="inner_login">
<div class="login_pananyang">
<form action="/protected" method="post">
<fieldset>
<legend class="screen_out">로그인 정보 입력폼</legend>
<div class="box_login">
<div class="inp_text">
<label for="loginId" class="screen_out">아이디</label>
<input type="text" id="loginId" name="loginId" placeholder="ID" >
</div>
<div class="inp_text">
<label for="loginPw" class="screen_out">비밀번호</label>
<input type="password" id="loginPw" name="password" placeholder="Password" >
</div>
</div>
<button type="submit" class="btn_login" anabled>로그인</button>
<span class="txt_find">
<a href="http://127.0.0.1:3000/createID" class="link_find">아직 회원이 아니시라면 / 회원가입 </a>
</span>
</div>
</fieldset>
</form>
</div>
</div>
@@protected.html.ep
%layout 'default';
%title 'PROTECTED';
#아이디/비밀번호 오류 경고
@@alert.html.ep
%layout 'default';
%title 'SIGN IN ERROR';
<head>
<script>
alert("아이디/비밀번호를 다시 확인해주세요!");
location.href = "http://127.0.0.1:3000/login";
</script>
</head>
#회원가입 아이디 중복 경고
@@alert2.html.ep
%layout 'default';
%title 'SIGN UP ERROR';
<head>
<script>
alert("해당 아이디는 이미 사용중 입니다.");
location.href = "http://127.0.0.1:3000/createID";
</script>
</head>
#회원가입 HTML
@@ createID.html.ep
% layout 'default';
% title 'SIGN UP';
<style>
a {
color: #333;
text-decoration: none;
float: right;
}
input {
-webkit-writing-mode: horizontal-tb !important;
text-rendering: auto;
color: initial;
letter-spacing: normal;
word-spacing: normal;
text-transform: none;
text-indent: 0px;
text-shadow: none;
display: inline-block;
text-align: start;
-webkit-appearance: textfield;
background-color: white;
-webkit-rtl-ordering: logical;
cursor: text;
margin: 0em;
font: 400 13.3333px Arial;
padding: 1px 0px;
border-width: 2px;
border-style: inset;
border-color: initial;
border-image: initial;
}
.inner_createID {
position: absolute;
left: 50%;
top: 50%;
margin: -145px 0 0 -160px;
}
.createID_pananyang{
position: relative;
width: 320px;
margin: 0 auto;
}
.screen_out {
position: absolute;
width: 0;
height: 0;
overflow: hidden;
line-height: 0;
text-indent: -9999px;
}
body, button, input, select, td, textarea, th {
font-size: 13px;
line-height: 1.5;
-webkit-font-smoothing: antialiased;
}
fieldset, img {
border: 0;
}
.createID_pananyang .box_createID {
margin: 35px 0 0;
border: 1px solid #ddd;
border-radius: 3px;
background-color: #fff;
box-sizing: border-box;
}
.createID_pananyang .inp_text {
position: relative;
width: 100%;
margin: 0;
padding: 18px 19px 19px;
box-sizing: border-box;
}
.createID_pananyang .inp_text+.inp_text {
border-top: 1px solid #ddd;
}
.inp_text input {
display: block;
width: 100%;
height: 100%;
font-size: 13px;
color: #000;
border: none;
outline: 0;
-webkit-appearance: none;
background-color: transparent;
}
.btn_createID {
margin: 20px 0 0;
width: 100%;
height: 48px;
border-radius: 3px;
border-color: #777;
font-size: 16px;
color: #fff;
background-color: #777;
}
</style>
<div class="inner_createID">
<div class="createID_pananyang">
<form action="/createID" method="post">
<fieldset>
<legend class="screen_out">회원가입 정보 입력폼</legend>
<div class="box_createID">
<div class="inp_text">
<label for="loginId" class="screen_out">아이디</label>
<input type="text" id="userid" name="userid" placeholder="20자 이내의 문자열을 입력하세요" >
</div>
<div class="inp_text">
<label for="loginPw" class="screen_out">비밀번호</label>
<input type="password" id="passwd" name="passwd" placeholder="10자 이내의 숫자를 입력하세요" >
</div>
</div>
<button type="submit" class="btn_createID" anabled>가입하기</button>
</fieldset>
</form>
</div>
</div>
@@ write.html.ep
% layout 'default';
% title 'WRITE';
<style>
p{
text-align:right;
}
</style>
<form action="/<%= session 'USERID' %>/write" method="post">
<table width=580 border=0 cellpadding=2 cellspacing=1 bgcolor=#777>
<table style="margin-left:auto; margin-right:auto;">
<tr>
<td height=20 colspan=4 align=center bgcolor=#777>
<font color=white><b>글쓰기</b></font>
</td>
</tr>
<tr>
<td bgcolor=white>
<table bgcolor=white>
<tr>
<td>이름</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>제목</td>
<td><input type="text" name="title"></td>
</tr>
<tr>
<td>내용</td>
<td colspan=4>
<textarea name="content" cols=80 rows=5></textarea>
</td>
</tr>
<tr>
<td colspan=10 align=center>
<input type="submit" value="저장">
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td bgcolor=#999999>
<table width=100%>
<tr>
<td>
<p>
<a href='/<%= session 'USERID' %>/list' style="text-decoration:none;"><font color=white>목록보기</font></a>
</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
@@ list.html.ep
% layout 'default';
% title 'LIST';
<table width=580 border=0 cellpadding=2 cellspacing=1 bgcolor=#777>
<tr height=20 colspan=4 align=center bgcolor=#CCCCCC >
<table style="margin-left:auto; margin-right:auto;">
<td color=white>No. </td>
<td>제목</td>
<td>글쓴이</td>
<td>date</td>
</tr>
% for my $id ( reverse sort { $a <=> $b } keys %$articles ) {
<tr bgcolor="white">
<td><%= $id %></td>
<td><a href="/<%=session 'USERID' %>/read/<%= $id %>"><%= $articles->{$id}{title} %></a></td>
<td><%= $articles->{$id}{name} %></td>
<td><%= $articles->{$id}{wdate} %></td>
</tr>
% }
<tr>
<td colspan=4 bgcolor=#999999>
<table width=100%>
<tr>
<td width=2000 align=center height=20>
<a href="/<%= session 'USERID' %>/write" style="text-decoration:none;"><font color=white>글쓰기</font></a>
</td>
</tr>
</table>
</td>
</tr>
</table>
@@ read.html.ep
% layout 'default';
% title 'READ';
<table width=580 border=0 cellpadding=2 cellspacing=1 bgcolor=#777777>
<table style="margin-left:auto; margin-right:auto;">
<tr>
<td height=20 colspan=4 align=center bgcolor=#999999>
<font color=white><b><%= $articles->{$id}{title} %></b></font>
</td>
</tr>
<tr>
<td width=50 height=20 align=center bgcolor=#EEEEEE> 글쓴이 </td>
<td width=240 bgcolor=white> <%= $articles->{$id}{name} %> </td>
<td width=50 height=20 align=center bgcolor=#EEEEEE> 날짜 </td>
<td width=240 bgcolor=white> <%= $articles->{$id}{wdate} %> </td>
</tr>
<tr>
<td bgcolor=white colspan=4>
<font color=black>
<pre><%= $articles->{$id}{content} %></pre>
</font>
</td>
</tr>
<tr>
<td colspan=4 bgcolor=#999999>
<table width=100%>
<tr>
<td width=2000 align=left height=20>
<a href='/<%=session 'USERID' %>/list' style="text-decoration:none;"><font color=white>목록보기</font></a>
<a href='/<%=session 'USERID' %>/write' style="text-decoration:none;"><font color=white>글쓰기</font></a>
<a href='/<%=session 'USERID' %>/edit/<%= $id %>' style="text-decoration:none;"><font color=white>수정</font></a>
<a href='/<%=session 'USERID' %>/delete/<%= $id %>' style="text-decoration:none;"><font color=white>삭제</font></a>
</td>
</tr>
</table>
</td>
</tr>
</table>
@@ edit.html.ep
% layout 'default';
% title 'EDIT';
<form action="/<%=session 'USERID' %>/edit" method="post">
<input type="hidden" name="id" value="<%= $id %>">
<table width=580 border=0 cellpadding=2 cellspacing=1 bgcolor=#777777>
<table style="margin-left:auto; margin-right:auto;">
<tr>
<td height=20 colspan=4 align=center bgcolor=#999999>
<font color=white><b>수정</b></font>
</td>
</tr>
<tr>
<td bgcolor=white>
<table bgcolor=white>
<tr>
<td>이름</td>
<td><input type="text" name="name" value="<%= $articles->{$id}{name} %>"></td>
</tr>
<tr>
<td>제목</td>
<td><input type="text" name="title" value="<%= $articles->{$id}{title} %>"></td>
</tr>
<tr>
<td>내용</td>
<td colspan=4>
<textarea name="content" cols=80 rows=5><%= $articles->{$id}{content} %></textarea>
</td>
</tr>
<tr>
<td colspan=10 align=center>
<input type="submit" value="수정확인">
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td bgcolor=#999999>
<table width=100%>
<tr>
<td>
<a href='/<%=session 'USERID' %>/list' style="text-decoration:none;"><font color=white>목록보기</font></a>
<a href='/<%=session 'USERID' %>/write' style="text-decoration:none;"><font color=white>글쓰기</font></a>
<a href='/<%=session 'USERID' %>/read/<%= $id %>' style="text-decoration:none;"><font color=white>취소</font></a>
<a href='/<%=session 'USERID' %>/delete/<%= $id %>' style="text-decoration:none;"><font color=white>삭제</font></a>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>