root/dev/common/admin_sync.php @ 176

Revision 176, 5.7 KB (checked in by exi, 14 years ago)

Changes to sync_server.php to reduce the memory load during script execution.

Line 
1<?php
2require_once("class.page.php");
3require_once("class.contract.php");
4require_once("admin_menu.php");
5require_once('class.http.php');
6
7$page = new Page();
8$page->setAdmin();
9$page->setTitle("Administration - Synchronization");
10
11if ($_REQUEST['do'] == 'sync')
12{
13    $http = new http_request(KB_SYNCURL);
14    $fp = $http->connect();
15    if (!$fp)
16    {
17        $html .= "Could not connect to Server:<br/>\n";
18        $html .= $http->getError();
19
20        $page->addContext($menubox->generate());
21        $page->setContent($html);
22        $page->generate();
23        return;
24    }
25
26    $content_file = 'DATA_START';
27    $qry = new DBQuery();
28    $qry->execute("select plt_name, plt_externalid from kb3_pilots where plt_externalid != 0");
29    while ($data = $qry->getRow())
30    {
31        $content_file .= '!'.$data['plt_name'].'|'.$data['plt_externalid'].'-';
32    }
33
34    $content_file .= 'ITEMS_START';
35    $qry->execute("select itm_name, itm_externalid, itm_value from kb3_items where itm_value != 0");
36    while ($data = $qry->getRow())
37    {
38        $content_file .= '§'.$data['itm_name'].'|'.$data['itm_externalid'].'|'.$data['itm_value'].'-';
39    }
40
41    $content_file = gzdeflate($content_file);
42
43    $http->set_postdata('data', $content_file);
44    $http->set_useragent("EVE-KB SYNC (VER ".KB_VERSION.")");
45    $http->set_header('X-KBHost: '.base64_encode(KB_HOST));
46    $http->setSockettimeout(30);
47    $file = $http->get_content();
48    $header = $http->get_header();
49
50    preg_match("/X-KBVersion: (.*)/", $header, $match);
51    $version = explode('.', trim($match[1]));
52    $recv = $http->get_recv();
53    $sended = $http->get_sent();
54
55    // the response ($file) contains ids new to us
56    $data = @gzinflate($file);
57    if ($data == false)
58    {
59        $html .= "getting compressed data failed, server response was:<br><pre>\n";
60        $html .= $file."</pre>\n";
61    }
62    else
63    {
64        unset($file);
65
66        // get all names we'll find
67        preg_match_all("^!(.*?)\|(.*?)-^", $data, $matches);
68        $results = count($matches[1]);
69        $update = new DBQuery();
70        for ($i = 0; $i<$results; $i++)
71        {
72            $update->execute("update kb3_pilots set plt_externalid='".addslashes($matches[2][$i])."' where plt_name='".addslashes($matches[1][$i])."' limit 1");
73        }
74        $html .= "Synchronization complete, got $results new ids from server running version ".$version[0].'.'.$version[1].'.'.$version[2].'.<br/>';
75
76        if (isset($_REQUEST['itm_update']))
77        {
78            preg_match_all('^§(.*?)\|(.*?)\|(.*?)-^', $data, $matches);
79            unset($data);
80            $results = count($matches[1]);
81            if (isset($_REQUEST['itm_overwrite']))
82            {
83                $add_qrystr = '';
84            }
85            else
86            {
87                $add_qrystr = ' AND itm_value=0';
88            }
89
90            for ($i = 0; $i<$results; $i++)
91            {
92                $update->execute("update kb3_items set itm_externalid='".addslashes($matches[2][$i])."', itm_value='".addslashes($matches[3][$i])."' where itm_name='".addslashes($matches[1][$i])."'".$add_qrystr." limit 1");
93            }
94            if ($results == 0)
95            {
96                $html .= 'No items fetched, itm_sync_module may be offline.<br/>';
97            }
98            else
99            {
100                $html .= $results.' item prices have been fetched.<br/>';
101            }
102        }
103
104        $html .= "Sent ".round($sended/1024, 2)." kB and received ".round($recv/1024, 2)." kB of data.<br>\n";
105        $html .= '<a href="?a=admin_sync">Back</a>';
106
107        // check for updates here
108        // we might move this to a new/second point some time
109        $ownversion = explode('.', KB_VERSION);
110        if ($version[1] > $ownversion[1] && $version[1] % 2 == 1)
111        {
112            // test for new minor updates below the dev-version
113            if ($version[1]-1 > $ownversion[1])
114            {
115                $upgrade = true;
116            }
117        }
118        elseif ($version[1] > $ownversion[1] && $version[1] % 2 == 0)
119        {
120            // we get here in case there is a new minor version thats not a dev
121            $upgrade = true;
122        }
123        if ($version[0] > $ownversion[0] || $upgrade)
124        {
125            $html .= "Looks like your Killboard version is pretty old, perhaps you want to upgrade it ?<br/>\n";
126            $html .= "Check the <a href='http://www.eve-dev.net/forums/viewforum.php?f=2'>EVE-Dev Forums</a> for new releases and additional information<br>\n";
127        }
128    }
129}
130else
131{
132    $html .= 'You can synchronize your external characterids for the portrait generation with the EVE-Dev.org-Server here.<br>';
133    $html .= 'Your Server will try to contact <i>"'.KB_SYNCURL.'"</i> to exchange the data.<br>';
134    $html .= 'One synchronization every one or two weeks should be enough.<br>';
135    $html .= 'Please don\'t abuse this free service!<br>';
136    $html .= '<form id="options" name="options" method="post" action="?a=admin_sync">';
137    $html .= "<table class=kb-subtable>";
138    $html .= "<tr><td width=120><b>Update item values</b></td><td><input type=checkbox name=itm_update id=itm_update";
139    $html .= " checked=\"checked\"></td></tr>";
140    $html .= "<tr><td width=120><b>Overwrite local values</b></td><td><input type=checkbox name=itm_overwrite id=itm_overwrite";
141    $html .= " checked=\"checked\">(If disabled only values with 0 will be overwritten)</td></tr>";
142    $html .= '<input type="hidden" name="do" value="sync">';
143    $html .= '<tr><td width=120></td><td><input type=submit name=submit value="Synchronize now"></td></tr></table></form>';
144}
145$page->addContext($menubox->generate());
146$page->setContent($html);
147$page->generate();
148?>
Note: See TracBrowser for help on using the browser.