Advertisement
Guest User

sitemap generator

a guest
May 7th, 2012
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.53 KB | None | 0 0
  1. <?php
  2.  
  3. class Sitemap_Generator
  4. {
  5.     /**
  6.      * Base url - must be slash on the end
  7.      *
  8.      * @access private
  9.      */
  10.     private $_base_url = NULL;
  11.    
  12.     /**
  13.      * Storage all informations about the page
  14.      *
  15.      * @access private
  16.      */
  17.     private $_items = array();
  18.    
  19.     /**
  20.      * Content of sitemap
  21.      *
  22.      * @access private
  23.      */
  24.     private $_contain;
  25.    
  26.     /**
  27.      * Urlset - encapsulates the file and references the current protocol standard
  28.      *
  29.      * @access private
  30.      */
  31.     private $_urlset = 'http://www.sitemaps.org/schemas/sitemap/0.9';
  32.    
  33.     /**
  34.      * Encoding
  35.      *
  36.      * @access private
  37.      */
  38.     private $_encoding = 'UTF-8';
  39.    
  40.     /**
  41.      * XML version
  42.      *
  43.      * @access private
  44.      */
  45.     private $_xml_version = '1.0';
  46.    
  47.     /**
  48.      * How many spaces for indent
  49.      *
  50.      * @access private
  51.      */
  52.     private $_tab = 4;
  53.    
  54.     /**
  55.      * Last mod static
  56.      *
  57.      * @access private
  58.      */
  59.     private $_last_mod_static = FALSE;
  60.    
  61.     /**
  62.      * Set indentation
  63.      *
  64.      * @access private
  65.      * @param $hm - length indentation
  66.      */
  67.     private function _indent($hm = 1)
  68.     {
  69.         $tab = NULL;
  70.        
  71.         for($i = 0; $i < $hm; $i++)
  72.         {
  73.             for($n = 0; $n < $this -> _tab; $n++)
  74.             {
  75.                 $tab .= ' ';
  76.             }
  77.         }
  78.        
  79.         return $tab;
  80.     }
  81.    
  82.     /**
  83.      * In constructor is configured sitemap
  84.      *
  85.      * @access public
  86.      * @param $config - config array
  87.      */
  88.     public function __construct($config = array())
  89.     {
  90.         isset($config['urlset']) ? $this -> _urlset = $config['urlset'] : NULL;
  91.         isset($config['encoding']) ? $this -> _encoding = $config['encoding'] : NULL;
  92.         isset($config['xml_version']) ? $this -> _xml_version = $config['xml_version'] : NULL;
  93.        
  94.         $this -> _contain = NULL; //set empty string
  95.     }
  96.    
  97.     /**
  98.      * Set base url (with slash on the end)
  99.      *
  100.      * @access public
  101.      * @param $url - base url
  102.      */
  103.     public function set_base_url($url)
  104.     {
  105.         $this -> _base_url = $url;
  106.     }
  107.    
  108.     /**
  109.      * Set last mod static
  110.      *
  111.      * @access public
  112.      * @param $date - date
  113.      */
  114.     public function set_last_mod_static($date)
  115.     {
  116.         $this -> _last_mod_static = $date;
  117.     }
  118.    
  119.     /**
  120.      * Add items
  121.      *
  122.      * @access public
  123.      * @param $loc - URL of the page. This URL must begin with the protocol (such as http) and end with a trailing slash, if your web server requires it. This value must be less than 2,048 characters
  124.      * @param $optional - optional tags
  125.      */
  126.     public function add_site($loc, $optional = array())
  127.     {
  128.         $array['loc'] = $this -> _base_url . $loc;
  129.         isset($optional['lastmod']) ? $array['lastmod'] = $optional['lastmod'] : NULL;
  130.         isset($optional['changefreq']) ? $array['changefreq'] = $optional['changefreq'] : NULL;
  131.         isset($optional['priority']) ? $array['priority'] = $optional['priority'] : NULL;
  132.        
  133.         if($this -> _last_mod_static)
  134.         {
  135.             $array['lastmod'] =  $this -> _last_mod_static;
  136.         }
  137.        
  138.         //set item
  139.         $this -> _items[] = $array;
  140.     }
  141.    
  142.     /**
  143.      * Generate sitemap
  144.      *
  145.      * @access public
  146.      */
  147.     public function generate()
  148.     {
  149.         //set begin
  150.         $this -> _contain .= '<?xml version="' . $this -> _xml_version . '" encoding="' . $this -> _encoding . '"?>' . PHP_EOL;
  151.         $this -> _contain .= '<urlset xmlns="' . $this -> _urlset . '">' . PHP_EOL;
  152.        
  153.         //set items
  154.         foreach($this -> _items as $item)
  155.         {
  156.             $this -> _contain .= $this -> _indent() . '<url>' . PHP_EOL;
  157.            
  158.             foreach($item as $key => $value)
  159.             {
  160.                 $this -> _contain .= $this -> _indent(2) . '<' . $key . '>' . $value . '</' . $key . '>' . PHP_EOL;
  161.             }
  162.            
  163.             $this -> _contain .= $this -> _indent() . '</url>' . PHP_EOL;
  164.         }
  165.        
  166.         //set end
  167.         $this -> _contain .= '</urlset>';
  168.        
  169.         return $this -> _contain;
  170.     }
  171.    
  172.     /**
  173.      * Download sitemap
  174.      *
  175.      * @access public
  176.      */
  177.     public function download()
  178.     {
  179.         header("Content-type: text/xml");
  180.         header('Content-Disposition: attachment; filename="sitemap-genereted-' . time() . '.xml"');
  181.        
  182.         echo $this -> generate();
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement