-
Notifications
You must be signed in to change notification settings - Fork 18
/
cdr_play.php
104 lines (100 loc) · 2.97 KB
/
cdr_play.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
<?php
if (!defined('FREEPBX_IS_AUTH')) { die('No direct script access allowed'); }
include_once("crypt.php");
$crypt = new Crypt();
$REC_CRYPT_PASSWORD = (isset($amp_conf['AMPPLAYKEY']) && trim((string) $amp_conf['AMPPLAYKEY']) != "")?trim((string) $amp_conf['AMPPLAYKEY']):'TheWindCriesMary';
$path = $crypt->decrypt($_REQUEST['recordingpath'],$REC_CRYPT_PASSWORD);
if(!empty($path)) {
$extension = pathinfo((string) $path,PATHINFO_EXTENSION);
// This will set the Content-Type to the appropriate setting for the file
$ctype ='';
switch( $extension ) {
case "WAV":
case "wav":
$ctype="audio/x-wav";
break;
case "ulaw":
$ctype="audio/basic";
case "alaw":
$ctype="audio/x-alaw-basic";
case "sln":
$ctype="audio/x-wav";
case "gsm":
$ctype="audio/x-gsm";
case "g729":
$ctype="audio/x-g729";
//Need to convert these to a supported HTML5 format..
header("HTTP/1.0 404 Not Found");
die();
default: //not downloadable
header("HTTP/1.0 404 Not Found");
die();
break ;
}
// Gather relevent info about file
$size = filesize($path);
$name = basename((string) $path);
$length = $size; // Content length
$start = 0; // Start byte
$end = $size - 1; // End byte
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header('Content-Description: File Transfer');
header("Content-Transfer-Encoding: binary");
header('Content-Type: '.$ctype);
header("Accept-Ranges: 0-".$end);
if (isset($_SERVER['HTTP_RANGE'])) {
$c_start = $start;
$c_end = $end;
[, $range] = explode('=', (string) $_SERVER['HTTP_RANGE'], 2);
if (str_contains($range, ',')) {
header('HTTP/1.1 416 Requested Range Not Satisfiable');
header("Content-Range: bytes $start-$end/$size");
exit;
}
if ($range == '-') {
$c_start = $size - substr($range, 1);
}else{
$range = explode('-', $range);
$c_start = $range[0];
$c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $size;
}
$c_end = ($c_end > $end) ? $end : $c_end;
if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) {
header('HTTP/1.1 416 Requested Range Not Satisfiable');
header("Content-Range: bytes $start-$end/$size");
exit;
}
$start = $c_start;
$end = $c_end;
$length = $end - $start + 1;
header('HTTP/1.1 206 Partial Content');
} else {
header("HTTP/1.1 200 OK");
}
header("Content-Range: bytes $start-$end/$size");
header('Content-length: ' . $size);
header('Content-Disposition: attachment;filename="' . $name.'"');
$buffer = 1024 * 8;
$wstart = $start;
ob_end_clean();
ob_start();
set_time_limit(0);
while(true) {
$fp = fopen($path, "rb");
fseek($fp, $wstart);
if(!feof($fp) && ($p = ftell($fp)) <= $end) {
if ($p + $buffer > $end) {
$buffer = $end - $p + 1;
}
$contents = fread($fp, $buffer);
fclose($fp);
echo $contents;
ob_flush();
flush();
} else {
break;
}
$wstart = $wstart + $buffer;
}
}