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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
|
# --
# Copyright (C) 2001-2021 OTRS AG, https://otrs.com/
# Copyright (C) 2021 Znuny GmbH, https://znuny.org/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
# --
package Kernel::System::CalendarTemplateGenerator;
use strict;
use warnings;
use Kernel::Language;
use Kernel::System::VariableCheck qw(:all);
our @ObjectDependencies = (
'Kernel::Config',
'Kernel::System::Calendar',
'Kernel::System::Calendar::Appointment',
'Kernel::System::DateTime',
'Kernel::System::HTMLUtils',
'Kernel::System::Log',
'Kernel::System::User',
'Kernel::System::Main',
'Kernel::System::Group',
'Kernel::System::Valid',
);
=head1 NAME
Kernel::System::CalendarTemplateGenerator - signature lib
=head1 DESCRIPTION
All signature functions.
=head1 PUBLIC INTERFACE
=head2 new()
create an object. Do not use it directly, instead use:
use Kernel::System::ObjectManager;
local $Kernel::OM = Kernel::System::ObjectManager->new();
my $TemplateGeneratorObject = $Kernel::OM->Get('Kernel::System::TemplateGenerator');
=cut
sub new {
my ( $Type, %Param ) = @_;
# allocate new hash for object
my $Self = {};
bless( $Self, $Type );
$Self->{RichText} = $Kernel::OM->Get('Kernel::Config')->Get('Frontend::RichText');
return $Self;
}
=head2 NotificationEvent()
replace all OTRS smart tags in the notification body and subject
my %NotificationEvent = $CalendarTemplateGeneratorObject->NotificationEvent(
AppointmentID => 123,
Recipient => $UserDataHashRef, # Agent data get result
Notification => $NotificationDataHashRef,
UserID => 123,
);
=cut
sub NotificationEvent {
my ( $Self, %Param ) = @_;
# check needed stuff
for my $Needed (qw(Notification Recipient UserID)) {
if ( !$Param{$Needed} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Need $Needed!",
);
return;
}
}
if ( !IsHashRefWithData( $Param{Notification} ) ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Notification is invalid!",
);
return;
}
my %Notification = %{ $Param{Notification} };
# get system default language
my $DefaultLanguage = $Kernel::OM->Get('Kernel::Config')->Get('DefaultLanguage') || 'en';
my $Languages = [ $Param{Recipient}->{UserLanguage}, $DefaultLanguage, 'en' ];
my $Language;
LANGUAGE:
for my $Item ( @{$Languages} ) {
next LANGUAGE if !$Item;
next LANGUAGE if !$Notification{Message}->{$Item};
# set language
$Language = $Item;
last LANGUAGE;
}
# if no language, then take the first one available
if ( !$Language ) {
my @NotificationLanguages = sort keys %{ $Notification{Message} };
$Language = $NotificationLanguages[0];
}
# copy the correct language message attributes to a flat structure
for my $Attribute (qw(Subject Body ContentType)) {
$Notification{$Attribute} = $Notification{Message}->{$Language}->{$Attribute};
}
my $Start = '<';
my $End = '>';
if ( $Notification{ContentType} =~ m{text\/html} ) {
$Start = '<';
$End = '>';
}
# get html utils object
my $HTMLUtilsObject = $Kernel::OM->Get('Kernel::System::HTMLUtils');
# do text/plain to text/html convert
if ( $Self->{RichText} && $Notification{ContentType} =~ /text\/plain/i ) {
$Notification{ContentType} = 'text/html';
$Notification{Body} = $HTMLUtilsObject->ToHTML(
String => $Notification{Body},
);
}
# do text/html to text/plain convert
if ( !$Self->{RichText} && $Notification{ContentType} =~ /text\/html/i ) {
$Notification{ContentType} = 'text/plain';
$Notification{Body} = $HTMLUtilsObject->ToAscii(
String => $Notification{Body},
);
}
# get notify texts
for my $Text (qw(Subject Body)) {
if ( !$Notification{$Text} ) {
$Notification{$Text} = "No Notification $Text for $Param{Type} found!";
}
}
# replace place holder stuff
$Notification{Body} = $Self->_Replace(
RichText => $Self->{RichText},
Text => $Notification{Body},
Recipient => $Param{Recipient},
AppointmentID => $Param{AppointmentID},
CalendarID => $Param{CalendarID},
UserID => $Param{UserID},
Language => $Language,
);
$Notification{Subject} = $Self->_Replace(
RichText => 0,
Text => $Notification{Subject},
Recipient => $Param{Recipient},
AppointmentID => $Param{AppointmentID},
CalendarID => $Param{CalendarID},
UserID => $Param{UserID},
Language => $Language,
);
# add URLs and verify to be full HTML document
if ( $Self->{RichText} ) {
$Notification{Body} = $HTMLUtilsObject->LinkQuote(
String => $Notification{Body},
);
}
return %Notification;
}
=begin Internal:
=cut
sub _Replace {
my ( $Self, %Param ) = @_;
# check needed stuff
for my $Needed (qw(Text RichText UserID)) {
if ( !defined $Param{$Needed} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Need $Needed!"
);
return;
}
}
# check for mailto links
# since the subject and body of those mailto links are
# uri escaped we have to uri unescape them, replace
# possible placeholders and then re-uri escape them
$Param{Text} =~ s{
(href="mailto:[^\?]+\?)([^"]+")
}
{
my $MailToHref = $1;
my $MailToHrefContent = $2;
$MailToHrefContent =~ s{
((?:subject|body)=)(.+?)("|&)
}
{
my $SubjectOrBodyPrefix = $1;
my $SubjectOrBodyContent = $2;
my $SubjectOrBodySuffix = $3;
my $SubjectOrBodyContentUnescaped = URI::Escape::uri_unescape $SubjectOrBodyContent;
my $SubjectOrBodyContentReplaced = $Self->_Replace(
%Param,
Text => $SubjectOrBodyContentUnescaped,
RichText => 0,
);
my $SubjectOrBodyContentEscaped = URI::Escape::uri_escape_utf8 $SubjectOrBodyContentReplaced;
$SubjectOrBodyPrefix . $SubjectOrBodyContentEscaped . $SubjectOrBodySuffix;
}egx;
$MailToHref . $MailToHrefContent;
}egx;
my $Start = '<';
my $End = '>';
if ( $Param{RichText} ) {
$Start = '<';
$End = '>';
$Param{Text} =~ s/(\n|\r)//g;
}
# get needed objects
my $AppointmentObject = $Kernel::OM->Get('Kernel::System::Calendar::Appointment');
my $CalendarObject = $Kernel::OM->Get('Kernel::System::Calendar');
my %Calendar;
my %Appointment;
if ( $Param{AppointmentID} ) {
%Appointment = $AppointmentObject->AppointmentGet(
AppointmentID => $Param{AppointmentID},
);
%Calendar = $CalendarObject->CalendarGet(
CalendarID => $Appointment{CalendarID} || $Param{CalendarID},
);
}
elsif ( $Param{CalendarID} ) {
%Calendar = $CalendarObject->CalendarGet(
CalendarID => $Param{CalendarID},
);
}
# get config object
my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
# special replace from secret config options
my @SecretConfigOptions = qw(
DatabasePw
SearchUserPw
UserPw
SendmailModule::AuthPassword
AuthModule::Radius::Password
PGP::Key::Password
Customer::AuthModule::DB::CustomerPassword
Customer::AuthModule::Radius::Password
PublicFrontend::AuthPassword
);
# replace the secret config options before the normal config options
for my $SecretConfigOption (@SecretConfigOptions) {
my $Tag = $Start . 'OTRS_CONFIG_' . $SecretConfigOption . $End;
$Param{Text} =~ s{$Tag}{xxx}gx;
}
# replace config options
my $Tag = $Start . 'OTRS_CONFIG_';
$Param{Text} =~ s{$Tag(.+?)$End}{$ConfigObject->Get($1) // ''}egx;
# cleanup
$Param{Text} =~ s/$Tag.+?$End/-/gi;
my %Recipient = %{ $Param{Recipient} || {} };
# get user object
my $UserObject = $Kernel::OM->Get('Kernel::System::User');
if ( !%Recipient && $Param{RecipientID} ) {
%Recipient = $UserObject->GetUserData(
UserID => $Param{RecipientID},
NoOutOfOffice => 1,
);
}
# Instantiate a new language object with the given language.
my $LanguageObject = Kernel::Language->new(
UserLanguage => $Param{Language} || 'en',
);
# supported appointment fields
my %AppointmentTagsSkip = (
ParentID => 1,
RecurrenceType => 1,
RecurrenceFrequency => 1,
RecurrenceCount => 1,
RecurrenceInterval => 1,
RecurrenceUntil => 1,
RecurrenceID => 1,
RecurrenceExclude => 1,
NotificationCustom => 1,
NotificationTemplate => 1,
NotificationCustomUnitCount => 1,
NotificationCustomUnit => 1,
NotificationCustomUnitPointOfTime => 1,
NotificationCustomRelativePointOfTime => 1,
NotificationCustomRelativeUnit => 1,
NotificationCustomRelativeUnitCount => 1,
NotificationCustomDateTime => 1,
);
# ------------------------------------------------------------ #
# process appointment
# ------------------------------------------------------------ #
# replace config options
$Tag = $Start . 'OTRS_APPOINTMENT_';
# replace appointment tags
ATTRIBUTE:
for my $Attribute ( sort keys %Appointment ) {
next ATTRIBUTE if !$Attribute;
next ATTRIBUTE if $AppointmentTagsSkip{$Attribute};
# setup a new tag for the current attribute
my $MatchTag = $Tag . uc $Attribute;
# map NotificationTime attribute
if ( $Attribute eq 'NotificationDate' ) {
$MatchTag = $Tag . 'NOTIFICATIONTIME';
}
my $Replacement = '';
# process datetime strings (timestamps)
if (
$Attribute eq 'StartTime'
|| $Attribute eq 'EndTime'
|| $Attribute eq 'NotificationDate'
|| $Attribute eq 'CreateTime'
|| $Attribute eq 'ChangeTime'
)
{
if ( !$Appointment{$Attribute} ) {
$Replacement = '-';
}
else {
# Get the stored date time.
my $TagSystemTimeObject = $Kernel::OM->Create(
'Kernel::System::DateTime',
ObjectParams => {
String => $Appointment{$Attribute},
},
);
# Convert to recipients time zone.
$TagSystemTimeObject->ToTimeZone(
TimeZone => $Recipient{UserTimeZone}
// $TagSystemTimeObject->UserDefaultTimeZoneGet(),
);
my $DateFormat = 'DateFormat';
# Do not include time component for all-day appointments,
# but only for start and end dates.
if (
$Appointment{AllDay}
&& (
$Attribute eq 'StartTime'
|| $Attribute eq 'EndTime'
)
)
{
$DateFormat .= 'Short';
}
# Prepare dates and times.
$Replacement = $LanguageObject->FormatTimeString( $TagSystemTimeObject->ToString(), $DateFormat ) || '';
}
}
# process createby and changeby
elsif ( $Attribute eq 'CreateBy' || $Attribute eq 'ChangeBy' ) {
$Replacement = $UserObject->UserName(
UserID => $Appointment{$Attribute},
);
}
# process team ids
elsif ( $Attribute eq 'TeamID' ) {
next ATTRIBUTE if !IsArrayRefWithData( $Appointment{$Attribute} );
if (
!$Kernel::OM->Get('Kernel::System::Main')->Require( 'Kernel::System::Calendar::Team', Silent => 1 )
)
{
next ATTRIBUTE;
}
# instanciate a new team object
my $TeamObject = Kernel::System::Calendar::Team->new();
# get a list of available (readable) teams
my %TeamList = $TeamObject->TeamList(
Valid => 0,
UserID => $Self->{UserID},
);
next ATTRIBUTE if !IsHashRefWithData( \%TeamList );
my @TeamNames;
if ( IsHashRefWithData( \%TeamList ) ) {
TEAMKEY:
for my $TeamKey ( @{ $Appointment{$Attribute} } ) {
next TEAMKEY if !$TeamList{$TeamKey};
push @TeamNames, $TeamList{$TeamKey};
}
}
next ATTRIBUTE if !IsArrayRefWithData( \@TeamNames );
# replace team ids with a comma seperated list of team names
$Replacement = join ', ', @TeamNames;
}
# process resource ids
elsif ( $Attribute eq 'ResourceID' ) {
next ATTRIBUTE if !IsArrayRefWithData( $Appointment{$Attribute} );
my @UserNames;
USERID:
for my $UserID ( @{ $Appointment{$Attribute} } ) {
next USERID if !$UserID;
my $UserName = $UserObject->UserName(
UserID => $UserID,
);
next USERID if !$UserName;
push @UserNames, $UserName;
}
next ATTRIBUTE if !IsArrayRefWithData( \@UserNames );
# replace resource ids with a comma seperated list of team names
$Replacement = join ', ', @UserNames;
}
# process all day and recurring tags
elsif (
$Attribute eq 'AllDay'
|| $Attribute eq 'Recurring'
)
{
my $TranslatedString = $LanguageObject->Translate('No');
if ( $Appointment{$Attribute} ) {
$TranslatedString = $LanguageObject->Translate('Yes');
}
$Replacement = $TranslatedString;
}
# process description tag
elsif (
$Attribute eq 'Description'
&& $Self->{RichText}
&& $Appointment{$Attribute}
)
{
my $HTMLUtilsObject = $Kernel::OM->Get('Kernel::System::HTMLUtils');
$Replacement = $HTMLUtilsObject->ToHTML(
String => $Appointment{$Attribute},
);
}
# process all other single values
else {
if ( !$Appointment{$Attribute} ) {
$Replacement = '-';
}
else {
$Replacement = $Appointment{$Attribute};
}
}
$Replacement ||= '';
# replace the tags
$Param{Text} =~ s{$MatchTag$End}{$Replacement}egx;
}
# cleanup
$Param{Text} =~ s/$Tag.+?$End/-/gi;
# ------------------------------------------------------------ #
# process calendar
# ------------------------------------------------------------ #
# replace config options
$Tag = $Start . 'OTRS_CALENDAR_';
# replace appointment tags
ATTRIBUTE:
for my $Attribute ( sort keys %Calendar ) {
next ATTRIBUTE if !$Attribute;
# setup a new tag for the current attribute
my $MatchTag = $Tag . uc $Attribute;
my $Replacement = '';
# process datetime strings (timestamps)
if ( $Attribute eq 'CreateTime' || $Attribute eq 'ChangeTime' ) {
# Get the stored date time.
my $TagSystemTimeObject = $Kernel::OM->Create(
'Kernel::System::DateTime',
ObjectParams => {
String => $Calendar{$Attribute},
},
);
# Convert to recipients time zone.
$TagSystemTimeObject->ToTimeZone(
TimeZone => $Recipient{UserTimeZone}
// $TagSystemTimeObject->UserDefaultTimeZoneGet(),
);
# Prepare dates and times.
$Replacement = $LanguageObject->FormatTimeString( $TagSystemTimeObject->ToString(), 'DateFormat' ) || '';
}
# process createby and changeby
elsif ( $Attribute eq 'CreateBy' || $Attribute eq 'ChangeBy' ) {
$Replacement = $UserObject->UserName(
UserID => $Calendar{$Attribute},
);
}
# process group id
elsif ( $Attribute eq 'GroupID' ) {
$Replacement = $Kernel::OM->Get('Kernel::System::Group')->GroupLookup(
GroupID => $Calendar{$Attribute},
);
}
# process valid id
elsif ( $Attribute eq 'ValidID' ) {
$Replacement = $Kernel::OM->Get('Kernel::System::Valid')->ValidLookup(
ValidID => $Calendar{$Attribute},
);
$Replacement = $LanguageObject->Translate($Replacement);
}
# process all other single values
else {
if ( !$Calendar{$Attribute} ) {
$Replacement = '-';
}
else {
$Replacement = $Calendar{$Attribute};
}
}
# replace the tags
$Param{Text} =~ s{$MatchTag$End}{$Replacement}egx;
}
# cleanup
$Param{Text} =~ s/$Tag.+?$End/-/gi;
my $HashGlobalReplace = sub {
my ( $Tag, %H ) = @_;
# Generate one single matching string for all keys to save performance.
my $Keys = join '|', map {quotemeta} grep { defined $H{$_} } keys %H;
# Add all keys also as lowercase to be able to match case insensitive,
# e. g. <OTRS_CUSTOMER_From> and <OTRS_CUSTOMER_FROM>.
for my $Key ( sort keys %H ) {
$H{ lc $Key } = $H{$Key};
}
$Param{Text} =~ s/(?:$Tag)($Keys)$End/$H{ lc $1 }/ieg;
};
# get recipient data and replace it with <OTRS_...
$Tag = $Start . 'OTRS_';
# include more readable tag <OTRS_NOTIFICATION_RECIPIENT
my $RecipientTag = $Start . 'OTRS_NOTIFICATION_RECIPIENT_';
if (%Recipient) {
# HTML quoting of content
if ( $Param{RichText} ) {
ATTRIBUTE:
for my $Attribute ( sort keys %Recipient ) {
next ATTRIBUTE if !$Recipient{$Attribute};
$Recipient{$Attribute} = $Kernel::OM->Get('Kernel::System::HTMLUtils')->ToHTML(
String => $Recipient{$Attribute},
);
}
}
$HashGlobalReplace->( "$Tag|$RecipientTag", %Recipient );
}
# cleanup
$Param{Text} =~ s/$RecipientTag.+?$End/-/gi;
return $Param{Text};
}
1;
=end Internal:
=head1 TERMS AND CONDITIONS
This software is part of the OTRS project (L<https://otrs.org/>).
This software comes with ABSOLUTELY NO WARRANTY. For details, see
the enclosed file COPYING for license information (GPL). If you
did not receive this file, see L<https://www.gnu.org/licenses/gpl-3.0.txt>.
=cut
|