index.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. if(isset($_POST['db'])||isset($_POST['user'])||isset($_POST['password'])||isset($_POST['server'])||isset($_POST['email'])){
  3. if(!empty($_POST['db'])&&!empty($_POST['user'])&&!empty($_POST['password'])&&!empty($_POST['server'])){
  4. require_once('../lib/sql.class.php');
  5. function run_scripts($folder){
  6. static $pdo;
  7. if(!$pdo){
  8. $pdo = new PDO("mysql:dbname={$_POST['db']};host={$_POST['server']}",$_POST['user'],$_POST['password']) or trigger_error('Failed to connect to the database');
  9. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  10. }
  11. $steps = array(
  12. basename($folder)=>array()
  13. );
  14. foreach(scandir($folder) as $file){
  15. if(!in_array($file,array('.','..'))){
  16. $path = "{$folder}/{$file}";
  17. if(is_file($path) && substr($file,-3) == 'sql'){
  18. $source = file_get_contents($path);
  19. try{
  20. $stmt = $pdo->prepare($source);
  21. $stmt->execute();
  22. do{null;}while($stmt->nextRowset());
  23. $info = $stmt?$stmt->errorInfo()[2]:$pdo->errorInfo()[2];
  24. $pass = $stmt !== false;
  25. }catch(PDOException $e){
  26. $info = $e->getMessage();
  27. $pass = false;
  28. }
  29. $steps[basename($folder)][basename($file,'.sql')] = array($pass,$info);
  30. }elseif(is_dir($path)){
  31. $steps = array_merge($steps,run_scripts($path));
  32. }
  33. }
  34. }
  35. return $steps;
  36. }
  37. header('Content-type: application/json');
  38. if(empty($_POST['uninstall'])){
  39. $res = run_scripts('db_install');
  40. if(!empty($_POST['email'])){
  41. require_once('../lib/bugs.class.php');
  42. Bugs::connect($_POST['server'],$_POST['user'],$_POST['password'],$_POST['db']);
  43. if(Bugs::$sql->query("SELECT COUNT(*) count FROM users")->assoc_result['count'] === 0){
  44. $user = Bugs::user($_POST['user'],$_POST['email'],$_POST['password']);
  45. $pass = true;
  46. $info = '';
  47. if($user){
  48. $user->active = true;
  49. Bugs::$sql->query("
  50. INSERT INTO r_permission_user (per_id,u_id)
  51. VALUES (1,?)
  52. ",'i',$user->id)->execute();
  53. if(!Bugs::login($user,$_POST['password'])){
  54. $pass = false;
  55. $info = 'Failed to automatically log in.';
  56. }
  57. }else{
  58. $pass = false;
  59. $info = 'Could not create user.';
  60. }
  61. $res['99_User'] = array(
  62. 'Created default user and logged you in'=>array(
  63. $pass,
  64. $info
  65. )
  66. );
  67. }
  68. }
  69. }else{
  70. $res = run_scripts('db_uninstall');
  71. }
  72. die(json_encode($res));
  73. }else{
  74. header('Content-type: application/json');
  75. die(json_encode(false));
  76. }
  77. }
  78. ?>
  79. <!doctype html>
  80. <head>
  81. <meta charset="utf8"/>
  82. <title>Install</title>
  83. <script src="../js/juju/core.js"></script>
  84. <script src="../js/juju/dom.js"></script>
  85. <script src="../js/juju/canvas.js"></script>
  86. <script src="../js/juju/fetch.js"></script>
  87. <script src="install.js"></script>
  88. <link rel="stylesheet" href="../css/main.css"></link>
  89. </head>
  90. <body>
  91. <ul class="hidden" id="log"></ul>
  92. <form class="hidden" method="POST">
  93. <div>
  94. <label for="server">
  95. Server:
  96. </label>
  97. <input name="server"/>
  98. </div>
  99. <div>
  100. <label for="db">
  101. DB:
  102. </label>
  103. <input name="db"/>
  104. </div>
  105. <div>
  106. <label for="user">
  107. User:
  108. </label>
  109. <input name="user"/>
  110. </div>
  111. <div>
  112. <label for="email">
  113. Email:
  114. </label>
  115. <input type="email" name="email"/>
  116. </div>
  117. <div>
  118. <label for="password">
  119. Password:
  120. </label>
  121. <input type="password" name="password"/>
  122. </div>
  123. <div>
  124. <input type="submit" value="Install"/>
  125. <button id="uninstall" value="Uninstall">Uninstall</button>
  126. <input type="hidden" name="uninstall" value=""/>
  127. </div>
  128. </form>
  129. </body>
  130. </html>