Visa Källkod

The following files exist in this folder. Click to view.

NamnTypStorlek
account.phpPHP Fil11.6 KB
admin.phpPHP Fil23.6 KB
authors.phpPHP Fil4.1 KB
book.phpPHP Fil5.9 KB
db_cnnt.phpPHP Fil407 B
header.phpPHP Fil1.7 KB
home.phpPHP Fil8.4 KB
index.phpPHP Fil5.3 KB
loan_handler.phpPHP Fil1.9 KB
manage_authors.phpPHP Fil4.7 KB
my_loans.phpPHP Fil6.9 KB
S.sqlSQL Fil3.3 KB
search.phpPHP Fil11 KB

account.php

285 lines UTF-8 Windows (CRLF) - Type: PHP Fil
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
<?php
session_start
();
require_once 
'db_cnnt.php';
global 
$pdo;

$action $_POST['action'] ?? '';
$error '';
$success '';

// salt pass, custom hashing
function customPasswordHash($pwd)
{
    
$saltBefore "12Aq@y";
    
$saltAfter "ö%$";
    return 
sha1($saltBefore $pwd $saltAfter);
}

function 
customPasswordVerify($pwd$hash)
{
    return 
customPasswordHash($pwd) === $hash;
}

// register nya  ancändare - validera input & skapa konto
if ($action === 'register') {
    
$username trim($_POST['username'] ?? '');
    
$email trim($_POST['email'] ?? '');
    
$password $_POST['password'] ?? '';

    if (empty(
$username) || empty($email) || empty($password)) {
        
$error 'Fyll i alla fält.';
    } elseif (!
filter_var($emailFILTER_VALIDATE_EMAIL)) {
        
$error 'Ogiltig e-postadress.';
    } elseif (
strlen($password) < 4) {
        
$error 'Lösenordet måste vara minst 4 tecken långt.';
    } else {
        
// Kontrollera om användarnamn eller e-post redan finns
        
$statement $pdo->prepare("SELECT användare_id FROM användare WHERE användare_namn = ? OR email = ?");
        
$statement->execute([$username$email]);
        if (
$statement->fetch()) {
            
$error 'Användarnamnet eller e-postadressen finns redan.';
        } else {
            
// Endast SHA1 kryptering
            
$sha1Hash customPasswordHash($password);
            
$statement $pdo->prepare("INSERT INTO användare (användare_namn, email, sha1_hash, roll_id, skapad) VALUES (?, ?, ?, 2, NOW())");
            
$statement->execute([$username$email$sha1Hash]);
            
$success 'Konto skapat! Du kan nu logga in.';
        }
    }
}

// login
if ($action === 'login') {
    
$username trim($_POST['username'] ?? '');
    
$password $_POST['password'] ?? '';
    
$remember = isset($_POST['remember']);

    
// validate input then fetch user from the db
    
if (empty($username) || empty($password)) {
        
$error 'Felaktigt användarnamn eller lösenord.';
    } else {
        
$statement $pdo->prepare("SELECT a.*, r.roll_namn FROM användare a JOIN roll r ON a.roll_id = r.roll_id WHERE a.användare_namn = ?");
        
$statement->execute([$username]);
        
$user $statement->fetch();

        if (
$user && !empty($user['sha1_hash'])) {
            
$valid customPasswordVerify($password$user['sha1_hash']);
        } else {
            
$valid false;
        }
        
// check login - if valid set session & cookies (remember me = 7 days)
        
if (!$user || !$valid) {
                
$error 'Felaktigt användarnamn eller lösenord.';
            } else {
                if (
$remember) {
                    
$expire time() + (24 60 60);
                    
setcookie('user_id'$user['användare_id'], $expire'/');
                    
setcookie('username'$user['användare_namn'], $expire'/');
                    
setcookie('role'$user['roll_namn'], $expire'/');
            }

            
$_SESSION['user_id'] = $user['användare_id'];
            
$_SESSION['username'] = $user['användare_namn'];
            
$_SESSION['role'] = $user['roll_namn'];
            
$_SESSION['loggedin'] = true;
            
header("Location: home.php");
            exit;
        }
    }
}

// Kontrollera om användaren är inloggad
$isLoggedIn = isset($_SESSION['loggedin']) && $_SESSION['loggedin'] === true;

// Logga ut användare
if ($action === 'logout') {
    
// Radera alla cookies
    
setcookie('user_id'''time() - 3600'/');
    
setcookie('username'''time() - 3600'/');
    
setcookie('role'''time() - 3600'/');
    
session_destroy();
    
header("Location: index.php");
    exit;
}

// Byt lösenord (kräver att användaren är inloggad)
if ($action === 'change_password') {
    if (!
$isLoggedIn) {
        
header("Location: index.php?noaccess=1");
        exit;
    }
    
// få lösenord fields från formen
    
$currentPassword $_POST['current_password'] ?? '';
    
$newPassword $_POST['new_password'] ?? '';
    
$confirmPassword $_POST['confirm_password'] ?? '';

    
// Validera lösenord fältet
    
if (empty($currentPassword) || empty($newPassword) || empty($confirmPassword)) {
        
$error 'Fyll i alla fält.';
    } elseif (
$newPassword !== $confirmPassword) {
        
$error 'Lösenorden matchar inte.';
    } else {
        
// Hämta nuvarande lösenord från databasen
        
$statement $pdo->prepare("SELECT sha1_hash FROM användare WHERE användare_id = ?");
        
$statement->execute([$_SESSION['user_id']]);
        
$user $statement->fetch();

        
// Verifiera nuvarande lösenord
        
$valid false;
        if (!empty(
$user['sha1_hash'])) {
            
$valid customPasswordVerify($currentPassword$user['sha1_hash']);
        }

        if (!
$valid) {
            
$error 'Fel nuvarande lösenord.';
        } else {
            
// Uppdatera med nytt lösenord (endast SHA1)
            
$sha1Hash customPasswordHash($newPassword);
            
$statement $pdo->prepare("UPDATE användare SET sha1_hash = ? WHERE användare_id = ?");
            
$statement->execute([$sha1Hash$_SESSION['user_id']]);
            
$success 'Lösenordet har ändrats!';
        }
    }
}

