-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
executable file
·131 lines (108 loc) · 3.63 KB
/
index.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
<?php
/**
* Kirby3 Dejure - Auto-linking legal norms to dejure.org for Kirby v3
*
* @package Kirby CMS
* @author S1SYPHOS <[email protected]>
* @link http://twobrain.io
* @version 1.4.4
* @license MIT
*/
@include_once __DIR__ . '/vendor/autoload.php';
use S1SYPHOS\DejureOnline;
/**
* Initializes new DejureOnline (DJO) instance
*
* @return \S1SYPHOS\DejureOnline
*/
function dejureInit(): \S1SYPHOS\DejureOnline
{
# Ensure that path to cache directory exists
$cacheDir = kirby()->root('cache') . '/dejure-online.org/';
# Initialize DJO instance
return new DejureOnline(
option('kirby3-dejure.driver', 'file'),
option('kirby3-dejure.caching', ['storage' => $cacheDir])
);
}
/**
* Processes linkable citations & caches text (if uncached or expired)
*
* @param string $text Original (unprocessed) text
* @param string $ignore Judicial file numbers to be ignored
* @return string Processed text if successful, otherwise unprocessed text
*/
function dejurify(string $text, string $ignore = ''): string
{
# Create DJO instance
$object = dejureInit();
# Set defaults
# (1) General information
$object->setEmail(option('kirby3-dejure.email', ''));
# (2) Text processing
$object->setBuzer(option('kirby3-dejure.buzer', false));
$object->setClass(option('kirby3-dejure.class', ''));
$object->setLineBreak(option('kirby3-dejure.lineBreak', 'auto'));
$object->setLinkStyle(option('kirby3-dejure.linkStyle', 'weit'));
$object->setTarget(option('kirby3-dejure.target', '_blank'));
$object->setTooltip(option('kirby3-dejure.tooltip', 'beschreibend'));
# (3) Connection
$object->setStreamTimeout(option('kirby3-dejure.streamTimeout', 10));
$object->setTimeout(option('kirby3-dejure.timeout', 3));
$object->setUserAgent(option('kirby3-dejure.userAgent', 'kirby3-dejure @ ' . Kirby\Http\Server::host()));
# (4) Caching
$object->setCacheDuration(option('kirby3-dejure.cacheDuration', 2));
# Fallback to global ignore
if (empty($ignore)) {
$ignore = option('kirby3-dejure.ignore', '');
}
return $object->dejurify($text, $ignore);
}
/**
* Clears DJO cache
*
* @return bool Whether cache was cleared
*/
function clearDJO(): bool
{
# Create DJO instance
$object = dejureInit();
# Clear cache
return $object->clearCache();
}
Kirby::plugin('s1syphos/kirby3-dejure', [
'hooks' => [
'kirbytext:after' => function (string $text): string
{
# Process text by all means when page template is allowlisted
if (in_array(page()->intendedTemplate(), option('kirby3-dejure.allowList', [])) === true) {
return dejurify($text);
}
# Leave text unmodified if ..
# (1) .. plugin is disabled (default)
if (!option('kirby3-dejure.enabled', false)) {
return $text;
}
# (2) .. page template is blocklisted
if (in_array(page()->intendedTemplate(), option('kirby3-dejure.blockList', [])) === true) {
return $text;
}
return dejurify($text);
},
],
'pageMethods' => [
'dejurify' => function (string $text, string $ignore = ''): string
{
return dejurify($text, $ignore);
},
],
'fieldMethods' => [
'dejurify' => function (Kirby\Cms\Field $field, string $ignore = '', bool $useKirbytext = true): string
{
if ($useKirbytext === true) {
return dejurify($field->kt(), $ignore);
}
return dejurify($field, $ignore);
},
],
]);