Модули Kohana -path .htaccess защита и медиа-файлы - PullRequest
1 голос
/ 30 июня 2011

В Kohana есть modules -путь защиты в .htaccess

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

Как я могу разрешить такие пути, как:

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

Я хотел бы включить в свой модуль javascript и другие медиа-файлы и при этом защитить другие файлы модуля, такие как .php

Я мог бы разрешить целый modules -путь, но тогда все .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.');

Ответы [ 2 ]

1 голос
/ 02 июля 2011

Решение, добавьте этот RewriteCond непосредственно перед RewriteRule

# 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]
1 голос
/ 30 июня 2011

Лучшее решение - обслуживать эти файлы с помощью медиа-контроллера. Таким образом, пользователь может запросить «js / script.js», а Kohana загрузит первый найденный файл, используя каскадную файловую структуру. Есть хороший медиа-контроллер, который поставляется с Kohana, он находится в модуле Userguide:

Строка 247 классов / 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);
    }
}

Это не будет самым быстрым решением, но если вы правильно установите заголовки ответов, файлы должны быть кэшированы в браузере клиента.

...