You are here

function minisite_site_parse in Mini site 7

Parse minisite.

1 call to minisite_site_parse()
minisite_site_build in ./minisite.module
Build minisite.

File

includes/minisite.site.inc, line 105
minisite.site.inc

Code

function minisite_site_parse($minisite_info) {
  list($minisite_site_request, $minisite_site_url, $minisite_entity_alias) = minisite_site_paths_info($minisite_info);

  // Make sure a HTML file is called.
  if (substr($minisite_site_request, -5, 1) != '.') {
    $minisite_site_request = $minisite_site_request . '/index.html';
  }

  // Bypass lib error.
  libxml_use_internal_errors(TRUE);
  $stream_context_options = [
    "ssl" => [
      "verify_peer" => FALSE,
      "verify_peer_name" => FALSE,
    ],
  ];
  $stream_context = stream_context_create($stream_context_options);
  libxml_set_streams_context($stream_context);
  $document = new \DOMDocument();
  $document
    ->loadHTMLFile($minisite_site_request);

  // Return 404 if document is empty.
  if (empty($document) || empty($document->textContent)) {
    return FALSE;
  }
  $head = $document
    ->getElementsByTagName('head')
    ->item(0);

  // Return 404 is document head is empty.
  if (empty($head)) {
    return FALSE;
  }
  else {

    // Update base href.
    $tag_base = $document
      ->createElement('base');
    $tag_base
      ->setAttribute('href', $minisite_site_url);
    if ($head
      ->hasChildNodes()) {
      $head
        ->insertBefore($tag_base, $head->firstChild);
    }
    else {
      $head
        ->appendChild($tag_base);
    }
  }

  // Generate minisite full alias.
  $minisite_full_alias = url($minisite_entity_alias, [
    'absolute' => TRUE,
  ]);

  // Update link tag.
  foreach ($document
    ->getElementsByTagName('a') as $item) {
    $href = $item
      ->getAttribute('href');

    // Keep absolute URL.
    if (parse_url($href, PHP_URL_SCHEME) != '') {
      continue;
    }

    // If href starts with . or root link.
    if (substr($href, 0) == '.' || substr($href, 0) == '/') {
      continue;
    }

    // If href starts with relative path.
    if (substr($href, 0, 2) == '..') {
      $item
        ->setAttribute('href', $minisite_full_alias . '/' . substr($href, 3));
      continue;
    }

    // If href is marked not rewrite, then ignore it.
    $regex = '/\\.(' . preg_replace('/ +/', '|', preg_quote(MINISITE_EXTENSIONS_NOREWRITE)) . ')$/i';
    if (preg_match($regex, $href)) {
      continue;
    }

    // Default URL rewrite behaviours.
    $regex = "/" . preg_quote($minisite_entity_alias, '/') . "\\/(.*)\\/" . basename(current_path()) . "/";
    $relative = '';
    preg_match($regex, current_path(), $matches);
    if (!empty($matches[1])) {
      $relative = $matches[1] . '/';
    }
    $item
      ->setAttribute('href', $minisite_full_alias . '/' . $relative . $href);
  }

  // Replace link.
  foreach ($document
    ->getElementsByTagName('link') as $item) {
    $href = $item
      ->getAttribute('href');

    // Relative path.
    if (substr($href, 0, 2) == '..') {
      $item
        ->setAttribute('href', $minisite_site_url . substr($href, 3));
      continue;
    }
  }

  // Replace script.
  foreach ($document
    ->getElementsByTagName('script') as $item) {
    $src = $item
      ->getAttribute('src');

    // Relative path.
    if (substr($href, 0, 2) == '..') {
      $item
        ->setAttribute('src', $minisite_site_url . substr($src, 3));
      continue;
    }
  }

  // Replace image tag.
  foreach ($document
    ->getElementsByTagName('img') as $item) {
    $src = $item
      ->getAttribute('src');

    // Relative path.
    if (substr($href, 0, 2) == '..') {
      $item
        ->setAttribute('src', $minisite_site_url . substr($src, 3));
      continue;
    }
  }

  // Allow modules to modify minisite html
  drupal_alter('minisite_html', $document);
  $html = $document
    ->saveHTML();
  return $html;
}