forked from htmlacademy-php1/2074903-doingsdone-14
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sign.php
91 lines (78 loc) · 2.07 KB
/
sign.php
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
<?php
require_once 'init.php';
require_once 'model.php';
require_once 'helpers.php';
require_once 'myfunction.php';
$users = get_users($con);
$emails = array_column($users, 'email');
$projects = [];
$project_id = null;
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$required = ['email', 'password', 'name'];
$errors = [];
$rules = [
'email' => function ($value) use ($emails) {
return validate_email($value, $emails);
},
'password' => function ($value) {
return validate_length($value, 8, 15);
},
'name' => function ($value) {
return validate_name($value);
}
];
$user_form = filter_input_array(
INPUT_POST,
[
'email' => FILTER_DEFAULT,
'password' => FILTER_DEFAULT,
'name' => FILTER_DEFAULT
],
true
);
foreach ($user_form as $key => $value) {
if (!empty($rules[$key])) {
$rule = $rules[$key];
$errors[$key] = $rule($value);
}
if (in_array($key, $required) && empty($value)) {
$errors[$key] = "Поле $key надо заполнить";
}
}
$errors = array_filter($errors);
if (count($errors)) {
$page_content = include_template(
'register.php',
[
'user' => $user_form,
'errors' => $errors
]
);
} else {
$result = add_user($con, $user_form);
if ($result) {
$user_id = mysqli_insert_id($con);
header("Location: index.php");
}
}
} else {
$page_content = include_template('register.php');
}
$navigation_content = include_template(
'navigation.php',
[
'user' => [],
'projects' => $projects,
'project_id' => $project_id,
'content' => $page_content
]
);
$layout_content = include_template(
'layout.php',
[
'user' => [],
'navigation' => $navigation_content,
'title' => 'Дела в порядке'
]
);
print($layout_content);