The base template for building the Netsyms Apps.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /*
  3. * This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. */
  7. require_once __DIR__ . "/required.php";
  8. // if we're logged in, we don't need to be here.
  9. if (!empty($_SESSION['loggedin']) && $_SESSION['loggedin'] === true && !isset($_GET['permissionerror'])) {
  10. header('Location: app.php');
  11. die();
  12. }
  13. /**
  14. * Show a simple HTML page with a line of text and a button. Matches the UI of
  15. * the AccountHub login flow.
  16. *
  17. * @global type $SETTINGS
  18. * @global type $SECURE_NONCE
  19. * @global type $Strings
  20. * @param string $title Text to show, passed through i18n
  21. * @param string $button Button text, passed through i18n
  22. * @param string $url URL for the button
  23. */
  24. function showHTML(string $title, string $button, string $url) {
  25. global $SETTINGS, $SECURE_NONCE, $Strings;
  26. ?>
  27. <!DOCTYPE html>
  28. <meta charset="UTF-8">
  29. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  30. <meta name="viewport" content="width=device-width, initial-scale=1">
  31. <title><?php echo $SETTINGS['site_title']; ?></title>
  32. <link rel="icon" href="static/img/logo.svg">
  33. <link href="static/css/bootstrap.min.css" rel="stylesheet">
  34. <style nonce="<?php echo $SECURE_NONCE; ?>">
  35. .display-5 {
  36. font-size: 2.5rem;
  37. font-weight: 300;
  38. line-height: 1.2;
  39. }
  40. .banner-image {
  41. max-height: 100px;
  42. margin: 2em auto;
  43. border: 1px solid grey;
  44. border-radius: 15%;
  45. }
  46. </style>
  47. <div class="container mt-4">
  48. <div class="row justify-content-center">
  49. <div class="col-12 text-center">
  50. <img class="banner-image" src="./static/img/logo.svg" />
  51. </div>
  52. <div class="col-12 text-center">
  53. <h1 class="display-5 mb-4"><?php $Strings->get($title); ?></h1>
  54. </div>
  55. <div class="col-12 col-sm-8 col-lg-6">
  56. <div class="card mt-4">
  57. <div class="card-body">
  58. <a href="<?php echo $url; ?>" class="btn btn-primary btn-block"><?php $Strings->get($button); ?></a>
  59. </div>
  60. </div>
  61. </div>
  62. </div>
  63. </div>
  64. <?php
  65. }
  66. if (!empty($_GET['logout'])) {
  67. showHTML("You have been logged out.", "Log in again", "./index.php");
  68. die();
  69. }
  70. if (empty($_SESSION["login_code"])) {
  71. $redirecttologin = true;
  72. } else {
  73. try {
  74. $uidinfo = AccountHubApi::get("checkloginkey", ["code" => $_SESSION["login_code"]]);
  75. if ($uidinfo["status"] == "ERROR") {
  76. throw new Exception();
  77. }
  78. if (is_numeric($uidinfo['uid'])) {
  79. $user = new User($uidinfo['uid'] * 1);
  80. foreach ($SETTINGS['permissions'] as $perm) {
  81. if (!$user->hasPermission($perm)) {
  82. showHTML("no access permission", "sign out", "./action.php?action=signout");
  83. die();
  84. }
  85. }
  86. Session::start($user);
  87. $_SESSION["login_code"] = null;
  88. header('Location: app.php');
  89. showHTML("Logged in", "Continue", "./app.php");
  90. die();
  91. } else {
  92. throw new Exception();
  93. }
  94. } catch (Exception $ex) {
  95. $redirecttologin = true;
  96. }
  97. }
  98. if ($redirecttologin) {
  99. try {
  100. $urlbase = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://" . $_SERVER['HTTP_HOST'] . (($_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443) ? ":" . $_SERVER['SERVER_PORT'] : "");
  101. $iconurl = $urlbase . str_replace("index.php", "", $_SERVER["REQUEST_URI"]) . "static/img/logo.svg";
  102. $codedata = AccountHubApi::get("getloginkey", ["appname" => $SETTINGS["site_title"], "appicon" => $iconurl]);
  103. if ($codedata['status'] != "OK") {
  104. throw new Exception($Strings->get("login server unavailable", false));
  105. }
  106. $redirecturl = $urlbase . $_SERVER['REQUEST_URI'];
  107. $_SESSION["login_code"] = $codedata["code"];
  108. $locationurl = $codedata["loginurl"] . "?code=" . htmlentities($codedata["code"]) . "&redirect=" . htmlentities($redirecturl);
  109. header("Location: $locationurl");
  110. showHTML("Continue", "Continue", $locationurl);
  111. die();
  112. } catch (Exception $ex) {
  113. sendError($ex->getMessage());
  114. }
  115. }