Completed
Pull Request — master (#233)
by
unknown
10:59
created

Upgrade to new PHP Analysis Engine

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
Bug Best Practice introduced by
The expression $file of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch 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:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
51
            notFound ();
52
            return;
53
        }
54
    }
55
56
    switch ($type)
57
    {
58
        case "jpg":
59
            header("Content-Type: image/jpeg");
60
            if ($book->getThumbnail (getURLParam ("width"), getURLParam ("height"))) {
61
                // The cover had to be resized
62
                return;
63
            }
64
            break;
65
        default:
66
            $data = $book->getDataById ($idData);
67
            header("Content-Type: " . $data->getMimeType ());
68
            break;
69
    }
70
    $file = $book->getFilePath ($type, $idData, true);
71
    if ($type == "epub" && $config['cops_update_epub-metadata'])
72
    {
73
        $book->getUpdatedEpub ($idData);
74
        return;
75
    }
76
    if ($type == "jpg") {
77
        header('Content-Disposition: filename="' . basename ($file) . '"');
78
    } else {
79
        header('Content-Disposition: attachment; filename="' . basename ($file) . '"');
80
    }
81
82
    $dir = $config['calibre_internal_directory'];
83
    if (empty ($config['calibre_internal_directory'])) {
84
        $dir = Base::getDbDirectory ();
85
    }
86
87
    if (empty ($config['cops_x_accel_redirect'])) {
88
        $filename = $dir . $file;
89
        $fp = fopen($filename, 'rb');
90
        header("Content-Length: " . filesize($filename));
91
        fpassthru($fp);
92
    }
93
    else {
94
        header ($config['cops_x_accel_redirect'] . ": " . $dir . $file);
95
    }
96