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) 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
        'azw'   => 'application/x-mobipocket-ebook',
21
        'azw1'  => 'application/x-topaz-ebook',
22
        'azw2'  => 'application/x-kindle-application',
23
        'azw3'  => 'application/x-mobi8-ebook',
24
        'cbz'   => 'application/x-cbz',
25
        'cbr'   => 'application/x-cbr',
26
        'djv'   => 'image/vnd.djvu',
27
        'djvu'  => 'image/vnd.djvu',
28
        'doc'   => 'application/msword',
29
        'epub'  => 'application/epub+zip',
30
        'fb2'   => 'text/fb2+xml',
31
        'ibooks'=> 'application/x-ibooks+zip',
32
        'kepub' => 'application/epub+zip',
33
        'kobo'  => 'application/x-koboreader-ebook',
34
        'mobi'  => 'application/x-mobipocket-ebook',
35
        'lit'   => 'application/x-ms-reader',
36
        'lrs'   => 'text/x-sony-bbeb+xml',
37
        'lrf'   => 'application/x-sony-bbeb',
38
        'lrx'   => 'application/x-sony-bbeb',
39
        'ncx'   => 'application/x-dtbncx+xml',
40
        'opf'   => 'application/oebps-package+xml',
41
        'otf'   => 'application/x-font-opentype',
42
        'pdb'   => 'application/vnd.palm',
43
        'pdf'   => 'application/pdf',
44
        'prc'   => 'application/x-mobipocket-ebook',
45
        'rtf'   => 'application/rtf',
46
        'svg'   => 'image/svg+xml',
47
        'ttf'   => 'application/x-font-truetype',
48
        'tpz'   => 'application/x-topaz-ebook',
49
        'wmf'   => 'image/wmf',
50
        'xhtml' => 'application/xhtml+xml',
51
        'xpgt'  => 'application/adobe-page-template+xml',
52
        'zip'   => 'application/zip'
53
    );
54
55 60
    public function __construct($post, $book = null) {
56 60
        $this->id = $post->id;
57 60
        $this->name = $post->name;
58 60
        $this->format = $post->format;
59 60
        $this->realFormat = str_replace ("ORIGINAL_", "", $post->format);
60 60
        $this->extension = strtolower ($this->realFormat);
61 60
        $this->book = $book;
62 60
    }
63
64 50
    public function isKnownType () {
65 50
        return array_key_exists ($this->extension, self::$mimetypes);
66
    }
67
68 50
    public function getMimeType () {
69 50
        $result = "application/octet-stream";
70 50
        if ($this->isKnownType ()) {
71 50
            return self::$mimetypes [$this->extension];
72 1
        } elseif (function_exists('finfo_open') === true) {
73 1
            $finfo = finfo_open(FILEINFO_MIME_TYPE);
74
75 1
            if (is_resource($finfo) === true)
76 1
            {
77 1
                $result = finfo_file($finfo, $this->getLocalPath ());
78 1
            }
79
80 1
            finfo_close($finfo);
81
82 1
        }
83 1
        return $result;
84
    }
85
86 1
    public function isEpubValidOnKobo () {
87 1
        return $this->format == "EPUB" || $this->format == "KEPUB";
88
    }
89
90 47
    public function getFilename () {
91 47
        return $this->name . "." . strtolower ($this->format);
92
    }
93
94 1
    public function getUpdatedFilename () {
95 1
        return $this->book->getAuthorsSort () . " - " . $this->book->title;
96
    }
97
98 1
    public function getUpdatedFilenameEpub () {
99 1
        return $this->getUpdatedFilename () . ".epub";
100
    }
101
102 1
    public function getUpdatedFilenameKepub () {
103 1
        return $this->getUpdatedFilename () . ".kepub.epub";
104
    }
105
106 46
    public function getDataLink ($rel, $title = NULL) {
107 46
        global $config;
108
109 46
        if ($rel == Link::OPDS_ACQUISITION_TYPE && $config['cops_use_url_rewriting'] == "1") {
110 2
            return $this->getHtmlLinkWithRewriting($title);
111
        }
112
113 45
        return self::getLink ($this->book, $this->extension, $this->getMimeType (), $rel, $this->getFilename (), $this->id, $title);
114
    }
