PageRenderTime 24ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/app/install/index.php

https://github.com/ddrl46/bugspray
PHP | 467 lines | 437 code | 6 blank | 24 comment | 9 complexity | fdecfcb87ab42ae6dbe7777376d29a17 MD5 | raw file
  1. <?php
  2. /*
  3. * bugspray issue tracking software
  4. * Copyright (c) 2009 a2h - http://a2h.uni.cc/
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * Under section 7b of the GNU General Public License you are
  17. * required to preserve this notice. Additional attribution may be
  18. * found in the NOTICES.txt file provided with the Program.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. // any function from functions.php would do
  25. $done = function_exists('db_query');
  26. // the faster the installer loads, the better
  27. function install_callback($buffer)
  28. {
  29. global $done;
  30. $buffer = str_replace("\r","",$buffer);
  31. $buffer = str_replace("\n","",$buffer);
  32. $buffer = str_replace("\t","",$buffer);
  33. if (!$done)
  34. {
  35. return $buffer;
  36. }
  37. else
  38. {
  39. global $page;
  40. $page->addBodyPre($buffer);
  41. }
  42. }
  43. if (!isset($_POST['act']) || $done)
  44. {
  45. ob_start('install_callback');
  46. ?>
  47. <?php if (!$done): ?>
  48. <!DOCTYPE html>
  49. <html lang="en">
  50. <head>
  51. <meta charset="UTF-8">
  52. <title>bugspray installer</title>
  53. <script type="text/javascript" src="../js/jquery-1.3.2.min.js"></script>
  54. <script type="text/javascript" src="../js/html5.js"></script>
  55. <link rel="stylesheet" type="text/css" href="installer.css" />
  56. <style type="text/css">
  57. header,section,footer,aside,nav,article,figure
  58. {
  59. display:block;
  60. }
  61. table
  62. {
  63. border-spacing:0px;
  64. }
  65. td
  66. {
  67. border:0px;
  68. padding:0px;
  69. }
  70. </style>
  71. </head>
  72. <body>
  73. <?php else: ?>
  74. <script type="text/javascript">
  75. var origtitle = document.title;
  76. document.title = 'bugspray installer';
  77. </script>
  78. <?php endif; ?>
  79. <div id="installer_wrap1">
  80. <img src="<?php echo $done ? 'install/' : ''; ?>bg.jpg" id="installer_bg" alt="" />
  81. <table id="installer_wrap2">
  82. <tr>
  83. <td style="vertical-align:middle;text-align:center;">
  84. <div id="installer_wrap3" style="<?php echo !$done ? 'display:none;' : ''; ?>">
  85. <nav id="installer_nav">
  86. <div class="left disabled"><img src="<?php echo $done ? 'install/' : ''; ?>install_arrow_left.png" alt="" /></div>
  87. <div class="right enabled"><img src="<?php echo $done ? 'install/' : ''; ?>install_arrow_right.png" alt="" /></div>
  88. <div style="clear:both;"></div>
  89. </nav>
  90. <section id="installer_content">
  91. <?php if (!$done): ?>
  92. <div id="installer_content_1" class="installer_content_slide">
  93. <h2>Welcome to the bugspray installer</h2>
  94. <p>Hello and thank you for choosing to install bugspray!</p>
  95. <?php
  96. $prqmsg = '';
  97. if (!function_exists('json_encode'))
  98. {
  99. $prqerr = true;
  100. $prqmsg .= 'Your PHP version needs to be at least 5.2.0 to run bugspray.<br />';
  101. }
  102. if (!is_writable('../settings.php'))
  103. {
  104. $prqerr = true;
  105. $prqmsg .= 'The <code>settings.php</code> file does not exist or is not writable (CHMOD 777 on UNIX systems).<br />';
  106. }
  107. if ($prqerr)
  108. {
  109. echo '<div id="installer_prq_error">'.$prqmsg.'<br />You may continue if you wish, however errors will likely occur.</div>';
  110. }
  111. else
  112. {
  113. echo '<p>This install won\'t take long at all, in fact, the prerequisite checks just passed :)</p>';
  114. echo '<p>Let\'s keep moving! Click the arrow button over there on the right.</p>';
  115. }
  116. ?>
  117. </div>
  118. <div id="installer_content_2" class="installer_content_slide">
  119. <h2>Install configuration</h2>
  120. <p>Just fill out the form to get started. You can change more stuff after you install<br />
  121. bugspray. Make sure the details are correct, as the next step will install bugspray!</p>
  122. <form id="installer_form">
  123. <table id="installer_tableform">
  124. <tr>
  125. <td>MySQL server name</td>
  126. <td><input type="text" name="mysql_server" value="localhost" /></td>
  127. </tr>
  128. <tr>
  129. <td>Database name</td>
  130. <td><input type="text" name="mysql_database" /></td>
  131. </tr>
  132. <tr>
  133. <td>Database username</td>
  134. <td><input type="text" name="mysql_username" /></td>
  135. </tr>
  136. <tr>
  137. <td>Database password</td>
  138. <td><input type="text" name="mysql_password" /></td>
  139. </tr>
  140. <tr>
  141. <td colspan="2"><hr /></td>
  142. </tr>
  143. <tr>
  144. <td>Bugspray username</td>
  145. <td><input type="text" name="bugspray_username" /></td>
  146. </tr>
  147. <tr>
  148. <td>Bugspray e-mail</td>
  149. <td><input type="text" name="bugspray_email" /></td>
  150. </tr>
  151. <tr>
  152. <td>Bugspray password</td>
  153. <td><input type="password" name="bugspray_password" /></td>
  154. </tr>
  155. </table>
  156. </form>
  157. </div>
  158. <div id="installer_content_3" class="installer_content_slide">
  159. <h2>Installing bugspray...</h2>
  160. <p>This won't take long, sit tight...</p>
  161. <p id="installer_steps">
  162. <div id="mysqltest">MySQL connection test... <img src="loading.gif" alt="" /></div>
  163. <div id="settings" style="display:none;">Saving settings file... <img src="loading.gif" alt="" /></div>
  164. <div id="mysqlinstall" style="display:none;">Populating database... <img src="loading.gif" alt="" /></div>
  165. <div id="adduser" style="display:none;">Adding initial user... <img src="loading.gif" alt="" /></div>
  166. </p>
  167. <div id="installer_error" style="display:none;">
  168. <b>Install did not complete successfully, message given was:</b>
  169. <div id="installer_error_message"></div>
  170. </div>
  171. </div>
  172. <?php else: ?>
  173. <div id="installer_content_1" class="installer_content_slide">
  174. <h2>Installing bugspray...</h2>
  175. <p>This won't take long, sit tight...</p>
  176. <p>
  177. MySQL connection test... <img src="install/tick.png" alt="" /><br />
  178. Saving settings file... <img src="install/tick.png" alt="" /><br />
  179. Populating database... <img src="install/tick.png" alt="" /><br />
  180. Adding initial user... <img src="install/tick.png" alt="" />
  181. </p>
  182. </div>
  183. <div id="installer_content_2" class="installer_content_slide">
  184. <h2>Done!</h2>
  185. <p><img src="install/done.png" alt="" /></p>
  186. <p>Hooray! Bugspray's been successfully installed!</p>
  187. <p><b>For security reasons please delete the <code>install</code> folder!</b></p>
  188. </div>
  189. <?php endif; ?>
  190. </section>
  191. </div>
  192. <script type="text/javascript">
  193. <?php if (!$done): ?>
  194. $("#installer_wrap3").fadeIn(2000);
  195. <?php endif; ?>
  196. var page = 1;
  197. var offset = 0;
  198. var offsets = [];
  199. var slidewidth = 640;
  200. $(".installer_content_slide").each(function(){
  201. i = $(this).attr('id').replace('installer_content','');
  202. offsets[i] = offset;
  203. $(this).css({'left':offset+8+'px'});
  204. offset += slidewidth+8;
  205. });
  206. $("#installer_nav .left").click(function(){
  207. if ($(this).hasClass('enabled'))
  208. {
  209. page -= 1;
  210. refreshPages();
  211. }
  212. });
  213. $("#installer_nav .right").click(function(){
  214. if ($(this).hasClass('enabled'))
  215. {
  216. page += 1;
  217. <?php if ($done): ?>
  218. if (page != 3)
  219. {
  220. <?php endif; ?>
  221. refreshPages();
  222. <?php if ($done): ?>
  223. }
  224. else
  225. {
  226. document.title = origtitle;
  227. $("#installer_wrap1").fadeOut(2000);
  228. }
  229. <?php endif; ?>
  230. }
  231. });
  232. refreshPages();
  233. function refreshPages()
  234. {
  235. $("#installer_nav .left").removeClass('enabled').removeClass('disabled');
  236. $("#installer_nav .right").removeClass('enabled').removeClass('disabled');
  237. if (page == 1)
  238. {
  239. $("#installer_nav .left").addClass('disabled');
  240. $("#installer_nav .right").addClass('enabled');
  241. }
  242. if (page == 2)
  243. {
  244. $("#installer_nav .left").addClass('enabled');
  245. $("#installer_nav .right").addClass('enabled');
  246. }
  247. <?php if (!$done): ?>
  248. if (page == 3)
  249. {
  250. $("#installer_nav .left").addClass('disabled');
  251. $("#installer_nav .right").addClass('disabled');
  252. setTimeout(installrun,$.fx.speeds._default);
  253. }
  254. <?php endif; ?>
  255. $(".installer_content_slide").each(function(){
  256. i = $(this).attr('id').replace('installer_content','');
  257. $(this).animate({'left':offsets[i]-(page-1)*slidewidth+8},$.fx.speeds._default);
  258. });
  259. }
  260. <?php if (!$done): ?>
  261. function installcomponent(type,successfunc)
  262. {
  263. $("#"+type).show();
  264. $.ajax({
  265. type: 'post',
  266. url: 'index.php',
  267. data: 'act='+type+'&'+$("#installer_form").serialize(),
  268. dataType: 'json',
  269. success: function(data){
  270. window.location.hostname == '127.0.0.1' || window.location.hostname == 'localhost' ? delay = 250 : delay = 0;
  271. setTimeout(function(){
  272. if (!data.success)
  273. {
  274. $("#"+type+" img").attr({'src':'cross.png'});
  275. $("#installer_error_message").html(data.message);
  276. $("#installer_error").slideDown();
  277. $("#installer_nav .left").removeClass('disabled').addClass('enabled');
  278. }
  279. else
  280. {
  281. $("#"+type+" img").attr({'src':'tick.png'});
  282. successfunc();
  283. }
  284. }, delay);
  285. }
  286. });
  287. }
  288. function installrun()
  289. {
  290. installcomponent('mysqltest',function(){
  291. installcomponent('settings',function(){
  292. installcomponent('mysqlinstall',function(){
  293. installcomponent('adduser',function(){
  294. location.href = location.href.substring(0,location.href.length-8)+'?installerdone';
  295. });
  296. });
  297. });
  298. });
  299. }
  300. <?php endif; ?>
  301. </script>
  302. </td>
  303. </tr>
  304. </table>
  305. </div>
  306. <?php if (!$done): ?>
  307. </body>
  308. </html>
  309. <?php endif; ?>
  310. <?php
  311. ob_end_flush();
  312. }
  313. elseif ($_POST['act'] == 'mysqltest')
  314. {
  315. $arr = array();
  316. $con = mysql_connect($_POST['mysql_server'],$_POST['mysql_username'],$_POST['mysql_password']);
  317. if (!$con)
  318. {
  319. $arr['success'] = false;
  320. $arr['message'] = 'Could not connect to the MySQL server with provided details, reason: ' . mysql_error();
  321. }
  322. else
  323. {
  324. $dbcon = mysql_select_db($_POST['mysql_database']);
  325. if (!$dbcon)
  326. {
  327. $arr['success'] = false;
  328. $arr['message'] = 'Could not select provided database name in MySQL, reason: ' . mysql_error();
  329. }
  330. else
  331. {
  332. $arr['success'] = true;
  333. }
  334. }
  335. echo json_encode($arr);
  336. }
  337. elseif ($_POST['act'] == 'settings')
  338. {
  339. $arr = array();
  340. $mysql_server = $_POST['mysql_server'];
  341. $mysql_username = $_POST['mysql_username'];
  342. $mysql_password = $_POST['mysql_password'];
  343. $mysql_database = $_POST['mysql_database'];
  344. $mysql_prefix = $_POST['mysql_prefix'];
  345. $sfstr =
  346. "<?php
  347. \$mysql_server = '$mysql_server';
  348. \$mysql_username = '$mysql_username';
  349. \$mysql_password = '$mysql_password';
  350. \$mysql_database = '$mysql_database';
  351. \$mysql_prefix = '$mysql_prefix'; // not implemented
  352. \$recaptcha_use = false; // this will probably be moved to the database
  353. \$recaptcha_key_public = '';
  354. \$recaptcha_key_private = '';
  355. ?>";
  356. $sffile = fopen('../settings.php','w');
  357. if ($sffile)
  358. {
  359. if (fwrite($sffile,$sfstr))
  360. {
  361. $arr['success'] = true;
  362. }
  363. else
  364. {
  365. $arr['success'] = false;
  366. $arr['message'] = 'The <code>settings.php</code> file could not be written to.';
  367. }
  368. }
  369. else
  370. {
  371. $arr['success'] = false;
  372. $arr['message'] = 'The <code>settings.php</code> file could not be opened.';
  373. }
  374. fclose($sffile);
  375. echo json_encode($arr);
  376. }
  377. elseif ($_POST['act'] == 'mysqlinstall')
  378. {
  379. $arr = array();
  380. $dumpfile = fopen('dump.sql','r');
  381. $dumpstr = fread($dumpfile,filesize('dump.sql'));
  382. $dumparr = explode(";\r\n",$dumpstr);
  383. if (count($dumparr) < 3)
  384. {
  385. $dumparr = explode(";\n",$dumpstr);
  386. }
  387. fclose($dumpfile);
  388. $arr['success'] = true;
  389. mysql_connect($_POST['mysql_server'],$_POST['mysql_username'],$_POST['mysql_password']);
  390. mysql_select_db($_POST['mysql_database']);
  391. foreach ($dumparr as $dumpquery)
  392. {
  393. if ($arr['success'] && str_replace(' ','',$dumpquery) != $dumpquery)
  394. {
  395. if (!mysql_query($dumpquery))
  396. {
  397. $arr['success'] = false;
  398. $arr['message'] = 'Could not populate the database, reason: '.mysql_error();
  399. }
  400. }
  401. }
  402. echo json_encode($arr);
  403. }
  404. elseif ($_POST['act'] == 'adduser')
  405. {
  406. $arr = array();
  407. $u = $_POST['bugspray_username'];
  408. $s = md5(rand(0,9001));
  409. $p = hash('whirlpool',$s.$_POST['bugspray_password']);
  410. $e = $_POST['bugspray_email'];
  411. mysql_connect($_POST['mysql_server'],$_POST['mysql_username'],$_POST['mysql_password']);
  412. mysql_select_db($_POST['mysql_database']);
  413. if (mysql_query("INSERT INTO users (username,password,password_salt,when_registered,email,avatar_type,avatar_location,`group`) ".
  414. "VALUES ('$u','$p','$s',NOW(),'$e',1,'img/defaultava.png',2)"))
  415. {
  416. $arr['success'] = true;
  417. }
  418. else
  419. {
  420. $arr['success'] = false;
  421. $arr['message'] = 'Could not add the initial user account, reason: '.mysql_error().'<br /><br /><b>The database has been cleared</b>';
  422. $num_tables = mysql_list_tables($_POST['mysql_database']);
  423. while($row = mysql_fetch_row($num_tables))
  424. {
  425. $delete_table = mysql_query("DROP TABLE IF EXISTS {$row[0]}");
  426. }
  427. }
  428. echo json_encode($arr);
  429. }
  430. ?>