1# -- 2# Copyright (C) 2001-2020 OTRS AG, https://otrs.com/ 3# -- 4# This software comes with ABSOLUTELY NO WARRANTY. For details, see 5# the enclosed file COPYING for license information (GPL). If you 6# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt. 7# -- 8 9package Kernel::Output::HTML::FilterText::AutoLink; 10 11use strict; 12use warnings; 13 14our $ObjectManagerDisabled = 1; 15 16sub new { 17 my ( $Type, %Param ) = @_; 18 19 # allocate new hash for object 20 my $Self = {}; 21 bless( $Self, $Type ); 22 23 return $Self; 24} 25 26sub Pre { 27 my ( $Self, %Param ) = @_; 28 29 # check needed stuff 30 if ( !defined $Param{Data} ) { 31 $Kernel::OM->Get('Kernel::System::Log')->Log( 32 Priority => 'error', 33 Message => 'Need Data!' 34 ); 35 $Kernel::OM->Get('Kernel::Output::HTML::Layout')->FatalDie(); 36 } 37 38 return $Param{Data}; 39} 40 41sub Post { 42 my ( $Self, %Param ) = @_; 43 44 # get layout object 45 my $LayoutObject = $Kernel::OM->Get('Kernel::Output::HTML::Layout'); 46 47 # check needed stuff 48 if ( !defined $Param{Data} ) { 49 $Kernel::OM->Get('Kernel::System::Log')->Log( 50 Priority => 'error', 51 Message => 'Need Data!' 52 ); 53 $LayoutObject->FatalDie(); 54 } 55 56 # get config object 57 my $ConfigObject = $Kernel::OM->Get('Kernel::Config'); 58 59 # check whether auto article links should be used 60 return $Param{Data} 61 if !$ConfigObject->Get('Frontend::Output::OutputFilterTextAutoLink'); 62 63 # find words to replace 64 my %Config = %{ $ConfigObject->Get("Frontend::Output::OutputFilterTextAutoLink") }; 65 66 LINK: 67 for my $Link ( values %Config ) { 68 69 next LINK if !$Link->{RegExp}; 70 71 # iterage through regular expressions and create a hash with found keywords 72 my %Keywords = (); 73 for my $RegExp ( @{ $Link->{RegExp} } ) { 74 my @Count = $RegExp =~ m{\(}gx; 75 my $Elements = scalar @Count; 76 77 if ( my @MatchData = ${ $Param{Data} } =~ m{((?<!\w)$RegExp)}gxi ) { 78 my $Counter = 0; 79 KEYWORD: 80 while ( $MatchData[$Counter] ) { 81 82 my $HoleMatchString = $MatchData[$Counter]; 83 $HoleMatchString =~ s/^\s+|\s+$//g; 84 if ( $Keywords{$HoleMatchString} ) { 85 $Counter += $Elements + 1; 86 next KEYWORD; 87 } 88 89 for ( 1 .. $Elements ) { 90 $Keywords{$HoleMatchString}{$_} = $MatchData[ $Counter + $_ ]; 91 } 92 $Counter += $Elements + 1; 93 } 94 } 95 } 96 97 # iterate trough keywords and replace them with URLs from the configuration 98 for my $Keyword ( sort keys %Keywords ) { 99 my %KW = %{ $Keywords{$Keyword} }; 100 my $URLLink = ''; 101 102 DATA: 103 for my $URLRef ( values %{$Link} ) { 104 next DATA if !$URLRef || ref($URLRef) ne 'HASH'; 105 106 # check URL configuration sanity 107 next DATA if !$URLRef->{URL} || !$URLRef->{Image} || !$URLRef->{Target}; 108 109 my $KeywordQuote = $LayoutObject->Ascii2Html( Text => $Keyword ); 110 my $URL = $URLRef->{URL}; 111 112 # replace the whole keyword 113 my $KeywordLinkEncode = $LayoutObject->LinkEncode($Keyword); 114 $URL =~ s/<MATCH>/$KeywordLinkEncode/g; 115 116 # replace the keyword components 117 for ( sort keys %KW ) { 118 $KeywordLinkEncode = $LayoutObject->LinkEncode( $KW{$_} ); 119 $URL =~ s/<MATCH$_>/$KeywordLinkEncode/g; 120 } 121 122 # find out if it is an internal image or an external image 123 my $Image = $URLRef->{Image}; 124 if ( $Image !~ m{^ http }smx ) { 125 $Image = $LayoutObject->{Images} . $URLRef->{Image}; 126 } 127 128 # create the url string 129 $URL = "<a href=\"$URL\" target=\"$URLRef->{Target}\">"; 130 $URL .= "<img border=\"0\" src=\"$Image\" "; 131 $URL .= " alt=\"$URLRef->{Description}: $KeywordQuote\""; 132 $URL .= " title=\"$URLRef->{Description}: $KeywordQuote\"/></a>"; 133 $URLLink .= ' ' if ($URLLink); 134 $URLLink .= $URL; 135 } 136 137 # Replace the built URLs in the original text 138 if ($URLLink) { 139 ${ $Param{Data} } =~ s/($Keyword)/$1 $URLLink/gi; 140 } 141 } 142 } 143 144 return $Param{Data}; 145} 146 1471; 148