forked from opencaching/opencaching-pl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
newmp3.php
235 lines (189 loc) · 9.88 KB
/
newmp3.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
<?php
use src\Utils\Database\XDb;
use src\Utils\Generators\Uuid;
use src\Models\OcConfig\OcConfig;
use src\Models\ApplicationContainer;
require_once (__DIR__.'/lib/common.inc.php');
//user logged in?
$loggedUser = ApplicationContainer::GetAuthorizedUser();
if (!$loggedUser) {
$target = urlencode(tpl_get_current_page());
tpl_redirect('login.php?target=' . $target);
exit;
}
$tplname = 'newmp3';
$view = tpl_getView();
$view->setVar('maxMp3Size', $maxmp3size);
require_once(__DIR__.'/src/Views/newmp3.inc.php');
$objectid = isset($_REQUEST['objectid']) ? $_REQUEST['objectid'] : 0;
$type = isset($_REQUEST['type']) ? $_REQUEST['type'] : -1;
$def_seq_m = isset($_REQUEST['def_seq_m']) ? $_REQUEST['def_seq_m'] : 1; // set up default seq for newly added mp3
$bNoDisplay = isset($_REQUEST['notdisplay']) ? $_REQUEST['notdisplay'] : 0;
if (($bNoDisplay != 0) && ($bNoDisplay != 1))
$bNoDisplay = 0;
$title = isset($_REQUEST['title']) ? stripslashes($_REQUEST['title']) : '';
$allok = true;
if (!is_numeric($objectid))
$allok = false;
if (!is_numeric($type))
$allok = false;
if ($allok == true) {
//check if object exists and we are the owner (allowed to upload a mp3)
switch ($type) {
// log
case 1:
$rs = XDb::xSql(
"SELECT `user_id`, `cache_id` FROM `cache_logs`
WHERE `deleted`=0 AND `id`= ? LIMIT 1", $objectid);
if ( ! $r = XDb::xFetchArray($rs))
$allok = false;
else {
if ($r['user_id'] != $loggedUser->getUserId())
$allok = false;
$cacheid = $r['cache_id'];
tpl_set_var('cacheid', $cacheid);
tpl_set_var('mp3typedesc', $mp3typedesc_log);
$rCache['name'] = XDb::xMultiVariableQueryValue(
"SELECT `name` FROM `caches` WHERE `cache_id`= :1 LIMIT 1", '-no-name-', $cacheid);
tpl_set_var('cachename', htmlspecialchars($rCache['name'], ENT_COMPAT, 'UTF-8'));
tpl_set_var('begin_cacheonly', '<!--');
tpl_set_var('end_cacheonly', '-->');
}
XDb::xFreeResults($rs);
break;
// cache
case 2:
$rs = XDb::xSql(
"SELECT `user_id`, `cache_id`, `name` FROM `caches`
WHERE `cache_id`= ? LIMIT 1", $objectid);
if ( !$r = XDb::xFetchArray($rs) )
$allok = false;
else {
tpl_set_var('cachename', htmlspecialchars($r['name'], ENT_COMPAT, 'UTF-8'));
tpl_set_var('cacheid', $r['cache_id']);
tpl_set_var('mp3typedesc', $mp3typedesc_cache);
if ($r['user_id'] != $loggedUser->getUserId())
$allok = false;
}
tpl_set_var('begin_cacheonly', '');
tpl_set_var('end_cacheonly', '');
XDb::xFreeResults($rs);
break;
default:
$allok = false;
break;
}
$errnofilegiven = false;
if (isset($_REQUEST['submit'])) {
if (isset($_FILES['file']['error'])) {
if ($_FILES['file']['error'] == UPLOAD_ERR_NO_FILE) {
$errnofilegiven = true;
$allok = false;
}
}
}
$errnotitle = false;
if (($title == '') && (isset($_REQUEST['submit']))) {
$allok = false;
$errnotitle = true;
}
if ($allok == true) {
// ok, we have a valid objectid and are the owner ... so show the form or write to DB ...
if (isset($_REQUEST['submit'])) {
// was the file uploaded successfully ?
if ($_FILES['file']['error'] != 0) {
// oops ... no idea how to handle this.
$tplname = 'message';
tpl_set_var('messagetitle', $message_title_internal);
tpl_set_var('message_start', '');
tpl_set_var('message_end', '');
tpl_set_var('message', $message_internal);
tpl_BuildTemplate();
exit;
} else {
// correct file extension?
$fna = mb_split('\\.', $_FILES['file']['name']);
$extension = mb_strtolower($fna[count($fna) - 1]);
if (mb_strpos($mp3extensions, ';' . $extension . ';') === false) {
$tplname = 'message';
tpl_set_var('messagetitle', $message_title_wrongext);
tpl_set_var('message_start', '');
tpl_set_var('message_end', '');
tpl_set_var('message', $message_wrongext);
tpl_BuildTemplate();
exit;
}
// file too big?
if ($_FILES['file']['size'] > $maxmp3size) {
$tplname = 'message';
tpl_set_var('messagetitle', $message_title_toobig);
tpl_set_var('message_start', '');
tpl_set_var('message_end', '');
tpl_set_var('message', $message_toobig);
tpl_BuildTemplate();
exit;
}
$uuid = Uuid::create();
// move the file and insert entry to DB
move_uploaded_file($_FILES['file']['tmp_name'], $mp3dir . '/' . $uuid . '.' . $extension);
XDb::xSql(
"INSERT INTO mp3 (`uuid`, `url`, `last_modified`, `title`, `date_created`, `last_url_check`,
`object_id`, `object_type`, `user_id`, `local`, `display`, `node`, `seq`)
VALUES (? , ?, NOW(), ?, NOW(), NOW(), ?, ?, ?, 1, ?, ?, ?)",
$uuid, $mp3url . '/' . $uuid . '.' . $extension, $title, $objectid,
$type, $loggedUser->getUserId(), ($bNoDisplay == 1) ? '0' : '1', OcConfig::getSiteNodeId(), $def_seq_m);
switch ($type) {
// log
case 1:
XDb::xSql(
"UPDATE `cache_logs` SET `mp3count`=`mp3count`+1, `last_modified`=NOW()
WHERE `id`= ? LIMIT 1", $objectid);
tpl_redirect('viewcache.php?cacheid=' . urlencode($cacheid));
break;
// cache
case 2:
XDb::xSql(
"UPDATE `caches` SET `mp3count`=`mp3count`+1, `last_modified`=NOW()
WHERE `cache_id`= ? LIMIT 1", $objectid);
tpl_redirect('editcache.php?cacheid=' . urlencode($objectid));
break;
}
tpl_redirect_absolute($mp3url . '/' . $uuid . '.' . $extension);
exit;
}
}
tpl_set_var('notdisplaychecked', ($bNoDisplay == 1) ? ' checked="checked"' : '');
tpl_set_var('type', htmlspecialchars($type, ENT_COMPAT, 'UTF-8'));
tpl_set_var('objectid', htmlspecialchars($objectid, ENT_COMPAT, 'UTF-8'));
tpl_set_var('def_seq_m', htmlspecialchars($def_seq_m, ENT_COMPAT, 'UTF-8')); //update hidden value in newmp3.tbl.php
tpl_set_var('title', htmlspecialchars($title, ENT_COMPAT, 'UTF-8'));
tpl_set_var('maxmp3size', $maxmp3size);
tpl_set_var('submit', $submit);
tpl_set_var('errnotitledesc', '');
tpl_set_var('errnomp3givendesc', '');
} else {
if (($errnofilegiven == true) || ($errnotitle = true)) {
tpl_set_var('notdisplaychecked', ($bNoDisplay == 1) ? ' checked="checked"' : '');
tpl_set_var('type', htmlspecialchars($type, ENT_COMPAT, 'UTF-8'));
tpl_set_var('objectid', htmlspecialchars($objectid, ENT_COMPAT, 'UTF-8'));
tpl_set_var('title', htmlspecialchars($title, ENT_COMPAT, 'UTF-8'));
tpl_set_var('maxmp3size', $maxmp3size);
tpl_set_var('submit', $submit);
tpl_set_var('errnomp3givendesc', '');
tpl_set_var('errnotitledesc', '');
if ($errnofilegiven == true)
tpl_set_var('errnomp3givendesc', $errnomp3givendesc);
if ($errnotitle == true)
tpl_set_var('errnotitledesc', $errnotitledesc);
}
else {
$tplname = 'message';
tpl_set_var('messagetitle', $message_title_internal);
tpl_set_var('message_start', '');
tpl_set_var('message_end', '');
tpl_set_var('message', $message_internal);
}
}
}
//make the template and send it out
tpl_BuildTemplate();