aboutsummaryrefslogtreecommitdiff
path: root/init.php
blob: b1396249a2132b1389757473e863020c93583470 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('assert.exception', 1);
date_default_timezone_set('Europe/Moscow');

mb_internal_encoding('UTF-8');
mb_regex_encoding('UTF-8');

const APP_ROOT = __DIR__;
define('START_TIME', microtime(true));

set_include_path(get_include_path().PATH_SEPARATOR.APP_ROOT);

spl_autoload_register(function($class) {
    static $libs = [
        'lib/tags' => ['Tag', 'tags'],
        'lib/pages' => ['Page', 'pages'],
        'lib/posts' => ['Post', 'posts'],
        'lib/uploads' => ['Upload', 'uploads'],
        'engine/model' => ['model'],
        'engine/skin' => ['SkinContext'],
    ];

    if (str_ends_with($class, 'Handler')) {
        $path = APP_ROOT.'/handler/'.str_replace('\\', '/', $class).'.php';
    } else {
        foreach ($libs as $lib_file => $class_names) {
            if (in_array($class, $class_names)) {
                $path = APP_ROOT.'/'.$lib_file.'.php';
                break;
            }
        }
    }

    if (!isset($path))
        $path = APP_ROOT.'/lib/'.$class.'.php';

    if (!is_file($path))
        return;

    require_once $path;
});

if (!file_exists(APP_ROOT.'/config.yaml'))
    die('Fatal: config.yaml not found');

$config = yaml_parse_file(APP_ROOT.'/config.yaml');
if ($config === false)
    die('Fatal: failed to parse config.yaml');

// i know what i'm doing. do you?
umask($config['umask']);

require_once 'functions.php';
require_once 'engine/mysql.php';
require_once 'engine/router.php';
require_once 'engine/request.php';
require_once 'engine/logging.php';

try {
    if (is_cli()) {
        verify_hostname($config['domain']);
        $_SERVER['HTTP_HOST'] = $config['domain'];
        $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
    } else {
        verify_hostname();
        if (array_key_exists('HTTP_X_REAL_IP', $_SERVER))
            $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_X_REAL_IP'];

        require_once 'engine/strings.php';
        require_once 'engine/skin.php';
        require_once 'lib/admin.php';
    }
} catch (RuntimeException $e) {
    die('Fatal error: '.$e->getMessage());
}

$__logger = is_dev()
    ? new FileLogger(APP_ROOT.'/log/debug.log')
    : new DatabaseLogger();
$__logger->enable();

if (!is_dev()) {
    if (file_exists(APP_ROOT.'/config-static.php'))
        $config['static'] = require_once 'config-static.php';
    else
        die('confic-static.php not found');

    // turn off errors output on production domains
    error_reporting(0);
    ini_set('display_errors', 0);
}

if (!is_cli()) {
    $__lang = Strings::getInstance();
    $__lang->load('main');
}

require 'vendor/autoload.php';