可读的PHP网站地图:忽略某些无法正常工作的目录

时间:2018-09-05 18:20:23

标签: php html sitemap

我正在尝试将PHP脚本集成到网站上,该网站创建一个站点地图并将其显示为相关页面上的可读html。我不是在寻找搜索引擎的XML网站地图,而是在寻找所有页面和整个网站的可读层次结构概述。

因此,我使用了该现成的脚本http://www.apptools.com/phptools/dynamicsitemap.php并对其进行了修改,以便可以在PHP 7下使用。所做的更改主要涉及PHP手册建议的不推荐使用的功能的替换。由于我不会使用集成的JavaScript函数,因此也没有集成脚本的这一部分。

现在,脚本可以按我的预期正常工作。但是有一个问题使我有些疯狂了。该脚本按字母顺序显示目录和(子)页面。当我排除子目录时,例如ABC,从主目录开始(按字母顺序排序的站点地图中的最后一个目录),子目录在站点地图中消失了(按预期),但主目录也消失了。

示例:主目录= XYZ,子目录= ABC。 ABC将从站点地图中排除,因此不会显示。当我在$ ignore-array中添加“ ABC”(请参见下面的代码)时,它从站点地图中消失,但主目录“ XYZ”也消失了。这仅发生在sitemap-list中的最后一个目录中。

我无法阐明此问题的来源。对于所有其他目录,该代码可以正常工作,据我估计,该代码也应适用于列表中的最后一个目录。

这里的任何人都可以解决此问题吗?提前非常感谢!

<?php
$startin="";

$imgpath="";

$types=array(
".php",
);

$htmltypes=array(
".php",
);

$ignore=array(
"ABC",
);

$id=0;
echo "<div id=\"sitemap\"><ul id=\"list$id\">\n";
$id++;
$divs="";
if(substr($startin,strlen($startin)-1,1)=="/")
$startin=trim($startin,"/");
foreach($types as $type){
if (file_exists($_SERVER['DOCUMENT_ROOT']."$startin/index$type")){
    $index=$_SERVER['DOCUMENT_ROOT']."$startin"."/index$type";
    break;
}
}
$types=join($types,"|");
$types="($types)";
if(!is_array($htmltypes))
$htmltypes=array();
if(count($htmltypes)==0)
$htmltypes=$types;
if(!$imgpath)
$imgpath=".";
echo "<li><strong><a href=\"$startin/\">".getTitle($index)."</a></strong>\n";
showlist($_SERVER['DOCUMENT_ROOT']."$startin");
echo "</li></ul></div>\n";
if (is_array($divs)){
$divs="'".join($divs,"','")."'";
echo "<script type=\"text/javascript\">\n";
echo "//<![CDATA[\n";
echo "d=Array($divs);\n";
echo "for (i=0;i<d.length;i++){\n";
echo "\ttoggle('list'+d[i],'img'+d[i]);\n";
echo "}\n";
echo "//]]>\n";
echo "</script>\n";
}

function showlist($path){
global $ignore, $id, $divs, $imgpath, $types, $startin;
$dirs=array();
$divs=array();
$files=array();
if(is_dir($path)){
    if ($dir = @opendir($path)) {
        while (($file = readdir($dir)) !== false) {
            if ($file!="." && $file!=".." && !in_array($file,$ignore)){
                if (is_dir("$path/$file")){
                    if (file_exists("$path/$file/index.php"))
                        $dirs[$file]=getTitle("$path/$file/index.php");
                    elseif (file_exists("$path/$file/index.html"))
                        $dirs[$file]=getTitle("$path/$file/index.html");
                    elseif (file_exists("$path/$file/index.htm"))
                        $dirs[$file]=getTitle("$path/$file/index.htm");
                    else
                        $dirs[$file]=$file;
                } else {
                    if (preg_match("$types", $file)){
                        $files[$file]=getTitle("$path/$file");
                        if (strlen($files[$file])==0)
                            $files[$file]=$file;
                    }
                }
            }
      }  
      closedir($dir);
    }
    natcasesort($dirs);
    $url=str_replace($_SERVER['DOCUMENT_ROOT'],"",$path);
    $n=substr_count("$url/$","/");
    $base=substr_count($startin,"/")+1;
    $indent=str_pad("",$n-1,"\t");
    echo "$indent<ul id=\"list$id\">\n";
    if ($n>$base)
        $divs[]="$id";
    foreach($dirs as $d=>$t){
        $id++;
        echo "$indent\t<li><a href=\"javascript:toggle('list$id','img$id')\"></a>";
        echo " <strong><a href=\"$url/$d/\">$t</a></strong>\n";
        showlist("$path/$d");
        echo "$indent\t</li>\n";
    }
    natcasesort($files);
    $id++;
    foreach($files as $f=>$t){
        echo "$indent\t<li> <a href=\"$url/$f\">$t</a></li>\n";
    }
    echo "$indent</ul>\n";
}
}

function getTitle($file){
global $htmltypes;
$title="";
$p=pathinfo($file);
if(!in_array(strtolower($p['extension']),$htmltypes)){
    $f=file_get_contents($file);
    if(preg_match("'<title>(.+)</title>'i", $f, $matches)){
        $title=$matches[1];
    }
}
$title=$title?$title:basename($file);
return htmlentities(trim(strip_tags($title)));
}
?>

0 个答案:

没有答案