私がメインで使用しているプロバイダ(aikis)のユーザーページ一覧機能を模して作ってみたCGIスクリプトです。
機能的には、WWWサーバーのドキュメントルート(ユーザーディレクトリ)をサーチし、各ユーザーのフォルダにindex.htmlがある場合にHTML文書の<TITLE>タグのタイトルを取得してます。その後、更新時刻順にソートして表示しています。
まず、opendir()、readdir()でターゲットになるディレクトリにあるファイルリストを取得します。このリストにはディレクトリだけでなくファイルもリストされているので、次の処理でディレクトリだけを検索します。
続いて、取得したリストで opendir()出来るかどうかテストします。オープンできない場合はファイルと見なし、次のリストの処理に移ります。オープンできる場合はディレクトリですので、その中のindex.htmlを open()してみます。
open()出来る場合は、ファイルの先頭から適当なバイト数(今回は999999バイト。実際にはファイルサイズ分で終わるでしょう)を読み込みます。読み込んだら、処理の都合上、改行コードを削除します。この後<TITLE>を基準に split()関数を使ってタイトルを取得します。(この処理は、ややトリッキーな処理ですね)
この後、ソートのために更新時刻等のファイル情報を stat()関数をつかって取得します。最終更新時刻は $mtimeに格納されているので、これを localtime()関数で時刻データに変換します。
ここまできたら、ディレクトリ名やタイトル、更新時刻等を push()関数で配列にリストしていきます。この時にソートしやすくするために、先頭フィールドに stat()で取得した$mtimeのデータを加えているのが特徴です。
リストアップが終わったら、先頭フィールドの $mtimeをもとに sort()関数で逆順ソートしています。あとは、これを適当な形式(今回はテーブルを使用)で表示していくだけです。
#!/usr/local/bin/perl # require 'jcode.pl'; $taget_dir = "./"; # 検索対象のディレクトリ、サーバーに応じて変更のこと #--------------------------------------------------------- # ターゲットディレクトリの一覧を取得する # ここで得られるリストにはファイルも含まれているので、 # 次の処理でディレクトリのみを検索するようにしている #--------------------------------------------------------- opendir(DIR, $taget_dir); @readlist = readdir(DIR); closedir(DIR); #--------------------------------------------------------- # ターゲットディレクトリ自身をのぞいて、ディレクトリ内の # index.htmlを読み込んで、タイトルを取得する #--------------------------------------------------------- foreach $dir (@readlist) { #------------------------------------------------ # ターゲットディレクトリ以外を検索 #------------------------------------------------ if ( $dir ne "." ) { #-------------------------------------- # ディレクトリとしてオープンできるか? #-------------------------------------- if (opendir(DIR,"$dir")) { # opendir() がtrueの時 #-------------------------------- # index.htmlをオープンできるか? #-------------------------------- if( open( DB, "$taget_dir$dir/index.html")) { #--------------------------------------- # 先頭から999999バイト読み込む # 実際にはファイルサイズ分のことが多い #--------------------------------------- sysread( DB, $line, 999999 ); close(DB); &jcode'convert(*line,'sjis'); $line =~ s/\n//g; $line =~ s/\r//g; $line =~ s/title>/TITLE>/ig; ($temp1, $temp2) = split(/ /ig, $line); ($title, $temp1) = split(/<\/TITLE>/ig, $temp2); #--------------------------------- # ファイル情報(更新時刻)の取得 #--------------------------------- ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = stat("$taget_dir$dir/index.html"); ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($mtime); $month = ($mon + 1); if ($month < 10) { $month = "0$month"; } if ($mday < 10) { $mday = "0$mday"; } #------------------------------------------------------------- # 検索データリストの作成 #------------------------------------------------------------- # 先頭に更新時刻の元データをつけておくことで、ソートしやすく # している #------------------------------------------------------------- push( @dirdata, "$mtime\,$year-$month-$mday\,$dir\,$title" ); } } closedir(DIR); } } #------------------------------------- # 更新された順番にソート(逆順) #------------------------------------- @index = sort {$b cmp $a;} @dirdata; #------------------------------------- # HTML画面の出力 #------------------------------------- print "Content-type: text/html\n\n"; print "\n\n"; print " \n\n"; print "ホームページ一覧 \n"; print "\n\n"; print "\n\n"; print "
\n"; print "\n"; print "ホームページ一覧\n"; print "\n"; exit;
\n"; print "aikisに対抗して、同じようなものを作ってみました。
更新順に表\示されます。\n"; print "\n"; print "
\n"; print "\n"; print " \n"; $i =1; #----------------------------------- # 各項目の出力 #----------------------------------- foreach $temp (@index) { ( $mtime,$fdate,$dir,$title) = split( /,/, $temp); print "No. \n"; print "User \n"; print "Title \n"; print "Update \n"; print "\n"; print " \n"; $i++; } print "$i \n"; print "$dir \n"; print "$title \n"; print "$fdate \n"; print "
\n"; print "