Test Setup Failed
Pull Request — master (#519)
by
unknown
56s
created

fetch.php (1 issue)

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 dirname(__FILE__) . '/config.php';
10
    require_once dirname(__FILE__) . '/base.php';
11
12
    global $config;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
13
14
    if ($config['cops_fetch_protect'] == '1') {
15
        session_start();
16
        if (!isset($_SESSION['connected'])) {
17
            notFound();
18
            return;
19
        }
20
    }
21
22
    $expires = 60*60*24*14;
23
    header('Pragma: public');
24
    header('Cache-Control: max-age=' . $expires);
25
    header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');
26
    $bookId   = getURLParam('id', NULL);
27
    $type     = getURLParam('type', 'jpg');
28
    $idData   = getURLParam('data', NULL);
29
    $viewOnly = getURLParam('view', FALSE);
30
31
    if (is_null($bookId)) {
32
        $book = Book::getBookByDataId($idData);
33
    } else {
34
        $book = Book::getBookById($bookId);
35
    }
36
37
    if (!$book) {
38
        notFound ();
39
        return;
40
    }
41
42
    if ($book && ($type == 'jpg' || empty ($config['calibre_internal_directory']))) {
43
        if ($type == 'jpg') {
44
            $file = $book->getFilePath($type);
45
        } else {
46
            $file = $book->getFilePath($type, $idData);
47
        }
48
        if (is_null($file) || !file_exists($file)) {
49
            notFound();
50
            return;
51
        }
52
    }
53
54
    switch ($type)
55
    {
56
        case 'jpg':
57
            header('Content-Type: image/jpeg');
58
            //by default, we don't cache
59
            $thumbnailCacheFullpath = null;
60
            if ( isset($config['cops_thumbnail_cache_directory']) && $config['cops_thumbnail_cache_directory'] !== '' ) {
61
                $thumbnailCacheFullpath = $config['cops_thumbnail_cache_directory'];
62
                //if multiple databases, add a subfolder with the database ID
63
                $thumbnailCacheFullpath .= !is_null(GetUrlParam (DB)) ? 'db-' . GetUrlParam (DB) . DIRECTORY_SEPARATOR : '';
64
                //when there are lots of thumbnails, it's better to save files in subfolders, so if the book's uuid is
65
                //"01234567-89ab-cdef-0123-456789abcdef", we will save the thumbnail in .../0/12/34567-89ab-cdef-0123-456789abcdef-...
66
                $thumbnailCacheFullpath .= substr($book->uuid, 0, 1) . DIRECTORY_SEPARATOR . substr($book->uuid, 1, 2) . DIRECTORY_SEPARATOR;
67
                //check if cache folder exists or create it
68
                if ( file_exists($thumbnailCacheFullpath) || mkdir($thumbnailCacheFullpath, 0700, true) ) {
69
                    //we name the thumbnail from the book's uuid and it's dimensions (width and/or height)
70
                    $thumbnailCacheName = substr($book->uuid, 3) . '-' . getURLParam('width') . 'x' . getURLParam('height') . '.jpg';
71
                    $thumbnailCacheFullpath = $thumbnailCacheFullpath . $thumbnailCacheName;
72
                } else {
73
                    //error creating the folder, so we don't cache
74
                    $thumbnailCacheFullpath = null;
75
                }
76
            }
77
78
            if ( $thumbnailCacheFullpath !== null && file_exists($thumbnailCacheFullpath) ) {
79
                //return the already cached thumbnail
80
                readfile( $thumbnailCacheFullpath );
81
                return;
82
            }
83
84
            if ($book->getThumbnail (getURLParam('width'), getURLParam('height'), $thumbnailCacheFullpath)) {
85
                //if we don't cache the thumbnail, imagejpeg() in $book->getThumbnail() already return the image data
86
                if ( $thumbnailCacheFullpath === null ) {
87
                    // The cover had to be resized
88
                    return;
89
                } else {
90
                    //return the just cached thumbnail
91
                    readfile( $thumbnailCacheFullpath );
92
                    return;
93
                }
94
            }
95
            break;
96
        default:
97
            $data = $book->getDataById($idData);
98
            header('Content-Type: ' . $data->getMimeType());
99
            break;
100
    }
101
    $file = $book->getFilePath($type, $idData, true);
102
    if (!$viewOnly && $type == 'epub' && $config['cops_update_epub-metadata']) {
103
        $book->getUpdatedEpub($idData);
104
        return;
105
    }
106
    if ($type == 'jpg') {
107
        header('Content-Disposition: filename="' . basename($file) . '"');
108
    } elseif ($viewOnly) {
109
        header('Content-Disposition: inline');
110
    } else {
111
        header('Content-Disposition: attachment; filename="' . basename($file) . '"');
112
    }
113
114
    $dir = $config['calibre_internal_directory'];
115
    if (empty($config['calibre_internal_directory'])) {
116
        $dir = Base::getDbDirectory();
117
    }
118
119
    if (empty($config['cops_x_accel_redirect'])) {
120
        $filename = $dir . $file;
121
        $fp = fopen($filename, 'rb');
122
        header('Content-Length: ' . filesize($filename));
123
        fpassthru($fp);
124
    } else {
125
        header($config['cops_x_accel_redirect'] . ': ' . $dir . $file);
126
    }
127