Advertisement
imoda

recursive sitemap generator

May 21st, 2012
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.40 KB | None | 0 0
  1. <?php
  2.  
  3.     header("Content-type: text/xml");
  4.     header("Cache-Control: private, max-age=10800, pre-check=10800");
  5.     header("Pragma: private");
  6.     header("Expires: " . date(DATE_RFC822, strtotime("2 day")));
  7.    
  8.     if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
  9.        
  10.         header('Last-Modified: ' . $_SERVER['HTTP_IF_MODIFIED_SINCE'], true, 304);
  11.         exit;
  12.     }
  13.  
  14.     $config = array(
  15.         'startDir' => '.',
  16.         'includeDir' => true,
  17.         'include' => array(
  18.             "hourly" => array(''),
  19.             "daily" => array('pdf'),
  20.             "weekly" => array('php'),
  21.             "monthly" => array('')
  22.         ),
  23.         'exclude' => array(
  24.             '/includes',
  25.             '/xml',
  26.             '/images',
  27.             '/javascript',
  28.             '/css',
  29.             '/sitemap.php'
  30.         )
  31.     );
  32.    
  33.     //begin
  34.    
  35.     echo '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
  36.    
  37.     scanDirectory($config['startDir'], $config);
  38.    
  39.     echo '</urlset>';
  40.  
  41.     function scanDirectory($path, $config) {
  42.        
  43.         if (substr($path, 0, 1) == '.') {
  44.            
  45.             $path = substr($path, 1);
  46.         }
  47.    
  48.         if (is_readable('.' . $path)) {
  49.            
  50.             if (is_dir('.' . $path)) {
  51.                
  52.                 if ($config['includeDir']) {
  53.                    
  54.                     $time = date("c", filemtime($path));
  55.    
  56.                     echo '<url><loc>http://' . $_SERVER["SERVER_NAME"] . $path . '</loc><lastmod>' . $time . '</lastmod><changefreq>weekly</changefreq><priority>0.5</priority></url>';
  57.                 }
  58.                
  59.                 readFiles('.' . $path, $config);
  60.             }
  61.         }
  62.     }
  63.    
  64.     function readFiles($dir, $config) {
  65.        
  66.         $directory_list = opendir($dir);
  67.        
  68.         while($file = readdir($directory_list)) {
  69.            
  70.             if ($file != '.' && $file != '..') {
  71.                
  72.                 $path = $dir . "/" . $file;
  73.                
  74.                 if (is_readable($path)) {
  75.            
  76.                     if (substr($path, 0, 1) == '.') {
  77.                        
  78.                         $path = substr($path, 1);
  79.                     }
  80.            
  81.                     if (!in_array($path, $config['exclude'])) {
  82.                    
  83.                         if (is_dir('.' . $path)) {
  84.                            
  85.                             scanDirectory('.' . $path, $config);
  86.                         }
  87.                         else if (is_file('.' . $path)) {
  88.                            
  89.                             $extension = end(explode('.', $path));
  90.                            
  91.                             foreach($config['include'] as $change => $files) {
  92.                                
  93.                                 if (in_array($extension, $files)) {
  94.                                    
  95.                                     $time = date("c", filemtime('.' . $path));
  96.        
  97.                                     echo '<url><loc>http://' . $_SERVER["SERVER_NAME"] . $path . '</loc><lastmod>' . $time . '</lastmod><changefreq>' . $change . '</changefreq><priority>0.5</priority></url>';
  98.                                 }
  99.                             }
  100.                         }
  101.                     }
  102.                 }
  103.             }
  104.         }
  105.     }
  106.  
  107. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement