-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtorrent_cache.php
71 lines (61 loc) · 2.63 KB
/
torrent_cache.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
/**
* The torrent cache and generator script takes care of dynamically generating .torrents
* when they are requested by clients.
* To optimize performance .torrent-objects are cached in the cache directory so that
* on any second request they can be directly sent without re-calculating the SHA1 strings.
*
* A file is requested by torrent_cache.php?file=/some_file.filetype
*
* Symlinks outside the $fileDirectory are not allowed. Either do hardlinking or make the
* entire $fileDirectory a symlink somewhere.
*
* The optional webseeds must be HTTP servers with a similar structure to $fileDirectory
* For instance if you have a webseed called "http://burp.boinc.dk/download/" and a file
* $fileDirectory/dir/file
* then the webseed musst respond to queries on http://burp.boinc.dk/download/dir/file.
*
* Please see config.php for the setting of these variables.
*/
require_once("config.php");
function isSubDir($possibleSubDir, $parent){
$realPossible = realpath($possibleSubDir);
$realParent = realpath($parent);
return (substr($realPossible, 0, strlen($realParent)) == $realParent);
}
// Get the file request and do some checkups
$file = $_GET["file"];
if (!$file) throw new IllegalArgumentException("No file specified");
if (strpos(urldecode($file), "..")!==false) throw new IllegalArgumentException("Cannot use '..' in path");
// See if we've got the file
while (!$fileModTime){
if (($fileModTime = @filemtime($fileDirectory.$file)) === false){
$pos = strpos($file, "/", 1);
if ($pos === false){
throw new IllegalArgumentException("File does not exist");
} else {
$file = substr($file, $pos);
}
}
}
$file = $fileDirectory.$file;
if (!$fileFilter->isValid($file)) throw new IllegalArgumentException("File was not accepted by the server for tracking.");
// Everything's fine let's lookup the .torrent in the cache if needed:
$cache_args = "file=".$file."&modtime=".$fileModTime;
$cacheddata=get_cached_data(TORRENT_CACHE_TTL,$cache_args);
if ($cacheddata){ //If we have got the data in cache
$torrent = unserialize($cacheddata); // use the cached data
} else { //if not do queries etc to generate new data
for ($i=0; $i<sizeof($webseeds);$i++){
$realWebseeds[] = $webseeds[$i].substr($file, strlen($fileDirectory));
}
$torrent = new Torrent($file, $trackerURL, $realWebseeds);
$torrent->ensureSHA1Loaded();
db_init();
$torrent->register();
set_cache_data(serialize($torrent),$cache_args); //save data in cache
};
header("Content-type: application/x-bittorrent");
header("Content-Disposition: attachment; filename=\"".basename($file).".torrent\"");
echo $torrent->toEncoded();
?>