[h1]Perl 維護 Web 程式碼: Vovo2000.Com Perl Web Utilities (1) [/h1]
這些 Perl code 是大約 2003 年使用 Windows Server 2003 + Apache 的 Web Server 維護程式 ... 各種老舊!
不過當時可 works,有部分還都一直用到 2007~2008 年左右!
貼出來的原因是,整理房間時,發現一個 8MB CF Card 裡面存了這些 code
(沒看錯, 8MB CF Made in Japan, Canon PowerShot A5Z 送的...)
這些 code 已經十幾年...目前完全沒使用了,若要拿去參考請小心。
[h2]Windows Server 備份 Apache Backup[/h2]
代碼:
#!c:/perl/bin/perl
use File::NCopy qw(copy);
use Time:😜iece;
$file_toBackup = "D:/Vovoutil/BackupUtil/toBackup.txt";
$pathTarget = "E:/backup/".localtime->wday;
$pathTarget = "E:\\backup\\".localtime->wday;
print $pathTarget;
if( open( FILE, $file_toBackup ) )
{
foreach( <FILE> )
{
$a = $_;
system( "mkdir $_" );
print "備份: ".$a.$/;
copy \1, $_, $pathTarget;
}
close( FILE );
}
[h2]Windows deltempfile.bat[/h2]
因為 Session File 會存在 Temp,定時清一下
代碼:
del /f /q c:\winnt\temp\*.*
[h2]Windows deltempfile.bat[/h2]
Goal: 因為 PHP/Apache2 Session File 會存在 Temp,定時清一下
代碼:
del /f /q c:\winnt\temp\*.*
[h2]Replace String in Files Perl Code[/h2]
這段 code 目的很簡單,就是把所有 Files 裡面的 "web.vovo.idv.tw" & "www.vovo2000.com" 改成 "vovo2000.com"
代碼:
#!C:/perl/bin/perl
use File::Find;
sub mysub(); # Our custom subroutine
@directories = ("D:/apache2/htdocs/");
$tar1 = "web.vovo.idv.tw";
$tar2 = "www.vovo.idv.tw";
find(\&mysub, @directories);
sub mysub()
{
my ($filename) = $_;
if ( ($filename =~ /\.htm$/) || ($filename =~ /\.html$/) || ($filename =~ /\.php$/) )
{
print "$filename ";
if( open( FILE, $filename ) )
{
$a = join( "", <FILE> );
close( FILE );
$r = 0;
if ( $a =~ /$tar1/ )
{
$r = 1;
}
if ( $a =~ /$tar2/ )
{
$r = 1;
}
if ( $r == 1 )
{
### 先備份
if ( open( FILE, ">$filename".".orig" ) )
{
print FILE ($a);
close(FILE);
}
### 至換
$a =~ s/$tar1/vovo2000.com/g;
$a =~ s/$tar2/vovo2000.com/g;
if ( open( FILE, ">$filename" ) )
{
print FILE ($a);
close(FILE);
}
print "=============>改".$/;
}
else
{
print "=>不改".$/;
}
}
}
}
[h2]DailyArist.pl Perl Code[/h2]
2000 ~ 2005 年,Vovo2000.com 還是採用 Gallery 1 or Gallery 2
這是產生每日投稿的 Perl Code(從 Gallery 目錄裡面硬 parse)
代碼:
#!C:/Perl/bin/Perl.exe
use File::Find;
use Time:😜iece;
use POSIX qw(strftime);
use Switch;
$timeOneDay = 86400;
$dayMultipler = 1;
$ByteCount = 0;
$fileAll = 0;
$fileJpg = 0;
$fileYes = 0;
$pathToTra = "D:/Apache2/htdocs/gallery/albums/";
$pathWebPreFix = "/gallery/albums/";
$thumbExt = ".thumb.jpg";
$contrib = "distrib";
$roomerlist = "D:/Apache2/htdocs/friend/roomer_list.txt";
$targetPath = "D:/Apache2/htdocs/news/";
$targetExt = "dailyArtist.htm";
$template = "D:/vovoutil/template-dailyArtist.tpl";
@fname=();
@roomer_list=();
if ( open(FILE, $template) )
{
$strTpl = join( "", <FILE>);
close( FILE );
}
if ( open(FILE, $roomerlist) )
{
@roomer_list = <FILE>;
close( FILE );
}
$roomer_count = scalar(@roomer_list);
## 產生 YYYYMMDD.htm
srand;
$intLuckGuy = int(rand()*$roomer_count);
( $a, $b, $c ) = split( ",", $roomer_list[$intLuckGuy] );
( $e, $f ) = split( "\/", $c );
print $e.$/;
print $e.$/;
print $e.$/;
$pathToTra = $pathToTra.$e;
find(\&fileHighLight, $pathToTra);
$img_high = $fname[int(rand()*scalar(@fname))];
print "We show this...: ".$img_high.$/;
$strTpl =~ s/{AUTH}/$e/g;
$strTpl =~ s/{RNAME}/$a/g;
$strTpl =~ s/{HIGHLIGHT}/$img_high/g;
$strTpl =~ s/{FNUM}/$b/g;
$strTpl =~ s/{FIDX}/$c/g;
#print $strTpl.$/;
if( open(FILE, ">$targetPath".$targetExt ) )
{
print FILE ($strTpl);
close( FILE );
}
exit;
sub fileHighLight
{
if ((/$thumbExt/) )
{
$tmp = $File::Find::name;
$tmp2 = $tmp;
$tmp2 =~ s/$pathToTra//g;
#($auth[$fileYes], $fnam[$fileYes])=split( "\/", $tmp2 );
#print $auth[$fileYes].$/;
$fname[$fileYes++] = $tmp2;
print $tmp.$/;
}
}
代碼:
#!C:/perl/bin/perl.exe
$directory2004 = "D:\\Apache2\\htdocs\\news\\2004\\2004*.htm";
$file_tmp = "D:/VovoUtil/tmparch.file";
$file_newsarchive = "D:/Apache2/htdocs/news/news_archives.htm";
$file_template1 = "template-newsArchives.tpl";
$file_roomer_list = "D:/Apache2/Htdocs/friend/roomer_list.txt";
system( "dir $directory2004 > $file_tmp" );
$i = 0;
$j = 0;
$template_str2003 = "[ <a href=\"/news/2004/{HTML}\" title=\"Vovo2000.Com Site News {DATE}\">{DATE}</a> ]";
#####################################
# 取出新聞
#####################################
if ( open( FILE, "<$file_tmp" ) )
{
@a = <FILE>;
foreach( @a )
{
#print $_;
if ( index( $_, ".htm" ) > 0 )
{
$strbuf = $template_str2003;
($c1,$c2,$c3,$c4) = split( / +/, $_ );
$c1 = $c4;
$c1 =~ s/-m//g;
$c1 =~ s/.htm//g;
$c1 = substr( $c1, 0, 4)."-".substr( $c1, 4, 2 )."-".substr( $c1, 6, 2 );
$strbuf =~ s/{DATE}/$c1/g;
$strbuf =~ s/{HTML}/$c4/g;
$strAll .= $strbuf;
if ( $i++ > 8 ) { $i = 0; $strAll .= "<BR />"; }
}
}
close( FILE );
}
print $strAll;
########################################## 把 template 讀進來,然後至換
if ( open( FILE, "<$file_template1" ) )
{
$strTpl1 = join( "", <FILE> );
close( FILE );
}
$loct = localtime();
$strTpl1 =~ s/{V2K_NEWS_THISYEAR_DAYBYDAY}/$strAll/;
$strTpl1 =~ s/{UPDATE_TIME}/$loct/;
#print $strTpl1;
########################################## 寫進去
if ( open( FILE, ">$file_newsarchive" ) )
{
print FILE ($strTpl1);
close( FILE );
}
exit;
[h2]Apache Log Rotate and Restart Perl Code[/h2]
這段 Code 是因為 2003 ~ 2004 年左右的 Apache "for Windows" LogRotate
功能還是很爛! 所以只好自己寫。
代碼:
#!C:/Perl/bin/Perl.exe
($sec,$min,$hour,$mday,$mon,$year,$wday)=(localtime)[0,1,2,3,4,5,6];
$year += 1900;
$mon += 1;
$logg = $year."-".$mon."-".$mday.".log";
print "Stopping APACHE2!\n";
system( "D:\\Apache2\\bin\\apache -k stop" );
print "Renaming!...\n";
system( "move D:\\Apache2\\logs\\access.log D:\\Apache2\\logs\\$logg" );
print "Restarting APACHE2!\n";
system( "D:\\Apache2\\bin\\apache -k start" );
(2014-06-22 18:10)