You are here

function _responsive_favicons_validate_tags in Responsive Favicons 8

Same name and namespace in other branches
  1. 7 responsive_favicons.module \_responsive_favicons_validate_tags()

Helper function to check whether responsive favicon files exist and are readable. This function also strips any pasted content that is not a link or a meta tag.

Parameters

string $html: html tag

Return value

array $missing_files

1 call to _responsive_favicons_validate_tags()
responsive_favicons_load_all_icons in ./responsive_favicons.module
Load the responsive favicons that are valid.

File

./responsive_favicons.module, line 112
Contains responsive_favicons.module.

Code

function _responsive_favicons_validate_tags($html) {
  global $base_path;
  $found = [];
  $missing = [];
  $dom = new DOMDocument();
  $dom
    ->loadHTML($html);

  // DRUPAL_ROOT contains the sub directory of the Drupal install (if present),
  // in our case we do not want this as $file_path already contains this.
  $docroot = preg_replace('/' . preg_quote($base_path, '/') . '$/', '/', DRUPAL_ROOT);

  // Find all the apple touch icons.
  $tags = $dom
    ->getElementsByTagName('link');
  foreach ($tags as $tag) {
    $url_path = _responsive_favicons_normalise_path($tag
      ->getAttribute('href'));
    $file_path = parse_url($url_path, PHP_URL_PATH);
    $tag
      ->setAttribute('href', $url_path);
    if (file_exists($docroot . $file_path) && is_readable($docroot . $file_path)) {
      $found[] = $dom
        ->saveXML($tag);
    }
    else {
      $missing[] = $dom
        ->saveXML($tag);
    }
  }

  // Find any Windows 8 meta tags.
  $tags = $dom
    ->getElementsByTagName('meta');
  foreach ($tags as $tag) {
    $name = $tag
      ->getAttribute('name');

    // We only validate the image file.
    if ($name === 'msapplication-TileImage') {
      $url_path = _responsive_favicons_normalise_path($tag
        ->getAttribute('content'));
      $file_path = parse_url($url_path, PHP_URL_PATH);
      $tag
        ->setAttribute('content', $url_path);
      if (file_exists($docroot . $file_path) && is_readable($docroot . $file_path)) {
        $found[] = $dom
          ->saveXML($tag);
      }
      else {
        $missing[] = $dom
          ->saveXML($tag);
      }
    }
    else {
      $found[] = $dom
        ->saveXML($tag);
    }
  }
  return [
    'found' => $found,
    'missing' => $missing,
  ];
}