You are here

function _cdn_basic_parse_raw_mapping in CDN 6.2

Same name and namespace in other branches
  1. 7.2 cdn.basic.inc \_cdn_basic_parse_raw_mapping()

Parse the raw (textual) mapping into a lookup table, where the key is the file extension and the value is a list of CDN URLs that serve the file.

Parameters

$mapping_raw: A raw (textual) mapping.

Return value

The corresponding mapping lookup table.

2 calls to _cdn_basic_parse_raw_mapping()
cdn_basic_get_servers in ./cdn.basic.inc
Gets the servers on which a file is available when basic mode is enabled.
cdn_cacheaudit in ./cdn.module
Implements hook_cacheaudit().

File

./cdn.basic.inc, line 89
Logic for basic mode ("Origin Pull mode" in the UI).

Code

function _cdn_basic_parse_raw_mapping($mapping_raw) {
  $mapping = array();
  if (!empty($mapping_raw)) {
    $lines = preg_split("/[\n\r]+/", $mapping_raw, -1, PREG_SPLIT_NO_EMPTY);
    foreach ($lines as $line) {

      // Ignore empty lines.
      $line = trim($line);
      if (empty($line)) {
        continue;
      }

      // Parse this line. It may or may not limit the CDN URL to a list of
      // file extensions.
      if (strpos($line, '|') !== FALSE) {
        $parts = explode('|', $line);
        $cdn_urls = explode(' ', trim($parts[0]));
        $extensions = explode(' ', trim(str_replace('.', '', drupal_strtolower($parts[1]))));

        // Convert to lower case, remove periods, whitespace and split on ' '.
      }
      else {
        $cdn_urls = explode(' ', trim($line));
        $extensions = array(
          '*',
        );

        // Use the asterisk as a wildcard.
      }

      // Remove trailing slash from URLs.
      for ($i = 0; $i < count($cdn_urls); $i++) {
        $cdn_urls[$i] = rtrim($cdn_urls[$i], '/');
      }

      // Create the mapping lookup table.
      foreach ($extensions as $extension) {
        if (!isset($mapping[$extension])) {
          $mapping[$extension] = array();
        }
        $mapping[$extension] = array_merge($mapping[$extension], $cdn_urls);
      }
    }
  }
  return $mapping;
}