1# This Source Code Form is subject to the terms of the Mozilla Public
2# License, v. 2.0. If a copy of the MPL was not distributed with this
3# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4#
5# This Source Code Form is "Incompatible With Secondary Licenses", as
6# defined by the Mozilla Public License, v. 2.0.
7
8package Bugzilla::Update;
9
10use 5.10.1;
11use strict;
12use warnings;
13
14use Bugzilla::Constants;
15
16use constant TIME_INTERVAL => 86400; # Default is one day, in seconds.
17use constant TIMEOUT       => 5; # Number of seconds before timeout.
18
19# Look for new releases and notify logged in administrators about them.
20sub get_notifications {
21    return if !Bugzilla->feature('updates');
22    return if (Bugzilla->params->{'upgrade_notification'} eq 'disabled');
23
24    my $local_file = bz_locations()->{'datadir'} . '/' . LOCAL_FILE;
25    # Update the local XML file if this one doesn't exist or if
26    # the last modification time (stat[9]) is older than TIME_INTERVAL.
27    if (!-e $local_file || (time() - (stat($local_file))[9] > TIME_INTERVAL)) {
28        unlink $local_file; # Make sure the old copy is away.
29        return { 'error' => 'no_update' } if (-e $local_file);
30
31        my $error = _synchronize_data();
32        # If an error is returned, leave now.
33        return $error if $error;
34    }
35
36    # If we cannot access the local XML file, ignore it.
37    return { 'error' => 'no_access' } unless (-r $local_file);
38
39    my $twig = XML::Twig->new();
40    $twig->safe_parsefile($local_file);
41    # If the XML file is invalid, return.
42    return { 'error' => 'corrupted' } if $@;
43    my $root = $twig->root;
44
45    my @releases;
46    foreach my $branch ($root->children('branch')) {
47        my $release = {
48            'branch_ver' => $branch->{'att'}->{'id'},
49            'latest_ver' => $branch->{'att'}->{'vid'},
50            'status'     => $branch->{'att'}->{'status'},
51            'url'        => $branch->{'att'}->{'url'},
52            'date'       => $branch->{'att'}->{'date'}
53        };
54        push(@releases, $release);
55    }
56
57    # On which branch is the current installation running?
58    my @current_version =
59        (BUGZILLA_VERSION =~ m/^(\d+)\.(\d+)(?:(rc|\.)(\d+))?\+?$/);
60
61    my @release;
62    if (Bugzilla->params->{'upgrade_notification'} eq 'development_snapshot') {
63        @release = grep {$_->{'status'} eq 'development'} @releases;
64        # If there is no development snapshot available, then we are in the
65        # process of releasing a release candidate. That's the release we want.
66        unless (scalar(@release)) {
67            @release = grep {$_->{'status'} eq 'release-candidate'} @releases;
68        }
69    }
70    elsif (Bugzilla->params->{'upgrade_notification'} eq 'latest_stable_release') {
71        @release = grep {$_->{'status'} eq 'stable'} @releases;
72    }
73    elsif (Bugzilla->params->{'upgrade_notification'} eq 'stable_branch_release') {
74        # We want the latest stable version for the current branch.
75        # If we are running a development snapshot, we won't match anything.
76        my $branch_version = $current_version[0] . '.' . $current_version[1];
77
78        # We do a string comparison instead of a numerical one, because
79        # e.g. 2.2 == 2.20, but 2.2 ne 2.20 (and 2.2 is indeed much older).
80        @release = grep {$_->{'branch_ver'} eq $branch_version} @releases;
81
82        # If the branch is now closed, we should strongly suggest
83        # to upgrade to the latest stable release available.
84        if (scalar(@release) && $release[0]->{'status'} eq 'closed') {
85            @release = grep {$_->{'status'} eq 'stable'} @releases;
86            return {'data' => $release[0], 'deprecated' => $branch_version};
87        }
88    }
89    else {
90      # Unknown parameter.
91      return {'error' => 'unknown_parameter'};
92    }
93
94    # Return if no new release is available.
95    return unless scalar(@release);
96
97    # Only notify the administrator if the latest version available
98    # is newer than the current one.
99    my @new_version =
100        ($release[0]->{'latest_ver'} =~ m/^(\d+)\.(\d+)(?:(rc|\.)(\d+))?\+?$/);
101
102    # We convert release candidates 'rc' to integers (rc ? 0 : 1) in order
103    # to compare versions easily.
104    $current_version[2] = ($current_version[2] && $current_version[2] eq 'rc') ? 0 : 1;
105    $new_version[2] = ($new_version[2] && $new_version[2] eq 'rc') ? 0 : 1;
106
107    my $is_newer = _compare_versions(\@current_version, \@new_version);
108    return ($is_newer == 1) ? {'data' => $release[0]} : undef;
109}
110
111sub _synchronize_data {
112    my $local_file = bz_locations()->{'datadir'} . '/' . LOCAL_FILE;
113
114    my $ua = LWP::UserAgent->new();
115    $ua->timeout(TIMEOUT);
116    $ua->protocols_allowed(['http', 'https']);
117    # If the URL of the proxy is given, use it, else get this information
118    # from the environment variable.
119    my $proxy_url = Bugzilla->params->{'proxy_url'};
120    if ($proxy_url) {
121        $ua->proxy(['http', 'https'], $proxy_url);
122    }
123    else {
124        $ua->env_proxy;
125    }
126    my $response = eval { $ua->mirror(REMOTE_FILE, $local_file) };
127
128    # $ua->mirror() forces the modification time of the local XML file
129    # to match the modification time of the remote one.
130    # So we have to update it manually to reflect that a newer version
131    # of the file has effectively been requested. This will avoid
132    # any new download for the next TIME_INTERVAL.
133    if (-e $local_file) {
134        # Try to alter its last modification time.
135        my $can_alter = utime(undef, undef, $local_file);
136        # This error should never happen.
137        $can_alter || return { 'error' => 'no_update' };
138    }
139    elsif ($response && $response->is_error) {
140        # We have been unable to download the file.
141        return { 'error' => 'cannot_download', 'reason' => $response->status_line };
142    }
143    else {
144        return { 'error' => 'no_write', 'reason' => $@ };
145    }
146
147    # Everything went well.
148    return 0;
149}
150
151sub _compare_versions {
152    my ($old_ver, $new_ver) = @_;
153    while (scalar(@$old_ver) && scalar(@$new_ver)) {
154        my $old = shift(@$old_ver) || 0;
155        my $new = shift(@$new_ver) || 0;
156        return $new <=> $old if ($new <=> $old);
157    }
158    return scalar(@$new_ver) <=> scalar(@$old_ver);
159
160}
161
1621;
163
164__END__
165
166=head1 NAME
167
168Bugzilla::Update - Update routines for Bugzilla
169
170=head1 SYNOPSIS
171
172  use Bugzilla::Update;
173
174  # Get information about new releases
175  my $new_release = Bugzilla::Update::get_notifications();
176
177=head1 DESCRIPTION
178
179This module contains all required routines to notify you
180about new releases. It downloads an XML file from bugzilla.org
181and parses it, in order to display information based on your
182preferences. Absolutely no information about the Bugzilla version
183you are running is sent to bugzilla.org.
184
185=head1 FUNCTIONS
186
187=over
188
189=item C<get_notifications()>
190
191 Description: This function informs you about new releases, if any.
192
193 Params:      None.
194
195 Returns:     On success, a reference to a hash with data about
196              new releases, if any.
197              On failure, a reference to a hash with the reason
198              of the failure and the name of the unusable XML file.
199
200=back
201
202=cut
203