PHP显示基于文件名的文件链接

时间:2014-05-11 13:12:46

标签: php

我有一个php代码,显示自用户上次访问以来被视为“新/更新”的文件列表。目前脚本扫描服务器并根据文件类型返回结果.pdf / .htm / .php我想知道是否有办法改变这一点,以便脚本根据文件名或$ title返回结果。以下是我认为需要修补的代码片段。

     <?

// declare globals
$docRoot=false;
$treeTop=false;
$debug=false;
$fileCount=0;
$since=false;
$disallow=false;

function startsWith($s1,$s2)
{
    $l1=strlen($s1);
    $l2=strlen($s2);
    if ($l1<$l2)
        return false;
    for ($x=0;$x<$l2;++$x)
    {
        if (strtolower($s1{$x})!=strtolower($s2{$x}))
            return false;
    }
    return true;
}

function mystrtotime($s1)
{
    $y=strtok($s1,"-");
    $m=strtok("-");
    $d=strtok("-");
    return mktime(0,0,0,$m,$d,$y);
}

function readRobotSign()
{
    global $docRoot,$disallow;

    // Initialize.
    $disallow=array();

    $robotSignName=$docRoot."/robots.txt";

    $robotSign=fopen($robotSignName,"r");

    // If there is no no-trespassing sign, then everything is open.
    if ($robotSign===false)
        return;

    $doingMine=false;
    while (!feof($robotSign))
    {
        $lineIn=fgets($robotSign);
        $field=strtok($lineIn," \n\r");
        $value=strtok(" \n\r");

        if (!$doingMine)
        {
            if (strcasecmp($field,"user-agent:")==0
                && (strcasecmp($value,"*")==0 || strcasecmp($value,"whatsnew")==0))
            {
                $doingMine=true;
            }
        }
        else // doingMine
        {
            if (strcasecmp($field,"user-agent:")==0)
                break;
            else if (strcasecmp($field,"disallow:")==0)
                $disallow[]=$value;
        }
    }
    fclose($robotSign);
}

function disallowed($path)
{
    global $disallow;

    $z=count($disallow);
    for ($x=0;$x<$z;++$x)
    {
        if (startsWith($path,$disallow[$x]))
            return true;
    }
    return false;
}

function htmlTitle($fullpath)
{
    $shortName=strrchr($fullpath,'/');
    $x1=strrpos($fullpath,'/');
    $x2=strrpos($fullpath,'.');
    if ($x1!==false && $x2!==false)
        $shortName=substr($fullpath,$x1+1,$x2-$x1-1);
    else
        $shortName=$fullpath;
    $file=fopen($fullpath,"r");
    if (!$file)
        return $shortName;

    $inToken=false;
    $inTitle=false;
    $title="";

    while (($c1=fgetc($file)))
    {
        if ($inToken)
        {
            if ($c1!='>')
              {
                $token.=$c1;
            }
            else
            {
                $inToken=false;
                if (strtolower($token)=="title")
                {
                    $inTitle=true;
                    $title="";
                }
                else if (strtolower($token)=="/title")
                {
                    break;
                }
            }
        }
        else
        {
            if ($c1=='<')
              {
                $inToken=true;
                $token="";
            }
            else if ($inTitle)
                $title.=$c1;
        }
    }
    fclose($file);
    if ($title!='')
        return $title;
    else
        return $shortName;
}

function traverse($path)
{
    global $docRoot,$since,$fileCount;

    if ($path=='/')
        $path='';
    $truepath=$docRoot.$path;
    $dir=@opendir($truepath);
    if (!$dir)
    {
        echo '?? invalid directory ',$path;
        return;
    }
    while (($file1 = readdir($dir)) !== false)
    {
        if ($file1=='.' || $file1=='..')
            continue;
        $relpath=$path.'/'.$file1;
        $fullpath=$truepath.'/'.$file1;

        if (disallowed($relpath)) continue;
        if (is_dir($fullpath))
        {
            traverse($relpath);
        }
        else if (is_file($fullpath))
        {
            $mtime=filemtime($fullpath);
            if ($mtime<$since)
                continue;
            $pType=strrchr($file1,'.');
            if ($pType===false)
                continue;
            else if ($pType=='.htm' || $pType=='.php')
            {
                $title=htmlTitle($fullpath);
            }
            else if ($pType=='.pdf')
            {
                $title=$file1;
            }
            else
                continue; // ignore other file types

            $mtimestr=date('d M Y',$mtime);
            echo "<li><a href=\"$relpath\">$title</a> ($mtimestr)";
            ++$fileCount;
        }
    }
    closedir($dir);
}

function newStuff()
{
    global $treeTop,$fileCount;

    echo '<ul>';
    $fileCount=0;
    traverse($treeTop);
    echo '</ul>';
}

/* "Main" function
Get everything started.
Then the following useful things are available in the template:

newStuff()
$fileCount
$days
$last
$since

*/ 

    global $docRoot,$treeTop,$days,$sinceMethod,$debug,$since,$last;

    $docRoot=$_SERVER['DOCUMENT_ROOT'];

    // Get parms

    if (array_key_exists("tree",$_GET))
        $treeTop=$_GET["tree"];
    else
        $treeTop="/";
    if (array_key_exists("since",$_GET))
        $sinceMethod=$_GET["since"];
    else
        $sinceMethod="d";
    if (array_key_exists("days",$_GET))
        $days=$_GET["days"];
    else
        $days="30";
    if (array_key_exists("debug",$_GET))
        $debug=$_GET["debug"];
    else
        $debug="";

    // Get cookies
    if (array_key_exists("last",$_COOKIE))
    {
        $visitLast=mystrtotime($_COOKIE["last"]);
        if ($visitLast>0)
            $last=date('d M Y',$visitLast); // for display
        else
            $last="unknown";
    }
    else
    {
        $visitLast=0;
        $last="unknown";
    }

    if (array_key_exists("penult",$_COOKIE))
    {
        $visitPenult=mystrtotime($_COOKIE["penult"]);
    }
    else
    {
        $visitPenult=0;
    }

    // Set cookies
    $today=time();
    if ($visitLast>$today-2*24*60*60)
    {
        $newCookies=false;
        $visitLast=$visitPenult;
    }
    else
        $newCookies=true;

    if ($newCookies)
    {
        // last visit is now today
        setcookie("last",date('Y-m-d',$today),$today+180*24*60*60);
        // penultimate visit is now previous last
        if ($visitLast>0)
            setcookie("penult",date('Y-m-d',$visitLast),$today+2*24*60*60);
    }

    // Figure out "since" date
    if ($sinceMethod{0}=='v' && $visitLast>0)
        $since=$visitLast;
    else
    {
        $idays=(int)$days;
        if ($idays==0)
            $idays=30;
        $since=time()-$idays*24*60*60;
    }

    readRobotSign();

?>
<html><head><title>What's New?</title>
<body>

<h1>What's New?</h1>

<hr>

<form action="whatsnew.php" method=get>
List files changed:<p>
  <input type=radio name=since value=days checked>
  within <input name=days size=4 maxlength=4 value=<?=$days?>> days.<p>
  <input type=radio name=since value=visit>
  since my last visit. (<?=$last?>)<p>
<input type=submit value="What's new?">
</form><p>
<?=newStuff()?>
<?=$fileCount?> new or updated files.<p>
<p>

</html>

脚本链接是http://www.nctfleetlist.co.uk/temptestcook/whatsnew.php 提前谢谢

0 个答案:

没有答案