PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/instal/functions.php

https://bitbucket.org/nexea/x00n
PHP | 157 lines | 122 code | 30 blank | 5 comment | 22 complexity | 012101d5356b8990c38492e0d63d79d1 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. error_reporting(E_ALL ^ E_NOTICE);
  3. function update_config()
  4. {
  5. $config_data = '<?php'."\n\n";
  6. $config_data .= '$mysql_host = \'' . $_POST['server'] . '\';' . "\n";
  7. $config_data .= '$mysql_db = \'' . $_POST['dbname'] . '\';' . "\n";
  8. $config_data .= '$mysql_user = \'' . $_POST['dbuser'] . '\';' . "\n";
  9. $config_data .= '$mysql_pass = \'' . $_POST['dbpass'] . '\';' . "\n\n";
  10. $config_data .= 'define(\'x00n_INSTALLED\', true);'."\n\n";
  11. $config_data .= '?' . '>'; // Done this to prevent highlighting editors getting confused!
  12. if(!($fp = fopen('../config.inc.php', 'w')))
  13. {
  14. die('Make config.inc.php writable -> 666');
  15. }
  16. else
  17. {
  18. $result = @fputs($fp, $config_data, strlen($config_data));
  19. @fclose($fp);
  20. }
  21. if( !mysql_connect($_POST['server'],$_POST['dbuser'],$_POST['dbpass']) )
  22. {
  23. die('Cant connect to databaseserver');
  24. }
  25. if( !mysql_select_db($_POST['dbname']) )
  26. {
  27. die('Cant select database');
  28. }
  29. }
  30. function basic_query()
  31. {
  32. $sql_lines = implode(' ', file(dirname(__FILE__) . '/install.sql'));
  33. $sql_lines = explode("\n", $sql_lines);
  34. include('../config.inc.php');
  35. if( !mysql_connect($mysql_host,$mysql_user,$mysql_pass) )
  36. {
  37. die('Cant connect to databaseserver');
  38. }
  39. if( !mysql_select_db($mysql_db) )
  40. {
  41. die('Cant select database');
  42. }
  43. // Execute the SQL.
  44. $current_statement = '';
  45. $failures = array();
  46. $exists = array();
  47. foreach ($sql_lines as $count => $line)
  48. {
  49. // No comments allowed!
  50. if (substr($line, 0, 1) != '#')
  51. $current_statement .= "\n" . rtrim($line);
  52. // Is this the end of the query string?
  53. if (empty($current_statement) || (preg_match('~;[\s]*$~s', $line) == 0 && $count != count($sql_lines)))
  54. continue;
  55. // Does this table already exist? If so, don't insert more data into it!
  56. if (preg_match('~^\s*INSERT INTO ([^\s\n\r]+?)~', $current_statement, $match) != 0 && in_array($match[1], $exists))
  57. {
  58. $current_statement = '';
  59. continue;
  60. }
  61. if (!mysql_query($current_statement))
  62. {
  63. $error_message = sqlerr($db_connection);
  64. // Error 1050: Table already exists!
  65. if (strpos($error_message, 'already exists') === false)
  66. $failures[$count] = $error_message;
  67. elseif (preg_match('~^\s*CREATE TABLE ([^\s\n\r]+?)~', $current_statement, $match) != 0)
  68. $exists[] = $match[1];
  69. }
  70. $current_statement = '';
  71. }
  72. }
  73. function insert_administrator()
  74. {
  75. if( $_POST['adminpass'] != $_POST['adminpassconfirm'] )
  76. {
  77. die('error: The Admin passwords do not match!');
  78. }
  79. $username = $_POST['administratorName'];
  80. $usermail = $_POST['adminemail'];
  81. $secret = mksecret();
  82. $wantpasshash = md5($secret . $_POST['adminpass'] . $secret);
  83. $editsecret = mksecret();
  84. $ret = mysql_query("INSERT INTO users (username, class, passhash, secret, editsecret, email, status, added) VALUES (" .
  85. implode(",", array_map("sqlesc", array($username, 6, $wantpasshash, $secret, $editsecret, $usermail, 'confirmed'))) .
  86. ",'" . get_date_time() . "')");
  87. }
  88. function config()
  89. {
  90. $online = gmdate("Y-m-d");
  91. $added = sqlesc(get_date_time());
  92. mysql_query("INSERT INTO config (name,value) VALUES ('siteonline','true')");
  93. mysql_query("INSERT INTO config (name,value) VALUES ('onlinesince','$online')");
  94. mysql_query("INSERT INTO config (name,value) VALUES ('sitename','".$_POST['sitename']."')");
  95. mysql_query("INSERT INTO config (name,value) VALUES ('domain','".$_POST['domain']."')");
  96. mysql_query("INSERT INTO config (name,value) VALUES ('announce_url','".$_POST['announce']."')");
  97. mysql_query("INSERT INTO config (name,value) VALUES ('sitemail','".$_POST['sitemail']."')");
  98. mysql_query("INSERT INTO config (name,value) VALUES ('funds',0 )");
  99. mysql_query("INSERT INTO config (name,value) VALUES ('peerlimit',1000 )");
  100. mysql_query("INSERT INTO news (userid,body,added) VALUES (1,'Welcome to your new TrakR installation. <br> If you ever have a problem please feel free to ask us at <a href=\"http://x00n.com\">x00n Dev Team</a>',$added)");
  101. }
  102. function finale()
  103. {
  104. echo'Install Finished <BR>
  105. Please remove the install directory or use chmod to make it non-accessible<BR>
  106. You may login <BR>
  107. <a href="../index.php">HERE</a>
  108. ';
  109. }
  110. function mksecret($len = 20) {
  111. $ret = "";
  112. for ($i = 0; $i < $len; $i++)
  113. $ret .= chr(mt_rand(0, 255));
  114. return $ret;
  115. }
  116. function get_date_time($timestamp = 0)
  117. {
  118. if ($timestamp)
  119. return date("Y-m-d H:i:s", $timestamp);
  120. else
  121. return gmdate("Y-m-d H:i:s");
  122. }
  123. function sqlesc($x) {
  124. return "'".mysql_real_escape_string($x)."'";
  125. }
  126. ?>