root/mods/Sync_Server/sync_server.php @ 46

Revision 46, 2.4 KB (checked in by andrewgunn, 16 years ago)

Added the Sync_Server mod, which is a stand-alone server to handle the pilot synchronization. I've also updated globals.php to reflect the new address.

Line 
1<?php
2require_once('db.php');
3
4// modify the header here so the client gets our version
5header('X-KBVersion: '.KB_VERSION);
6
7if (!strstr($_SERVER['HTTP_USER_AGENT'], 'EVE-KB SYNC'))
8{
9    echo 'Current version is '.KB_VERSION.'.<br>';
10    echo 'You should not call this file directly unless you\'re told to<br>';
11    return;
12}
13
14// this is an example for version checking with client ban
15// in case we change the protocol/data format
16preg_match("^\(VER (.*?)\)^", $_SERVER['HTTP_USER_AGENT'], $match);
17$version = explode('.', $match[1]);
18if ($version[0] <= 1 && $version[1] < 1)
19{
20    echo "Your Killboard is too old, please upgrade to 1.2.0 or newer<br>\n";
21    return;
22}
23
24// check if we got a file named 'data' supplied with the request
25if (!file_exists($_FILES['data']['tmp_name']))
26{
27    var_dump($_FILES);
28    echo "malformed request, expecting data-file<br>\n";
29    return;
30}
31
32// we got our file, process
33$data = gzinflate(file_get_contents($_FILES['data']['tmp_name']));
34
35// get all names we'll find
36preg_match_all("^!(.*?)\|(.*?)-^", &$data, $matches);
37unset($data);
38$results = count($matches[1]);
39if ($results == 0)
40{
41    // if we got no ids from the client we won't send him ours
42    // bad idea for new installations
43    //echo "malformed request<br>\n";
44    //return;
45}
46
47// construct an array with name as key and id as value
48for ($i = 0; $i<$results; $i++)
49{
50    $s_data[$matches[1][$i]] = $matches[2][$i];
51}
52
53// now get our list from the database
54$qry = new DBQuery();
55$qry->execute("select plt_name, plt_externalid from kb3_pilots where plt_externalid != 0");
56while ($data = $qry->getRow())
57{
58    $data_array[$data['plt_name']] = $data['plt_externalid'];
59}
60$update = new DBQuery();
61
62// compare the entries supplied with our own
63foreach ($s_data as $name => $id)
64{
65    if (!$data_array[$name])
66    {
67        // we dont got that one in our database, update
68        // TODO: we don't care about missing pilots yet
69        $update->execute("update kb3_pilots set plt_externalid='".addslashes($id)."' where plt_name='".addslashes($name)."' limit 1");
70    }
71    else
72    {
73        // unset to save comparison time, we know the client has it
74        unset($data_array[$name]);
75    }
76}
77
78// $data_array now contains only unknown ids to the client
79$content_file = 'DATA_START';
80foreach ($data_array as $name => $id)
81{
82    $content_file .= '!'.$name.'|'.$id.'-';
83}
84
85// return the compressed data back to the client
86echo gzdeflate($content_file);
87?>
Note: See TracBrowser for help on using the browser.