You are here

pages_restriction.helpers.inc in Pages Restriction Access 7

Contains the helpers functions for the Pages Restriction.

File

includes/pages_restriction.helpers.inc
View source
<?php

/**
 * @file
 * Contains the helpers functions for the Pages Restriction.
 */

/**
 * Update page on cache so it can be retrieved later.
 *
 * @param string $next_page_path
 *   String with path to set on database.
 */
function _pages_restriction_set_next_page_path($next_page_path) {
  global $language;
  $lang_name = $language->language;
  $current_site_languages = language_list('enabled');
  $path_alias = drupal_lookup_path('alias', $next_page_path);
  if ($path_alias == FALSE) {
    $path_alias = $next_page_path;
  }
  $translated_path = _pages_restriction_get_translated_path($path_alias);
  if (strpos($translated_path, $lang_name . "/") !== FALSE) {
    $translated_path = str_replace($lang_name . "/", "", $translated_path);
  }
  $next_translated_path = array();
  if (count($current_site_languages[1]) >= 2 || array_key_exists($lang_name, $current_site_languages[1]) && $lang_name != 'en') {
    $rules = explode(PHP_EOL, variable_get('pages_restriction_rules'));
    $thank_you_id = explode("/", $next_page_path);
    foreach ($rules as $key => $rule) {
      $pages = explode("|", $rule);
      foreach ($pages as $page) {
        $page = preg_replace('/\\/%/', '', $page);
        if ($translated_path == $page) {
          $pages[1] = preg_replace('/\\/%/', '', $pages[1]);
          $next_translated_path[$key] = $pages[1];
          if (!empty($thank_you_id[1])) {
            $next_translated_path[$key] = $pages[1] . "/" . $thank_you_id[1];
          }
          $next_translated_path[$key] = preg_replace("/\r|\n/", "", $next_translated_path[$key]);
          break;
        }
      }
    }
  }
  foreach ($next_translated_path as $key => $path) {
    if ($path_alias === $path) {
      $path_key = $key;
      break;
    }
    if (!array_search($path_alias, $next_translated_path)) {
      $path_alias = current($next_translated_path);
    }
  }
  if ($path_key !== NULL) {
    $path_alias = $next_translated_path[$path_key];
  }
  $rules = _pages_restriction_get_rules_by_path($path_alias);
  foreach ($rules as $key => $rule) {
    if (!isset($_SESSION['pages_restriction_id_session_' . $key])) {

      // Start a custom session and define the useful information.
      $session_id = uuid_generate();
      _pages_restriction_debug_info(t('Session @session started on path @next_page_path'), array(
        '@session' => $session_id,
        '@next_page_path' => $next_page_path,
      ));
      $_SESSION['pages_restriction_id_session_' . $key] = $session_id;
    }

    // Define Array containing cached data.
    $data = array(
      'id_session' => $_SESSION['pages_restriction_id_session_' . $key],
      'next_page_path' => $next_translated_path[$path_key] != $path_alias ? $path_alias : $next_translated_path[$path_key],
      'restrict_rules' => $key,
    );

    // Insert data to cache table.
    if (!is_null($data)) {
      db_merge('cache_pages_restriction')
        ->key(array(
        'id_session' => $_SESSION['pages_restriction_id_session_' . $key],
        'restrict_rules' => $key,
      ))
        ->fields($data)
        ->execute();
    }
  }
}

/**
 * Update page on cache so it can be retrieved later.
 *
 * @param string $path
 *   String with path to be used in rule validation.
 *
 * @return array
 *   Array with all the id pages found in rule validation.
 */
function _pages_restriction_get_rules_by_path($path) {
  $ids = array();
  $rules = explode(PHP_EOL, variable_get('pages_restriction_rules'));
  foreach ($rules as $key => $rule) {
    $pages = explode("|", $rule);
    foreach ($pages as $page) {
      if (preg_match('@^' . str_replace('%', '\\w*', trim($page)) . '$@', trim($path))) {
        $ids[$key] = $pages;
        break;
      }
    }
  }
  return $ids;
}

/**
 * Rule to get the next page path translated for multilingual sites.
 *
 * @param string $path
 *   String current URL Path that users access.
 *
 * @return string
 *   Return the next page path translated.
 */
function _pages_restriction_get_translated_path($path) {
  $translated_path = preg_replace('"/"', '', url(current_path(), array(
    'relative' => TRUE,
  )), 1, $count);
  if ($path == $translated_path) {
    return $translated_path;
  }
  $rules = explode(PHP_EOL, variable_get('pages_restriction_rules'));
  foreach ($rules as $rule) {
    $pages = explode("|", $rule);
    foreach ($pages as $page) {
      $page = preg_replace('/\\/%/', '', $page);
      if ($translated_path == $page) {
        $translated_path = $pages[0];
        break;
      }
    }
  }
  return $translated_path;
}

/**
 * Restriction rules feature for Pages Restriction.
 *
 * @param string $path
 *   String current URL Path that users access.
 * @param string $method
 *   String with method that the access is restricting it.
 *
 * @return bool
 *   Return a TRUE bool value if the access is restricted.
 */
