Hi,

I'm working on a module that needs to be able to dynamically set whether a page is secure or not. What do you think about adding a hook in function securepages_match that let's modules determine if the page should be secure? Something like this:

function securepages_match($path) {
  /** 
   * Check to see if the current menu item has a preference and ignore the
   * secure pages settings
   */
  if (function_exists('menu_get_item')) {
    $item = menu_get_item(menu_get_active_item());
    if (isset($item['secure'])) {
      return $item['secure'];
    }
  }
  /** 
   * Let modules implement a hook to determine if the page should be secure.
   */
  if (function_exists('module_invoke_all')) {
    $return = module_invoke_all('securepage', $path);
    if (in_array(TRUE, $return)) return 1;
  }

  /**
   * Check to see if the page matches the current settings
   */
  $secure = variable_get('securepages_secure', 1);
  $pages = variable_get('securepages_pages', "node/add*\nnode/*/edit\nuser/*\nadmin*");
  $ignore = variable_get('securepages_ignore', "*/autocomplete/*\n*/ajax/*");

  if ($ignore) {
    $regexp = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote($ignore, '/')) .')$/';
    if (preg_match($regexp, $path)) {
      if ($_SERVER['HTTPS'] == 'on') {
        return 1;
      }
      else {
        return 0;
      }
    }
  }
  if ($pages) {
    $regexp = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote($pages, '/')) .')$/';
    return !($secure xor preg_match($regexp, $path)) ? 1 : 0;
  }
  else {
    return;
  }
}
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

grendzy’s picture

Version: 5.x-1.6 » 7.x-1.x-dev
mh86’s picture

Status: Active » Needs review
FileSize
1 KB

Here is another approach in order to allow modules to alter the securepages redirect logic.

I have a very special case, where one page just can not be served as https, but the rest is role based. At the moment the secure pages module would not allow such a thing. With the attached patch other modules have the possibility to influence the redirect mechanism.

klausi’s picture

Title: add a hook? » Redirect alter hook
Issue summary: View changes
FileSize
1.37 KB

Patch does not apply anymore, rerolled.

Dubs’s picture

This is still a valid request I think and would be very useful for module developers. Can this please be applied?

peter.thorndycraft’s picture

Could you please explain the "Patch does not apply anymore, rerolled."??

I also require this functionality. Is there a way to programatically set a page to be secure?

peter.thorndycraft’s picture