| 1: | <?php |
| 2: | |
| 3: | namespace nightmare\epub; |
| 4: | |
| 5: | use nightmare\fs; |
| 6: | use Exception; |
| 7: | use ZipArchive; |
| 8: | |
| 9: | class reader { |
| 10: | private $zip; |
| 11: | private $file_path; |
| 12: | |
| 13: | |
| 14: | private $epub_opf_dir; |
| 15: | |
| 16: | public function __construct($file_path) { |
| 17: | $this->file_path = $file_path; |
| 18: | $this->zip = new ZipArchive(); |
| 19: | |
| 20: | $this->check(); |
| 21: | $this->read_metadata(); |
| 22: | } |
| 23: | |
| 24: | private function check() { |
| 25: | |
| 26: | if (!is_file($this->file_path)) { |
| 27: | throw new Exception('epub not exists or not permission'); |
| 28: | } |
| 29: | |
| 30: | if (filesize($this->file_path) < 1) { |
| 31: | throw new Exception('epub not exists or not permission'); |
| 32: | } |
| 33: | |
| 34: | |
| 35: | if ($this->zip->open($this->file_path) !== TRUE) { |
| 36: | throw new Exception('epub read error'); |
| 37: | } |
| 38: | |
| 39: | if ($this->zip->getFromName('mimetype') !== 'application/epub+zip') { |
| 40: | throw new Exception('epub format error'); |
| 41: | } |
| 42: | } |
| 43: | |
| 44: | private function read_metadata() { |
| 45: | $meta = $this->zip->getFromName('META-INF/container.xml'); |
| 46: | $meta = simplexml_load_string($meta); |
| 47: | $this->epub_opf_dir = (string) $meta->rootfiles->rootfile['full-path']; |
| 48: | |
| 49: | $meta = $this->zip->getFromName($this->epub_opf_dir); |
| 50: | $meta = simplexml_load_string($meta); |
| 51: | |
| 52: | var_dump($meta->metadata->children('http://purl.org/dc/elements/1.1/')); |
| 53: | } |
| 54: | } |
| 55: | |