~vcs-imports/personalbackup/trunk

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
#!/usr/bin/perl -w

# $Id: preferences.cgi 165 2006-11-12 00:24:58Z linuxtuxie $

#############################################################################
# PersonalBackup -  A company-wide solution for backing up all your         #
#                   Windows machines and Samba shares                       #
#                                                                           #
# Copyright (C) 2004 Kim Kuylen / Melexis                                   #
#                                                                           #
# This program is free software; you can redistribute it and/or             #
# modify it under the terms of the GNU General Public License               #
# as published by the Free Software Foundation.                             #
#############################################################################

use lib "__libinstalldir__";
use strict;
use DBI;
use PBUtils;
use PBConfig;
use PBDatabase;
use Authenticate;

use CGI;
use CGI::Session;
use URI::Escape;
use HTML::Template;

##########################################
# configuration
##########################################

my $conf          = new PBConfig("__confdir__/personalbackup.conf");
my $sitename      = $conf->get("sitename");
my $administrator = $conf->get("administrator");
my $suffix        = $conf->get("suffix");
my $tempdir       = $conf->get("tempdir");
my $webserver     = $conf->get("webserver");
my $templatedir   = $conf->get("templatedir");
my $langref       = $conf->get("languages");
my $defaultlang   = $conf->get("defaultlang");
my $allowupdate   = $conf->get("allowupdate");
my $DEBUG         = $conf->get("DEBUG");
my $dbh           = new PBDatabase();
my $auth          = new Authenticate();
my %languages     = %$langref;
my $q             = new CGI;
my $session    = new CGI::Session( undef, $q, { Directory => "$tempdir" } );
my $globalstat = "";
my $user_name;

if ( defined( $q->param('logout') ) && $q->param('logout') eq "on" )
{    # Logout user
    $session->clear( ["~logged-in"] );
}

if ( $session->param("~logged-in") ) {
    $user_name = $session->param('user_name');
}
else {    # User did not login yet....forward him to the login page
    print $q->redirect("login.cgi");
    exit;
}

if ( defined( $q->param('submit') ) && $q->param('submit') eq "Update" ) {
    ChangeData();
    print $q->redirect( "index.cgi?CGISESSID="
            . $session->id()
            . "&globalstat="
            . uri_escape($globalstat) );
}
else {
    print $q->header;
    PrintHTMLMain();
}

$dbh->disconnect;

sub PrintHTMLMain {

    my ( $username, $realname, $emailaddress, $language, $user_id ) =
        $dbh->exec_single_select(
        "SELECT u.username, u.realname, u.emailadres, u.language, u.id FROM users u WHERE u.username= '"
            . $user_name
            . "'" );
    my $password = $auth->getpwd($user_name);

    my $session_id = $session->id();

    my $template =
        HTML::Template->new(
        filename => "${templatedir}/${language}/preferences.html" );
    $template->param(
        REALNAME     => $realname,
        GLOBALSTAT   => $globalstat,
        SESSIONID    => $session_id,
        USERID       => $user_id,
        USERNAME     => $username,
        PASSWORD     => $password,
        ALLOWUPDATE  => $allowupdate,
        EMAILADDRESS => $emailaddress,
        LANGUAGE     => $language
    );

    my @langs = ();
    foreach my $key ( keys %languages ) {
        my $data = {};
        $data->{LANGOPT}      = $key;
        $data->{LANGSELECTED} = $defaultlang eq $key;
        $data->{LANGVAL}      = $languages{$key};
        push @langs, $data;
    }
    $template->param( LANGUAGES => \@langs );
    print $template->output();

}

sub ChangeData {
    my $password     = uri_unescape( $q->param('password') );
    my $emailaddress = uri_unescape( $q->param('emailaddress') );
    my $realname     = uri_unescape( $q->param('realname') );
    my $language     = uri_unescape( $q->param('language') );

    $dbh->exec_dml(
        "UPDATE users SET realname = ?, emailadres = ?, language = ? WHERE username = ?",
        $realname, $emailaddress, $language, $user_name );

    if ( $allowupdate && ( $password ne $auth->getpwd($user_name) ) ) {
        $auth->storepwd( $user_name, $password );
    }
    $globalstat = "Preferences for '$realname' successfully updated";

}