You are here

function cdn_get_domains in CDN 6.2

Same name and namespace in other branches
  1. 7.2 cdn.module \cdn_get_domains()

Get all domains from which files might be served. This information is necessary for some modules, e.g. Boost.

Return value

An array of domain names.

3 calls to cdn_get_domains()
cdn_admin_details_form_validate in ./cdn.admin.inc
Default validate callback for the details form.
cdn_cacheaudit in ./cdn.module
Implements hook_cacheaudit().
_cdn_prefetch_dns in ./cdn.module
Helper function to add DNS prefetching to the page.

File

./cdn.module, line 564

Code

function cdn_get_domains() {
  $domains = array();

  // Origin Pull mode domains.
  if (variable_get(CDN_MODE_VARIABLE, FALSE) == CDN_MODE_BASIC) {
    cdn_load_include('basic');
    $mapping = cdn_basic_get_mapping();
    $lines = preg_split("/[\n\r]+/", $mapping, -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.
      $parts = explode('|', $line);

      // It may also list more than one domain.
      foreach (explode(' ', $parts[0]) as $part) {

        // Remove white space.
        $part = trim($part);

        // Allow for protocol-relative domains. We prepend "http:", otherwise
        // parse_url() won't return anything.
        if (strpos($part, '//') === 0) {
          $part = 'http:' . $part;
        }
        $domains[] = parse_url($part, PHP_URL_HOST);
      }
    }
  }
  elseif (variable_get(CDN_MODE_VARIABLE, FALSE) == CDN_MODE_ADVANCED) {
    cdn_load_include('advanced');
    $db = _cdn_advanced_get_db_connection();

    // In case no connection to the database could be made, pretend no
    // domains are being used.
    if (!$db) {
      return array();
    }

    // Retrieve all unique domains (by retrieving one URL per server) and then
    // parsing the domain names in those URLs.
    $sql = "SELECT url\n            FROM synced_files\n            GROUP BY server";
    $stmt = $db
      ->prepare($sql);
    $stmt
      ->execute();
    $rows = $stmt
      ->fetchAll(PDO::FETCH_ASSOC);
    foreach ($rows as $row) {
      $domains[] = parse_url($row['url'], PHP_URL_HOST);
    }
  }
  return array_unique($domains);
}