669a2b4b9abbbc2d363568aed5485ada971dd0d2
[GitHub/WoltLab/WCF.git] /
1 <?php
2 namespace wcf\system\package\plugin;
3 use wcf\data\acp\template\ACPTemplate;
4 use wcf\data\acp\template\ACPTemplateList;
5 use wcf\data\template\listener\TemplateListenerEditor;
6 use wcf\data\template\listener\TemplateListenerList;
7 use wcf\data\template\Template;
8 use wcf\data\template\TemplateList;
9 use wcf\system\cache\builder\TemplateListenerCodeCacheBuilder;
10 use wcf\system\devtools\pip\IDevtoolsPipEntryList;
11 use wcf\system\devtools\pip\IGuiPackageInstallationPlugin;
12 use wcf\system\devtools\pip\TXmlGuiPackageInstallationPlugin;
13 use wcf\system\form\builder\container\FormContainer;
14 use wcf\system\form\builder\field\data\processor\CustomFormFieldDataProcessor;
15 use wcf\system\form\builder\field\dependency\ValueFormFieldDependency;
16 use wcf\system\form\builder\field\IntegerFormField;
17 use wcf\system\form\builder\field\option\OptionFormField;
18 use wcf\system\form\builder\field\user\group\option\UserGroupOptionFormField;
19 use wcf\system\form\builder\field\validation\FormFieldValidationError;
20 use wcf\system\form\builder\field\validation\FormFieldValidator;
21 use wcf\system\form\builder\field\MultilineTextFormField;
22 use wcf\system\form\builder\field\SingleSelectionFormField;
23 use wcf\system\form\builder\field\TextFormField;
24 use wcf\system\form\builder\IFormDocument;
25 use wcf\system\WCF;
26 use wcf\util\StringUtil;
27
28 /**
29 * Installs, updates and deletes template listeners.
30 *
31 * @author Alexander Ebert, Matthias Schmidt
32 * @copyright 2001-2019 WoltLab GmbH
33 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
34 * @package WoltLabSuite\Core\System\Package\Plugin
35 */
36 class TemplateListenerPackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin implements IGuiPackageInstallationPlugin {
37 use TXmlGuiPackageInstallationPlugin {
38 setEntryData as defaultSetEntryData;
39 }
40
41 /**
42 * @inheritDoc
43 */
44 public $className = TemplateListenerEditor::class;
45
46 /**
47 * @inheritDoc
48 */
49 protected function handleDelete(array $items) {
50 $sql = "DELETE FROM wcf".WCF_N."_".$this->tableName."
51 WHERE packageID = ?
52 AND environment = ?
53 AND eventName = ?
54 AND name = ?
55 AND templateName = ?";
56 $statement = WCF::getDB()->prepareStatement($sql);
57 foreach ($items as $item) {
58 $statement->execute([
59 $this->installation->getPackageID(),
60 $item['elements']['environment'],
61 $item['elements']['eventname'],
62 $item['attributes']['name'],
63 $item['elements']['templatename']
64 ]);
65 }
66 }
67
68 /**
69 * @inheritDoc
70 */
71 protected function prepareImport(array $data) {
72 $niceValue = isset($data['elements']['nice']) ? intval($data['elements']['nice']) : 0;
73 if ($niceValue < -128) {
74 $niceValue = -128;
75 }
76 else if ($niceValue > 127) {
77 $niceValue = 127;
78 }
79
80 return [
81 'environment' => $data['elements']['environment'],
82 'eventName' => $data['elements']['eventname'],
83 'niceValue' => $niceValue,
84 'name' => $data['attributes']['name'],
85 'options' => isset($data['elements']['options']) ? StringUtil::normalizeCsv($data['elements']['options']) : '',
86 'permissions' => isset($data['elements']['permissions']) ? StringUtil::normalizeCsv($data['elements']['permissions']) : '',
87 'templateCode' => $data['elements']['templatecode'],
88 'templateName' => $data['elements']['templatename']
89 ];
90 }
91
92 /**
93 * @inheritDoc
94 */
95 protected function findExistingItem(array $data) {
96 $sql = "SELECT *
97 FROM wcf".WCF_N."_".$this->tableName."
98 WHERE packageID = ?
99 AND name = ?
100 AND templateName = ?
101 AND eventName = ?
102 AND environment = ?";
103 $parameters = [
104 $this->installation->getPackageID(),
105 $data['name'],
106 $data['templateName'],
107 $data['eventName'],
108 $data['environment']
109 ];
110
111 return [
112 'sql' => $sql,
113 'parameters' => $parameters
114 ];
115 }
116
117 /**
118 * @inheritDoc
119 */
120 protected function cleanup() {
121 // clear cache immediately
122 TemplateListenerCodeCacheBuilder::getInstance()->reset();
123 }
124
125 /**
126 * @inheritDoc
127 * @since 3.1
128 */
129 public static function getSyncDependencies() {
130 return [];
131 }
132
133 /**
134 * @inheritDoc
135 * @since 5.2
136 */
137 protected function addFormFields(IFormDocument $form) {
138 $ldq = preg_quote(WCF::getTPL()->getCompiler()->getLeftDelimiter(), '~');
139 $rdq = preg_quote(WCF::getTPL()->getCompiler()->getRightDelimiter(), '~');
140
141 $getEvents = function($templateList) use ($ldq, $rdq) {
142 $templateEvents = [];
143 /** @var ACPTemplate|Template $template */
144 foreach ($templateList as $template) {
145 if (preg_match_all("~{$ldq}event\ name\=\'(?<event>[\w]+)\'{$rdq}~", $template->getSource(), $matches)) {
146 $templates[$template->templateName] = $template->templateName;
147
148 foreach ($matches['event'] as $event) {
149 if (!isset($templateEvents[$template->templateName])) {
150 $templateEvents[$template->templateName] = [];
151 }
152
153 $templateEvents[$template->templateName][] = $event;
154 }
155 }
156 }
157
158 foreach ($templateEvents as &$events) {
159 sort($events);
160 }
161 unset($events);
162
163 return $templateEvents;
164 };
165
166 $templateList = new TemplateList();
167 $templateList->getConditionBuilder()->add(
168 'template.packageID IN (?)',
169 [array_merge(
170 [$this->installation->getPackage()->packageID],
171 array_keys($this->installation->getPackage()->getAllRequiredPackages())
172 )]
173 );
174 $templateList->getConditionBuilder()->add('template.templateGroupID IS NULL');
175 $templateList->sqlOrderBy = 'template.templateName ASC';
176 $templateList->readObjects();
177
178 $templateEvents = $getEvents($templateList);
179
180 $acpTemplateList = new ACPTemplateList();
181 $acpTemplateList->getConditionBuilder()->add(
182 'acp_template.packageID IN (?)',
183 [array_merge(
184 [$this->installation->getPackage()->packageID],
185 array_keys($this->installation->getPackage()->getAllRequiredPackages())
186 )]
187 );
188 $acpTemplateList->sqlOrderBy = 'acp_template.templateName ASC';
189 $acpTemplateList->readObjects();
190
191 $acpTemplateEvents = $getEvents($acpTemplateList);
192
193 /** @var FormContainer $dataContainer */
194 $dataContainer = $form->getNodeById('data');
195
196 $dataContainer->appendChildren([
197 TextFormField::create('name')
198 ->label('wcf.acp.pip.templateListener.name')
199 ->description('wcf.acp.pip.templateListener.name.description')
200 ->required()
201 ->addValidator(new FormFieldValidator('format', function(TextFormField $formField) {
202 if (!preg_match('~^[a-z][A-z]+$~', $formField->getValue())) {
203 $formField->addValidationError(
204 new FormFieldValidationError(
205 'format',
206 'wcf.acp.pip.templateListener.name.error.format'
207 )
208 );
209 }
210 })),
211
212 SingleSelectionFormField::create('environment')
213 ->label('wcf.acp.pip.templateListener.environment')
214 ->description('wcf.acp.pip.templateListener.environment.description')
215 ->required()
216 ->options([
217 'admin' => 'admin',
218 'user' => 'user'
219 ])
220 ->value('user')
221 ->addValidator(new FormFieldValidator('uniqueness', function(SingleSelectionFormField $formField) {
222 /** @var TextFormField $nameField */
223 $nameField = $formField->getDocument()->getNodeById('name');
224
225 /** @var SingleSelectionFormField $templateNameFormField */
226 $templateNameFormField = $formField->getDocument()->getNodeById('frontendTemplateName');
227
228 /** @var SingleSelectionFormField $acpTemplateNameFormField */
229 $acpTemplateNameFormField = $formField->getDocument()->getNodeById('acpTemplateName');
230
231 if (
232 $formField->getDocument()->getFormMode() === IFormDocument::FORM_MODE_CREATE ||
233 $this->editedEntry->getAttribute('name') !== $nameField->getSaveValue() ||
234 $this->editedEntry->getElementsByTagName('environment')->item(0)->nodeValue !== $formField->getSaveValue() ||
235 (
236 $formField->getSaveValue() === 'admin' &&
237 $this->editedEntry->getElementsByTagName('templatename')->item(0)->nodeValue !== $acpTemplateNameFormField->getSaveValue()
238 ) ||
239 (
240 $formField->getSaveValue() === 'user' &&
241 $this->editedEntry->getElementsByTagName('templatename')->item(0)->nodeValue !== $templateNameFormField->getSaveValue()
242 )
243 ) {
244 $listenerList = new TemplateListenerList();
245 $listenerList->getConditionBuilder()->add(
246 'name = ?',
247 [$nameField->getSaveValue()]
248 );
249
250 if ($formField->getSaveValue() === 'admin') {
251 /** @var SingleSelectionFormField $templateNameField */
252 $templateNameField = $formField->getDocument()->getNodeById('acpTemplateName');
253
254 /** @var SingleSelectionFormField $eventNameField */
255 $eventNameField = $formField->getDocument()->getNodeById('acp_' . $templateNameField->getSaveValue() . '_eventName');
256 }
257 else {
258 /** @var SingleSelectionFormField $templateNameField */
259 $templateNameField = $formField->getDocument()->getNodeById('frontendTemplateName');
260
261 /** @var SingleSelectionFormField $eventNameField */
262 $eventNameField = $formField->getDocument()->getNodeById($templateNameField->getSaveValue() . '_eventName');
263 }
264
265 $templateName = $templateNameField->getSaveValue();
266 $eventName = $eventNameField->getSaveValue();
267
268 $listenerList->getConditionBuilder()->add('templateName = ?', [$templateName]);
269 $listenerList->getConditionBuilder()->add('eventName = ?', [$eventName]);
270 $listenerList->getConditionBuilder()->add('environment = ?', [$formField->getSaveValue()]);
271
272 if ($listenerList->countObjects() > 0) {
273 $nameField->addValidationError(
274 new FormFieldValidationError(
275 'notUnique',
276 'wcf.acp.pip.templateListener.name.error.notUnique'
277 )
278 );
279 }
280 }
281 })),
282
283 SingleSelectionFormField::create('frontendTemplateName')
284 ->objectProperty('templatename')
285 ->label('wcf.acp.pip.templateListener.templateName')
286 ->description('wcf.acp.pip.templateListener.templateName.description')
287 ->required()
288 ->options(array_combine(array_keys($templateEvents), array_keys($templateEvents)))
289 ->filterable()
290 ->addDependency(
291 ValueFormFieldDependency::create('environment')
292 ->fieldId('environment')
293 ->values(['user'])
294 ),
295
296 SingleSelectionFormField::create('acpTemplateName')
297 ->objectProperty('templatename')
298 ->label('wcf.acp.pip.templateListener.templateName')
299 ->description('wcf.acp.pip.templateListener.templateName.description')
300 ->required()
301 ->options(array_combine(array_keys($acpTemplateEvents), array_keys($acpTemplateEvents)))
302 ->filterable()
303 ->addDependency(
304 ValueFormFieldDependency::create('environment')
305 ->fieldId('environment')
306 ->values(['admin'])
307 )
308 ]);
309
310 /** @var SingleSelectionFormField $frontendTemplateName */
311 $frontendTemplateName = $form->getNodeById('frontendTemplateName');
312 foreach ($templateEvents as $templateName => $events) {
313 $dataContainer->appendChild(
314 SingleSelectionFormField::create($templateName . '_eventName')
315 ->objectProperty('eventname')
316 ->label('wcf.acp.pip.templateListener.eventName')
317 ->description('wcf.acp.pip.templateListener.eventName.description')
318 ->required()
319 ->options(array_combine($events, $events))
320 ->addDependency(
321 ValueFormFieldDependency::create('templateName')
322 ->field($frontendTemplateName)
323 ->values([$templateName])
324 )
325 );
326 }
327
328 /** @var SingleSelectionFormField $acpTemplateName */
329 $acpTemplateName = $form->getNodeById('acpTemplateName');
330 foreach ($acpTemplateEvents as $templateName => $events) {
331 $dataContainer->appendChild(
332 SingleSelectionFormField::create('acp_' . $templateName . '_eventName')
333 ->objectProperty('eventname')
334 ->label('wcf.acp.pip.templateListener.eventName')
335 ->description('wcf.acp.pip.templateListener.eventName.description')
336 ->required()
337 ->options(array_combine($events, $events))
338 ->addDependency(
339 ValueFormFieldDependency::create('acpTemplateName')
340 ->field($acpTemplateName)
341 ->values([$templateName])
342 )
343 );
344 }
345
346 $dataContainer->appendChildren([
347 MultilineTextFormField::create('templateCode')
348 ->objectProperty('templatecode')
349 ->label('wcf.acp.pip.templateListener.templateCode')
350 ->description('wcf.acp.pip.templateListener.templateCode.description')
351 ->required(),
352
353 IntegerFormField::create('niceValue')
354 ->objectProperty('nice')
355 ->label('wcf.acp.pip.templateListener.niceValue')
356 ->description('wcf.acp.pip.templateListener.niceValue.description')
357 ->nullable()
358 ->minimum(-128)
359 ->maximum(127),
360
361 OptionFormField::create()
362 ->description('wcf.acp.pip.templateListener.options.description')
363 ->packageIDs(array_merge(
364 [$this->installation->getPackage()->packageID],
365 array_keys($this->installation->getPackage()->getAllRequiredPackages())
366 )),
367
368 UserGroupOptionFormField::create()
369 ->description('wcf.acp.pip.templateListener.permissions.description')
370 ->packageIDs(array_merge(
371 [$this->installation->getPackage()->packageID],
372 array_keys($this->installation->getPackage()->getAllRequiredPackages())
373 ))
374 ]);
375
376 // ensure proper normalization of template code
377 $form->getDataHandler()->add(new CustomFormFieldDataProcessor('templateCode', function(IFormDocument $document, array $parameters) {
378 $parameters['data']['templatecode'] = StringUtil::unifyNewlines(StringUtil::escapeCDATA($parameters['data']['templatecode']));
379
380 return $parameters;
381 }));
382 }
383
384 /**
385 * @inheritDoc
386 * @since 5.2
387 */
388 protected function fetchElementData(\DOMElement $element, $saveData) {
389 $data = [
390 'environment' => $element->getElementsByTagName('environment')->item(0)->nodeValue,
391 'eventName' => $element->getElementsByTagName('eventname')->item(0)->nodeValue,
392 'name' => $element->getAttribute('name'),
393 'packageID' => $this->installation->getPackage()->packageID,
394 'templateCode' => $element->getElementsByTagName('templatecode')->item(0)->nodeValue,
395 'templateName' => $element->getElementsByTagName('templatename')->item(0)->nodeValue
396 ];
397
398 $nice = $element->getElementsByTagName('nice')->item(0);
399 if ($nice !== null) {
400 $data['niceValue'] = $nice->nodeValue;
401 }
402 else if ($saveData) {
403 $data['niceValue'] = 0;
404 }
405
406 foreach (['options', 'permissions'] as $elementName) {
407 $optionalElement = $element->getElementsByTagName($elementName)->item(0);
408 if ($optionalElement !== null) {
409 $data[$elementName] = $optionalElement->nodeValue;
410 }
411 else if ($saveData) {
412 $data[$elementName] = '';
413 }
414 }
415
416 return $data;
417 }
418
419 /**
420 * @inheritDoc
421 * @since 5.2
422 */
423 public function getElementIdentifier(\DOMElement $element) {
424 return sha1(
425 $element->getElementsByTagName('templatename')->item(0)->nodeValue . '/' .
426 $element->getElementsByTagName('eventname')->item(0)->nodeValue . '/' .
427 $element->getElementsByTagName('environment')->item(0)->nodeValue . '/' .
428 $element->getAttribute('name')
429 );
430 }
431
432 /**
433 * @inheritDoc
434 * @since 5.2
435 */
436 public function setEntryData($identifier, IFormDocument $document) {
437 if ($this->defaultSetEntryData($identifier, $document)) {
438 $data = $this->getElementData($this->getElementByIdentifier($this->getProjectXml(), $identifier));
439
440 switch ($data['environment']) {
441 case 'admin':
442 /** @var SingleSelectionFormField $templateName */
443 $templateName = $document->getNodeById('acpTemplateName');
444
445 /** @var SingleSelectionFormField $eventName */
446 $eventName = $document->getNodeById('acp_' . $data['templateName'] . '_eventName');
447 break;
448
449 case 'user':
450 /** @var SingleSelectionFormField $templateName */
451 $templateName = $document->getNodeById('frontendTemplateName');
452
453 /** @var SingleSelectionFormField $eventName */
454 $eventName = $document->getNodeById($data['templateName'] . '_eventName');
455 break;
456
457 default:
458 throw new \LogicException("Unknown enviornment '{$data['environment']}'.");
459 }
460
461 $templateName->value($data['templateName']);
462 $eventName->value($data['eventName']);
463
464 return true;
465 }
466
467 return false;
468 }
469
470 /**
471 * @inheritDoc
472 * @since 5.2
473 */
474 protected function setEntryListKeys(IDevtoolsPipEntryList $entryList) {
475 $entryList->setKeys([
476 'name' => 'wcf.acp.pip.templateListener.name',
477 'templateName' => 'wcf.acp.pip.templateListener.templateName',
478 'eventName' => 'wcf.acp.pip.templateListener.eventName',
479 'environment' => 'wcf.acp.pip.templateListener.environment'
480 ]);
481 }
482
483 /**
484 * @inheritDoc
485 * @since 5.2
486 */
487 protected function prepareXmlElement(\DOMDocument $document, IFormDocument $form) {
488 $data = $form->getData()['data'];
489
490 $listener = $document->createElement($this->tagName);
491 $listener->setAttribute('name', $data['name']);
492
493 $this->appendElementChildren(
494 $listener,
495 [
496 'environment',
497 'templatename',
498 'eventname',
499 'templatecode' => [
500 'cdata' => true
501 ],
502 'nice' => 0,
503 'options' => '',
504 'permissions' => ''
505 ],
506 $form
507 );
508
509 return $listener;
510 }
511
512 /**
513 * @inheritDoc
514 * @since 5.2
515 */
516 protected function prepareDeleteXmlElement(\DOMElement $element) {
517 $templateListener = $element->ownerDocument->createElement($this->tagName);
518 $templateListener->setAttribute('name', $element->getAttribute('name'));
519
520 foreach (['environment', 'templatename', 'eventname'] as $childElement) {
521 $templateListener->appendChild($element->ownerDocument->createElement(
522 $childElement,
523 $element->getElementsByTagName($childElement)->item(0)->nodeValue
524 ));
525 }
526
527 return $templateListener;
528 }
529
530 /**
531 * @inheritDoc
532 * @since 5.2
533 */
534 protected function deleteObject(\DOMElement $element) {
535 $elements= [];
536 foreach (['environment', 'templatename', 'eventname'] as $childElement) {
537 $elements[$childElement] = $element->getElementsByTagName($childElement)->item(0)->nodeValue;
538 }
539
540 $this->handleDelete([[
541 'attributes' => ['name' => $element->getAttribute('name')],
542 'elements' => $elements
543 ]]);
544 }
545 }