-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathextend.php
156 lines (150 loc) · 6.05 KB
/
extend.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
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
use Flarum0x\Extend\DiskS3\DiskS3Driver;
use Flarum0x\Extend\ImageSize\ConfigureImgs;
use Flarum0x\Extend\ImageSize\FormatImgs;
use Flarum0x\Extend\Misc\GenerateCustomSlugWhenSaving;
// use Flarum0x\Extend\Misc\GlobalLandingPageMiddleware;
use Flarum\Extend;
use Flarum\Discussion\Event\Saving;
use Flarum\Extend\ThrottleApi;
use Flarum\Foundation\Application;
use Flarum\Foundation\Paths;
use Flarum\Frontend\Document;
use Flarum\Http\RequestUtil;
use Flarum\Http\UrlGenerator;
use Flarum\Post\Post;
use Illuminate\Support\Arr;
use Psr\Http\Message\ServerRequestInterface;
$config = @include 'config.php';
return array_filter([
// (new Extend\Middleware('forum'))->add(GlobalLandingPageMiddleware::class),
(new Extend\Frontend('forum'))
->css(__DIR__ . '/resources/less/post-table.less')
->css(__DIR__ . '/resources/less/nav-widget.less')
->css(__DIR__ . '/resources/less/custom.less'),
(new Extend\Event)
->listen(Saving::class, GenerateCustomSlugWhenSaving::class),
/**
* redis for queue / session / cache
*
* config example:
* [
* 'redisConfig' => [
* 'host' => 'redis',
* 'password' => null,
* 'port' => 6379,
* 'database' => 0
* ],
* ]
*/
(function () use ($config) {
if (empty($config) || !array_key_exists('redisConfig', $config) || empty($redisConfig = $config['redisConfig'])) {
return null;
}
return new Blomstra\Redis\Extend\Redis($redisConfig);
})(),
// prevent duplicate post
(new ThrottleApi())
->set('preventDuplicate', function (ServerRequestInterface $request) {
if (!in_array($request->getAttribute('routeName'), ['discussions.create', 'posts.create'])) {
return;
}
$actor = RequestUtil::getActor($request);
$latestPost = Post::where('user_id', $actor->id)
->where('created_at', '>=', new DateTime('-10 minutes'))
->orderBy('id', 'DESC')
->first();
if ($latestPost) {
$latestContent = resolve('flarum.formatter')->unparse($latestPost->content);
$currentContent = Arr::get($request->getParsedBody(), 'data.attributes.content', '');
if ($latestContent === $currentContent) {
return true;
}
}
}),
// custom header
(new Extend\Frontend('forum'))
->content(function (Document $document) {
$app = resolve(Application::class);
$headStrList = [];
$prefetchUrlList = $app->config('prefetchUrlList', []);
foreach ($prefetchUrlList as $value) {
$headStrList[] = '<link rel="dns-prefetch" href="' . $value . '">';
$headStrList[] = '<link rel="preconnect" href="' . $value . '">';
}
$customHeadStr = $app->config('customHead', '');
if ($customHeadStr) {
$headStrList[] = $customHeadStr;
}
$document->head = array_merge($headStrList, $document->head);
}),
// s3 disk driver
(function () use ($config) {
if (empty($config) || !array_key_exists('disk_s3_config', $config)) {
return null;
}
return (new Extend\Filesystem())->driver('disk_s3', DiskS3Driver::class);
})(),
// CDN URL Replacement
(new Extend\Filesystem())
->disk('flarum-assets', function (Paths $paths, UrlGenerator $url) {
$app = resolve(Application::class);
$cdnBase = rtrim($app->config('cdnUrl', $url->to('forum')->path('')), '\/');
$origUrl = $app->config('url', $url->to('forum')->path(''));
return [
'root' => "$paths->public/assets",
'url' => str_replace($origUrl, $cdnBase, $url->to('forum')->path('assets'))
];
})
->disk('flarum-avatars', function (Paths $paths, UrlGenerator $url) {
$app = resolve(Application::class);
$cdnBase = rtrim($app->config('cdnUrl', $url->to('forum')->path('')), '\/');
$origUrl = $app->config('url', $url->to('forum')->path(''));
return [
'root' => "$paths->public/assets/avatars",
'url' => str_replace($origUrl, $cdnBase, $url->to('forum')->path('assets/avatars'))
];
}),
// fancybox
(new Extend\Frontend('forum'))
->content(function (Document $document) {
$document->head[] = '<script defer type="text/javascript" src="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/fancybox/3.5.7/jquery.fancybox.min.js"></script>';
$document->head[] = '<link rel="preload" as="style" href="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/fancybox/3.5.7/jquery.fancybox.min.css" onload="this.onload=null;this.rel=\'stylesheet\'">';
$document->foot[] = <<<HTML
<script>
flarum.core.compat.extend.extend(flarum.core.compat['components/CommentPost'].prototype, 'oncreate', function (output, vnode) {
const self = this;
this.$('img').not('.emoji').not(".Avatar").not($(".PostMeta-ip img")).each(function () {
var currentImage = $(this);
var checksrc = currentImage.attr("data-orig-src");
if (checksrc) {
$(this).wrap("<a class=\"fancybox\" href='" + currentImage.attr("data-orig-src") + "'></a>");
}
else {
$(this).wrap("<a class=\"fancybox\" href='" + currentImage.attr("src") + "'></a>");
}
try {
$().ready(function(){
$().fancybox({
selector: '.fancybox'
});
})
} catch (e) {
console.error(e.name);
console.error(e.message);
}
});
});
</script>
HTML;
}),
(new Extend\Formatter)
->render(FormatImgs::class)
->configure(ConfigureImgs::class),
], 'boolval');