You are here

function cf_http_reduce_html_headers in Common Functionality 7

Same name and namespace in other branches
  1. 7.2 modules/cf_http/cf_http.module \cf_http_reduce_html_headers()

Reads and processes a website page at the given path.

Why: Custom php scripts need a straight-forward and easy way to pull data from another website. This is useful as an alternative to iframe and has advantages and disadvantages to iframes. An advantage is that this allows showing remote content even if the remote url is down (via caching). A disadvantage is that remote images and links need to be processed, updated, and possibly even manually cached.

Parameters

$text: Hostname or ip address of the server. Should not contain http:// or similary prefixes.

$depth: The amount of shrinkage to perform. Any number from 1 to 6.

$preserve: A boolean representing whether or not to preserve the header structure when the depth of a given header is reduced to a number greater than 6. If preserve is false, all header formatting will be lost.

array $function_history: (optional) An array of function names, ie: array('0' => 'my_function_name').

Return value

array An array containing the connection status and return http response. The array keys:

  • reduced: A boolean with TRUE representing that the text was successful

reduced and FALSE otherwise.

  • text: The http text with all html headers reduced by $depth.

File

modules/cf_http/cf_http.module, line 576

Code

function cf_http_reduce_html_headers($text, $depth = 1, $preserve = TRUE, array $function_history = array()) {
  cf_error_append_history($function_history, __FUNCTION__);
  $results = array(
    'reduced' => FALSE,
    'text' => '',
  );
  if (!is_string($text)) {
    cf_error_not_string($function_history, 'text');
    return $results;
  }
  if ($depth < 0 || $depth > 6) {
    return $results;
  }
  $results['text'] = $text;
  foreach (array(
    6,
    5,
    4,
    3,
    2,
    1,
  ) as $number) {
    $reduced = $number + $depth;
    $tag = 'h' . $reduced;
    $matches = array();
    if ($reduced > 6) {
      $tag = 'div';
    }
    if ($tag != 'div' || $preserve) {
      if (preg_match_all('/<h' . $number . '([^>]*)>/i', $results['text'], $matches) > 0) {
        $results['text'] = preg_replace('/<h' . $number . '>/i', '<' . $tag . ' class="cf_http-was_h' . $number . '">', $results['text']);
        foreach ($matches[1] as $match_key => $match) {
          if (!empty($match)) {
            $class_matches = array();
            if (preg_match('/class="([^"]*)"/i', $match, $class_matches) == 0) {
              $class_matches = array();
              if (preg_match("/class='([^']*)'/i", $match, $class_matches) == 0) {
                $results['text'] = preg_replace('/<h' . $number . '([^>]*)>/i', '<' . $tag . ' ${1} class="cf_http-was_h' . $number . '">', $results['text']);
              }
              else {
                $new_attributes = preg_replace("/\\bclass='([^']*)'/i", "class='" . $class_matches[1] . ' cf_http-was_h' . $number . "'", $match);
                $results['text'] = preg_replace('/<h' . $number . '[^>]*>/i', '<' . $tag . ' ' . $new_attributes . '>', $results['text']);
              }
            }
            else {
              $new_attributes = preg_replace('/\\bclass="([^"]*)"/i', 'class="' . $class_matches[1] . ' cf_http-was_h' . $number . '"', $match);
              $results['text'] = preg_replace('/<h' . $number . '[^>]*>/i', '<' . $tag . ' ' . $new_attributes . '>', $results['text']);
            }
          }
        }
        $results['text'] = preg_replace('/<\\/h' . $number . '>/i', '</' . $tag . '>', $results['text']);
      }
    }
    else {
      $results['text'] = preg_replace('/<h' . $number . '([^>]*)>/i', '<' . $tag . ' ${1}' . '">', $results['text']);
      $results['text'] = preg_replace('/<\\/h' . $number . '>/i', '</' . $tag . '>', $results['text']);
    }
  }
  if (!is_string($results['text'])) {
    $results['text'] = '';
    return $results;
  }
  $results['reduced'] = TRUE;
  return $results;
}