// Radera konto (kräver inloggning)
if ($action === 'delete_account') {
    if (!
$isLoggedIn) {
        
header("Location: index.php?noaccess=1");
        exit;
    }
    
// Ta bort användaren från databasen
    
$statement $pdo->prepare("DELETE FROM användare WHERE användare_id = ?");
    
$statement->execute([$_SESSION['user_id']]);

    
// Radera cookies och session
    
setcookie('user_id'''time() - 3600'/');
    
setcookie('username'''time() - 3600'/');
    
setcookie('role'''time() - 3600'/');
    
session_destroy();
    
header("Location: index.php?success=deleted");
    exit;
}
?>
<!DOCTYPE html>
<html lang="sv">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Konto & Hantering</title>
    <link rel="stylesheet" href="style.css">
</head>
<body class="auth-page">
<?php if (!$isLoggedIn): ?>
    <div class="auth-container">
        <div class="auth-left">
            <div class="auth-branding">
                <h1>Biblioteket</h1>
                <p class="tagline">Din portal till kunskapen</p>
            </div>
        </div>

        <div class="auth-right">
            <?php if ($error) echo '<div class="notice error">' $error '</div>'?>
            <?php if ($success) echo '<div class="notice success">' $success '</div>'?>

            <div class="auth-tabs">
                <button class="tab-btn active" data-tab="login">Logga in</button>
                <button class="tab-btn" data-tab="register">Registrera</button>
            </div>

            <div class="auth-forms">
                <!-- Login Form -->
                <div class="tab-content active" id="login-tab">
                    <form action="account.php" method="post">
                        <input type="hidden" name="action" value="login">
                        <h2>Välkommen tillbaka</h2>
                        <div class="input-group">
                            <label for="login-username">Användarnamn</label>
                            <input type="text" id="login-username" name="username" placeholder="Skriv ditt användarnamn"
                                   required>
                        </div>
                        <div class="input-group">
                            <label for="login-password">Lösenord</label>
                            <input type="password" id="login-password" name="password" placeholder="Skriv ditt lösenord"
                                   required>
                        </div>
                        <div class="form-footer">
                            <label class="checkbox-label">
                                <input type="checkbox" name="remember">
                                <span>Kom ihåg mig</span>
                            </label>
                        </div>
                        <button type="submit" class="auth-btn">Logga in</button>
                    </form>
                </div>

                <!-- Register Form -->
                <div class="tab-content" id="register-tab">
                    <form action="account.php" method="post">
                        <input type="hidden" name="action" value="register">
                        <h2>Skapa ditt konto</h2>
                        <div class="input-group">
                            <label for="reg-username">Användarnamn</label>
                            <input type="text" id="reg-username" name="username" placeholder="Välj ett användarnamn"
                                   required>
                        </div>
                        <div class="input-group">
                            <label for="reg-email">E-post</label>
                            <input type="email" id="reg-email" name="email" placeholder="din@email.se" required>
                        </div>
                        <div class="input-group">
                            <label for="reg-password">Lösenord</label>
                            <input type="password" id="reg-password" name="password"
                                   placeholder="Välj ett starkt lösenord" required>
                        </div>
                        <button type="submit" class="auth-btn">Skapa konto</button>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <script>
        document.querySelectorAll('.tab-btn').forEach(btn => {
            btn.addEventListener('click', () => {
                const tab = btn.dataset.tab;

                // Update buttons
                document.querySelectorAll('.tab-btn').forEach(b => b.classList.remove('active'));
                btn.classList.add('active');

                // Update content
                document.querySelectorAll('.tab-content').forEach(c => c.classList.remove('active'));
                document.getElementById(tab + '-tab').classList.add('active');
            });
        });
    </script>
<?php else: ?>
    <div class="center-wrap" style="background: white;">
        <?php if ($error) echo '<div class="notice error">' $error '</div>'?>
        <?php if ($success) echo '<div class="notice success">' $success '</div>'?>
        <!-- Change Password Form -->
        <form action="account.php" method="post">
            <input type="hidden" name="action" value="change_password">
            <h2>Byt lösenord</h2>
            <input type="password" name="current_password" placeholder="Nuvarande lösenord" required>
            <input type="password" name="new_password" placeholder="Nytt lösenord" required>
            <input type="password" name="confirm_password" placeholder="Bekräfta nytt lösenord" required>
            <button type="submit">Byt lösenord</button>
        </form>
        <!-- Delete Account Form -->
        <form action="account.php" method="post"
              onsubmit="return confirm('Är du säker på att du vill radera ditt konto? Detta kan inte ångras.');">
            <input type="hidden" name="action" value="delete_account">
            <button type="submit" style="margin-top:1em;">Radera konto</button>
        </form>
        <form action="account.php" method="post" style="margin-top:1em;">
            <input type="hidden" name="action" value="logout">
            <button type="submit">Logga ut</button>
        </form>
    </div>
<?php endif; ?>
</body>
</html>