]> ruin.nu Git - ndwebbie.git/blob - lib/NDWeb/Controller/Settings.pm
Use localhost for mail
[ndwebbie.git] / lib / NDWeb / Controller / Settings.pm
1 package NDWeb::Controller::Settings;
2
3 use strict;
4 use warnings;
5 use feature ":5.10";
6 use parent 'Catalyst::Controller';
7
8 use NDWeb::Include;
9
10 use DateTime::TimeZone;
11 use Mail::Sendmail;
12 use Email::Valid;
13
14 =head1 NAME
15
16 NDWeb::Controller::Settings - Catalyst Controller
17
18 =head1 DESCRIPTION
19
20 Catalyst Controller.
21
22 =head1 METHODS
23
24 =cut
25
26
27 =head2 index 
28
29 =cut
30
31 sub index :Path :Args(0) {
32         my ( $self, $c ) = @_;
33         my $dbh = $c->model;
34
35         $c->stash(error => $c->flash->{error});
36
37         my @stylesheets = ('Default');
38         my $dir = $c->path_to('root/static/css/black.css')->dir;
39         while (my $file = $dir->next){
40                 if(!$file->is_dir && $file->basename =~ m{^(\w+)\.css$}){
41                         push @stylesheets,$1;
42                 }
43         }
44         $c->stash(stylesheets => \@stylesheets);
45
46         my ($birthday,$timezone,$email) = $dbh->selectrow_array(q{
47 SELECT birthday,timezone,email FROM users WHERE uid = $1
48                 },undef,$c->user->id);
49         $c->stash(birthday => $birthday);
50         $c->stash(email =>  $c->flash->{email} // $email);
51
52         my @timezone = split m{/},$timezone,2;
53         $c->stash(timezone => \@timezone);
54
55         my @cat = DateTime::TimeZone->categories;
56         unshift @cat, 'GMT';
57         $c->stash(tzcategories => \@cat);
58
59         my @countries = DateTime::TimeZone->names_in_category($timezone[0]);
60         $c->stash(tzcountries => \@countries);
61 }
62
63 sub changeStylesheet : Local {
64         my ( $self, $c ) = @_;
65         my $dbh = $c->model;
66
67         my $query = $dbh->prepare(q{UPDATE users SET css = NULLIF($2,'Default')
68                 WHERE uid = $1
69         });
70         $query->execute($c->user->id,html_escape $c->req->param('stylesheet'));
71
72         $c->res->redirect($c->uri_for(''));
73 }
74
75 sub changeBirthday : Local {
76         my ( $self, $c ) = @_;
77         my $dbh = $c->model;
78
79         my $query = $dbh->prepare(q{UPDATE users SET birthday = NULLIF($2,'')::date
80                 WHERE uid = $1
81                 });
82         eval{
83                 $query->execute($c->user->id,html_escape $c->req->param('birthday'));
84         };
85         if ($@){
86                 if ($@ =~ /invalid input syntax for type date/){
87                         $c->flash(error => 'Bad syntax for day, use YYYY-MM-DD.');
88                 }else{
89                         $c->flash(error => $@);
90                 }
91         }
92         $c->res->redirect($c->uri_for(''));
93 }
94
95 sub changeTimezone : Local {
96         my ( $self, $c ) = @_;
97         my $dbh = $c->model;
98
99         my $timezone = $c->req->param('timezone');
100         my $query = $dbh->prepare(q{UPDATE users SET timezone = $2 WHERE uid = $1});
101         eval{
102                 $dbh->selectrow_array(q{SELECT NOW() AT TIME ZONE $1},undef,$timezone);
103                 $query->execute($c->user->id,$timezone );
104         };
105         if ($@){
106                 $c->flash(error => $@);
107         }
108         $c->res->redirect($c->uri_for(''));
109 }
110
111 sub changePassword : Local {
112         my ( $self, $c ) = @_;
113         my $dbh = $c->model;
114
115         if (length $c->req->param('pass') < 4) {
116                 $c->flash(error => "Your password need to be at least 4 characters");
117         } else {
118                 my $query = $dbh->prepare(q{UPDATE users SET password = $1
119                         WHERE password = crypt($2,password) AND uid = $3
120                 });
121                 $query->execute($c->req->param('pass'),$c->req->param('oldpass'),$c->user->id);
122
123                 $c->flash(error => "Old password was invalid") unless $query->rows;
124         }
125
126         $c->res->redirect($c->uri_for(''));
127 }
128
129 sub changeEmail : Local {
130         my ( $self, $c ) = @_;
131         my $dbh = $c->model;
132
133         my $email = $c->req->param('email');
134
135         unless (Email::Valid->address($email)){
136                 $c->flash(email => $email);
137                 $c->flash(error => 'Invalid email address');
138                 $c->res->redirect($c->uri_for(''));
139                 return,
140         }
141
142         eval{
143                 my $insert = $dbh->prepare(q{
144 INSERT INTO email_change (uid,email) VALUES ($1,$2) RETURNING id;
145                         });
146                 $insert->execute($c->user->id,$email);
147
148                 my ($id) = $insert->fetchrow_array;
149
150                 my %mail = (
151                         smtp => 'localhost',
152                         To      => $email,
153                         From    => 'NewDawn Command <nd@ruin.nu>',
154                         'Content-type' => 'text/plain; charset="UTF-8"',
155                         Subject => 'Change email address',
156                         Message => qq{
157 You have requested to change email address on the NewDawn website.
158 If that is not the case, then feel free to ignore this email. Otherwise
159 use the following url to confirm the change:
160
161 }.$c->uri_for('confirmEmail',$id)."\n",
162                 );
163
164                 if (sendmail %mail) {
165                         $c->flash(error => 'Sent mail for confirmation.');
166                 }else {
167                         $c->flash(error => $Mail::Sendmail::error);
168                 }
169         };
170         if($@){
171                 if($@ =~ /duplicate key value violates unique constraint/){
172                         $c->flash(email => $email);
173                         $c->flash(error => 'Something went wrong, try to set the email again');
174                 }else{
175                         die $@;
176                 }
177         }
178         $c->res->redirect($c->uri_for(''));
179 }
180
181 sub confirmEmail : Local {
182         my ( $self, $c, $id ) = @_;
183         my $dbh = $c->model;
184
185         $dbh->begin_work;
186         my $query = $dbh->prepare(q{
187 UPDATE email_change SET confirmed = TRUE
188 WHERE uid = $1 AND id = $2 AND NOT confirmed
189 RETURNING email
190                 });
191         $query->execute($c->user->id,$id);
192         my ($email) = $query->fetchrow_array;
193
194         if ($email){
195                 $dbh->do(q{UPDATE users SET email = $2 WHERE uid = $1}
196                         ,undef,$c->user->id,$email);
197                 $c->flash(error => "Email updated.");
198         }else{
199                 $c->flash(error => "$id is not a valid change id for your account, or already confirmed");
200         }
201         $dbh->commit;
202         $c->res->redirect($c->uri_for(''));
203 }
204
205
206 =head1 AUTHOR
207
208 Michael Andreen (harv@ruin.nu)
209
210 =head1 LICENSE
211
212 GPL 2.0, or later.
213
214 =cut
215
216 1;