2024-04-01 19:36:03 +02:00
|
|
|
<?php
|
|
|
|
namespace Vichan;
|
|
|
|
|
2024-08-11 00:56:16 +02:00
|
|
|
use RuntimeException;
|
2024-10-04 12:57:37 +02:00
|
|
|
use Vichan\Data\Driver\{HttpDriver, ErrorLogLogDriver, FileLogDriver, LogDriver, StderrLogDriver, SyslogLogDriver};
|
2024-08-15 15:11:32 +02:00
|
|
|
use Vichan\Service\HCaptchaQuery;
|
2024-08-15 15:17:54 +02:00
|
|
|
use Vichan\Service\NativeCaptchaQuery;
|
2024-08-15 15:11:32 +02:00
|
|
|
use Vichan\Service\ReCaptchaQuery;
|
|
|
|
use Vichan\Service\RemoteCaptchaQuery;
|
2024-04-01 19:36:03 +02:00
|
|
|
|
2024-04-03 23:42:05 +02:00
|
|
|
defined('TINYBOARD') or exit;
|
2024-04-01 19:36:03 +02:00
|
|
|
|
2024-08-11 00:56:16 +02:00
|
|
|
class Context {
|
|
|
|
private array $definitions;
|
2024-04-01 19:36:03 +02:00
|
|
|
|
2024-08-11 00:56:16 +02:00
|
|
|
public function __construct(array $definitions) {
|
|
|
|
$this->definitions = $definitions;
|
2024-04-09 11:20:04 +02:00
|
|
|
}
|
|
|
|
|
2024-08-11 00:56:16 +02:00
|
|
|
public function get(string $name): mixed {
|
|
|
|
if (!isset($this->definitions[$name])) {
|
|
|
|
throw new RuntimeException("Could not find a dependency named $name");
|
2024-04-03 23:42:05 +02:00
|
|
|
}
|
|
|
|
|
2024-08-11 00:56:16 +02:00
|
|
|
$ret = $this->definitions[$name];
|
|
|
|
if (is_callable($ret) && !is_string($ret) && !is_array($ret)) {
|
|
|
|
$ret = $ret($this);
|
|
|
|
$this->definitions[$name] = $ret;
|
|
|
|
}
|
|
|
|
return $ret;
|
2024-04-09 11:20:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-11 00:56:16 +02:00
|
|
|
function build_context(array $config): Context {
|
|
|
|
return new Context([
|
|
|
|
'config' => $config,
|
2024-10-04 12:57:37 +02:00
|
|
|
LogDriver::class => function($c) {
|
2024-08-11 00:56:16 +02:00
|
|
|
$config = $c->get('config');
|
|
|
|
|
|
|
|
$name = $config['log_system']['name'];
|
2024-10-04 12:57:37 +02:00
|
|
|
$level = $config['debug'] ? LogDriver::DEBUG : LogDriver::NOTICE;
|
2024-08-11 00:56:16 +02:00
|
|
|
$backend = $config['log_system']['type'];
|
|
|
|
|
|
|
|
// Check 'syslog' for backwards compatibility.
|
|
|
|
if ((isset($config['syslog']) && $config['syslog']) || $backend === 'syslog') {
|
2024-10-04 12:57:37 +02:00
|
|
|
return new SyslogLogDriver($name, $level, $this->config['log_system']['syslog_stderr']);
|
2024-08-11 00:56:16 +02:00
|
|
|
} elseif ($backend === 'file') {
|
2024-10-04 12:57:37 +02:00
|
|
|
return new FileLogDriver($name, $level, $this->config['log_system']['file_path']);
|
2024-08-11 00:56:16 +02:00
|
|
|
} elseif ($backend === 'stderr') {
|
2024-10-04 12:57:37 +02:00
|
|
|
return new StderrLogDriver($name, $level);
|
2024-08-11 00:56:16 +02:00
|
|
|
} else {
|
2024-10-04 12:57:37 +02:00
|
|
|
return new ErrorLogLogDriver($name, $level);
|
2024-08-11 00:56:16 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
HttpDriver::class => function($c) {
|
|
|
|
$config = $c->get('config');
|
2024-10-04 01:01:47 +02:00
|
|
|
return new HttpDriver($config['upload_by_url_timeout'], $config['max_filesize']);
|
2024-08-15 15:11:32 +02:00
|
|
|
},
|
|
|
|
RemoteCaptchaQuery::class => function($c) {
|
|
|
|
$config = $c->get('config');
|
|
|
|
$http = $c->get(HttpDriver::class);
|
2024-08-15 16:11:28 +02:00
|
|
|
switch ($config['captcha']['provider']) {
|
|
|
|
case 'recaptcha':
|
|
|
|
return new ReCaptchaQuery($http, $config['captcha']['recaptcha']['secret']);
|
|
|
|
case 'hcaptcha':
|
|
|
|
return new HCaptchaQuery(
|
|
|
|
$http,
|
|
|
|
$config['captcha']['hcaptcha']['secret'],
|
|
|
|
$config['captcha']['hcaptcha']['sitekey']
|
|
|
|
);
|
|
|
|
default:
|
|
|
|
throw new RuntimeException('No remote captcha service available');
|
2024-08-15 15:11:32 +02:00
|
|
|
}
|
2024-08-15 15:17:54 +02:00
|
|
|
},
|
|
|
|
NativeCaptchaQuery::class => function($c) {
|
|
|
|
$config = $c->get('config');
|
2024-08-15 16:33:56 +02:00
|
|
|
if ($config['captcha']['provider'] !== 'native') {
|
2024-08-15 16:11:28 +02:00
|
|
|
throw new RuntimeException('No native captcha service available');
|
|
|
|
}
|
|
|
|
return new NativeCaptchaQuery(
|
|
|
|
$c->get(HttpDriver::class),
|
2024-08-15 15:17:54 +02:00
|
|
|
$config['domain'],
|
2024-08-15 16:33:56 +02:00
|
|
|
$config['captcha']['native']['provider_check'],
|
|
|
|
$config['captcha']['native']['extra']
|
2024-08-15 15:17:54 +02:00
|
|
|
);
|
2024-04-07 21:10:39 +02:00
|
|
|
},
|
|
|
|
CacheDriver::class => function($c) {
|
|
|
|
// Use the global for backwards compatibility.
|
|
|
|
return \cache::getCache();
|
2024-08-11 00:56:16 +02:00
|
|
|
}
|
|
|
|
]);
|
2024-04-01 19:36:03 +02:00
|
|
|
}
|