You are here

context_http_headers.module in Context HTTP Headers 7

Adds HTTP Header reaction to Context

File

context_http_headers.module
View source
<?php

/**
 * @file
 * Adds HTTP Header reaction to Context
 */

/**
 * Implements hook_context_http_headers_def().
 */
function context_http_headers_context_http_header_def() {
  return array(
    'extra_headers' => array(
      '#title' => t('Extra HTTP Headers'),
      '#description' => t("Enter one HTTP header per line\n with the form 'KEY:VALUE', no spaces!\n \n      To remove a header use 'KEY:'\n Headers are replaced in full. "),
      '#type' => 'textarea',
    ),
  );
}

/**
 * Implements hook_context_http_headers_build().
 * 
 * Should return array in the format of:  
 * array( 
 *    'header-1' => array('value1', 'value2', etc),
 *    'header-2' => array(...),
 * )
 */
function context_http_headers_context_http_header_build($http_header_items) {
  $keyed_headers = array();
  if ($http_header_items['extra_headers']) {
    $extra_headers = explode("\n", $http_header_items['extra_headers']);
    $values = array();
    foreach ($extra_headers as $extra_header) {
      list($header, $value) = explode(":", $extra_header);
      $keyed_headers[$header][] = trim($value);
    }
  }
  return $keyed_headers;
}

/**
 * Helper function to operate on the set of headers for each hook.
 *  
 * Concatenates several values into one string so they can be set all at once.
 *
 * @param array $header_builds
 *   containing all the headers for its hook.
 *   array( 
 *     'header' => array( 'key1' => 'value1', 'key2' => 'value2', ... ) 
 *   )
 * 
 * @return array
 *   array of string headers
 *   array(
 *     'ANOTHER' => 'ecp=test,!stop,with-cache',
 *     'EXAMPLE' => 'another,header,for,this-hook'
 *   )
 */
function _context_http_headers_header_map($header_builds) {
  $headers = array();
  foreach ($header_builds as $key => $value) {
    if (is_array($value)) {
      $value = implode(",", $value);
    }

    // Cleans all array values prior to returning header values.
    $headers[$key] = check_plain($value);
  }
  return $headers;
}

/*****************************************
 * context 3 integration
 *****************************************/

/**
 * Implements hook_ctools_plugin_api().
 * 
 * Make module compatible with context 3
 */
function context_http_headers_ctools_plugin_api($module, $api) {
  if ($module == 'context' && $api == 'plugins') {
    return array(
      'version' => 3,
    );
  }
}

/**
 * Implements hook_context_plugins().
 * 
 * Make module compatible with context 3
 */
function context_http_headers_context_plugins() {
  $plugins = array();
  $plugins['context_reaction_http_header'] = array(
    'handler' => array(
      'path' => drupal_get_path('module', 'context_http_headers'),
      'file' => 'context_reaction_http_header.inc',
      'class' => 'context_reaction_http_header',
      'parent' => 'context_reaction',
    ),
  );
  return $plugins;
}

/**
 * Implements hook_context_registry().
 * 
 * Make module compatible with context 3
 */
function context_http_headers_context_registry() {
  $registry['reactions'] = array(
    'http_header' => array(
      'title' => t('HTTP Headers'),
      'plugin' => 'context_reaction_http_header',
    ),
  );
  return $registry;
}

/**
 * Implements hook_context_page_reaction().
 * 
 * Adds HTTP headers based on the active context
 */
function context_http_headers_context_page_reaction() {
  if (function_exists("context_context_reactions")) {
    _do_context2_reaction();
  }
  else {
    if ($plugin = context_get_plugin('reaction', 'http_header')) {
      $plugin
        ->execute();
    }
  }
}