-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetch.php
196 lines (144 loc) · 5.83 KB
/
fetch.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
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
<?php
/**
* COPS (Calibre OPDS PHP Server)
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Gordon Page <gordon@incero.com> with integration/modification by Sébastien Lucas <sebastien@slucas.fr>
*/
/**
* Get filename for the book
* @param Book book
* @return string translitated filename like Tom_Hanna_Somatics.123.fb2
*/
function getBookFileName($book) {
$tr = array(
"Ґ"=>"G","Ё"=>"YO","Є"=>"E","Ї"=>"YI","І"=>"I",
"і"=>"i","ґ"=>"g","ё"=>"yo","№"=>"#","є"=>"e",
"ї"=>"yi","А"=>"A","Б"=>"B","В"=>"V","Г"=>"G",
"Д"=>"D","Е"=>"E","Ж"=>"ZH","З"=>"Z","И"=>"I",
"Й"=>"Y","К"=>"K","Л"=>"L","М"=>"M","Н"=>"N",
"О"=>"O","П"=>"P","Р"=>"R","С"=>"S","Т"=>"T",
"У"=>"U","Ф"=>"F","Х"=>"H","Ц"=>"TS","Ч"=>"CH",
"Ш"=>"SH","Щ"=>"SCH","Ъ"=>"'","Ы"=>"YI","Ь"=>"",
"Э"=>"E","Ю"=>"YU","Я"=>"YA","а"=>"a","б"=>"b",
"в"=>"v","г"=>"g","д"=>"d","е"=>"e","ж"=>"zh",
"з"=>"z","и"=>"i","й"=>"y","к"=>"k","л"=>"l",
"м"=>"m","н"=>"n","о"=>"o","п"=>"p","р"=>"r",
"с"=>"s","т"=>"t","у"=>"u","ф"=>"f","х"=>"h",
"ц"=>"ts","ч"=>"ch","ш"=>"sh","щ"=>"sch","ъ"=>"'",
"ы"=>"yi","ь"=>"","э"=>"e","ю"=>"yu","я"=>"ya",
" "=>"_", ',' => '', '.' => '', '!' => '', '"' => ''
);
$author = Author::getAuthorByBookId($book->id);
$author = $author[0];
$serie = Serie::getSerieByBookId($book->id);
$filename = array();
//get lastname
$names = array_reverse( explode(' ', $author->name) );
$filename[] = strtr($names[0],$tr);
if ($serie) {
$filename[] = strtr($serie->name,$tr);
}
$filename[] = strtr($book->title,$tr);
//var_dump($book, $autor, $serie);
return strtolower( implode('_', $filename) ).'.'.$book->fileType.'.zip';
}
/** send file to browser
* @param string $filename full path
* @param string $type mime type. for zipped - 'ZIP'
*/
function sendFile($filename, $type = 'zip') {
global $config;
header("Location: ".dirname($_SERVER['SCRIPT_NAME']).DS.$config['flibusta_cache'].DS.$filename);
//global $config;
//
//$expires = 60*60*24*14;
//
//header("Pragma: public");
//header("Cache-Control: maxage=".$expires);
//header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');
//
//header("Content-Type: " . Data::$mimetypes[$type]);
//
//if ($type == "jpg") {
// header('Content-Disposition: filename="' . basename ($filename) . '"');
//} else {
// header('Content-Disposition: attachment; filename="' . basename ($filename) . '"');
//}
}
define ('DS', DIRECTORY_SEPARATOR);
require_once ("config.php");
require_once ("book.php");
require_once ("data.php");
global $config;
$bookId = getURLParam ("id", NULL);
$type = getURLParam ("type", "jpg");
$idData = getURLParam ("data", NULL);
if (is_null ($bookId))
{
$book = Book::getBookByDataId($idData);
}
else
{
$book = Book::getBookById($bookId);
}
$cache_dir = dirname(__FILE__).DS.$config['flibusta_cache']; //relative to current folder
//extracted files will be stored as zipped
$filename = getBookFileName($book);
//$book->id.'.'.$book->fileType.'.zip';
if (! file_exists( $cache_dir ) ) {
if ( mkdir($cache_dir, 0777) )
echo "cache created: $cache_dir <br>";
else
echo "FAIL: unable to create cache: $cache_dir <br>";
}
//check if the file already extracted
//TODO: break folder to few folders to avoid ahving too much files in one folder
//http://test.lo/opds/cops/fetch.php?page=3&id=12732
if (file_exists($cache_dir.DS.$filename)) {
sendFile($filename);
die;
}
$list = glob($config['flibusta_folder'].DS.'*fb*.zip');
//search for large zip file which contains selected book
$matches = array();
$file = null;
foreach ($list as $line) {
preg_match_all('/(-)(?P<start>[0-9]+)-(?P<end>[0-9]+)/', $line, $matches);
//echo "<p>$line";
//echo "<p>{$matches['start'][0]}, {$book->id}, {$matches['end'][0]}";
if ( (int) $book->id >= (int) $matches['start'][0] && (int) $book->id <= (int) $matches['end'][0]) {
$file = $line;
break;
}
}
//echo "<p>{$book->id}: $file";
//die;
if (! $file ) {
echo 'cannot find bundle for file:'.$book->id.'.'.$book->fileType;
die;
}
//var_dump($book, $file, $book->id.'.'.$book->fileType);
$zip = new ZipArchive;
$res = $zip->open($file);
if ($res === TRUE) {
// extract it to the path we determined above
$zip->extractTo($cache_dir.DS, $book->id.'.'.$book->fileType );
$zip->close();
//pack it ro individual zip file
$zip = new ZipArchive;
if ($zip->open($cache_dir.DS.$filename, ZipArchive::CREATE) === TRUE) {
$zip->addFile($cache_dir.DS.$book->id.'.'.$book->fileType, $book->id.'.'.$book->fileType);
$zip->close();
//unlink($cache_dir.DS.$book->id.'.'.$book->fileType); //remove uncompressed one
//echo $cache_dir.DS.$filename;
//die;
sendFile($filename);
} else {
echo 'failed to create archive';
}
}
else {
echo 'FAIL: unable to open input file:'.$file;
}
?>