4

Apacheのautoindexモジュールを介してPHPを使用してApacheで認証済みファイルリストを作成しようとしています。

私が想像したのは、ApacheにPHPスクリプトをヘッダーファイルとして実行させることでした。Apacheでヘッダーファイルに対してPHPを正しく実行することができ、ログインCookieも正常に検出されます。しかし、Apacheはヘッダーファイルを別のリクエストとして実行しているようです。つまり、PHPからリダイレクトヘッダーを送信しようとすると、実行されません。

私の(簡略化された)Apache構成:

DocumentRoot "/path/to/files_root"
Alias /~extra "/path/to/extra-data"

<Directory "/path/to/extra-data">
    Options -Indexes -MultiViews +Includes
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>

IndexOptions FancyIndexing HTMLTable SuppressHTMLPreamble
AddType text/html .php .html .htm
AddOutputFilter INCLUDES .php
AddHandler application/x-httpd-php .php
HeaderName "/~extra/HEADER.php"

私のHEADER.phpファイル:

<?php

if ( ! my_validate_cookie_function()) {
    header('HTTP/1.1 302 Found');
    header('Location: http://login.example.com/');
    exit(1);
}

そのため、ヘッダーはブラウザに送信されません。Apache環境のviariablesの設定は、HEADER.phpの実行が終了した瞬間に長い間行われていないため、機能していないようです。

Cookie自体は暗号化されているため、PHPで検証する必要があります。

望ましい効果を達成する方法について何か提案はありますか?

4

4 に答える 4

3

index.phpタグに次のコードを含むファイルをディレクトリに挿入する必要があります<body>

function fileindex($folder) {
    if (!is_dir($folder)) {
        return array(); //empty if not a folder
    }
    $list = scandir($folder);
    array_shift($list); //first two values are always . & ..
    array_shift($list);
    return $list;
}
/* auth stuff here */
if (is_auth) {
    echo "<h1> Index of ".getcwd()."</h1>\n<ul>";
    echo "\n<li><a href=\"/\">Parent Directory</a>";
    foreach (fileindex(".") as $i) {
        echo "\n<li><a href=\"".$i."\">".htmlentities($i, ENT_QUOTES|"ENT_HTML401", "UTF-8", true)."</a></li>";
    }
    echo "</ul>";
}

使用できないと言われたのでindex.php、Apacheを使用してディレクトリをリダイレクトし、wherever/other.php?directory=pathそこから作業する必要があります。

.htaccessソリューションでは

RewriteCond %{REQUEST_URI} -d
RewriteRule ^(.*)$ wherever/other.php?directory=$1 [L]

ただし、最も注目すべき点は、フォルダがaや。$_GETではなくパラメータであることに対応するために、PHPコードを少し編集する必要があることです。getcwd()fileindex(".")

于 2012-06-11T15:39:07.363 に答える
0

exit(1)多分問題の1 。その番号を削除してみてください。

私はそれexitがのエイリアスでdieあり、dieすべてのテキストをクライアントに出力することを知っています。

ちなみに使用をお勧めしたいと思いますmod_rewriteRewriteCondクッキーが存在するかどうかを確認できます。セッション変数などをもっとチェックしたい場合、これは機能しません。

于 2012-06-11T13:02:56.827 に答える
0

次のコードを使用できます。

<Directory "/path/to/files_root">
  DirectoryIndex redirected_page.php
</Directory>
于 2012-06-11T13:08:08.577 に答える
0

これが私が最終的に得たものです。称賛は、解決策に向けて私をほのめかしたスコットSに行きます。

Apache構成:

ServerName listing.example.com
# A path to a location which you wanted listed
DocumentRoot "/path/to/listed/location"

<Directory "/path/to/listed/location">
    # Ignore .htaccess files
    AllowOverride None
    # Disable Apache's own indexing, CGI execution and Apache Includes
    Options -Indexes -MultiViews -ExecCGI -Includes FollowSymLinks
    Order allow,deny
    Allow from all
</Directory>

<Directory "/path/to/php/listing/app">
    Options -MultiViews ExecCGI
    Order allow,deny
    Allow from all
</Directory>

<IfModule dir_module>
    # index.php should not be listed here as we will not want PHP scripts to be executed
    DirectoryIndex index.html index.htm
</IfModule>

<IfModule alias_module>
    # This is for CSS/JS files
    Alias /~media /path/to/php/listing/app/media
</IfModule>

<IfModule xsendfile_module>
    # Because the files listed might be large, we want to pass the file handling
    # to Apache via XSendFile Module
    # @see: https://tn123.org/mod_xsendfile/
    XSendFile on
    XSendFilePath "/path/to/listed/location"
</IfModule>

# Uncomment the two setting below if your listing a mounted drive
# EnableSendfile Off
# EnableMMAP Off

<IfModule rewrite_module>
    RewriteEngine On
    # Make sure there are trailing slashes for Directory URLs
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
    RewriteRule ^(.*[^/])$ $1/ [R=302,L]
    # Pass all file+dir requests to a PHP handler
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d [OR]
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f
    RewriteRule .* /path/to/php/listing/app/index.php/$0 [L]
</IfModule>

そして(非常に単純化されたバージョンの)私のphpスクリプト:

<?php

if (my_authentication_check()) {
    $requested_path = realpath($_SERVER['DOCUMENT_ROOT'] . $_SERVER['REQUEST_URI']);
    $filename = substr($requested_path, strrpos($requested_path, DIRECTORY_SEPARATOR) + 1);

    if ($requested_path) {
        if (is_dir($requested_path)) {
            // Show dir contents
            $dir_contents = scandir($requested_path);
        } else {
            // Pass file download to XSendFile module
            header('X-Sendfile: ' . $requested_path);
            header('Content-Type: ' . my_get_file_mime($filename));
            exit;
        }
    } else {
        // display 404 error
    }
} else {
    // redirect to login screen, or display an error... whatever you fancy
}

私は実際に自分のライブラリでKohanaフレームワークを使用して、認証チェックの処理、リストの表示などを行っています。ファイルのアクセス許可も考慮する必要があります。たとえば、* nixシステムでは、dirsの実行可能権限を確認する必要がありますが、Windowsでは読み取りアクセスで十分です。

于 2012-06-24T01:14:07.987 に答える