You are here

function _site_disclaimer_prep_allowed_paths in Site Disclaimer 7

Same name and namespace in other branches
  1. 6 site_disclaimer.admin.inc \_site_disclaimer_prep_allowed_paths()

Create 'site_disclaimer_allow_nodes' variable from links found in:

  • all translations of Checkbox labels,
  • all translations of Site Disclaimer nodes,

Parameters

$nid: Site Disclaimer node nid.

$delete_nid: If supplied, nid of node being deleted.

2 calls to _site_disclaimer_prep_allowed_paths()
site_disclaimer_admin_settings_submit in ./site_disclaimer.admin.inc
Submit the site_disclaimer_admin_settings form.
_site_disclaimer_nodeapi_work in ./site_disclaimer.module

File

./site_disclaimer.admin.inc, line 636
Administration settings for Site Disclaimer module.

Code

function _site_disclaimer_prep_allowed_paths($nid, $delete_nid = NULL) {
  $allow_nids = array(
    $nid,
  );
  $languages = language_list();
  $nids = _site_disclaimer_nid_all_translations($nid);

  // Get all translations of checkbox label and process links (@link and @"Post title")
  $checkbox_labels = _site_disclaimer_checkbox_label_all_translations();
  foreach ($checkbox_labels as $langcode => $checkbox_label) {
    $use_link = strpos($checkbox_label, '@link') !== FALSE;
    if ($use_link && !empty($nids[$langcode])) {
      $allow_nids[] = $nids[$langcode];
    }
    _site_disclaimer_checkbox_label_substitute_links($checkbox_label, $linked_nodes);
    $allow_nids += $linked_nodes;
  }

  // All Site Disclaimer node translations may be shown - extract links from them
  $links = array();
  foreach ($nids as $langcode => $tnid) {
    $node = node_load($tnid);
    if ($node) {
      $items = field_get_items('node', $node, 'body');
      if ($items) {
        $terms = field_view_value('node', $node, 'body', $items[0]);
        $node_langcode = isset($node->language) ? $node->language : 'und';

        // Get the absolute node path for extraction of relative links.
        $path = url('node/' . $node->nid, array(
          'language' => $node_langcode == 'und' ? NULL : $languages[$node_langcode],
          'absolute' => TRUE,
        ));

        // Extract all links in a node.
        $urls = array_keys(_site_disclaimer_extract_links($terms['#markup'], $path));
        $links += array(
          $langcode => array(),
        );
        $links[$langcode] += $urls;
      }
    }
  }

  // Parse all links, extract only node ids
  global $base_url;
  $len = drupal_strlen($base_url) + 1;

  // Remove base and slash prefix
  foreach ($links as $langcode => $urls) {
    foreach ($urls as $link) {

      // Ignore external links
      if (strpos($link, $base_url . '/') === 0) {
        $start = $len;

        // Allow both clean and non-clean urls
        $no_clean_url = strpos($link, '?q=');
        if ($no_clean_url !== FALSE) {
          $start = $no_clean_url + 3;
        }
        $end = strpos($link, '?', $start);
        if ($end === FALSE) {
          $end = strpos($link, '#', $start);
        }
        if ($end === FALSE) {
          $end = drupal_strlen($link);
        }
        $link = drupal_substr($link, $start, $end - $start);
        $path = drupal_get_normal_path($link, $langcode);
        if (!$path || $path == $link) {
          $path = drupal_get_normal_path($link, '');
        }
        if (!$path || $path == $link) {
          $path = drupal_get_normal_path($link, language_default('language'));
        }
        $ret = preg_match('!^node/([0-9]+)!i', $path, $matches);
        if ($ret == 1 && is_numeric($matches[1]) && (!isset($delete_nid) || $delete_nid != $matches[1])) {
          $allow_nids[] = $matches[1];
        }
      }
    }
  }
  $allow_nids = array_unique($allow_nids);

  // 'site_disclaimer_allow_nodes' lists all nodes that will avoid a goto to an acceptance form.
  variable_set('site_disclaimer_allow_nodes', $allow_nids);
}