2012-05-28 08:01:33 +03:00
|
|
|
|
<?php
|
|
|
|
|
/**
|
2013-12-05 11:52:51 +02:00
|
|
|
|
* COPS (Calibre OPDS PHP Server)
|
2012-05-28 08:01:33 +03:00
|
|
|
|
*
|
|
|
|
|
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
|
|
|
|
|
* @author Gordon Page <gordon@incero.com> with integration/modification by S<EFBFBD>bastien Lucas <sebastien@slucas.fr>
|
|
|
|
|
*/
|
2013-12-05 11:52:51 +02:00
|
|
|
|
|
2012-05-28 08:01:33 +03:00
|
|
|
|
require_once ("config.php");
|
2012-05-28 08:05:05 +03:00
|
|
|
|
require_once ("book.php");
|
2012-07-01 15:24:54 +03:00
|
|
|
|
require_once ("data.php");
|
2013-07-07 11:15:49 +03:00
|
|
|
|
|
|
|
|
|
function notFound () {
|
|
|
|
|
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
|
|
|
|
|
header("Status: 404 Not Found");
|
|
|
|
|
|
|
|
|
|
$_SERVER['REDIRECT_STATUS'] = 404;
|
|
|
|
|
}
|
2013-12-05 11:52:51 +02:00
|
|
|
|
|
2013-12-10 22:34:19 +02:00
|
|
|
|
if ($config ['cops_fetch_protect'] == "1") {
|
|
|
|
|
session_start();
|
|
|
|
|
if (!isset($_SESSION['connected'])) {
|
|
|
|
|
notFound ();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-28 08:01:33 +03:00
|
|
|
|
global $config;
|
2012-05-28 08:06:12 +03:00
|
|
|
|
$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');
|
2012-06-24 09:16:47 +03:00
|
|
|
|
$bookId = getURLParam ("id", NULL);
|
2012-05-28 08:05:05 +03:00
|
|
|
|
$type = getURLParam ("type", "jpg");
|
2012-06-23 23:11:13 +03:00
|
|
|
|
$idData = getURLParam ("data", NULL);
|
2012-06-24 09:16:47 +03:00
|
|
|
|
if (is_null ($bookId))
|
|
|
|
|
{
|
|
|
|
|
$book = Book::getBookByDataId($idData);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
$book = Book::getBookById($bookId);
|
|
|
|
|
}
|
2013-12-05 11:52:51 +02:00
|
|
|
|
|
2013-07-07 11:15:49 +03:00
|
|
|
|
if (!$book) {
|
|
|
|
|
notFound ();
|
2013-12-05 11:52:51 +02:00
|
|
|
|
return;
|
2013-07-07 11:15:49 +03:00
|
|
|
|
}
|
2013-12-05 11:52:51 +02:00
|
|
|
|
|
2013-07-07 11:15:49 +03:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-12-05 11:52:51 +02:00
|
|
|
|
|
2012-05-28 08:01:33 +03:00
|
|
|
|
switch ($type)
|
|
|
|
|
{
|
|
|
|
|
case "jpg":
|
2012-12-23 15:16:54 +02:00
|
|
|
|
header("Content-Type: image/jpeg");
|
2012-05-28 08:01:33 +03:00
|
|
|
|
if (isset($_GET["width"]))
|
|
|
|
|
{
|
|
|
|
|
$file = $book->getFilePath ($type);
|
|
|
|
|
// get image size
|
2013-12-05 11:55:13 +02:00
|
|
|
|
if ($size = GetImageSize($file)) {
|
2012-05-28 08:01:33 +03:00
|
|
|
|
$w = $size[0];
|
|
|
|
|
$h = $size[1];
|
|
|
|
|
//set new size
|
|
|
|
|
$nw = $_GET["width"];
|
2013-07-05 12:12:22 +03:00
|
|
|
|
if ($nw > $w) { break; }
|
2012-05-28 08:01:33 +03:00
|
|
|
|
$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
|
2013-07-04 23:37:27 +03:00
|
|
|
|
imagejpeg($dst_img,null,80);
|
2012-05-28 08:01:33 +03:00
|
|
|
|
imagedestroy($src_img);
|
|
|
|
|
imagedestroy($dst_img);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2012-05-28 08:06:12 +03:00
|
|
|
|
if (isset($_GET["height"]))
|
|
|
|
|
{
|
|
|
|
|
$file = $book->getFilePath ($type);
|
|
|
|
|
// get image size
|
2013-12-05 11:55:13 +02:00
|
|
|
|
if ($size = GetImageSize($file)) {
|
2012-05-28 08:06:12 +03:00
|
|
|
|
$w = $size[0];
|
|
|
|
|
$h = $size[1];
|
|
|
|
|
//set new size
|
|
|
|
|
$nh = $_GET["height"];
|
2013-07-05 12:12:22 +03:00
|
|
|
|
if ($nh > $h) { break; }
|
2012-05-28 08:06:12 +03:00
|
|
|
|
$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
|
2013-07-04 23:37:27 +03:00
|
|
|
|
imagejpeg($dst_img,null,80);
|
2012-05-28 08:06:12 +03:00
|
|
|
|
imagedestroy($src_img);
|
|
|
|
|
imagedestroy($dst_img);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2012-05-28 08:01:33 +03:00
|
|
|
|
break;
|
2012-05-28 08:05:05 +03:00
|
|
|
|
default:
|
2012-12-23 15:16:54 +02:00
|
|
|
|
header("Content-Type: " . Data::$mimetypes[$type]);
|
2012-05-28 08:01:33 +03:00
|
|
|
|
break;
|
|
|
|
|
}
|
2012-06-23 23:11:13 +03:00
|
|
|
|
$file = $book->getFilePath ($type, $idData, true);
|
2013-01-02 22:50:44 +02:00
|
|
|
|
if ($type == "epub" && $config['cops_update_epub-metadata'])
|
|
|
|
|
{
|
|
|
|
|
$book->getUpdatedEpub ($idData);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2013-01-03 22:40:43 +02:00
|
|
|
|
if ($type == "jpg") {
|
|
|
|
|
header('Content-Disposition: filename="' . basename ($file) . '"');
|
|
|
|
|
} else {
|
|
|
|
|
header('Content-Disposition: attachment; filename="' . basename ($file) . '"');
|
|
|
|
|
}
|
2013-12-05 11:52:51 +02:00
|
|
|
|
|
2013-01-20 14:49:08 +02:00
|
|
|
|
$dir = $config['calibre_internal_directory'];
|
|
|
|
|
if (empty ($config['calibre_internal_directory'])) {
|
2013-04-03 16:00:09 +03:00
|
|
|
|
$dir = Base::getDbDirectory ();
|
2013-01-20 14:49:08 +02:00
|
|
|
|
}
|
2013-12-05 11:52:51 +02:00
|
|
|
|
|
2013-01-19 07:19:46 +02:00
|
|
|
|
if (empty ($config['cops_x_accel_redirect'])) {
|
2013-01-20 14:49:08 +02:00
|
|
|
|
$filename = $dir . $file;
|
2013-01-19 07:19:46 +02:00
|
|
|
|
$fp = fopen($filename, 'rb');
|
|
|
|
|
header("Content-Length: " . filesize($filename));
|
|
|
|
|
fpassthru($fp);
|
|
|
|
|
}
|
|
|
|
|
else {
|
2013-01-20 14:49:08 +02:00
|
|
|
|
header ($config['cops_x_accel_redirect'] . ": " . $dir . $file);
|
2013-01-19 07:19:46 +02:00
|
|
|
|
}
|