init.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. umask(0077);
  3. set_error_handler(function ($level, $message, $file = '', $line = 0) {
  4. throw new ErrorException($message, 0, $level, $file, $line);
  5. });
  6. set_exception_handler(function ($e) {
  7. error_log((string) $e);
  8. http_response_code(500);
  9. echo '<h1>Error</h1><p>An error occured.<p>';
  10. });
  11. register_shutdown_function(function () { // Also catch fatal errors
  12. if (($error = error_get_last()) !== NULL)
  13. throw new ErrorException($error['message'], 0, $error['type'], $error['file'], $error['line']);
  14. });
  15. const ROOT_PATH = __DIR__;
  16. define('CONF', parse_ini_file(ROOT_PATH . '/config.ini', true, INI_SCANNER_TYPED));
  17. define('DB', new PDO('sqlite:' . ROOT_PATH . '/db/servnest.db'));
  18. DB->exec('PRAGMA foreign_keys = ON;');
  19. DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  20. date_default_timezone_set('UTC');
  21. foreach (explode(',', preg_replace('/[A-Z0-9]|q=|;|-|\./', '', $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? '')) as $client_locale)
  22. if (in_array($client_locale, array_diff(scandir(ROOT_PATH . '/locales'), ['..', '.']), true)) {
  23. $locale = $client_locale;
  24. break;
  25. }
  26. define('LOCALE', $locale ?? 'en');
  27. putenv('LANG=C.UTF-8');
  28. setlocale(LC_MESSAGES, 'C.UTF-8');
  29. bindtextdomain('messages', ROOT_PATH . '/locales/' . LOCALE);
  30. header('Content-Language: ' . LOCALE);
  31. const SERVICES_USER = ['reg', 'ns', 'ht'];
  32. const LF = "\n";
  33. const PLACEHOLDER_DOMAIN = 'example'; // From RFC2606: Reserved Top Level DNS Names > 2. TLDs for Testing, & Documentation Examples
  34. const PLACEHOLDER_IPV6 = '2001:db8::3'; // From RFC3849: IPv6 Address Prefix Reserved for Documentation
  35. const PLACEHOLDER_IPV4 = '203.0.113.42'; // From RFC5737: IPv4 Address Blocks Reserved for Documentation
  36. foreach (array_diff(scandir(ROOT_PATH . '/fn'), ['..', '.']) as $file)
  37. require ROOT_PATH . '/fn/' . $file;
  38. require ROOT_PATH . '/pages.php';