init.php 1.9 KB

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