Home> programming > ディレクトリ差分パス取得スクリプト

ディレクトリ差分パス取得スクリプト

  • September 5, 2007 10:08 PM
  • programming

自分用のスクリプトで使う予定の部品が出来上がったので公開しておく。

<?php
##############################
# ディレクトリ差分パス取得スクリプト
# PHP script drawn by skim (https://w3neu.net/)
# 注意:ディレクトリ名の大文字小文字は特に区別していない
# 2007Sep05:1.0.0 既知のインジェクションバグがまだ残ってるが、実用上問題無いので放置
# 動作無保証。他はX11ライセンスに準じます。
##############################
if (isset($_GET[a])&&isUrl($_GET[a])) {
$altHost = $_GET[a];
} else {//ネームベースホスト名を強制する場合、サーバのあるディレクトリのファイルパスを適当に記述すればOK
$altHost = "";//基本的にはnullで
# $alttHost = "https://localhost/";//必要に応じて変更可
}
redefineScriptPaths($altHost);//パス周りのグローバル変数を拡張(必須)
$baseUrl = $_SERVER[simpleHost] . $_SERVER[simplePath];//拡張したグローバル変数からスクリプト自身のパス取得
if (isset($_GET[t])&&isUrl($_GET[t])) {
$targetUrl = $_GET[t];
} else {
$targetUrl = $baseUrl;//指定が無ければスクリプト自身の存在するディレクトリをターゲットに設定
# $targetUrl = "https://localhost/";//特にデフォルト指定先が設定したければ変更可
}
$relPath = relPathFromUrl($baseUrl, $targetUrl);//相対パス取得
// ファイル一覧を表示するサンプルここから
if (is_dir("$relPath")){
$dir = opendir("$relPath");
while($file = readdir($dir)) {
if(is_file("$relPath$file")||is_dir("$relPath$file")) {
#echo "<img src=\"$targetUrl$file\"><br />\n";
echo suppleSlash($targetUrl)."$file<br />\n";
}
}
closedir($dir);
}
//サンプルここまで
function isUrl($str) {//urlかどうか判別(日本語URLには非対応)
if (preg_match('/^(https?)(:\/\/[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)$/', $str) && $str!="") {
return 1;
} else {
return 0;
}
}
function suppleSlash($url) {
if (isUrl($url)) {
if (eregi("/$", $url)){
} else {
$url = $url . "/";
}
return $url;
}else {
echo "$url という \nURLではない文字列が入力されているみたいなので処理を中断しました。";
return exit();
}
}
function explodeUrl2Path($url){//URLを使いやすいように分解
if (isUrl($url)) {
if (eregi("/$", $url)){
$url = substr($url, 0, -1);
}
$parsedUrl = parse_url($url);
$pathArray = explode("/", $parsedUrl[path]);
$pathArray[0] = $parsedUrl[host];//先頭には何も入ってないのでホストを突っ込んでおく
return $pathArray;
} else {
echo "$url という \nURLではない文字列が入力されているみたいなので処理を中断しました。";
return exit();
}
}
function relPathFromUrl($selfUrl, $targetUrl){//絶対URLパス同士を比較して相対パスを返す
$selfUrlArray = explodeUrl2Path($selfUrl);
$targetUrlArray = explodeUrl2Path($targetUrl);
if ($selfUrlArray[0]!=$targetUrlArray[0]){
echo "ホストが異なるようなので処理を中断しました。";
return exit();
}
$selfPathCount = count($selfUrlArray) - 1;
for ($i = 1;$i<=$selfPathCount;$i++) {
if ($selfUrlArray[$i]!=$targetUrlArray[$i]) {
break;
}
}
$commonPathCount = $i - 1;
$defPathCount = $selfPathCount - $commonPathCount;
if ($defPathCount==0) {
$relPath = "./";
} else {
$relPath = "" ;
for ($j = 1;$j<=$defPathCount;$j++){
$relPath .= "../";
}
$targetPathCount = count($targetUrlArray) - 1;
for ($k = $i;$k<=$targetPathCount;$k++){
$relPath .= $targetUrlArray[$k] . "/";
}
}
return $relPath;
}
function redefineScriptPaths($altHost) {//自分自身の絶対URLパスを再定義
$altHostArray = parse_url($altHost);//強制的にIPベース=>ネームベースのホスト名にしたい時に上書き
if ($altHostArray[scheme]=="http" && $altHostArray[host]!="") {
$_SERVER[simpleHost] = "https://" . $altHostArray[host] . "/";
} else {
$_SERVER[simpleHost] = "https://" . $_SERVER[SERVER_NAME] . "/";
}
$pathArray = explode("/", $_SERVER[PHP_SELF]);
$fileId = count($pathArray) - 1;
$_SERVER[simpleFile] = $pathArray[$fileId];
$_SERVER[simplePath] = substr(ereg_replace($_SERVER[simpleFile], "", $_SERVER[PHP_SELF]), 1);
$_SERVER[simpleUrl] = $_SERVER[simpleHost] . $_SERVER[simplePath] . $_SERVER[simpleFile];
return $_SERVER;
}

10月の連休で今年こそ完成させる!

Trackbacks:0

TrackBack URL for this entry
https://w3neu.net/mt/mt-tb.cgi/205
Listed below are links to weblogs that reference
ディレクトリ差分パス取得スクリプト from Sequentially Altered Days

Comments:0

Comment Form

Index of all entries

Home> programming > ディレクトリ差分パス取得スクリプト

Categories
Archives
Syndication

Return to page top