Beispiel #1
0
 public static function body($page, $pageLinks, $itemLevel)
 {
     // $baseFolder = ($itemLevel <= 0) ? '' : str_repeat('../',$itemLevel) ;
     $body = $page->content;
     $domDocument = new DOMDocument();
     $domDocument->loadHTML($body);
     $base = $domDocument->getElementsByTagName('base');
     $option = JFactory::getApplication()->input->get('option');
     $comParams = JComponentHelper::getParams($option);
     $newBaseURL = rtrim($comParams->get('base_url'), '/') . '/';
     $links = $domDocument->getElementsByTagName('link');
     $scripts = $domDocument->getElementsByTagName('script');
     $images = $domDocument->getElementsByTagName('img');
     $pictures = $domDocument->getElementsByTagName('source');
     $body = str_replace(JURI::root(), '', $body);
     foreach ($links as $link) {
         $url = $link->getAttribute('href');
         $relativeUrl = StaticContentHelperUrl::getRelativeLink($url);
         $link->setAttribute('href', $relativeUrl);
         $body = str_replace('href="' . htmlspecialchars($url) . '"', 'href="' . $relativeUrl . '"', $body);
         self::copyFile($link, 'href');
     }
     foreach ($images as $img) {
         $url = $img->getAttribute('src');
         $relativeUrl = StaticContentHelperUrl::getRelativeLink($url);
         $img->setAttribute('src', $relativeUrl);
         $body = str_replace('src="' . $url . '"', 'src="' . $relativeUrl . '"', $body);
         self::copyFile($img, 'src');
     }
     foreach ($pictures as $picture) {
         $url = $picture->getAttribute('srcset');
         $relativeUrl = StaticContentHelperUrl::getRelativeLink($url);
         $picture->setAttribute('srcset', $relativeUrl);
         $body = str_replace('src="' . $url . '"', 'src="' . $relativeUrl . '"', $body);
         self::copyFile($picture, 'srcset');
     }
     foreach ($scripts as $script) {
         $url = $script->getAttribute('src');
         $relativeUrl = StaticContentHelperUrl::getRelativeLink($url);
         $script->setAttribute('src', $relativeUrl);
         $body = str_replace('src="' . $url . '"', 'src="' . $relativeUrl . '"', $body);
         self::copyFile($script, 'src');
     }
     if (!empty($pageLinks['print'])) {
         $body = self::fixPrintLinks($body, $pageLinks['print']);
     }
     $body = self::fixMenuLinks($body, $pageLinks);
     $body = self::fixBannersLinks($body);
     // Need to parse DOM again since the original 'base' could
     // have been already replaced.
     $domDocument = new DOMDocument();
     $domDocument->loadHTML($body);
     if ($base = $domDocument->getElementsByTagName('base')) {
         foreach ($base as $node) {
             $href = $node->getAttribute('href');
             $body = str_replace('<base href="' . $href . '" />', '<base href="' . $newBaseURL . '" />', $body);
         }
     }
     unset($domDocument);
     return $body;
 }
Beispiel #2
0
 /**
  * Discover internal links
  * 
  */
 public function _discoverInteralLinks()
 {
     $tmpLinks = array('print' => array(), 'pages' => array());
     foreach ($this->_links['menu'] as $link) {
         $dom = new DomDocument();
         $dom->loadHTML($link->content);
         $aElements = $dom->getElementsByTagName('a');
         foreach ($aElements as $aElement) {
             $href = $aElement->getAttribute('href');
             $full_link = StaticContentHelperUrl::getFullLink($href);
             if (!StaticContentHelperMenu::isSatisfatoryLink($full_link)) {
                 continue;
             }
             $pageName = StaticContentHelperUrl::createPageName($full_link);
             if (StaticContentHelperUrl::isPrintLink($full_link)) {
                 $tmpLinks['print'][$href] = $pageName;
             }
             if (StaticContentHelperUrl::isFeedLink($full_link)) {
                 $relative_link = $pageName;
             } else {
                 $relative_link = StaticContentHelperUrl::getRelativeLink($href);
             }
             if (!$this->existsLink($full_link)) {
                 $cache_id = md5($full_link);
                 $content = $this->_cachePage->get($cache_id);
                 if (empty($content)) {
                     $content = file_get_contents($full_link);
                     $this->_cachePage->store($content, $cache_id);
                 }
                 $link = new stdClass();
                 $link->file = $pageName;
                 $link->full = $full_link;
                 $link->relative = ltrim($relative_link, '/');
                 $link->content = $content;
                 array_push($tmpLinks['pages'], $link);
             }
         }
         unset($dom);
     }
     return $tmpLinks;
 }