-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform.php
280 lines (239 loc) · 9.05 KB
/
form.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
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
<?php
include("includes/init.php");
// declare constants
const INPUT_TYPE = ["article", "video"];
const VALID_EXTNS = ["jpg", "jpeg", "png"];
const UPLOAD_PATH = "img/uploads/";
$GLOBALS['messages'] = array();
$upload_submitted = false;
// is valid inputs of form
$is_valid = true;
if (isset($_POST["submit_button"])){
// grab text inputs
$title = filter_input(INPUT_POST, 'title', FILTER_SANITIZE_STRING);
$tag_line = filter_input(INPUT_POST, 'tag_line', FILTER_SANITIZE_STRING);
$url = filter_input(INPUT_POST, 'url', FILTER_SANITIZE_URL);
$tag_1 = filter_input(INPUT_POST, 'tag_1', FILTER_SANITIZE_STRING);
$tag_2 = filter_input(INPUT_POST, 'tag_2', FILTER_SANITIZE_STRING);
// check if tags are the distinct and not null
if (!($tag_1 == $tag_2)) {
if ($tag_1 && $tag_2) {
$field_str = "tag_1, tag_2";
$param_str = ":tag_1, :tag_2";
} else if ($tag_1) {
$field_str = "tag_1";
$param_str = ":tag_1";
} else if ($tag_2) {
$field_str = "tag_2";
$param_str = ":tag_2";
} else {
$is_valid = false;
}
} else {
$is_valid = false;
record_message("Tag can only be selected once. ");
}
// only can be 'article' or 'video'
if (in_array(filter_input(INPUT_POST, 'input_type', FILTER_SANITIZE_STRING), INPUT_TYPE)){
$input_type = filter_input(INPUT_POST, 'input_type', FILTER_SANITIZE_STRING);
} else {
$is_valid = false;
}
// grab file data
$file = $_FILES["input_photo"];
$upload_name = basename($file["name"]);
$upload_ext = strtolower(pathinfo($upload_name, PATHINFO_EXTENSION) );
// assert valid file extension on backend
if (!in_array($upload_ext, VALID_EXTNS)){
$is_valid = false;
};
// catenate path for backend database
$file_path = UPLOAD_PATH . $upload_name;
// check output...
var_dump($title);
var_dump($tag_line);
var_dump($tag_1);
var_dump($tag_2);
var_dump($url);
var_dump($input_type);
var_dump($upload_name, $upload_ext);
var_dump($file_path);
// connection string for heroku
$connection_string= "dbname=d9bvjse2g8ba1h host=ec2-54-243-210-70.compute-1.amazonaws.com port=5432 user=dxwirrhzaydomo password=62adc98f8f11caa8d9a71c385d70edb1483dbb761458189b20f5ba9f6ddfae6e sslmode=require";
// PDO connection using heroku string
$conn = new PDO ("pgsql:".$connection_string);
if ($is_valid) {
$insertion_query = "INSERT INTO posts2 (title, tag_line, url, input_type, file_path, $field_str) VALUES (:title, :tag_line, :url, :input_type, :file_path, $param_str)";
$params = array(
":title" => $title,
":tag_line" => $tag_line,
":url" => $url,
":input_type" => $input_type,
":file_path" => $file_path,
);
if ($field_str == "tag_1, tag_2") {
$params["tag_1"] = $tag_1;
$params["tag_2"] = $tag_2;
} else if ($field_str == "tag_1") {
$params["tag_1"] = $tag_1;
} else if ($field_str == "tag_2") {
$params["tag_2"] = $tag_2;
} else {
record_message("Tag values are invalid, check inputs. ");
}
if ($file['error']==0) {
// $ext_str = pathinfo($file['name'], PATHINFO_EXTENSION);
// $ext = filter_var(strtolower($ext_str), FILTER_SANITIZE_STRING);
if (exec_sql_query($conn, $insertion_query, $params)) {
move_uploaded_file($file["tmp_name"], $file_path);
} else {
record_message("Execution of query failed, check inputs.");
}
record_message("Successful Upload!");
} else {
record_message("Unsuccessful upload, check your inputs.");
}
} else {
record_message("Unsuccessful upload, check your inputs.");
}
//TODO: MOVE UPLOADED FILE TO 'img/uploads' folder on successful input into database
};
function print_story($story) {
?>
<tr>
<td><?php echo htmlspecialchars($story["title"]);?></td>
<td>
<?php echo htmlspecialchars($story["tag_line"]);?>
</td>
<td><?php echo htmlspecialchars($story["url"]);?></td>
<td><?php echo "<img id='form_photo' src =\"". ($story["file_path"]). "\">";?></td>
<td><?php echo htmlspecialchars($story["tag_1"]);?></td>
<td><?php echo htmlspecialchars($story["tag_2"]);?></td>
<td><?php echo "<a href=\"". "/edit.php?id=". $story["id"] . "\"> <button class='btn' type='submit' action='edit.php'> Edit </button></a>";?></td>
</tr>
<?php
}
?>
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js"> <!--<![endif]-->
<head>
<?php include "includes/header-core.php"; ?>
</head>
<body id="index">
<!--[if lt IE 7]>
<p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="#">upgrade your browser</a> to improve your experience.</p>
<![endif]-->
<?php include "includes/navbar.php"; ?>
<?php if ($current_user) {?>
<div class="container">
<div class="row">
<h2 class="center-align"> Enter a New Story</h2>
<form method="post" action="form.php" enctype="multipart/form-data" id="submission_form">
<div class="row">
<!-- left side -->
<div class="section col s12 l6">
<h5>Title</h5>
<input class="col s12" type="text" name="title">
</div>
<div class="section col s12 l6">
<h5>Tag Line</h5>
<input id="tag_line" class="col s12" type="text" name="tag_line" data-length="80">
</div>
<div class="section col s12 l6">
<h5>Video or Article</h5>
<p>
<input name="input_type" type="radio" id="article" value="article" />
<label for="article">Article</label>
<input name="input_type" type="radio" id="video" value="video" />
<label for="video">Video</label>
</p>
</div>
<!-- right side -->
<div class="section col s12 l6">
<h5>URL</h5]>
<input class="col s12" type="text" name="url" pattern="https?://.+" title="https or http URLs only">
</div>
<div class="section col s12 l6">
<h5>Tag #1</h5>
<p><select name="tag_1" form="submission_form">
<option value=NULL selected disabled>Select a Tag</option>
<?php
$tag_opts = array("people", "education", "impact", "lifestyle", "events");
foreach ($tag_opts as $tag) {
echo "<option value=$tag>".ucfirst($tag)."</option>";
}
?></select></p>
</div>
<div class="section col s12 l6">
<h5>Tag #2</h5>
<p><select name="tag_2" form="submission_form">
<option value=NULL selected>Select a Tag</option>
<?php
$tag_opts = array("people", "education", "impact", "lifestyle", "events");
foreach ($tag_opts as $tag) {
echo "<option value=$tag>".ucfirst($tag)."</option>";
}
?></select></p>
</div>
<div class="col s12 l6 center-align">
<div class="file-field btn" id="file_">
<span>Upload File</span>
<input type="file" name= "input_photo" id="input_photo" accept="image/*">
</div>
</div>
<div class="col s12 l6 center-align">
<input type="submit" class= "btn" name="submit_button">
</div>
<?php print_messages(); ?>
</form>
</div> <!-- close row -->
<div class="row">
<h2 class="center-align"> Edit an Existing Story</h2>
<?php
$connection_string= "dbname=d9bvjse2g8ba1h host=ec2-54-243-210-70.compute-1.amazonaws.com port=5432 user=dxwirrhzaydomo password=62adc98f8f11caa8d9a71c385d70edb1483dbb761458189b20f5ba9f6ddfae6e sslmode=require";
// PDO connection using heroku string
$conn = new PDO ("pgsql:".$connection_string);
$select_all = "SELECT * From posts2";
$params = array(
);
$stories = exec_sql_query($conn, $select_all, $params)->fetchAll();;
if (isset($stories) and !empty($stories)) {
?>
<table>
<thead>
<th>Title</th>
<th>Tagline</th>
<th>URL</th>
<th>Image</th>
<th>Tag 1</th>
<th>Tag 2</th>
</thead>
<tbody>
<?php
foreach($stories as $story) {
print_story($story);
}
?>
</tbody>
</table>
<?php
}else{
echo "<p>No stories.</p>";
}
?>
</div>
</div>
<?php } else{ ?>
<div class="section col s12 l12 center-align">
<h5>You need to login to view this page</h5>
</div>
<?php } ?>
</div><!--close container -->
<?php include "includes/footer.php"; ?>
<?php include "includes/js-scripts.php"; ?>
</body>
</html>