home *** CD-ROM | disk | FTP | other *** search
/ mcgregor.k12.mn.us / www.mcgregor.k12.mn.us.tar / www.mcgregor.k12.mn.us / sitemap / gwsitemap.php < prev    next >
PHP Script  |  2010-09-08  |  5KB  |  186 lines

  1. <?php
  2. /*
  3. File Name: sitemap.php
  4. Author: Gary White
  5. Last modified: April 25, 2006
  6.  
  7. Copyright (C) 2004-2005  Gary White
  8.  
  9. This program is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU General Public License
  11. as published by the Free Software Foundation.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License in the included gpl.txt file for
  17. details.
  18.  
  19. See the readme.txt file for installation and usage.
  20.  
  21. May 12, 2005 - Modified getTitle function to a slighly cleaner
  22. regular expression for extracting the title from files.
  23.  
  24. April 25, 2006 - Fixed small bug to correct display when starting
  25. in a directory other than the site's root.
  26. */
  27.  
  28. // A root relative path to the top level directory you want indexed.
  29. $startin="/";
  30.  
  31. // A root relative path to the location where you place the images.
  32. // Do NOT include a trailing slash. Correct usage would be similar to:
  33. // $imgpath="/images/sitemap";
  34. //
  35. // If you leave it set to an empty string, the program will assume the
  36. // images are located in the same directory as the script.
  37. $imgpath="/sitemap";
  38.  
  39. // The $types array contains the file extensions of files you want to
  40. // show in the site map.
  41. $types=array(
  42.     ".php",
  43.     ".html",
  44.     ".htm",
  45.     ".shtm",
  46.     ".sthml"
  47. );
  48.  
  49. // The $htmltypes is an array containing the file types of HTML files,
  50. // that is files that will contain the HTML <title> tag. The script will
  51. // try to extract the <title> from these files. Any file types indexed
  52. // that are NOT in this array will simply use the file name and not 
  53. // attempt to get the title.
  54. $htmltypes=array(
  55.     ".php",
  56.     ".html",
  57.     ".htm",
  58.     ".shtm",
  59.     ".sthml",
  60. );
  61.  
  62. // Files and/or directories to ignore. Anything in this array will not
  63. // be included in the site map.
  64. $ignore=array(
  65.     ".htaccess",
  66.     "cgi-bin",
  67.     "images",
  68.     "index.htm",
  69.     "index.html",
  70.     "index.php",
  71.     "robots.txt",
  72.     "/content",
  73.     "/images",
  74.     "/slideshow",
  75. );
  76.  
  77. /*
  78. ==============================================
  79. You should not need to make changes below here
  80. ==============================================
  81. */
  82. $id=0;
  83. echo "<div id=\"sitemap\"><ul id=\"list$id\">\n";
  84. $id++;
  85. $divs="";
  86. if(substr($startin,strlen($startin)-1,1)=="/")
  87.     $startin=trim($startin,"/");
  88. foreach($types as $type){
  89.     if (file_exists($_SERVER['DOCUMENT_ROOT']."$startin/index$type")){
  90.         $index=$_SERVER['DOCUMENT_ROOT']."$startin"."/index$type";
  91.         break;
  92.     }
  93. }
  94. $types=join($types,"|");
  95. $types="($types)";
  96. if(!is_array($htmltypes))
  97.     $htmltypes=array();
  98. if(count($htmltypes)==0)
  99.     $htmltypes=$types;
  100. if(!$imgpath)
  101.     $imgpath=".";
  102. echo "<li><img src=\"$imgpath/server.gif\" align=\"left\" alt=\"\" /><strong><a href=\"$startin/\">".getTitle($index)."</a></strong>\n";
  103. showlist($_SERVER['DOCUMENT_ROOT']."$startin");
  104. echo "</li></ul></div>\n";
  105. if (is_array($divs)){
  106.     $divs="'".join($divs,"','")."'";
  107.     echo "<script type=\"text/javascript\">\n";
  108.     echo "//<![CDATA[\n";
  109.     echo "d=Array($divs);\n";
  110.     echo "for (i=0;i<d.length;i++){\n";
  111.     echo "\ttoggle('list'+d[i],'img'+d[i]);\n";
  112.     echo "}\n";
  113.     echo "//]]>\n";
  114.     echo "</script>\n";
  115. }
  116.  
  117.  
  118. function showlist($path){
  119.     global $ignore, $id, $divs, $imgpath, $types, $startin;
  120.     $dirs=array();
  121.     $files=array();
  122.     if(is_dir($path)){
  123.         if ($dir = @opendir($path)) {
  124.             while (($file = readdir($dir)) !== false) {
  125.                 if ($file!="." && $file!=".." && !in_array($file,$ignore)){
  126.                     if (is_dir("$path/$file")){
  127.                         if (file_exists("$path/$file/index.php"))
  128.                             $dirs[$file]=getTitle("$path/$file/index.php");
  129.                         elseif (file_exists("$path/$file/index.html"))
  130.                             $dirs[$file]=getTitle("$path/$file/index.html");
  131.                         elseif (file_exists("$path/$file/index.htm"))
  132.                             $dirs[$file]=getTitle("$path/$file/index.htm");
  133.                         else
  134.                             $dirs[$file]=$file;
  135.                     } else {
  136.                         if (ereg("$types$", $file)){
  137.                             $files[$file]=getTitle("$path/$file");
  138.                             if (strlen($files[$file])==0)
  139.                                 $files[$file]=$file;
  140.                         }
  141.                     }
  142.                 }
  143.           }  
  144.           closedir($dir);
  145.         }
  146.         natcasesort($dirs);
  147.         $url=str_replace($_SERVER['DOCUMENT_ROOT'],"",$path);
  148.         $n=substr_count("$url/$","/");
  149.         $base=substr_count($startin,"/")+1;
  150.         $indent=str_pad("",$n-1,"\t");
  151.         echo "$indent<ul id=\"list$id\">\n";
  152.         if ($n>$base)
  153.             $divs[]="$id";
  154.         $imgsrc="minus";
  155.         foreach($dirs as $d=>$t){
  156.             $id++;
  157.             echo "$indent\t<li><a href=\"javascript:toggle('list$id','img$id')\"><img src=\"$imgpath/$imgsrc.gif\" id=\"img$id\" align=\"middle\" border=\"0\" alt=\"\" /></a>";
  158.             echo "<img src=\"$imgpath/folder.gif\" alt=\"\" align=\"middle\" />";
  159.             echo " <strong><a href=\"$url/$d/\">$t</a></strong>\n";
  160.             showlist("$path/$d");
  161.             echo "$indent\t</li>\n";
  162.         }
  163.         natcasesort($files);
  164.         $id++;
  165.         foreach($files as $f=>$t){
  166.             echo "$indent\t<li><img style=\"padding-left:8px;\" src=\"$imgpath/html.gif\" alt=\"\" border=\"0\" /> <a href=\"$url/$f\">$t</a></li>\n";
  167.         }
  168.         echo "$indent</ul>\n";
  169.     }
  170. }
  171.  
  172. function getTitle($file){
  173.     global $htmltypes;
  174.     $title="";
  175.     $p=pathinfo($file);
  176.     if(!in_array(strtolower($p['extension']),$htmltypes)){
  177.         $f=file_get_contents($file);
  178.         if(preg_match("'<title>(.+)</title>'i", $f, $matches)){
  179.             $title=$matches[1];
  180.         }
  181.     }
  182.     $title=$title?$title:basename($file);
  183.     return htmlentities(trim(strip_tags($title)));
  184. }
  185. ?>
  186.