<!DOCTYPE html>
<html lang="de" data-theme="light">
<script>
    localStorage.removeItem('uniquePostIdentifier');
</script>
<?
require_once("../../mysyde/common/version_comment.inc.php");
require_once("intranet_functions.inc.php");


if (isset($_POST["fgztiknxbhhk"])) {
  $_SESSION['login_id'] = $_POST["fgztiknxbhhk"];
  header('location:/');
  exit();
}


$translation = \DynCom\mysyde\common\classes\Registry::get('translation');




$isAzure = true;

if ($isAzure) {
  
  $parts = parse_url("https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."");
  parse_str($parts['query'], $query);
  $code = $query['code'];
  if ($code!==null) {
    $token = getToken($code);
    $headers = [
        'Authorization: Bearer '.$token[0],
        'Content-Type: application/json'
    ];
    $result = "";
    $fcmUrl = 'https://graph.microsoft.com/v1.0/me';
    $cRequest = curl_init();
    curl_setopt($cRequest, CURLOPT_URL, $fcmUrl);
    curl_setopt($cRequest, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($cRequest, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($cRequest, CURLOPT_SSL_VERIFYPEER, false);
    $result = curl_exec($cRequest);
    $obj = json_decode($result);
    $email = $obj->userPrincipalName;
    curl_close($cRequest);
    

      //code...
    

    $fcmUrl_mail = 'https://graph.microsoft.com/v1.0/users/'.$email;
    $cRequest_mail = curl_init();
    curl_setopt($cRequest_mail, CURLOPT_URL, $fcmUrl_mail);
    curl_setopt($cRequest_mail, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($cRequest_mail, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($cRequest_mail, CURLOPT_SSL_VERIFYPEER, false);
    $result_mail = curl_exec($cRequest_mail);
    $obj_l = json_decode($result_mail);
    $email_l = $obj_l->userPrincipalName;
    $name = $obj_l->displayName;
    curl_close($cRequest_mail);

    
    if (!isset($obj_l->{'error'}) && $obj_l->{'error'} === null) {
      // $email_l = $obj->;
      // echo $email_l;
      $noPublish = "CALL insertTokenMicrosoftKey('$token[0]', '$email_l', '$token[1]');";
      @mysqli_query($GLOBALS['mysql_con'],$noPublish);
      // $email_l = $obj->userPrincipalName;
      @mysqli_query($GLOBALS['mysql_con'], "CALL insertUserFromAzure('".$name."', '".$email_l."');");
      $sql_opt = @mysqli_query($GLOBALS['mysql_con'], "SELECT id FROM main_contact WHERE email='".$email_l."'");
      // $sql_opt = "SELECT id FROM main_contact WHERE email='".$email_l."'";

      $_SESSION['login_id'] = @mysqli_fetch_assoc($sql_opt)['id'];
      header('location:/');
    }else {
      $_SESSION['login_id'] = 0;
      header("location: https://login.microsoftonline.com/common/oauth2/v2.0/logout?post_logout_redirect_uri=https://intranet.awo-hamburg.de/dev/de/logout/");
    }
    


    // $query = "SELECT * from main_contact WHERE email = '$email'";
    // $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    // if (@mysqli_num_rows($result) > 0) {
    //   while ($main_contact = @mysqli_fetch_array($result)) {
        
    //       if($main_contact['active'] == 0){
    //         $_SESSION["login_error"] = "Dieser Account wurde noch nicht freigeschalten.";
    //         header("location: https://login.microsoftonline.com/common/oauth2/v2.0/logout?post_logout_redirect_uri=https://awo-hamburg.breadcrumb-online.de/dev/de/logout/");
    //       }else{
    //         $noPublish = "CALL insertTokenMicrosoftKey('$token[0]', '$email', '$token[1]')";
    //         @mysqli_query($GLOBALS['mysql_con'],$noPublish);
    //         $_SESSION['login_id'] = $main_contact['id'];
    //         header('location:/');
    //       }
    //   } 
    // }else{
    //   $_SESSION["login_error"] = "Die eingegebene E-Mail ist nicht vorhanden.";
    //   header("location: https://login.microsoftonline.com/common/oauth2/v2.0/logout?post_logout_redirect_uri=https://awo-hamburg.breadcrumb-online.de/dev/de/logout/");
    // }
  }else {
      // $fcmUrl = 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=93d2326e-40da-4938-a4d4-1df743ebd29a&response_type=code&scope=offline_access%20User.Read%20openid%20profile%20email%20User.Read.All&state=12345';
      // header("location: ". $fcmUrl);
       
        session_unset();
        session_destroy();
                        
      ?>
      <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="robots" content="noindex, nofollow" />
    <title>Login - <?= $GLOBALS['language']["site_title_name"]?></title>
    <?
        $favivon = getFavicon($site['id']);
        get_gulp_sources('/layout/frontend/b2c/dist/','style','script');
        create_includes($GLOBALS["layout"]["id"], $GLOBALS["navigation"]["forward_page_id"], 'css');
        create_includes($GLOBALS["layout"]["id"], $GLOBALS["navigation"]["forward_page_id"], 'js');
        create_live_edit_includes();
    ?>
    <link rel="icon" type="image/x-icon" href="/layout/frontend/b2c/dist/favicons/<?php echo $favivon?>">
    <link rel="shortcut icon" href="/layout/frontend/b2c/dist/favicons/<?php echo $favivon?>">

</head>

<body class="login_area">
  <?php $translation = \DynCom\mysyde\common\classes\Registry::get('translation');  ?>
        <div class="login_container">
            <div class="login_content">
                <div id="logo-area">
                    <? get_content("logo"); ?>
                    <div class="quote-box">
                        <div id="quote"></div>
                        <p class="author"></p>
                        <button id="refresh">neues Zitat laden</button>
                    </div> 
                    <? get_content("login_bg"); ?>
                </div>
                <div id="login-area">
                    <div id="login_form">
                      <? get_content("icon_logo_login"); ?>
                        <span><?= $GLOBALS['language']["site_title_name"]?> </span>
                        <h1><?php echo $translation->get("intranet_login_title"); ?></h1>
                        <form class="contact2-form validate-form" name="form_admin_login" id="form_admin_login"
                            action="?action=intranet_login" method="post">
                            <!-- <div class="wrapper-login">
                                <span>Login</span>
                                <div class="mail wrap-input2 validate-input" data-validate="Name is required">
                                    <input class="input2" type="text" name="input_login" id="input_login">
                                </div>
                                <span><?php echo $translation->get('backend_password_label'); ?></span>
                                <div class="wrap-input2 validate-input" data-validate="Name is required">
                                    <input class="input2" name="input_password" type="password" id="input_password">
                                </div>
                            </div>
                            <div class="login_button">
                                <a class="forgot_password"href="?action=intranet_forgot_password">Kennwort vergessen?</a>
                                <input id="login-button" class="button" name="" value="Anmelden" type="submit" />
                            </div> -->
                            <div class="login_button">
                                <!-- <a class="forgot_password"href="?action=intranet_forgot_password">Kennwort vergessen?</a> -->
                                <!-- <input id="login-button_microsoft" class="button" name="" value="Anmelden Microsoft" type="submit" /> -->
                                <a href="https://login.microsoftonline.com/awo-hamburg.de/oauth2/v2.0/authorize?client_id=93d2326e-40da-4938-a4d4-1df743ebd29a&response_type=code&scope=offline_access%20User.ReadWrite%20User.ReadBasic.All%20Presence.ReadWrite%20Calendars.ReadWrite%20Mail.ReadWrite%20Mail.Read%20Mail.Read.Shared%20Mail.ReadBasic%20Mail.ReadBasic.Shared%20Mail.ReadWrite.Shared%20Mail.Send%20Mail.Send.Shared&state=1234">Weiter mit Microsoft-Konto</a>
                            </div>
                            
                        </form>
                       
                        <div class="error"><?
                              if (session_status() === PHP_SESSION_NONE) {
                                session_start();
                              }
                              if (isset($_SESSION["login_error"])) {
                                echo $_SESSION["login_error"];
                              } 
                              ?></div>
                    </div>
                    <div id="button-area">
                            <? get_content("extern_login_button"); ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
        var colorWheel = ['#000'];

var quoteAuthor = "";
var quoteText = "";

function getQuote() {

  $.getJSON("https://api.forismatic.com/api/1.0/?method=getQuote&key=457653&format=jsonp&lang=en&jsonp=?", function(data) {
    quoteText = data.quoteText;

    if (data.quoteAuthor) {
      quoteAuthor = data.quoteAuthor;
    } else {
      quoteAuthor = "Anonymous"
    }

    $('#quote').animate({
      opacity: 0
    }, 500, function() {
      $('#quote').html(quoteText);
      $(this).animate({
        opacity: 1
      }, 500);
    });

    $('.author').animate({
      opacity: 0
    }, 500, function() {
      $('.author').html(" - " + quoteAuthor);
      $(this).animate({
        opacity: 1
      }, 500);
    });

    var choice = Math.floor(Math.random() * colorWheel.length);

    $('body').animate({
      backgroundColor: colorWheel[choice],
      color: colorWheel[choice],
    }, 1000);

    $('button').animate({
        opacity: 1
    }, 1000);
    
  }); 
}

$(document).ready(function() {
  var quoteInfo = [];
  $('#refresh').click(function(event) {
    event.preventDefault();
    getQuote();
  });
});

getQuote();


  setTimeout(() => {
        document.addEventListener("message", function(event) {
            window.ReactNativeWebView.postMessage(event.data);
        }, false);
    }, 100);


    </script>
    <script
        src="/layout/frontend/b2c/dist/js/bc.min.js?time=<?= filemtime($_SERVER['DOCUMENT_ROOT'] . '/layout/frontend/b2c/dist/js/bc.min.js')?>">
    </script>
</body>
      <?php
      exit();
  }
}
else {


?>


<?php
}
?>

</html>

