You are here

function _iframe_sanitize in Iframe 6

Cleanup user-entered values for a iframe field according to field settings.

Parameters

$item: A single iframe item, usually containing url, title, and attributes.

$delta: The delta value if this field is one of multiple fields.

$field: The CCK field definition.

$node: The node containing this iframe.

1 call to _iframe_sanitize()
iframe_field in ./iframe.module
Implementation of hook_field().

File

./iframe.module, line 343
Defines simple iframe field types. based on the cck-module "link" by quicksketch MODULE-Funtions

Code

function _iframe_sanitize(&$item, $delta, &$field, &$node) {
  static $iframe_count = 0;

  // unique ids for all iframes on a page, on many-node-pages too
  dmsg(3, 'func _iframe_sanitize');

  // Don't try to process empty iframes.
  if (empty($item['url']) && empty($item['title'])) {
    return;
  }

  // Replace URL tokens.
  if (module_exists('token') && $field['enable_tokens']) {

    // Load the node if necessary for nodes in views.
    $token_node = isset($node->nid) ? node_load($node->nid) : $node;
    $item['url'] = token_replace($item['url'], 'node', $token_node);
  }
  $type = iframe_validate_url($item['url']);
  $url = iframe_cleanup_url($item['url']);

  // Separate out the anchor if any.
  if (strpos($url, '#') !== FALSE) {
    $item['fragment'] = drupal_substr($url, strpos($url, '#') + 1);
    $url = drupal_substr($url, 0, strpos($url, '#'));
  }

  // Separate out the query string if any.
  if (strpos($url, '?') !== FALSE) {
    $item['query'] = drupal_substr($url, strpos($url, '?') + 1);
    $url = drupal_substr($url, 0, strpos($url, '?'));
  }

  // Save the new URL without the anchor or query.
  $item['url'] = $url;

  // Create a shortened URL for display.
  $display_url = $type == iframe_EMAIL ? str_replace('mailto:', '', $url) : url($url, array(
    'query' => isset($item['query']) ? $item['query'] : NULL,
    'fragment' => isset($item['fragment']) ? $item['fragment'] : NULL,
    'absolute' => TRUE,
  ));
  if (drupal_strlen($display_url) > 72) {
    $display_url = drupal_substr($display_url, 0, 72) . "...";
  }
  $item['display_url'] = $display_url;

  // Use the title defined at the field level.
  if ($field['title'] == 'value' && drupal_strlen(trim($field['title_value']))) {
    $title = $field['title_value'];
  }
  else {
    $title = $item['title'];
  }

  // Replace tokens.
  if (module_exists('token') && ($field['title'] == 'value' || $field['enable_tokens'])) {

    // Load the node if necessary for nodes in views.
    $token_node = isset($node->nid) ? node_load($node->nid) : $node;
    $title = filter_xss(token_replace($title, 'node', $token_node), array(
      'b',
      'br',
      'code',
      'em',
      'i',
      'img',
      'span',
      'strong',
      'sub',
      'sup',
      'tt',
      'u',
    ));
    $item['html'] = TRUE;
  }
  $item['display_title'] = empty($title) ? '' : $title;

  // load attributes by node
  _iframe_load($item);

  // Add attributes defined at the widget level
  $attributes = array();
  if (!empty($item['attributes']) && is_array($item['attributes'])) {
    foreach ($item['attributes'] as $attribute => $attbvalue) {
      if (isset($attbvalue) && ($field['attributes'][$attribute] == 'user' || $attribute == 'width' || $attribute == 'class' || $attribute == 'height' || $attribute == 'frameborder' || $attribute == 'scrolling' || $attribute == 'transparency')) {
        $attributes[$attribute] = $attbvalue;
      }
    }
  }

  // Add attributes defined at the field level, if not set
  if (is_array($field['attributes'])) {
    foreach ($field['attributes'] as $attribute => $attbvalue) {
      if (!empty($attbvalue) && $attbvalue != 'default' && $attbvalue != 'user' && !isset($attributes[$attribute])) {
        $attributes[$attribute] = $attbvalue;
      }
    }
  }

  // Remove the rel=nofollow for internal iframes.
  if ($type != iframe_EXTERNAL && isset($attributes['rel']) && strpos($attributes['rel'], 'nofollow') !== FALSE) {
    $attributes['rel'] = str_replace('nofollow', '', $attributes['rel']);
    if (empty($attributes['rel'])) {
      unset($attributes['rel']);
    }
  }
  $item['attributes'] = $attributes;

  // Add the widget label.
  $item['label'] = $field['widget']['label'];

  // Add identifier for the iframe html-code
  $item['html-id'] = 'iframe-' . $iframe_count;
  $iframe_count++;
  if (!isset($item['attributes']['class'])) {
    $item['attributes']['class'] = '';
  }
  $item['attributes']['class'] .= ' iframe-delta-' . $delta;
}