Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
831 florian 1
<?php
2
 
3
////////////////////////////////////////////////////////////////////////////////
4
//                                                                            //
5
//   Copyright (C) 2006  Phorum Development Team                              //
6
//   http://www.phorum.org                                                    //
7
//                                                                            //
8
//   This program is free software. You can redistribute it and/or modify     //
9
//   it under the terms of either the current Phorum License (viewable at     //
10
//   phorum.org) or the Phorum License that was distributed with this file    //
11
//                                                                            //
12
//   This program is distributed in the hope that it will be useful,          //
13
//   but WITHOUT ANY WARRANTY, without even the implied warranty of           //
14
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     //
15
//                                                                            //
16
//   You should have received a copy of the Phorum License                    //
17
//   along with this program.                                                 //
18
////////////////////////////////////////////////////////////////////////////////
19
 
20
if(!defined("PHORUM")) return;
21
 
22
/**
23
 * Parses the Phorum version number.
24
 * @param version - version number to parse
25
 * @return An array containing two elements. The first one holds the release
26
 *         type, which can be "unknown" (parse failed), "snapshot", "stable"
27
 *         or "development". The version can either be NULL or an array
28
 *         containing a splitted up version number (only for "stable"
29
 *         and "development").
30
 */
31
function phorum_parse_version($version)
32
{
33
    if (preg_match('/^\w+-(svn|cvs)-\d+$/', $version)) {
34
        $release = 'snapshot';
35
        $parsed_version = array(0,0,0,0);
36
    } elseif (preg_match('/^(\d+)\.(\d+).(\d+)([a-z])?$/', $version, $m)) {
37
        $release = 'stable';
38
        $parsed_version = array_slice($m, 1);
39
    } elseif (preg_match('/^(\d+)\.(\d+).(\d+)(-alpha|-beta|-RC\d+)?$/', $version, $m)) {
40
        $release = 'development';
41
        $parsed_version = array_slice($m, 1);
42
    } else {
43
        $release = 'unknown';
44
        $parsed_version = NULL;
45
    }
46
 
47
    return array($release, $parsed_version);
48
}
49
 
50
/**
51
 * Compares two version numbers as returned by phorum_parse_version()
52
 * and tells which of those two is larger.
53
 * @param version1 - The first version number
54
 * @param version2 - The second version number
55
 * @return 1 if version1 is higher than version2, 0 if equal, -1 if lower
56
 */
57
 function phorum_compare_version($version1, $version2)
58
{
59
    // Compare segment by segment which version is higher.
60
    // Segments 1, 2 and 3 are always numbers. Segment 4 can be
61
    // a post-release version letter (a, b, c, etc.) or a
62
    // development release marker (-alpha and -beta).
63
    for ($s=0; $s<=3; $s++) {
64
        if ($s != 3) {
65
            if ($version1[$s] > $version2[$s]) return 1;
66
            if ($version1[$s] < $version2[$s]) return -1;
67
        } else {
68
            // Build a numerical representation for segment 4.
69
            // * 0 if no segment 4 is set
70
            // * 1 for alpha
71
            // * 2 for beta
72
            // * ord for single char version additions (a = 97)
73
            $v1 = 0; $v2 = 0;
74
            if (isset($version1[$s])) {
75
                if ($version1[$s] == '-alpha') $v1 = 1;
76
                elseif ($version1[$s] == '-beta') $v1 = 2;
77
                elseif (strlen($version1[$s]) == 1) $v1 = ord($version1[$s]);
78
            }
79
            if (isset($version2[$s])) {
80
                if ($version2[$s] == '-alpha') $v2 = 1;
81
                elseif ($version2[$s] == '-beta') $v2 = 2;
82
                elseif (strlen($version2[$s]) == 1) $v2 = ord($version2[$s]);
83
            }
84
            // Same version number with a development suffix is
85
            // considered lower than without any suffix.
86
            if ($v1 == 0 && ($v2 == 1 || $v2 == 2)) return 1;
87
            if (($v1 == 1 || $v1 == 2) && $v2 == 0) return -1;
88
 
89
            if ($v1 > $v2) return 1;
90
            if ($v1 < $v2) return -1;
91
        }
92
    }
93
 
94
    // No difference was found.
95
    return 0;
96
}
97
 
98
/**
99
 * Retrieves the available software versions from the Phorum website.
100
 * The format of the data returned from the server is two lines. The first
101
 * line is for the stable version and the second for the development version.
102
 * Each line contains pipe separated values, with the following fields in it:
103
 * <version>|<release date>|<downloadloc 1>|<downloadloc 2>|...|<downloadloc n>
104
 * @return releases - An array of releases for release types "stable" and "development".
105
 */
106
function phorum_available_releases()
107
{
108
    $releases = array();
109
    $fp = @fopen("http://phorum.org/version.php", "r");
110
    if ($fp) {
111
        foreach (array("stable", "development") as $release) {
112
            $line = fgets($fp, 1024);
113
            if (strstr($line, '|')) {
114
                $fields = explode('|', $line);
115
                if (count($fields) >= 3) {
116
                    // See if we can parse the version and if the parsed
117
                    // release type matches the release type we're expecting.
118
                    $parsed_version = phorum_parse_version($fields[0]);
119
                    if ($parsed_version[0] == $release) {
120
                        $releases[$release] = array(
121
                            "version"   => array_shift($fields),
122
                            "pversion"  => $parsed_version[1],
123
                            "date"      => array_shift($fields),
124
                            "locations" => $fields
125
                        );
126
                    }
127
                }
128
            }
129
        }
130
        fclose($fp);
131
    }
132
 
133
    return $releases;
134
}
135
 
136
/**
137
 * Finds out if there are any upgrades available for a version of Phorum.
138
 * @param version - the version to check for (default is the running version)
139
 * @return releases - An array of available releases with the
140
 *         "upgrade" field set in case the release would be an
141
 *         upgrade for the currently running Phorum software.
142
 */
143
function phorum_find_upgrades($version = PHORUM)
144
{
145
    // Parse the running version of phorum.
146
    list ($running_release, $running_version) = phorum_parse_version($version);
147
 
148
    // Retrieve the available releases.
149
    $releases = phorum_available_releases();
150
 
151
    // Check if an upgrade is available for the running release.
152
    // If we're running a stable version, we only compare to the current
153
    // stable release. If we're running a development version, we compare both
154
    // stable and development.
155
    if (isset($releases["stable"])) {
156
        $avail_version = $releases["stable"]["pversion"];
157
        if (phorum_compare_version($running_version, $avail_version) == -1) {
158
            $releases["stable"]["upgrade"] = true;
159
        } else {
160
            $releases["stable"]["upgrade"] = false;
161
        }
162
    }
163
    if (($running_release == 'development' || $running_release == 'snapshot') && isset($releases["development"])) {
164
        $avail_version = $releases["development"]["pversion"];
165
        if (phorum_compare_version($running_version, $avail_version) == -1) {
166
            $releases["development"]["upgrade"] = true;
167
        } else {
168
            $releases["development"]["upgrade"] = false;
169
        }
170
    }
171
 
172
    return $releases;
173
}
174
 
175
?>