web-dev-qa-db-ja.com

「アクセス拒否」ページが出力されたときにモジュールはどのように検出できますか?

Drupalが「アクセス拒否」ページを出力しているとき、モジュールがそれを検出することはどのように可能ですか?
私はDrupal 6でそれを行う方法を知っています。Drupal 7.でそれを行う方法を知る必要があります。

16
kiamlaluno

Drupal 7では、すでに設定されているHTTPヘッダーを返す関数は drupal_get_http_header() であり、パラメーターとしてHTTPヘッダー名が必要です。 authorize_access_denied_pa​​geを見てください。 () 、および drupal_fast_404() コードは、その関数に渡す値を明確にします。

_  // authorize_access_denied_page()
  drupal_add_http_header('Status', '403 Forbidden');
  watchdog('access denied', 'authorize.php', NULL, WATCHDOG_WARNING);
  drupal_set_title('Access denied');
  return t('You are not allowed to access this page.');
_
_// drupal_fast_404()
if ($fast_paths && preg_match($fast_paths, $_GET['q'])) {
  drupal_add_http_header('Status', '404 Not Found');
  $fast_404_html = variable_get('404_fast_html', '<html xmlns="http://www.w3.org/1999/xhtml"><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL "@path" was not found on this server.</p></body></html>');
  // Replace @path in the variable with the page path.
  print strtr($fast_404_html, array('@path' => check_plain(request_uri())));
  exit;
}
_

"Status"ヘッダーが403で始まる場合、Drupalはアクセス拒否ページを出力します。

drupal_get_http_header('Status')の呼び出しが遅れて発生することを確認してください。 hook_init()の呼び出しは早すぎますが、hook_page_alter()(またはテーマのプリプロセスフック)の呼び出しは、更新されたヘッダー情報を持ちます。

10
kiamlaluno

403および404エラーが発生したときに表示するページを設定できます(admin/settings/error-reporting)。

hook_menu()に新しいページを追加し、このページを403エラーコールバックとして設定できると思います。カスタムメニューコールバックがヒットすると、「アクセス拒否」ページが出力されていることがわかります。

13
opi

これはBoost 7.xで行います。それはきれいではありませんが、それは仕事を成し遂げます。

function boost_page_delivery_callback_alter(&$callback, $set = FALSE) {
  if ($callback == 'drupal_deliver_html_page') {
    $callback = 'boost_deliver_html_page';
  }
}

function boost_deliver_html_page($page_callback_result) {
  global $_boost;

  // Menu status constants are integers; page content is a string or array.
  if (is_int($page_callback_result)) {
    // @todo: Break these up into separate functions?
    switch ($page_callback_result) {

      // …

      case MENU_ACCESS_DENIED:
        // 403 page.
        $_boost['menu_item']['status'] = 403;
        break;

      // …

    }
    // …
  }
  // …   
}
12
mikeytown2

モジュールは、ページ「_Administer > Site configuration > Error reporting_」によって変更される「Default 403 (access denied) page」の値を傍受する可能性があります。

  1. _hook_enable_では、_variable_get_/_variable_set_を使用して、既存の値をコピーを2次変数に、そして変数を独自の変数に置き換えますパス(_hook_menu_を使用して登録したもの)。

  2. 「エラー報告」フォームを変更する _hook_form_FORM_ID_alter_を使用して2次変数の読み取り/書き込みを行う

  3. ユーザーから完全に見えないようにする場合は、ページコールバックでdrupal_goto( the_value_of_the_secondary_variable )を呼び出すことができます。

  4. _hook_disable_では、値を復元を2次変数から取得します。


そして、それだけです。「アクセス拒否」がトリガーされたときに、モジュールは通知されますクリーンな方法で(そしてユーザーには見えません)。

4
wildpeaks

確かにPHPのget_headers()関数を使用できますか?

http://php.net/manual/en/function.get-headers.php

返される配列の最初の要素は、応答コードになります。 「403」が含まれている場合、Drupalは「アクセス拒否」ページを返しました。

それを呼ぶのに最適な場所はどこかわかりません。おそらくhook_exit()、あなたのニーズに応じて:

http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hook_exit/6

2
Greg

これは、Drupal 7。

// get the menu router item for the current page
$router_item = menu_get_item();

// if there is no router item, this page is not found
$is_page_not_found_404 = empty($router_item);

// if 'access' is empty for the router item, access is denied
$is_access_denied_403 = empty($router_item['access']);
2
Lindsay