2007年12月26日 星期三

Directory traversal in PHP

好久沒寫有意義的東西了....
這次先丟快兩個月前寫的東西,用來走訪資料夾的內容,並且可以設定最大走訪深度和忽略檔案功能
/**
 * @brief Traverse directories and files.
 * @param $path Start point of traversal.
 * @param $limit Traverse directory level.
 * @param $ignore Regular expression of ignore file.
 */
function listDir( $path = '.', $limit = 0, $ignore = '(^\..*|(.+\.php$)|cgi-bin)' ) {
    static $level = 0;    // Trace back counter.
    if( $limit == 0 || $level < $limit ) {    // If limit is set to zero, or level is lesser than limit, do action.
        $dirHandler = opendir( $path );

        echo "<ul>\n";

        if( !ereg( $ignore, '.' ) || !ereg( $ignore, '..' ) )    // Protection. If '.' or '..' runs recursion, PROGRAM WILL CRASH!
            $ignore = '^\.{1,2}$|'.$ignore;
        while( false !== ( $file = readdir( $dirHandler ) ) ) {
            if( !ereg( $ignore, $file ) ) {
                echo "<li>\n";

                if( is_dir( "$path/$file" ) ) {
                    echo "<strong" . ( ( $level == 0) ? ' class="title"' : '' ) . ">$file<strong>\n";    // First level directory's class is "title"
                    $level++;
                    listDir( "$path/$file", $limit, $ignore );
                    $level--;
                } else {
                    echo '<a href="' . "$path/$file" . '">' . $file . '</a>' . "\n";
                }

                echo "</li>\n";
            }
        }

        echo "</ul>\n";

        closedir( $dirHandler );
    }
}

View by PHP source
如果因為跳出問題很難看的,可以使用上面的連結。
$path為走訪的起點,預設會從當前所在資料夾開始。
$limit是最大走訪深度,0為預設值,代表沒有限制。
$ignore是黑名單的正則表達式,預設會擋'.'和'..'[?],還有*.php以及cgi-bin資料夾。基本上除非很有把握,否則不要擅自更動。
想改輸入效果可以更改echo出來的字串。
為什麼不用Apache?因為它的autoindex實在是看得太清楚了

沒有留言:

張貼留言