PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/sticky-notes/install.php

#
PHP | 136 lines | 92 code | 21 blank | 23 comment | 5 complexity | ca8821a9d40207c50a760bc8ecf9a151 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Sticky Notes pastebin
  4. * @ver 0.3
  5. * @license BSD License - www.opensource.org/licenses/bsd-license.php
  6. *
  7. * Copyright (c) 2012 Sayak Banerjee <sayakb@kde.org>
  8. * All rights reserved. Do not remove this copyright notice.
  9. */
  10. // Define constants
  11. define('IN_INSTALL', true);
  12. // Include necessary files
  13. include_once('init.php');
  14. /* COMMENT OUT WHEN INSTALLING */
  15. /* UNCOMMENT ONCE INSTALLING IS COMPLETED */
  16. $gsod->trigger('Install file locked. Check out the README file for installation instructions.');
  17. // Check is config file is present
  18. if (!file_exists(realpath('config.php')))
  19. {
  20. $gsod->trigger('Config file not found. Please rename config.sample.php to ' .
  21. 'config.php and make it writable.');
  22. }
  23. // Check if the config file is writable
  24. if (!is_writable(realpath('config.php')))
  25. {
  26. $gsod->trigger('Config file is not writable. Please adjust the permissions ' .
  27. 'to start installation.');
  28. }
  29. // Check if DB data is set
  30. $db_fields = array($config->db_host, $config->db_name, $config->db_username,
  31. $config->db_password, $config->db_prefix);
  32. foreach ($db_fields as $field)
  33. {
  34. if (empty($field))
  35. {
  36. $gsod->trigger('One or more database options have not been set in the config file.');
  37. }
  38. }
  39. // Check if the tables already exist
  40. $sql = "SHOW TABLES LIKE '{$db->prefix}%'";
  41. $rows = $db->query($sql);
  42. if (!empty($rows) && count($rows) > 0)
  43. {
  44. $gsod->trigger('One or more tables already exist in the specified database. '.
  45. 'Please drop them to start installation.');
  46. }
  47. // Create the config file
  48. $config->create($config->db_host, $config->db_port, $config->db_name,
  49. $config->db_username, $config->db_password, $config->db_prefix);
  50. // Create the table structure
  51. $db->query("CREATE TABLE IF NOT EXISTS {$db->prefix}main (" .
  52. "id INT(12) UNSIGNED NOT NULL AUTO_INCREMENT, " .
  53. "author VARCHAR(50) DEFAULT '', " .
  54. "project VARCHAR(50) DEFAULT '', " .
  55. "timestamp INT(11) UNSIGNED NOT NULL, " .
  56. "expire INT(11) UNSIGNED NOT NULL, " .
  57. "data MEDIUMTEXT NOT NULL, " .
  58. "language VARCHAR(50) NOT NULL DEFAULT 'php', " .
  59. "password VARCHAR(40) NOT NULL, " .
  60. "salt VARCHAR(5) NOT NULL, " .
  61. "private TINYINT(1) NOT NULL DEFAULT 0, " .
  62. "hash INT(12) UNSIGNED NOT NULL, " .
  63. "ip VARCHAR(50) NOT NULL, " .
  64. "PRIMARY KEY(id))");
  65. $db->query("CREATE TABLE IF NOT EXISTS {$db->prefix}session (" .
  66. "sid VARCHAR(40) NOT NULL, " .
  67. "timestamp INT(11) UNSIGNED NOT NULL, " .
  68. "PRIMARY KEY(sid))");
  69. $db->query("CREATE TABLE IF NOT EXISTS {$db->prefix}cron (" .
  70. "timestamp INT(11) UNSIGNED NOT NULL DEFAULT 0, " .
  71. "locked TINYINT(1) NOT NULL DEFAULT 0)");
  72. $db->query("CREATE TABLE IF NOT EXISTS {$db->prefix}users (" .
  73. "id INT(12) UNSIGNED NOT NULL AUTO_INCREMENT, " .
  74. "username VARCHAR(50) NOT NULL, " .
  75. "password VARCHAR(40) NOT NULL, " .
  76. "salt VARCHAR(5) NOT NULL, " .
  77. "email VARCHAR(100) NOT NULL, " .
  78. "dispname VARCHAR(100) DEFAULT '', " .
  79. "sid VARCHAR(40) DEFAULT '', " .
  80. "lastlogin INT(11) UNSIGNED DEFAULT 0, " .
  81. "PRIMARY KEY(id))");
  82. $db->query("CREATE TABLE IF NOT EXISTS {$db->prefix}ipbans (" .
  83. "ip VARCHAR(50) NOT NULL, " .
  84. "PRIMARY KEY(ip))");
  85. // Add index and charset data
  86. $db->query("ALTER TABLE {$db->prefix}main DEFAULT CHARACTER SET utf8");
  87. $db->query("ALTER TABLE {$db->prefix}main CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci");
  88. $db->query("ALTER TABLE {$db->prefix}main AUTO_INCREMENT = 1000");
  89. $db->query("CREATE INDEX {$db->prefix}idx_private ON {$db->prefix}main(id, private)");
  90. $db->query("CREATE INDEX {$db->prefix}idx_author ON {$db->prefix}main(id, author)");
  91. $db->query("CREATE INDEX {$db->prefix}idx_project ON {$db->prefix}main(id, project)");
  92. $db->query("CREATE INDEX {$db->prefix}idx_data ON {$db->prefix}main(id, data)");
  93. $db->query("CREATE INDEX {$db->prefix}idx_sid ON {$db->prefix}session(sid)");
  94. $db->query("CREATE INDEX {$db->prefix}idx_adminuser ON {$db->prefix}users(username)");
  95. $db->query("CREATE INDEX {$db->prefix}idx_adminsid ON {$db->prefix}users(sid)");
  96. // Fill in empty values to cron table
  97. $db->query("INSERT INTO {$db->prefix}cron VALUES (0, 0)");
  98. // Generate a salt and password
  99. $salt = substr(sha1(time()), rand(0, 34), 5);
  100. $password = substr(sha1(sha1(time())), rand(0, 31), 8);
  101. $hash = sha1($password . $salt);
  102. // Add the default admin user
  103. $sql = "INSERT INTO {$db->prefix}users " .
  104. "(username, password, salt, email) " .
  105. "VALUES ('admin', '{$hash}', '{$salt}', 'admin@sticky.notes')";
  106. $db->query($sql);
  107. // Done!
  108. $gsod->trigger(
  109. "Successfully installed! You can log in to the admin panel with the following credentials:" .
  110. "<ul><li>Username: <b>admin</b></li>" .
  111. "<li>Password: <b>{$password}</b></li></ul>" .
  112. "Please make a note of this password. You can change it from the User Management section of " .
  113. "the <a href=\"../admin/\">admin panel</a>."
  114. );
  115. ?>