forked from ScriptureEarth/ScriptureEarth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path00-AudioSaveZip.php
More file actions
430 lines (381 loc) · 18.5 KB
/
00-AudioSaveZip.php
File metadata and controls
430 lines (381 loc) · 18.5 KB
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<?php
// The html tags must not be above and below this php section because the server displays the document and not saves the document!
// Usage: 00-AudioSaveZip.php"+"?T=OT&st="+st+"&iso="+iso+"&rod="+rod+"&Books="+OT.substring(0, OT.length-1) or
// 00-AudioSaveZip.php"+"?T=NT&st="+st+"&iso="+iso+"&rod="+rod+"&Books="+NT.substring(0, NT.length-1)
include ('./OT_Books.php'); // $OT_array
include ('./NT_Books.php'); // $NT_array
include ('./include/conn.inc.php'); // connect to the database named 'scripture'
$db = get_my_db();
include ('./translate/functions.php');
$st = 'eng';
if (isset($_GET['st'])) {
$st = $_GET['st'];
if (!preg_match('/^([a-z]{3})$/', $st)) {
die('`st` is more than 3 characters!</body></html>');
}
}
define ('OT_EngBook', 2);
define ('NT_EngBook', 2);
define ('OT_Abbrev', 5);
define ('NT_Abbrev', 5);
function OT_Test($PDF, $OT_Index) { // returns true if the
global $OT_array; // from NT_Books.php
$a_index = 0;
foreach ($OT_array[$OT_Index] as $a) {
if ($PDF == $a_index) return true;
$a_index++;
}
return false;
}
function NT_Test($PDF, $NT_Index) { // returns true if the
global $NT_array; // from NT_Books.php
$a_index = 0;
foreach ($NT_array[$NT_Index] as $a) {
if ($PDF == $a_index) return true;
$a_index++;
}
return false;
}
if (isset($_GET['iso'])) {
$iso = $_GET['iso'];
if (!preg_match('/^([a-z]{3})$/', $iso)) {
die('`ISO` is more than 3 characters!</body></html>');
}
}
else
die(translate('No ISO was found.', $st, 'sys'));
if (isset($_GET['rod'])) {
$rod = $_GET['rod'];
if (!preg_match('/^([a-zA-Z0-9]{1,5})$/', $rod)) {
die('`ROD_Code` is more than 5 characters!</body></html>');
}
}
else
$rod = '00000';
$Testament = '';
if (isset($_GET['T'])) {
$Testament = $_GET['T'];
if (!preg_match('/^(OT|NT)$/', $Testament)) {
die('`Testament` is not Old or New Testament!</body></html>');
}
}
else
die(translate('No Testament was found.', $st, 'sys'));
if (isset($_GET['Books'])) { // numbers of the books
$Books = $_GET['Books'];
if (!preg_match('/^([, 0-9]+)$/', $Books)) {
die('`Books` were not just numbers!</body></html>');
}
}
else
die(translate('There were no selected audio files.', $st, 'sys'));
$dirname = './data/'.$iso.'/audio/';
/*********************************************************************************************************************
============================================== start AudioSaveZip ===============================================
*********************************************************************************************************************/
$Abbrev_Books = [];
$Selected_Book_Numbers = explode(',', $Books); // numbers of the books
if ($Testament == 'NT') {
for ($index = 0; $index < count($Selected_Book_Numbers); $index++) { // count all selected books in the NT
if ($Selected_Book_Numbers[$index] > 26) {
echo 'Skipped. The selected book number is greater than the number of books in the NT: '.$Selected_Book_Numbers[$index].'<br />';
continue;
}
file_put_contents('AudioSaveZip.txt', 'line 98 iso: '.$iso.'; NT Books (should be books by cammas): #'.$Books."#\n", FILE_APPEND | LOCK_EX);
file_put_contents('AudioSaveZip.txt', 'line 99: NT (0-26): #'.$Selected_Book_Numbers[$index].'#; Book Abbrev: #'.$NT_abbrev_array[$Selected_Book_Numbers[$index]]."#\n", FILE_APPEND | LOCK_EX);
$Abbrev_Books[] = $NT_abbrev_array[$Selected_Book_Numbers[$index]]; // all selected abbreviated books in the NT
}
}
elseif ($Testament == 'OT') {
for ($index = 0; $index < count($Selected_Book_Numbers); $index++) { // count all selected books in the OT
if ($Selected_Book_Numbers[$index] > 38) {
echo 'Skipped. The selected book number is greater than the number of books in the OT: '.$Selected_Book_Numbers[$index].'<br />';
continue;
}
file_put_contents('AudioSaveZip.txt', 'line 109: iso: '.$iso.'; OT Books (should be books by cammas): #'.$Books."#\n", FILE_APPEND | LOCK_EX);
file_put_contents('AudioSaveZip.txt', 'line 110: OT (0-38): #'.$Selected_Book_Numbers[$index].'#; Book Abbrev: #'.$OT_abbrev_array[$Selected_Book_Numbers[$index]]."#\n", FILE_APPEND | LOCK_EX);
$Abbrev_Books[] = $OT_abbrev_array[$Selected_Book_Numbers[$index]]; // all selected abbreviated books in the OT
}
}
else {
die('It is not suppose to happen. Testament is empty.');
}
//print_r($Selected_Book_Numbers);
//echo "<br />";
//print_r($Abbrev_Books);
if (empty($Abbrev_Books)) {
die('Books don"t match up!');
}
$SE_LogPath = "log/";
$CRLF = "\r\n";
// $_SERVER['REMOTE_ADDR'] is the IP address from which the user is viewing the current page.
if ($_SERVER['REMOTE_ADDR'] != '127.0.0.1' && $_SERVER['REMOTE_ADDR'] != '::1') {
$SE_LogPath = "log/";
$CRLF = "\n";
}
// 187.148.228.80 - - [03/Oct/2014:11:23:20 -0600] User has [number of OT/NT audio books] OT/NT audio books. "GET /data/ngu/PDF/47-1COngu-web.pdf HTTP/1.1" 206 33125 "http://www.scriptureearth.org/data/ngu/PDF/47-1COngu-web.pdf"
$data = $_SERVER['REMOTE_ADDR'] . ' - - [' . date("d/M/Y:h:i:s") . ' -0500] User has ' . count($Selected_Book_Numbers) . ' OT/NT audio books. "GET data/' . $iso . '/audio/zipped.zip HTTP/1.1" 206 1024 "https://www.scriptureearth.org/data/' . $iso . '/audio/zipped.zip" "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36"' . $CRLF;
// write $data to $SE_LogPath . "zip-access.log"
file_put_contents($SE_LogPath . "zip-access.log", $data, FILE_USE_INCLUDE_PATH | FILE_APPEND | LOCK_EX);
$files = [];
$stingBooks = str_replace(",", "','", $Books); // gives zzzz','zzzz','zzzz
$stingBooks = "'" . $stingBooks . "'"; // gives 'zzzz','zzzz','zzzz'
if ($Testament == 'OT') {
$query = "SELECT OT_Audio_Filename, OT_Audio_Book FROM OT_Audio_Media WHERE ISO = '$iso' AND ROD_Code = '$rod' AND OT_Audio_Book IN (".$stingBooks.")"; // "IN" handles this
$result=$db->query($query) or die (translate('Query failed:', $st, 'sys') . ' ' . $db->error . '</body></html>');
if ($result->num_rows == 0) {
die(translate('The ISO language code is not found.', $st, 'sys') . '</body></html>');
}
while ($row = $result->fetch_array()) {
$temp = trim($row["OT_Audio_Filename"]);
if (!file_exists($dirname.$temp)) {
continue;
}
$files[] = $temp;
file_put_contents('AudioSaveZip.txt', 'line 153: iso: '.$iso.'; OT (0-38): OT_Audio_Filename: #'.$temp."#\n", FILE_APPEND | LOCK_EX);
}
}
else {
$query = "SELECT NT_Audio_Filename, NT_Audio_Book FROM NT_Audio_Media WHERE ISO = '$iso' AND ROD_Code = '$rod' AND NT_Audio_Book IN (".$stingBooks.")"; // "IN" handles this
$result=$db->query($query) or die (translate('Query failed:', $st, 'sys') . ' ' . $db->error . '</body></html>');
if ($result->num_rows <= 0) {
die(translate('The ISO language code is not found.', $st, 'sys') . '</body></html>');
}
while ($row = $result->fetch_array()) {
$temp = trim($row["NT_Audio_Filename"]);
if (!file_exists($dirname.$temp)) {
continue;
}
$files[] = $temp;
file_put_contents('AudioSaveZip.txt', 'line 168: iso: '.$iso.'; NT (0-26): NT_Audio_Filename: #'.$temp."#\n", FILE_APPEND | LOCK_EX);
}
}
if (!$files) { // if no array files were found
die(translate('Error! files[0] is null!', $st, 'sys').'</body></html>');
}
//print_r($files);
//echo "<br />";
/*********************************************************************************************************
delete all files more than 24 hours from now
*********************************************************************************************************/
$todays_date = getdate();
$todays_date_timestamp = mktime(0,0,0,$todays_date['mon'],$todays_date['mday'],$todays_date['year']);
clearstatcache();
$aFiles = glob('zipfiles/*');
if (is_array($aFiles)) {
foreach ($aFiles as $filename) {
if ($todays_date_timestamp >= (filemtime($filename) + 86400)) { // 86400 is 24 hours in seconds
//echo 'delete: ' . $filename . '<br />';
unlink($filename);
}
}
}
/*********************************************************************************************************
delete same ISO Codes and same filesizes
*********************************************************************************************************/
clearstatcache();
$aFiles = glob('zipfiles/*'); // after delete(s) get new directory
if (is_array($aFiles)) {
$ISO_Code = '';
$filesize = 0;
$temp = 0;
foreach ($aFiles as $filename) {
$temp = strpos($filename, '_'); // strpos = false when not found
if ($temp !== false) {
$ISO_Code_array = [];
$ISO_Code_array = explode('_', $filename);
if ($ISO_Code === $ISO_Code_array[1]) {
if ($filesize === filesize($filename)) {
//echo 'delete: ' . $filename . '<br />';
unlink($filename);
}
else {
$filesize = filesize($filename);
}
}
else {
$ISO_Code = $ISO_Code_array[1];
$filesize = filesize($filename);
}
}
}
}
$filenames = $files;
// create the object
$zip = new ZipArchive();
// ********************************************************************************************************
// Creating zip file.
// ********************************************************************************************************
$Zip_Filename = str_replace(' ', '', microtime());
$Zip_Filename = str_replace('.', '', $Zip_Filename);
$Zip_Filename = $Testament.'_'.$iso.'_'.$Zip_Filename.'.zip';
$archive_file_name = 'zipfiles/' . $Zip_Filename;
//echo "zipfiles/" . $Zip_Filename."<br />";
//echo $filenames[0];
// ********************************************************************************************************
// create the file and throw the error if unsuccessful
// ********************************************************************************************************
if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE) !== TRUE) {
die(translate('Cannot open', $st, 'sys')." $archive_file_name.</body></html>");
}
/*
None of the 'echo' work because of the header(...)!!!!!
*/
//echo "<div style='font-size: 14pt; margin-top: 50px; margin-left: 100px; '>Creating the zip file.</div>";
//echo "<br />Starting the creation of the zip file:<br />";
//flush();
// ********************************************************************************************************
// Add each file of MP3 file array to the zip file. It takes some time.
// If interuupted by user with the "blue" box the script won't get to "unlink('./zipfiles/'.$file)" (see below)!
// And this zip will stay in the "zipfiles" sub-directory without beging delete.
// ********************************************************************************************************
//$yearDate = (int)date('Y'); // get the date of the file
foreach ($filenames as $file) {
//$temp = $dirname . $file;
//echo "temp= $temp<br />";
//if copy is successful
if (@copy($dirname . $file, './zipfiles/'.$file)) { // copy from './data/'.$iso.'/audio/'.$file to './zipfiles/'.$file
// File copied from remote!
$zip->addFile('./zipfiles/'.$file, $file); // add './zipfiles/'.$file to the zip archive file (addFile remote server files will NOT work!)
//$zip->setMtimeName($file, mktime(0,0,0,12,25,$yearDate)); // set the date of the file
}
//if addFile fails
else {
$errors = error_get_last();
echo "COPY ERROR: " . $errors['type'];
echo "<br />" . $errors['message'];
sleep(4); // sleep for 4 seconds
}
//echo "Z ";
//flush();
//echo "$temp<br />";
}
//echo "<br /><br />Writing the zip file...<br />";
//flush();
set_time_limit(60); // The maximum execution time in seconds. If set to zero, no time limit is imposed.
$zip->setArchiveComment(translate('Downloaded from ScriptureEarth.org.', $st, 'sys'));
// Close the zip file.
$zip->close();
// ********************************************************************************************************
// delete copy 'destination' files (above using @copy) must be after zip closes
// ********************************************************************************************************
foreach ($filenames as $file) {
if (file_exists('./zipfiles/'.$file)) {
unlink('./zipfiles/'.$file);
}
}
//echo "The zip file is clossed.<br />";
//flush();
/*
if (file_exists($archive_file_name))
echo "Yeah! It exists!<br /><br />";
else
echo "It does not exist.<br /><br />";
*/
$file = $Zip_Filename;
//$file_real = $dirname . $category . $file;
$file_real = './zipfiles/' . $file;
$ip = $_SERVER['REMOTE_ADDR'];
//echo "<br />Creation of the 'header's for download.<br />";
//flush();
// Check to see if the download script was called
if (basename($_SERVER['PHP_SELF']) == '00-AudioSaveZip.php'){
if ($_SERVER['QUERY_STRING'] != null){
// HACK ATTEMPT CHECK
// Make sure the request isn't escaping to another directory
// [strpos($file, '/') > 0 is not good because the filename has a path]
if (substr($file, 0, 1) == '.' || strpos($file, '..') > 0 || substr($file, 0, 1) == '/') { // || strpos($file, '/') > 0){
// Display hack attempt error
die(translate('Hack attempt detected!', $st, 'sys'));
}
// If requested file exists
if (file_exists($file_real)){
// Get extension of requested file
$extension = strtolower(substr(strrchr($file, "."), 1));
//echo "extension= " . $extension . "<br />";
// Determine correct MIME type
switch($extension){
//case "zip": $type = "application/x-zip-compressed"; break; // up until 12/18/2021 when going from PHP 7.2 to PHP 7.4
case "zip": $type = "application/zip"; break;
case "asf": $type = "video/x-ms-asf"; break;
case "avi": $type = "video/x-msvideo"; break;
case "exe": $type = "application/octet-stream"; break;
case "html": $type = "text/html"; break;
case "htm": $type = "text/html"; break;
case "mov": $type = "video/quicktime"; break;
case "mp3": $type = "audio/mpeg"; break;
case "mpg": $type = "video/mpeg"; break;
case "mpeg": $type = "video/mpeg"; break;
case "pdf": $type = "application/pdf"; break;
case "php": $type = "text/plain"; break;
case "doc": $type = "application/msword"; break;
case "xls": $type = "application/vnd.ms-excel"; break;
case "ppt": $type = "application/vnd.ms-powerpoint"; break;
case "gif": $type = "image/gif"; break;
case "png": $type = "image/png"; break;
case "jpeg": $type = "image/jpg"; break;
case "jpg": $type = "image/jpg"; break;
case "rar": $type = "encoding/x-compress"; break;
case "txt": $type = "text/plain"; break;
case "wav": $type = "audio/wav"; break;
case "wma": $type = "audio/x-ms-wma"; break;
case "wmv": $type = "video/x-ms-wmv"; break;
default: $type = "application/force-download"; break;
}
ob_start(); // Creates an output buffer.
// Fix IE bug [0]
$header_file = (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE')) ? preg_replace('/\./', '%2e', $file, substr_count($file, '.') - 1) : $file;
// Prepare headers
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Description: File Transfer");
header("Content-Type: " . $type);
header("Content-Disposition: attachment; filename=\"" . $header_file . "\";");
//header('Content-Disposition: attachment; filename="'.$name.'"');
/* The three lines below basically make the download non-cacheable. */
header("Cache-control: private");
header('Pragma: private');
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
//header("Cache-Control: public", false);
//header("Pragma: public");
//header("Expires: 0");
header("Accept-Ranges: bytes");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($file_real));
//@ob_end_clean(); //turn off output buffering to decrease cpu usage
// required for IE, otherwise Content-Disposition may be ignored
if (ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
// ********************************************************************************************************
// Send file for 'download' for the user
// ********************************************************************************************************
ini_set('upload_max_filesize', '1000M');
ini_set('post_max_size', '1000M');
ini_set('memory_limit', '1000M' ); // this one for sure! I don't know what number to put at. At least over 300M
ini_set('max_input_time', 3000);
ini_set('max_execution_time', 3000);
if ($stream = fopen($file_real, 'rb')){
while (!feof($stream) && connection_status() == 0){
//reset time limit for big files
set_time_limit(0); // The maximum execution time in seconds. If set to zero, no time limit is imposed.
//print(fread($stream, 1024*8));
print(fread($stream, 1*(1024*1024)));
flush();
}
fclose($stream);
}
if (file_exists($file_real)) {
unlink($file_real); // delete zip file
}
ob_end_flush(); // Flush (send) the output buffer and turn off output buffering.
}
else {
// Requested file does not exist (File not found)
echo(translate('Requested audio file does not exist.', $st, 'sys'));
die();
}
// Completed: 187.148.228.80 - - [03/Oct/2014:11:23:20 -0600] "GET /data/ngu/PDF/47-1COngu-web.pdf HTTP/1.1" 206 33125 "http://www.scriptureearth.org/data/ngu/PDF/47-1COngu-web.pdf"
$data = 'Completed: ' . $_SERVER['REMOTE_ADDR'] . ' - - [' . date("d/M/Y:h:i:s") . ' -0500] "GET data/' . $iso . '/audio/zipped.zip HTTP/1.1" 200 1024 "https://www.scriptureearth.org/data/' . $iso . '/audio/zipped.zip" "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36"' . $CRLF;
file_put_contents($SE_LogPath . "zip-access.log", $data, FILE_USE_INCLUDE_PATH | FILE_APPEND | LOCK_EX);
}
}
?>