forked from PodcastGenerator/PodcastGenerator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
episodes_upload.php
341 lines (315 loc) · 13.7 KB
/
episodes_upload.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?php
############################################################
# PODCAST GENERATOR
#
# Created by Alberto Betella and Emil Engler
# http://www.podcastgenerator.net
#
# This is Free Software released under the GNU/GPL License.
############################################################
require 'checkLogin.php';
require '../core/include_admin.php';
if (count($_POST) > 0) {
checkToken();
// CHeck if all fields are set (except "category")
$req_fields = [
$_POST['title'],
$_POST['shortdesc'],
$_POST['date'],
$_POST['time'],
$_POST['explicit']
];
// Check if fields are missing
for ($i = 0; $i < count($req_fields); $i++) {
if (empty($req_fields[$i])) {
$error = _('Missing fields');
goto error;
}
}
// If no categories were selected, add the 'uncategorized'
// category. Otherwise, ensure that no more than three categories
// were actually selected.
if (empty($_POST['category'])) {
$_POST['category'] = array();
array_push($_POST['category'], 'uncategorized');
} elseif (isset($_POST['category']) && count((array)$_POST['category']) > 3) {
$error = _('Too many categories selected (max: 3)');
goto error;
}
// Fill up empty categories (to avoid warnings)
for ($i = 0; $i < 3; $i++) {
if (!isset($_POST['category'][$i])) {
$_POST['category'][$i] = '';
}
}
// Check author e-mail
if (!empty($_POST['authoremail'])) {
if (!filter_var($_POST['authoremail'], FILTER_VALIDATE_EMAIL)) {
$error = _('Invalid Author E-Mail provided');
goto error;
}
}
// Check episode and season numbers
if (!empty($_POST['episodenum'])) {
if (!is_numeric($_POST['episodenum'])) {
$error = _('Invalid Episode Number provided');
goto error;
}
$episodeNum = $_POST['episodenum'] + 0;
if (!is_integer($episodeNum) || $episodeNum < 1) {
$error = _('Invalid Episode Number provided');
goto error;
}
}
if (!empty($_POST['seasonnum'])) {
if (!is_numeric($_POST['seasonnum'])) {
$error = _('Invalid Season Number provided');
goto error;
}
$seasonNum = $_POST['seasonnum'] + 0;
if (!is_integer($seasonNum) || $seasonNum < 1) {
$error = _('Invalid Season Number provided');
goto error;
}
}
if (strlen($_POST['shortdesc']) > 255) {
$error = _("Size of the 'Short Description' exceeded");
goto error;
}
// If we have custom tags, ensure that they're valid XML
$customTags = $_POST['customtags'];
if (!isWellFormedXml($customTags)) {
if ($config['customtagsenabled'] == 'yes') {
$error = _('Custom tags are not well-formed');
goto error;
} else {
// if we have custom tags disabled and the POST value is misformed,
// just clear it out.
$customTags = '';
}
}
// Skip files if they are not strictly named
if ($config['strictfilenamepolicy'] == 'yes') {
if (!preg_match('/^[\w.]+$/', basename($_FILES['file']['name']))) {
$error = _('Invalid filename, only A-Z, a-z, underscores and dots are permitted');
goto error;
}
}
$link = str_replace('?', '', $config['link']);
$link = str_replace('=', '', $link);
$link = str_replace('$url', '', $link);
$targetfile = '../' . $config['upload_dir'] . $_POST['date'] . '_' . basename($_FILES['file']['name']);
$targetfile = str_replace(' ', '_', $targetfile);
if (file_exists($targetfile)) {
$appendix = 1;
while (file_exists($targetfile)) {
$targetfile = '../' . $config['upload_dir'] . $_POST['date'] . '_' . $appendix . '_' . basename($_FILES['file']['name']);
$targetfile = str_replace(' ', '_', $targetfile);
$appendix++;
}
}
$targetfile = strtolower($targetfile);
$targetfile_without_ext = strtolower('../' . $config['upload_dir'] . pathinfo($targetfile, PATHINFO_FILENAME));
$validTypes = simplexml_load_file('../components/supported_media/supported_media.xml');
$fileextension = pathinfo($targetfile, PATHINFO_EXTENSION);
$validFileExt = false;
foreach ($validTypes->mediaFile as $item) {
if ($fileextension == $item->extension) {
$validFileExt = true;
break;
}
}
if (!$validFileExt) {
$error = _('Invalid file extension');
goto error;
}
if (!move_uploaded_file($_FILES['file']['tmp_name'], $targetfile)) {
$error = _('The file upload was not successfully');
goto error;
}
$mimetype = getmime($targetfile);
if (!$mimetype) {
$error = _('The uploaded file is not readable (permission error)');
goto error;
}
$validMimeType = false;
foreach ($validTypes->mediaFile as $item) {
if ($mimetype == $item->mimetype) {
$validMimeType = true;
break;
}
}
if (!$validMimeType) {
$error = sprintf(_('Unsupported mime type detected for file with extension "%s"'), $fileextension);
// Delete the file if the mime type is invalid
unlink($targetfile);
goto error;
}
// Get datetime
$datetime = strtotime($_POST['date'] . ' ' . $_POST['time']);
// Set file date to this date
touch($targetfile, $datetime);
$fileinfo = getID3Info($targetfile);
$duration = $fileinfo['playtime_string']; // Get duration
$bitrate = $fileinfo['audio']['bitrate']; // Get bitrate
$frequency = $fileinfo['audio']['sample_rate']; // Frequency
// Go and actually generate the episode
// It easier to not dynamically generate the file
$episodefeed = '<?xml version="1.0" encoding="utf-8"?>
<PodcastGenerator>
<episode>
<guid>' . htmlspecialchars($config['url'] . "?" . $link . "=" . $targetfile) . '</guid>
<titlePG>' . htmlspecialchars($_POST['title'], ENT_NOQUOTES) . '</titlePG>
<episodeNumPG>' . $_POST['episodenum'] . '</episodeNumPG>
<seasonNumPG>' . $_POST['seasonnum'] . '</seasonNumPG>
<shortdescPG><![CDATA[' . $_POST['shortdesc'] . ']]></shortdescPG>
<longdescPG><![CDATA[' . $_POST['longdesc'] . ']]></longdescPG>
<imgPG></imgPG>
<categoriesPG>
<category1PG>' . htmlspecialchars($_POST['category'][0]) . '</category1PG>
<category2PG>' . htmlspecialchars($_POST['category'][1]) . '</category2PG>
<category3PG>' . htmlspecialchars($_POST['category'][2]) . '</category3PG>
</categoriesPG>
<keywordsPG>' . htmlspecialchars($_POST['itunesKeywords']) . '</keywordsPG>
<explicitPG>' . $_POST['explicit'] . '</explicitPG>
<authorPG>
<namePG>' . htmlspecialchars($_POST['authorname']) . '</namePG>
<emailPG>' . htmlspecialchars($_POST['authoremail']) . '</emailPG>
</authorPG>
<fileInfoPG>
<size>' . intval($_FILES['file']['size'] / 1000 / 1000) . '</size>
<duration>' . $duration . '</duration>
<bitrate>' . substr(strval($bitrate), 0, 3) . '</bitrate>
<frequency>' . $frequency . '</frequency>
</fileInfoPG>
<customTagsPG><![CDATA[' . $customTags . ']]></customTagsPG>
</episode>
</PodcastGenerator>';
file_put_contents($targetfile_without_ext . '.xml', $episodefeed);
// Write image if set
if (isset($fileinfo["comments"]["picture"])) {
$imgext = ($fileinfo["comments"]["picture"][0]["image_mime"] == "image/png") ? 'png' : 'jpg';
$img_filename = $config["absoluteurl"] . $config["img_dir"] . pathinfo($targetfile, PATHINFO_FILENAME) . '.' . $imgext;
file_put_contents($img_filename, $fileinfo["comments"]["picture"][0]["data"]);
}
generateRSS();
pingServices();
$success = true;
error:
}
$categories = simplexml_load_file('../categories.xml');
if (!isset($customTags)) {
$customTags = '';
}
?>
<!DOCTYPE html>
<html>
<head>
<title><?= htmlspecialchars($config['podcast_title']); ?> - <?= _('Upload Episode') ?></title>
<meta charset="utf-8">
<link rel="stylesheet" href="../core/bootstrap/style.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" type="image/x-icon" href="<?= $config['url'] ?>favicon.ico">
</head>
<body>
<?php
include 'js.php';
include 'navbar.php';
?>
<br>
<div class="container">
<h1><?php _('Upload Episode'); ?></h1>
<?php if (isset($success)) { ?>
<strong><p style="color: #2ecc71;"><?= htmlspecialchars($_POST['title']) . ' ' . _('uploaded successfully') ?></p></strong>
<?php } ?>
<?php if (isset($error)) { ?>
<strong><p style="color: #e74c3c;"><?= $error ?></p></strong>
<?php } ?>
<form method="POST" enctype="multipart/form-data">
<div class="row">
<div class="col-6">
<h3><?= _('Main Informations') ?></h3>
<hr>
<div class="form-group">
<?= _('File') ?>*:<br>
<input type="file" name="file" required><br>
</div>
<div class="form-group">
<?= _('Title') ?>*:<br>
<input type="text" name="title" class="form-control" required>
</div>
<div class="form-group">
<?= _('Short Description') ?>*:<br>
<input type="text" id="shortdesc" name="shortdesc" class="form-control" maxlength="255" oninput="shortDescCheck()" required>
<i id="shortdesc_counter">255 <?= _('characters remaining') ?></i>
</div>
<div class="form-group" style="display: <?= ($config['categoriesenabled'] != 'yes') ? 'none' : 'block' ?>">
<?= _('Category') ?>:<br>
<small><?= _('You can select up to 3 categories') ?></small><br>
<select name="category[ ]" multiple>
<?php foreach ($categories as $item) { ?>
<option value="<?= htmlspecialchars($item->id) ?>"><?= htmlspecialchars($item->description) ?></option>
<?php } ?>
</select>
</div>
<div class="form-group">
<?= _('Publication Date') ?>:<br>
<small><?= _('If you select a date in the future, it will be published then') ?></small><br>
<?= _('Date') ?>*:<br>
<input name="date" type="date" value="<?= date("Y-m-d") ?>" required><br>
<?= _('Time') ?>*:<br>
<input name="time" type="time" value="<?= date("H:i") ?>" required><br>
</div>
</div>
<div class="col-6">
<h3><?= _('Extra Informations') ?></h3>
<hr>
<div class="form-group">
<?= _('Long Description') ?>:<br>
<textarea name="longdesc"></textarea><br>
</div>
<div class="form-group">
<?= _('Episode Number') ?>:<br>
<input type="text" name="episodenum" pattern="[0-9]*" class="form-control"><br>
</div>
<div class="form-group">
<?= _('Season Number') ?>:<br>
<input type="text" name="seasonnum" pattern="[0-9]*" class="form-control"><br>
</div>
<div class="form-group">
<?= _('iTunes Keywords') ?>:<br>
<input type="text" name="itunesKeywords" placeholder="Keyword1, Keyword2 (max 12)" class="form-control"><br>
</div>
<div class="form-group">
<?= _('Explicit content') ?>:<br>
<label><input type="radio" value="yes" name="explicit"> <?= _('Yes') ?></label>
<label><input type="radio" value="no" name="explicit" checked> <?= _('No') ?></label><br>
</div>
<div class="form-group">
<?= _('Author') ?>*:<br>
<input type="text" class="form-control" name="authorname" placeholder="<?= htmlspecialchars($config["author_name"]) ?>"><br>
<input type="email" class="form-control" name="authoremail" placeholder="<?= htmlspecialchars($config["author_email"]) ?>"><br>
</div>
<div class="form-group" style="display: <?= ($config['customtagsenabled'] != 'yes') ? 'none' : 'block' ?>">
<?= _('Custom Tags') ?><br>
<textarea name="customtags"><?= htmlspecialchars($customTags) ?></textarea><br>
</div>
</div>
</div>
<div class="row">
<div class="col-6 offset-6">
<input type="hidden" name="token" value="<?= $_SESSION['token'] ?>">
<input type="submit" class="btn btn-success btn-lg" value="<?= _('Upload episode') ?>">
</div>
</div>
</form>
</div>
<script type="text/javascript">
function shortDescCheck() {
let shortdesc = document.getElementById("shortdesc").value;
let maxlength = 255;
let counter = document.getElementById("shortdesc_counter").innerText = (maxlength - shortdesc.length) + " " + <?= '"' . _('characters remaining') . '"' ?>;
}
</script>
</body>
</html>