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::Dashboard::RSS;
10
11use strict;
12use warnings;
13
14use LWP::UserAgent;
15use XML::FeedPP;
16
17our $ObjectManagerDisabled = 1;
18
19sub new {
20    my ( $Type, %Param ) = @_;
21
22    # allocate new hash for object
23    my $Self = {%Param};
24    bless( $Self, $Type );
25
26    # get needed parameters
27    for my $Needed (qw(Config Name UserID)) {
28        die "Got no $Needed!" if ( !$Self->{$Needed} );
29    }
30
31    return $Self;
32}
33
34sub Preferences {
35    my ( $Self, %Param ) = @_;
36
37    return;
38}
39
40sub Config {
41    my ( $Self, %Param ) = @_;
42
43    return (
44        %{ $Self->{Config} },
45        CacheKey => 'RSS'
46            . $Self->{Config}->{URL} . '-'
47            . $Kernel::OM->Get('Kernel::Output::HTML::Layout')->{UserLanguage},
48    );
49}
50
51sub Run {
52    my ( $Self, %Param ) = @_;
53
54    # Default URL
55    my $FeedURL = $Self->{Config}->{URL};
56
57    # get layout object
58    my $LayoutObject = $Kernel::OM->Get('Kernel::Output::HTML::Layout');
59
60    my $Language = $LayoutObject->{UserLanguage};
61
62    # Check if URL for UserLanguage is available
63    if ( $Self->{Config}->{"URL_$Language"} ) {
64        $FeedURL = $Self->{Config}->{"URL_$Language"};
65    }
66    else {
67
68        # Check for main language for languages like es_MX (es in this case)
69        ($Language) = split /_/, $Language;
70        if ( $Self->{Config}->{"URL_$Language"} ) {
71            $FeedURL = $Self->{Config}->{"URL_$Language"};
72        }
73    }
74
75    # Configure a local instance of LWP::UserAgent for passing to XML::FeedPP.
76    my $UserAgent = LWP::UserAgent->new();
77
78    my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
79
80    # In some scenarios like transparent HTTPS proxies, it can be necessary to turn off
81    #   SSL certificate validation.
82    if ( $ConfigObject->Get('WebUserAgent::DisableSSLVerification') ) {
83        $UserAgent->ssl_opts(
84            verify_hostname => 0,
85        );
86    }
87
88    # Set proxy settings if configured, and make sure to allow all supported protocols
89    #   (please see bug#12512 for more information).
90    my $Proxy = $ConfigObject->Get('WebUserAgent::Proxy');
91    if ($Proxy) {
92        $UserAgent->proxy( [ 'http', 'https', 'ftp' ], $Proxy );
93    }
94
95    # get content
96    my $Feed;
97
98    TRY:
99    for ( 1 .. 3 ) {
100        $Feed = eval {
101            XML::FeedPP->new(
102                $FeedURL,
103                'xml_deref'     => 1,
104                'utf8_flag'     => 1,
105                'lwp_useragent' => $UserAgent,
106            );
107        };
108        last TRY if $Feed;
109    }
110
111    if ( !$Feed ) {
112        return $LayoutObject->{LanguageObject}->Translate( 'Can\'t connect to %s!', $FeedURL );
113    }
114
115    my $Count = 0;
116    ITEM:
117    for my $Item ( $Feed->get_item() ) {
118        $Count++;
119        last ITEM if $Count > $Self->{Config}->{Limit};
120        my $Time = $Item->pubDate();
121        my $Ago;
122        if ($Time) {
123            my $SystemDateTimeObject = $Kernel::OM->Create(
124                'Kernel::System::DateTime',
125                ObjectParams => {
126                    String => $Time,
127                },
128            );
129
130            my $CurSystemDateTimeObject = $Kernel::OM->Create('Kernel::System::DateTime');
131
132            $Ago = $CurSystemDateTimeObject->ToEpoch() - $SystemDateTimeObject->ToEpoch();
133            $Ago = $LayoutObject->CustomerAge(
134                Age   => $Ago,
135                Space => ' ',
136            );
137        }
138
139        $LayoutObject->Block(
140            Name => 'ContentSmallRSSOverviewRow',
141            Data => {
142                Title => $Item->title(),
143                Link  => $Item->link(),
144            },
145        );
146        if ($Ago) {
147            $LayoutObject->Block(
148                Name => 'ContentSmallRSSTimeStamp',
149                Data => {
150                    Ago   => $Ago,
151                    Title => $Item->title(),
152                    Link  => $Item->link(),
153                },
154            );
155        }
156        else {
157            $LayoutObject->Block(
158                Name => 'ContentSmallRSS',
159                Data => {
160                    Ago   => $Ago,
161                    Title => $Item->title(),
162                    Link  => $Item->link(),
163                },
164            );
165        }
166    }
167    my $Content = $LayoutObject->Output(
168        TemplateFile => 'AgentDashboardRSSOverview',
169        Data         => {
170            %{ $Self->{Config} },
171        },
172    );
173
174    return $Content;
175}
176
1771;
178