forked from ScriptureEarth/ScriptureEarth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path00-SaveAsVideoZip.php
More file actions
221 lines (188 loc) · 9.7 KB
/
00-SaveAsVideoZip.php
File metadata and controls
221 lines (188 loc) · 9.7 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
<?php
// The html tags must not above and below this php section because the server displays the document and not saves the document!
// Usage: 00-SaveAsVideoZip.php"+"?st="+st+"&iso="+iso+"&saveAsVideo="+saveAsVideo
include ('./translate/functions.php');
$st = 'eng';
if (isset($_GET['st'])) {
$st = $_GET['st'];
$st = preg_replace('/^([a-z]{3})/', '$1', $st);
if ($st == NULL) {
die('‘st’ is empty.</body></html>');
}
}
if (isset($_GET['iso'])) {
$iso = $_GET['iso'];
$iso = preg_replace('/^([a-z]{3})/', '$1', $iso);
if ($iso == NULL) {
die('‘ISO’ ' . translate('is empty', $st, 'sys') . '.</body></html>');
}
}
else
die(translate('No ISO was found.', $st, 'sys'));
if (isset($_GET['saveAsVideo'])) {
$saveAsVideo = $_GET['saveAsVideo'];
//$iso = preg_replace('/^([a-z]{3})/', '$1', $iso);
if ($saveAsVideo == NULL) {
die('‘saveAsVideo’ ' . translate('is empty', $st, 'sys') . '.</body></html>');
}
}
else
die(translate('No saveAsVideo was found.', $st, 'sys'));
$dirname = './data/'.$iso.'/video/';
// insert the new zip file here = $file
$SE_LogPath = "";
$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 only 1 in the animation video. "GET /data/ngu/video/zipped.zip HTTP/1.1" 206 33125 "http://www.scriptureearth.org/data/ngu/video/zipped.zip"
$data = $_SERVER['REMOTE_ADDR'] . ' - - [' . date("d/M/Y:h:i:s") . ' -0500] User has only 1 mp4 file in the animation video. "GET data/' . $iso . '/video/zipped.zip HTTP/1.1" 206 1024 "data/' . $iso . '/video/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);
$dir = opendir($dirname) or die("There is no video folder under ‘$iso’.</body></html>");
if (file_exists($saveAsVideo)) {
$file = $saveAsVideo;
}
else {
die('Error! ‘saveAsVideo’ is not found!</body></html>');
}
/*********************************************************************************************************
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);
}
}
}
// Create the object
$zip = new ZipArchive();
// Creating zip file.
$Zip_Filename = str_replace(' ', '', microtime());
$Zip_Filename = str_replace('.', '', $Zip_Filename);
$Zip_Filename = $iso.'_Video_'.$Zip_Filename.'.zip';
$archive_file_name = 'zipfiles/' . $Zip_Filename;
//echo "zipfiles/" . $Zip_Filename."<br />";
// create the file and throw the error if unsuccessful
if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE) !== TRUE) {
echo (translate('Cannot open', $st, 'sys')." $archive_file_name.</body></html>");
die();
}
/*
None of the 'echo' work because of the header(...)!!!!!
*/
// ********************************************************************************************************
// add the file to the zip file
// ********************************************************************************************************
$zip->addFile($file, basename($file));
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();
//echo "The zip file is clossed.<br />";
//flush();
$file = $Zip_Filename;
$file_real = './zipfiles/' . $file;
$ip = $_SERVER['REMOTE_ADDR'];
// Check to see if the download script was called
if (basename($_SERVER['PHP_SELF']) == '00-SaveAsVideoZip.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
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);
}
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 . '/video/zipped.zip HTTP/1.1" 200 1024 "data/' . $iso . '/video/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);
}
}
?>