*/}}

setup.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <html>
  2. <head>
  3. <title>
  4. Setup Selfauth
  5. </title>
  6. <style>
  7. h1{text-align:center;margin-top:5%;}
  8. h2{text-align:center;}
  9. .instructions{text-align:center;}
  10. .message{margin-top:20px;text-align:center;font-size:1.2em;font-weight:bold;}
  11. pre {width:400px; margin-left:auto; margin-right:auto;margin-bottom:50px;}
  12. form{
  13. margin-left:auto;
  14. width:300px;
  15. margin-right:auto;
  16. text-align:center;
  17. margin-top:20px;
  18. border:solid 1px black;
  19. padding:20px;
  20. }
  21. .form-line{ margin-top:5px;}
  22. .submit{width:100%}
  23. </style>
  24. </head>
  25. <body>
  26. <h1>Setup Selfauth</h1>
  27. <div>
  28. <?php
  29. define('RANDOM_BYTE_COUNT', 32);
  30. $app_url = 'http' . (isset($_SERVER['HTTPS']) ? 's' : '') . '://' . $_SERVER['HTTP_HOST']
  31. . str_replace('setup.php', '', $_SERVER['REQUEST_URI']);
  32. if (function_exists('random_bytes')) {
  33. $bytes = random_bytes(RANDOM_BYTE_COUNT);
  34. $strong_crypto = true;
  35. } elseif (function_exists('openssl_random_pseudo_bytes')) {
  36. $bytes = openssl_random_pseudo_bytes(RANDOM_BYTE_COUNT, $strong_crypto);
  37. } else {
  38. $bytes = '';
  39. for ($i=0; $i < RANDOM_BYTE_COUNT; $i++) {
  40. $bytes .= chr(mt_rand(0, 255));
  41. }
  42. $strong_crypto = false;
  43. }
  44. $app_key = bin2hex($bytes);
  45. $configfile= __DIR__ . '/config.php';
  46. $configured = true;
  47. if (file_exists($configfile)) {
  48. include_once $configfile;
  49. if ((!defined('APP_URL') || APP_URL == '')
  50. || (!defined('APP_KEY') || APP_KEY == '')
  51. || (!defined('USER_HASH') || USER_HASH == '')
  52. || (!defined('USER_URL') || USER_URL == '')
  53. ) {
  54. $configured = false;
  55. }
  56. } else {
  57. $configured = false;
  58. }
  59. if ($configured) : ?>
  60. <h2>System already configured</h2>
  61. <div class="instructions">
  62. If you with to reconfigure, please remove config.php and reload this page.
  63. </div>
  64. <?php else : ?>
  65. <?php if ($strong_crypto === false) : ?>
  66. <h2>
  67. WARNING: this version of PHP does not support functions 'random_bytes' or 'openssl_random_pseudo_bytes'.
  68. This means your application is not as secure as it could be. You may continues, but it is strongly recommended you upgrade PHP.
  69. </h2>
  70. <?php endif; ?>
  71. <div class="instructions">In order to configure Selfauth, you need to fill in a few values, this page helps generate those options.</div>
  72. <?php if (isset($_POST['username'])) : ?>
  73. <div>
  74. <?php
  75. $app_url = 'http' . (isset($_SERVER['HTTPS']) ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] . str_replace('setup.php', '', $_SERVER['REQUEST_URI']);
  76. $user = $_POST['username'];
  77. $user_tmp = trim(preg_replace('/^https?:\/\//', '', $_POST['username']), '/');
  78. $pass = md5($user_tmp . $_POST['password'] . $app_key);
  79. $config_file_contents = "<?php
  80. define('APP_URL', '$app_url');
  81. define('APP_KEY', '$app_key');
  82. define('USER_HASH', '$pass');
  83. define('USER_URL', '$user');";
  84. $file_written = false;
  85. if (is_writeable($configfile) && !$configured) {
  86. $handle = fopen($configfile, 'w');
  87. if ($handle) {
  88. $result = fwrite($handle, $config_file_contents);
  89. if ($result !== false) {
  90. $file_written = true;
  91. }
  92. }
  93. fclose($handle);
  94. }else{
  95. }
  96. if ($file_written) {
  97. echo '<div class="message">config.php was successfully written to disk</div>';
  98. } else {
  99. echo '<div class="message">Fill in the file config.php with the following content</div>';
  100. echo '<pre>';
  101. echo htmlentities($config_file_contents);
  102. echo '</pre>';
  103. }
  104. ?>
  105. </div>
  106. <?php endif ?>
  107. <form method="POST" action="">
  108. <div class="form-line"><label>Login Url:</label> <input name='username' /></div>
  109. <div class="form-line"><label>Password:</label> <input type='password' name='password' /></div>
  110. <div class="form-line"><input class="submit" type="submit" name="submit" value="Generate Config"/></div>
  111. </form>
  112. <?php endif; ?>
  113. </body>
  114. </html>