Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Sitemap
- * Using DOM Document search throught page and extract href attributes
- *
- */
- class Sitemap {
- public $baseUrl;
- private $basePath = '/';
- private $sitemapName = 'sitemap.xml';
- private $disallowedUrls = array('#', 'google', 'facebook', 'twitter', 'status', 'login', 'register', 'wishlist', 'account', 'linkedin', 'checkout');
- public function __construct()
- {
- $this->html = new DOMDocument();
- }
- /**
- * @return string
- */
- public function getBasePath()
- {
- return $this->basePath;
- }
- /**
- * @param string $basePath
- */
- public function setBasePath($basePath)
- {
- $this->basePath = $basePath;
- }
- /**
- * @return string
- */
- public function getSitemapName()
- {
- return $this->sitemapName;
- }
- /**
- * @param string $sitemapName
- */
- public function setSitemapName($sitemapName)
- {
- $this->sitemapName = $sitemapName;
- }
- /**
- * Use this method if want to set custom baseurl other then is setted in SitemapSettings
- *
- * @param string $url
- */
- public function setBaseUrl($url)
- {
- $this->baseUrl = $url;
- }
- /**
- * Generate sitemap and write directly to file
- * Using base url as start point for creating sitemap
- */
- public function generateSitemap()
- {
- $sitemap = $this->_sitemap($this->baseUrl);
- foreach ($sitemap as $addurl )
- {
- if($this->exludedList($addurl) == TRUE){
- $sitemaps[] = $this->_sitemap($addurl);
- }
- }
- $sitemapXML = $this->createXml($sitemaps, $url); // Undefined URL
- return $this->writeToFile($sitemapXML);
- }
- private function _sitemap($url)
- {
- $this->debugError();
- $this->html->loadHTML( file_get_contents($url));
- foreach ( $this->html->getElementsByTagname( 'a' ) as $link ) {
- $urlsite = $link->getAttribute( 'href' );
- if( ($this->exludedList($urlsite) == TRUE) && (!empty($urlsite)) ){
- if( !$this->getUrlProtocol($urlsite) )
- {
- $href[] = $this->baseUrl.trim($urlsite, '/');
- } else {
- if($this->checkExist($urlsite))
- $href[] = $urlsite;
- }
- }
- }
- return $href;
- }
- /**
- * Check if collected url is relative or absolute url
- *
- * @param string $urlsite
- */
- private function getUrlProtocol($urlsite)
- {
- $protocol = parse_url($urlsite,PHP_URL_SCHEME);
- if(empty($protocol)){
- return false;
- }
- return true;
- }
- private function createXml($map, $url){
- $sitemapXML = '<?xml version="1.0" encoding="UTF-8"?>
- <urlset xmlns="http://www.google.com/schemas/sitemap/0.9">';
- foreach ($this->unique($map) as $value) {
- $sitemapXML .= '<url>
- <loc>'. $value .'</loc>
- <lastmod>'. date('Y-m-d') .'T'. date('H:i:s').'+01:00</lastmod>
- <changefreq>daily</changefreq>
- <priority>0.5</priority>
- </url>';
- }
- $sitemapXML .= '</urlset>';
- return $sitemapXML;
- }
- /**
- * This method exlude urls from sitemap if contain value from array
- *
- * @param string $urlForCheck
- */
- public function exludedList($urlForCheck)
- {
- $i = 0;
- foreach ($this->disallowedUrls as $value) {
- if(strpos($urlForCheck, $value) !== FALSE){
- $i++;
- }
- }
- if($i > 0){
- return false;
- } else {
- return true;
- }
- }
- public function unique($input)
- {
- $input = call_user_func_array('array_merge', $input);
- $input = array_unique($input);
- $input = array_values($input);
- return $input;
- }
- private function checkExist($urlsite)
- {
- $ch = curl_init();
- $cOption = array(
- CURLOPT_URL => $urlsite,
- CURLOPT_HEADER => true,
- CURLOPT_NOBODY => true,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_TIMEOUT => 10
- );
- curl_setopt_array($ch, $cOption);
- curl_exec($ch);
- $info = curl_getinfo($ch,CURLINFO_HTTP_CODE);
- if( $info == 0 ){
- return FALSE;
- } else {
- return TRUE;
- }
- }
- public function writeToFile($sitemap)
- {
- $file = $this->getSitemapName();
- $open = fopen($file, 'w');
- if(fwrite($open, $sitemap) !== FALSE){
- $check = 'Mapa sajta je uspešno generisana<br> Link do mape <a href="'. $this->baseUrl .'sitemap.xml" target="_blank">mapa</a>.';
- } else {
- $check = 'Došlo je do greške prilikom generisanja mape';
- }
- fclose($open);
- return $check;
- }
- public function debugError()
- {
- return libxml_use_internal_errors(true);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement