forked from oohoo/moodle-block_tts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.php
171 lines (146 loc) · 4.75 KB
/
file.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
<?php
/**
* *************************************************************************
* * OOHOO - TTS - Text To Speech **
* *************************************************************************
* @package block **
* @subpackage TTS **
* @name TTS **
* @copyright oohoo.biz **
* @link http://oohoo.biz **
* @author Ryan Thomas (Original Author) **
* @author Dustin Durand **
* @author Nicolas Bretin **
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later **
* *************************************************************************
* ************************************************************************ */
require_once('../../config.php');
require_login();
$relativepath = get_file_argument('file.php');
// relative path must start with '/', because of backup/restore!!!
if (!$relativepath)
{
error('No valid arguments supplied or incorrect server configuration');
}
else if ($relativepath{0} != '/')
{
error('No valid arguments supplied, path does not start with slash!');
}
$directories = explode('/', $relativepath);
//print_object($directories);
//Quick Security Checks
$clean = true;
foreach ($directories as $directory)
{
$pos = strpos($directory, '..');
if ($pos !== false)
{
$clean = false;
}
}
//%dir%---/sound_cache/%course%/mp3_tts/google/en/11fb1475647d5679733d89acea1632fc.mp3"
//Security Checks - we are going to use some basic security to try to keep this file
//from being abused. Its far from the fastest script, but this helps
$file = $directories[count($directories) - 1];
$filename = explode('.', $file);
$extention = $filename[count($filename) - 1];
//print $extention;
if ($directories[1] != 'sound_cache')
{
$clean = false;
}
elseif ($directories[3] != 'mp3_tts')
{
$clean = false;
}
elseif (!$DB->record_exists('course', array('id' => $directories[2])))
{
$clean = false;
}
elseif ($extention != 'mp3' && $extention != 'wav')
{
$clean = false;
}
$filePath = $CFG->dataroot . $relativepath;
// check that file exists
if (!$clean || !file_exists($filePath))
{
not_found();
}
session_write_close(); // unlock session during fileserving
send_file($filePath, $file, $extention);
/**
* Send a header 404 page not found to the user
*/
function not_found()
{
header('HTTP/1.0 404 not found');
}
/**
* Read file
* @param string $filename The string file name
* @param boolean $retbytes
* @return boolean
*/
function readfile_chunked($filename, $retbytes = true)
{
$chunksize = 1 * (1024 * 1024); // 1MB chunks - must be less than 2MB!
$buffer = '';
$cnt = 0;
$handle = fopen($filename, 'rb');
if ($handle === false)
{
return false;
}
while (!feof($handle))
{
@set_time_limit(60 * 60); //reset time limit to 60 min - should be enough for 1 MB chunk
$buffer = fread($handle, $chunksize);
echo $buffer;
flush();
if ($retbytes)
{
$cnt += strlen($buffer);
}
}
$status = fclose($handle);
if ($retbytes && $status)
{
return $cnt; // return num. bytes delivered like readfile() does.
}
return $status;
}
/**
* Send file to download to the user
* @global stdClass $CFG
* @global stdClass $COURSE
* @global stdClass $SESSION
* @param string $path The path of the file
* @param string $filename The file name
* @param string $extension The file extension
*/
function send_file($path, $filename, $extension)
{
global $CFG, $COURSE, $SESSION;
//print $path . " " . $filename . " " . $extension;exit();
$filesize = filesize($path);
//IE compatibiltiy HACK!
if (ini_get('zlib.output_compression'))
{
ini_set('zlib.output_compression', 'Off');
}
//try to disable automatic sid rewrite in cookieless mode
@ini_set("session.use_trans_sid", "false");
@header('Content-Disposition: inline; filename="' . $filename . '"');
$lifetime = $lifetime = 86400;
@header('Cache-Control: max-age=' . $lifetime);
@header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $lifetime) . ' GMT');
@header('Pragma: ');
// Just send it out raw
@header('Content-Length: ' . $filesize);
@header('Content-Type: ' . $mimetype);
while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite
readfile_chunked($path);
die; //no more chars to output!!!
}
?>