Simplify relative path handling.

--HG--
extra : rebase_source : d20290cad2dca3ea1f04a85f899c026c0991bbe5
This commit is contained in:
Sébastien Lucas 2013-08-30 10:35:15 +02:00
parent 881f76d87a
commit 30f801443e
2 changed files with 27 additions and 11 deletions

View file

@ -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 ();
}