Test Setup Failed
Pull Request — master (#424)
by
unknown
50:02
created

fetch.php (4 issues)

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
    // -DC- Add png type
43
    if ($book && ($type == 'jpg' || $type == 'png' || empty ($config['calibre_internal_directory']))) {
44
    	  if ($type == 'jpg' || $type == 'png') {
45
            $file = $book->getFilePath($type);
46
        } else {
47
            $file = $book->getFilePath($type, $idData);
48
        }
49
        if (is_null($file) || !file_exists($file)) {
50
            notFound();
51
            return;
52
        }
53
    }
54
55
    switch ($type)
56
    {
57
    		// -DC- Add png type
58
        case 'jpg':
59
        case 'png':
60
        		if ($type == 'jpg') {
61
          	  header('Content-Type: image/jpeg');
62
        		}
63
        		else {
64
        			header('Content-Type: image/png');
65
        		}
66
            //by default, we don't cache
67
            $thumbnailCacheFullpath = null;
68
            if ( isset($config['cops_thumbnail_cache_directory']) && $config['cops_thumbnail_cache_directory'] !== '' ) {
69
                $thumbnailCacheFullpath = $config['cops_thumbnail_cache_directory'];
70
                //if multiple databases, add a subfolder with the database ID
71
                $thumbnailCacheFullpath .= !is_null(GetUrlParam (DB)) ? 'db-' . GetUrlParam (DB) . DIRECTORY_SEPARATOR : '';
72
                //when there are lots of thumbnails, it's better to save files in subfolders, so if the book's uuid is
73
                //"01234567-89ab-cdef-0123-456789abcdef", we will save the thumbnail in .../0/12/34567-89ab-cdef-0123-456789abcdef-...
74
                $thumbnailCacheFullpath .= substr($book->uuid, 0, 1) . DIRECTORY_SEPARATOR . substr($book->uuid, 1, 2) . DIRECTORY_SEPARATOR;
75
                //check if cache folder exists or create it
76
                if ( file_exists($thumbnailCacheFullpath) || mkdir($thumbnailCacheFullpath, 0700, true) ) {
77
                    //we name the thumbnail from the book's uuid and it's dimensions (width and/or height)
78
                    $thumbnailCacheName = substr($book->uuid, 3) . '-' . getURLParam('width') . 'x' . getURLParam('height') . '.' . $type;
79
                    $thumbnailCacheFullpath = $thumbnailCacheFullpath . $thumbnailCacheName;
80
                } else {
81
                    //error creating the folder, so we don't cache
82
                    $thumbnailCacheFullpath = null;
83
                }
84
            }
85
86
            if ( $thumbnailCacheFullpath !== null && file_exists($thumbnailCacheFullpath) ) {
87
                //return the already cached thumbnail
88
                readfile( $thumbnailCacheFullpath );
89
                return;
90
            }
91
92
            if ($book->getThumbnail (getURLParam('width'), getURLParam('height'), $thumbnailCacheFullpath)) {
93
                //if we don't cache the thumbnail, imagejpeg() in $book->getThumbnail() already return the image data
94
                if ( $thumbnailCacheFullpath === null ) {
95
                    // The cover had to be resized
96
                    return;
97
                } else {
98
                    //return the just cached thumbnail
99
                    readfile( $thumbnailCacheFullpath );
100
                    return;
101
                }
102
            }
103
            break;
104
        default:
105
            $data = $book->getDataById($idData);
106
            header('Content-Type: ' . $data->getMimeType());
107
            break;
108
    }
109
    $file = $book->getFilePath($type, $idData, true);
110
    if (!$viewOnly && $type == 'epub' && $config['cops_update_epub-metadata']) {
111
        $book->getUpdatedEpub($idData);
112
        return;
113
    }
114
    // -DC- Add png type
115
    if ($type == 'jpg' || $type == 'png') {
116
        header('Content-Disposition: filename="' . basename($file) . '"');
117
    } elseif ($viewOnly) {
118
        header('Content-Disposition: inline');
119
    } else {
120
        header('Content-Disposition: attachment; filename="' . basename($file) . '"');
121
    }
122
123
    // -DC- File is a full path
124
    //$dir = $config['calibre_internal_directory'];
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
125
    //if (empty($config['calibre_internal_directory'])) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
85% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
126
    //    $dir = Base::getDbDirectory();
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
127
    //}
128
    $dir = '';
129
130
    if (empty($config['cops_x_accel_redirect'])) {
131
        $filename = $dir . $file;
132
        $fp = fopen($filename, 'rb');
133
        header('Content-Length: ' . filesize($filename));
134
        fpassthru($fp);
135
        fclose($fp); // -DC- Close file
136
    } else {
137
        header($config['cops_x_accel_redirect'] . ': ' . $dir . $file);
138
    }
139