-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapi.php
71 lines (59 loc) · 1.48 KB
/
api.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
<?php
/*
* API Example
*/
// Perform some form of security validation.
// EG., You could check their PHP session is logged in.
$granted = true; // Bypass validation.
if (!$granted) {
header ("HTTP/1.1 404 Not Found");
die();
}
// Get filename passed via URL
$filename = $_GET['f'];
// Usually this would be hidden above the web server root. EG., '/hidden/media/'
$base = 'media/';
// Whitelist filename characters. Edit if your filenames require other characters.
// Never include the '/' character.
// ie., Learn from the link below, if you think it is already in there, because it's not!
// http://php.net/manual/en/reference.pcre.pattern.syntax.php
$ok = preg_match('/^[-A-Za-z0-9_.]+$/', $filename);
// Ignore requests that smell funny
if (!$ok) {
header ("HTTP/1.1 404 Not Found");
die();
}
$file = explode('.', $filename);
// You could check they have rights to access the media here.
// - No checking in this example.
// Get the type by file extension
switch ($file[count($file)-1]) {
case 'mp3':
$type = 'audio/mpeg';
break;
case 'ogg':
$type = 'audio/ogg';
break;
case 'oga':
$type = 'audio/ogg';
break;
case 'm4a':
$type = 'audio/mp4';
break;
case 'm4v':
$type = 'video/mp4';
break;
case 'mp4':
$type = 'video/mp4';
break;
case 'webm':
$type = 'video/webm';
break;
case 'ogv':
$type = 'video/ogg';
break;
}
$path = $base . $filename;
// Include and call smartReadFile
require_once('smartReadFile.php');
smartReadFile($path, $filename, $type);