You are here

rss_permissions.module in RSS Permissions 7

Same filename and directory in other branches
  1. 6 rss_permissions.module

File

rss_permissions.module
View source
<?php

/**
 * Implementation of hook_help().
 */
function rss_permissions_help($path, $arg) {
  switch ($path) {
    case 'admin/help#rss_permissions':
      return '<p>' . t('RSS permissions module adds role-based permissions to various RSS feeds on a Drupal site. Go to <a href="@rss_permissions">rss_permissions module</a> section of the Permissions page to set permissions for various feeds per role.', array(
        '@rss_permissions' => url('admin/people/permissions', array(
          'fragment' => 'module-rss_permissions',
        )),
      )) . '</p>' . '<p>' . t('You can manage permissions for main site RSS feed, aggregator RSS feeds (if Aggregator module is enabled), main blog RSS feed and user blog RSS feeds (if Blog module is enabled), and taxonomy RSS feeds (if Taxonomy module is enabled).') . '</p>';
      break;
  }
}

/**
 * Implementation of hook_permission().
 */
function rss_permissions_permission() {
  $rss_permissions = array(
    'access site RSS feed' => array(
      'title' => t('Access site RSS feed'),
    ),
  );
  if (module_exists('taxonomy')) {
    $rss_permissions['access taxonomy RSS feeds'] = array(
      'title' => t('Access taxonomy Term RSS feeds'),
      'description' => t('Only available when the Taxonomy module is enabled.'),
    );
  }
  if (module_exists('blog')) {
    $rss_permissions['access user blog RSS feeds'] = array(
      'title' => t('Access user blog RSS feeds'),
      'description' => t('For feeds typically available at blog/3/feed, for example.'),
    );
    $rss_permissions['access main blog RSS feed'] = array(
      'title' => t('Access main blog RSS feed.'),
    );
  }
  if (module_exists('aggregator')) {
    $rss_permissions['access aggregator RSS feeds'] = array(
      'title' => t('Access Aggregator RSS feeds.'),
    );
  }
  return $rss_permissions;
}

/**
 * Implementation of hook_menu_alter().
 */
function rss_permissions_menu_alter(&$items) {

  // Check permissions for main site RSS feed.
  $items['rss.xml']['access callback'] = 'rss_permissions_menu_access';
  $items['rss.xml']['access arguments'] = array(
    'node',
  );

  // Check permissions for taxonomy RSS feed.
  if (module_exists('taxonomy')) {
    $items['taxonomy/term/%taxonomy_term/feed']['access callback'] = 'rss_permissions_menu_access';
    $items['taxonomy/term/%taxonomy_term/feed']['access arguments'] = array(
      'taxonomy',
    );
  }

  // Check permissions for blog RSS feeds.
  if (module_exists('blog')) {

    // Blog user feeds.
    $items['blog/%user/feed']['access callback'] = 'rss_permissions_menu_access';
    $items['blog/%user/feed']['access arguments'] = array(
      'blog_user',
      1,
    );

    // Main blog feed.
    $items['blog/feed']['access callback'] = 'rss_permissions_menu_access';
    $items['blog/feed']['access arguments'] = array(
      'blog',
    );
  }

  // Check permissions for aggregator RSS feeds.
  if (module_exists('aggregator')) {

    // Aggregator feeds
    $items['aggregator/rss']['access callback'] = 'rss_permissions_menu_access';
    $items['aggregator/rss']['access arguments'] = array(
      'aggregator',
    );

    // OPML (blogroll)
    $items['aggregator/opml']['access callback'] = 'rss_permissions_menu_access';
    $items['aggregator/opml']['access arguments'] = array(
      'aggregator',
    );
  }
}

/**
 * Implementation of hook_theme_registry_alter to override theme_feed_icon().
 */
function rss_permissions_theme_registry_alter(&$theme_registry) {
  $theme_registry['feed_icon']['function'] = 'rss_permissions_theme_feed_icon';
}

/**
 * Overriding theme_feed_icon().
 * Check permission to view the feed before displaying the icon.
 */
