1 | <?php |
---|
2 | require_once('db.php'); |
---|
3 | |
---|
4 | // modify the header here so the client gets our version |
---|
5 | header('X-KBVersion: '.KB_VERSION); |
---|
6 | |
---|
7 | if (!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 |
---|
16 | preg_match("^\(VER (.*?)\)^", $_SERVER['HTTP_USER_AGENT'], $match); |
---|
17 | $version = explode('.', $match[1]); |
---|
18 | if ($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 |
---|
25 | if (!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 |
---|
36 | preg_match_all("^!(.*?)\|(.*?)-^", &$data, $matches); |
---|
37 | unset($data); |
---|
38 | $results = count($matches[1]); |
---|
39 | if ($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 |
---|
48 | for ($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"); |
---|
56 | while ($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 |
---|
63 | foreach ($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'; |
---|
80 | foreach ($data_array as $name => $id) |
---|
81 | { |
---|
82 | $content_file .= '!'.$name.'|'.$id.'-'; |
---|
83 | } |
---|
84 | |
---|
85 | // return the compressed data back to the client |
---|
86 | echo gzdeflate($content_file); |
---|
87 | ?> |
---|