2011-06-30 1 views
1

modules -path保護があります私のようなパス可能性がどのように:Kohanaのモジュール-path .htaccessの保護とメディア-files

http://localhost/modules/mymodule/media/js/myjavascript.js

を私はしたいと思います私のモジュールにjavascriptやその他のメディアファイルを含めて、他のモジュールファイルを保護してください。.php

modules -pathをすべて許可することができますが、すべて.php-ファイルもリストされます。

# Protect application and system files from being viewed 
RewriteRule ^(?:application|system)\b.* index.php/$0 [L] 

確かに基本的なPHPの - 保護があるが、私はまだ、誰もが私のmodules -pathを一覧表示することができるとは思わないだろう。

<?php defined('SYSPATH') or die('No direct script access.'); 

答えて

1

最良の解決策は、メディアコントローラを使用してこれらのファイルを提供することです。ユーザーは "js/script.js"を要求でき、Kohanaはカスケードファイル構造を使って最初に見つかったファイルをロードします。 Kohanaのが付属して良いメディアコントローラがあります、それは、ユーザガイドのモジュールにあります:

Line 247 of classes/controller/userguide.php

public function action_media() 
{ 
    // Get the file path from the request 
    $file = $this->request->param('file'); 

    // Find the file extension 
    $ext = pathinfo($file, PATHINFO_EXTENSION); 

    // Remove the extension from the filename 
    $file = substr($file, 0, -(strlen($ext) + 1)); 

    if ($file = Kohana::find_file('media/guide', $file, $ext)) 
    { 
     // Check if the browser sent an "if-none-match: <etag>" header, and tell if the file hasn't changed 
     $this->response->check_cache(sha1($this->request->uri()).filemtime($file), $this->request); 

     // Send the file content as the response 
     $this->response->body(file_get_contents($file)); 

     // Set the proper headers to allow caching 
     $this->response->headers('content-type', File::mime_by_ext($ext)); 
     $this->response->headers('last-modified', date('r', filemtime($file))); 
    } 
    else 
    { 
     // Return a 404 status 
     $this->response->status(404); 
    } 
} 

は、これは文句を言わない最速の解決策になるが、あなたは正しくレスポンスヘッダを設定した場合、ファイルが上にキャッシュする必要がありますクライアントブラウザ。

1

ソリューション、ただのRewriteRuleの前にこののRewriteCondを追加

# Protect application and system files from being viewed 
RewriteCond %{REQUEST_URI} !^(.*/)*(application|application/cache|modules/[^/]*)/media/.*$ 
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L] 
関連する問題