-
Notifications
You must be signed in to change notification settings - Fork 0
/
send.php
155 lines (139 loc) · 5.29 KB
/
send.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
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
<?php
header('Content-type: application/json');
// Set up all response parameters
$response = array();
$response['status'] = 'failed';
$response['error'] = array();
$response['error']['category'] = 'Unknown';
$response['error']['description'] = 'An unknown error has occured';
// Check Permissions
// no perm required
// Set up all input parameters
// $UID = UID
if (
isset($_POST['name']) &&
isset($_POST['spitzname']) &&
isset($_POST['wohnort']) &&
isset($_POST['geburtstag']) &&
isset($_POST['traumberuf']) &&
isset($_POST['wunsch']) &&
isset($_POST['eigenschaft1']) &&
isset($_POST['eigenschaft2']) &&
isset($_POST['eigenschaft3']) &&
isset($_FILES['kinderfoto'])
) {
$name = trim($_POST['name']);
$spitzname = trim($_POST['spitzname']);
$wohnort = trim($_POST['wohnort']);
$geburtstag = trim($_POST['geburtstag']);
$traumberuf = trim($_POST['traumberuf']);
$wunsch = trim($_POST['wunsch']);
$eigenschaft1 = trim($_POST['eigenschaft1']);
$eigenschaft2 = trim($_POST['eigenschaft2']);
$eigenschaft3 = trim($_POST['eigenschaft3']);
$kinderfoto = $_FILES['kinderfoto'];
} else {
$response['error']['category'] = 'Parameter error';
$response['error']['description'] = 'One or more parameter are missing';
goto end;
}
// Check Parameters
if (mb_strlen($name, 'UTF-8') <= 0 || mb_strlen($name, 'UTF-8') > 128) {
$response['error']['category'] = 'Parameter error';
$response['error']['description'] = "The parameter 'name' is wrong";
goto end;
}
if (mb_strlen($spitzname, 'UTF-8') > 128) {
$response['error']['category'] = 'Parameter error';
$response['error']['description'] = "The parameter 'spitzname' is wrong";
goto end;
}
if (mb_strlen($wohnort, 'UTF-8') <= 0 || mb_strlen($wohnort, 'UTF-8') > 128) {
$response['error']['category'] = 'Parameter error';
$response['error']['description'] = "The parameter 'wohnort' is wrong";
goto end;
}
if (mb_strlen($geburtstag, 'UTF-8') <= 0 || mb_strlen($geburtstag, 'UTF-8') > 128) {
$response['error']['category'] = 'Parameter error';
$response['error']['description'] = "The parameter 'geburtstag' is wrong";
goto end;
}
if (mb_strlen($traumberuf, 'UTF-8') <= 0 || mb_strlen($traumberuf, 'UTF-8') > 128) {
$response['error']['category'] = 'Parameter error';
$response['error']['description'] = "The parameter 'traumberuf' is wrong";
goto end;
}
if (mb_strlen($wunsch, 'UTF-8') <= 0 || mb_strlen($wunsch, 'UTF-8') > 1024) {
$response['error']['category'] = 'Parameter error';
$response['error']['description'] = "The parameter 'wunsch' is wrong";
goto end;
}
if (mb_strlen($eigenschaft1, 'UTF-8') <= 0 || mb_strlen($eigenschaft1, 'UTF-8') > 1024) {
$response['error']['category'] = 'Parameter error';
$response['error']['description'] = "The parameter 'eigenschaft1' is wrong";
goto end;
}
if (mb_strlen($eigenschaft2, 'UTF-8') <= 0 || mb_strlen($eigenschaft2, 'UTF-8') > 1024) {
$response['error']['category'] = 'Parameter error';
$response['error']['description'] = "The parameter 'eigenschaft2' is wrong";
goto end;
}
if (mb_strlen($eigenschaft3, 'UTF-8') <= 0 || mb_strlen($eigenschaft3, 'UTF-8') > 1024) {
$response['error']['category'] = 'Parameter error';
$response['error']['description'] = "The parameter 'eigenschaft3' is wrong";
goto end;
}
if ($kinderfoto['size'] > 32000000) {
$response['error']['category'] = 'File error';
$response['error']['description'] = "The file 'kinderfoto' is too big";
goto end;
}
// Request
// Connenct to database
include 'mysqlcredentials.php';
// Check connection
if ($pdo === false) {
$response['error']['category'] = 'MySQL error';
$response['error']['description'] = 'Could not connect to database';
goto end;
} else {
// Update database
// prepare statement
$statement = $pdo->prepare("INSERT INTO `einsendungen`(`name`, `spitzname`, `wohnort`, `geburtstag`, `traumberuf`, `wunsch`, `eigenschaft1`, `eigenschaft2`, `eigenschaft3`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
// execute statement
$statement->execute(array($name, $spitzname, $wohnort, $geburtstag, $traumberuf, $wunsch, $eigenschaft1, $eigenschaft2, $eigenschaft3));
// check response from statement
if($statement->errorCode() != 0) {
$stmntError = $statement->errorInfo();
$response['error']['category'] = 'MySQL error';
$response['error']['description'] = $stmntError[2];
goto end;
} else {
// Check how many rows were affected
if ($statement->rowCount() <= 0) {
$response['error']['category'] = 'MySQL error';
$response['error']['description'] = 'Failed to save into database';
goto end;
} else {
// save picture
$saveDirectory = 'bilder/';
// check how many files are in that folder
$files = new FilesystemIterator($saveDirectory, FilesystemIterator::SKIP_DOTS);
$filecount1 = sprintf("%05d", iterator_count($files));
$filecount2 = sprintf("%05d", $filecount1 + 1);
// Target filenam
$clean_name = preg_replace("/[^a-zA-Z0-9]/", "", str_replace(' ', '-', $name));
$kinderfoto_extension = pathinfo($kinderfoto['name'], PATHINFO_EXTENSION);
$kinderfoto_filename = $filecount2."-".$clean_name.'-kinderfoto.'.$kinderfoto_extension;
$kinderfoto_target_file = $saveDirectory.'/'.$kinderfoto_filename;
// move
move_uploaded_file($kinderfoto['tmp_name'], $kinderfoto_target_file);
$response['status'] = 'successful';
unset($response['error']);
}
}
}
// Finish request
end:
echo json_encode($response, JSON_PRETTY_PRINT);
?>