# リンク作成スクリプト
#---------------------------------------
#
# 管理者としてログオンする時は link.php?edit でアクセス
#---------------------------------------
# テンプレートHTMLの特殊タグ
#
# 前!prev> 次へ!next>
# メインコンテンツの内容
# 文字データ@
# 文字データA
# 文字データB
# 文字データC
# !main> メインコンテンツ終わり
# $LinksPerPage個無かった時のHTML!dummy>
# バナー
#---------------------------------------
# 各種設定
#
$User="takebe"; # 管理者名
$Password="365e"; # 管理者パスワード
$Template="link_temp.html"; # テンプレートHTML
$LinksPerPage=8; # 1ページに表示するリンク数 0で全て
$DummyBanner=""; # ダミーバナー(バナーが無いとき表示)
$DataDir="./dat/"; # データフォルダ パーミッションは666か777じゃ無いとダメ
$DataFile="{$DataDir}Link.txt"; # リンクデータファイル
$BannerFile="{$DataDir}Banner.txt"; # リンクデータファイル
#---------------------------------------
# 関数
#
function ReadDataFile($FileName,&$Result,$DataPerLine){
$Result=array();
if(is_file($FileName)){
foreach(file($FileName) as $Line){
foreach($Line=explode("|",$Line) as $K=>$V){$Line[$K]=base64_decode($V);}
if(count($Line)==$DataPerLine){$Result[]=$Line;}
}
}
}
function WriteDataFile($FileName,&$Data,$DataPerLine){
if($FH=fopen($FileName,"wb")){
foreach($Data as $V){
foreach($V as $K=>$VV){$V[$K]=base64_encode($VV);}
if(count($V)==$DataPerLine){fwrite($FH,implode('|',$V)."\n");}
}
fclose($FH);
}
}
#---------------------------------------
# メインプログラム
#
if(!is_dir($DataDir)){
@ mkdir($DataDir);
if($FH=fopen($DataDir.'.htaccess','w')){
fwrite($FH,"deny from all\n\n");
fclose($FH);
}
}
$Self=$_SERVER['PHP_SELF'];
# データの読み込み
ReadDataFile($DataFile,$LinkData,5);
ReadDataFile($BannerFile,$BannerData,2);
$Last=count($LinkData)-1;
# 管理ログオンか?
$Auth=false;
$AuthOpt="?";
if(isset($_GET['edit'])||isset($_POST['edit'])){
$Auth=isset($_SERVER['PHP_AUTH_USER'])&&($_SERVER['PHP_AUTH_USER']==$User)&&($_SERVER['PHP_AUTH_PW']==$Password);
if(!$Auth){header("WWW-Authenticate: Basic realm=\"Link administrator auth\"");header("HTTP/1.0 401 Unauthorized");die("\n
Unauthorized
\n");}
$AuthOpt="?edit&";
$Caption="リンクの追加";$Mode="add";$ModeVal="-1";$wd=0;$_POST['D0']="localhost";
for($i=0;$i<5;$i++){$_POST["D{$i}"] = @ stripcslashes($_POST["D{$i}"]);}
if(isset($_POST['csl'])){
list($_POST['D0'],$_POST['D1'],$_POST['D2'],$_POST['D3'],$_POST['D4'])=array();
}elseif(isset($_POST['add'])){ # 追加処理
if($_POST['D1']!=""){
if($_POST['add']<0){$Last++;$_POST['add']=$Last;}
$LinkData[$_POST['add']]=array($_POST['D0'],$_POST['D1'],$_POST['D2'],$_POST['D3'],$_POST['D4']);$wd++;
}
}elseif(isset($_POST['edt']) && isset($LinkData[$_POST['edit']])){
list($_POST['D0'],$_POST['D1'],$_POST['D2'],$_POST['D3'],$_POST['D4'])=$LinkData[$_POST['edit']];
$Mode="add";$ModeVal=$_POST['edit'];$Caption="リンクの編集";
}elseif(isset($_POST['del']) && isset($LinkData[$_POST['edit']])){
unset($LinkData[$_POST['edit']]);$LinkData=array_values ($LinkData);$Last=count($LinkData)-1;$wd++;
}elseif(isset($_POST['up']) && isset($LinkData[$_POST['edit']]) && isset($LinkData[$_POST['edit']-1])){
$a=$LinkData[$_POST['edit']];$LinkData[$_POST['edit']]=$LinkData[$_POST['edit']-1];$LinkData[$_POST['edit']-1]=$a;$wd++;
}elseif(isset($_POST['dn']) && isset($LinkData[$_POST['edit']]) && isset($LinkData[$_POST['edit']+1])){
$a=$LinkData[$_POST['edit']];$LinkData[$_POST['edit']]=$LinkData[$_POST['edit']+1];$LinkData[$_POST['edit']+1]=$a;$wd++;
}elseif(isset($_POST['ins']) && isset($LinkData[$_POST['edit']])){
if($_POST['edit']){$LinkData=array_merge(array_slice($LinkData,0,$_POST['edit']),array($LinkData[$_POST['edit']]),array_slice($LinkData,$_POST['edit']));
}else{$LinkData=array_merge(array($LinkData[$_POST['edit']]),$LinkData);}
$wd++;
}
if($wd>0){WriteDataFile($DataFile,$LinkData,5);list($_POST['D0'],$_POST['D1'],$_POST['D2'],$_POST['D3'],$_POST['D4'])=array();}
$BCaption="バナーの追加";$BMode="badd";$BModeVal="-1";$wb=0;
$_POST["B1"] = @ stripcslashes($_POST["B1"]);
if(isset($_POST['bca'])){
$_POST["B1"]="";
}elseif(isset($_POST['badd']) && ($_POST["B1"]!="")){
if($_POST['badd']<0){$_POST['badd']=count($BannerData);}
$BannerData[$_POST['badd']]=array($_POST["B0"],$_POST["B1"]);$wb++;
}elseif(isset($_POST['bedt']) && ($_POST["B1"]!="") && isset($BannerData[$_POST['edit']])){
$BannerData[$_POST['edit']]=array($_POST["B0"],$_POST["B1"]);$wb++;
}elseif(isset($_POST['bdel']) && isset($BannerData[$_POST['edit']])){
unset($BannerData[$_POST['edit']]);$BannerData=array_values($BannerData);$wb++;
}
if($wb>0){WriteDataFile($BannerFile,$BannerData,2);}
$_POST["B0"]='';$_POST["B1"]='';
}
# ページ処理
$Page=$_GET['p'] or $_POST['p'] or 0;
$PrevTag=$NextTag="";
if($LinksPerPage){
$Pages=array_chunk($LinkData,$LinksPerPage,TRUE);
unset($LinkData);
$MaxPage=count($Pages)-1;
if(!isset($Pages[$Page])){$Page=0;}
$PageData=$Pages[$Page];
if(!is_array($PageData)){$PageData=array();}
unset($Pages);
if($Page){$PrevTag="\"$Self{$AuthOpt}p=".($Page-1).'"';}
if($Page<$MaxPage){$NextTag="\"$Self{$AuthOpt}p=".($Page+1).'"';}
}else{$PageData=&$LinkData;}
# テンプレートの読み込み
if(is_file($Template)){
$TemplateHtml='';
foreach(file($Template) as $Line){$TemplateHtml.=rtrim($Line)."";}
unset($Line);
}else{die('
テンプレートがありません
');}
# Prev タグ処理
while(preg_match('/(.*?)<\/!prev>/i',$TemplateHtml,$Match)){
$Match[1]=preg_replace('/\"prev.url\"/i',$PrevTag,$Match[1]);
if($PrevTag==""){$Match[1]="";}
$TemplateHtml=str_replace($Match[0],$Match[1],$TemplateHtml);
}
# Next タグ処理
while(preg_match('/(.*?)<\/!next>/i',$TemplateHtml,$Match)){
$Match[1]=preg_replace('/\"next.url\"/i',$NextTag,$Match[1]);
if($NextTag==""){$Match[1]="";}
$TemplateHtml=str_replace($Match[0],$Match[1],$TemplateHtml);
}
# Main/Dummy タグ処理
if(preg_match('/(.*?)<\/!main>/i',$TemplateHtml,$Match)){
$Count=$LinksPerPage;
$RepMatch=$Match[0];
$Main="";
foreach($PageData as $K=>$One){
$Count--;
if($Auth){$OneMain='";}else{$OneMain="";$OneFoot="";}
$OneMain.=preg_replace('/\"link.url\"/i',$One[0],$Match[1]);
for($i=1;$i<5;$i++){$OneMain=preg_replace("//i",$One[$i],$OneMain);}
$Main.="".$OneMain.$OneFoot;
}
unset($OneMain);
if(preg_match('/(.*?)<\/!dummy>/i',$TemplateHtml,$Match)){
$TemplateHtml=str_replace($Match[0],"",$TemplateHtml);
for($i=0;$i<$Count;$i++){$Main.="".$Match[1];}
}
$TemplateHtml=str_replace($RepMatch,$Main,$TemplateHtml);
unset($Main);
unset($RepMatch);
}else{die('
テンプレートにMainが有りません
');}
# バナーの処理
while(preg_match('//i',$TemplateHtml,$Match)){
if(count($BannerData)){
$Banner=array();foreach($BannerData as $K=>$V){$Banner=$Banner+array_fill(count($Banner),$V[0],$K);}
shuffle($Banner);$K=array_shift($Banner);$Banner=$BannerData[$K][1];unset($BannerData[$K]);
}else{$Banner=$DummyBanner;}
$TemplateHtml=preg_replace('//i',$Banner,$TemplateHtml,1);
}
if(!$Auth){die(str_replace('',"\n",$TemplateHtml));}
# /body タグ処理
if(preg_match('/<\/body>/i',$TemplateHtml,$Match)){
$NewForm=<<