1) Исправления в связи со сменой API MySQL
[openlib.git] / www / fetch.php
diff --git a/www/fetch.php b/www/fetch.php
new file mode 100644 (file)
index 0000000..4b51f87
--- /dev/null
@@ -0,0 +1,148 @@
+<?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>
+ */
+    
+    require_once ("config.php");
+    require_once ("book.php");
+    require_once ("data.php");
+
+function mb_basename($filepath, $suffix = NULL) {
+    $splited = preg_split ( '/\//', rtrim ( $filepath, '/ ' ) );
+        return substr ( basename ( 'X' . $splited [count ( $splited ) - 1], 
+        $suffix ), 1 );
+        }
+
+function notFound () {
+    header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
+    header("Status: 404 Not Found");
+
+    $_SERVER['REDIRECT_STATUS'] = 404;
+}
+    
+    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');
+    $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);
+    }
+
+    if (!$book) {
+        notFound ();
+        return;     
+    }
+
+
+    if ($book && ($type == "jpg" || empty ($config['calibre_internal_directory']))) {
+        if ($type == "jpg") {
+            $file = $book->getFilePath ($type);
+        } else {
+            $file = $book->getFilePath ($type, $idData);
+        }
+        if (!$file || !file_exists ($file)) {
+            notFound ();
+            return;
+        }
+    }
+
+    if ($type == "jpg") {
+            header("Content-Type: image/jpeg");
+            if (isset($_GET["width"]))
+            {
+                $file = $book->getFilePath ($type);
+                // get image size
+                if($size = GetImageSize($file)){
+                    $w = $size[0];
+                    $h = $size[1];
+                    //set new size
+                    $nw = $_GET["width"];
+                    $nh = ($nw*$h)/$w;
+                }
+                else{
+                    //set new size
+                    $nw = "160";
+                    $nh = "120";
+                }
+                //draw the image
+                $src_img = imagecreatefromjpeg($file);
+                $dst_img = imagecreatetruecolor($nw,$nh);
+                imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $nw, $nh, $w, $h);//resizing the image
+                imagejpeg($dst_img,null,80);
+                imagedestroy($src_img);
+                imagedestroy($dst_img);
+                return;
+            }
+            if (isset($_GET["height"]))
+            {
+                $file = $book->getFilePath ($type);
+                // get image size
+                if($size = GetImageSize($file)){
+                    $w = $size[0];
+                    $h = $size[1];
+                    //set new size
+                    $nh = $_GET["height"];
+                    $nw = ($nh*$w)/$h;
+                }
+                else{
+                    //set new size
+                    $nw = "160";
+                    $nh = "120";
+                }
+                //draw the image
+                $src_img = imagecreatefromjpeg($file);
+                $dst_img = imagecreatetruecolor($nw,$nh);
+                imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $nw, $nh, $w, $h);//resizing the image
+                imagejpeg($dst_img,null,80);
+                imagedestroy($src_img);
+                imagedestroy($dst_img);
+                return;
+            }
+    }
+
+    $file = $book->getFilePath ($type, $idData, false);
+
+    $filename = $dir . $file;
+
+    if ($type == "jpg") {
+        header('Content-Type: image/jpg');
+        header('Content-Disposition: filename="' . mb_basename ($file) . '"');
+#    } elseif ($type == "fb2.zip") {
+#        $z = new ZipArchive();
+#        $z->open($filename);  
+#        $zname = $z->getNameIndex(0);
+#        $zstat = $z->statIndex(0);   
+#        header('Content-Type: text/fb2+xml');
+#        header('Content-Disposition: filename="' . $zname . '"');
+    } else {
+        header("Content-Type: " . Data::$mimetypes[$type]);
+        header('Content-Disposition: attachment; filename="' . mb_basename ($file) . '"');
+    }
+    
+    if (empty ($config['cops_x_accel_redirect'])) {
+        $filename = $dir . $file;
+#        if ($type == "fb2.zip") {
+#          $fp = $z->getStream($zname);
+#          header("Content-Length: " . $zstat['size']);
+#        } else {
+          $fp = fopen($filename, 'rb');
+          header("Content-Length: " . filesize($filename));
+#         }
+        fpassthru($fp);
+    }
+    else {
+        header ($config['cops_x_accel_redirect'] . ": " . $dir . $file);
+    }