1 package NDWeb::Controller::Settings;
6 use parent 'Catalyst::Controller';
10 use DateTime::TimeZone;
16 NDWeb::Controller::Settings - Catalyst Controller
31 sub index :Path :Args(0) {
32 my ( $self, $c ) = @_;
35 $c->stash(error => $c->flash->{error});
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$}){
44 $c->stash(stylesheets => \@stylesheets);
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);
52 my @timezone = split m{/},$timezone,2;
53 $c->stash(timezone => \@timezone);
55 my @cat = DateTime::TimeZone->categories;
57 $c->stash(tzcategories => \@cat);
59 my @countries = DateTime::TimeZone->names_in_category($timezone[0]);
60 $c->stash(tzcountries => \@countries);
63 sub changeStylesheet : Local {
64 my ( $self, $c ) = @_;
67 my $query = $dbh->prepare(q{UPDATE users SET css = NULLIF($2,'Default')
70 $query->execute($c->user->id,html_escape $c->req->param('stylesheet'));
72 $c->res->redirect($c->uri_for(''));
75 sub changeBirthday : Local {
76 my ( $self, $c ) = @_;
79 my $query = $dbh->prepare(q{UPDATE users SET birthday = NULLIF($2,'')::date
83 $query->execute($c->user->id,html_escape $c->req->param('birthday'));
86 if ($@ =~ /invalid input syntax for type date/){
87 $c->flash(error => 'Bad syntax for day, use YYYY-MM-DD.');
89 $c->flash(error => $@);
92 $c->res->redirect($c->uri_for(''));
95 sub changeTimezone : Local {
96 my ( $self, $c ) = @_;
99 my $timezone = $c->req->param('timezone');
100 my $query = $dbh->prepare(q{UPDATE users SET timezone = $2 WHERE uid = $1});
102 $dbh->selectrow_array(q{SELECT NOW() AT TIME ZONE $1},undef,$timezone);
103 $query->execute($c->user->id,$timezone );
106 $c->flash(error => $@);
108 $c->res->redirect($c->uri_for(''));
111 sub changePassword : Local {
112 my ( $self, $c ) = @_;
115 if (length $c->req->param('pass') < 4) {
116 $c->flash(error => "Your password need to be at least 4 characters");
118 my $query = $dbh->prepare(q{UPDATE users SET password = $1
119 WHERE password = crypt($2,password) AND uid = $3
121 $query->execute($c->req->param('pass'),$c->req->param('oldpass'),$c->user->id);
123 $c->flash(error => "Old password was invalid") unless $query->rows;
126 $c->res->redirect($c->uri_for(''));
129 sub changeEmail : Local {
130 my ( $self, $c ) = @_;
133 my $email = $c->req->param('email');
135 if ($email =~ /^s?$/) {
136 my $update = $dbh->prepare(q{
137 UPDATE users SET email = NULL WHERE uid = $1;
139 $update->execute($c->user->id);
140 $c->flash(error => 'email cleared');
141 $c->res->redirect($c->uri_for(''));
145 unless (Email::Valid->address($email)){
146 $c->flash(email => $email);
147 $c->flash(error => 'Invalid email address');
148 $c->res->redirect($c->uri_for(''));
153 my $insert = $dbh->prepare(q{
154 INSERT INTO email_change (uid,email) VALUES ($1,$2) RETURNING id;
156 $insert->execute($c->user->id,$email);
158 my ($id) = $insert->fetchrow_array;
163 From => 'NewDawn Command <nd@ruin.nu>',
164 'Content-type' => 'text/plain; charset="UTF-8"',
165 Subject => 'Change email address',
167 You have requested to change email address on the NewDawn website.
168 If that is not the case, then feel free to ignore this email. Otherwise
169 use the following url to confirm the change:
171 }.$c->uri_for('confirmEmail',$id)."\n",
174 if (sendmail %mail) {
175 $c->flash(error => 'Sent mail for confirmation.');
177 $c->flash(error => $Mail::Sendmail::error);
181 if($@ =~ /duplicate key value violates unique constraint/){
182 $c->flash(email => $email);
183 $c->flash(error => 'Something went wrong, try to set the email again');
188 $c->res->redirect($c->uri_for(''));
191 sub confirmEmail : Local {
192 my ( $self, $c, $id ) = @_;
196 my $query = $dbh->prepare(q{
197 UPDATE email_change SET confirmed = TRUE
198 WHERE uid = $1 AND id = $2 AND NOT confirmed
201 $query->execute($c->user->id,$id);
202 my ($email) = $query->fetchrow_array;
205 $dbh->do(q{UPDATE users SET email = $2 WHERE uid = $1}
206 ,undef,$c->user->id,$email);
207 $c->flash(error => "Email updated.");
209 $c->flash(error => "$id is not a valid change id for your account, or already confirmed");
212 $c->res->redirect($c->uri_for(''));
218 Michael Andreen (harv@ruin.nu)