3 * Create an ePub compatible book file.
5 * Please note, once finalized a book can no longer have chapters of data added or changed.
7 * License: GNU LGPL, Attribution required for commercial implementations, requested for everything else.
9 * Thanks to: Adam Schmalhofer and Kirstyn Fox for invaluable input and for "nudging" me in the right direction :)
11 * @author A. Grandt <php@grandt.com>
12 * @copyright 2009-2014 A. Grandt
13 * @license GNU LGPL 2.1
15 * @link http://www.phpclasses.org/package/6115
16 * @link https://github.com/Grandt/PHPePub
17 * @uses Zip.php version 1.50; http://www.phpclasses.org/browse/package/6110.html or https://github.com/Grandt/PHPZip
21 const REQ_ZIP_VERSION
= 1.60;
23 const IDENTIFIER_UUID
= 'UUID';
24 const IDENTIFIER_URI
= 'URI';
25 const IDENTIFIER_ISBN
= 'ISBN';
27 /** Ignore all external references, and do not process the file for these */
28 const EXTERNAL_REF_IGNORE
= 0;
29 /** Process the file for external references and add them to the book */
30 const EXTERNAL_REF_ADD
= 1;
31 /** Process the file for external references and add them to the book, but remove images, and img tags */
32 const EXTERNAL_REF_REMOVE_IMAGES
= 2;
33 /** Process the file for external references and add them to the book, but replace images, and img tags with [image] */
34 const EXTERNAL_REF_REPLACE_IMAGES
= 3;
36 const DIRECTION_LEFT_TO_RIGHT
= "ltr";
37 const DIRECTION_RIGHT_TO_LEFT
= "rtl";
39 const BOOK_VERSION_EPUB2
= "2.0";
40 const BOOK_VERSION_EPUB3
= "3.0";
42 private $bookVersion = EPub
::BOOK_VERSION_EPUB2
;
44 public $maxImageWidth = 768;
45 public $maxImageHeight = 1024;
47 public $splitDefaultSize = 250000;
48 /** Gifs can crash some early ADE based readers, and are disabled by default.
49 * getImage will convert these if it can, unless this is set to TRUE.
51 public $isGifImagesEnabled = FALSE;
52 public $isReferencesAddedToToc = TRUE;
57 private $language = "en";
58 private $identifier = "";
59 private $identifierType = "";
60 private $description = "";
62 private $authorSortKey = "";
63 private $publisherName = "";
64 private $publisherURL = "";
67 private $coverage = "";
68 private $relation = "";
69 private $sourceURL = "";
71 private $chapterCount = 0;
74 private $isFinalized = FALSE;
75 private $isCoverImageSet = FALSE;
76 private $buildTOC = FALSE;
77 private $tocTitle = NULL;
78 private $tocFileName = NULL;
79 private $tocCSSClass = NULL;
80 private $tocAddReferences = FALSE;
81 private $tocCssFileName = NULL;
83 private $fileList = array();
84 private $writingDirection = EPub
::DIRECTION_LEFT_TO_RIGHT
;
85 private $languageCode = "en";
88 * Used for building the TOC.
89 * If this list is overwritten it MUST contain at least "text" as an element.
91 public $referencesOrder = NULL;
93 private $dateformat = 'Y-m-d\TH:i:s.000000P'; // ISO 8601 long
94 private $dateformatShort = 'Y-m-d'; // short date format to placate ePubChecker.
95 private $headerDateFormat = "D, d M Y H:i:s T";
97 protected $isCurlInstalled;
98 protected $isGdInstalled;
99 protected $isExifInstalled;
100 protected $isFileGetContentsInstalled;
101 protected $isFileGetContentsExtInstalled;
103 private $bookRoot = "OEBPS/";
104 private $docRoot = NULL;
105 private $EPubMark = TRUE;
106 private $generator = "";
109 public $isLogging = TRUE;
111 public $encodeHTML = FALSE;
113 private $mimetypes = array(
114 "js" => "application/x-javascript", "swf" => "application/x-shockwave-flash", "xht" => "application/xhtml+xml", "xhtml" => "application/xhtml+xml", "zip" => "application/zip",
115 "aif" => "audio/x-aiff", "aifc" => "audio/x-aiff", "aiff" => "audio/x-aiff", "au" => "audio/basic", "kar" => "audio/midi", "m3u" => "audio/x-mpegurl", "mid" => "audio/midi", "midi" => "audio/midi", "mp2" => "audio/mpeg", "mp3" => "audio/mpeg", "mpga" => "audio/mpeg", "oga" => "audio/ogg", "ogg" => "audio/ogg", "ra" => "audio/x-realaudio", "ram" => "audio/x-pn-realaudio", "rm" => "audio/x-pn-realaudio", "rpm" => "audio/x-pn-realaudio-plugin", "snd" => "audio/basic", "wav" => "audio/x-wav",
116 "bmp" => "image/bmp", "djv" => "image/vnd.djvu", "djvu" => "image/vnd.djvu", "gif" => "image/gif", "ief" => "image/ief", "jpe" => "image/jpeg", "jpeg" => "image/jpeg", "jpg" => "image/jpeg", "pbm" => "image/x-portable-bitmap", "pgm" => "image/x-portable-graymap", "png" => "image/png", "pnm" => "image/x-portable-anymap", "ppm" => "image/x-portable-pixmap", "ras" => "image/x-cmu-raster", "rgb" => "image/x-rgb", "tif" => "image/tif", "tiff" => "image/tiff", "wbmp" => "image/vnd.wap.wbmp", "xbm" => "image/x-xbitmap", "xpm" => "image/x-xpixmap", "xwd" => "image/x-windowdump",
117 "asc" => "text/plain", "css" => "text/css", "etx" => "text/x-setext", "htm" => "text/html", "html" => "text/html", "rtf" => "text/rtf", "rtx" => "text/richtext", "sgm" => "text/sgml", "sgml" => "text/sgml", "tsv" => "text/tab-seperated-values", "txt" => "text/plain", "wml" => "text/vnd.wap.wml", "wmls" => "text/vnd.wap.wmlscript", "xml" => "text/xml", "xsl" => "text/xml",
118 "avi" => "video/x-msvideo", "mov" => "video/quicktime", "movie" => "video/x-sgi-movie", "mp4" => "video/mp4", "mpe" => "video/mpeg", "mpeg" => "video/mpeg", "mpg" => "video/mpeg", "mxu" => "video/vnd.mpegurl", "ogv" => "video/ogg", "qt" => "video/quicktime", "webm" => "video/webm");
120 // These are the ONLY allowed types in that these are the ones ANY reader must support, any other MUST have the fallback attribute pointing to one of these.
121 private $coreMediaTypes = array("image/gif", "image/jpeg", "image/png", "image/svg+xml", "application/xhtml+xml", "application/x-dtbook+xml", "application/xml", "application/x-dtbncx+xml", "text/css", "text/x-oeb1-css", "text/x-oeb1-document");
123 private $opsContentTypes = array("application/xhtml+xml", "application/x-dtbook+xml", "application/xml", "application/x-dtbncx+xml", "text/x-oeb1-document");
125 private $forbiddenCharacters = array("?", "[", "]", "/", "\\", "=", "<", ">", ":", ";", ",", "'", "\"", "&", "$", "#", "*", "(", ")", "|", "~", "`", "!", "{", "}", "%");
127 private $htmlContentHeader = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"\n \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n<title></title>\n</head>\n<body>\n";
128 private $htmlContentFooter = "</body>\n</html>\n";
135 function __construct($bookVersion = EPub
::BOOK_VERSION_EPUB2
, $languageCode = "en", $writingDirection = EPub
::DIRECTION_LEFT_TO_RIGHT
) {
136 include_once("Zip.php");
137 include_once("Logger.php");
139 $this->bookVersion
= $bookVersion;
140 $this->writingDirection
= $writingDirection;
141 $this->languageCode
= $languageCode;
143 $this->log
= new Logger("EPub", $this->isLogging
);
145 /* Prepare Logging. Just in case it's used. later */
146 if ($this->isLogging
) {
147 $this->log
->logLine("EPub class version....: " . self
::VERSION
);
148 $this->log
->logLine("EPub req. Zip version.: " . self
::REQ_ZIP_VERSION
);
149 $this->log
->logLine("Zip version...........: " . Zip
::VERSION
);
150 $this->log
->dumpInstalledModules();
153 if (!defined("Zip::VERSION") || Zip
::VERSION
< self
::REQ_ZIP_VERSION
) {
154 die("<p>EPub version " . self
::VERSION
. " requires Zip.php at version " . self
::REQ_ZIP_VERSION
. " or higher.<br />You can obtain the latest version from <a href=\"http://www.phpclasses.org/browse/package/6110.html\">http://www.phpclasses.org/browse/package/6110.html</a>.</p>");
157 include_once("EPubChapterSplitter.php");
158 include_once("EPub.HtmlEntities.php");
159 include_once("EPub.NCX.php");
160 include_once("EPub.OPF.php");
169 * @TODO make sure elements in the destructor match the current class elements
171 function __destruct() {
172 unset($this->bookVersion
, $this->maxImageWidth
, $this->maxImageHeight
);
173 unset($this->splitDefaultSize
, $this->isGifImagesEnabled
, $this->isReferencesAddedToToc
);
174 unset($this->zip
, $this->title
, $this->language
, $this->identifier
, $this->identifierType
);
175 unset($this->description
, $this->author
, $this->authorSortKey
, $this->publisherName
);
176 unset($this->publisherURL
, $this->date
, $this->rights
, $this->coverage
, $this->relation
);
177 unset($this->sourceURL
, $this->chapterCount
, $this->opf
, $this->ncx
, $this->isFinalized
);
178 unset($this->isCoverImageSet
, $this->fileList
, $this->writingDirection
, $this->languageCode
);
179 unset($this->referencesOrder
, $this->dateformat
, $this->dateformatShort
, $this->headerDateFormat
);
180 unset($this->isCurlInstalled
, $this->isGdInstalled
, $this->isExifInstalled
);
181 unset($this->isFileGetContentsInstalled
, $this->isFileGetContentsExtInstalled
, $this->bookRoot
);
182 unset($this->docRoot
, $this->EPubMark
, $this->generator
, $this->log
, $this->isLogging
);
183 unset($this->encodeHTML
, $this->mimetypes
, $this->coreMediaTypes
, $this->opsContentTypes
);
184 unset($this->forbiddenCharacters
, $this->htmlContentHeader
, $this->htmlContentFooter
);
185 unset($this->buildTOC
, $this->tocTitle
, $this->tocCSSClass
, $this->tocAddReferences
);
186 unset($this->tocFileName
, $this->tocCssFileName
);
190 * initialize defaults.
192 private function initialize() {
193 $this->referencesOrder
= array(
194 Reference
::COVER
=> "Cover Page",
195 Reference
::TITLE_PAGE
=> "Title Page",
196 Reference
::ACKNOWLEDGEMENTS
=> "Acknowledgements",
197 Reference
::BIBLIOGRAPHY
=> "Bibliography",
198 Reference
::COLOPHON
=> "Colophon",
199 Reference
::COPYRIGHT_PAGE
=> "Copyright",
200 Reference
::DEDICATION
=> "Dedication",
201 Reference
::EPIGRAPH
=> "Epigraph",
202 Reference
::FOREWORD
=> "Foreword",
203 Reference
::TABLE_OF_CONTENTS
=> "Table of Contents",
204 Reference
::NOTES
=> "Notes",
205 Reference
::PREFACE
=> "Preface",
206 Reference
::TEXT
=> "First Page",
207 Reference
::LIST_OF_ILLUSTRATIONS
=> "List of Illustrations",
208 Reference
::LIST_OF_TABLES
=> "List of Tables",
209 Reference
::GLOSSARY
=> "Glossary",
210 Reference
::INDEX
=> "Index");
212 $this->docRoot
= filter_input(INPUT_SERVER
, "DOCUMENT_ROOT") . "/";
214 $this->isCurlInstalled
= extension_loaded('curl') && function_exists('curl_version');
215 $this->isGdInstalled
= extension_loaded('gd') && function_exists('gd_info');
216 $this->isExifInstalled
= extension_loaded('exif') && function_exists('exif_imagetype');
217 $this->isFileGetContentsInstalled
= function_exists('file_get_contents');
218 $this->isFileGetContentsExtInstalled
= $this->isFileGetContentsInstalled
&& ini_get('allow_url_fopen');
220 $this->zip
= new Zip();
221 $this->zip
->setExtraField(FALSE);
222 $this->zip
->addFile("application/epub+zip", "mimetype");
223 $this->zip
->setExtraField(TRUE);
224 $this->zip
->addDirectory("META-INF");
226 $this->content
= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<container version=\"1.0\" xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\">\n\t<rootfiles>\n\t\t<rootfile full-path=\"" . $this->bookRoot
. "book.opf\" media-type=\"application/oebps-package+xml\" />\n\t</rootfiles>\n</container>\n";
228 if (!$this->isEPubVersion2()) {
229 $this->htmlContentHeader
= "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
230 . "<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\">\n"
232 . "<meta http-equiv=\"Default-Style\" content=\"text/html; charset=utf-8\" />\n"
233 . "<title></title>\n"
238 $this->zip
->addFile($this->content
, "META-INF/container.xml", 0, NULL, FALSE);
239 $this->content
= NULL;
240 $this->ncx
= new Ncx(NULL, NULL, NULL, $this->languageCode
, $this->writingDirection
);
241 $this->opf
= new Opf();
242 $this->ncx
->setVersion($this->bookVersion
);
243 $this->opf
->setVersion($this->bookVersion
);
244 $this->opf
->addItem("ncx", "book.ncx", Ncx
::MIMETYPE
);
245 $this->chapterCount
= 0;
249 * Add dynamically generated data as a file to the book.
251 * @param string $fileName Filename to use for the file, must be unique for the book.
252 * @param string $fileId Unique identifier for the file.
253 * @param string $fileData File data
254 * @param string $mimetype file mime type
255 * @return bool $success
257 function addFile($fileName, $fileId, $fileData, $mimetype) {
258 if ($this->isFinalized
|| array_key_exists($fileName, $this->fileList
)) {
262 $fileName = $this->normalizeFileName($fileName);
264 $compress = (strpos($mimetype, "image/") !== 0);
266 $this->zip
->addFile($fileData, $this->bookRoot
.$fileName, 0, NULL, $compress);
267 $this->fileList
[$fileName] = $fileName;
268 $this->opf
->addItem($fileId, $fileName, $mimetype);
273 * Add a large file directly from the filestystem to the book.
275 * @param string $fileName Filename to use for the file, must be unique for the book.
276 * @param string $fileId Unique identifier for the file.
277 * @param string $filePath File path
278 * @param string $mimetype file mime type
279 * @return bool $success
281 function addLargeFile($fileName, $fileId, $filePath, $mimetype) {
282 if ($this->isFinalized
|| array_key_exists($fileName, $this->fileList
)) {
285 $fileName = $this->normalizeFileName($fileName);
287 if ($this->zip
->addLargeFile($filePath, $this->bookRoot
.$fileName)) {
288 $this->fileList
[$fileName] = $fileName;
289 $this->opf
->addItem($fileId, $fileName, $mimetype);
296 * Add a CSS file to the book.
298 * @param string $fileName Filename to use for the CSS file, must be unique for the book.
299 * @param string $fileId Unique identifier for the file.
300 * @param string $fileData CSS data
301 * @param int $externalReferences How to handle external references, EPub::EXTERNAL_REF_IGNORE, EPub::EXTERNAL_REF_ADD or EPub::EXTERNAL_REF_REMOVE_IMAGES? See documentation for <code>processCSSExternalReferences</code> for explanation. Default is EPub::EXTERNAL_REF_IGNORE.
302 * @param string $baseDir Default is "", meaning it is pointing to the document root. NOT used if $externalReferences is set to EPub::EXTERNAL_REF_IGNORE.
304 * @return bool $success
306 function addCSSFile($fileName, $fileId, $fileData, $externalReferences = EPub
::EXTERNAL_REF_IGNORE
, $baseDir = "") {
307 if ($this->isFinalized
|| array_key_exists($fileName, $this->fileList
)) {
310 $fileName = Zip
::getRelativePath($fileName);
311 $fileName = preg_replace('#^[/\.]+#i', "", $fileName);
313 if ($externalReferences !== EPub
::EXTERNAL_REF_IGNORE
) {
314 $cssDir = pathinfo($fileName);
315 $cssDir = preg_replace('#^[/\.]+#i', "", $cssDir["dirname"] . "/");
316 if (!empty($cssDir)) {
317 $cssDir = preg_replace('#[^/]+/#i', "../", $cssDir);
320 $this->processCSSExternalReferences($fileData, $externalReferences, $baseDir, $cssDir);
323 $this->addFile($fileName, "css_" . $fileId, $fileData, "text/css");
329 * Add a chapter to the book, as a chapter should not exceed 250kB, you can parse an array with multiple parts as $chapterData.
330 * These will still only show up as a single chapter in the book TOC.
332 * @param string $chapterName Name of the chapter, will be use din the TOC
333 * @param string $fileName Filename to use for the chapter, must be unique for the book.
334 * @param string $chapter Chapter text in XHTML or array $chapterData valid XHTML data for the chapter. File should NOT exceed 250kB.
335 * @param bool $autoSplit Should the chapter be split if it exceeds the default split size? Default=FALSE, only used if $chapterData is a string.
336 * @param int $externalReferences How to handle external references, EPub::EXTERNAL_REF_IGNORE, EPub::EXTERNAL_REF_ADD or EPub::EXTERNAL_REF_REMOVE_IMAGES? See documentation for <code>processChapterExternalReferences</code> for explanation. Default is EPub::EXTERNAL_REF_IGNORE.
337 * @param string $baseDir Default is "", meaning it is pointing to the document root. NOT used if $externalReferences is set to EPub::EXTERNAL_REF_IGNORE.
338 * @return mixed $success FALSE if the addition failed, else the new NavPoint.
340 function addChapter($chapterName, $fileName, $chapterData = NULL, $autoSplit = FALSE, $externalReferences = EPub
::EXTERNAL_REF_IGNORE
, $baseDir = "") {
341 if ($this->isFinalized
) {
344 $fileName = Zip
::getRelativePath($fileName);
345 $fileName = preg_replace('#^[/\.]+#i', "", $fileName);
346 $fileName = $this->sanitizeFileName($fileName);
348 $chapter = $chapterData;
349 if ($autoSplit && is_string($chapterData) && mb_strlen($chapterData) > $this->splitDefaultSize
) {
350 $splitter = new EPubChapterSplitter();
352 $chapterArray = $splitter->splitChapter($chapterData);
353 if (count($chapterArray) > 1) {
354 $chapter = $chapterArray;
358 if (!empty($chapter) && is_string($chapter)) {
359 if ($externalReferences !== EPub
::EXTERNAL_REF_IGNORE
) {
360 $htmlDirInfo = pathinfo($fileName);
361 $htmlDir = preg_replace('#^[/\.]+#i', "", $htmlDirInfo["dirname"] . "/");
362 $this->processChapterExternalReferences($chapter, $externalReferences, $baseDir, $htmlDir);
365 if ($this->encodeHTML
=== TRUE) {
366 $chapter = $this->encodeHtml($chapter);
369 $this->chapterCount++
;
370 $this->addFile($fileName, "chapter" . $this->chapterCount
, $chapter, "application/xhtml+xml");
371 $this->opf
->addItemRef("chapter" . $this->chapterCount
);
373 $navPoint = new NavPoint($this->decodeHtmlEntities($chapterName), $fileName, "chapter" . $this->chapterCount
);
374 $this->ncx
->addNavPoint($navPoint);
375 $this->ncx
->chapterList
[$chapterName] = $navPoint;
376 } else if (is_array($chapter)) {
377 $fileNameParts = pathinfo($fileName);
378 $extension = $fileNameParts['extension'];
379 $name = $fileNameParts['filename'];
382 $this->chapterCount++
;
384 $oneChapter = each($chapter);
385 while ($oneChapter) {
386 list($k, $v) = $oneChapter;
387 if ($this->encodeHTML
=== TRUE) {
388 $v = $this->encodeHtml($v);
391 if ($externalReferences !== EPub
::EXTERNAL_REF_IGNORE
) {
392 $this->processChapterExternalReferences($v, $externalReferences, $baseDir);
395 $partName = $name . "_" . $partCount;
396 $this->addFile($partName . "." . $extension, $partName, $v, "application/xhtml+xml");
397 $this->opf
->addItemRef($partName);
399 $oneChapter = each($chapter);
401 $partName = $name . "_1." . $extension;
402 $navPoint = new NavPoint($this->decodeHtmlEntities($chapterName), $partName, $partName);
403 $this->ncx
->addNavPoint($navPoint);
405 $this->ncx
->chapterList
[$chapterName] = $navPoint;
406 } else if (!isset($chapterData) && strpos($fileName, "#") > 0) {
407 $this->chapterCount++
;
408 //$this->opf->addItemRef("chapter" . $this->chapterCount);
410 $navPoint = new NavPoint($this->decodeHtmlEntities($chapterName), $fileName, "chapter" . $this->chapterCount
);
411 $this->ncx
->addNavPoint($navPoint);
412 $this->ncx
->chapterList
[$chapterName] = $navPoint;
413 } else if (!isset($chapterData) && $fileName=="TOC.xhtml") {
414 $this->chapterCount++
;
415 $this->opf
->addItemRef("toc");
417 $navPoint = new NavPoint($this->decodeHtmlEntities($chapterName), $fileName, "chapter" . $this->chapterCount
);
418 $this->ncx
->addNavPoint($navPoint);
419 $this->ncx
->chapterList
[$chapterName] = $navPoint;
425 * Add one chapter level.
427 * Subsequent chapters will be added to this level.
429 * @param string $navTitle
430 * @param string $navId
431 * @param string $navClass
432 * @param int $isNavHidden
433 * @param string $writingDirection
434 * @return NavPoint The new NavPoint for that level.
436 function subLevel($navTitle = NULL, $navId = NULL, $navClass = NULL, $isNavHidden = FALSE, $writingDirection = NULL) {
437 return $this->ncx
->subLevel($this->decodeHtmlEntities($navTitle), $navId, $navClass, $isNavHidden, $writingDirection);
441 * Step back one chapter level.
443 * Subsequent chapters will be added to this chapters parent level.
445 function backLevel() {
446 $this->ncx
->backLevel();
450 * Step back to the root level.
452 * Subsequent chapters will be added to the rooot NavMap.
454 function rootLevel() {
455 $this->ncx
->rootLevel();
459 * Step back to the given level.
460 * Useful for returning to a previous level from deep within the structure.
461 * Values below 2 will have the same effect as rootLevel()
463 * @param int $newLevel
465 function setCurrentLevel($newLevel) {
466 $this->ncx
->setCurrentLevel($newLevel);
470 * Get current level count.
471 * The indentation of the current structure point.
473 * @return current level count;
475 function getCurrentLevel() {
476 return $this->ncx
->getCurrentLevel();
480 * Wrap ChapterContent with Head and Footer
483 * @return string $content
485 private function wrapChapter($content) {
486 return $this->htmlContentHeader
. "\n" . $content . "\n" . $this->htmlContentFooter
;
490 * Reference pages is usually one or two pages for items such as Table of Contents, reference lists, Author notes or Acknowledgements.
491 * These do not show up in the regular navigation list.
493 * As they are supposed to be short.
495 * @param string $pageName Name of the chapter, will be use din the TOC
496 * @param string $fileName Filename to use for the chapter, must be unique for the book.
497 * @param string $pageData Page content in XHTML. File should NOT exceed 250kB.
498 * @param string $reference Reference key
499 * @param int $externalReferences How to handle external references. See documentation for <code>processChapterExternalReferences</code> for explanation. Default is EPub::EXTERNAL_REF_IGNORE.
500 * @param string $baseDir Default is "", meaning it is pointing to the document root. NOT used if $externalReferences is set to EPub::EXTERNAL_REF_IGNORE.
501 * @return bool $success
503 function addReferencePage($pageName, $fileName, $pageData, $reference, $externalReferences = EPub
::EXTERNAL_REF_IGNORE
, $baseDir = "") {
504 if ($this->isFinalized
) {
507 $fileName = Zip
::getRelativePath($fileName);
508 $fileName = preg_replace('#^[/\.]+#i', "", $fileName);
511 if (!empty($pageData) && is_string($pageData)) {
512 if ($this->encodeHTML
=== TRUE) {
513 $pageData = $this->encodeHtml($pageData);
516 $this->wrapChapter($pageData);
518 if ($externalReferences !== EPub
::EXTERNAL_REF_IGNORE
) {
519 $htmlDirInfo = pathinfo($fileName);
520 $htmlDir = preg_replace('#^[/\.]+#i', "", $htmlDirInfo["dirname"] . "/");
521 $this->processChapterExternalReferences($pageData, $externalReferences, $baseDir, $htmlDir);
524 $this->addFile($fileName, "ref_" . $reference, $pageData, "application/xhtml+xml");
526 if ($reference !== Reference
::TABLE_OF_CONTENTS
|| !isset($this->ncx
->referencesList
[$reference])) {
527 $this->opf
->addItemRef("ref_" . $reference, FALSE);
528 $this->opf
->addReference($reference, $pageName, $fileName);
530 $this->ncx
->referencesList
[$reference] = $fileName;
531 $this->ncx
->referencesName
[$reference] = $pageName;
539 * Add custom metadata to the book.
541 * It is up to the builder to make sure there are no collisions. Metadata are just key value pairs.
543 * @param string $name
544 * @param string $content
546 function addCustomMetadata($name, $content) {
547 $this->opf
->addMeta($name, $content);
551 * Add DublinCore metadata to the book
553 * Use the DublinCore constants included in EPub, ie DublinCore::DATE
555 * @param string $dublinCore name
556 * @param string $value
558 function addDublinCoreMetadata($dublinCoreConstant, $value) {
559 if ($this->isFinalized
) {
563 $this->opf
->addDCMeta($dublinCoreConstant, $this->decodeHtmlEntities($value));
567 * Add a cover image to the book.
568 * If the $imageData is not set, the function assumes the $fileName is the path to the image file.
570 * The styling and structure of the generated XHTML is heavily inspired by the XHTML generated by Calibre.
572 * @param string $fileName Filename to use for the image, must be unique for the book.
573 * @param string $imageData Binary image data
574 * @param string $mimetype Image mimetype, such as "image/jpeg" or "image/png".
575 * @return bool $success
577 function setCoverImage($fileName, $imageData = NULL, $mimetype = NULL,$bookTitle) {
578 if ($this->isFinalized
|| $this->isCoverImageSet
|| array_key_exists("CoverPage.html", $this->fileList
)) {
582 if ($imageData == NULL) {
583 // assume $fileName is the valid file path.
584 if (!file_exists($fileName)) {
585 // Attempt to locate the file using the doc root.
586 $rp = realpath($this->docRoot
. "/" . $fileName);
589 // only assign the docroot path if it actually exists there.
593 $image = $this->getImage($fileName);
594 $imageData = $image['image'];
595 $mimetype = $image['mime'];
596 $fileName = preg_replace("#\.[^\.]+$#", "." . $image['ext'], $fileName);
600 $path = pathinfo($fileName);
601 $imgPath = "images/" . $path["basename"];
603 if (empty($mimetype) && file_exists($fileName)) {
604 list($width, $height, $type, $attr) = getimagesize($fileName);
605 $mimetype = image_type_to_mime_type($type);
607 if (empty($mimetype)) {
608 $ext = strtolower($path['extension']);
612 $mimetype = "image/" . $ext;
617 if ($this->isEPubVersion2()) {
618 $coverPage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
619 . "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"\n"
620 . " \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n"
621 . "<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\" xml:lang=\"en\">\n"
623 . "\t\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>\n"
624 . "\t\t<title>Cover Image</title>\n"
625 . "\t\t<link type=\"text/css\" rel=\"stylesheet\" href=\"Styles/CoverPage.css\" />\n"
628 . "\t" . $bookTitle . "\n"
630 . "\t\t\t<img src=\"" . $imgPath . "\" alt=\"Cover image\" style=\"height: 100%\"/>\n"
635 $coverPage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
636 . "<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\">\n"
638 . "\t<meta http-equiv=\"Default-Style\" content=\"text/html; charset=utf-8\" />\n"
639 . "\t\t<title>Cover Image</title>\n"
640 . "\t\t<link type=\"text/css\" rel=\"stylesheet\" href=\"Styles/CoverPage.css\" />\n"
643 . "\t\t<section epub:type=\"cover\">\n"
644 . "\t" . $bookTitle . "\n"
645 . "\t\t\t<img src=\"" . $imgPath . "\" alt=\"Cover image\" style=\"height: 30%\"/>\n"
650 $coverPageCss = "@page, body, div, img {\n"
651 . "\tpadding: 0pt;\n"
654 . "\ttext-align: center;\n"
657 $this->addCSSFile("Styles/CoverPage.css", "CoverPageCss", $coverPageCss);
658 $this->addFile($imgPath, "CoverImage", $imageData, $mimetype);
659 $this->addReferencePage("CoverPage", "CoverPage.xhtml", $coverPage, "cover");
660 $this->isCoverImageSet
= TRUE;
665 * Process external references from a HTML to the book. The chapter itself is not stored.
666 * the HTML is scanned for <link..., <style..., and <img tags.
667 * Embedded CSS styles and links will also be processed.
668 * Script tags are not processed, as scripting should be avoided in e-books.
670 * EPub keeps track of added files, and duplicate files referenced across multiple
671 * chapters, are only added once.
673 * If the $doc is a string, it is assumed to be the content of an HTML file,
674 * else is it assumes to be a DOMDocument.
676 * Basedir is the root dir the HTML is supposed to "live" in, used to resolve
677 * relative references such as <code><img src="../images/image.png"/></code>
679 * $externalReferences determines how the function will handle external references.
681 * @param mixed &$doc (referenced)
682 * @param int $externalReferences How to handle external references, EPub::EXTERNAL_REF_IGNORE, EPub::EXTERNAL_REF_ADD or EPub::EXTERNAL_REF_REMOVE_IMAGES? Default is EPub::EXTERNAL_REF_ADD.
683 * @param string $baseDir Default is "", meaning it is pointing to the document root.
684 * @param string $htmlDir The path to the parent HTML file's directory from the root of the archive.
686 * @return bool FALSE if uncuccessful (book is finalized or $externalReferences == EXTERNAL_REF_IGNORE).
688 protected function processChapterExternalReferences(&$doc, $externalReferences = EPub
::EXTERNAL_REF_ADD
, $baseDir = "", $htmlDir = "") {
689 if ($this->isFinalized
|| $externalReferences === EPub
::EXTERNAL_REF_IGNORE
) {
693 $backPath = preg_replace('#[^/]+/#i', "../", $htmlDir);
694 $isDocAString = is_string($doc);
698 $xmlDoc = new DOMDocument();
699 @$xmlDoc->loadHTML($doc);
704 $this->processChapterStyles($xmlDoc, $externalReferences, $baseDir, $htmlDir);
705 $this->processChapterLinks($xmlDoc, $externalReferences, $baseDir, $htmlDir, $backPath);
706 $this->processChapterImages($xmlDoc, $externalReferences, $baseDir, $htmlDir, $backPath);
707 $this->processChapterSources($xmlDoc, $externalReferences, $baseDir, $htmlDir, $backPath);
710 //$html = $xmlDoc->saveXML();
712 $htmlNode = $xmlDoc->getElementsByTagName("html");
713 $headNode = $xmlDoc->getElementsByTagName("head");
714 $bodyNode = $xmlDoc->getElementsByTagName("body");
717 for ($index = 0; $index < $htmlNode->item(0)->attributes
->length
; $index++
) {
718 $nodeName = $htmlNode->item(0)->attributes
->item($index)->nodeName
;
719 $nodeValue = $htmlNode->item(0)->attributes
->item($index)->nodeValue
;
721 if ($nodeName != "xmlns") {
722 $htmlNS .= " $nodeName=\"$nodeValue\"";
726 $xml = new DOMDocument('1.0', "utf-8");
727 $xml->lookupPrefix("http://www.w3.org/1999/xhtml");
728 $xml->preserveWhiteSpace
= FALSE;
729 $xml->formatOutput
= TRUE;
731 $xml2Doc = new DOMDocument('1.0', "utf-8");
732 $xml2Doc->lookupPrefix("http://www.w3.org/1999/xhtml");
733 $xml2Doc->loadXML("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"\n \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\"$htmlNS>\n</html>\n");
734 $html = $xml2Doc->getElementsByTagName("html")->item(0);
735 $html->appendChild($xml2Doc->importNode($headNode->item(0), TRUE));
736 $html->appendChild($xml2Doc->importNode($bodyNode->item(0), TRUE));
738 // force pretty printing and correct formatting, should not be needed, but it is.
739 $xml->loadXML($xml2Doc->saveXML());
740 $doc = $xml->saveXML();
742 if (!$this->isEPubVersion2()) {
743 $doc = preg_replace('#^\s*<!DOCTYPE\ .+?>\s*#im', '', $doc);
750 * Process images referenced from an CSS file to the book.
752 * $externalReferences determins how the function will handle external references.
754 * @param string &$cssFile (referenced)
755 * @param int $externalReferences How to handle external references, EPub::EXTERNAL_REF_IGNORE, EPub::EXTERNAL_REF_ADD or EPub::EXTERNAL_REF_REMOVE_IMAGES? Default is EPub::EXTERNAL_REF_ADD.
756 * @param string $baseDir Default is "", meaning it is pointing to the document root.
757 * @param string $cssDir The of the CSS file's directory from the root of the archive.
759 * @return bool FALSE if unsuccessful (book is finalized or $externalReferences == EXTERNAL_REF_IGNORE).
761 protected function processCSSExternalReferences(&$cssFile, $externalReferences = EPub
::EXTERNAL_REF_ADD
, $baseDir = "", $cssDir = "") {
762 if ($this->isFinalized
|| $externalReferences === EPub
::EXTERNAL_REF_IGNORE
) {
766 $backPath = preg_replace('#[^/]+/#i', "../", $cssDir);
768 preg_match_all('#url\s*\([\'\"\s]*(.+?)[\'\"\s]*\)#im', $cssFile, $imgs, PREG_SET_ORDER
);
770 $itemCount = count($imgs);
771 for ($idx = 0; $idx < $itemCount; $idx++
) {
773 if ($externalReferences === EPub
::EXTERNAL_REF_REMOVE_IMAGES
|| $externalReferences === EPub
::EXTERNAL_REF_REPLACE_IMAGES
) {
774 $cssFile = str_replace($img[0], "", $cssFile);
778 $pathData = pathinfo($source);
779 $internalSrc = $pathData['basename'];
781 $isSourceExternal = FALSE;
783 if ($this->resolveImage($source, $internalPath, $internalSrc, $isSourceExternal, $baseDir, $cssDir, $backPath)) {
784 $cssFile = str_replace($img[0], "url('" . $backPath . $internalPath . "')", $cssFile);
785 } else if ($isSourceExternal) {
786 $cssFile = str_replace($img[0], "", $cssFile); // External image is missing
787 } // else do nothing, if the image is local, and missing, assume it's been generated.
794 * Process style tags in a DOMDocument. Styles will be passed as CSS files and reinserted into the document.
796 * @param DOMDocument &$xmlDoc (referenced)
797 * @param int $externalReferences How to handle external references, EPub::EXTERNAL_REF_IGNORE, EPub::EXTERNAL_REF_ADD or EPub::EXTERNAL_REF_REMOVE_IMAGES? Default is EPub::EXTERNAL_REF_ADD.
798 * @param string $baseDir Default is "", meaning it is pointing to the document root.
799 * @param string $htmlDir The path to the parent HTML file's directory from the root of the archive.
801 * @return bool FALSE if uncuccessful (book is finalized or $externalReferences == EXTERNAL_REF_IGNORE).
803 protected function processChapterStyles(&$xmlDoc, $externalReferences = EPub
::EXTERNAL_REF_ADD
, $baseDir = "", $htmlDir = "") {
804 if ($this->isFinalized
|| $externalReferences === EPub
::EXTERNAL_REF_IGNORE
) {
807 // process inlined CSS styles in style tags.
808 $styles = $xmlDoc->getElementsByTagName("style");
809 $styleCount = $styles->length
;
810 for ($styleIdx = 0; $styleIdx < $styleCount; $styleIdx++
) {
811 $style = $styles->item($styleIdx);
813 $styleData = preg_replace('#[/\*\s]*\<\!\[CDATA\[[\s\*/]*#im', "", $style->nodeValue
);
814 $styleData = preg_replace('#[/\*\s]*\]\]\>[\s\*/]*#im', "", $styleData);
816 $this->processCSSExternalReferences($styleData, $externalReferences, $baseDir, $htmlDir);
817 $style->nodeValue
= "\n" . trim($styleData) . "\n";
823 * Process link tags in a DOMDocument. Linked files will be loaded into the archive, and the link src will be rewritten to point to that location.
824 * Link types text/css will be passed as CSS files.
826 * @param DOMDocument &$xmlDoc (referenced)
827 * @param int $externalReferences How to handle external references, EPub::EXTERNAL_REF_IGNORE, EPub::EXTERNAL_REF_ADD or EPub::EXTERNAL_REF_REMOVE_IMAGES? Default is EPub::EXTERNAL_REF_ADD.
828 * @param string $baseDir Default is "", meaning it is pointing to the document root.
829 * @param string $htmlDir The path to the parent HTML file's directory from the root of the archive.
830 * @param string $backPath The path to get back to the root of the archive from $htmlDir.
832 * @return bool FALSE if uncuccessful (book is finalized or $externalReferences == EXTERNAL_REF_IGNORE).
834 protected function processChapterLinks(&$xmlDoc, $externalReferences = EPub
::EXTERNAL_REF_ADD
, $baseDir = "", $htmlDir = "", $backPath = "") {
835 if ($this->isFinalized
|| $externalReferences === EPub
::EXTERNAL_REF_IGNORE
) {
838 // process link tags.
839 $links = $xmlDoc->getElementsByTagName("link");
840 $linkCount = $links->length
;
841 for ($linkIdx = 0; $linkIdx < $linkCount; $linkIdx++
) {
842 $link = $links->item($linkIdx);
843 $source = $link->attributes
->getNamedItem("href")->nodeValue
;
846 $pathData = pathinfo($source);
847 $internalSrc = $pathData['basename'];
849 if (preg_match('#^(http|ftp)s?://#i', $source) == 1) {
850 $urlinfo = parse_url($source);
852 if (strpos($urlinfo['path'], $baseDir."/") !== FALSE) {
853 $internalSrc = substr($urlinfo['path'], strpos($urlinfo['path'], $baseDir."/") +
strlen($baseDir) +
1);
856 @$sourceData = getFileContents($source);
857 } else if (strpos($source, "/") === 0) {
858 @$sourceData = file_get_contents($this->docRoot
. $source);
860 @$sourceData = file_get_contents($this->docRoot
. $baseDir . "/" . $source);
863 if (!empty($sourceData)) {
864 if (!array_key_exists($internalSrc, $this->fileList
)) {
865 $mime = $link->attributes
->getNamedItem("type")->nodeValue
;
867 $mime = "text/plain";
869 if ($mime == "text/css") {
870 $this->processCSSExternalReferences($sourceData, $externalReferences, $baseDir, $htmlDir);
871 $this->addCSSFile($internalSrc, $internalSrc, $sourceData, EPub
::EXTERNAL_REF_IGNORE
, $baseDir);
872 $link->setAttribute("href", $backPath . $internalSrc);
874 $this->addFile($internalSrc, $internalSrc, $sourceData, $mime);
876 $this->fileList
[$internalSrc] = $source;
878 $link->setAttribute("href", $backPath . $internalSrc);
880 } // else do nothing, if the link is local, and missing, assume it's been generated.
886 * Process img tags in a DOMDocument.
887 * $externalReferences will determine what will happen to these images, and the img src will be rewritten accordingly.
889 * @param DOMDocument &$xmlDoc (referenced)
890 * @param int $externalReferences How to handle external references, EPub::EXTERNAL_REF_IGNORE, EPub::EXTERNAL_REF_ADD or EPub::EXTERNAL_REF_REMOVE_IMAGES? Default is EPub::EXTERNAL_REF_ADD.
891 * @param string $baseDir Default is "", meaning it is pointing to the document root.
892 * @param string $htmlDir The path to the parent HTML file's directory from the root of the archive.
893 * @param string $backPath The path to get back to the root of the archive from $htmlDir.
895 * @return bool FALSE if uncuccessful (book is finalized or $externalReferences == EXTERNAL_REF_IGNORE).
897 protected function processChapterImages(&$xmlDoc, $externalReferences = EPub
::EXTERNAL_REF_ADD
, $baseDir = "", $htmlDir = "", $backPath = "") {
898 if ($this->isFinalized
|| $externalReferences === EPub
::EXTERNAL_REF_IGNORE
) {
902 $postProcDomElememts = array();
903 $images = $xmlDoc->getElementsByTagName("img");
904 $itemCount = $images->length
;
906 for ($idx = 0; $idx < $itemCount; $idx++
) {
907 $img = $images->item($idx);
909 if ($externalReferences === EPub
::EXTERNAL_REF_REMOVE_IMAGES
) {
910 $postProcDomElememts[] = $img;
911 } else if ($externalReferences === EPub
::EXTERNAL_REF_REPLACE_IMAGES
) {
912 $altNode = $img->attributes
->getNamedItem("alt");
914 if ($altNode !== NULL && strlen($altNode->nodeValue
) > 0) {
915 $alt = $altNode->nodeValue
;
917 $postProcDomElememts[] = array($img, $this->createDomFragment($xmlDoc, "<em>[" . $alt . "]</em>"));
919 $source = $img->attributes
->getNamedItem("src")->nodeValue
;
921 $parsedSource = parse_url($source);
922 $internalSrc = $this->sanitizeFileName(urldecode(pathinfo($parsedSource['path'], PATHINFO_BASENAME
)));
924 $isSourceExternal = FALSE;
926 if ($this->resolveImage($source, $internalPath, $internalSrc, $isSourceExternal, $baseDir, $htmlDir, $backPath)) {
927 $img->setAttribute("src", $backPath . $internalPath);
928 } else if ($isSourceExternal) {
929 $postProcDomElememts[] = $img; // External image is missing
930 } // else do nothing, if the image is local, and missing, assume it's been generated.
934 foreach ($postProcDomElememts as $target) {
935 if (is_array($target)) {
936 $target[0]->parentNode
->replaceChild($target[1], $target[0]);
938 $target->parentNode
->removeChild($target);
945 * Process source tags in a DOMDocument.
946 * $externalReferences will determine what will happen to these images, and the img src will be rewritten accordingly.
948 * @param DOMDocument &$xmlDoc (referenced)
949 * @param int $externalReferences How to handle external references, EPub::EXTERNAL_REF_IGNORE, EPub::EXTERNAL_REF_ADD or EPub::EXTERNAL_REF_REMOVE_IMAGES? Default is EPub::EXTERNAL_REF_ADD.
950 * @param string $baseDir Default is "", meaning it is pointing to the document root.
951 * @param string $htmlDir The path to the parent HTML file's directory from the root of the archive.
952 * @param string $backPath The path to get back to the root of the archive from $htmlDir.
954 * @return bool FALSE if uncuccessful (book is finalized or $externalReferences == EXTERNAL_REF_IGNORE).
956 protected function processChapterSources(&$xmlDoc, $externalReferences = EPub
::EXTERNAL_REF_ADD
, $baseDir = "", $htmlDir = "", $backPath = "") {
957 if ($this->isFinalized
|| $externalReferences === EPub
::EXTERNAL_REF_IGNORE
) {
961 if ($this->bookVersion
!== EPub
::BOOK_VERSION_EPUB3
) {
962 // ePub 2 does not support multimedia formats, and they must be removed.
963 $externalReferences = EPub
::EXTERNAL_REF_REMOVE_IMAGES
;
966 $postProcDomElememts = array();
967 $images = $xmlDoc->getElementsByTagName("source");
968 $itemCount = $images->length
;
969 for ($idx = 0; $idx < $itemCount; $idx++
) {
970 $img = $images->item($idx);
971 if ($externalReferences === EPub
::EXTERNAL_REF_REMOVE_IMAGES
) {
972 $postProcDomElememts[] = $img;
973 } else if ($externalReferences === EPub
::EXTERNAL_REF_REPLACE_IMAGES
) {
974 $altNode = $img->attributes
->getNamedItem("alt");
976 if ($altNode !== NULL && strlen($altNode->nodeValue
) > 0) {
977 $alt = $altNode->nodeValue
;
979 $postProcDomElememts[] = array($img, $this->createDomFragment($xmlDoc, "[" . $alt . "]"));
981 $source = $img->attributes
->getNamedItem("src")->nodeValue
;
983 $parsedSource = parse_url($source);
984 $internalSrc = $this->sanitizeFileName(urldecode(pathinfo($parsedSource['path'], PATHINFO_BASENAME
)));
986 $isSourceExternal = FALSE;
988 if ($this->resolveMedia($source, $internalPath, $internalSrc, $isSourceExternal, $baseDir, $htmlDir, $backPath)) {
989 $img->setAttribute("src", $backPath . $internalPath);
990 } else if ($isSourceExternal) {
991 $postProcDomElememts[] = $img; // External image is missing
992 } // else do nothing, if the image is local, and missing, assume it's been generated.
998 * Resolve an image src and determine it's target location and add it to the book.
1000 * @param string $source Image Source link.
1001 * @param string &$internalPath (referenced) Return value, will be set to the target path and name in the book.
1002 * @param string &$internalSrc (referenced) Return value, will be set to the target name in the book.
1003 * @param string &$isSourceExternal (referenced) Return value, will be set to TRUE if the image originated from a full URL.
1004 * @param string $baseDir Default is "", meaning it is pointing to the document root.
1005 * @param string $htmlDir The path to the parent HTML file's directory from the root of the archive.
1006 * @param string $backPath The path to get back to the root of the archive from $htmlDir.
1008 protected function resolveImage($source, &$internalPath, &$internalSrc, &$isSourceExternal, $baseDir = "", $htmlDir = "", $backPath = "") {
1009 if ($this->isFinalized
) {
1014 if (preg_match('#^(http|ftp)s?://#i', $source) == 1) {
1015 $urlinfo = parse_url($source);
1016 $urlPath = pathinfo($urlinfo['path']);
1018 if (strpos($urlinfo['path'], $baseDir."/") !== FALSE) {
1019 $internalSrc = $this->sanitizeFileName(urldecode(substr($urlinfo['path'], strpos($urlinfo['path'], $baseDir."/") +
strlen($baseDir) +
1)));
1021 $internalPath = $urlinfo["scheme"] . "/" . $urlinfo["host"] . "/" . pathinfo($urlinfo["path"], PATHINFO_DIRNAME
);
1022 $isSourceExternal = TRUE;
1023 $imageData = $this->getImage($source);
1024 } else if (strpos($source, "/") === 0) {
1025 $internalPath = pathinfo($source, PATHINFO_DIRNAME
);
1028 if (!file_exists($path)) {
1029 $path = $this->docRoot
. $path;
1032 $imageData = $this->getImage($path);
1034 $internalPath = $htmlDir . "/" . preg_replace('#^[/\.]+#', '', pathinfo($source, PATHINFO_DIRNAME
));
1036 $path = $baseDir . "/" . $source;
1037 if (!file_exists($path)) {
1038 $path = $this->docRoot
. $path;
1041 $imageData = $this->getImage($path);
1043 if ($imageData !== FALSE) {
1044 $iSrcInfo = pathinfo($internalSrc);
1045 if (!empty($imageData['ext']) && $imageData['ext'] != $iSrcInfo['extension']) {
1046 $internalSrc = $iSrcInfo['filename'] . "." . $imageData['ext'];
1048 $internalPath = Zip
::getRelativePath("images/" . $internalPath . "/" . $internalSrc);
1049 if (!array_key_exists($internalPath, $this->fileList
)) {
1050 $this->addFile($internalPath, "i_" . $internalSrc, $imageData['image'], $imageData['mime']);
1051 $this->fileList
[$internalPath] = $source;
1059 * Resolve a media src and determine it's target location and add it to the book.
1061 * @param string $source Source link.
1062 * @param string $internalPath (referenced) Return value, will be set to the target path and name in the book.
1063 * @param string $internalSrc (referenced) Return value, will be set to the target name in the book.
1064 * @param string $isSourceExternal (referenced) Return value, will be set to TRUE if the image originated from a full URL.
1065 * @param string $baseDir Default is "", meaning it is pointing to the document root.
1066 * @param string $htmlDir The path to the parent HTML file's directory from the root of the archive.
1067 * @param string $backPath The path to get back to the root of the archive from $htmlDir.
1069 protected function resolveMedia($source, &$internalPath, &$internalSrc, &$isSourceExternal, $baseDir = "", $htmlDir = "", $backPath = "") {
1070 if ($this->isFinalized
) {
1076 if (preg_match('#^(http|ftp)s?://#i', $source) == 1) {
1077 $urlinfo = parse_url($source);
1079 if (strpos($urlinfo['path'], $baseDir."/") !== FALSE) {
1080 $internalSrc = substr($urlinfo['path'], strpos($urlinfo['path'], $baseDir."/") +
strlen($baseDir) +
1);
1082 $internalPath = $urlinfo["scheme"] . "/" . $urlinfo["host"] . "/" . pathinfo($urlinfo["path"], PATHINFO_DIRNAME
);
1083 $isSourceExternal = TRUE;
1084 $mediaPath = $this->getFileContents($source, true);
1085 $tmpFile = $mediaPath;
1086 } else if (strpos($source, "/") === 0) {
1087 $internalPath = pathinfo($source, PATHINFO_DIRNAME
);
1089 $mediaPath = $source;
1090 if (!file_exists($mediaPath)) {
1091 $mediaPath = $this->docRoot
. $mediaPath;
1094 $internalPath = $htmlDir . "/" . preg_replace('#^[/\.]+#', '', pathinfo($source, PATHINFO_DIRNAME
));
1096 $mediaPath = $baseDir . "/" . $source;
1097 if (!file_exists($mediaPath)) {
1098 $mediaPath = $this->docRoot
. $mediaPath;
1102 if ($mediaPath !== FALSE) {
1103 $mime = $this->getMime($source);
1104 $internalPath = Zip
::getRelativePath("media/" . $internalPath . "/" . $internalSrc);
1106 if (!array_key_exists($internalPath, $this->fileList
) &&
1107 $this->addLargeFile($internalPath, "m_" . $internalSrc, $mediaPath, $mime)) {
1108 $this->fileList
[$internalPath] = $source;
1110 if (isset($tmpFile)) {
1119 * Get Book Chapter count.
1122 * @return number of chapters
1124 function getChapterCount() {
1125 return $this->chapterCount
;
1129 * Book title, mandatory.
1131 * Used for the dc:title metadata parameter in the OPF file as well as the DocTitle attribute in the NCX file.
1133 * @param string $title
1135 * @return bool $success
1137 function setTitle($title) {
1138 if ($this->isFinalized
) {
1141 $this->title
= $title;
1151 function getTitle() {
1152 return $this->title
;
1156 * Book language, mandatory
1158 * Use the RFC3066 Language codes, such as "en", "da", "fr" etc.
1161 * Used for the dc:language metadata parameter in the OPF file.
1163 * @param string $language
1165 * @return bool $success
1167 function setLanguage($language) {
1168 if ($this->isFinalized
|| mb_strlen($language) != 2) {
1171 $this->language
= $language;
1176 * Get Book language.
1181 function getLanguage() {
1182 return $this->language
;
1186 * Unique book identifier, mandatory.
1187 * Use the URI, or ISBN if available.
1189 * An unambiguous reference to the resource within a given context.
1191 * Recommended best practice is to identify the resource by means of a
1192 * string conforming to a formal identification system.
1194 * Used for the dc:identifier metadata parameter in the OPF file, as well
1195 * as dtb:uid in the NCX file.
1197 * Identifier type should only be:
1198 * EPub::IDENTIFIER_URI
1199 * EPub::IDENTIFIER_ISBN
1200 * EPub::IDENTIFIER_UUID
1202 * @param string $identifier
1203 * @param string $identifierType
1205 * @return bool $success
1207 function setIdentifier($identifier, $identifierType) {
1208 if ($this->isFinalized
|| ($identifierType !== EPub
::IDENTIFIER_URI
&& $identifierType !== EPub
::IDENTIFIER_ISBN
&& $identifierType !== EPub
::IDENTIFIER_UUID
)) {
1211 $this->identifier
= $identifier;
1212 $this->identifierType
= $identifierType;
1217 * Get Book identifier.
1220 * @return $identifier
1222 function getIdentifier() {
1223 return $this->identifier
;
1227 * Get Book identifierType.
1230 * @return $identifierType
1232 function getIdentifierType() {
1233 return $this->identifierType
;
1237 * Book description, optional.
1239 * An account of the resource.
1241 * Description may include but is not limited to: an abstract, a table of
1242 * contents, a graphical representation, or a free-text account of the
1245 * Used for the dc:source metadata parameter in the OPF file
1247 * @param string $description
1249 * @return bool $success
1251 function setDescription($description) {
1252 if ($this->isFinalized
) {
1255 $this->description
= $description;
1260 * Get Book description.
1263 * @return $description
1265 function getDescription() {
1266 return $this->description
;
1270 * Book author or creator, optional.
1271 * The $authorSortKey is basically how the name is to be sorted, usually
1272 * it's "Lastname, First names" where the $author is the straight
1273 * "Firstnames Lastname"
1275 * An entity primarily responsible for making the resource.
1277 * Examples of a Creator include a person, an organization, or a service.
1278 * Typically, the name of a Creator should be used to indicate the entity.
1280 * Used for the dc:creator metadata parameter in the OPF file and the
1281 * docAuthor attribure in the NCX file.
1282 * The sort key is used for the opf:file-as attribute in dc:creator.
1284 * @param string $author
1285 * @param string $authorSortKey
1287 * @return bool $success
1289 function setAuthor($author, $authorSortKey) {
1290 if ($this->isFinalized
) {
1293 $this->author
= $author;
1294 $this->authorSortKey
= $authorSortKey;
1304 function getAuthor() {
1305 return $this->author
;
1309 * Publisher Information, optional.
1311 * An entity responsible for making the resource available.
1313 * Examples of a Publisher include a person, an organization, or a service.
1314 * Typically, the name of a Publisher should be used to indicate the entity.
1316 * Used for the dc:publisher and dc:relation metadata parameters in the OPF file.
1318 * @param string $publisherName
1319 * @param string $publisherURL
1321 * @return bool $success
1323 function setPublisher($publisherName, $publisherURL) {
1324 if ($this->isFinalized
) {
1327 $this->publisherName
= $publisherName;
1328 $this->publisherURL
= $publisherURL;
1333 * Get Book publisherName.
1336 * @return $publisherName
1338 function getPublisherName() {
1339 return $this->publisherName
;
1343 * Get Book publisherURL.
1346 * @return $publisherURL
1348 function getPublisherURL() {
1349 return $this->publisherURL
;
1353 * Release date, optional. If left blank, the time of the finalization will
1356 * A point or period of time associated with an event in the lifecycle of
1359 * Date may be used to express temporal information at any level of
1360 * granularity. Recommended best practice is to use an encoding scheme,
1361 * such as the W3CDTF profile of ISO 8601 [W3CDTF].
1363 * Used for the dc:date metadata parameter in the OPF file
1365 * @param long $timestamp
1367 * @return bool $success
1369 function setDate($timestamp) {
1370 if ($this->isFinalized
) {
1373 $this->date
= $timestamp;
1374 $this->opf
->date
= $timestamp;
1384 function getDate() {
1389 * Book (copy)rights, optional.
1391 * Information about rights held in and over the resource.
1393 * Typically, rights information includes a statement about various
1394 * property rights associated with the resource, including intellectual
1397 * Used for the dc:rights metadata parameter in the OPF file
1399 * @param string $rightsText
1401 * @return bool $success
1403 function setRights($rightsText) {
1404 if ($this->isFinalized
) {
1407 $this->rights
= $rightsText;
1417 function getRights() {
1418 return $this->rights
;
1424 * The topic of the resource.
1426 * Typically, the subject will be represented using keywords, key phrases,
1427 * or classification codes. Recommended best practice is to use a
1428 * controlled vocabulary. To describe the spatial or temporal topic of the
1429 * resource, use the Coverage element.
1431 * @param string $subject
1433 function setSubject($subject) {
1434 if ($this->isFinalized
) {
1437 $this->opf
->addDCMeta(DublinCore
::SUBJECT
, $this->decodeHtmlEntities($subject));
1441 * Book source URL, optional.
1443 * A related resource from which the described resource is derived.
1445 * The described resource may be derived from the related resource in whole
1446 * or in part. Recommended best practice is to identify the related
1447 * resource by means of a string conforming to a formal identification system.
1449 * Used for the dc:source metadata parameter in the OPF file
1451 * @param string $sourceURL
1453 * @return bool $success
1455 function setSourceURL($sourceURL) {
1456 if ($this->isFinalized
) {
1459 $this->sourceURL
= $sourceURL;
1464 * Get Book sourceURL.
1467 * @return $sourceURL
1469 function getSourceURL() {
1470 return $this->sourceURL
;
1474 * Coverage, optional.
1476 * The spatial or temporal topic of the resource, the spatial applicability
1477 * of the resource, or the jurisdiction under which the resource is relevant.
1479 * Spatial topic and spatial applicability may be a named place or a location
1480 * specified by its geographic coordinates. Temporal topic may be a named
1481 * period, date, or date range. A jurisdiction may be a named administrative
1482 * entity or a geographic place to which the resource applies. Recommended
1483 * best practice is to use a controlled vocabulary such as the Thesaurus of
1484 * Geographic Names [TGN]. Where appropriate, named places or time periods
1485 * can be used in preference to numeric identifiers such as sets of
1486 * coordinates or date ranges.
1488 * Used for the dc:coverage metadata parameter in the OPF file
1490 * Same as ->addDublinCoreMetadata(DublinCore::COVERAGE, $coverage);
1492 * @param string $coverage
1494 * @return bool $success
1496 function setCoverage($coverage) {
1497 if ($this->isFinalized
) {
1500 $this->coverage
= $coverage;
1505 * Get Book coverage.
1510 function getCoverage() {
1511 return $this->coverage
;
1515 * Set book Relation.
1517 * A related resource.
1519 * Recommended best practice is to identify the related resource by means
1520 * of a string conforming to a formal identification system.
1522 * @param string $relation
1524 function setRelation($relation) {
1525 if ($this->isFinalized
) {
1528 $this->relation
= $relation;
1532 * Get the book relation.
1534 * @return string The relation.
1536 function getRelation() {
1537 return $this->relation
;
1541 * Set book Generator.
1543 * The generator is a meta tag added to the ncx file, it is not visible
1544 * from within the book, but is a kind of electronic watermark.
1546 * @param string $generator
1548 function setGenerator($generator) {
1549 if ($this->isFinalized
) {
1552 $this->generator
= $generator;
1556 * Get the book relation.
1558 * @return string The generator identity string.
1560 function getGenerator() {
1561 return $this->generator
;
1565 * Set ePub date formate to the short yyyy-mm-dd form, for compliance with
1566 * a bug in EpubCheck, prior to its version 1.1.
1568 * The latest version of ePubCheck can be obtained here:
1569 * http://code.google.com/p/epubcheck/
1572 * @return bool $success
1574 function setShortDateFormat() {
1575 if ($this->isFinalized
) {
1578 $this->dateformat
= $this->dateformatShort
;
1585 function setIgnoreEmptyBuffer($ignoreEmptyBuffer = TRUE) {
1586 die ("Function was deprecated, functionality is no longer needed.");
1590 * Set the references title for the ePub 3 landmarks section
1592 * @param string $referencesTitle
1593 * @param string $referencesId
1594 * @param string $referencesClass
1597 function setReferencesTitle($referencesTitle = "Guide", $referencesId = "", $referencesClass = "references") {
1598 if ($this->isFinalized
) {
1601 $this->ncx
->referencesTitle
= is_string($referencesTitle) ? trim($referencesTitle) : "Guide";
1602 $this->ncx
->referencesId
= is_string($referencesId) ? trim($referencesId) : "references";
1603 $this->ncx
->referencesClass
= is_string($referencesClass) ? trim($referencesClass) : "references";
1608 * Set the references title for the ePub 3 landmarks section
1610 * @param bool $referencesTitle
1612 function setisReferencesAddedToToc($isReferencesAddedToToc = TRUE) {
1613 if ($this->isFinalized
) {
1616 $this->isReferencesAddedToToc
= $isReferencesAddedToToc === TRUE;
1626 function isFinalized() {
1627 return $this->isFinalized
;
1631 * Build the Table of Contents. This is not strictly necessary, as most eReaders will build it from the navigation structure in the .ncx file.
1633 * @param string $cssFileName Include a link to this css file in the TOC html.
1634 * @param string $tocCSSClass The TOC is a <div>, if you need special formatting, you can add a css class for that div. Default is "toc".
1635 * @param string $title Title of the Table of contents. Default is "Table of Contents". Use this for ie. languages other than English.
1636 * @param bool $addReferences include reference pages in the TOC, using the $referencesOrder array to determine the order of the pages in the TOC. Default is TRUE.
1637 * @param bool $addToIndex Add the TOC to the NCX index at the current leve/position. Default is FALSE
1638 * @param string $tocFileName Change teh default name of the TOC file. The default is "TOC.xhtml"
1640 function buildTOC($cssFileName = NULL, $tocCSSClass = "toc", $title = "Table of Contents", $addReferences = TRUE, $addToIndex = FALSE, $tocFileName = "TOC.xhtml") {
1641 if ($this->isFinalized
) {
1644 $this->buildTOC
= TRUE;
1645 $this->tocTitle
= $title;
1646 $this->tocFileName
= $this->normalizeFileName($tocFileName);
1647 if (!empty($cssFileName)) {
1648 $this->tocCSSFileName
= $this->normalizeFileName($cssFileName);
1650 $this->tocCSSClass
= $tocCSSClass;
1651 $this->tocAddReferences
= $addReferences;
1653 $this->opf
->addItemRef("ref_" . Reference
::TABLE_OF_CONTENTS
, FALSE);
1654 $this->opf
->addReference(Reference
::TABLE_OF_CONTENTS
, $title, $this->tocFileName
);
1657 $navPoint = new NavPoint($this->decodeHtmlEntities($title), $this->tocFileName
, "ref_" . Reference
::TABLE_OF_CONTENTS
);
1658 $this->ncx
->addNavPoint($navPoint);
1660 $this->ncx
->referencesList
[Reference
::TABLE_OF_CONTENTS
] = $this->tocFileName
;
1661 $this->ncx
->referencesName
[Reference
::TABLE_OF_CONTENTS
] = $title;
1665 private function finalizeTOC() {
1666 if (!$this->buildTOC
) {
1670 if (empty($this->tocTitle
)) {
1671 $this->tocTitle
= "Table of Contents";
1674 $tocData = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
1676 if ($this->isEPubVersion2()) {
1677 $tocData .= "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"\n"
1678 . " \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n"
1679 . "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
1680 . "<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n";
1682 $tocData .= "<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\">\n"
1683 . "<head>\n<meta http-equiv=\"Default-Style\" content=\"text/html; charset=utf-8\" />\n";
1686 if (!empty($this->tocCssFileName
)) {
1687 $tocData .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"" . $this->tocCssFileName
. "\" />\n";
1690 $tocData .= "<title>" . $this->tocTitle
. "</title>\n"
1693 . "<h3>" . $this->tocTitle
. "</h3>\n<div";
1695 if (!empty($this->tocCSSClass
)) {
1696 $tocData .= " class=\"" . $this->tocCSSClass
. "\"";
1700 while (list($item, $descriptive) = each($this->referencesOrder
)) {
1701 if ($item === "text") {
1702 while (list($chapterName, $navPoint) = each($this->ncx
->chapterList
)) {
1703 $fileName = $navPoint->getContentSrc();
1704 $level = $navPoint->getLevel() -2;
1705 $tocData .= "\t<p>" . str_repeat("      ", $level) . "<a href=\"" . $this->sanitizeFileName($fileName) . "\">" . $chapterName . "</a></p>\n";
1707 } else if ($this->tocAddReferences
=== TRUE) {
1708 if (array_key_exists($item, $this->ncx
->referencesList
)) {
1709 $tocData .= "\t<p><a href=\"" . $this->ncx
->referencesList
[$item] . "\">" . $descriptive . "</a></p>\n";
1710 } else if ($item === "toc") {
1711 $tocData .= "\t<p><a href=\"TOC.xhtml\">" . $this->tocTitle
. "</a></p>\n";
1712 } else if ($item === "cover" && $this->isCoverImageSet
) {
1713 $tocData .= "\t<p><a href=\"CoverPage.xhtml\">" . $descriptive . "</a></p>\n";
1717 $tocData .= "</div>\n</body>\n</html>\n";
1719 $this->addReferencePage($this->tocTitle
, $this->tocFileName
, $tocData, Reference
::TABLE_OF_CONTENTS
);
1726 function isEPubVersion2() {
1727 return $this->bookVersion
=== EPub
::BOOK_VERSION_EPUB2
;
1731 * @param string $cssFileName
1732 * @param string $title
1735 function buildEPub3TOC($cssFileName = NULL, $title = "Table of Contents") {
1736 $this->ncx
->referencesOrder
= $this->referencesOrder
;
1737 $this->ncx
->setDocTitle($this->decodeHtmlEntities($this->title
));
1738 return $this->ncx
->finalizeEPub3($title, $cssFileName);
1742 * @param string $fileName
1743 * @param string $tocData
1746 function addEPub3TOC($fileName, $tocData) {
1747 if ($this->isEPubVersion2() || $this->isFinalized
|| array_key_exists($fileName, $this->fileList
)) {
1750 $fileName = Zip
::getRelativePath($fileName);
1751 $fileName = preg_replace('#^[/\.]+#i', "", $fileName);
1753 $this->zip
->addFile($tocData, $this->bookRoot
.$fileName);
1755 $this->fileList
[$fileName] = $fileName;
1756 $this->opf
->addItem("toc", $fileName, "application/xhtml+xml", "nav");
1761 * Check for mandatory parameters and finalize the e-book.
1762 * Once finalized, the book is locked for further additions.
1764 * @return bool $success
1766 function finalize() {
1767 if ($this->isFinalized
|| $this->chapterCount
== 0 || empty($this->title
) || empty($this->language
)) {
1771 if (empty($this->identifier
) || empty($this->identifierType
)) {
1772 $this->setIdentifier($this->createUUID(4), EPub
::IDENTIFIER_UUID
);
1775 if ($this->date
== 0) {
1776 $this->date
= time();
1779 if (empty($this->sourceURL
)) {
1780 $this->sourceURL
= $this->getCurrentPageURL();
1783 if (empty($this->publisherURL
)) {
1784 $this->sourceURL
= $this->getCurrentServerURL();
1787 // Generate OPF data:
1788 $this->opf
->setIdent("BookId");
1789 $this->opf
->initialize($this->title
, $this->language
, $this->identifier
, $this->identifierType
);
1791 $DCdate = new DublinCore(DublinCore
::DATE
, gmdate($this->dateformat
, $this->date
));
1792 $DCdate->addOpfAttr("event", "publication");
1793 $this->opf
->metadata
->addDublinCore($DCdate);
1795 if (!empty($this->description
)) {
1796 $this->opf
->addDCMeta(DublinCore
::DESCRIPTION
, $this->decodeHtmlEntities($this->description
));
1799 if (!empty($this->publisherName
)) {
1800 $this->opf
->addDCMeta(DublinCore
::PUBLISHER
, $this->decodeHtmlEntities($this->publisherName
));
1803 if (!empty($this->publisherURL
)) {
1804 $this->opf
->addDCMeta(DublinCore
::RELATION
, $this->decodeHtmlEntities($this->publisherURL
));
1807 if (!empty($this->author
)) {
1808 $author = $this->decodeHtmlEntities($this->author
);
1809 $this->opf
->addCreator($author, $this->decodeHtmlEntities($this->authorSortKey
), MarcCode
::AUTHOR
);
1810 $this->ncx
->setDocAuthor($author);
1813 if (!empty($this->rights
)) {
1814 $this->opf
->addDCMeta(DublinCore
::RIGHTS
, $this->decodeHtmlEntities($this->rights
));
1817 if (!empty($this->coverage
)) {
1818 $this->opf
->addDCMeta(DublinCore
::COVERAGE
, $this->decodeHtmlEntities($this->coverage
));
1821 if (!empty($this->sourceURL
)) {
1822 $this->opf
->addDCMeta(DublinCore
::SOURCE
, $this->sourceURL
);
1825 if (!empty($this->relation
)) {
1826 $this->opf
->addDCMeta(DublinCore
::RELATION
, $this->decodeHtmlEntities($this->relation
));
1829 if ($this->isCoverImageSet
) {
1830 $this->opf
->addMeta("cover", "coverImage");
1833 if (!empty($this->generator
)) {
1834 $gen = $this->decodeHtmlEntities($this->generator
);
1835 $this->opf
->addMeta("generator", $gen);
1836 $this->ncx
->addMetaEntry("dtb:generator", $gen);
1839 if ($this->EPubMark
) {
1840 $this->opf
->addMeta("generator", "EPub (Version " . self
::VERSION
. ") by A. Grandt, http://www.phpclasses.org/package/6115");
1843 reset($this->ncx
->chapterList
);
1844 list($firstChapterName, $firstChapterNavPoint) = each($this->ncx
->chapterList
);
1845 $firstChapterFileName = $firstChapterNavPoint->getContentSrc();
1846 $this->opf
->addReference(Reference
::TEXT
, $this->decodeHtmlEntities($firstChapterName), $firstChapterFileName);
1848 $this->ncx
->setUid($this->identifier
);
1850 $this->ncx
->setDocTitle($this->decodeHtmlEntities($this->title
));
1852 $this->ncx
->referencesOrder
= $this->referencesOrder
;
1853 if ($this->isReferencesAddedToToc
) {
1854 $this->ncx
->finalizeReferences();
1857 $this->finalizeTOC();
1859 if (!$this->isEPubVersion2()) {
1860 $this->addEPub3TOC("epub3toc.xhtml", $this->buildEPub3TOC());
1863 $opfFinal = $this->fixEncoding($this->opf
->finalize());
1864 $ncxFinal = $this->fixEncoding($this->ncx
->finalize());
1866 if (mb_detect_encoding($opfFinal, 'UTF-8', true) === "UTF-8") {
1867 $this->zip
->addFile($opfFinal, $this->bookRoot
."book.opf");
1869 $this->zip
->addFile(mb_convert_encoding($opfFinal, "UTF-8"), $this->bookRoot
."book.opf");
1872 if (mb_detect_encoding($ncxFinal, 'UTF-8', true) === "UTF-8") {
1873 $this->zip
->addFile($ncxFinal, $this->bookRoot
."book.ncx");
1875 $this->zip
->addFile(mb_convert_encoding($ncxFinal, "UTF-8"), $this->bookRoot
."book.ncx");
1881 $this->isFinalized
= TRUE;
1886 * Ensure the encoded string is a valid UTF-8 string.
1888 * Note, that a mb_detect_encoding on the returned string will still return ASCII if the entire string is comprized of characters in the 1-127 range.
1890 * @link: http://snippetdb.com/php/convert-string-to-utf-8-for-mysql
1891 * @param string $in_str
1892 * @return string converted string.
1894 function fixEncoding($in_str) {
1895 if (mb_detect_encoding($in_str) == "UTF-8" && mb_check_encoding($in_str,"UTF-8")) {
1898 return utf8_encode($in_str);
1903 * Return the finalized book.
1905 * @return string with the book in binary form.
1907 function getBook() {
1908 if (!$this->isFinalized
) {
1912 return $this->zip
->getZipData();
1916 * Remove disallowed characters from string to get a nearly safe filename
1918 * @param string $fileName
1919 * @return mixed|string
1921 function sanitizeFileName($fileName) {
1922 $fileName1 = str_replace($this->forbiddenCharacters
, '', $fileName);
1923 $fileName2 = preg_replace('/[\s-]+/', '-', $fileName1);
1924 return trim($fileName2, '.-_');
1929 * Cleanup the filepath, and remove leading . and / characters.
1931 * Sometimes, when a path is generated from multiple fragments,
1932 * you can get something like "../data/html/../images/image.jpeg"
1933 * ePub files don't work well with that, this will normalize that
1934 * example path to "data/images/image.jpeg"
1936 * @param string $fileName
1937 * @return string normalized filename
1939 function normalizeFileName($fileName) {
1940 return preg_replace('#^[/\.]+#i', "", Zip
::getRelativePath($fileName));
1944 * Save the ePub file to local disk.
1946 * @param string $fileName
1947 * @param string $baseDir If empty baseDir is absolute to server path, if omitted it's relative to script path
1948 * @return The sent file name if successfull, FALSE if it failed.
1950 function saveBook($fileName, $baseDir = '.') {
1952 // Make fileName safe
1953 $fileName = $this->sanitizeFileName($fileName);
1955 // Finalize book, if it's not done already
1956 if (!$this->isFinalized
) {
1960 if (stripos(strrev($fileName), "bupe.") !== 0) {
1961 $fileName .= ".epub";
1964 // Try to open file access
1965 $fh = fopen($baseDir.'/'.$fileName, "w");
1968 fputs($fh, $this->getBook());
1971 // if file is written return TRUE
1975 // return FALSE by default
1980 * Return the finalized book size.
1984 function getBookSize() {
1985 if (!$this->isFinalized
) {
1989 return $this->zip
->getArchiveSize();
1993 * Send the book as a zip download
1995 * Sending will fail if the output buffer is in use. You can override this limit by
1996 * calling setIgnoreEmptyBuffer(TRUE), though the function will still fail if that
1997 * buffer is not empty.
1999 * @param string $fileName The name of the book without the .epub at the end.
2000 * @return The sent file name if successfull, FALSE if it failed.
2002 function sendBook($fileName) {
2003 if (!$this->isFinalized
) {
2007 if (stripos(strrev($fileName), "bupe.") !== 0) {
2008 $fileName .= ".epub";
2011 if (TRUE === $this->zip
->sendZip($fileName, "application/epub+zip")) {
2018 * Generates an UUID.
2020 * Default version (4) will generate a random UUID, version 3 will URL based UUID.
2022 * Added for convinience
2024 * @param int $bookVersion UUID version to retrieve, See lib.uuid.manual.html for details.
2025 * @param string $url
2026 * @return string The formatted uuid
2028 function createUUID($bookVersion = 4, $url = NULL) {
2029 include_once("lib.uuid.php");
2030 return UUID
::mint($bookVersion, $url, UUID
::nsURL
);
2034 * Get the url of the current page.
2035 * Example use: Default Source URL
2037 * $return string Page URL.
2039 function getCurrentPageURL() {
2040 $pageURL = $this->getCurrentServerURL() . filter_input(INPUT_SERVER
, "REQUEST_URI");
2045 * Get the url of the server.
2046 * Example use: Default Publisher URL
2048 * $return string Server URL.
2050 function getCurrentServerURL() {
2051 $serverURL = 'http';
2052 $https = filter_input(INPUT_SERVER
, "HTTPS");
2053 $port = filter_input(INPUT_SERVER
, "SERVER_PORT");
2055 if ($https === "on") {
2058 $serverURL .= "://" . filter_input(INPUT_SERVER
, "SERVER_NAME");
2059 if ($port != "80") {
2060 $serverURL .= ":" . $port;
2062 return $serverURL . '/';
2066 * Try to determine the mimetype of the file path.
2068 * @param string $source Path
2069 * @return string mimetype, or FALSE.
2071 function getMime($source) {
2072 return $this->mimetypes
[pathinfo($source, PATHINFO_EXTENSION
)];
2076 * Get an image from a file or url, return it resized if the image exceeds the $maxImageWidth or $maxImageHeight directives.
2078 * The return value is an array.
2079 * ['width'] is the width of the image.
2080 * ['height'] is the height of the image.
2081 * ['mime'] is the mime type of the image. Resized images are always in jpeg format.
2082 * ['image'] is the image data.
2083 * ['ext'] is the extension of the image file.
2085 * @param string $source path or url to file.
2088 function getImage($source) {
2091 $mime = "application/octet-stream";
2096 $image = $this->getFileContents($source);
2098 if ($image !== FALSE && strlen($image) > 0) {
2099 $imageFile = imagecreatefromstring($image);
2100 if ($imageFile !== false) {
2101 $width = ImageSX($imageFile);
2102 $height = ImageSY($imageFile);
2104 if ($this->isExifInstalled
) {
2105 @$type = exif_imagetype($source);
2106 $mime = image_type_to_mime_type($type);
2108 if ($mime === "application/octet-stream") {
2109 $mime = $this->image_file_type_from_binary($image);
2111 if ($mime === "application/octet-stream") {
2112 $mime = $this->getMimeTypeFromUrl($source);
2118 if ($width <= 0 || $height <= 0) {
2124 if ($this->isGdInstalled
) {
2125 if ($width > $this->maxImageWidth
) {
2126 $ratio = $this->maxImageWidth
/$width;
2128 if ($height*$ratio > $this->maxImageHeight
) {
2129 $ratio = $this->maxImageHeight
/$height;
2132 if ($ratio < 1 || empty($mime) || ($this->isGifImagesEnabled
!== FALSE && $mime == "image/gif")) {
2133 $image_o = imagecreatefromstring($image);
2134 $image_p = imagecreatetruecolor($width*$ratio, $height*$ratio);
2136 if ($mime == "image/png") {
2137 imagealphablending($image_p, false);
2138 imagesavealpha($image_p, true);
2139 imagealphablending($image_o, true);
2141 imagecopyresampled($image_p, $image_o, 0, 0, 0, 0, ($width*$ratio), ($height*$ratio), $width, $height);
2143 imagepng($image_p, NULL, 9);
2144 $image = ob_get_contents();
2149 imagecopyresampled($image_p, $image_o, 0, 0, 0, 0, ($width*$ratio), ($height*$ratio), $width, $height);
2151 imagejpeg($image_p, NULL, 80);
2152 $image = ob_get_contents();
2155 $mime = "image/jpeg";
2158 imagedestroy($image_o);
2159 imagedestroy($image_p);
2164 static $mimeToExt = array (
2165 'image/jpeg' => 'jpg',
2166 'image/gif' => 'gif',
2167 'image/png' => 'png'
2170 if (isset($mimeToExt[$mime])) {
2171 $ext = $mimeToExt[$mime];
2176 $rv['width'] = $width*$ratio;
2177 $rv['height'] = $height*$ratio;
2178 $rv['mime'] = $mime;
2179 $rv['image'] = $image;
2186 * Get file contents, using curl if available, else file_get_contents
2188 * @param string $source
2191 function getFileContents($source, $toTempFile = FALSE) {
2192 $isExternal = preg_match('#^(http|ftp)s?://#i', $source) == 1;
2194 if ($isExternal && $this->isCurlInstalled
) {
2199 $info = array('http_code' => 500);
2201 curl_setopt($ch, CURLOPT_HEADER
, 0);
2202 curl_setopt($ch, CURLOPT_URL
, str_replace(" ","%20",$source));
2203 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, true);
2204 curl_setopt($ch, CURLOPT_FOLLOWLOCATION
, true);
2205 curl_setopt($ch, CURLOPT_BUFFERSIZE
, 4096);
2208 $outFile = tempnam(sys_get_temp_dir(), "EPub_v" . EPub
::VERSION
. "_");
2209 $fp = fopen($outFile, "w+b");
2210 curl_setopt($ch, CURLOPT_FILE
, $fp);
2212 $res = curl_exec($ch);
2213 $info = curl_getinfo($ch);
2218 $res = curl_exec($ch);
2219 $info = curl_getinfo($ch);
2224 if ($info['http_code'] == 200 && $res != false) {
2233 if ($this->isFileGetContentsInstalled
&& (!$isExternal || $this->isFileGetContentsExtInstalled
)) {
2234 @$data = file_get_contents($source);
2241 * get mime type from image data
2243 * By fireweasel found on http://stackoverflow.com/questions/2207095/get-image-mimetype-from-resource-in-php-gd
2244 * @staticvar array $type
2245 * @param object $binary
2248 function image_file_type_from_binary($binary) {
2251 '/\A(?:(\xff\xd8\xff)|(GIF8[79]a)|(\x89PNG\x0d\x0a)|(BM)|(\x49\x49(?:\x2a\x00|\x00\x4a))|(FORM.{4}ILBM))/',
2253 return 'application/octet-stream';
2255 static $type = array (
2259 4 => 'image/x-windows-bmp',
2261 6 => 'image/x-ilbm',
2263 return $type[count($hits) - 1];
2267 * @param string $source URL Source
2268 * @return string MimeType
2270 function getMimeTypeFromUrl($source) {
2273 $srev = strrev($source);
2274 $pos = strpos($srev, "?");
2275 if ($pos !== FALSE) {
2276 $srev = substr($srev, $pos+
1);
2279 $pos = strpos($srev, ".");
2280 if ($pos !== FALSE) {
2281 $ext = strtolower(strrev(substr($srev, 0, $pos)));
2284 if ($ext !== FALSE) {
2285 return $this->getMimeTypeFromExtension($ext);
2287 return "application/octet-stream";
2291 * @param string $ext Extension
2292 * @return string MimeType
2294 function getMimeTypeFromExtension($ext) {
2299 return 'image/jpeg';
2305 return 'image/x-windows-bmp';
2309 return 'image/tiff';
2312 return 'image/x-ilbm';
2314 return "application/octet-stream";
2319 * Encode html code to use html entities, safeguarding it from potential character encoding peoblems
2320 * This function is a bit different from the vanilla htmlentities function in that it does not encode html tags.
2322 * The regexp is taken from the PHP Manual discussion, it was written by user "busbyjon".
2323 * http://www.php.net/manual/en/function.htmlentities.php#90111
2325 * @param string $string string to encode.
2327 public function encodeHtml($string) {
2328 $string = strtr($string, $this->html_encoding_characters
);
2330 //return preg_replace("/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,5};)/", "&\\1", $string);
2331 //return preg_replace("/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,5};)/", "&", $string);
2336 * Helper function to create a DOM fragment with given markup.
2338 * @author Adam Schmalhofer
2340 * @param DOMDocument $dom
2341 * @param string $markup
2342 * @return DOMNode fragment in a node.
2344 protected function createDomFragment($dom, $markup) {
2345 $node = $dom->createDocumentFragment();
2346 $node->appendXML($markup);
2351 * Retrieve an array of file names currently added to the book.
2352 * $key is the filename used in the book
2353 * $value is the original filename, will be the same as $key for most entries
2355 * @return array file list
2357 function getFileList() {
2358 return $this->fileList
;
2362 * @deprecated Use Zip::getRelativePath($relPath) instead.
2364 function relPath($relPath) {
2365 die ("Function was deprecated, use Zip::getRelativePath(\$relPath); instead");
2369 * Set default chapter target size.
2370 * Default is 250000 bytes, and minimum is 10240 bytes.
2372 * @param int $size segment size in bytes
2375 function setSplitSize($size) {
2376 $this->splitDefaultSize
= (int)$size;
2377 if ($size < 10240) {
2378 $this->splitDefaultSize
= 10240; // Making the file smaller than 10k is not a good idea.
2383 * Get the chapter target size.
2387 function getSplitSize() {
2388 return $this->splitDefaultSize
;
2392 * Remove all non essential html tags and entities.
2394 * @global type $htmlEntities
2395 * @param string $string
2396 * @return string with the stripped entities.
2398 function decodeHtmlEntities($string) {
2399 global $htmlEntities;
2401 $string = preg_replace('~\s*<br\s*/*\s*>\s*~i', "\n", $string);
2402 $string = preg_replace('~\s*</(p|div)\s*>\s*~i', "\n\n", $string);
2403 $string = preg_replace('~<[^>]*>~', '', $string);
2405 $string = strtr($string, $htmlEntities);
2407 $string = str_replace('&', '&', $string);
2408 $string = str_replace('&amp;', '&', $string);
2409 $string = preg_replace('~&(#x*[a-fA-F0-9]+;)~', '&\1', $string);
2410 $string = str_replace('<', '<', $string);
2411 $string = str_replace('>', '>', $string);
2417 * Simply remove all HTML tags, brute force and no finesse.
2419 * @param string $string html
2422 function html2text($string) {
2423 return preg_replace('~<[^>]*>~', '', $string);
2430 return $this->log
->getLog();