/** * Constructor * * @param string $file path to epub file to work on * @throws Exception if metadata could not be loaded */ public function __construct($file) { // open file $this->file = $file; $zip = new ZipArchive(); if (!@$zip->open($this->file)) { throw new Exception('Failed to read epub file'); } // read container data $data = $zip->getFromName('META-INF/container.xml'); if ($data == false) { throw new Exception('Failed to access epub container data'); } $xml = new DOMDocument(); $xml->registerNodeClass('DOMElement', 'EPubDOMElement'); $xml->loadXML($data); $xpath = new EPubDOMXPath($xml); $nodes = $xpath->query('//n:rootfiles/n:rootfile[@media-type="application/oebps-package+xml"]'); $this->meta = $nodes->item(0)->attr('full-path'); // load metadata $data = $zip->getFromName($this->meta); if (!$data) { throw new Exception('Failed to access epub metadata'); } $this->xml = new DOMDocument(); $this->xml->registerNodeClass('DOMElement', 'EPubDOMElement'); $this->xml->loadXML($data); $this->xml->formatOutput = true; $this->xpath = new EPubDOMXPath($this->xml); $zip->close(); }
/** * Constructor * * @param string $file path to epub file to work on * @param string $zipClass class to handle zip * @throws Exception if metadata could not be loaded */ public function __construct($file, $zipClass = 'clsTbsZip') { // open file $this->file = $file; $this->zip = new $zipClass(); if (!$this->zip->Open($this->file)) { throw new Exception('Failed to read epub file'); } // read container data if (!$this->zip->FileExists(METADATA_FILE)) { throw new Exception("Unable to find metadata.xml"); } $data = $this->zip->FileRead(METADATA_FILE); if ($data == false) { throw new Exception('Failed to access epub container data'); } $xml = new DOMDocument(); $xml->registerNodeClass('DOMElement', 'EPubDOMElement'); $xml->loadXML($data); $xpath = new EPubDOMXPath($xml); $nodes = $xpath->query('//n:rootfiles/n:rootfile[@media-type="application/oebps-package+xml"]'); $this->meta = $nodes->item(0)->attr('full-path'); // load metadata if (!$this->zip->FileExists($this->meta)) { throw new Exception("Unable to find " . $this->meta); } $data = $this->zip->FileRead($this->meta); if (!$data) { throw new Exception('Failed to access epub metadata'); } $this->xml = new DOMDocument(); $this->xml->registerNodeClass('DOMElement', 'EPubDOMElement'); $this->xml->loadXML($data); $this->xml->formatOutput = true; $this->xpath = new EPubDOMXPath($this->xml); }