init.php 2.0 KB

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