aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/web/parser.php
blob: 4783148f8d856c3e1317356cd4b2d17344a094ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
include("../config/os_modules_mysql.php");


//Supress all Warnings/Errors
//error_reporting(0);

$now = time();

//
// Search DB
//
mysql_connect ($DB_HOST, $DB_USER, $DB_PASSWORD);
mysql_select_db ($DB_NAME);

function GetURL($host, $port, $url)
{
    $url = "http://$host:$port/$url";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);

    $data = curl_exec($ch);
    if (curl_errno($ch) == 0)
    {
        curl_close($ch);
        return $data;
    }

    curl_close($ch);
    return "";
}

function CheckHost($hostname, $port)
{
    global $now;

    $xml = GetURL($hostname, $port, "?method=collector");
    if ($xml == "")	//No data was retrieved? (CURL may have timed out)
        $failcounter = "failcounter + 1";
    else
        $failcounter = "0";

    //Update nextcheck to be 10 minutes from now. The current OS instance
    //won't be checked again until at least this much time has gone by.
    $next = $now + 600;

    mysql_query("UPDATE hostsregister SET nextcheck = $next," .
                " checked = 1, failcounter = " . $failcounter .
                " WHERE host = '" . mysql_real_escape_string($hostname) . "'" . 
                " AND port = '" . mysql_real_escape_string($port) . "'");

    if ($xml != "") 
        parse($hostname, $port, $xml);
}

