Completed
Push — master ( 267988...a9e5cc )
by Sébastien
13:40
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) class file
4
 *
5
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6
 * @author     S�bastien Lucas <[email protected]>
7
 */
8
9
require_once('base.php');
10
11
class Data extends Base {
12
    public $id;
13
    public $name;
14
    public $format;
15
    public $realFormat;
16
    public $extension;
17
    public $book;
18
19
    public static $mimetypes = array(
20
        'aac'   => 'audio/aac',
21
        'azw'   => 'application/x-mobipocket-ebook',
22
        'azw1'  => 'application/x-topaz-ebook',
23
        'azw2'  => 'application/x-kindle-application',
24
        'azw3'  => 'application/x-mobi8-ebook',
25
        'cbz'   => 'application/x-cbz',
26
        'cbr'   => 'application/x-cbr',
27
        'djv'   => 'image/vnd.djvu',
28
        'djvu'  => 'image/vnd.djvu',
29
        'doc'   => 'application/msword',
30
        'epub'  => 'application/epub+zip',
31
        'fb2'   => 'text/fb2+xml',
32
        'ibooks'=> 'application/x-ibooks+zip',
33
        'kepub' => 'application/epub+zip',
34
        'kobo'  => 'application/x-koboreader-ebook',
35
        'm4a'   => 'audio/mp4',
36
        'mobi'  => 'application/x-mobipocket-ebook',
37
        'mp3'   => 'audio/mpeg',
38
        'lit'   => 'application/x-ms-reader',
39
        'lrs'   => 'text/x-sony-bbeb+xml',
40
        'lrf'   => 'application/x-sony-bbeb',
41
        'lrx'   => 'application/x-sony-bbeb',
42
        'ncx'   => 'application/x-dtbncx+xml',
43
        'opf'   => 'application/oebps-package+xml',
44
        'otf'   => 'application/x-font-opentype',
45
        'pdb'   => 'application/vnd.palm',
46
        'pdf'   => 'application/pdf',
47
        'prc'   => 'application/x-mobipocket-ebook',
48
        'rtf'   => 'application/rtf',
49
        'svg'   => 'image/svg+xml',
50
        'ttf'   => 'application/x-font-truetype',
51
        'tpz'   => 'application/x-topaz-ebook',
52
        'wav'   => 'audio/wav',
53
        'wmf'   => 'image/wmf',
54
        'xhtml' => 'application/xhtml+xml',
55
        'xpgt'  => 'application/adobe-page-template+xml',
56
        'zip'   => 'application/zip'
57
    );
58
59 60
    public function __construct($post, $book = null) {
60 60
        $this->id = $post->id;
61 60
        $this->name = $post->name;
62 60
        $this->format = $post->format;
63 60
        $this->realFormat = str_replace ("ORIGINAL_", "", $post->format);
64 60
        $this->extension = strtolower ($this->realFormat);
65 60
        $this->book = $book;
66 60
    }
67
68 50
    public function isKnownType () {
69 50
        return array_key_exists ($this->extension, self::$mimetypes);
70
    }
71
72 50
    public function getMimeType () {
73 50
        $result = "application/octet-stream";
74 50
        if ($this->isKnownType ()) {
75 50
            return self::$mimetypes [$this->extension];
76 1
        } elseif (function_exists('finfo_open') === true) {
77 1
            $finfo = finfo_open(FILEINFO_MIME_TYPE);
78
79 1
            if (is_resource($finfo) === true)
80 1
            {
81 1
                $result = finfo_file($finfo, $this->getLocalPath ());
82 1
            }
83
84 1
            finfo_close($finfo);
85
86 1
        }
87 1
        return $result;
88
    }
89
90 1
    public function isEpubValidOnKobo () {
91 1
        return $this->format == "EPUB" || $this->format == "KEPUB";
92
    }
93
94 47
    public function getFilename () {
95 47
        return $this->name . "." . strtolower ($this->format);
96
    }
97
98 1
    public function getUpdatedFilename () {
99 1
        return $this->book->getAuthorsSort () . " - " . $this->book->title;
100
    }
101
102 1
    public function getUpdatedFilenameEpub () {
103 1
        return $this->getUpdatedFilename () . ".epub";
104
    }
105
106 1
    public function getUpdatedFilenameKepub () {
107 1
        return $this->getUpdatedFilename () . ".kepub.epub";
108
    }
109
110 46
    public function getDataLink ($rel, $title = NULL) {
111 46
        global $config;
112
113 46
        if ($rel == Link::OPDS_ACQUISITION_TYPE && $config['cops_use_url_rewriting'] == "1") {
114 2
            return $this->getHtmlLinkWithRewriting($title);
115
        }
116
117 45
        return self::getLink ($this->book, $this->extension, $this->getMimeType (), $rel, $this->getFilename (), $this->id, $title);
118
    }
119
120 4
    public function getHtmlLink () {
121 4
        return $this->getDataLink(Link::OPDS_ACQUISITION_TYPE)->href;
122
    }
123
124 2
    public function getLocalPath () {
125 2
        return $this->book->path . "/" . $this->getFilename ();
126
    }
127
128 2
    public function getHtmlLinkWithRewriting ($title = NULL) {
129 2
        global $config;
130
131 2
        $database = "";
132 2
        if (!is_null (GetUrlParam (DB))) $database = GetUrlParam (DB) . "/";
133
134 2
        $href = "download/" . $this->id . "/" . $database;
135
136 2
        if ($config['cops_provide_kepub'] == "1" &&
137 2
            $this->isEpubValidOnKobo () &&
138 2
            preg_match("/Kobo/", $_SERVER['HTTP_USER_AGENT'])) {
139 1
            $href .= rawurlencode ($this->getUpdatedFilenameKepub ());
140 1
        } else {
141 2
            $href .= rawurlencode ($this->getFilename ());
142
        }
143 2
        return new Link ($href, $this->getMimeType (), Link::OPDS_ACQUISITION_TYPE, $title);
144
    }
145
146 59
    public static function getDataByBook ($book) {
147 59
        $out = array ();
148 59
        $result = parent::getDb ()->prepare('select id, format, name
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getDb() instead of getDataByBook()). Are you sure this is correct? If so, you might want to change this to $this->getDb().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
149 59
                                             from data where book = ?');
150 59
        $result->execute (array ($book->id));
151
152 59
        while ($post = $result->fetchObject ())
153
        {
154 59
            array_push ($out, new Data ($post, $book));
155 59
        }
156 59
        return $out;
157
    }
158
159 19
    public static function handleThumbnailLink ($urlParam, $height) {
160 19
        global $config;
161
162 19
        if (is_null ($height)) {
163 18
            if (preg_match ('/feed.php/', $_SERVER["SCRIPT_NAME"])) {
164
                $height = $config['cops_opds_thumbnail_height'];
165
            }
166
            else
167
            {
168 18
                $height = $config['cops_html_thumbnail_height'];
169
            }
170 18
        }
171 19
        if ($config['cops_thumbnail_handling'] != "1") {
172 19
            $urlParam = addURLParameter($urlParam, "height", $height);
173 19
        }
174
175 19
        return $urlParam;
176
    }
177
178 46
    public static function getLink ($book, $type, $mime, $rel, $filename, $idData, $title = NULL, $height = NULL)
179
    {
180 46
        global $config;
181
182 46
        $urlParam = addURLParameter("", "data", $idData);
183
184 46
        if (Base::useAbsolutePath () ||
185
            $rel == Link::OPDS_THUMBNAIL_TYPE ||
186
            ($type == "epub" && $config['cops_update_epub-metadata']))
187 46
        {
188 46
            if ($type != "jpg") $urlParam = addURLParameter($urlParam, "type", $type);
189 46
            if ($rel == Link::OPDS_THUMBNAIL_TYPE) {
190 19
                $urlParam = self::handleThumbnailLink($urlParam, $height);
191 19
            }
192 46
            $urlParam = addURLParameter($urlParam, "id", $book->id);
193 46
            if (!is_null (GetUrlParam (DB))) $urlParam = addURLParameter ($urlParam, DB, GetUrlParam (DB));
194 46
            if ($config['cops_thumbnail_handling'] != "1" &&
195 46
                !empty ($config['cops_thumbnail_handling']) &&
196 46
                $rel == Link::OPDS_THUMBNAIL_TYPE) {
197
                return new Link ($config['cops_thumbnail_handling'], $mime, $rel, $title);
198
            } else {
199 46
                return new Link ("fetch.php?" . $urlParam, $mime, $rel, $title);
200
            }
201
        }
202
        else
203
        {
204
            return new Link (str_replace('%2F','/',rawurlencode ($book->path."/".$filename)), $mime, $rel, $title);
205
        }
206
    }
207
}
208