1) Исправления в связи со сменой API MySQL
[openlib.git] / www / data.php
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 <sebastien@slucas.fr>
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         'azw3'  => 'application/x-mobipocket-ebook',
22         'cbz'   => 'application/x-cbz',
23         'cbr'   => 'application/x-cbr',
24         'doc'   => 'application/msword',
25         'epub'  => 'application/epub+zip',
26         'fb2'   => 'text/fb2+xml',
27         'kobo'  => 'application/x-koboreader-ebook',
28         'mobi'  => 'application/x-mobipocket-ebook',
29         'lit'   => 'application/x-ms-reader',
30         'lrs'   => 'text/x-sony-bbeb+xml',
31         'lrf'   => 'application/x-sony-bbeb',
32         'lrx'   => 'application/x-sony-bbeb',
33         'ncx'   => 'application/x-dtbncx+xml',
34         'opf'   => 'application/oebps-package+xml',
35         'otf'   => 'application/x-font-opentype',
36         'pdb'   => 'application/vnd.palm',
37         'pdf'   => 'application/pdf',
38         'prc'   => 'application/x-mobipocket-ebook',
39         'rtf'   => 'application/rtf',
40         'svg'   => 'image/svg+xml',
41         'ttf'   => 'application/x-font-truetype',
42         'wmf'   => 'image/wmf',
43         'xhtml' => 'application/xhtml+xml',
44         'xpgt'  => 'application/adobe-page-template+xml',
45         'zip'   => 'application/zip',
46         'fb2.zip' => 'application/fb2+zip'
47     );
48     
49     public function __construct($post, $book = null) {
50         $this->id = $post->id;
51         $this->name = $post->name;
52         $this->format = $post->format;
53         $this->realFormat = str_replace ("ORIGINAL_", "", $post->format);
54         $this->extension = strtolower ($this->realFormat);
55         $this->book = $book;
56     }
57     
58     public function isKnownType () {
59         return array_key_exists ($this->extension, self::$mimetypes);
60     }
61     
62     public function getMimeType () {
63         if ($this->isKnownType ()) {
64             return self::$mimetypes [$this->extension];
65         } else {
66             return "application/octet-stream";
67         }
68     }
69     
70     public function getFilename () {
71         return $this->name . "." . strtolower ($this->format);
72     }
73     
74     public function getUpdatedFilename () {
75         return $this->book->getAuthorsName () . " - " . $this->book->title;
76     }
77
78     public function getUpdatedFilenameEpub () {
79         return $this->getUpdatedFilename () . ".epub";
80     }
81
82     public function getUpdatedFilenameKepub () {
83         return $this->getUpdatedFilename () . ".kepub.epub";
84     }
85     
86     public function getDataLink ($rel, $title = NULL) {
87         return self::getBookLink ($this->book, $this->extension, $this->getMimeType (), $rel, $this->getFilename (), $this->id, $title);
88     }
89     
90     public function getLocalPath () {
91         return $this->book->path . "/" . $this->getFilename ();
92     }
93     
94     public function getHtmlLink () {
95         global $config;
96         return self::getBookLink ($this->book, $this->extension, $this->getMimeType (), NULL, $this->getFilename (), $this->id, $this->getFilename())->href;
97     }
98
99     public static function getBookLink ($book, $type, $mime, $rel, $filename, $idData, $title)
100     {
101         $id = $book->id;
102         $encoded_name=rawurlencode($filename);
103         $url = "fetch/$idData/$type/$id/$encoded_name";    
104         return new Link ($url,$mime,$rel,$filename);
105     }
106     
107     public static function getLink ($book, $type, $mime, $rel, $filename, $idData, $title = NULL, $height = NULL)
108     {
109         global $config;
110         
111         $urlParam = addURLParameter("", "data", $idData);
112         
113         if (preg_match ('/^\//', Base::getFileDirectory ()) || // Linux /
114             preg_match ('/^\w\:/', Base::getFileDirectory ()) || // Windows X:
115             $rel == Link::OPDS_THUMBNAIL_TYPE ||
116             ($type == "epub" && $config['cops_update_epub-metadata']))
117         {
118             if ($type != "jpg") $urlParam = addURLParameter($urlParam, "type", $type);
119             if ($rel == Link::OPDS_THUMBNAIL_TYPE) {
120                 if (is_null ($height)) {
121                     if (preg_match ('/feed.php/', $_SERVER["SCRIPT_NAME"])) {
122                         $height = $config['cops_opds_thumbnail_height'];
123                     }
124                     else
125                     {
126                         $height = $config['cops_html_thumbnail_height'];
127                     }
128                 }
129                 $urlParam = addURLParameter($urlParam, "height", $height);
130             }
131             $urlParam = addURLParameter($urlParam, "id", $book->id);
132             return new Link ("fetch.php?" . $urlParam, $mime, $rel, $title);
133         }
134         else
135         {
136             return new Link (str_replace('%2F','/',rawurlencode ($book->path."/".$filename)), $mime, $rel, $title);
137         }
138     }
139 }