function _pages_restriction_restrict_access(&$path, $method = 'direct function') {

  // Check if the user is 'administrator' or 'Content Editor'.
  if (user_is_logged_in()) {
    global $user;
    $is_admin = in_array('administrator', array_values($user->roles));
    $is_content_editor = in_array('Content Editor', array_values($user->roles));
    if ($is_admin || $is_content_editor) {
      return FALSE;
    }
  }

  // Get data from Cache.
  // Module Load is being added here for performance purposes.
  module_load_include('inc', 'pages_restriction', 'includes/pages_restriction.cache');
  $data = _pages_restriction_get_cached_data();

  // Explode all lines in variable to a item in array.
  $rules = _pages_restriction_get_rules_by_path($path);
  $total_matches = 0;
  foreach ($rules as $key => $rule) {
    $pages = $rule;
    foreach ($pages as $key_page => $page) {

      // If path matches an item on the line, verify it.
      if (preg_match('@^' . str_replace('%', '\\w*', trim($page)) . '$@', $path)) {

        // Increase number of total matches. This will be used for multirules.
        $total_matches++;

        // If it is the first item don't verify.
        if ($key_page === 0) {
          return FALSE;
        }

        // Define watchdog message/params for future use during debug.
        $watchdog_message = t('URL Change From @path to @new_path from @method');
        $watchdog_values = array(
          '@path' => $path,
          '@new_path' => $pages[0],
          '@method' => $method,
        );

        // If there is no session defined, send to first page.
        if (!isset($_SESSION['pages_restriction_id_session_' . $key])) {
          _pages_restriction_debug_info($watchdog_message, $watchdog_values);
          $path = $pages[0];
          return TRUE;
        }

        // If data for this id_session is empty, send to first page.
        if (!isset($data[$_SESSION['pages_restriction_id_session_' . $key]][$key])) {
          _pages_restriction_debug_info($watchdog_message, $watchdog_values);
          $path = $pages[0];
          return TRUE;
        }
        $cached_page = trim($data[$_SESSION['pages_restriction_id_session_' . $key]][$key]);

        // If page matches, stop it.
        if (trim($page) === $cached_page) {

          // If it is the last page,
          // unset the session so a new session ID can be generated.
          if ($key_page === count($pages) - 1) {
            _pages_restriction_unset_sections_with_path($path);
          }
          return FALSE;
        }
      }
    }
  }

  // If at least 1 match happened, we should redirect it to the stored page.
  if ($total_matches > 0) {

    // Consider that the first stored rule is the only important here.
    $key = array_keys($rules)[0];
    $path = $data[$_SESSION['pages_restriction_id_session_' . $key]][$key];
    $watchdog_values['@new_path'] = $path;
    _pages_restriction_debug_info($watchdog_message, $watchdog_values);
    return TRUE;
  }

  // Return FALSE if no condition are matched.
  return FALSE;
}

/**
 * Implements OneTouch Restriction Access.
 *
 * @param string $path_alias
 *   String current URL Path Alias that users access.
 */
function _pages_restriction_unset_sections_with_path($path_alias) {
  module_load_include('inc', 'pages_restriction', 'includes/pages_restriction.cache');
  $watchdog_message = t('Session @session ended on @$path_alias');
  $watchdog_values = array(
    '@$path_alias' => $path_alias,
  );
  $rules = _pages_restriction_get_rules_by_path($path_alias);
  foreach ($rules as $key => $rule) {
    foreach ($rule as $page) {
      if (preg_match('@^' . str_replace('%', '\\w*', trim($page)) . '$@', trim($path_alias))) {
        if (isset($_SESSION['pages_restriction_id_session_' . $key])) {
          $watchdog_values['@session'] = $_SESSION['pages_restriction_id_session_' . $key];
          _pages_restriction_debug_info($watchdog_message, $watchdog_values);
          _pages_restriction_delete_cached_data($_SESSION['pages_restriction_id_session_' . $key], $key);
          unset($_SESSION['pages_restriction_id_session_' . $key]);
        }
      }
    }
  }
}

/**
 * Prints a watchdog message if debug mode is active.
 *
 * @param string $message
 *   String with message. Can contain placeholders.
 * @param array $params
 *   Array with params to use on $message placeholders.
 * @param string $watchdog_type
 *   Type of Watchdog. Defaults to WATCHDOG_INFO.
 */
function _pages_restriction_debug_info($message, array $params = [], $watchdog_type = WATCHDOG_INFO) {
  if (variable_get('pages_restriction_debug_mode', FALSE)) {
    watchdog('pages_restriction', $message, $params, $watchdog_type);
  }
}

Functions

Namesort descending Description
_pages_restriction_debug_info Prints a watchdog message if debug mode is active.
_pages_restriction_get_rules_by_path Update page on cache so it can be retrieved later.
_pages_restriction_get_translated_path Rule to get the next page path translated for multilingual sites.
_pages_restriction_restrict_access Restriction rules feature for Pages Restriction.
_pages_restriction_set_next_page_path Update page on cache so it can be retrieved later.
_pages_restriction_unset_sections_with_path Implements OneTouch Restriction Access.