install.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. // Initialize core
  3. $f3=require("lib/base.php");
  4. $f3->mset(array(
  5. "UI" => "app/view/",
  6. "LOGS" => "log/",
  7. "TEMP" => "tmp/",
  8. "PREFIX" => "dict.",
  9. "LOCALES" => "app/dict/",
  10. "FALLBACK" => "en",
  11. "CACHE" => false,
  12. "AUTOLOAD" => "app/",
  13. "PACKAGE" => "Phproject",
  14. "site.url" => $f3->get("SCHEME") . "://" . $f3->get("HOST") . $f3->get("BASE") . "/"
  15. ));
  16. // Check if already installed
  17. if(is_file("config.ini")) {
  18. $f3->set("success", "Phproject is already installed.");
  19. }
  20. // Check PCRE version
  21. if((float)PCRE_VERSION < 7.9) {
  22. $f3->set("error", "PCRE version is out of date");
  23. }
  24. // Check for MySQL PDO
  25. if(!in_array("mysql", PDO::getAvailableDrivers())) {
  26. $f3->set("error", "MySQL PDO driver is not avaialble.");
  27. }
  28. // Check for GD library
  29. if(!function_exists("imagecreatetruecolor")) {
  30. $f3->set("warning", "GD library is not available. Profile pictures and file thumbnails will not work until it is installed.");
  31. }
  32. // Run installation process if post data received
  33. if($_POST) {
  34. $post = $_POST;
  35. try {
  36. // Connect to database
  37. $db = new \DB\SQL(
  38. "mysql:host=" . $post["db-host"] . ";port=" . $post["db-port"] . ";dbname=" . $post["db-name"],
  39. $post["db-user"],
  40. $post["db-pass"]
  41. );
  42. // Run installation scripts
  43. $install_db = file_get_contents("db/database.sql");
  44. $db->exec(explode(";", $install_db));
  45. // Create admin user
  46. $f3->set("db.instance", $db);
  47. $security = \Helper\Security::instance();
  48. $user = new \Model\User;
  49. $user->role = "admin";
  50. $user->rank = \Model\User::RANK_SUPER;
  51. $user->name = "Admin";
  52. $user->username = $post["user-username"] ?: "admin";
  53. $user->email = $post["user-email"];
  54. $user->salt = $security->salt();
  55. $user->password = $security->hash($post["user-password"] ?: "admin", $user->salt);
  56. $user->api_key = $security->salt_sha1();
  57. $user->save();
  58. // Ensure required directories exist
  59. if(!is_dir("tmp/cache")) {
  60. mkdir("tmp/cache", 0777, true);
  61. }
  62. if(!is_dir("log")) {
  63. mkdir("log", 0777, true);
  64. }
  65. // Save configruation options
  66. if(!empty($post["language"])) {
  67. \Model\Config::setVal("LANGUAGE", $post["language"]);
  68. }
  69. if($post["parser"] == "both") {
  70. \Model\Config::setVal("parse.markdown", 1);
  71. \Model\Config::setVal("parse.textile", 1);
  72. } elseif($post["parser"] == "markdown") {
  73. \Model\Config::setVal("parse.markdown", 0);
  74. \Model\Config::setVal("parse.textile", 1);
  75. } elseif($post["parser"] == "textile") {
  76. \Model\Config::setVal("parse.markdown", 1);
  77. \Model\Config::setVal("parse.textile", 0);
  78. }
  79. \Model\Config::setVal("site.name", $post['site-name']);
  80. \Model\Config::setVal("site.timezone", $post['site-timezone']);
  81. \Model\Config::setVal("site.public_registration", $post['site-public_registration']);
  82. \Model\Config::setVal("mail.from", $post['mail-from']);
  83. // Write database connection file
  84. $data = "[globals]\n";
  85. $data .= "db.host=\"{$post['db-host']}\"\n";
  86. $data .= "db.user=\"{$post['db-user']}\"\n";
  87. $data .= "db.pass=\"{$post['db-pass']}\"\n";
  88. $data .= "db.name=\"{$post['db-name']}\"\n";
  89. file_put_contents("config.ini", $data);
  90. $f3->set("success", "Installation complete.");
  91. } catch(PDOException $e) {
  92. $f3->set("warning", $e->getMessage());
  93. }
  94. }
  95. // Render installer template
  96. echo Template::instance()->render("install.html");