Advertisement
Guest User

Untitled

a guest
Aug 31st, 2015
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.38 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Sitemap
  5.  * Using DOM Document search throught page and extract href attributes
  6.  *
  7.  */    
  8. class Sitemap {
  9.    
  10.     public $baseUrl;
  11.  
  12.     private $basePath = '/';
  13.  
  14.     private $sitemapName = 'sitemap.xml';
  15.  
  16.     private $disallowedUrls =  array('#', 'google', 'facebook', 'twitter', 'status', 'login', 'register', 'wishlist', 'account', 'linkedin', 'checkout');
  17.  
  18.    
  19.     public function __construct()
  20.     {
  21.         $this->html = new DOMDocument();
  22.     }
  23.     /**
  24.      * @return string
  25.      */
  26.     public function getBasePath()
  27.     {
  28.         return $this->basePath;
  29.     }
  30.  
  31.     /**
  32.      * @param string $basePath
  33.      */
  34.     public function setBasePath($basePath)
  35.     {
  36.         $this->basePath = $basePath;
  37.     }
  38.  
  39.     /**
  40.      * @return string
  41.      */
  42.     public function getSitemapName()
  43.     {
  44.         return $this->sitemapName;
  45.     }
  46.  
  47.     /**
  48.      * @param string $sitemapName
  49.      */
  50.     public function setSitemapName($sitemapName)
  51.     {
  52.         $this->sitemapName = $sitemapName;
  53.     }
  54.     /**
  55.      * Use this method if want to set custom baseurl other then is setted in SitemapSettings
  56.      *
  57.      * @param string $url
  58.      */
  59.     public function setBaseUrl($url)
  60.     {
  61.         $this->baseUrl = $url;
  62.     }
  63.    
  64.     /**
  65.      * Generate sitemap and write directly to file
  66.      * Using base url as start point for creating sitemap
  67.      */
  68.     public function generateSitemap()
  69.     {      
  70.         $sitemap = $this->_sitemap($this->baseUrl);
  71.        
  72.         foreach ($sitemap as $addurl )
  73.         {
  74.             if($this->exludedList($addurl) == TRUE){
  75.                  $sitemaps[] = $this->_sitemap($addurl);
  76.             }
  77.         }
  78.        
  79.         $sitemapXML = $this->createXml($sitemaps, $url);  // Undefined URL
  80.        
  81.         return $this->writeToFile($sitemapXML);
  82.     }
  83.    
  84.    
  85.     private function _sitemap($url)
  86.     {
  87.         $this->debugError();
  88.         $this->html->loadHTML( file_get_contents($url));
  89.    
  90.         foreach ( $this->html->getElementsByTagname( 'a' ) as $link ) {
  91.             $urlsite = $link->getAttribute( 'href' );
  92.            
  93.             if( ($this->exludedList($urlsite) == TRUE) && (!empty($urlsite)) ){
  94.                 if( !$this->getUrlProtocol($urlsite) )
  95.                 {
  96.                     $href[] = $this->baseUrl.trim($urlsite, '/');
  97.                 } else {
  98.                     if($this->checkExist($urlsite))
  99.                         $href[] = $urlsite;
  100.                 }
  101.             }
  102.         }
  103.        
  104.         return $href;
  105.     }
  106.  
  107.  
  108.     /**
  109.      * Check if collected url is relative or absolute url
  110.      *
  111.      * @param string $urlsite
  112.      */
  113.     private function getUrlProtocol($urlsite)
  114.     {
  115.         $protocol = parse_url($urlsite,PHP_URL_SCHEME);
  116.  
  117.         if(empty($protocol)){
  118.             return false;
  119.         }
  120.  
  121.         return true;
  122.     }
  123.  
  124.    
  125.     private function createXml($map, $url){
  126.        
  127.         $sitemapXML = '<?xml version="1.0" encoding="UTF-8"?>
  128.                         <urlset xmlns="http://www.google.com/schemas/sitemap/0.9">';
  129.                    
  130.         foreach ($this->unique($map) as $value) {
  131.    
  132.             $sitemapXML .= '<url>
  133.                         <loc>'. $value .'</loc>
  134.                         <lastmod>'. date('Y-m-d') .'T'. date('H:i:s').'+01:00</lastmod>
  135.                         <changefreq>daily</changefreq>
  136.                         <priority>0.5</priority>
  137.                       </url>';
  138.         }
  139.        
  140.         $sitemapXML .= '</urlset>';
  141.        
  142.         return $sitemapXML;
  143.     }
  144.    
  145.     /**
  146.      * This method exlude urls from sitemap if contain value from array
  147.      *
  148.      * @param string $urlForCheck
  149.      */
  150.     public function exludedList($urlForCheck)
  151.     {
  152.        
  153.         $i = 0;
  154.         foreach ($this->disallowedUrls as $value) {
  155.            
  156.             if(strpos($urlForCheck, $value) !== FALSE){
  157.                 $i++;
  158.             }
  159.         }
  160.        
  161.         if($i > 0){
  162.             return false;
  163.         } else {
  164.             return true;
  165.         }
  166.     }
  167.    
  168.     public function unique($input)
  169.     {
  170.         $input = call_user_func_array('array_merge', $input);
  171.         $input = array_unique($input);
  172.         $input = array_values($input);
  173.        
  174.         return $input;
  175.     }
  176.    
  177.     private function checkExist($urlsite)
  178.     {
  179.         $ch = curl_init();
  180.        
  181.         $cOption = array(
  182.             CURLOPT_URL => $urlsite,
  183.             CURLOPT_HEADER => true,
  184.             CURLOPT_NOBODY => true,
  185.             CURLOPT_RETURNTRANSFER => true,
  186.             CURLOPT_TIMEOUT => 10
  187.         );
  188.        
  189.         curl_setopt_array($ch, $cOption);
  190.        
  191.         curl_exec($ch);
  192.         $info = curl_getinfo($ch,CURLINFO_HTTP_CODE);
  193.        
  194.         if( $info == 0 ){
  195.             return FALSE;
  196.         } else {
  197.             return TRUE;
  198.         }
  199.     }
  200.    
  201.     public function writeToFile($sitemap)
  202.     {
  203.         $file = $this->getSitemapName();
  204.         $open = fopen($file, 'w');
  205.        
  206.         if(fwrite($open, $sitemap) !== FALSE){
  207.             $check = 'Mapa sajta je uspešno generisana<br> Link do mape <a href="'. $this->baseUrl .'sitemap.xml" target="_blank">mapa</a>.';
  208.         } else {
  209.             $check = 'Došlo je do greške prilikom generisanja mape';
  210.         }
  211.         fclose($open);
  212.        
  213.         return $check;
  214.     }
  215.    
  216.     public function debugError()
  217.     {
  218.         return libxml_use_internal_errors(true);
  219.     }
  220. }
  221.  
  222. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement