Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
Moved "static" functions (the ones that already were static, an the ones
that should be) into helper classes to cleanup the ever growing EPUB
class.

Adding Rendition an AppleIbooks helper classes (epub3 only for now)

Updated examples and code to always name files xhtml (required due to
ePubCheck 4.0.1)
  • Loading branch information
Grandt committed Nov 7, 2015
1 parent c10bb64 commit 7606182
Show file tree
Hide file tree
Showing 30 changed files with 1,280 additions and 976 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"keywords": ["epub", "e-book"],
"homepage": "https://github.com/Grandt/PHPZip",
"license": "LGPL-2.1",
"version": "4.0.5",
"version": "4.0.6",
"minimum-stability": "stable",
"authors": [
{
Expand Down
7 changes: 4 additions & 3 deletions legacy/EPub.Test.Example.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

use PHPePub\Core\EPub;
use PHPePub\Core\EPubChapterSplitter;
use PHPePub\Core\Structure\OPF\DublinCore;
use PHPePub\Core\Logger;
use PHPePub\Core\Structure\OPF\DublinCore;
use PHPePub\Helpers\URLHelper;
use PHPZip\Zip\File\Zip;

error_reporting(E_ALL | E_STRICT);
Expand Down Expand Up @@ -40,8 +41,8 @@
$log->logLine("new EPub()");
$log->logLine("EPub class version.: " . EPub::VERSION);
$log->logLine("Zip version........: " . Zip::VERSION);
$log->logLine("getCurrentServerURL: " . $book->getCurrentServerURL());
$log->logLine("getCurrentPageURL..: " . $book->getCurrentPageURL());
$log->logLine("getCurrentServerURL: " . URLHelper::getCurrentServerURL());
$log->logLine("getCurrentPageURL..: " . URLHelper::getCurrentPageURL());

// Title and Identifier are mandatory!
$book->setTitle("Test book");
Expand Down
869 changes: 108 additions & 761 deletions src/PHPePub/Core/EPub.php

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions src/PHPePub/Core/StaticData.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,4 @@ class StaticData {
"opf" => "http://www.idpf.org/2007/opf",
"dcterms" => "http://purl.org/dc/terms/",
"dc" => "http://purl.org/dc/elements/1.1/");

public static $prefixNamespaces = array("rendition" => "http://www.idpf.org/vocab/rendition/#",
"ibooks" => "http://vocabulary.itunes.apple.com/rdf/ibooks/vocabulary-extensions-1.0");

}
18 changes: 17 additions & 1 deletion src/PHPePub/Core/Structure/Ncx.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class Ncx {

private $bookVersion = EPub::BOOK_VERSION_EPUB2;

/** @var EPub $parentBook */
private $parentBook = null;

private $navMap = null;
private $uid = null;
private $meta = array();
Expand Down Expand Up @@ -62,13 +65,20 @@ function __construct($uid = null, $docTitle = null, $docAuthor = null, $language
* @return void
*/
function __destruct() {
unset($this->bookVersion, $this->navMap, $this->uid, $this->meta);
unset($this->parentBook, $this->bookVersion, $this->navMap, $this->uid, $this->meta);
unset($this->docTitle, $this->docAuthor, $this->currentLevel, $this->lastLevel);
unset($this->languageCode, $this->writingDirection, $this->chapterList, $this->referencesTitle);
unset($this->referencesClass, $this->referencesId, $this->referencesList, $this->referencesName);
unset($this->referencesOrder);
}

/**
* @param EPub $parentBook
*/
public function setBook($parentBook) {
$this->parentBook = $parentBook;
}

/**
*
* Enter description here ...
Expand Down Expand Up @@ -312,9 +322,15 @@ function finalizeEPub3($title = "Table of Contents", $cssFileName = null) {
. "\t<head>\n"
. "\t\t<title>" . $this->docTitle . "</title>\n"
. "\t\t<meta http-equiv=\"default-style\" content=\"text/html; charset=utf-8\"/>\n";

if ($this->parentBook !== null) {
$end .= $this->parentBook->getViewportMetaLine();
}

if ($cssFileName !== null) {
$end .= "\t\t<link rel=\"stylesheet\" href=\"" . $cssFileName . "\" type=\"text/css\"/>\n";
}

$end .= "\t</head>\n"
. "\t<body epub:type=\"frontmatter toc\">\n"
. "\t\t<header>\n"
Expand Down
8 changes: 8 additions & 0 deletions src/PHPePub/Core/Structure/OPF/DublinCore.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ class DublinCore extends MetaValue {
const SUBJECT = "subject";
const TITLE = "title";
const TYPE = "type";
/**
* @var string
*/
private $name;
/**
* @var string
*/
private $value;

/**
* Class constructor.
Expand Down
3 changes: 3 additions & 0 deletions src/PHPePub/Core/Structure/OPF/Itemref.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class Itemref {

/**
* Class constructor.
*
* @param $idref
* @param bool $linear
*/
function __construct($idref, $linear = true) {
$this->setIdref($idref);
Expand Down
30 changes: 30 additions & 0 deletions src/PHPePub/Helpers/CalibreHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* PHPePub
* <CalibreHelper.php description here>
*
* @author A. Grandt <[email protected]>
* @copyright 2015- A. Grandt
* @license GNU LGPL 2.1
*/

namespace PHPePub\Helpers;


use PHPePub\Core\EPub;

class CalibreHelper {
/**
* @param EPub $book
* @param string $seriesName
* @param string $seriesIndex
* @param string $sortTitle
*/
public static function setCalibreMetadata($book, $seriesName, $seriesIndex, $sortTitle = null) {
$book->addCustomMetadata("calibre:series", $seriesName);
$book->addCustomMetadata("calibre:series_index", "" . $seriesIndex);
if (!empty($sortTitle)) {
$book->addCustomMetadata("calibre:title_sort", $sortTitle);
}
}
}
148 changes: 148 additions & 0 deletions src/PHPePub/Helpers/FileHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php
/**
* PHPePub
* <FileHelper.php description here>
*
* @author A. Grandt <[email protected]>
* @copyright 2015- A. Grandt
* @license GNU LGPL 2.1
*/

namespace PHPePub\Helpers;


use PHPePub\Core\EPub;
use PHPePub\Core\StaticData;
use RelativePath;

class FileHelper {
protected static $isCurlInstalled;
protected static $isFileGetContentsInstalled;
protected static $isFileGetContentsExtInstalled;

/**
* @return mixed
*/
public static function getIsCurlInstalled() {
if (!isset(self::$isCurlInstalled)) {
self::$isCurlInstalled = extension_loaded('curl') && function_exists('curl_version');
}
return self::$isCurlInstalled;
}

/**
* @return mixed
*/
public static function getIsFileGetContentsInstalled() {
if (!isset(self::$isFileGetContentsInstalled)) {
self::$isFileGetContentsInstalled = function_exists('file_get_contents');
}
return self::$isFileGetContentsInstalled;
}

/**
* @return mixed
*/
public static function getIsFileGetContentsExtInstalled() {
if (!isset(self::$isFileGetContentsExtInstalled)) {
self::$isFileGetContentsExtInstalled = self::getIsFileGetContentsInstalled() && ini_get('allow_url_fopen');
}
return self::$isFileGetContentsExtInstalled;
}

/**
* Remove disallowed characters from string to get a nearly safe filename
*
* @param string $fileName
*
* @return mixed|string
*/
public static function sanitizeFileName($fileName) {
$fileName1 = str_replace(StaticData::$forbiddenCharacters, '', $fileName);
$fileName2 = preg_replace('/[\s-]+/', '-', $fileName1);

return trim($fileName2, '.-_');
}

/**
* Get file contents, using curl if available, else file_get_contents
*
* @param string $source
* @param bool $toTempFile
*
* @return bool|mixed|null|string
*/
public static function getFileContents($source, $toTempFile = false) {
$isExternal = preg_match('#^(http|ftp)s?://#i', $source) == 1;

if ($isExternal && FileHelper::getIsCurlInstalled()) {
$ch = curl_init();
$outFile = null;
$fp = null;

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, str_replace(" ", "%20", $source));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 4096);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects
curl_setopt($ch, CURLOPT_ENCODING, ""); // handle all encodings
curl_setopt($ch, CURLOPT_USERAGENT, "EPub (Version " . EPub::VERSION . ") by A. Grandt"); // who am i
curl_setopt($ch, CURLOPT_AUTOREFERER, true); // set referer on redirect
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120); // timeout on connect
curl_setopt($ch, CURLOPT_TIMEOUT, 120); // timeout on response
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); // stop after 10 redirects
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Disabled SSL Cert checks

if ($toTempFile) {
$outFile = tempnam(sys_get_temp_dir(), "EPub_v" . EPub::VERSION . "_");
$fp = fopen($outFile, "w+b");
curl_setopt($ch, CURLOPT_FILE, $fp);

$res = curl_exec($ch);
$info = curl_getinfo($ch);

curl_close($ch);
fclose($fp);
} else {
$res = curl_exec($ch);
$info = curl_getinfo($ch);

curl_close($ch);
}

if ($info['http_code'] == 200 && $res != false) {
if ($toTempFile) {
return $outFile;
}

return $res;
}

return false;
}

if (FileHelper::getIsFileGetContentsInstalled() && (!$isExternal || FileHelper::getIsFileGetContentsExtInstalled())) {
@$data = file_get_contents($source);

return $data;
}

return false;
}

/**
* Cleanup the filepath, and remove leading . and / characters.
*
* Sometimes, when a path is generated from multiple fragments,
* you can get something like "../data/html/../images/image.jpeg"
* ePub files don't work well with that, this will normalize that
* example path to "data/images/image.jpeg"
*
* @param string $fileName
*
* @return string normalized filename
*/
public static function normalizeFileName($fileName) {
return preg_replace('#^[/\.]+#i', "", RelativePath::getRelativePath($fileName));
}
}
Loading

0 comments on commit 7606182

Please sign in to comment.