1 | <?php |
||
2 | /** |
||
3 | * COPS (Calibre OPDS PHP Server) class file |
||
4 | * |
||
5 | * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) |
||
6 | * @author Sébastien Lucas <[email protected]> |
||
7 | */ |
||
8 | |||
9 | require 'config.php'; |
||
10 | require 'base.php'; |
||
11 | |||
12 | function getComponentContent($book, $component, $add) |
||
13 | { |
||
14 | 7 | $data = $book->component($component); |
|
15 | |||
16 | $callback = function($m) use ($book, $component, $add) { |
||
17 | 7 | $method = $m[1]; |
|
18 | 7 | $path = $m[2]; |
|
19 | 7 | $end = ''; |
|
20 | 7 | if (preg_match('/^src\s*:/', $method)) { |
|
21 | 1 | $end = ')'; |
|
22 | } |
||
23 | 7 | if (preg_match('/^#/', $path)) { |
|
24 | 3 | return $method . "'" . $path . "'" . $end; |
|
25 | } |
||
26 | 7 | $hash = ''; |
|
27 | 7 | if (preg_match('/^(.+)#(.+)$/', $path, $matches)) { |
|
28 | 3 | $path = $matches[1]; |
|
29 | 3 | $hash = '#' . $matches[2]; |
|
30 | } |
||
31 | 7 | $comp = $book->getComponentName($component, $path); |
|
32 | 7 | if (!$comp) { |
|
33 | return $method . "'#'" . $end; |
||
34 | } |
||
35 | 7 | $out = $method . "'epubfs.php?" . $add . 'comp=' . $comp . $hash . "'" . $end; |
|
36 | 7 | if ($end) { |
|
37 | 1 | return $out; |
|
38 | } |
||
39 | 7 | return str_replace('&', '&', $out); |
|
40 | 7 | }; |
|
41 | |||
42 | 7 | $data = preg_replace_callback("/(src=)[\"']([^:]*?)[\"']/", $callback, $data); |
|
43 | 7 | $data = preg_replace_callback("/(href=)[\"']([^:]*?)[\"']/", $callback, $data); |
|
44 | 7 | $data = preg_replace_callback("/(\@import\s+)[\"'](.*?)[\"'];/", $callback, $data); |
|
45 | 7 | $data = preg_replace_callback('/(src\s*:\s*url\()(.*?)\)/', $callback, $data); |
|
46 | |||
47 | 7 | return $data; |
|
48 | } |
||
49 | |||
50 | if (php_sapi_name() === 'cli') { |
||
51 | return; |
||
52 | } |
||
53 | |||
54 | $idData = getURLParam('data', NULL); |
||
55 | $add = 'data=' . $idData . '&'; |
||
56 | if (!is_null(GetUrlParam(DB))) { |
||
57 | $add .= DB . '=' . GetUrlParam(DB) . '&'; |
||
58 | } |
||
59 | $myBook = Book::getBookByDataId($idData); |
||
60 | |||
61 | $book = new EPub($myBook->getFilePath('EPUB', $idData)); |
||
62 | |||
63 | $book->initSpineComponent(); |
||
64 | |||
65 | if (!isset($_GET['comp'])) { |
||
66 | notFound(); |
||
67 | return; |
||
68 | } |
||
69 | |||
70 | $component = $_GET['comp']; |
||
71 | |||
72 | try { |
||
73 | $data = getComponentContent($book, $component, $add); |
||
74 | |||
75 | $expires = 60*60*24*14; |
||
76 | header('Pragma: public'); |
||
77 | header('Cache-Control: maxage='.$expires); |
||
78 | header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT'); |
||
79 | header ('Content-Type: ' . $book->componentContentType($component)); |
||
80 | echo $data; |
||
81 | } catch (Exception $e) { |
||
82 | error_log($e); |
||
83 | notFound(); |
||
84 | } |
||
85 |