function rss_permissions_theme_feed_icon($variables) {
  if (rss_permissions_feed_url_access($variables['url'])) {
    return theme_feed_icon($variables);
  }
  return '';
}

/**
 * Check permissions to access menu callbacks.
 */
function rss_permissions_menu_access($module) {
  switch ($module) {

    // Main site RSS feed.
    case 'node':
      return user_access('access content') && user_access('access site RSS feed');
      break;

    // Taxonomy RSS feeds.
    case 'taxonomy':
      return user_access('access content') && user_access('access taxonomy RSS feeds');
      break;

    // Individual user blog RSS feeds.
    case 'blog_user':
      $account = func_get_arg(1);
      return blog_page_user_access($account) && user_access('access user blog RSS feeds');
      break;

    // Main blog RSS feed.
    case 'blog':
      return user_access('access content') && user_access('access main blog RSS feed');
      break;

    // Aggregator RSS feeds.
    case 'aggregator':
      return user_access('access news feeds') && user_access('access aggregator RSS feeds');
      break;

    // Reasonable default.
    default:
      return user_access('access content');
  }
}

/**
 * This function checks if user has permissions to view a given feed.
 * The logic is slightly different from rss_permissions_menu_access,
 * since we only have the feed URL to work with.
 */
function rss_permissions_feed_url_access($url) {
  global $base_url, $base_path;
  if (strstr($url, $base_url . $base_path)) {
    $url = str_replace($base_url, '', $url);
  }

  // Site's main RSS feed.
  if ('rss.xml' == $url || url('rss.xml', array(
    'absolute' => TRUE,
  )) == $url) {
    return user_access('access content') && user_access('access site RSS feed');
  }

  // Taxonomy RSS feeds.
  if (preg_match('/^.*taxonomy\\/term\\/([0-9]+)\\/feed$/i', $url, $matches) > 0) {
    return user_access('access content') && user_access('access taxonomy RSS feeds');
  }

  // Individual user blog RSS feeds.
  if (preg_match('/^.*blog\\/([0-9]+)\\/feed$/i', $url, $matches) > 0) {
    $blog_uid = $matches[1];
    $account = user_load($blog_uid);
    return blog_page_user_access($account) && user_access('access user blog RSS feeds');
  }

  // Main blog RSS feed.
  if (url('blog/feed') == $url) {
    return user_access('access content') && user_access('access main blog RSS feed');
  }

  // Aggregator RSS feeds.
  if (strpos($url, 'aggregator/rss') != FALSE || url('aggregator/opml') == $url) {
    return user_access('access news feeds') && user_access('access aggregator RSS feeds');
  }

  // Just to be safe, fall back to a reasonable default.
  return user_access('access content');
}

/**
 * Implementation of theme_html_head_alter().
 * Remove RSS link from HEAD, if the user has no permission for that feed.
 */
function rss_permissions_html_head_alter(&$head_elements) {
  foreach ($head_elements as $key => $element) {
    $rel = isset($element['#attributes']['rel']) ? $element['#attributes']['rel'] : NULL;
    if (isset($rel) && $rel == 'alternate') {
      $href = isset($element['#attributes']['href']) ? $element['#attributes']['href'] : NULL;
      if (isset($href) && !rss_permissions_feed_url_access($href)) {
        unset($head_elements[$key]);
      }
    }
  }
}

Functions

Namesort descending Description
rss_permissions_feed_url_access This function checks if user has permissions to view a given feed. The logic is slightly different from rss_permissions_menu_access, since we only have the feed URL to work with.
rss_permissions_help Implementation of hook_help().
rss_permissions_html_head_alter Implementation of theme_html_head_alter(). Remove RSS link from HEAD, if the user has no permission for that feed.
rss_permissions_menu_access Check permissions to access menu callbacks.
rss_permissions_menu_alter Implementation of hook_menu_alter().
rss_permissions_permission Implementation of hook_permission().
rss_permissions_theme_feed_icon Overriding theme_feed_icon(). Check permission to view the feed before displaying the icon.
rss_permissions_theme_registry_alter Implementation of hook_theme_registry_alter to override theme_feed_icon().