From 30f801443e5190d643a44dd862df751852a434ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Lucas?= Date: Fri, 30 Aug 2013 10:35:15 +0200 Subject: [PATCH] Simplify relative path handling. --HG-- extra : rebase_source : d20290cad2dca3ea1f04a85f899c026c0991bbe5 --- epubfs.php | 23 +++++++++++++++-------- resources/php-epub-meta/epub.php | 15 ++++++++++++--- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/epubfs.php b/epubfs.php index 53f32e2..e34c79e 100644 --- a/epubfs.php +++ b/epubfs.php @@ -13,27 +13,34 @@ $book = new EPub ("c:/Temp/Phare.epub"); $book->initSpineComponent (); $component = $_GET["comp"]; -$elementPath = NULL; -if (!empty ($_GET) && isset($_GET["path"]) && $_GET["path"] != "") { - $elementPath = $_GET["path"]; -} if (empty ($component)) { notFound (); } try { - $data = $book->component ($component, $elementPath); + $data = $book->component ($component); $directory = dirname ($component); - $data = preg_replace ("/src=[\"']([^:]*?)[\"']/", "src='epubfs.php?path=$1&comp=$component'", $data); - $data = preg_replace ("/href=[\"']([^:]*?)[\"']/", "href='epubfs.php?path=$1&comp=$component'", $data); - $data = preg_replace ("/\@import\s+[\"'](.*?)[\"'];/", "@import 'epubfs.php?comp={$directory}/$1';", $data); + $callback = function ($m) use ($book, $component) { + $method = $m[1]; + $path = $m[2]; + if (preg_match ("/^#/", $path)) { + return $path; + } + $comp = $book->getComponentName ($component, $path); + return "$method'epubfs.php?comp=$comp'"; + }; + + $data = preg_replace_callback ("/(src=)[\"']([^:]*?)[\"']/", $callback, $data); + $data = preg_replace_callback ("/(href=)[\"']([^:]*?)[\"']/", $callback, $data); + $data = preg_replace_callback ("/(\@import\s+)[\"'](.*?)[\"'];/", $callback, $data); header ("Content-Type: " . $book->componentContentType($component)); echo $data; } catch (Exception $e) { + error_log ($e); notFound (); } diff --git a/resources/php-epub-meta/epub.php b/resources/php-epub-meta/epub.php index ffc2ee8..286e2f0 100644 --- a/resources/php-epub-meta/epub.php +++ b/resources/php-epub-meta/epub.php @@ -153,9 +153,9 @@ class EPub { /** * Get the component content */ - public function component($comp, $elementPath) { + public function component($comp) { $path = str_replace ("-SLASH-", "/", $comp); - $path = $this->getFullPath ($path, $elementPath); + $path = $this->getFullPath ($path); if (!$this->zip->FileExists($path)) { throw new Exception ("Unable to find " . $path); } @@ -164,6 +164,15 @@ class EPub { return $data; } + public function getComponentName ($comp, $elementPath) { + $path = str_replace ("-SLASH-", "/", $comp); + $path = $this->getFullPath ($path, $elementPath); + if (!$this->zip->FileExists($path)) { + throw new Exception ("Unable to find " . $path); + } + return str_replace ("/", "-SLASH-", $path); + } + /** * Get the component content type */ @@ -533,7 +542,7 @@ class EPub { if (!empty ($context)) { $path = $this->combine (dirname ($path), $context); } - error_log ("FullPath : $path ($file / $context)"); + //error_log ("FullPath : $path ($file / $context)"); return $path; }