1) Исправления в связи со сменой API MySQL
[openlib.git] / www / data.php
diff --git a/www/data.php b/www/data.php
new file mode 100644 (file)
index 0000000..2219bc8
--- /dev/null
@@ -0,0 +1,139 @@
+<?php
+/**
+ * COPS (Calibre OPDS PHP Server) class file
+ *
+ * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
+ * @author     Sébastien Lucas <sebastien@slucas.fr>
+ */
+
+require_once('base.php');
+
+class Data extends Base {
+    public $id;
+    public $name;
+    public $format;
+    public $realFormat;
+    public $extension;
+    public $book;
+    
+    public static $mimetypes = array(
+        'azw'   => 'application/x-mobipocket-ebook',
+        'azw3'  => 'application/x-mobipocket-ebook',
+        'cbz'   => 'application/x-cbz',
+        'cbr'   => 'application/x-cbr',
+        'doc'   => 'application/msword',
+        'epub'  => 'application/epub+zip',
+        'fb2'   => 'text/fb2+xml',
+        'kobo'  => 'application/x-koboreader-ebook',
+        'mobi'  => 'application/x-mobipocket-ebook',
+        'lit'   => 'application/x-ms-reader',
+        'lrs'   => 'text/x-sony-bbeb+xml',
+        'lrf'   => 'application/x-sony-bbeb',
+        'lrx'   => 'application/x-sony-bbeb',
+        'ncx'   => 'application/x-dtbncx+xml',
+        'opf'   => 'application/oebps-package+xml',
+        'otf'   => 'application/x-font-opentype',
+        'pdb'   => 'application/vnd.palm',
+        'pdf'   => 'application/pdf',
+        'prc'   => 'application/x-mobipocket-ebook',
+        'rtf'   => 'application/rtf',
+        'svg'   => 'image/svg+xml',
+        'ttf'   => 'application/x-font-truetype',
+        'wmf'   => 'image/wmf',
+        'xhtml' => 'application/xhtml+xml',
+        'xpgt'  => 'application/adobe-page-template+xml',
+        'zip'   => 'application/zip',
+        'fb2.zip' => 'application/fb2+zip'
+    );
+    
+    public function __construct($post, $book = null) {
+        $this->id = $post->id;
+        $this->name = $post->name;
+        $this->format = $post->format;
+        $this->realFormat = str_replace ("ORIGINAL_", "", $post->format);
+        $this->extension = strtolower ($this->realFormat);
+        $this->book = $book;
+    }
+    
+    public function isKnownType () {
+        return array_key_exists ($this->extension, self::$mimetypes);
+    }
+    
+    public function getMimeType () {
+        if ($this->isKnownType ()) {
+            return self::$mimetypes [$this->extension];
+        } else {
+            return "application/octet-stream";
+        }
+    }
+    
+    public function getFilename () {
+        return $this->name . "." . strtolower ($this->format);
+    }
+    
+    public function getUpdatedFilename () {
+        return $this->book->getAuthorsName () . " - " . $this->book->title;
+    }
+
+    public function getUpdatedFilenameEpub () {
+        return $this->getUpdatedFilename () . ".epub";
+    }
+
+    public function getUpdatedFilenameKepub () {
+        return $this->getUpdatedFilename () . ".kepub.epub";
+    }
+    
+    public function getDataLink ($rel, $title = NULL) {
+        return self::getBookLink ($this->book, $this->extension, $this->getMimeType (), $rel, $this->getFilename (), $this->id, $title);
+    }
+    
+    public function getLocalPath () {
+        return $this->book->path . "/" . $this->getFilename ();
+    }
+    
+    public function getHtmlLink () {
+        global $config;
+        return self::getBookLink ($this->book, $this->extension, $this->getMimeType (), NULL, $this->getFilename (), $this->id, $this->getFilename())->href;
+    }
+
+    public static function getBookLink ($book, $type, $mime, $rel, $filename, $idData, $title)
+    {
+        $id = $book->id;
+        $encoded_name=rawurlencode($filename);
+        $url = "fetch/$idData/$type/$id/$encoded_name";    
+        return new Link ($url,$mime,$rel,$filename);
+    }
+    
+    public static function getLink ($book, $type, $mime, $rel, $filename, $idData, $title = NULL, $height = NULL)
+    {
+        global $config;
+        
+        $urlParam = addURLParameter("", "data", $idData);
+        
+        if (preg_match ('/^\//', Base::getFileDirectory ()) || // Linux /
+            preg_match ('/^\w\:/', Base::getFileDirectory ()) || // Windows X:
+            $rel == Link::OPDS_THUMBNAIL_TYPE ||
+            ($type == "epub" && $config['cops_update_epub-metadata']))
+        {
+            if ($type != "jpg") $urlParam = addURLParameter($urlParam, "type", $type);
+            if ($rel == Link::OPDS_THUMBNAIL_TYPE) {
+                if (is_null ($height)) {
+                    if (preg_match ('/feed.php/', $_SERVER["SCRIPT_NAME"])) {
+                        $height = $config['cops_opds_thumbnail_height'];
+                    }
+                    else
+                    {
+                        $height = $config['cops_html_thumbnail_height'];
+                    }
+                }
+                $urlParam = addURLParameter($urlParam, "height", $height);
+            }
+            $urlParam = addURLParameter($urlParam, "id", $book->id);
+            return new Link ("fetch.php?" . $urlParam, $mime, $rel, $title);
+        }
+        else
+        {
+            return new Link (str_replace('%2F','/',rawurlencode ($book->path."/".$filename)), $mime, $rel, $title);
+        }
+    }
+}