-
Notifications
You must be signed in to change notification settings - Fork 345
/
Copy pathcommon.php
273 lines (228 loc) · 8.23 KB
/
common.php
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
use Dice\Dice;
use helpers\Configuration;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\ErrorLogHandler;
use Monolog\Handler\NullHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
require __DIR__ . '/constants.php';
$autoloader = @include BASEDIR . '/vendor/autoload.php'; // we will show custom error
if ($autoloader === false) {
echo 'The PHP dependencies are missing. Did you run `composer install` in the selfoss directory?';
exit;
}
$startup_error = error_get_last();
// F3 crashes when there were PHP startups error even though
// they might not affect the program (e.g. unable to load an extension).
// It also sets its own error_reporting value and uses the previous one
// as a signal to disable the initialization failure check.
error_reporting(0);
$f3 = Base::instance();
// Disable deprecation warnings.
// Dice uses ReflectionParameter::getClass(), which is deprecated in PHP 8
// but we have not set an error handler yet because it needs a Logger instantiated by Dice.
error_reporting(E_ALL & ~E_DEPRECATED);
$f3->set('AUTOLOAD', false);
$f3->set('BASEDIR', BASEDIR);
$f3->set('LOCALES', BASEDIR . '/assets/locale/');
$configuration = new Configuration(__DIR__ . '/../config.ini', $_ENV);
$f3->set('DEBUG', $configuration->debug);
$f3->set('cache', $configuration->cache);
$f3->set('language', $configuration->language);
$dice = new Dice();
// DI rules
$substitutions = [
'substitutions' => [
// Instantiate configuration container.
Configuration::class => [
'instance' => function() use ($configuration) {
return $configuration;
},
'shared' => true,
],
// Choose database implementation based on config
daos\DatabaseInterface::class => ['instance' => 'daos\\' . $configuration->dbType . '\\Database'],
daos\ItemsInterface::class => ['instance' => 'daos\\' . $configuration->dbType . '\\Items'],
daos\SourcesInterface::class => ['instance' => 'daos\\' . $configuration->dbType . '\\Sources'],
daos\TagsInterface::class => ['instance' => 'daos\\' . $configuration->dbType . '\\Tags'],
Dice::class => ['instance' => function() use ($dice) {
return $dice;
}],
],
];
$shared = array_merge($substitutions, [
'shared' => true,
]);
$dice->addRule(helpers\Authentication::class, $shared);
// Database bridges
$dice->addRule(daos\Database::class, $shared);
$dice->addRule(daos\Items::class, $shared);
$dice->addRule(daos\Sources::class, $shared);
$dice->addRule(daos\Tags::class, $shared);
// Database implementation
$dice->addRule(daos\DatabaseInterface::class, $shared);
$dice->addRule(daos\ItemsInterface::class, $shared);
$dice->addRule(daos\SourcesInterface::class, $shared);
$dice->addRule(daos\TagsInterface::class, $shared);
// Database connection
if ($configuration->dbType === 'sqlite') {
$db_file = $configuration->dbFile;
// create empty database file if it does not exist
if (!is_file($db_file)) {
touch($db_file);
}
$dsn = 'sqlite:' . $db_file;
$dbParams = [
$dsn,
];
} elseif ($configuration->dbType === 'mysql') {
$host = $configuration->dbHost;
$port = $configuration->dbPort;
$database = $configuration->dbDatabase;
if ($port !== null) {
$dsn = "mysql:host=$host; port=$port; dbname=$database";
} else {
$dsn = "mysql:host=$host; dbname=$database";
}
$dbParams = [
$dsn,
$configuration->dbUsername,
$configuration->dbPassword,
[PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4;'],
];
} elseif ($configuration->dbType === 'pgsql') {
$host = $configuration->dbHost;
$port = $configuration->dbPort;
$database = $configuration->dbDatabase;
if ($port !== null) {
$dsn = "pgsql:host=$host; port=$port; dbname=$database";
} else {
$dsn = "pgsql:host=$host; dbname=$database";
}
$dbParams = [
$dsn,
$configuration->dbUsername,
$configuration->dbPassword,
];
}
$sqlParams = array_merge($shared, [
'constructParams' => $dbParams,
]);
// Define regexp function for SQLite
if ($configuration->dbType === 'sqlite') {
$sqlParams = array_merge($sqlParams, [
'call' => [
[
// DB\SQL uses PDO instance through composition
// and forwards calls of non-existent methods to it.
// But Dice can only call existing methods.
// Let’s walk around these limitations by directly
// calling the __call magic method.
'__call',
[
// https://www.sqlite.org/lang_expr.html#the_like_glob_regexp_and_match_operators
'sqliteCreateFunction',
[
'regexp',
function($pattern, $text) {
return preg_match('/' . addcslashes($pattern, '/') . '/', $text);
},
2,
],
],
],
],
]);
}
$dice->addRule(DB\SQL::class, $sqlParams);
$dice->addRule('$iconStorageBackend', [
'instanceOf' => helpers\Storage\FileStorage::class,
'constructParams' => [
$configuration->datadir . '/favicons',
],
]);
$dice->addRule(helpers\IconStore::class, array_merge($shared, [
'constructParams' => [
['instance' => '$iconStorageBackend'],
],
]));
$dice->addRule('$thumbnailStorageBackend', [
'instanceOf' => helpers\Storage\FileStorage::class,
'constructParams' => [
$configuration->datadir . '/thumbnails',
],
]);
$dice->addRule(helpers\ThumbnailStore::class, array_merge($shared, [
'constructParams' => [
['instance' => '$thumbnailStorageBackend'],
],
]));
// Fallback rule
$dice->addRule('*', $substitutions);
$f3->set('CONTAINER', function($class) use ($dice) {
return $dice->create($class);
});
$dice->addRule(Logger::class, [
'shared' => true,
'constructParams' => ['selfoss'],
]);
$dice->addRule(helpers\FeedReader::class, [
'constructParams' => [
$configuration->cache,
],
]);
// init logger
$log = $dice->create(Logger::class);
if ($configuration->loggerLevel === 'NONE') {
$handler = new NullHandler();
} else {
$logger_destination = $configuration->loggerDestination;
if (strpos($logger_destination, 'file:') === 0) {
$handler = new StreamHandler(substr($logger_destination, 5), $configuration->loggerLevel);
} elseif ($logger_destination === 'error_log') {
$handler = new ErrorLogHandler(ErrorLogHandler::OPERATING_SYSTEM, $configuration->loggerLevel);
} else {
echo 'The `logger_destination` option needs to be either `error_log` or a file path prefixed by `file:`.';
exit;
}
$formatter = new LineFormatter(null, null, true, true);
$formatter->includeStacktraces(true);
$handler->setFormatter($formatter);
}
$log->pushHandler($handler);
if (isset($startup_error)) {
$log->warn('PHP likely encountered a startup error: ', [$startup_error]);
}
// init error handling
$f3->set('ONERROR',
function(Base $f3) use ($configuration, $log, $handler) {
$exception = $f3->get('EXCEPTION');
try {
if ($exception) {
$log->error($exception->getMessage(), ['exception' => $exception]);
} else {
$log->error($f3->get('ERROR.text'));
}
if ($configuration->debug !== 0) {
echo $f3->get('lang_error') . ': ';
echo $f3->get('ERROR.text') . "\n";
echo $f3->get('ERROR.trace');
} else {
if ($handler instanceof StreamHandler) {
echo $f3->get('lang_error_check_log_file', $handler->getUrl()) . PHP_EOL;
} elseif ($handler instanceof ErrorLogHandler) {
echo $f3->get('lang_error_check_system_logs') . PHP_EOL;
} else {
echo $f3->get('lang_error') . PHP_EOL;
}
}
} catch (Exception $e) {
echo $f3->get('lang_error_unwritable_logs') . PHP_EOL;
echo $e->getMessage() . PHP_EOL;
}
}
);
if ($configuration->debug !== 0) {
ini_set('display_errors', '0');
}