<?php

include './vendor/autoload.php';

//Clear all cookies as these pages set loads of crap cookies.
if (isset($_SERVER['HTTP_COOKIE'])) {
    $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
    foreach($cookies as $cookie) {
        $parts = explode('=', $cookie);
        $name = trim($parts[0]);
        setcookie($name, '', time()-1000);
        setcookie($name, '', time()-1000, '/');
    }
}

$tiscreport_location = '/sites/tiscreport.org/live/www';

//run the settings.php for tiscreport settings
include $tiscreport_location . '/sites/default/settings.php';

$servername = $databases['default']['default']['host'];
$username = $databases['default']['default']['username'];
$password = $databases['default']['default']['password'];
$database = $databases['default']['default']['database'];


$path = explode('/', $_SERVER['REQUEST_URI']);


if (
  (!isset($path[1]) || $path[1] != 'statementcontent') // Only accept requests for statementcontent
  ||
  (!isset($path[2]) || !is_numeric($path[2])) // Don't handle request if there is no numeric statement node id
) {
  header("HTTP/1.0 404 Not Found");
  exit;
}


// Create connection
$conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->connect_error) {
  header("HTTP/1.0 500 Database error");
  die("Connection failed: " . $conn->connect_error);
}


//Lastly sanitise the node id:

$nid = (int) $path[2];

//Get the relevant filename for the statement
$sql = "SELECT fm.uri, fm.filemime, fsu.field_statement_url_url FROM field_data_field_statement_file fsf
INNER JOIN file_managed fm ON fm.fid = fsf.field_statement_file_fid
INNER JOIN field_data_field_statement_url fsu ON fsu.entity_id = fsf.entity_id
WHERE fsf.entity_id = $nid";


$result = $conn->query($sql);

if ($result && $result->num_rows > 0) {
  $row = $result->fetch_assoc();


  $path = str_replace('public://', './', $row['uri']);
  $local_file_contents = file_get_contents($path);

  if ($row['filemime'] == 'text/html') {
    //process the html;
    $local_file_contents = get_display_markup($local_file_contents, $row['field_statement_url_url']);
  }

  header('Content-Type: ' . $row['filemime']);
  print $local_file_contents;
  exit;


}


$conn->close();


function get_display_markup($content, $original_url) {
  libxml_use_internal_errors(TRUE);
  $doc = new DOMDocument;

  $doc->validateOnParse = FALSE;
  $doc->preserveWhiteSpace = FALSE;


  if ($doc->loadHTML($content)) {
    //libxml_clear_errors();

    //Set all relative paths to absolute urls if the source document url was provided
    if (!empty($original_url)) {
      $replace_urls = ['a', 'img', 'iframe', 'script', 'link'];

      foreach ($replace_urls as $tag) {

        $elements = $doc->getElementsByTagName($tag);

        /**
         * @var $element Dom\LeafNode
         */
        foreach ($elements as $element) {

          switch ($element->tagName) {
            case 'img':
            case 'iframe':
            case 'frame':
            case 'embed':
            case 'script':
              $attr = 'src';
              break;
            case 'object':
              $attr = 'data';
              break;
            case 'a':
              //Add a target attribute
              $element->setAttribute('target', '_top');
            default:
              $attr = 'href';
          }

          // Get the value of the attribute
          $url_attr = $element->getAttribute($attr);

          $absolute_url = getAbsoluteURL($url_attr, $original_url);

          $element->setAttribute($attr, $absolute_url);
          $element->setAttribute('style', 'max-width:100%;');

        }
      }
    }
    $result = $doc->savehtml();
    if (strlen($result > 10)) {
      return $doc->savehtml();
    }
  }

  //try regex

  $parsed = parse_url($original_url);

  //stash absolute links
  $content = str_replace('src="http', 'src_tisc_rep="http', $content);
  $content = str_replace('href="http', 'href_tisc_rep="http', $content);

  //return $content;
  //deal with hrefs:

  $regex_relative = '/<(a|link)([^>]*)href=["\']([^\/][^"\']*)["\']([^>]*)>/ui';
  $regex_relative_root = '/<(a|link)([^>]*)href=["\']([\/][^"\']*)["\']([^>]*)>/ui';
  // replacement for each subpattern (3 in total)
  // basically here we are adding missing baseurl to href

  $replace_root = '<$1$2href="' . $parsed['scheme'] . '://' . $parsed['host'] . '$3"$4>';
  $replace_relative = '<$1$2href="' . $parsed['scheme'] . '://' . $parsed['host'] . substr($parsed['path'], 0, strrpos($parsed['path'], '/')) . '/$3"$4>';

  $content = preg_replace($regex_relative, $replace_relative, $content);
  $content = preg_replace($regex_relative_root, $replace_root, $content);


  //deal with src,

  $regex_relative = '/<(img|iframe|frame|embed|script)([^>]*)src=["\']([^\/][^"\']*)["\']([^>]*)>/ui';
  $regex_relative_root = '/<(img|iframe|frame|embed|script)([^>]*)src=["\']([\/][^"\']*)["\']([^>]*)>/ui';

  $replace_root = '<$1$2src="' . $parsed['scheme'] . '://' . $parsed['host'] . '$3"$4>';
  $replace_relative = '<$1$2src="' . $parsed['scheme'] . '://' . $parsed['host'] . substr($parsed['path'], 0, strrpos($parsed['path'], '/')) . '/$3"$4>';

  $content = preg_replace($regex_relative, $replace_relative, $content);
  $content = preg_replace($regex_relative_root, $replace_root, $content);


  //restore absolute links
  $content = str_replace('src_tisc_rep="http', 'src="http', $content);
  $content = str_replace('href_tisc_rep="http', 'href="http', $content);


  return $content;

  print "Something went wrong trying to convert HTML  for $original_url" . print_r($doc, TRUE);
  return "<pre>" . htmlspecialchars($content) . "</pre>";
}

/**
 * Will turn a relative URL into and absolute one. Optionally specify
 * a source URL, otherwise it will assume this page as source
 *
 * @param string $source_url The page URL
 * @param string $relative_url The URL to be made absolute
 *
 * @return string
 */
function getAbsoluteURL($relative_url, $source_url = NULL) {
  //use the source URL for this page if none given
  if (is_null($source_url)) {
    $source_url = !is_null($this->originalUrl) ? $this->originalUrl : $this->sourceUrl;
  }

  $url_parts = parse_url($relative_url);
  if (isset($url_parts['scheme']) && in_array($url_parts['scheme'], [
      'mailto',
      'tel',
      'callto',
    ])) {
    return $relative_url;
  }
  return http_build_url($source_url, $url_parts, HTTP_URL_JOIN_PATH);
}
