1) Исправления в связи со сменой API MySQL
[openlib.git] / www / fetch.php
1 <?php
2 /**
3  * COPS (Calibre OPDS PHP Server) 
4  *
5  * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6  * @author     Gordon Page <gordon@incero.com> with integration/modification by Sébastien Lucas <sebastien@slucas.fr>
7  */
8     
9     require_once ("config.php");
10     require_once ("book.php");
11     require_once ("data.php");
12
13 function mb_basename($filepath, $suffix = NULL) {
14     $splited = preg_split ( '/\//', rtrim ( $filepath, '/ ' ) );
15         return substr ( basename ( 'X' . $splited [count ( $splited ) - 1], 
16         $suffix ), 1 );
17         }
18
19 function notFound () {
20     header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
21     header("Status: 404 Not Found");
22
23     $_SERVER['REDIRECT_STATUS'] = 404;
24 }
25     
26     global $config;
27     $expires = 60*60*24*14;
28     header("Pragma: public");
29     header("Cache-Control: maxage=".$expires);
30     header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');
31     $bookId = getURLParam ("id", NULL);
32     $type = getURLParam ("type", "jpg");
33     $idData = getURLParam ("data", NULL);
34
35     if (is_null ($bookId))
36     {
37         $book = Book::getBookByDataId($idData);
38     }
39     else
40     {
41         $book = Book::getBookById($bookId);
42     }
43
44     if (!$book) {
45         notFound ();
46         return;     
47     }
48
49
50     if ($book && ($type == "jpg" || empty ($config['calibre_internal_directory']))) {
51         if ($type == "jpg") {
52             $file = $book->getFilePath ($type);
53         } else {
54             $file = $book->getFilePath ($type, $idData);
55         }
56         if (!$file || !file_exists ($file)) {
57             notFound ();
58             return;
59         }
60     }
61
62     if ($type == "jpg") {
63             header("Content-Type: image/jpeg");
64             if (isset($_GET["width"]))
65             {
66                 $file = $book->getFilePath ($type);
67                 // get image size
68                 if($size = GetImageSize($file)){
69                     $w = $size[0];
70                     $h = $size[1];
71                     //set new size
72                     $nw = $_GET["width"];
73                     $nh = ($nw*$h)/$w;
74                 }
75                 else{
76                     //set new size
77                     $nw = "160";
78                     $nh = "120";
79                 }
80                 //draw the image
81                 $src_img = imagecreatefromjpeg($file);
82                 $dst_img = imagecreatetruecolor($nw,$nh);
83                 imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $nw, $nh, $w, $h);//resizing the image
84                 imagejpeg($dst_img,null,80);
85                 imagedestroy($src_img);
86                 imagedestroy($dst_img);
87                 return;
88             }
89             if (isset($_GET["height"]))
90             {
91                 $file = $book->getFilePath ($type);
92                 // get image size
93                 if($size = GetImageSize($file)){
94                     $w = $size[0];
95                     $h = $size[1];
96                     //set new size
97                     $nh = $_GET["height"];
98                     $nw = ($nh*$w)/$h;
99                 }
100                 else{
101                     //set new size
102                     $nw = "160";
103                     $nh = "120";
104                 }
105                 //draw the image
106                 $src_img = imagecreatefromjpeg($file);
107                 $dst_img = imagecreatetruecolor($nw,$nh);
108                 imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $nw, $nh, $w, $h);//resizing the image
109                 imagejpeg($dst_img,null,80);
110                 imagedestroy($src_img);
111                 imagedestroy($dst_img);
112                 return;
113             }
114     }
115
116     $file = $book->getFilePath ($type, $idData, false);
117
118     $filename = $dir . $file;
119
120     if ($type == "jpg") {
121         header('Content-Type: image/jpg');
122         header('Content-Disposition: filename="' . mb_basename ($file) . '"');
123 #    } elseif ($type == "fb2.zip") {
124 #        $z = new ZipArchive();
125 #        $z->open($filename);  
126 #        $zname = $z->getNameIndex(0);
127 #        $zstat = $z->statIndex(0);   
128 #        header('Content-Type: text/fb2+xml');
129 #        header('Content-Disposition: filename="' . $zname . '"');
130     } else {
131         header("Content-Type: " . Data::$mimetypes[$type]);
132         header('Content-Disposition: attachment; filename="' . mb_basename ($file) . '"');
133     }
134     
135     if (empty ($config['cops_x_accel_redirect'])) {
136         $filename = $dir . $file;
137 #        if ($type == "fb2.zip") {
138 #          $fp = $z->getStream($zname);
139 #          header("Content-Length: " . $zstat['size']);
140 #        } else {
141           $fp = fopen($filename, 'rb');
142           header("Content-Length: " . filesize($filename));
143 #         }
144         fpassthru($fp);
145     }
146     else {
147         header ($config['cops_x_accel_redirect'] . ": " . $dir . $file);
148     }