add_post.php (2867B) download
1<?php
2 if ($_SERVER["REQUEST_METHOD"] == "POST") {
3 $errors = array(); // initialize an empty array to store errors
4
5 // Check if voornaam is set and not empty
6 if (isset($_POST['voornaam']) && !empty($_POST['voornaam'])) {
7 $fname = $_POST['voornaam'];
8 } else {
9 $errors[] = "Voornaam is required";
10 }
11
12 // Check if achternaam is set and not empty
13 if (isset($_POST['achternaam']) && !empty($_POST['achternaam'])) {
14 $lname = $_POST['achternaam'];
15 } else {
16 $errors[] = "Achternaam is required";
17 }
18
19 // Check if email is set and not empty
20 if (isset($_POST['email']) && !empty($_POST['email'])) {
21 $email = $_POST['email'];
22 } else {
23 $errors[] = "E-mail is required";
24 }
25
26 // Check if password is set and not empty
27 if (isset($_POST['password']) && !empty($_POST['password'])) {
28 $password = $_POST['password'];
29 } else {
30 $errors[] = "Wachtwoord is required";
31 }
32
33 // Check if permissions is set
34 if (isset($_POST['permissions'])) {
35 $permissions = $_POST['permissions'];
36 } else {
37 $errors[] = "Permissies zijn vereist";
38 }
39
40 // Check if there are any errors
41 if (count($errors) > 0) {
42 // Print out the errors
43 foreach ($errors as $error) {
44 echo $error . "<br>";
45 }
46 } else {
47 // Pass the password through a hashing function
48 $hashed_pwd = password_hash($password, PASSWORD_DEFAULT);
49
50 //create a database object with table user
51 $u = $db->get(Model\User::class);
52
53 //check if email already exists
54 if ($u->load($email)) {
55 echo "this email address is taken: " . $email;
56 } else {
57 $succes = false;
58 //set new user data
59 $u->email = $email;
60 $u->fname = $fname;
61 $u->lname = $lname;
62 $u->pwd = $hashed_pwd;
63
64 //add user with the add function
65 if ($u->insert()) {
66 $succes = true;
67 }
68
69
70 //create a database object with table permission for each permission
71 //set the data and execute the add function
72 foreach ($permissions as $permission) {
73 $p = $db->get(Model\PermissionUser::class);
74 $p->email = $email;
75 $p->id = (int) $permission;
76 if ($p->insert()) {
77 $succes = true;
78 }
79 }
80 if ($succes) {
81 echo "succes!";
82 }
83 }
84 }
85 }
86 ?>