1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package CleverStyle CMS |
4
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
5
|
|
|
* @copyright Copyright (c) 2011-2015, Nazar Mokrynskyi |
6
|
|
|
* @license MIT License, see license.txt |
7
|
|
|
*/ |
8
|
|
|
namespace cs; |
9
|
|
|
use h; |
10
|
|
|
/** |
11
|
|
|
* Core class. |
12
|
|
|
* Provides loading of base system configuration |
13
|
|
|
* |
14
|
|
|
* @method static Core instance($check = false) |
15
|
|
|
*/ |
16
|
|
|
class Core { |
17
|
|
|
use Singleton; |
18
|
|
|
/** |
19
|
|
|
* Is object constructed |
20
|
|
|
* @var bool |
21
|
|
|
*/ |
22
|
|
|
protected $constructed = false; |
23
|
|
|
/** |
24
|
|
|
* @var mixed[] |
25
|
|
|
*/ |
26
|
|
|
protected $config = []; |
27
|
|
|
/** |
28
|
|
|
* Loading of base system configuration, creating of missing directories |
29
|
|
|
*/ |
30
|
|
|
protected function construct () { |
31
|
|
|
$this->config = $this->load_config(); |
32
|
|
|
_include_once(DIR.'/config/main.php', false); |
33
|
|
|
defined('DEBUG') || define('DEBUG', false); |
34
|
|
|
defined('DOMAIN') || define('DOMAIN', $this->config['domain']); |
35
|
|
|
date_default_timezone_set($this->config['timezone']); |
36
|
|
|
if (!is_dir(PUBLIC_STORAGE)) { |
37
|
|
|
@mkdir(PUBLIC_STORAGE, 0775, true); |
38
|
|
|
file_put_contents( |
39
|
|
|
PUBLIC_STORAGE.'/.htaccess', |
40
|
|
|
'Allow From All |
41
|
|
|
<ifModule mod_headers.c> |
42
|
|
|
Header always append X-Frame-Options DENY |
43
|
|
|
Header set Content-Type application/octet-stream |
44
|
|
|
</ifModule> |
45
|
|
|
' |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
if (!is_dir(CACHE)) { |
49
|
|
|
@mkdir(CACHE, 0770); |
50
|
|
|
} |
51
|
|
|
if (!is_dir(PUBLIC_CACHE)) { |
52
|
|
|
@mkdir(PUBLIC_CACHE, 0770); |
53
|
|
|
file_put_contents( |
54
|
|
|
PUBLIC_CACHE.'/.htaccess', |
55
|
|
|
'<FilesMatch "\.(css|js|html)$"> |
56
|
|
|
Allow From All |
57
|
|
|
</FilesMatch> |
58
|
|
|
<ifModule mod_expires.c> |
59
|
|
|
ExpiresActive On |
60
|
|
|
ExpiresDefault "access plus 1 month" |
61
|
|
|
</ifModule> |
62
|
|
|
<ifModule mod_headers.c> |
63
|
|
|
Header set Cache-Control "max-age=2592000, public" |
64
|
|
|
</ifModule> |
65
|
|
|
AddEncoding gzip .js |
66
|
|
|
AddEncoding gzip .css |
67
|
|
|
AddEncoding gzip .html |
68
|
|
|
' |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
if (!is_dir(LOGS)) { |
72
|
|
|
@mkdir(LOGS, 0770); |
73
|
|
|
} |
74
|
|
|
if (!is_dir(TEMP)) { |
75
|
|
|
@mkdir(TEMP, 0775); |
76
|
|
|
file_put_contents( |
77
|
|
|
TEMP.'/.htaccess', |
78
|
|
|
"Allow From All\n" |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
$this->fill_post_request(); |
82
|
|
|
$this->constructed = true; |
83
|
|
|
} |
84
|
|
|
/** |
85
|
|
|
* Fill `$_POST` and `$_REQUEST` when there is request method different than POST or if Content-Type is JSON |
86
|
|
|
*/ |
87
|
|
|
protected function fill_post_request () { |
88
|
|
|
/** |
89
|
|
|
* @var _SERVER $_SERVER |
90
|
|
|
*/ |
91
|
|
|
if (!$_SERVER->content_type) { |
92
|
|
|
return; |
93
|
|
|
} |
94
|
|
|
/** |
95
|
|
|
* Support for JSON requests, filling $_POST array for request method different than POST |
96
|
|
|
*/ |
97
|
|
|
if (preg_match('#^application/([^+\s]+\+)?json#', $_SERVER->content_type)) { |
98
|
|
|
$_POST = _json_decode(@file_get_contents('php://input')) ?: []; |
99
|
|
|
} elseif ( |
100
|
|
|
strtolower($_SERVER->request_method) !== 'post' && |
101
|
|
|
strpos($_SERVER->content_type, 'application/x-www-form-urlencoded') === 0 |
102
|
|
|
) { |
103
|
|
|
@parse_str(file_get_contents('php://input'), $_POST); |
104
|
|
|
} |
105
|
|
|
$_REQUEST = $_POST + $_REQUEST; |
106
|
|
|
} |
107
|
|
|
/** |
108
|
|
|
* Load main.json config file and return array of it contents |
109
|
|
|
* |
110
|
|
|
* @return array |
111
|
|
|
* |
112
|
|
|
* @throws ExitException |
113
|
|
|
*/ |
114
|
|
|
protected function load_config () { |
115
|
|
|
if (!file_exists(DIR.'/config/main.json')) { |
116
|
|
|
throw new ExitException( |
117
|
|
|
h::p('Config file not found, is system installed properly?'). |
118
|
|
|
h::a( |
119
|
|
|
'How to install CleverStyle CMS', |
120
|
|
|
[ |
121
|
|
|
'href' => 'https://github.com/nazar-pc/CleverStyle-CMS/wiki/Installation' |
122
|
|
|
] |
123
|
|
|
), |
124
|
|
|
500 |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
return file_get_json_nocomments(DIR.'/config/main.json'); |
128
|
|
|
} |
129
|
|
|
/** |
130
|
|
|
* Getting of base configuration parameter |
131
|
|
|
* |
132
|
|
|
* @param string $item |
133
|
|
|
* |
134
|
|
|
* @return false|string |
135
|
|
|
*/ |
136
|
|
|
function get ($item) { |
137
|
|
|
return isset($this->config[$item]) ? $this->config[$item] : false; |
138
|
|
|
} |
139
|
|
|
/** |
140
|
|
|
* Setting of base configuration parameter (available only at object construction) |
141
|
|
|
* |
142
|
|
|
* @param string $item |
143
|
|
|
* @param mixed $value |
144
|
|
|
*/ |
145
|
|
|
function set ($item, $value) { |
146
|
|
|
if (!$this->constructed) { |
147
|
|
|
$this->config[$item] = $value; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
/** |
151
|
|
|
* Getting of base configuration parameter |
152
|
|
|
* |
153
|
|
|
* @param string $item |
154
|
|
|
* |
155
|
|
|
* @return false|string |
156
|
|
|
*/ |
157
|
|
|
function __get ($item) { |
158
|
|
|
return $this->get($item); |
159
|
|
|
} |
160
|
|
|
/** |
161
|
|
|
* Setting of base configuration parameter (available only at object construction) |
162
|
|
|
* |
163
|
|
|
* @param string $item |
164
|
|
|
* @param mixed $value |
165
|
|
|
*/ |
166
|
|
|
function __set ($item, $value) { |
167
|
|
|
$this->set($item, $value); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|