
<!DOCTYPE html>
<html>
<head>
  <title>Telserv API v2</title>
  <link href='css/typography.css' media='screen' rel='stylesheet' type='text/css' />
  <link href='css/reset.css' media='screen' rel='stylesheet' type='text/css' />
  <link href='css/screen.css' media='screen' rel='stylesheet' type='text/css' />
  <link href='css/reset.css' media='print' rel='stylesheet' type='text/css' />
  <link href='css/screen.css' media='print' rel='stylesheet' type='text/css' />

  <link href='custom.css' media='screen' rel='stylesheet' type='text/css' />

  <script type="text/javascript" src="lib/shred.bundle.js"></script>
  <script src='lib/jquery-1.8.0.min.js' type='text/javascript'></script>
  <script src='lib/jquery.slideto.min.js' type='text/javascript'></script>
  <script src='lib/jquery.wiggle.min.js' type='text/javascript'></script>
  <script src='lib/jquery.ba-bbq.min.js' type='text/javascript'></script>
  <script src='lib/handlebars-2.0.0.js' type='text/javascript'></script>
  <script src='lib/underscore-min.js' type='text/javascript'></script>
  <script src='lib/backbone-min.js' type='text/javascript'></script>
  <script src='lib/swagger-client.js' type='text/javascript'></script>
  <script src='swagger-ui.js' type='text/javascript'></script>
  <script src='lib/highlight.7.3.pack.js' type='text/javascript'></script>
  <script src='lib/marked.js' type='text/javascript'></script>

  <!-- enabling this will enable oauth2 implicit scope support -->
  <script src='lib/swagger-oauth.js' type='text/javascript'></script>
  <script type="text/javascript">
    $(function () {
      // Function to validate URL to prevent XSS attacks via data: URIs
      function isValidSwaggerUrl(url) {
        if (!url) return false;

        // Block data: URIs, javascript: URIs, and other dangerous protocols
        var lowerUrl = url.toLowerCase();
        if (lowerUrl.startsWith('data:') ||
            lowerUrl.startsWith('javascript:') ||
            lowerUrl.startsWith('vbscript:') ||
            lowerUrl.startsWith('file:')) {
          return false;
        }

        // Only allow http:// and https:// protocols
        try {
          var urlObj = new URL(url, window.location.href);
          return urlObj.protocol === 'http:' || urlObj.protocol === 'https:';
        } catch (e) {
          return false;
        }
      }

      var url = window.location.search.match(/url=([^&]+)/);
      if (url && url.length > 1) {
        url = decodeURIComponent(url[1]);

        // Validate the URL to prevent XSS
        if (!isValidSwaggerUrl(url)) {
          console.error('Security: Invalid or potentially malicious URL detected and blocked');
          alert('Invalid Swagger URL provided. Only HTTP(S) URLs are allowed.');
          url = "https://apiv2.sipserv.nl/resources";
        }
      } else {
        var url = window.location.search.match(/url=([^&]+)/);
        if (url && url.length > 1) {
          url = decodeURIComponent(url[1]);

          // Validate the URL to prevent XSS
          if (!isValidSwaggerUrl(url)) {
            console.error('Security: Invalid or potentially malicious URL detected and blocked');
            alert('Invalid Swagger URL provided. Only HTTP(S) URLs are allowed.');
            url = "https://apiv2.sipserv.nl/resources";
          }
        } else {
          url = "https://apiv2.sipserv.nl/resources";
        }
        /*
          if (window.location.href.indexOf("partnerapidev.sipserv.nl") > -1)
              url = "https://partnerapidev.sipserv.nl/resources";
          else if (window.location.href.indexOf("partnerapi.sipserv.nl") > -1)
              url = "https://partnerapi.sipserv.nl/resources";
          else
              url = "http://localhost/partnerAPI/resources";*/
      }
      window.swaggerUi = new SwaggerUi({
        url: url,
        dom_id: "swagger-ui-container",
        supportedSubmitMethods: ['get', 'post', 'put', 'delete', 'patch'],
        onComplete: function (swaggerApi, swaggerUi) {
          $('pre code').each(function (i, e) {
            hljs.highlightBlock(e)
          });
        },
        onFailure: function (data) {
          log("Unable to Load SwaggerUI");
        },
        docExpansion: "none",
        sorter: "alpha"
      });

      function addApiKeyAuthorization() {
        var key = $('#input_apiKey')[0].value;
        log("key: " + key);
        if (key && key.trim() != "") {
          log("added key " + key);
          window.authorizations.add("api_key", new ApiKeyAuthorization("api_key", key, "query"));
        }
      }

      $('#input_apiKey').change(function () {
        addApiKeyAuthorization();
      });

      window.swaggerUi.load();
    });

    function setCookie(cname, cvalue, exdays) {
      var d = new Date();
      d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
      var expires = "expires=" + d.toUTCString();
      document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
    }

    function getCookie(cname) {
      var name = cname + "=";
      var decodedCookie = decodeURIComponent(document.cookie);
      var ca = decodedCookie.split(';');
      for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
          c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
          return c.substring(name.length, c.length);
        }
      }
      return "";
    }
  </script>
</head>

<body class="swagger-section" onload="$('#apiKey').val(getCookie('apiKey'))">
  <table style="width: 100%">
    <tr>
      <td style="vertical-align: top"><img src="logo.png" style="padding: 10px" /></td>
      <td style="vertical-align: top; text-align: right; font-family: Droid Sans, sans-serif; font-size: 0.9em; padding: 10px">
        Provide your API key:<br />
        <input id="apiKey" style="font-family: Droid Sans, sans-serif; width: 260px; margin-top: 3px; text-align: right" onchange="setCookie('apiKey', this.value, 365)" /><input type="button" style="font-family: Droid Sans, sans-serif" value="set" onclick="$('[name=apiKey]').val(getCookie('apiKey'))" />
      </td>
    </tr>
  </table>
  <div id="message-bar" class="swagger-ui-wrap"></div>
  <div id="swagger-ui-container" class="swagger-ui-wrap"></div>
<script type='text/javascript'>$("#input_apiKey").parent().replaceWith(
    "<div class='input'><input type='text' id='txtUsername' placeholder='Username' style='width:100px'/></div>" +
    "<div class='input'><input type='password' id='txtPassword' placeholder='Password' style='width:100px'/></div>");

$('#txtUsername').change(addBasicAuth);
$('#txtPassword').change(addBasicAuth);

function addBasicAuth() {
    var username = $('#txtUsername').val().replace(/^\s+|\s+$/gm, ''); //trim
    var password = $('#txtPassword').val().replace(/^\s+|\s+$/gm, '');
    if (username && password) {
        window.authorizations.add("basic", new PasswordAuthorization("basic", username, password));
    }
}
</script></body>
</html>
