These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * COPS (Calibre OPDS PHP Server) |
||
4 | * |
||
5 | * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) |
||
6 | * @author S�bastien Lucas <[email protected]> |
||
7 | */ |
||
8 | |||
9 | require_once ("config.php"); |
||
10 | require_once ("book.php"); |
||
11 | require_once ("data.php"); |
||
12 | |||
13 | global $config; |
||
14 | |||
15 | if ($config ['cops_fetch_protect'] == "1") { |
||
16 | session_start(); |
||
17 | if (!isset($_SESSION['connected'])) { |
||
18 | notFound (); |
||
19 | return; |
||
20 | } |
||
21 | } |
||
22 | |||
23 | $expires = 60*60*24*14; |
||
24 | header("Pragma: public"); |
||
25 | header("Cache-Control: maxage=".$expires); |
||
26 | header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT'); |
||
27 | $bookId = getURLParam ("id", NULL); |
||
28 | $type = getURLParam ("type", "jpg"); |
||
29 | $idData = getURLParam ("data", NULL); |
||
30 | if (is_null ($bookId)) |
||
31 | { |
||
32 | $book = Book::getBookByDataId($idData); |
||
33 | } |
||
34 | else |
||
35 | { |
||
36 | $book = Book::getBookById($bookId); |
||
37 | } |
||
38 | |||
39 | if (!$book) { |
||
40 | notFound (); |
||
41 | return; |
||
42 | } |
||
43 | |||
44 | if ($book && ($type == "jpg" || empty ($config['calibre_internal_directory']))) { |
||
45 | if ($type == "jpg") { |
||
46 | $file = $book->getFilePath ($type); |
||
47 | } else { |
||
48 | $file = $book->getFilePath ($type, $idData); |
||
49 | } |
||
50 | if (!$file || !file_exists ($file)) { |
||
0 ignored issues
–
show
|
|||
51 | notFound (); |
||
52 | return; |
||
53 | } |
||
54 | } |
||
55 | |||
56 | switch ($type) |
||
57 | { |
||
58 | case "jpg": |
||
59 | header("Content-Type: image/jpeg"); |
||
60 | //by default, we don't cache |
||
61 | $thumbnailCacheFullpath = null; |
||
62 | if ( isset($config['cops_thumbnail_cache_directory']) && $config['cops_thumbnail_cache_directory'] !== '' ) { |
||
63 | $thumbnailCacheFullpath = $config['cops_thumbnail_cache_directory']; |
||
64 | //if multiple databases, add a subfolder with the database ID |
||
65 | $thumbnailCacheFullpath .= !is_null (GetUrlParam (DB)) ? 'db-' . GetUrlParam (DB) . DIRECTORY_SEPARATOR : ''; |
||
66 | //when there are lots of thumbnails, it's better to save files in subfolders, so if the book's uuid is |
||
67 | //"01234567-89ab-cdef-0123-456789abcdef", we will save the thumbnail in .../0/12/34567-89ab-cdef-0123-456789abcdef-... |
||
68 | $thumbnailCacheFullpath .= substr($book->uuid, 0, 1) . DIRECTORY_SEPARATOR . substr($book->uuid, 1, 2) . DIRECTORY_SEPARATOR; |
||
69 | //check if cache folder exists or create it |
||
70 | if ( file_exists($thumbnailCacheFullpath) || mkdir($thumbnailCacheFullpath, 0700, true) ) { |
||
71 | //we name the thumbnail from the book's uuid and it's dimensions (width and/or height) |
||
72 | $thumbnailCacheName = substr($book->uuid, 3) . '-' . getURLParam ("width") . 'x' . getURLParam ("height") . '.jpg'; |
||
73 | $thumbnailCacheFullpath = $thumbnailCacheFullpath . $thumbnailCacheName; |
||
74 | } |
||
75 | else { |
||
76 | //error creating the folder, so we don't cache |
||
77 | $thumbnailCacheFullpath = null; |
||
78 | } |
||
79 | } |
||
80 | |||
81 | if ( $thumbnailCacheFullpath !== null && file_exists($thumbnailCacheFullpath) ) { |
||
82 | //return the already cached thumbnail |
||
83 | readfile( $thumbnailCacheFullpath ); |
||
84 | return; |
||
85 | } |
||
86 | |||
87 | if ($book->getThumbnail (getURLParam ("width"), getURLParam ("height"), $thumbnailCacheFullpath)) { |
||
88 | //if we don't cache the thumbnail, imagejpeg() in $book->getThumbnail() already return the image data |
||
89 | if ( $thumbnailCacheFullpath === null ) { |
||
90 | // The cover had to be resized |
||
91 | return; |
||
92 | } |
||
93 | else { |
||
94 | //return the just cached thumbnail |
||
95 | readfile( $thumbnailCacheFullpath ); |
||
96 | return; |
||
97 | } |
||
98 | } |
||
99 | break; |
||
100 | default: |
||
101 | $data = $book->getDataById ($idData); |
||
102 | header("Content-Type: " . $data->getMimeType ()); |
||
103 | break; |
||
104 | } |
||
105 | $file = $book->getFilePath ($type, $idData, true); |
||
106 | if ($type == "epub" && $config['cops_update_epub-metadata']) |
||
107 | { |
||
108 | $book->getUpdatedEpub ($idData); |
||
109 | return; |
||
110 | } |
||
111 | if ($type == "jpg") { |
||
112 | header('Content-Disposition: filename="' . basename ($file) . '"'); |
||
113 | } else { |
||
114 | header('Content-Disposition: attachment; filename="' . basename ($file) . '"'); |
||
115 | } |
||
116 | |||
117 | $dir = $config['calibre_internal_directory']; |
||
118 | if (empty ($config['calibre_internal_directory'])) { |
||
119 | $dir = Base::getDbDirectory (); |
||
120 | } |
||
121 | |||
122 | if (empty ($config['cops_x_accel_redirect'])) { |
||
123 | $filename = $dir . $file; |
||
124 | $fp = fopen($filename, 'rb'); |
||
125 | header("Content-Length: " . filesize($filename)); |
||
126 | fpassthru($fp); |
||
127 | } |
||
128 | else { |
||
129 | header ($config['cops_x_accel_redirect'] . ": " . $dir . $file); |
||
130 | } |
||
131 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: