aboutsummaryrefslogtreecommitdiff
path: root/init.php
blob: 6d9f5f0b9981561d27b6370721e19b6093c25225 (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
<?php

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

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

define('ROOT', __DIR__);
define('START_TIME', microtime(true));

set_include_path(get_include_path().PATH_SEPARATOR.ROOT);

spl_autoload_register(function($class) {
    if (str_ends_with($class, 'Exception')) {
        $path = ROOT.'/engine/exceptions/'.$class.'.php';
    } else if (in_array($class, ['MySQLConnection', 'SQLiteConnection', 'CommonDatabase'])) {
        $path = ROOT.'/engine/database/'.$class.'.php';
    } else if (str_starts_with($class, 'handler\\')) {
        $path = ROOT.'/'.str_replace('\\', '/', $class).'.php';
    }

    if (isset($path)) {
        if (!is_file($path))
            return;
    } else {
        foreach (['engine', 'lib', 'model'] as $dir) {
            if (is_file($path = ROOT.'/'.$dir.'/'.$class.'.php'))
                break;
        }
    }

    require_once $path;
});

$config = require_once 'config.php';
if (file_exists(ROOT.'/config-local.php')) {
    $config = array_replace($config, require 'config-local.php');
}

// turn off errors output on production domains

require_once 'functions.php';

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

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

if (!$config['is_dev']) {
    error_reporting(0);
    ini_set('display_errors', 0);
}

logging::setLogFile($config['log_file']);
logging::enable();

require 'vendor/autoload.php';