-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.php
198 lines (164 loc) · 5.95 KB
/
create.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
<?php
require_once "assets/config/config.php";
require_once "vendor/autoload.php";
require_once "assets/functions/procedural_database_functions.php";
require_once "assets/functions/login_functions.php";
use mysql_xdevapi\Exception;
use Miniature\Resize;
//unset($_SESSION['id']);
if (!isset($_SESSION['id'])) {
header('Location: login.php');
exit();
}
$save_result = false;
if (($_SERVER['REQUEST_METHOD'] === 'POST') && isset($_POST['submit'], $_FILES['image'])) {
$data = $_POST['cms'];
$errors = array();
$exif_data = [];
$file_name = $_FILES['image']['name']; // Temporary file for thumbnails directory:
$file_size = $_FILES['image']['size'];
$file_tmp = $_FILES['image']['tmp_name'];
$file_type = $_FILES['image']['type'];
$file_ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));
/*
* Set EXIF data info of image for database table that is
* if it contains the info otherwise set to null.
*/
if ($file_ext === 'jpeg' || $file_ext === 'jpg') {
/*
* I don't like suppressing errors, but this
* is the only way that I can so
* until find out how to do it this will
* have to do.
*/
$exif_data = @exif_read_data($file_tmp);
if (array_key_exists('Make', $exif_data) && array_key_exists('Model', $exif_data)) {
$data['Model'] = $exif_data['Make'] . ' ' . $exif_data['Model'];
}
if (array_key_exists('ExposureTime', $exif_data)) {
$data['ExposureTime'] = $exif_data['ExposureTime'] . "s";
}
if (array_key_exists('ApertureFNumber', $exif_data['COMPUTED'])) {
$data['Aperture'] = $exif_data['COMPUTED']['ApertureFNumber'];
}
if (array_key_exists('ISOSpeedRatings', $exif_data)) {
$data['ISO'] = "ISO " . $exif_data['ISOSpeedRatings'];
}
if (array_key_exists('FocalLengthIn35mmFilm', $exif_data)) {
$data['FocalLength'] = $exif_data['FocalLengthIn35mmFilm'] . "mm";
}
} else {
$data['Model'] = null;
$data['ExposureTime'] = null;
$data['Aperture'] = null;
$data['ISO'] = null;
$data['FocalLength'] = null;
}
$data['content'] = trim($data['content']);
$extensions = array("jpeg", "jpg", "png");
if (in_array($file_ext, $extensions, true) === false) {
$errors[] = "extension not allowed, please choose a JPEG or PNG file.";
}
if ($file_size >= 44040192) {
$errors[] = 'File size must be less than or equal to 42 MB';
}
/*
* Set the paths to the correct folders
*/
$dir_path = 'assets/uploads/';
/*
* Create unique name for image.
*/
$new_file_name = $dir_path . 'img-gallery-' . time() . '-' . IMAGE_WIDTH . 'x' . IMAGE_HEIGHT . '.' . $file_ext;
move_uploaded_file($file_tmp, $new_file_name);
$resize = new Resize($new_file_name);
$resize->resizeImage(IMAGE_WIDTH, IMAGE_HEIGHT);
$resize->saveImage($new_file_name, 100);
/*
* Set path information for database table.
*/
$data['image_path'] = $new_file_name;
/*
* If no errors save ALL the information to the
* database table.
*/
if (empty($errors) === true) {
/* Save to Database Table CMS */
try {
$today = $todayDate = new DateTime('today', new DateTimeZone("America/Detroit"));
} catch (Exception $e) {
}
$data['date_updated'] = $data['date_added'] = $today->format("Y-m-d H:i:s");
$result = insertData($data, $pdo, 'cms');
if ($result) {
header("Location: index.php");
exit();
}
} else {
return $errors;
}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=yes, initial-scale=1.0">
<title>Create Post</title>
<link rel="stylesheet" media="all" href="assets/css/miniature.css">
</head>
<body class="site">
<div class="nav">
<input type="checkbox" id="nav-check">
<h3 class="nav-title">
PHP Procedural Tutorial
</h3>
<div class="nav-btn">
<label for="nav-check">
<span></span>
<span></span>
<span></span>
</label>
</div>
<div class="nav-links">
<a href="index.php">Home</a>
<a href="create.php">Create</a>
<a href="logout.php">Logout</a>
</div>
</div>
<div class="sidebar">
</div>
<main id="content" class="checkStyle">
<form id="formData" class="form_classes" action="create.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="cms[user_id]" value="3">
<input type="hidden" name="cms[author]" value="John Pepp">
<input type="hidden" name="action" value="upload">
<div class="file-style">
<input id="file" class="file-input-style" type="file" name="image">
<label for="file">Select file</label>
</div>
<select class="select-css" name="cms[page]">
<option value="index">Home</option>
<option value="blog" selected>Blog</option>
<option value="about">About</option>
</select>
<div class="heading-style">
<label class="heading_label_style" for="heading">Heading</label>
<input class="enter_input_style" id="heading" type="text" name="cms[heading]" value="" tabindex="1" required
autofocus>
</div>
<div class="content-style">
<label class="text_label_style" for="content">Content</label>
<textarea class="text_input_style" id="content" name="cms[content]" tabindex="2"></textarea>
</div>
<div class="submit-button">
<button class="form-button" type="submit" name="submit" value="enter">submit</button>
</div>
</form>
</main>
<footer class="colophon">
<p>© <?php echo date("Y") ?> The Photo Tech Guru</p>
</footer>
</body>
</html>