Completed
Pull Request — master (#303)
by
unknown
03:17
created

Data   B

Complexity

Total Complexity 39

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
dl 0
loc 199
ccs 90
cts 96
cp 0.9375
rs 8.2857
c 0
b 0
f 0
wmc 39
lcom 1
cbo 2

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A isKnownType() 0 3 1
A getMimeType() 0 17 4
A isEpubValidOnKobo() 0 3 2
A getFilename() 0 3 1
A getUpdatedFilename() 0 3 1
A getUpdatedFilenameEpub() 0 3 1
A getDataLink() 0 9 3
A getHtmlLink() 0 3 1
A getLocalPath() 0 3 1
A getDataByBook() 0 12 2
A handleThumbnailLink() 0 18 4
C getLink() 0 29 11
A getUpdatedFilenameKepub() 0 5 1
B getHtmlLinkWithRewriting() 0 17 5
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 63
    public function __construct($post, $book = null) {
60 63
        $this->id = $post->id;
61 63
        $this->name = $post->name;
62 63
        $this->format = $post->format;
63 63
        $this->realFormat = str_replace ("ORIGINAL_", "", $post->format);
64 63
        $this->extension = strtolower ($this->realFormat);
65 63
        $this->book = $book;
66 63
    }
67
68 53
    public function isKnownType () {
69 53
        return array_key_exists ($this->extension, self::$mimetypes);
70
    }
71
72 53
    public function getMimeType () {
73 53
        $result = "application/octet-stream";
74 53
        if ($this->isKnownType ()) {
75 53
            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 50
    public function getFilename () {
95 50
        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
        $str = $this->getUpdatedFilename () . ".kepub.epub";
108 1
        return str_replace(array(':', '#', '&'),
109 1
                           array('-', '-', ' '), $str );
110
    }
111
112 49
    public function getDataLink ($rel, $title = NULL) {
113 49
        global $config;
114
115 49
        if ($rel == Link::OPDS_ACQUISITION_TYPE && $config['cops_use_url_rewriting'] == "1") {
116 2
            return $this->getHtmlLinkWithRewriting($title);
117
        }
118
119 48
        return self::getLink ($this->book, $this->extension, $this->getMimeType (), $rel, $this->getFilename (), $this->id, $title);
120
    }
121
122 5
    public function getHtmlLink () {
123 5
        return $this->getDataLink(Link::OPDS_ACQUISITION_TYPE)->href;
124
    }
125
126 2
    public function getLocalPath () {
127 2
        return $this->book->path . "/" . $this->getFilename ();
128
    }
129
130 2
    public function getHtmlLinkWithRewriting ($title = NULL) {
131 2
        global $config;
132
133 2
        $database = "";
134 2
        if (!is_null (GetUrlParam (DB))) $database = GetUrlParam (DB) . "/";
135
136 2
        $href = "download/" . $this->id . "/" . $database;
137
138 2
        if ($config['cops_provide_kepub'] == "1" &&
139 2
            $this->isEpubValidOnKobo () &&
140 2
            preg_match("/Kobo/", $_SERVER['HTTP_USER_AGENT'])) {
141 1
            $href .= rawurlencode ($this->getUpdatedFilenameKepub ());
142 1
        } else {
143 2
            $href .= rawurlencode ($this->getFilename ());
144
        }
145 2
        return new Link ($href, $this->getMimeType (), Link::OPDS_ACQUISITION_TYPE, $title);
146
    }
147
148 62
    public static function getDataByBook ($book) {
149 62
        $out = array ();
150 62
        $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...
151 62
                                             from data where book = ?');
152 62
        $result->execute (array ($book->id));
153
154 62
        while ($post = $result->fetchObject ())
155
        {
156 62
            array_push ($out, new Data ($post, $book));
157 62
        }
158 62
        return $out;
159
    }
160
161 19
    public static function handleThumbnailLink ($urlParam, $height) {
162 19
        global $config;
163
164 19
        if (is_null ($height)) {
165 18
            if (preg_match ('/feed.php/', $_SERVER["SCRIPT_NAME"])) {
166
                $height = $config['cops_opds_thumbnail_height'];
167
            }
168
            else
169
            {
170 18
                $height = $config['cops_html_thumbnail_height'];
171
            }
172 18
        }
173 19
        if ($config['cops_thumbnail_handling'] != "1") {
174 19
            $urlParam = addURLParameter($urlParam, "height", $height);
175 19
        }
176
177 19
        return $urlParam;
178
    }
179
180 49
    public static function getLink ($book, $type, $mime, $rel, $filename, $idData, $title = NULL, $height = NULL)
181
    {
182 49
        global $config;
183
184 49
        $urlParam = addURLParameter("", "data", $idData);
185
186 49
        if (Base::useAbsolutePath () ||
187
            $rel == Link::OPDS_THUMBNAIL_TYPE ||
188
            ($type == "epub" && $config['cops_update_epub-metadata']))
189 49
        {
190 49
            if ($type != "jpg") $urlParam = addURLParameter($urlParam, "type", $type);
191 49
            if ($rel == Link::OPDS_THUMBNAIL_TYPE) {
192 19
                $urlParam = self::handleThumbnailLink($urlParam, $height);
193 19
            }
194 49
            $urlParam = addURLParameter($urlParam, "id", $book->id);
195 49
            if (!is_null (GetUrlParam (DB))) $urlParam = addURLParameter ($urlParam, DB, GetUrlParam (DB));
196 49
            if ($config['cops_thumbnail_handling'] != "1" &&
197 49
                !empty ($config['cops_thumbnail_handling']) &&
198 49
                $rel == Link::OPDS_THUMBNAIL_TYPE) {
199
                return new Link ($config['cops_thumbnail_handling'], $mime, $rel, $title);
200
            } else {
201 49
                return new Link ("fetch.php?" . $urlParam, $mime, $rel, $title);
202
            }
203
        }
204
        else
205
        {
206
            return new Link (str_replace('%2F','/',rawurlencode ($book->path."/".$filename)), $mime, $rel, $title);
207
        }
208
    }
209
}
210