From b1778cd3b30c1fb26cabafdeeb63004b8d6c282e Mon Sep 17 00:00:00 2001 From: trob Date: Mon, 19 Sep 2016 13:15:10 +0200 Subject: [PATCH] use COM_FABRIK_LIVESITE_ROOT for scheme,host,port handle subdirs (which may or maynot be included in relative pathes) check for existing scheme to determine if abs. path (checking for base would add the base to external pathes) --- components/com_fabrik/helpers/pdf.php | 49 ++++++++++++--------------- 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/components/com_fabrik/helpers/pdf.php b/components/com_fabrik/helpers/pdf.php index 1f5ba8b29ef..58c31f3e31f 100644 --- a/components/com_fabrik/helpers/pdf.php +++ b/components/com_fabrik/helpers/pdf.php @@ -76,35 +76,26 @@ public static function fullPaths(&$data) $data = str_replace('xmlns=', 'ns=', $data); libxml_use_internal_errors(true); + $base_root = COM_FABRIK_LIVESITE_ROOT . '/'; // scheme, host, port, without trailing /,add it + $subdir = str_replace(COM_FABRIK_LIVESITE_ROOT,'',COM_FABRIK_LIVESITE); // subdir /xx/ + $subdir = ltrim($subdir,'/'); + + $schemeString = '://'; //if no schemeString found assume path is relative + try { $ok = new SimpleXMLElement($data); if ($ok) { - $uri = JUri::getInstance(); - $host = $uri->getHost(); - $port = $uri->getPort(); - - if (empty($port)) - { - $port = 80; - } - - // If the port is not default, add it - if (! (($uri->getScheme() == 'http' && $port == 80) || - ($uri->getScheme() == 'https' && $port == 443))) { - $host .= ':' . $port; - } - - $base = $uri->getScheme() . '://' . $host; $imgs = $ok->xpath('//img'); foreach ($imgs as &$img) { - if (!strstr($img['src'], $base)) + if (!strstr($img['src'], $schemeString)) { - $img['src'] = $base . $img['src']; + $base = strstr($img['src'], $subdir) ? $base_root : $base_root . $subdir; + $img['src'] = $base . ltrim($img['src'],'/'); } } @@ -113,9 +104,10 @@ public static function fullPaths(&$data) foreach ($as as &$a) { - if (!strstr($a['href'], $base)) + if (!strstr($a['href'], $schemeString)) { - $a['href'] = $base . $a['href']; + $base = strstr($a['href'], $subdir) ? $base_root : $base_root . $subdir; + $a['href'] = $base . ltrim($a['href'],'/'); } } @@ -124,9 +116,10 @@ public static function fullPaths(&$data) foreach ($links as &$link) { - if ($link['rel'] == 'stylesheet' && !strstr($link['href'], $base)) + if ($link['rel'] == 'stylesheet' && !strstr($link['href'], $schemeString)) { - $link['href'] = $base . $link['href']; + $base = strstr($link['href'], $subdir) ? $base_root : $base_root . $subdir; + $link['href'] = $base . ltrim($link['href'],'/'); } } @@ -149,14 +142,14 @@ public static function fullPaths(&$data) exit; } //Create the full path via general str_replace + //todo: relative URL starting without / else { - $uri = JUri::getInstance(); - $base = $uri->getScheme() . '://' . $uri->getHost(); - $data = str_replace('href="/', 'href="'.$base.'/', $data); - $data = str_replace('src="/', 'src="'.$base.'/', $data); - $data = str_replace("href='/", "href='".$base.'/', $data); - $data = str_replace("src='/", "src='".$base.'/', $data); + $base = $base_root . $subdir; + $data = str_replace('href="/', 'href="' . $base, $data); + $data = str_replace('src="/', 'src="' . $base, $data); + $data = str_replace("href='/", "href='" . $base, $data); + $data = str_replace("src='/", "src='" . $base, $data); } } }