function parse($hostname, $port, $xml)
{
    global $now;

    ///////////////////////////////////////////////////////////////////////
    //
    // Search engine sim scanner
    //

    //
    // Load XML doc from URL
    //
    $objDOM = new DOMDocument();
    $objDOM->resolveExternals = false;

    //Don't try and parse if XML is invalid or we got an HTML 404 error.
    if ($objDOM->loadXML($xml) == False)
        return;

    //
    // Get the region data to update
    //
    $regiondata = $objDOM->getElementsByTagName("regiondata");

    //If returned length is 0, collector method may have returned an error
    if ($regiondata->length == 0)
        return;

    $regiondata = $regiondata->item(0);

    //
    // Update nextcheck so this host entry won't be checked again until after
    // the DataSnapshot module has generated a new set of data to be parsed.
    //
    $expire = $regiondata->getElementsByTagName("expire")->item(0)->nodeValue;
    $next = $now + $expire;

    $updater = mysql_query("UPDATE hostsregister SET nextcheck = $next " .
            "WHERE host = '" . mysql_real_escape_string($hostname) . "' AND " .
            "port = '" . mysql_real_escape_string($port) . "'");

    //
    // Get the region data to be saved in the database
    //
    $regionlist = $regiondata->getElementsByTagName("region");

    foreach ($regionlist as $region)
    {
        $regioncategory = $region->getAttributeNode("category")->nodeValue;

        //
        // Start reading the Region info
        //
        $info = $region->getElementsByTagName("info")->item(0);

        $regionuuid = $info->getElementsByTagName("uuid")->item(0)->nodeValue;

        $regionname = $info->getElementsByTagName("name")->item(0)->nodeValue;

        $regionhandle = $info->getElementsByTagName("handle")->item(0)->nodeValue;

        $url = $info->getElementsByTagName("url")->item(0)->nodeValue;

        //
        // First, check if we already have a region that is the same
        //
        $check = mysql_query("SELECT * FROM osregions WHERE regionuuid = '" .
                mysql_real_escape_string($regionuuid) . "'");

        if (mysql_num_rows($check) > 0)
        {
            mysql_query("DELETE FROM osregions WHERE regionuuid = '" .
                    mysql_real_escape_string($regionuuid) . "'");
            mysql_query("DELETE FROM parcels WHERE regionuuid = '" .
                    mysql_real_escape_string($regionuuid) . "'");
            mysql_query("DELETE FROM allparcels WHERE regionUUID = '" .
                    mysql_real_escape_string($regionuuid) . "'");
            mysql_query("DELETE FROM parcelsales WHERE regionUUID = '" .
                    mysql_real_escape_string($regionuuid) . "'");
            mysql_query("DELETE FROM objects WHERE regionuuid = '" .
                    mysql_real_escape_string($regionuuid) . "'");
        }

        $data = $region->getElementsByTagName("data")->item(0);
        $estate = $data->getElementsByTagName("estate")->item(0);

        $username = $estate->getElementsByTagName("name")->item(0)->nodeValue;
        $useruuid = $estate->getElementsByTagName("uuid")->item(0)->nodeValue;

        $estateid = $estate->getElementsByTagName("id")->item(0)->nodeValue;

        //
        // Second, add the new info to the database
        //
        $sql = "INSERT INTO osregions VALUES('" .
                mysql_real_escape_string($regionname) . "','" .
                mysql_real_escape_string($regionuuid) . "','" .
                mysql_real_escape_string($regionhandle) . "','" .
                mysql_real_escape_string($url) . "','" .
                mysql_real_escape_string($username) ."','" .
                mysql_real_escape_string($useruuid) ."')";

        mysql_query($sql);

        //
        // Start reading the parcel info
        //
        $parcel = $data->getElementsByTagName("parcel");

        foreach ($parcel as $value)
        {
            $parcelname = $value->getElementsByTagName("name")->item(0)->nodeValue;

            $parceluuid = $value->getElementsByTagName("uuid")->item(0)->nodeValue;

            $infouuid = $value->getElementsByTagName("infouuid")->item(0)->nodeValue;

            $parcellanding = $value->getElementsByTagName("location")->item(0)->nodeValue;

            $parceldescription = $value->getElementsByTagName("description")->item(0)->nodeValue;

            $parcelarea = $value->getElementsByTagName("area")->item(0)->nodeValue;

            $parcelcategory = $value->getAttributeNode("category")->nodeValue;

            $parcelsaleprice = $value->getAttributeNode("salesprice")->nodeValue;

            $dwell = $value->getElementsByTagName("dwell")->item(0)->nodeValue;

            $owner = $value->getElementsByTagName("owner")->item(0);

            $owneruuid = $owner->getElementsByTagName("uuid")->item(0)->nodeValue;

            // Adding support for groups

            $group = $value->getElementsByTagName("group")->item(0);
            
            if ($group != "")
            {
                $groupuuid = $group->getElementsByTagName("groupuuid")->item(0)->nodeValue;
            }
            else
            {
                $groupuuid = "00000000-0000-0000-0000-000000000000";
            }

            //
            // Check bits on Public, Build, Script
            //
            $parcelforsale = $value->getAttributeNode("forsale")->nodeValue;
            $parceldirectory = $value->getAttributeNode("showinsearch")->nodeValue;
            $parcelbuild = $value->getAttributeNode("build")->nodeValue;
            $parcelscript = $value->getAttributeNode("scripts")->nodeValue;
            $parcelpublic = $value->getAttributeNode("public")->nodeValue;

            //
            // Save
            //
            $sql = "INSERT INTO allparcels VALUES('" .
                    mysql_real_escape_string($regionuuid) . "','" .
                    mysql_real_escape_string($parcelname) . "','" .
                    mysql_real_escape_string($owneruuid) . "','" .
                    mysql_real_escape_string($groupuuid) . "','" .
                    mysql_real_escape_string($parcellanding) . "','" .
                    mysql_real_escape_string($parceluuid) . "','" .
                    mysql_real_escape_string($infouuid) . "','" .
                    mysql_real_escape_string($parcelarea) . "' )";

            mysql_query($sql);

            if ($parceldirectory == "true")
            {
                $sql = "INSERT INTO parcels VALUES('" .
                        mysql_real_escape_string($regionuuid) . "','" .
                        mysql_real_escape_string($parcelname) . "','" .
                        mysql_real_escape_string($parceluuid) . "','" .
                        mysql_real_escape_string($parcellanding) . "','" .
                        mysql_real_escape_string($parceldescription) . "','" .
                        mysql_real_escape_string($parcelcategory) . "','" .
                        mysql_real_escape_string($parcelbuild) . "','" .
                        mysql_real_escape_string($parcelscript) . "','" .
                        mysql_real_escape_string($parcelpublic) . "','".
                        mysql_real_escape_string($dwell) . "','" .
                        mysql_real_escape_string($infouuid) . "','" .
                        mysql_real_escape_string($regioncategory) . "')";

                mysql_query($sql);
            }

            if ($parcelforsale == "true")
            {
                $sql = "INSERT INTO parcelsales VALUES('" .
                        mysql_real_escape_string($regionuuid) . "','" .
                        mysql_real_escape_string($parcelname) . "','" .
                        mysql_real_escape_string($parceluuid) . "','" .
                        mysql_real_escape_string($parcelarea) . "','" .
                        mysql_real_escape_string($parcelsaleprice) . "','" .
                        mysql_real_escape_string($parcellanding) . "','" .
                        mysql_real_escape_string($infouuid) . "', '" .
                        mysql_real_escape_string($dwell) . "', '" .
                        mysql_real_escape_string($estateid) . "', '" .
                        mysql_real_escape_string($regioncategory) . "')";

                mysql_query($sql);
            }
        }

        //
        // Handle objects
        //
        $objects = $data->getElementsByTagName("object");

        foreach ($objects as $value)
        {
            $uuid = $value->getElementsByTagName("uuid")->item(0)->nodeValue;

            $regionuuid = $value->getElementsByTagName("regionuuid")->item(0)->nodeValue;

            $parceluuid = $value->getElementsByTagName("parceluuid")->item(0)->nodeValue;

            $location = $value->getElementsByTagName("location")->item(0)->nodeValue;

            $title = $value->getElementsByTagName("title")->item(0)->nodeValue;

            $description = $value->getElementsByTagName("description")->item(0)->nodeValue;

            $flags = $value->getElementsByTagName("flags")->item(0)->nodeValue;

            mysql_query("INSERT INTO objects VALUES('" .
                    mysql_real_escape_string($uuid) . "','" .
                    mysql_real_escape_string($parceluuid) . "','" .
                    mysql_real_escape_string($location) . "','" .
                    mysql_real_escape_string($title) . "','" .
                    mysql_real_escape_string($description) . "','" .
                    mysql_real_escape_string($regionuuid) . "')");
        }
    }
}

$sql = "SELECT host, port FROM hostsregister " .
        "WHERE nextcheck < $now AND checked = 0 LIMIT 0,10";

$jobsearch = mysql_query($sql);

//
// If the sql query returns no rows, all entries in the hostsregister
// table have been checked. Reset the checked flag and re-run the
// query to select the next set of hosts to be checked.
//
if (mysql_num_rows($jobsearch) == 0)
{
    mysql_query("UPDATE hostsregister SET checked = 0");
    $jobsearch = mysql_query($sql);
}

while ($jobs = mysql_fetch_row($jobsearch))
    CheckHost($jobs[0], $jobs[1]);
?>