2009-05-07 12 views
0

ここにあるものはPHP5と互換性がありませんが、私は完全に何が失われています。私は "AddHandler x-mapp-php5 .php"という行を持っている私のWebサーバー上に.htaccessファイルを持っています。これはサーバー上の他のものすべてが必要とするため、PHP4にデフォルト設定されています。私はこのスクリプトが必要ですが、のみはPHP4で動作します。問題がどこにあるのか?PHP5の非互換性のためのハンティング

<? 
/* config for the script */ 
$download_path = "content"; /* path to your files, NB: no slash at the end */ 
$sort = "asort"; /* array sorting - alphabetical sorting for the array */ 

/* start the script... no more editing from here on... */ 

/* get a list of the files + dirs and turn the list into an array */ 
function file_list($dir) { 
    global $sort; 
    global $file_file_count; 
    if (is_dir($dir)) { 
    $fd = @opendir($dir); 
    while (($part = @readdir($fd)) == true) { 
     clearstatcache(); 
     if ($part != "." && $part != "..") { 
     $file_array[] = $part; 
     } 
    } 
    if ($fd == true) { 
    closedir($fd); 
    } 
    if (is_array($file_array)) { 
    $sort($file_array); 
    $file_file_count = count($file_array); 
    return $file_array; 
    } else { 
    return false; 
    } 
    } else { 
    return false; 
    } 
} 

/* function to convert to Mb, Kb and bytes */ 
function file_size($size) { 
    $megabyte = 1024 * 1024; 
    if ($size > $megabyte) { /* literal.float */ 
     $re_sized = sprintf("%01.2f", $size/$megabyte) . " Mb"; 
    } elseif ($size > 1024) { 
     $re_sized = sprintf("%01.2f", $size/1024) . " Kb"; 
    } else { 
     $re_sized = $size . " bytes"; 
    } 
    return $re_sized; 
} 

/* get a list of the files/dirs, put them into a table. */ 
function generate_file_list($path) { 
    global $download_path; 
    global $PHP_SELF; 
    $final_path = str_replace("//","/",str_replace("..","",urldecode($path))); 
    $file_array = file_list("$download_path/$final_path/"); 
    echo "<b>$final_path/</b>\n"; 
    echo "<br><br>\n\n"; 
    if ($file_array == false) { /* check if the dir is an array before we process it to foreach(); */ 
    echo "directory empty\n"; 
    } else { 
    echo "<table width=\"75%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n"; 
    echo "<tr><td><b>file</b></td><td><b>size</b></td></tr>\n"; 
    foreach ($file_array as $file_name) { 
     $is_file = "$download_path/$final_path/$file_name"; 
     $final_dir_name = urlencode($final_path); /* urlencode(); to prevent any broken links - decode on do_download(); */ 
     $final_file_name = urlencode($file_name); 
     $file_size = filesize("$download_path/$final_path/$file_name"); 
     $final_file_size = file_size($file_size); 
     if (is_file($is_file)) { 
     print "<tr><td><a href=\"$PHP_SELF?go=download&path=$final_dir_name&file=$final_file_name\">$file_name</a></td><td>$final_file_size</td></tr>\n"; 
     } elseif (is_dir($is_file)) { 
     print "<tr><td><a href=\"$PHP_SELF?go=list&path=$final_dir_name/$final_file_name\">$file_name/</a></td><td>&lt;dir&gt;</td></tr>\n"; /* we don't need a size for a directory */ 
     } 
    } 
    echo "</table>\n"; 
    } 
} 
/* allow the user to download the file... */ 
function do_download($path,$file) { 
    global $download_path; 
    $get_path = str_replace("//","/",str_replace("..","",stripslashes(urldecode($path)))); /* fopen adds \ to ' - so we strip 'em. */ 
    $get_file = str_replace("//","/",str_replace("..","",stripslashes(urldecode($file)))); 
    header("Content-Disposition: atachment; filename=$get_file"); 
    header("Content-Type: application/octet-stream"); 
    header("Content-Length: ".filesize("$download_path/$get_path/$get_file")); 
    header("Cache-Control: no-cache, must-revalidate"); 
    header("Pragma: no-cache"); 
    header("Expires: 0"); 
    $fp = fopen("$download_path/$get_path/$get_file","r"); 
    print fread($fp,filesize("$download_path/$get_path/$get_file")); 
    fclose($fp); 
    exit; 
} 

if (!isset($go)) { 
    $go = "dirlist"; 
} if ($go == "dirlist") { 
    generate_file_list(""); /* null, so we get a list for the root directory */ 
    } elseif ($go == "list" && isset($path)) { 
    if (isset($path)) { /* if the path is null - it returns a list for the root directory */ 
     generate_file_list($path); /* get a list for the path specified */ 
    } else { 
     generate_file_list(""); 
    } 
    } elseif ($go == "download") { 
     if (isset($path) && isset($file)) { 
     do_download($path,$file); /* download the file... */ 
     } else { 
    echo "no file selected to download :)\n"; 
    } 
} 
?> 

答えて

1

コードが$が$ファイル、および$パス変数は魔法のように設定されて行くことを想定しています。なぜならRegister Globalsとして知られている何かのあなたのために働く

<? 
/* config for the script */ 
$download_path = 'content'; /*path to your files, NB: no slash at the end*/ 
$sort = 'asort'; /* array sorting - alphabetical sorting for the array */ 

/* start the script... no more editing from here on... */ 
$go = $_REQUEST['go']; 
$file = $_REQUEST['file']; 
$path = $_REQUEST['path']; 

// ... 

この使用:この問題を回避する(とスクリプトを修正)するには、次のようになり、スクリプトの先頭を変更することができます。これはかなり大きなセキュリティ上の問題であったため、現在は無効になっています。

+0

ほとんど、そこにISSET呼び出しがあります。 if(isset($ _REQUEST ['go'])){$ go = $ _REQUEST ['go'];}を実行して、後のコードがまだ動作していることを確認します。 – jmucchiello

+0

これは必ずしも必要ではありません。値がない場合、nullに設定されます。 isset()でNullがfalseに評価されます。 –

+0

素晴らしいです。これは実際に私が以前に拾ったいくつかの古いコードであり、テストの目的でローカルサーバーにダンプしただけです。サーバーはローカルネットワークにしかオープンしていないため、セキュリティは重要ではありません。 –

2

fiXeddとjmucchielloの提案するグローバル登録問題を修正したら、generate_file_list関数の$ PHP_SELFへの参照を削除してください。まず、それはもう存在しません。今は$ _SERVER ['PHP_SELF']ですが、もっと重要なことは、あなたのスクリプトがそれを使用している方法が、あなたをクロスサイトスクリプティングの問題にさらしていることです。

より、この上で読む:http://www.seancoates.com/xss-woes

+0

良いキャッチ、私はそれらを完全に逃した。 :) –

+0

このスレッドを投稿した後も、私は互換性のない狩猟でそれらを修正しましたが、これはローカルサーバだけなので多少は必要ありませんでした...だから自分自身をハッキングする予定がない限り、うまくいくと思います。 :O このコードはかなり古く、私はそれがかなり恐ろしいことが予想されると思います。それは私が必要とするもののために働くと私は本当にそれを書き換えるために時間をかけたくありませんでした。 –

関連する問題