index.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. // Initialize core
  3. $f3=require("lib/base.php");
  4. $f3->mset(array(
  5. "UI" => "app/view/;app/plugin/",
  6. "ESCAPE" => false,
  7. "LOGS" => "log/",
  8. "TEMP" => "tmp/",
  9. "PREFIX" => "dict.",
  10. "LOCALES" => "app/dict/",
  11. "FALLBACK" => "en",
  12. "CACHE" => true,
  13. "AUTOLOAD" => "app/;lib/vendor/",
  14. "PACKAGE" => "Phproject",
  15. "TZ" => "UTC",
  16. "microtime" => microtime(true),
  17. "site.url" => $f3->get("SCHEME") . "://" . $f3->get("HOST") . $f3->get("BASE") . "/"
  18. ));
  19. // Redirect to installer if no config file is found
  20. if(!is_file("config.ini")) {
  21. header("Location: install.php");
  22. return;
  23. }
  24. // Get current Git revision
  25. if(is_file(".git/refs/heads/master")) {
  26. $f3->set("revision", trim(file_get_contents(".git/refs/heads/master")));
  27. } else {
  28. $f3->set("revision", "");
  29. }
  30. // Load configuration
  31. $f3->config("config-base.ini");
  32. $f3->config("config.ini");
  33. // Load routes
  34. $f3->config("app/routes.ini");
  35. // Set up error handling
  36. $f3->set("ONERROR", function(Base $f3) {
  37. if($f3->get("AJAX")) {
  38. if(!headers_sent()) {
  39. header("Content-type: application/json");
  40. }
  41. echo json_encode(array(
  42. "error" => $f3->get("ERROR.title"),
  43. "text" => $f3->get("ERROR.text")
  44. ));
  45. } else {
  46. switch($f3->get("ERROR.code")) {
  47. case 404:
  48. $f3->set("title", "Not Found");
  49. $f3->set("ESCAPE", false);
  50. echo \Helper\View::instance()->render("error/404.html");
  51. break;
  52. case 403:
  53. echo "You do not have access to this page.";
  54. break;
  55. default:
  56. if(ob_get_level()) {
  57. include "app/view/error/inline.html";
  58. } else {
  59. include "app/view/error/500.html";
  60. }
  61. }
  62. }
  63. });
  64. // Connect to database
  65. $f3->set("db.instance", new DB\SQL(
  66. "mysql:host=" . $f3->get("db.host") . ";port=" . $f3->get("db.port") . ";dbname=" . $f3->get("db.name"),
  67. $f3->get("db.user"),
  68. $f3->get("db.pass")
  69. ));
  70. // Load final configuration
  71. \Model\Config::loadAll();
  72. // Ensure database is up to date
  73. $version = \Helper\Security::instance()->checkDatabaseVersion();
  74. if($version !== true) {
  75. \Helper\Security::instance()->updateDatabase($version);
  76. }
  77. // Minify static resources
  78. // Cache for 1 week
  79. $f3->route("GET /minify/@type/@files", function(Base $f3, $args) {
  80. $f3->set("UI", $args["type"] . "/");
  81. echo Web::instance()->minify($args["files"]);
  82. }, $f3->get("cache_expire.minify"));
  83. // Initialize plugins and any included locales
  84. $pluginDir = scandir("app/plugin");
  85. $plugins = array();
  86. $locales = "";
  87. foreach($pluginDir as $pluginName) {
  88. if($pluginName != "." && $pluginName != ".." && is_dir("app/plugin/$pluginName") && is_file("app/plugin/$pluginName/base.php") && is_dir("app/plugin/$pluginName/dict")) {
  89. $locales .= ";app/plugin/$pluginName/dict/";
  90. }
  91. }
  92. if($locales) {
  93. $f3->set("LOCALES", $f3->get("LOCALES") . $locales);
  94. }
  95. foreach($pluginDir as $pluginName) {
  96. if($pluginName != "." && $pluginName != ".." && is_dir("app/plugin/$pluginName") && is_file("app/plugin/$pluginName/base.php")) {
  97. $pluginName = "Plugin\\" . str_replace(" ", "_", ucwords(str_replace("_", " ", $pluginName))) . "\\Base";
  98. $plugin = $pluginName::instance();
  99. $slug = \Web::instance()->slug($plugin->_package());
  100. $plugins[$slug] = $plugin;
  101. if(!$plugin->_installed()) {
  102. try {
  103. $plugin->_install();
  104. } catch (Exception $e) {
  105. $f3->set("error", "Failed to install plugin " . $plugin->_package() . ": " . $e->getMessage());
  106. }
  107. }
  108. try {
  109. $plugin->_load();
  110. } catch (Exception $e) {
  111. $f3->set("error", "Failed to initialize plugin " . $plugin->_package() . ": " . $e->getMessage());
  112. }
  113. }
  114. }
  115. $f3->set("plugins", $plugins);
  116. // Set up user session
  117. $user = new Model\User();
  118. $user->loadCurrent();
  119. // Load issue types
  120. $types = new \Model\Issue\Type();
  121. $f3->set("issue_types", $types->find(null, null, $f3->get("cache_expire.db")));
  122. // Run the application
  123. $f3->set("menuitem", false);
  124. $f3->run();