This repository has been archived by the owner on Aug 13, 2022. It is now read-only.
/
TwigExtension.php
238 lines (210 loc) · 6.95 KB
/
TwigExtension.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
<?php
declare(strict_types=1);
/*
* This file is part of the MediaModule for Zikula.
*
* (c) Christian Flach <hi@christianflach.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cmfcmf\Module\MediaModule\Twig;
use Cmfcmf\Module\MediaModule\Entity\Collection\CollectionEntity;
use Cmfcmf\Module\MediaModule\Entity\Media\AbstractMediaEntity;
use Cmfcmf\Module\MediaModule\Helper\PHPIniHelper;
use Cmfcmf\Module\MediaModule\Security\SecurityManager;
use Cmfcmf\Module\MediaModule\Upgrade\VersionChecker;
use Github\Exception\RuntimeException;
use Michelf\MarkdownExtra;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
use Zikula\Bundle\HookBundle\Dispatcher\HookDispatcherInterface;
use Zikula\Bundle\HookBundle\Hook\FilterHook;
use Zikula\ExtensionsModule\Api\ApiInterface\VariableApiInterface;
use Zikula\ExtensionsModule\Entity\RepositoryInterface\ExtensionRepositoryInterface;
/**
* Provides some custom Twig extensions.
*/
class TwigExtension extends AbstractExtension
{
/**
* @var TranslatorInterface
*/
private $translator;
/**
* @var MarkdownExtra
*/
private $markdownExtra;
/**
* @var HookDispatcherInterface
*/
private $hookDispatcher;
/**
* @var SecurityManager
*/
private $securityManager;
/**
* @var VersionChecker
*/
private $versionChecker;
/**
* @var ExtensionRepositoryInterface
*/
private $extensionRepository;
/**
* @var VariableApiInterface
*/
private $variableApi;
public function __construct(
TranslatorInterface $translator,
MarkdownExtra $markdownExtra,
HookDispatcherInterface $hookDispatcher,
SecurityManager $securityManager,
VersionChecker $versionChecker,
ExtensionRepositoryInterface $extensionRepository,
VariableApiInterface $variableApi
) {
$this->translator = $translator;
$this->markdownExtra = $markdownExtra;
$this->hookDispatcher = $hookDispatcher;
$this->securityManager = $securityManager;
$this->versionChecker = $versionChecker;
$this->extensionRepository = $extensionRepository;
$this->variableApi = $variableApi;
}
/**
* {@inheritdoc}
*/
public function getFilters()
{
return [
new TwigFilter('cmfcmfmediamodule_getdescription', [$this, 'escapeDescription'], ['is_safe' => ['html']])
];
}
/**
* {@inheritdoc}
*/
public function getFunctions()
{
return [
new TwigFunction('cmfcmfmediamodule_hasPermission', [$this, 'hasPermission']),
new TwigFunction('cmfcmfmediamodule_newversionavailable', [$this, 'newVersionAvailable']),
new TwigFunction('cmfcmfmediamodule_maxfilesize', [$this, 'maxFileSize'])
];
}
/**
* Checks whether or not a new version of the module is available.
*
* @return array|bool|string
*/
public function newVersionAvailable()
{
$lastNewVersionCheck = $this->variableApi->get('CmfcmfMediaModule', 'lastNewVersionCheck', 0);
$extension = $this->extensionRepository->findOneByName('CmfcmfMediaModule');
if (null === $extension) {
return false;
}
$currentVersion = $extension['version'];
if (time() > $lastNewVersionCheck + 24 * 60 * 60) {
// Last version check older than a day.
$this->checkForNewVersion($currentVersion);
}
$newVersionAvailable = $this->variableApi->get('CmfcmfMediaModule', 'newVersionAvailable', false);
if (false !== $newVersionAvailable) {
if ($newVersionAvailable === $currentVersion) {
// Somehow the user manually upgraded the module.
// Remove "Install new version" popup.
$this->variableApi->set('CmfcmfMediaModule', 'newVersionAvailable', false);
} else {
return $newVersionAvailable;
}
}
return false;
}
/**
* Checks if a new version of the module is available.
* Requires the CURL PHP extension.
*
* @param $currentVersion
*/
private function checkForNewVersion($currentVersion)
{
if (!function_exists('curl_init')) {
return;
}
$this->variableApi->set('CmfcmfMediaModule', 'lastNewVersionCheck', time());
try {
if (!$this->versionChecker->checkRateLimit()) {
// The remaining rate limit isn't high enough.
return;
}
$release = $this->versionChecker->getReleaseToUpgradeTo($currentVersion);
if (false !== $release) {
$this->variableApi->set('CmfcmfMediaModule', 'newVersionAvailable', $release['tag_name']);
}
} catch (RuntimeException $e) {
// Something went wrong with the GitHub API. Fail silently.
}
}
/**
* @param CollectionEntity|AbstractMediaEntity $entity
*
* @return string
*/
public function escapeDescription($entity)
{
$description = $entity->getDescription();
$strategy = null;
$hookName = null;
if ($entity instanceof CollectionEntity) {
$strategy = $this->variableApi->get('CmfcmfMediaModule', 'descriptionEscapingStrategyForCollection');
$hookName = 'collections';
} elseif ($entity instanceof AbstractMediaEntity) {
$strategy = $this->variableApi->get('CmfcmfMediaModule', 'descriptionEscapingStrategyForMedia');
$hookName = 'media';
} else {
throw new \LogicException();
}
$eventName = 'cmfcmfmediamodule.filter_hooks.' . $hookName . '.filter';
$hook = new FilterHook($description);
$description = $this->hookDispatcher->dispatch($eventName, $hook)->getData();
if (null === $description) {
$description = '';
}
switch ($strategy) {
case 'raw':
return $description;
case 'text':
return nl2br(htmlspecialchars($description));
case 'markdown':
return $this->markdownExtra->transform($description);
default:
throw new \LogicException();
}
}
/**
* Checks whether or not the current user has permission.
*
* @param string|object $objectOrType
* @param string $action
*
* @return bool
*/
public function hasPermission($objectOrType, $action)
{
return $this->securityManager->hasPermission($objectOrType, $action);
}
public function maxFileSize()
{
return PHPIniHelper::getMaxUploadSize();
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'cmfcmfmediamodule_twigextension';
}
}