This repository was archived by the owner on Nov 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewDef.php
191 lines (171 loc) · 7.02 KB
/
newDef.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
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
<?php
require 'vendor/autoload.php';
require 'session.php';
use SVBX\Deficiency;
function getClass() {
if (!empty($_POST['class'])) return $_POST['class'];
if (!empty($_GET['class'])) return $_GET['class'];
}
if ($_SESSION['role'] <= 10
|| (empty($_SESSION['bdPermit'])
&& (getClass() === 'bart'))) {
error_log('Unauthorized client tried to access newdef.php from ' . $_SERVER['HTTP_ORIGIN']);
header('This is not for you', true, 403);
exit;
}
// if POST data rec'd, try to INSERT new Def in db
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$id = intval($_POST['id']);
$postClass = !empty($_POST['class']) ? $_POST['class'] : '';
$class = sprintf('SVBX\%sDeficiency', $postClass);
$createdByField = $postClass === 'bart' ? 'userID' : 'username';
try {
$ref = new ReflectionClass($class);
$def = $ref->newInstanceArgs([ $id, $_POST ]);
$def->set('created_by', $_SESSION[$createdByField]);
$def->insert();
// if INSERT succesful, prepare, upload, and INSERT photo
// TODO: make all this one transcaction handled by the Deficiency object
// TODO: create classes for Comment and Attachment
if ($_FILES['CDL_pics']['size']
&& $_FILES['CDL_pics']['name']
&& $_FILES['CDL_pics']['tmp_name']
&& $_FILES['CDL_pics']['type'])
{
$CDL_pics = $_FILES['CDL_pics'];
} else $CDL_pics = null;
if ($CDL_pics) {
require 'uploadImg.php';
$link = new MysqliDb(DB_CREDENTIALS);
$table = 'CDL_pics';
$fields = [
'defID' => $def->get('id'),
'pathToFile' => null
];
$fields['pathToFile'] = saveImgToServer($CDL_pics, $fields['defID']); // TODO: if defID is missing, throw error
$fields['pathToFile'] = filter_var($fields['pathToFile'], FILTER_SANITIZE_SPECIAL_CHARS);
if ($fields['pathToFile']) {
if (!$link->insert($table, $fields))
$_SESSION['errorMsg'] = "There was a problem adding new picture: {$link->getLastError()}";
}
}
if (!empty($_FILES['attachment'])
&& $_FILES['attachment']['size']
&& $_FILES['attachment']['name']
&& $_FILES['attachment']['tmp_name']
&& $_FILES['attachment']['type'])
{
$attachment = $_FILES['attachment'];
} else $attachment = null;
if ($attachment) {
require('uploadAttachment.php');
$link = (!empty($link) && is_a($link, 'MysqliDb'))
? $link
: new MysqliDb(DB_CREDENTIALS);
uploadAttachment($link, 'attachment', 'uploads/bartdlUploads', $def->get('id'));
}
// if comment submitted commit it to a separate table
if (strlen($_POST['comment'])) {
$link = (!empty($link) && is_a($link, 'MysqliDb'))
? $link
: new MysqliDb(DB_CREDENTIALS);
list($table, $commentField, $defID) = [
$def->commentsTable['table'],
$def->commentsTable['field'],
$def->commentsTable['defID']
];
$fields = [
$defID => $def->get('id'),
$commentField => trim(filter_var($_POST['comment'], FILTER_SANITIZE_SPECIAL_CHARS)),
'userID' => $_SESSION['userID']
];
if ($fields[$commentField])
if (!$link->insert($table, $fields))
$_SESSION['errorMsg'] = "There was a problem adding new comment: {$link->getLastError()}";
}
$location = '/def.php';
$qs = '?' . ($class === 'SVBX\Deficiency'
? 'defID' : 'bartDefID')
. "={$def->get('id')}";
header("location: /def.php{$qs}");
} catch (\ReflectionException $e) {
error_log($e);
header("No Class found for the deficiency type $class", true, 400);
} catch (\Exception $e) {
error_log($e);
$_SESSION['errorMsg'] = 'Something went wrong in trying to add your new deficiency: ' . $e->getMessage();
$location = '/newDef.php';
$qs = '?' . http_build_query($def->get());
header("Location: $location{$qs}");
} catch (\Error $e) {
error_log($e);
} finally {
if (!empty($link) && is_a($link, 'MysqliDb')) $link->disconnect();
exit;
}
}
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$getClass = !empty($_GET['class']) ? $_GET['class'] : '';
$class = sprintf('SVBX\%sDeficiency', $getClass);
$template = $getClass === 'bart' ? 'bartForm.html.twig' : 'defForm.html.twig';
$pageHeading = '';
// this is a clone of another def
if (!empty($_GET['id'])) {
try {
$defID = intval($_GET['id']);
$ref = new ReflectionClass($class);
$def = $ref->newInstanceArgs([ $defID ]);
$def->set($_GET);
$pageHeading = "Clone Deficiency No. {$_GET['id']}";
} catch (\ReflectionException $e) {
error_log($e);
header("No Class found for the deficiency type $class", true, 400);
} catch (\Exception $e) {
error_log("{$_SERVER['PHP_SELF']} tried to fetch a non-existent Deficiency\n$e");
}
}
// this is a clone of a bart def (?)
elseif (!empty($_GET['descriptive_title_vta'])) {
try {
$ref = new ReflectionClass($class);
$def = $ref->newInstanceArgs([null, $_GET]);
} catch (\ReflectionException $e) {
error_log($e);
header("No Class found for the deficiency type $class", true, 400);
exit;
} catch (\Exception $e) {
error_log("{$_SERVER['PHP_SELF']} tried to fetch a non-existent Deficiency\n$e");
}
}
elseif (!empty($_GET)) {
$ref = new ReflectionClass($class);
$def = $ref->newInstanceArgs([ null, $_GET ]);
} else $def = null;
// instantiate Twig
$loader = new Twig_Loader_Filesystem('./templates');
$twig = new Twig_Environment($loader, [ 'debug' => getenv('PHP_ENV') === 'dev' ]);
if (getenv('PHP_ENV') === 'dev') $twig->addExtension(new Twig_Extension_Debug());
$context = [
'session' => $_SESSION,
'title' => 'Add new deficiency',
'pageHeading' => $pageHeading ?: "Add New Deficiency",
'formAction' => $_SERVER['PHP_SELF']
];
if (!empty($_SESSION['errorMsg']))
unset($_SESSION['errorMsg']);
try {
$context['options'] = $class::getLookupOptions();
if (!empty($def) && is_a($def, $class)) {
$def->set($class::MOD_HISTORY); // clear modification history
$data = $def->get();
$context['data'] = $data;
}
$twig->display($template, $context);
} catch (Exception $e) {
error_log($e);
} finally {
if (!empty($link) && is_a($link, 'MysqliDb')) $link->disconnect();
exit;
}
} else header("What do you think you're doing?", true, 400);
exit;