Beispiel #1
0
 protected function createEPub($id)
 {
     $epubSQL = "SELECT \r\n\t\t\tS.sid, S.title, S.storynotes, S.summary, UNIX_TIMESTAMP(S.date) as written, UNIX_TIMESTAMP(S.date) as updated, \r\n\t\t\tGROUP_CONCAT(DISTINCT U.nickname ORDER BY U.nickname ASC SEPARATOR ', ') as authors,\r\n\t\t\tGROUP_CONCAT(DISTINCT T.label ORDER BY T.tgid,T.label ASC SEPARATOR ', ') as tags\r\n\t\t\tFROM `tbl_stories`S\r\n\t\t\t\tINNER JOIN `tbl_stories_authors`rSA ON ( S.sid=rSA.sid ) \r\n\t\t\t\t\tINNER JOIN `tbl_users`U ON (rSA.aid=U.uid)\r\n\t\t\t\tINNER JOIN `tbl_stories_tags`rST ON (S.sid=rST.sid) \r\n\t\t\t\t\tINNER JOIN `tbl_tags`T ON (rST.tid=T.tid)\r\n\t\t\tWHERE S.sid=:sid \r\n\t\t\tGROUP BY S.sid";
     $epubData = $this->exec($epubSQL, array(':sid' => $id))[0];
     \Base::instance()->set('UI', "template/epub/");
     $filename = realpath("tmp/epub") . "/s{$epubData['sid']}.zip";
     // Load or create the namespace
     if ("" == @\Config::instance()->epub_namespace) {
         $cfg = \Config::instance();
         $cfg['epub_namespace'] = uuid_v5("6ba7b810-9dad-11d1-80b4-00c04fd430c8", \Base::instance()->get('HOST') . \Base::instance()->get('BASE'));
         $cfg->save();
     }
     /*
     This must be coming from admin panel at some point, right now we will fake it
     */
     $epubData['version'] = 2;
     // supported by most readers, v3 is still quite new
     $epubData['language'] = "de";
     $epubData['uuid'] = uuid_v5(\Config::instance()->epub_namespace, $epubData['title']);
     \Base::instance()->set('EPUB', $epubData);
     $body = "";
     $re_element = array("old" => array("<center>", "</center>"), "new" => array("<span style=\"text-align: center;\">", "</span>"));
     $elements_allowed = "<a><abbr><acronym><applet><b><bdo><big><br><cite><code><del><dfn><em><i><img><ins><kbd><map><ns:svg><q><samp><small><span><strong><sub><sup><tt><var>";
     // The folder *should* exist, but creating it and ignoring the outcome is the quickest way of making sure it really is there
     @mkdir("tmp/epub", 0777, TRUE);
     /*
     Create the Archive
     Since the mimetype file has to be at the beginning of the archive and uncompressed, we have to create the zip file from binary
     */
     file_put_contents($filename, base64_decode("UEsDBAoAAAAAAOmRAT1vYassFAAAABQAAAAIAAAAbWltZXR5cGVhcHBsaWNhdGlvbi9lcHViK3ppcFBLAQIUAAoAAAAAAOmRAT1vYassFAAAABQAAAAIAAAAAAAAAAAAIAAAAAAAAABtaW1ldHlwZVBLBQYAAAAAAQABADYAAAA6AAAAAAA="));
     $zip = new \ZipArchive();
     $res = $zip->open($filename);
     if ($res === TRUE) {
         // memorize the XML opening tag
         $xml = \View\Story::epubXMLtag();
         // add folder for container file & META-INF/container.xml
         $zip->addEmptyDir('META-INF');
         $zip->addFromString('META-INF/container.xml', $xml . \View\Story::epubContainer());
         // styles.css
         $zip->addEmptyDir('Styles');
         $zip->addFromString('Styles/styles.css', \View\Story::epubCSS());
         // add folder for content
         $zip->addEmptyDir('content');
         // title.xhtml
         $zip->addFromString('content/title.xhtml', $xml . \View\Story::epubPage(\View\Story::epubTitle(), $epubData['title'], $epubData['language']));
         // page[n].xhtml							| epub_page
         $chapters = $this->exec("SELECT C.title, C.inorder\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tFROM `tbl_chapters`C\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tWHERE C.validated > '0' AND C.sid = :sid\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tORDER BY C.inorder ASC ", [":sid" => $epubData['sid']]);
         if (sizeof($chapters) > 0) {
             $n = 1;
             foreach ($chapters as $chapter) {
                 $chapterText = $this->getChapter($epubData['sid'], $chapter['inorder'], FALSE);
                 $chapterTOC[] = array("number" => $n, "title" => "{$chapter['title']}");
                 $body = \View\Story::epubChapter($chapter['title'], strip_tags(str_replace($re_element['old'], $re_element['new'], $chapterText), $elements_allowed));
                 $zip->addFromString('content/chapter' . $n++ . '.xhtml', $xml . \View\Story::epubPage($body, $chapter['title'], $epubData['language']));
             }
         } else {
             return "__StoryError";
         }
         // root.opf
         $zip->addFromString('root.opf', $xml . \View\Story::epubRoot($chapterTOC));
         // TOC
         $zip->addFromString('toc.ncx', $xml . \View\Story::epubTOC($chapterTOC));
         if ($epubData['version'] == 3) {
             $zip->addFromString('toc.xhtml', $xml . \View\Story::epubTOC($chapterTOC, 3));
         }
         $zip->close();
     }
     return [@fopen($filename, "rb"), filesize($filename)];
 }