115
116 4
    public function getHtmlLink () {
117 4
        return $this->getDataLink(Link::OPDS_ACQUISITION_TYPE)->href;
118
    }
119
120 2
    public function getLocalPath () {
121 2
        return $this->book->path . "/" . $this->getFilename ();
122
    }
123
124 2
    public function getHtmlLinkWithRewriting ($title = NULL) {
125 2
        global $config;
126
127 2
        $database = "";
128 2
        if (!is_null (GetUrlParam (DB))) $database = GetUrlParam (DB) . "/";
129
130 2
        $href = "download/" . $this->id . "/" . $database;
131
132 2
        if ($config['cops_provide_kepub'] == "1" &&
133 2
            $this->isEpubValidOnKobo () &&
134 2
            preg_match("/Kobo/", $_SERVER['HTTP_USER_AGENT'])) {
135 1
            $href .= urlencode ($this->getUpdatedFilenameKepub ());
136 1
        } else {
137 2
            $href .= urlencode ($this->getFilename ());
138
        }
139 2
        return new Link ($href, $this->getMimeType (), Link::OPDS_ACQUISITION_TYPE, $title);
140
    }
141
142 59
    public static function getDataByBook ($book) {
143 59
        $out = array ();
144 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...
145 59
                                             from data where book = ?');
146 59
        $result->execute (array ($book->id));
147
148 59
        while ($post = $result->fetchObject ())
149
        {
150 59
            array_push ($out, new Data ($post, $book));
151 59
        }
152 59
        return $out;
153
    }
154
155 19
    public static function handleThumbnailLink ($urlParam, $height) {
156 19
        global $config;
157
158 19
        if (is_null ($height)) {
159 18
            if (preg_match ('/feed.php/', $_SERVER["SCRIPT_NAME"])) {
160
                $height = $config['cops_opds_thumbnail_height'];
161
            }
162
            else
163
            {
164 18
                $height = $config['cops_html_thumbnail_height'];
165
            }
166 18
        }
167 19
        if ($config['cops_thumbnail_handling'] != "1") {
168 19
            $urlParam = addURLParameter($urlParam, "height", $height);
169 19
        }
170
171 19
        return $urlParam;
172
    }
173
174 46
    public static function getLink ($book, $type, $mime, $rel, $filename, $idData, $title = NULL, $height = NULL)
175
    {
176 46
        global $config;
177
178 46
        $urlParam = addURLParameter("", "data", $idData);
179
180 46
        if (Base::useAbsolutePath () ||
181
            $rel == Link::OPDS_THUMBNAIL_TYPE ||
182
            ($type == "epub" && $config['cops_update_epub-metadata']))
183 46
        {
184 46
            if ($type != "jpg") $urlParam = addURLParameter($urlParam, "type", $type);
185 46
            if ($rel == Link::OPDS_THUMBNAIL_TYPE) {
186 19
                $urlParam = self::handleThumbnailLink($urlParam, $height);
187 19
            }
188 46
            $urlParam = addURLParameter($urlParam, "id", $book->id);
189 46
            if (!is_null (GetUrlParam (DB))) {
190 46
            	$urlParam = addURLParameter ($urlParam, DB, GetUrlParam (DB));
191 46
            	$urlParam = addURLParameter ($urlParam, VL, GetUrlParam (VL, 0));
192 46
            }
193
            if ($config['cops_thumbnail_handling'] != "1" &&
194
                !empty ($config['cops_thumbnail_handling']) &&
195 46
                $rel == Link::OPDS_THUMBNAIL_TYPE) {
196
                return new Link ($config['cops_thumbnail_handling'], $mime, $rel, $title);
197
            } else {
198
                return new Link ("fetch.php?" . $urlParam, $mime, $rel, $title);
199
            }
200
        }
201
        else
202
        {
203
            return new Link (str_replace('%2F','/',rawurlencode ($book->path."/".$filename)), $mime, $rel, $title);
204
        }
205
    }
206
}
207