您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. define('BASE', __DIR__);
  3. define('BASEURL', $_SERVER['SERVER_NAME']);
  4. $config = json_decode(file_get_contents(BASE . "/inc/config.json"));
  5. if (!empty($config)) {
  6. header('Location: /index.php');
  7. die();
  8. }
  9. require_once(BASE . '/inc/db.php');
  10. require_once(BASE . '/inc/user.php');
  11. $fieldNames = ['db-type', 'db-host', 'db-username', 'db-password', 'db-database', 'flyers-user', 'flyers-password'];
  12. function test_config($params) {
  13. global $fieldNames, $db, $user;
  14. $config = [
  15. "type" => $params['db-type']
  16. ];
  17. if (empty($params['flyers-user']) || empty($params['flyers-password']))
  18. return "All fields are required";
  19. switch ($params['db-type']) {
  20. case "mysql":
  21. if (!empty($params) && count($params) != count($fieldNames))
  22. return "All fields are required";
  23. $config += [
  24. 'host' => $params['db-host'],
  25. 'user' => $params['db-username'],
  26. 'pass' => $params['db-password'],
  27. 'db' => $params['db-database']
  28. ];
  29. $db = MysqlDb::Connect($config->host, $config->user, $config->pass, $config->db);
  30. break;
  31. case "sqlite":
  32. $db = SqliteDb::Connect();
  33. break;
  34. default:
  35. return "Invalid Database Type";
  36. }
  37. $success = $db->exec_multi("
  38. CREATE TABLE IF NOT EXISTS `members` (
  39. `skymanager_id` integer NOT NULL PRIMARY KEY,
  40. `name` varchar(128) NOT NULL,
  41. `username` varchar(64) NOT NULL,
  42. `voting_id` int DEFAULT NULL UNIQUE,
  43. `email` varchar(128) DEFAULT NULL,
  44. `pollworker` BOOLEAN NOT NULL DEFAULT false,
  45. `checkedin` BOOLEAN NOT NULL DEFAULT false);
  46. CREATE TABLE IF NOT EXISTS `proxy` (
  47. `voting_id` integer NOT NULL,
  48. `delegate_id` integer NOT NULL,
  49. PRIMARY KEY (`voting_id`, `delegate_id`));
  50. CREATE TABLE IF NOT EXISTS `positions` (
  51. `position` varchar(64) NOT NULL PRIMARY KEY,
  52. `description` varchar(128) NOT NULL UNIQUE,
  53. `active` BOOLEAN NOT NULL DEFAULT false
  54. );
  55. CREATE TABLE IF NOT EXISTS `votes` (
  56. `candidate_id` integer NOT NULL,
  57. `position` varchar(64) NOT NULL,
  58. `member_id` integer NOT NULL,
  59. -- `vote_type` enum('IN PERSON','ONLINE','PROXY IN PERSON','PROXY ONLINE','UNANIMOUS') NOT NULL DEFAULT 'ONLINE',
  60. `vote_type` varchar(24) NOT NULL DEFAULT 'ONLINE',
  61. `submitted_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  62. `submitter_id` integer NOT NULL,
  63. PRIMARY KEY (`position`,`member_id`),
  64. FOREIGN KEY (`position`) REFERENCES `positions` (`position`) ON DELETE CASCADE)
  65. ");
  66. if (!$success)
  67. return "Failed to set up database schema: " . $db->getError();
  68. $success = $user->login($params['flyers-user'], $params['flyers-password']);
  69. if (!$success)
  70. return "Login Failed";
  71. $db->query("UPDATE members SET `pollworker`=TRUE where skymanager_id=" . ((int) $user->getUserId()));
  72. if ($err = $db->getError())
  73. return "Failed to update user permissions: $err";
  74. $conf = "";
  75. $conf = json_encode($config, JSON_PRETTY_PRINT);
  76. if (file_put_contents(BASE . "/inc/config/config.json", $conf) === false)
  77. return "Failed to write configuration.";
  78. return false;
  79. }
  80. $params = [];
  81. foreach ($fieldNames as $field) {
  82. if (array_key_exists($field, $_POST) && !empty($_POST[$field]))
  83. $params[$field] = $_POST[$field];
  84. }
  85. $error = null;
  86. if (!empty($params))
  87. $error = test_config($params);
  88. if ($error === false) {
  89. header('Location: /index.php');
  90. die();
  91. }
  92. ?>
  93. <!doctype html>
  94. <html>
  95. <head>
  96. <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
  97. <link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css2?family=Fira+Sans:wght@400;600;800&display=swap" />
  98. <link rel="stylesheet" type="text/css" href="/styles/style.css" />
  99. <style type="text/css">
  100. form input#db-sqlite:checked~.form-row label[for="db-sqlite"] .radio-button-label,
  101. form input#db-mysql:checked~.form-row label[for="db-mysql"] .radio-button-label {
  102. background-color: #000;
  103. color: #fff;
  104. border: 2px solid #fff;
  105. box-shadow: 0px 0px 0px 2px #000;
  106. }
  107. form .form-row.conditional { display: none; }
  108. form input#db-mysql:checked~.form-row.mysql { display: block; }
  109. </style>
  110. </head>
  111. <body>
  112. <div id="container">
  113. <div class="header">
  114. <h1>Michigan Flyers</h1>
  115. <h2>Voting System Setup</h2>
  116. </div>
  117. <div class="content">
  118. <div class="page">
  119. <?php if(!empty($error)) echo "<span class=\"errormessage\">$error</span>"; ?>
  120. <form action="configure.php" method="POST">
  121. <div class="form-section">
  122. <input type="radio" id="db-sqlite" name="db-type" value="sqlite" checked />
  123. <input type="radio" id="db-mysql" name="db-type" value="mysql" />
  124. <h3>Database Setup</h3>
  125. <div class="form-row">
  126. <div class="selector">
  127. <label class="radio" for="db-sqlite">
  128. <span class="radio-button-label">SQLite</span>
  129. </label>
  130. <label class="radio" for="db-mysql">
  131. <span class="radio-button-label">MySQL</span>
  132. </label>
  133. </div>
  134. </div>
  135. <div class="form-row conditional mysql">
  136. <label for="db-host">Host</label>
  137. <input type="text" id="db-host" name="db-host" value="localhost" />
  138. </div>
  139. <div class="form-row conditional mysql">
  140. <label for="db-host">Host</label>
  141. <input type="text" id="db-host" name="db-host" value="localhost" />
  142. </div>
  143. <div class="form-row conditional mysql">
  144. <label for="db-database">Database Name</label>
  145. <input type="text" id="db-database" name="db-database" />
  146. </div>
  147. <div class="form-row conditional mysql">
  148. <label for="db-username">Username</label>
  149. <input type="text" id="db-username" name="db-username" />
  150. </div>
  151. <div class="form-row conditional mysql">
  152. <label for="db-password">Password</label>
  153. <input type="password" id="db-password" name="db-password" />
  154. </div>
  155. </div>
  156. <div class="form-section">
  157. <h3>Flyers Access Setup</h3>
  158. <div class="form-row">
  159. <label for="flyers-user">Voting Administrator</label>
  160. <input type="text" id="flyers-user" name="flyers-user" />
  161. </div>
  162. <div class="form-row">
  163. <label for="flyers-password">Password</label>
  164. <input type="password" name="flyers-password" />
  165. </div>
  166. <div class="form-row">
  167. <input type="submit" name="login" value="Setup!" />
  168. </div>
  169. </div>
  170. </form>
  171. </div>
  172. </div>
  173. </div>
  174. </body>
  175. </html>