3  * COPS (Calibre OPDS PHP Server) 
 
   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>
 
   9     require_once ("config.php");
 
  10     require_once ("book.php");
 
  11     require_once ("data.php");
 
  13 function mb_basename($filepath, $suffix = NULL) {
 
  14     $splited = preg_split ( '/\//', rtrim ( $filepath, '/ ' ) );
 
  15         return substr ( basename ( 'X' . $splited [count ( $splited ) - 1], 
 
  19 function notFound () {
 
  20     header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
 
  21     header("Status: 404 Not Found");
 
  23     $_SERVER['REDIRECT_STATUS'] = 404;
 
  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);
 
  35     if (is_null ($bookId))
 
  37         $book = Book::getBookByDataId($idData);
 
  41         $book = Book::getBookById($bookId);
 
  50     if ($book && ($type == "jpg" || empty ($config['calibre_internal_directory']))) {
 
  52             $file = $book->getFilePath ($type);
 
  54             $file = $book->getFilePath ($type, $idData);
 
  56         if (!$file || !file_exists ($file)) {
 
  63             header("Content-Type: image/jpeg");
 
  64             if (isset($_GET["width"]))
 
  66                 $file = $book->getFilePath ($type);
 
  68                 if($size = GetImageSize($file)){
 
  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);
 
  89             if (isset($_GET["height"]))
 
  91                 $file = $book->getFilePath ($type);
 
  93                 if($size = GetImageSize($file)){
 
  97                     $nh = $_GET["height"];
 
 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);
 
 116     $file = $book->getFilePath ($type, $idData, false);
 
 118     $filename = $dir . $file;
 
 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 . '"');
 
 131         header("Content-Type: " . Data::$mimetypes[$type]);
 
 132         header('Content-Disposition: attachment; filename="' . mb_basename ($file) . '"');
 
 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']);
 
 141           $fp = fopen($filename, 'rb');
 
 142           header("Content-Length: " . filesize($filename));
 
 147         header ($config['cops_x_accel_redirect'] . ": " . $dir . $file);