Back to index

lightning-sunbird  0.9+nobinonly
VersionCheck.php
Go to the documentation of this file.
00001 <?php
00002 /* -*- Mode: php; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00003 /* ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is the Extension Update Service.
00017  *
00018  * The Initial Developer of the Original Code is Vladimir Vukicevic.
00019  * Portions created by the Initial Developer are Copyright (C) 2004
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *  Vladimir Vukicevic <vladimir@pobox.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00040 $db_server = "";
00041 $db_user = "";
00042 $db_pass = "";
00043 $db_name = "";
00044 
00045 // error handling
00046 function bail ($errstr) {
00047     die("Error: " . $errstr);
00048 }
00049 
00050 
00051 // major.minor.release.build[+]
00052 // make sure this is a valid version
00053 function expandversion ($vstr) {
00054     $v = explode('.', $vstr);
00055 
00056     if ($vstr == '' || count($v) == 0 || count($v) > 4) {
00057         bail ('Bogus version.');
00058     }
00059 
00060     $vlen = count($v);
00061     $ret = array();
00062     $hasplus = 0;
00063 
00064     for ($i = 0; $i < 4; $i++) {
00065         if ($i > $vlen-1) {
00066             // this version chunk was not specified; give 0
00067             $ret[] = 0;
00068         } else {
00069             $s = $v[$i];
00070             if ($i == 3) {
00071                 // need to check for +
00072                 $slen = strlen($s);
00073                 if ($s{$slen-1} == '+') {
00074                     $s = substr($s, 0, $slen-1);
00075                     $hasplus = 1;
00076                 }
00077             }
00078 
00079             $ret[] = intval($s);
00080         }
00081     }
00082 
00083     $ret[] = $hasplus;
00084 
00085     return $ret;
00086 }
00087 
00088 function vercmp ($a, $b) {
00089     if ($a == $b)
00090         return 0;
00091 
00092     $va = expandversion($a);
00093     $vb = expandversion($b);
00094 
00095     for ($i = 0; $i < 5; $i++)
00096         if ($va[$i] != $vb[$i])
00097             return ($vb[$i] - $va[$i]);
00098 
00099     return 0;
00100 }
00101 
00102 
00103 //
00104 // These are passed in the GET string
00105 //
00106 
00107 if (!array_key_exists('reqVersion', $_GET))
00108     bail ("Invalid request.");
00109 
00110 $reqVersion = $_GET['reqVersion'];
00111 
00112 if ($reqVersion == 1) {
00113 
00114     if (!array_key_exists('id', $_GET) ||
00115         !array_key_exists('version', $_GET) ||
00116         !array_key_exists('maxAppVersion', $_GET) ||
00117         !array_key_exists('appID', $_GET) ||
00118         !array_key_exists('appVersion', $_GET))
00119         bail ("Invalid request.");
00120 
00121     $reqItemGuid = $_GET['id'];
00122     $reqItemVersion = $_GET['version'];
00123     $reqItemMaxAppVersion = $_GET['maxAppVersion'];
00124     $reqTargetAppGuid = $_GET['appID'];
00125     $reqTargetAppVersion = $_GET['appVersion'];
00126 } else {
00127     // bail
00128     bail ("Bad request version received");
00129 }
00130 
00131 // check args
00132 if (empty($reqItemGuid) || empty($reqItemVersion) || empty($reqTargetAppGuid)) {
00133     bail ("Invalid request.");
00134 }
00135 
00136 mysql_connect($db_server, $db_user, $db_pass)
00137     || bail ("Failed to connect to database.");
00138 
00139 mysql_select_db ($db_name)
00140     || bail ("Failed to select database.");
00141 
00142 // We need to fetch two things for the database:
00143 // 1) The current extension version's info, for a possibly updated max version
00144 // 2) The latest version available, if different from the above.
00145 //
00146 // We know:
00147 //  - $reqItemGuid
00148 //  - $reqItemVersion
00149 //  - $reqTargetAppGuid
00150 //  - $reqTargetAppVersion
00151 //
00152 // We need to get:
00153 //  - extension GUID
00154 //  - extension version
00155 //  - extension xpi link
00156 //  - app ID
00157 //  - app min version
00158 //  - app max version
00159 
00160 $query = "SELECT t_main.guid AS extguid,
00161                  t_version.version AS extversion,
00162                  t_version.uri AS exturi,
00163                  t_version.minappver AS appminver,
00164                  t_version.maxappver AS appmaxver,
00165                  t_applications.guid AS appguid
00166           FROM t_main, t_version, t_applications
00167           WHERE t_main.guid = '" . mysql_real_escape_string($reqItemGuid) . "' AND
00168                 t_main.id = t_version.id AND
00169                 t_version.appid = t_applications.appid AND
00170                 t_applications.guid = '" . mysql_real_escape_string($reqTargetAppGuid) . "'";
00171 
00172 $result = mysql_query ($query);
00173 
00174 if (!$result) {
00175     bail ('Query error: ' . mysql_error());
00176 }
00177 
00178 // info for this version
00179 $thisVersionData = '';
00180 // info for highest version
00181 $highestVersion = '';
00182 $highestVersionData = '';
00183 
00184 while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
00185     // is this row for the current version?
00186     if ($line['extversion'] == $reqItemVersion) {
00187         // if so
00188         $thisVersionData = $line;
00189     } else if (vercmp ($reqItemVersion, $line['extversion']) > 0) {
00190         // did we already see an update with a higher version than this?
00191         if ($highestVersion != '' && vercmp ($highestVersion, $line['extversion']) < 0)
00192             continue;
00193 
00194         // does this update support my current app version?
00195         if (vercmp($line['appmaxver'], $reqTargetAppVersion) > 0 ||
00196             vercmp($reqTargetAppVersion, $line['appminver']) > 0)
00197             continue;
00198 
00199         $highestVersion = $line['extversion'];
00200         $highestVersionData = $line;
00201     }
00202 }
00203 
00204 mysql_free_result ($result);
00205 
00206 //
00207 // Now to spit out the RDF.  We hand-generate because the data is pretty simple.
00208 //
00209 
00210 print "<?xml version=\"1.0\"?>\n";
00211 print "<RDF:RDF xmlns:RDF=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:em=\"http://www.mozilla.org/2004/em-rdf#\">\n\n";
00212 
00213 print "<RDF:Description about=\"urn:mozilla:extension:{$reqItemGuid}\">\n";
00214 
00215 // output list of updates (just highest and this)
00216 print " <em:updates><RDF:Seq>\n";
00217 if (!empty($thisVersionData))
00218     print "  <RDF:li resource=\"urn:mozilla:extension:{$reqItemGuid}:{$thisVersionData['extversion']}\"/>\n";
00219 if (!empty($highestVersionData))
00220     print "  <RDF:li resource=\"urn:mozilla:extension:{$reqItemGuid}:{$highestVersionData['extversion']}\"/>\n";
00221 print " </RDF:Seq></em:updates>\n";
00222 
00223 // output compat bits for firefox 0.9
00224 if (!empty($highestVersionData)) {
00225     print " <em:version>{$highestVersionData['extversion']}</em:version>\n";
00226     print " <em:updateLink>{$highestVersionData['exturi']}</em:updateLink>\n";
00227 }
00228 
00229 print "</RDF:Description>\n\n";
00230 
00231 function print_update ($data) {
00232     print "<RDF:Description about=\"urn:mozilla:extension:{$reqItemGuid}:{$data['extversion']}\">\n";
00233     print " <em:version>{$data['extversion']}</em:version>\n";
00234     print " <em:targetApplication>\n";
00235     print "  <RDF:Description>\n";
00236     print "   <em:id>{$data['appguid']}</em:id>\n";
00237     print "   <em:minVersion>{$data['appminver']}</em:minVersion>\n";
00238     print "   <em:maxVersion>{$data['appmaxver']}</em:maxVersion>\n";
00239     print "   <em:updateLink>{$data['exturi']}</em:updateLink>\n";
00240     print "  </RDF:Description>\n";
00241     print " </em:targetApplication>\n";
00242     print "</RDF:Description>\n";
00243 }
00244 
00245 if (!empty($thisVersionData))
00246     print_update ($thisVersionData);
00247 if (!empty($highestVersionData))
00248     print_update ($highestVersionData);
00249 
00250 print "</RDF:RDF>\n";
00251 
00252 ?>
00253